1 #ifndef NETCACHED__HPP
2 #define NETCACHED__HPP
3 
4 /*  $Id: netcached.hpp 573862 2018-11-02 14:40:17Z gouriano $
5  * ===========================================================================
6  *
7  *                            PUBLIC DOMAIN NOTICE
8  *               National Center for Biotechnology Information
9  *
10  *  This software/database is a "United States Government Work" under the
11  *  terms of the United States Copyright Act.  It was written as part of
12  *  the author's official duties as a United States Government employee and
13  *  thus cannot be copyrighted.  This software/database is freely available
14  *  to the public for use. The National Library of Medicine and the U.S.
15  *  Government have not placed any restriction on its use or reproduction.
16  *
17  *  Although all reasonable efforts have been taken to ensure the accuracy
18  *  and reliability of the software and data, the NLM and the U.S.
19  *  Government do not and cannot warrant the performance or results that
20  *  may be obtained by using this software or data. The NLM and the U.S.
21  *  Government disclaim all warranties, express or implied, including
22  *  warranties of performance, merchantability or fitness for any particular
23  *  purpose.
24  *
25  *  Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors:  Pavel Ivanov
30  *
31  * File Description: Network cache daemon
32  *
33  */
34 
35 
36 #include "nc_utils.hpp"
37 
38 
39 BEGIN_NCBI_SCOPE
40 
41 
42 struct SNCStateStat;
43 
44 
45 /// Policy for accepting passwords for reading and writing blobs
46 enum ENCBlobPassPolicy {
47     eNCBlobPassAny,     ///< Both blobs with password and without are accepted
48     eNCOnlyWithPass,    ///< Only blobs with password are accepted
49     eNCOnlyWithoutPass  ///< Only blobs without password are accepted
50 };
51 
52 
53 struct SNCSpecificParams : public CObject
54 {
55     map<string,string> source;
56     map<string,string> keys;
57     bool  disable;
58     bool  prolong_on_read;
59     bool  srch_on_read;
60     bool  fast_on_main;
61     ENCBlobPassPolicy pass_policy;
62     //Uint4 conn_timeout;
63     //Uint4 cmd_timeout;
64     Uint4 lifespan_ttl;
65     Uint4 max_ttl;
66     Uint4 blob_ttl;
67     Uint4 ver_ttl;
68     Uint2 ttl_unit;
69     Uint1 quorum;
70 
SNCSpecificParamsSNCSpecificParams71     SNCSpecificParams()
72       : disable(false), prolong_on_read(false), srch_on_read(false), fast_on_main(false),
73         pass_policy(eNCBlobPassAny), lifespan_ttl(0), max_ttl(0), blob_ttl(0), ver_ttl(0), ttl_unit(0), quorum(0)
74     {
75     }
SNCSpecificParamsSNCSpecificParams76     SNCSpecificParams(const SNCSpecificParams& o)
77       : source(o.source), keys(o.keys), disable(o.disable), prolong_on_read(o.prolong_on_read),
78         srch_on_read(o.srch_on_read), fast_on_main(o.fast_on_main),
79         pass_policy(o.pass_policy),
80         lifespan_ttl(o.lifespan_ttl), max_ttl(o.max_ttl), blob_ttl(o.blob_ttl), ver_ttl(o.ver_ttl), ttl_unit(o.ttl_unit),
81         quorum(o.quorum)
82     {
83     }
84     virtual ~SNCSpecificParams(void);
85 };
86 
87 /////////////////////////////////////////////////////////////////////////////
88 // CNCBlobKeyLight
89 class CNCBlobKeyLight
90 {
91 public:
92     CNCBlobKeyLight(void);
93     CNCBlobKeyLight(const CNCBlobKeyLight& another);
94     CNCBlobKeyLight& operator=(const CNCBlobKeyLight& another);
95     CNCBlobKeyLight(const string& packed_key);
96     CNCBlobKeyLight& operator=(const string& packed_key);
97     CNCBlobKeyLight(const CTempString& packed_key);
98     CNCBlobKeyLight& operator=(const CTempString& packed_key);
99 
100     CNCBlobKeyLight& Copy(const CNCBlobKeyLight& another);
101 
102     void Clear(void);
103     bool IsValid(void) const;
104 
105     unsigned int KeyVersion(void) const;
106     bool IsICacheKey(void) const;
107     const string& PackedKey(void) const;
108     const CTempString& RawKey(void) const;
109     const CTempString& SubKey(void) const;
110     const CTempString& Cache(void) const;
111     string KeyForLogs(void) const;
112 
113 protected:
114     void PackBlobKey(const CTempString& cache_name,
115                      const CTempString& blob_key,
116                      const CTempString& blob_subkey);
117     void UnpackBlobKey(void);
118     void SetKeyVersion(unsigned int v);
119 
120 private:
121     string m_PackedKey;
122     CTempString m_Cachename;
123     CTempString m_RawKey;
124     CTempString m_SubKey;
125     unsigned int m_KeyVersion;
126 };
127 
128 /////////////////////////////////////////////////////////////////////////////
129 // CNCBlobKey
130 class CNCBlobKey : public CNCBlobKeyLight,
131                    public CNetCacheKey
132 {
133 public:
134     CNCBlobKey(void);
135     void Assign( const CTempString& cache_name,
136                  const CTempString& blob_key,
137                  const CTempString& blob_subkey);
138     void Assign( const CTempString& blob_key);
139 
140 private:
141     CNCBlobKey(const CNCBlobKey& another);
142     CNCBlobKey& operator=(const CNCBlobKey& another);
143     CNCBlobKey(const string& packed_key);
144     CNCBlobKey& operator=(const string& packed_key);
145     CNCBlobKey(const CTempString& packed_key);
146     CNCBlobKey& operator=(const CTempString& packed_key);
147     CNCBlobKey& Copy(const CNCBlobKey& another);
148 };
149 
150 /////////////////////////////////////////////////////////////////////////////
151 inline
CNCBlobKeyLight(void)152 CNCBlobKeyLight::CNCBlobKeyLight(void) {
153     Clear();
154 }
155 inline
CNCBlobKeyLight(const CNCBlobKeyLight & another)156 CNCBlobKeyLight::CNCBlobKeyLight(const CNCBlobKeyLight& another) {
157     Copy(another);
158 }
159 inline
operator =(const CNCBlobKeyLight & another)160 CNCBlobKeyLight& CNCBlobKeyLight::operator=(const CNCBlobKeyLight& another) {
161     return Copy(another);
162 }
163 inline
CNCBlobKeyLight(const string & packed_key)164 CNCBlobKeyLight::CNCBlobKeyLight(const string& packed_key) {
165     *this = CTempString(packed_key);
166 }
167 inline
operator =(const string & packed_key)168 CNCBlobKeyLight& CNCBlobKeyLight::operator=(const string& packed_key) {
169     return *this = CTempString(packed_key);
170 }
171 inline
CNCBlobKeyLight(const CTempString & packed_key)172 CNCBlobKeyLight::CNCBlobKeyLight(const CTempString& packed_key) {
173     *this = packed_key;
174 }
175 inline
IsICacheKey(void) const176 bool CNCBlobKeyLight::IsICacheKey(void) const {
177     return !m_Cachename.empty();
178 }
179 inline
PackedKey(void) const180 const string& CNCBlobKeyLight::PackedKey(void) const {
181     return m_PackedKey;
182 }
183 inline
RawKey(void) const184 const CTempString& CNCBlobKeyLight::RawKey(void) const {
185     return m_RawKey;
186 }
187 inline
SubKey(void) const188 const CTempString& CNCBlobKeyLight::SubKey(void) const {
189     return m_SubKey;
190 }
191 inline
Cache(void) const192 const CTempString& CNCBlobKeyLight::Cache(void) const {
193     return m_Cachename;
194 }
195 inline
IsValid(void) const196 bool CNCBlobKeyLight::IsValid(void) const {
197     return !RawKey().empty();
198 }
199 inline
SetKeyVersion(unsigned int v)200 void CNCBlobKeyLight::SetKeyVersion(unsigned int v) {
201     m_KeyVersion = v;
202 }
203 inline
CNCBlobKey(void)204 CNCBlobKey::CNCBlobKey(void) {
205 }
206 inline
Assign(const CTempString & blob_key)207 void CNCBlobKey::Assign( const CTempString& blob_key) {
208     Assign( CTempString(), blob_key, CTempString());
209 }
210 
211 /////////////////////////////////////////////////////////////////////////////
212 /// Netcache server
213 class CNCServer
214 {
215 public:
216     static const SNCSpecificParams* GetAppSetup(const TStringMap& client_params);
217     static void WriteAppSetup(TNCBufferType& task, const TStringMap& client);
218     static void WriteAppSetup(TNCBufferType& task, const SNCSpecificParams* app);
219     static void WriteEnvInfo(CSrvSocketTask& task);
220 
221     /// Get inactivity timeout for each connection
222     //static unsigned GetDefConnTimeout(void);
223     static int GetDefBlobTTL(void);
224     /// Get name of client that should be used for administrative commands
225     static const string& GetAdminClient(void);
226 
227     static string GetHostRole(void);
228     static string GetHostLocation(void);
229 
230     /// Get total number of seconds the server is running
231     static int GetUpTime(void);
232     static void CachingCompleted(void);
233     static bool IsInitiallySynced(void);
234     static void InitialSyncComplete(void);
235     static void InitialSyncRequired(void);
236     static bool IsCachingComplete(void);
237     static bool IsDebugMode(void);
238 
239     static void ReadCurState(SNCStateStat& state);
240 
241 private:
242     CNCServer(void);
243 };
244 
245 
246 class CNCHeartBeat : public CSrvTask
247 {
248 public:
249     CNCHeartBeat(void);
250     virtual ~CNCHeartBeat(void);
251 
252     static void CheckConfFile(void);
253 private:
254     virtual void ExecuteSlice(TSrvThreadNum thr_num);
255 };
256 
257 
258 END_NCBI_SCOPE
259 
260 #endif /* NETCACHED__HPP */
261