1 #ifndef NETCACHE__NC_STORAGE__HPP
2 #define NETCACHE__NC_STORAGE__HPP
3 /*  $Id: nc_storage.hpp 577955 2019-01-10 14:14:00Z gouriano $
4  * ===========================================================================
5  *
6  *                            PUBLIC DOMAIN NOTICE
7  *               National Center for Biotechnology Information
8  *
9  *  This software/database is a "United States Government Work" under the
10  *  terms of the United States Copyright Act.  It was written as part of
11  *  the author's official duties as a United States Government employee and
12  *  thus cannot be copyrighted.  This software/database is freely available
13  *  to the public for use. The National Library of Medicine and the U.S.
14  *  Government have not placed any restriction on its use or reproduction.
15  *
16  *  Although all reasonable efforts have been taken to ensure the accuracy
17  *  and reliability of the software and data, the NLM and the U.S.
18  *  Government do not and cannot warrant the performance or results that
19  *  may be obtained by using this software or data. The NLM and the U.S.
20  *  Government disclaim all warranties, express or implied, including
21  *  warranties of performance, merchantability or fitness for any particular
22  *  purpose.
23  *
24  *  Please cite the author in any work or product based on this material.
25  *
26  * ===========================================================================
27  *
28  * Authors:  Pavel Ivanov
29  */
30 
31 
32 #include "nc_utils.hpp"
33 #include "nc_db_info.hpp"
34 
35 
36 namespace intr = boost::intrusive;
37 
38 
39 BEGIN_NCBI_SCOPE
40 
41 
42 class CNCBlobAccessor;
43 struct SNCStateStat;
44 class CNCPeerControl;
45 
46 
47 struct STimeTable_tag;
48 struct SKeyMap_tag;
49 
50 typedef intr::set_base_hook< intr::tag<STimeTable_tag>,
51                              intr::optimize_size<true> >    TTimeTableHook;
52 typedef intr::set_base_hook< intr::tag<SKeyMap_tag>,
53                              intr::optimize_size<true> >    TKeyMapHook;
54 
55 #define __NC_CACHEDATA_MONITOR     0
56 
57 class SNCCacheData : public TTimeTableHook,
58                       public TKeyMapHook,
59                       public SNCBlobSummary,
60                       public CSrvRCUUser
61 {
62 public:
63     SNCDataCoord coord;
64     string key;
65     int saved_dead_time;
66     Uint2 time_bucket;
67     Uint2 map_size;
68     Uint4 chunk_size;
69     CAtomicCounter_WithAutoInit ref_cnt;
70     CMiniMutex lock;
71 
72     SNCCacheData(void);
73     ~SNCCacheData(void);
74 
Get_ver_mgr(void) const75     CNCBlobVerManager* Get_ver_mgr(void) const {
76         return  ver_mgr;
77     }
78 
79 private:
80     CNCBlobVerManager* ver_mgr;
81 
82     SNCCacheData(const SNCCacheData&);
83     SNCCacheData& operator= (const SNCCacheData&);
84 
85 #if __NC_CACHEDATA_MONITOR
86     void x_Register(void);
87     void x_Revoke(void);
88 #endif
89 
90     virtual void ExecuteRCU(void);
91     friend class CNCBlobVerManager;
92 };
93 
94 
95 /// Blob storage for NetCache
96 class CNCBlobStorage
97 {
98 public:
99     static void GetBList(const string& mask, auto_ptr<TNCBufferType>& buffer,
100                          SNCBlobFilter* filters, const string& sep);
101     static string FindBlob(Uint2 bucket, const string& mask, Uint8 cr_time_hi);
102 
103     static bool Initialize(bool do_reinit);
104     static void Finalize(void);
105 
106     static bool ReConfig(const CNcbiRegistry& new_reg, string& err_message);
107     static void WriteSetup(CSrvSocketTask& task);
108     static void WriteEnvInfo(CSrvSocketTask& task);
109     static void WriteBlobStat(CSrvSocketTask& task);
110     static void WriteBlobList(TNCBufferType& buffer, const CTempString& mask);
111     static void WriteDbInfo(TNCBufferType& sendBuff, const CTempString& mask);
112 
113     static bool IsCleanStart(void);
114     static bool NeedStopWrite(void);
115     static bool AcceptWritesFromPeers(void);
116     static void SetDraining(bool draining);
117     static bool IsDraining(void);
118     static void AbandonDB(void);
119     static bool IsAbandoned(void);
120 
121     static string PrintablePassword(const string& pass);
122     /// Acquire access to the blob identified by key, subkey and version
123     static CNCBlobAccessor* GetBlobAccess(ENCAccessType access,
124                                           const string& key,
125                                           const string& password,
126                                           Uint2         time_bucket);
127     static Uint4 GetNewBlobId(void);
128 
129     /// Get number of files in the database
130     static size_t GetNDBFiles(void);
131     /// Get total size of database for the storage
132     static Int8 GetDBSize(void);
133     static Int8 GetDiskFree(void);
134     static Int8 GetAllowedDBSize(Int8 free_space);
135     static bool IsDBSizeAlert(void);
136     static void CheckDiskSpace(void);
137     static void MeasureDB(SNCStateStat& state);
138 
139     static int GetLatestBlobExpire(void);
140     static void GetFullBlobsList(Uint2 slot, TNCBlobSumList& blobs_lst, const CNCPeerControl* peer);
141     static Uint8 GetMaxSyncLogRecNo(void);
142     static void SaveMaxSyncLogRecNo(void);
143 
144     static string GetPurgeData(void);
145     static void SavePurgeData(void);
146 
147     static Uint8 GetMaxBlobSizeStore(void);
148 public:
149     // For internal use only
150 
151     /// Get meta information about blob with given coordinates.
152     ///
153     /// @return
154     ///   TRUE if meta information exists in the database and was successfully
155     ///   read. FALSE if database is corrupted.
156     static bool ReadBlobInfo(SNCBlobVerData* ver_data);
157     /// Write meta information about blob into the database.
158     static bool WriteBlobInfo(const string& blob_key,
159                               SNCBlobVerData* ver_data,
160                               SNCChunkMaps* maps,
161                               Uint8 cnt_chunks,
162                               SNCCacheData* cache_data);
163     /// Delete blob from database.
164     static void DeleteBlobInfo(const SNCBlobVerData* ver_data,
165                                SNCChunkMaps* maps);
166 
167     static bool ReadChunkData(SNCBlobVerData* ver_data,
168                               SNCChunkMaps* maps,
169                               Uint8 chunk_num,
170                               char*& buffer,
171                               Uint4& buf_size);
172     static char* WriteChunkData(SNCBlobVerData* ver_data,
173                                 SNCChunkMaps* maps,
174                                 SNCCacheData* cache_data,
175                                 Uint8 chunk_num,
176                                 char* buffer,
177                                 Uint4 buf_size);
178 
179     static void ReferenceCacheData(SNCCacheData* cache_data);
180     static void ReleaseCacheData(SNCCacheData* cache_data);
181     static void ChangeCacheDeadTime(SNCCacheData* cache_data);
182 
183 private:
184     CNCBlobStorage(void);
185 };
186 
187 
188 
189 //////////////////////////////////////////////////////////////////////////
190 //  Inline functions
191 //////////////////////////////////////////////////////////////////////////
192 inline
SNCCacheData(void)193 SNCCacheData::SNCCacheData(void)
194     : saved_dead_time(0),
195       time_bucket(0),
196       map_size(0),
197       chunk_size(0),
198       ver_mgr(NULL)
199 {
200 #if __NC_CACHEDATA_MONITOR
201     x_Register();
202 #endif
203 }
204 
205 inline
~SNCCacheData(void)206 SNCCacheData::~SNCCacheData(void)
207 {
208 #ifdef _DEBUG
209     if (ver_mgr || !coord.empty() || ref_cnt.Get() != 0) {
210         abort();
211     }
212 #endif
213 #if __NC_CACHEDATA_MONITOR
214     x_Revoke();
215 #endif
216 }
217 
218 END_NCBI_SCOPE
219 
220 #endif /* NETCACHE__NC_STORAGE__HPP */
221