NCBI C++ ToolKit
message_handler.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef NETCACHE__MESSAGE_HANDLER__HPP
2 #define NETCACHE__MESSAGE_HANDLER__HPP
3 /* $Id: message_handler.hpp 92088 2020-12-21 15:51:01Z 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  * File Description: Network cache daemon
31  *
32  */
33 
35 
36 #include "nc_utils.hpp"
37 
38 
40 
41 
42 class CNCActiveClientHub;
43 class CNCBlobAccessor;
44 struct SNCSpecificParams;
45 struct SNCBlobVerData;
46 struct SNCBlobSummary;
47 struct SNCBlobFilter;
48 struct SNCSyncEvent;
49 
50 
53  /// Command needs access to the blob.
54  fNeedsBlobAccess = 1 << 0,
55  /// Command needs to generate blob key if it's empty.
56  fCanGenerateKey = 1 << 1,
57  /// Command needs finished database caching to be executed locally.
59  /// Do not check access password during command execution.
61  /// Always execute the command locally without proxying to peers.
63  /// Command needs to search for the latest version of the blob on all servers.
64  fUsesPeerSearch = 1 << 5,
65  /// During the blob search on other servers command needs to know only
66  /// whether this blob exists or not, i.e. first positive response
67  /// of existence should finish the search.
69  /// Command comes from client and needs disk space to execute.
71  /// Command comes from other NC server and needs disk space to execute.
73  /// Byte order should be swapped when reading length of chunks in blob
74  /// transfer protocol.
75  fSwapLengthBytes = 1 << 9,
76  /// Command does not need a confirmation at the end of execution.
77  /// Usually, all commands need a reply,
78  /// but storing blob (STOR) may go without (in 'performance' mode)
79  fNoReplyOnFinish = 1 << 10,
80  /// There is exact size of the blob transferred to NetCache.
81  fReadExactBlobSize = 1 << 11,
82  /// Client will send blob data for the command and won't send EOF marker.
83  fSkipBlobEOF = 1 << 12,
84  /// Command copies sync log event from other server, not creates a new one.
85  fCopyLogEvent = 1 << 13,
86  /// Command can be executed only by admin client.
87  fNeedsAdminClient = 1 << 14,
88  /// Command can be executed only after successful execution of SYNC_START.
89  fRunsInStartedSync = 1 << 15,
90  /// Command should be executed even if server wants to abort
91  /// the synchronization.
93  /// Command should be executed with a lower priority.
95  /// Command shouldn't check the blob version (it doesn't come with
96  /// the command).
98  /// Access information about blob shouldn't be printed at the end
99  /// of the command.
101  /// Consider synchronization command to be successful even though
102  /// RequestStatus is not eStatus_OK.
104  /// This is PUT2 command and connection that used PUT2 command.
105  fCursedPUT2Cmd = 1 << 21,
106  /// Command comes from client, not from other NC server.
107  fComesFromClient = 1 << 22,
108  /// HTTP-related.
109  fIsHttp = 1 << 23,
110  /// Command needs access to the blob list.
111  fNeedsBlobList = 1 << 24,
112 
113 
125 };
127 
130  /// Command does not update blob expiration time
131  fNoProlong = 1 << 0,
132  /// Command does not create blob if it does not exist
133  fNoCreate = 1 << 1
134 };
136 
151 
152 /// Handler of all NetCache incoming requests.
153 /// Handler written to be reusable object so that if one connection to
154 /// NetCache is closed then handler from it can go to serve for another newly
155 /// opened connection.
157  public CSrvStatesTask<CNCMessageHandler>
158 {
159 public:
160  /// Create handler for the given NetCache server object
161  CNCMessageHandler(void);
162  virtual ~CNCMessageHandler(void);
163 
164  bool IsBlobWritingFinished(void);
165 
166  /// Extra information about each NetCache command
167  struct SCommandExtra {
168  /// Which method will process the command
169  State processor;
170  /// Command name to present to user in statistics
171  const char* cmd_name;
173  /// What access to the blob command should receive
176  };
177  /// Type definitions for NetCache protocol parser
181 
182  /// Command processors
183  State x_DoCmd_Health(void);
184  State x_DoCmd_Shutdown(void);
185  State x_DoCmd_Version(void);
186  State x_DoCmd_GetConfig(void);
187  State x_DoCmd_AckAlert(void);
188  State x_DoCmd_ReConfig(void);
189  State x_DoCmd_GetStat(void);
190  State x_DoCmd_Put(void);
191  State x_DoCmd_Get(void);
192  State x_DoCmd_GetLast(void);
193  State x_DoCmd_SetValid(void);
194  State x_DoCmd_GetSize(void);
195  State x_DoCmd_Prolong(void);
196  State x_DoCmd_HasBlob(void);
197  State x_DoCmd_Remove(void);
198  State x_DoCmd_IC_Store(void);
199  State x_DoCmd_SyncStart(void);
200  State x_DoCmd_SyncBlobsList(void);
201  State x_DoCmd_CopyPut(void);
202  State x_DoCmd_CopyProlong(void);
203  State x_DoCmd_SyncGet(void);
204  State x_DoCmd_SyncProlongInfo(void);
205  State x_DoCmd_SyncCommit(void);
206  State x_DoCmd_SyncCancel(void);
207  State x_DoCmd_GetMeta(void);
208  State x_DoCmd_ProxyMeta(void);
209  State x_DoCmd_CopyUpdate(void);
210  //State x_DoCmd_GetBlobsList(void);
211  /// Universal processor for all commands not implemented now.
212  State x_DoCmd_NotImplemented(void);
213 
214  State x_DoCmd_Purge(void);
215  State x_DoCmd_CopyPurge(void);
216 
217  State x_DoCmd_GetBList(void);
218  State x_DoCmd_GetBListNext(void);
219 
220  State x_FinishCommand(void);
221 
222 private:
223  void x_ResetFlags(void);
224  /// Set additional machine state flag
225  void x_SetFlag (ENCCmdFlags flag);
226  /// Remove additional machine state flag
227  void x_UnsetFlag(ENCCmdFlags flag);
228  /// Check if additional machine state flag is set
229  bool x_IsFlagSet(ENCCmdFlags flag);
230  bool x_IsUserFlagSet(ENCUserFlags flag);
231  bool x_IsCmdSucceeded(int cmd_status);
232  unsigned int x_GetBlobTTL(void);
233 
234  // State machine implementation
235 
236  State x_SocketOpened(void);
237  State x_CloseCmdAndConn(void);
238  State x_SaveStatsAndClose(void);
239  State x_PrintCmdsCntAndClose(void);
240  /// Read authentication message from client
241  State x_ReadAuthMessage(void);
242  /// Read command and start it if it's available
243  State x_ReadCommand(void);
244  /// Process "waiting" for blob locking. In fact just shift to next state
245  /// if lock is acquired and just return if not.
246  State x_WaitForBlobAccess(void);
247  State x_ReportBlobNotFound(void);
248  State x_CloseOnPeerError(void);
249  /// Read signature in blob transfer protocol
250  State x_ReadBlobSignature(void);
251  /// Read length of chunk in blob transfer protocol
252  State x_ReadBlobChunkLength(void);
253  /// Read chunk data in blob transfer protocol
254  State x_ReadBlobChunk(void);
255  /// Write data from blob to socket
256  State x_WriteBlobData(void);
257  State x_WriteSendBuff(void);
258  State x_WriteSyncStartExtra(void);
259  State x_ProxyToNextPeer(void);
260  State x_SendCmdAsProxy(void);
261  State x_WaitForPeerAnswer(void);
262  State x_WriteInitWriteResponse(void);
263  State x_ReadMetaNextPeer(void);
264  State x_SendGetMetaCmd(void);
265  State x_ReadMetaResults(void);
266  State x_ExecuteOnLatestSrvId(void);
267  State x_PutToNextPeer(void);
268  State x_SendPutToPeerCmd(void);
269  State x_ReadPutResults(void);
270  State x_PurgeToNextPeer(void);
271  State x_SendPurgeToPeerCmd(void);
272  State x_ReadPurgeResults(void);
273 
274  /// Close connection (or at least make CServer believe that we closed it
275  /// by ourselves)
276  void x_CloseConnection(void);
277  /// Assign command parameters returned by protocol parser to handler
278  /// member variables
279  void x_AssignCmdParams(void);
280  /// Print "request_start" message into diagnostics with all parameters of
281  /// the current command.
282  void x_PrintRequestStart(CSrvDiagMsg& diag_msg);
283  /// Start command returned by protocol parser
284  State x_StartCommand(void);
285  /// Command execution is finished, do cleanup work
286  void x_CleanCmdResources(void);
287  /// Start reading blob from socket
288  State x_StartReadingBlob(void);
289  /// Client finished sending blob, do cleanup
290  State x_FinishReadingBlob(void);
291 
292  void x_ProlongBlobDeadTime(unsigned int add_time);
293  void x_ProlongVersionLife(void);
294  void x_WriteFullBlobsList(void);
295  void x_GetCurSlotServers(void);
296 
297  void x_JournalBlobPutResult(int status, const string& blob_key, Uint2 blob_slot);
298  void x_ReportError( EHTTPStatus sts, bool eol = true);
299  void x_ReportError( const string& sts, bool eol = true);
300  CNCMessageHandler& x_ReportOK(const string& sts);
301  void x_LogCmdEvent( const CTempString& evt);
302  void x_LogCmdLog(void);
303 
306  /// NetCache protocol parser
309  /// Processor for the currently executed NetCache command
311  ///
313  /// Holder of the lock for blob
316  /// Length of the current blob chunk to read from socket
318 
320  ///
323  ///
325  ///
327  ///
328  string m_PrevCache;
329  ///
331  /// Time when command started execution
334  vector<string> m_CmdLog;
335 
336  /// Blob key in current command
338  /// Blob version in current command
340  /// "Password" to access the blob
341  string m_BlobPass;
342  /// Version of the blob key to generate
343  unsigned int m_KeyVersion;
344  /// Time-to-live value for the blob
345  unsigned int m_BlobTTL;
346  /// Offset to start blob reading from
348  /// Exact size of the blob sent by client
359  unique_ptr<TNCBufferType> m_SendBuff;
360  size_t m_SendPos;
379  string m_StatType;
384 
386  enum EHttpMode {
387  eNoHttp = 0,
388  eHttp10 = 1,
389  eHttp11 = 2
391 
392  bool x_IsHttpMode(void) const {
393  return m_HttpMode != eNoHttp;
394  }
395  void x_WriteHttpResponse(void);
396  void x_WriteHttpHeader(int cmd_status, size_t content_length, bool binary);
397 public:
398  void BeginProxyResponse(const CTempString& response, size_t content_length);
399 };
400 
401 
403 {
404 public:
405  CNCMsgHandler_Factory(void);
406  virtual ~CNCMsgHandler_Factory(void);
407 
408  virtual CSrvSocketTask* CreateSocketTask(void);
409 };
410 
411 
412 
413 //////////////////////////////////////////////////////////////////////////
414 // Inline functions
415 //////////////////////////////////////////////////////////////////////////
416 
417 inline bool
419 {
420  return m_ChunkLen == 0xFFFFFFFF;
421 }
422 
424 
425 #endif /* NETCACHE__MESSAGE_HANDLER__HPP */
Handler of all NetCache incoming requests.
SNSProtoParsedCmd< SCommandExtra > SParsedCmd
State x_ProxyToNextPeer(void)
SNCBlobFilter * m_BlobFilter
unsigned int m_KeyVersion
Version of the blob key to generate.
State x_CloseOnPeerError(void)
State x_DoCmd_SyncProlongInfo(void)
vector< string > m_CmdLog
State x_DoCmd_Shutdown(void)
Uint4 m_ChunkLen
Length of the current blob chunk to read from socket.
State x_DoCmd_GetSize(void)
State x_ReadBlobSignature(void)
Read signature in blob transfer protocol.
State x_DoCmd_HasBlob(void)
Uint8 m_StartPos
Offset to start blob reading from.
unique_ptr< TNCBufferType > m_SendBuff
State x_SendPutToPeerCmd(void)
int m_BlobVersion
Blob version in current command.
void x_ProlongVersionLife(void)
State x_SendPurgeToPeerCmd(void)
unsigned int m_BlobTTL
Time-to-live value for the blob.
State x_ReadAuthMessage(void)
Read authentication message from client.
void x_GetCurSlotServers(void)
State x_ReadBlobChunkLength(void)
Read length of chunk in blob transfer protocol.
State x_ReadCommand(void)
Read command and start it if it's available.
bool x_IsHttpMode(void) const
State x_DoCmd_Health(void)
Command processors.
void x_CloseConnection(void)
Close connection (or at least make CServer believe that we closed it by ourselves)
const SNCSpecificParams * m_BaseAppSetup
State x_ReadMetaNextPeer(void)
bool IsBlobWritingFinished(void)
void x_ProlongBlobDeadTime(unsigned int add_time)
State x_WaitForBlobAccess(void)
Process "waiting" for blob locking.
State x_FinishReadingBlob(void)
Client finished sending blob, do cleanup.
State x_ReadPutResults(void)
bool x_IsFlagSet(ENCCmdFlags flag)
Check if additional machine state flag is set.
State x_WriteSyncStartExtra(void)
State x_ReadPurgeResults(void)
void BeginProxyResponse(const CTempString &response, size_t content_length)
CNCActiveClientHub * m_ActiveHub
State x_PrintCmdsCntAndClose(void)
State x_DoCmd_GetBList(void)
State x_DoCmd_GetBListNext(void)
State x_DoCmd_SyncCancel(void)
const SNCSpecificParams * m_PrevAppSetup
State x_DoCmd_GetConfig(void)
State x_PurgeToNextPeer(void)
CNCMessageHandler(void)
Create handler for the given NetCache server object.
State x_SocketOpened(void)
State x_DoCmd_SyncGet(void)
CNCBlobKey m_NCBlobKey
Blob key in current command.
void x_ReportError(EHTTPStatus sts, bool eol=true)
TNCUserFlags m_UserFlags
bool x_IsUserFlagSet(ENCUserFlags flag)
State x_WriteInitWriteResponse(void)
SNCBlobVerData * m_CopyBlobInfo
SNCBlobSummary * m_LatestBlobSum
State x_DoCmd_NotImplemented(void)
Universal processor for all commands not implemented now.
void x_SetFlag(ENCCmdFlags flag)
Set additional machine state flag.
void x_WriteHttpResponse(void)
TProtoParser m_Parser
NetCache protocol parser.
set< Uint2 > m_SlotsDone
State x_ReadMetaResults(void)
State x_ReportBlobNotFound(void)
State x_DoCmd_Version(void)
void x_JournalBlobPutResult(int status, const string &blob_key, Uint2 blob_slot)
TServersList m_CheckSrvs
bool x_IsCmdSucceeded(int cmd_status)
State x_ReadBlobChunk(void)
Read chunk data in blob transfer protocol.
SNCSyncEvent * m_write_event
State x_StartReadingBlob(void)
Start reading blob from socket.
TServersList m_MirrorsDone
State x_SaveStatsAndClose(void)
State x_DoCmd_CopyPut(void)
const SNCSpecificParams * m_AppSetup
State x_DoCmd_GetLast(void)
State x_CloseCmdAndConn(void)
State x_SendGetMetaCmd(void)
void x_PrintRequestStart(CSrvDiagMsg &diag_msg)
Print "request_start" message into diagnostics with all parameters of the current command.
string m_BlobPass
"Password" to access the blob
State m_CmdProcessor
Processor for the currently executed NetCache command.
State x_WriteSendBuff(void)
State x_DoCmd_Remove(void)
State x_DoCmd_GetStat(void)
State x_DoCmd_SyncBlobsList(void)
CSrvTime m_CmdStartTime
Time when command started execution.
void x_LogCmdEvent(const CTempString &evt)
State x_DoCmd_IC_Store(void)
CNCMessageHandler & x_ReportOK(const string &sts)
State x_DoCmd_CopyPurge(void)
State x_FinishCommand(void)
State x_SendCmdAsProxy(void)
void x_AssignCmdParams(void)
Assign command parameters returned by protocol parser to handler member variables.
unsigned int x_GetBlobTTL(void)
void x_WriteHttpHeader(int cmd_status, size_t content_length, bool binary)
Uint8 m_Size
Exact size of the blob sent by client.
State x_DoCmd_AckAlert(void)
SNSProtoCmdDef< SCommandExtra > SCommandDef
Type definitions for NetCache protocol parser.
virtual ~CNCMessageHandler(void)
State x_WriteBlobData(void)
Write data from blob to socket.
State x_DoCmd_CopyProlong(void)
void x_WriteFullBlobsList(void)
State x_WaitForPeerAnswer(void)
State x_DoCmd_ProxyMeta(void)
void x_CleanCmdResources(void)
Command execution is finished, do cleanup work.
State x_DoCmd_ReConfig(void)
State x_DoCmd_SyncCommit(void)
void x_UnsetFlag(ENCCmdFlags flag)
Remove additional machine state flag.
State x_ExecuteOnLatestSrvId(void)
State x_DoCmd_CopyUpdate(void)
enum CNCMessageHandler::EHttpMode m_HttpMode
State x_DoCmd_SetValid(void)
CNetServProtoParser< SCommandExtra > TProtoParser
State x_PutToNextPeer(void)
State x_DoCmd_SyncStart(void)
State x_DoCmd_Prolong(void)
CNCBlobAccessor * m_BlobAccess
Holder of the lock for blob.
State x_StartCommand(void)
Start command returned by protocol parser.
State x_DoCmd_GetMeta(void)
virtual ~CNCMsgHandler_Factory(void)
virtual CSrvSocketTask * CreateSocketTask(void)
Class used in all diagnostic logging.
Definition: srv_diag.hpp:73
Factory that creates CSrvSocketTask-derived object for each connection coming to listening port which...
Task controlling a socket.
Special task which executes as finite state machine.
Definition: srv_tasks.hpp:283
Class incorporating convenient methods to work with struct timespec.
Definition: srv_time.hpp:61
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
uint8_t Uint1
1-byte (8-bit) unsigned integer
Definition: ncbitype.h:99
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
uint16_t Uint2
2-byte (16-bit) unsigned integer
Definition: ncbitype.h:101
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
ENCUserFlags
@ fNoUserFlags
@ fNoCreate
Command does not create blob if it does not exist.
@ fNoProlong
Command does not update blob expiration time.
Uint4 TNCCmdFlags
ENCProxyCmd
@ eProxyGetSize
@ eProxyRemove
@ eProxyRead
@ eProxyNone
@ eProxyWrite
@ eProxyProlong
@ eProxyGetMeta
@ eProxyHasBlob
@ eProxyGetBList
@ eProxyGetBList2
@ eProxySetValid
@ eProxyReadLast
ENCCmdFlags
@ fNeedsStorageCache
Command needs finished database caching to be executed locally.
@ fIsHttp
HTTP-related.
@ fCursedPUT2Cmd
This is PUT2 command and connection that used PUT2 command.
@ fReadExactBlobSize
There is exact size of the blob transferred to NetCache.
@ eSyncBlobCmd
@ fNeedsSpaceAsPeer
Command comes from other NC server and needs disk space to execute.
@ eBlobPut
@ fCopyLogEvent
Command copies sync log event from other server, not creates a new one.
@ fRunsInStartedSync
Command can be executed only after successful execution of SYNC_START.
@ fSyncCmdSuccessful
Consider synchronization command to be successful even though RequestStatus is not eStatus_OK.
@ fNoBlobVersionCheck
Command shouldn't check the blob version (it doesn't come with the command).
@ fComesFromClient
Command comes from client, not from other NC server.
@ fUsesPeerSearch
Command needs to search for the latest version of the blob on all servers.
@ fProhibitsSyncAbort
Command should be executed even if server wants to abort the synchronization.
@ fDoNotCheckPassword
Do not check access password during command execution.
@ eProxyBlobRead
@ fPeerFindExistsOnly
During the blob search on other servers command needs to know only whether this blob exists or not,...
@ fNeedsAdminClient
Command can be executed only by admin client.
@ fNeedsBlobAccess
Command needs access to the blob.
@ eClientBlobRead
@ fCanGenerateKey
Command needs to generate blob key if it's empty.
@ fDoNotProxyToPeers
Always execute the command locally without proxying to peers.
@ fNeedsBlobList
Command needs access to the blob list.
@ fNeedsSpaceAsClient
Command comes from client and needs disk space to execute.
@ eCopyBlobFromPeer
@ eProxyBlobWrite
@ fSwapLengthBytes
Byte order should be swapped when reading length of chunks in blob transfer protocol.
@ fNoReplyOnFinish
Command does not need a confirmation at the end of execution.
@ fNoBlobAccessStats
Access information about blob shouldn't be printed at the end of the command.
@ fSkipBlobEOF
Client will send blob data for the command and won't send EOF marker.
@ eClientBlobWrite
@ fNeedsLowerPriority
Command should be executed with a lower priority.
@ fNoCmdFlags
@ eRunsInStartedSync
Uint4 TNCUserFlags
EHTTPStatus
Statuses of commands to be set in diagnostics' request context Additional statuses can be taken from ...
Definition: nc_utils.hpp:98
ENCAccessType
Type of access to NetCache blob.
Definition: nc_utils.hpp:85
vector< Uint8 > TServersList
Definition: nc_utils.hpp:61
#define NCBI_PACKED_ENUM_END()
Definition: ncbiconf_msvc.h:74
Extra information about each NetCache command.
ENCAccessType blob_access
What access to the blob command should receive.
const char * cmd_name
Command name to present to user in statistics.
State processor
Which method will process the command.
Full information about NetCache blob (excluding key)
Definition: nc_db_info.hpp:96
Single event record.
Definition: sync_log.hpp:52
Modified on Sat Apr 13 11:46:40 2024 by modify_doxy.py rev. 669887