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

Go to the SVN repository for this file.

1 #ifndef NETCACHE_NC_UTILS__HPP
2 #define NETCACHE_NC_UTILS__HPP
3 /* $Id: nc_utils.hpp 93908 2021-06-03 14:08:09Z ucko $
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:
31  * Utility classes that now are used only in NetCache but can be used
32  * anywhere else.
33  */
34 
35 
36 #include <util/simple_buffer.hpp>
37 
38 
39 #define ATTR_PACKED NCBI_PACKED
40 
41 #if 0 // NCBI_HAS_STD_ATTRIBUTE(aligned) -- speculative
42 # define ATTR_ALIGNED_8 [[aligned(8)]]
43 #elif NCBI_HAS_ATTRIBUTE(gnu::aligned)
44 # define ATTR_ALIGNED_8 [[gnu::aligned(8)]]
45 #elif __has_attribute(aligned)
46 # define ATTR_ALIGNED_8 __attribute__ ((aligned(8)))
47 #else
48 # define ATTR_ALIGNED_8
49 #endif
50 
51 
52 
54 
55 
56 static const char* const kNCPeerClientName = "nc_peer";
57 
58 
61 typedef vector<Uint8> TServersList;
62 //typedef CSimpleBufferT<char> TNCBufferType;
63 class TNCBufferType : public CSimpleBufferT<char>
64 {
65 public:
66  TNCBufferType& WriteText(const char* buf) {
68  return *this;
69  }
70  TNCBufferType& WriteText(const string& buf) {
71  CSimpleBufferT<char>::append(buf.data(), buf.size());
72  return *this;
73  }
74  template <typename NumType>
75  TNCBufferType& WriteNumber(NumType num) {
76  return WriteText(NStr::NumericToString(num));
77  }
79  return WriteText(b ? "true" : "false");
80  }
81 };
82 
83 
84 /// Type of access to NetCache blob
86  eNCNone = 0,
87  eNCRead, ///< Read meta information only
88  eNCReadData, ///< Read blob data
89  eNCCreate, ///< Create blob or re-write its contents
90  eNCCopyCreate ///< (Re-)write blob from another NetCache (as opposed to
91  ///< writing from client)
93 
94 
95 /// Statuses of commands to be set in diagnostics' request context
96 /// Additional statuses can be taken from
97 /// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
99 // synchronization-related
100  /// SYNC_START: synchronization by blobs list will be performed.
102  /// SYNC_START: synchronization by events will be performed.
104  /// Same or newer blob is present on the server.
106  /// Synchronization cannot start because server is busy doing cleaning or
107  /// some other synchronization on this slot.
109  /// Synchronization is rejected because both servers tried to start it
110  /// simultaneously.
112 
113 // success
114  /// Command is ok and execution is good.
115  eStatus_OK = 200,
116  /// New resource has been created
118  /// The server is delivering only part of the resource
120 
121 // errors
122  /// Command is incorrect.
124  /// Bad password for accessing the blob.
126  /// Client was disabled in configuration.
128  /// Blob was not found.
130  /// Operation not allowed with current settings (e.g. password given but
131  /// ini-file says that only blobs without password are allowed)
133  /// Command requires admin privileges.
135  /// Command timeout is exceeded.
137  /// SETVALID cannot be executed as new version was already written.
139  /// Blob version did not match
141  /// Precondition stated in command has failed (size of blob was given but
142  /// data has a different size)
144  /// Blob size exceeds the allowed maximum.
146  /// Connection was closed too early (client didn't send all data or didn't
147  /// get confirmation about successful execution).
149  /// Internal server error.
152  /// Command is not implemented.
154  /// Peer returned something wrong
156  /// operation canceled because server needs to shutdown.
159  /// Command cannot be executed because NetCache didn't cache the database
160  /// contents yet.
162  /// Command should be proxied to peers but it's impossible to connect to any
164  /// There's not enough disk space to execute the command.
166  /// Synchronization is aborted because something went wrong.
167  eStatus_SyncAborted = 520
168 };
169 
170 
171 /// Initializes maps between status codes and error texts sent to client
172 /// to explain these codes.
173 void InitClientMessages(void);
174 EHTTPStatus GetStatusByMessage(const string& msg, EHTTPStatus def);
175 const string& GetMessageByStatus(EHTTPStatus sts);
176 
177 
178 /////////////////////////////////////////////////////////////////////////////
179 // alerts
180 
182 {
183 public:
184  enum EAlertType {
186  eStartupConfigChanged, ///< Configuration file changed
187  ePidFileFailed, ///< Reporting Pid failed
188  eStartAfterCrash, ///< InstanceGuard file was present on startup
189  eStorageReinit, ///< Data storage was reinitialized
190  eAccessDenied, ///< Command was rejected because client lacks administrative privileges
191  eSyncFailed, ///< Synchronization failed
192  ePeerIpChanged, ///< Peer IP address changed
193  eDiskSpaceNormal, ///< Free disk space is back to normal
194  eDatabaseTooLarge, ///< Database is too large (warning)
195  eDatabaseOverLimit, ///< Database size exceeded its limit (error, stop write)
196  eDiskSpaceLow, ///< Free disk space is below threshold
197  eDiskSpaceCritical, ///< Free disk space is below critical threshold
198 #ifdef _DEBUG
199 // it is convenient way to keep track of some events
251 #endif
252  eLastAlert
253  };
254 
258  };
259 
260  static void Register(EAlertType alert_type, const string& message);
261  static EAlertAckResult Acknowledge(const string& alert_id, const string& user);
262  static void Report(CSrvSocketTask& task, bool report_all);
263 
264 private:
265  static EAlertType x_IdToType(const string& alert_id);
266  static string x_TypeToId(EAlertType type);
267 };
268 
270 
271 #endif /* NETCACHE_NC_UTILS__HPP */
static void Register(EAlertType alert_type, const string &message)
Definition: nc_utils.cpp:188
static EAlertType x_IdToType(const string &alert_id)
Definition: nc_utils.cpp:292
@ eAcknowledged
Definition: nc_utils.hpp:257
static string x_TypeToId(EAlertType type)
Definition: nc_utils.cpp:301
static void Report(CSrvSocketTask &task, bool report_all)
Definition: nc_utils.cpp:170
static EAlertAckResult Acknowledge(const string &alert_id, const string &user)
Definition: nc_utils.cpp:200
@ eDebugExtraWrite
Definition: nc_utils.hpp:235
@ eDebugWriteBlobInfo1
Definition: nc_utils.hpp:208
@ eDebugOrphanRecordFound
Definition: nc_utils.hpp:200
@ eSyncFailed
Synchronization failed.
Definition: nc_utils.hpp:191
@ eDebugReadBlobInfoFailed0
Definition: nc_utils.hpp:203
@ eDebugMoveRecord1
Definition: nc_utils.hpp:227
@ eAccessDenied
Command was rejected because client lacks administrative privileges.
Definition: nc_utils.hpp:190
@ eDebugDeleteFile
Definition: nc_utils.hpp:230
@ eDebugSyncAborted1
Definition: nc_utils.hpp:246
@ eStorageReinit
Data storage was reinitialized.
Definition: nc_utils.hpp:189
@ eDebugMoveRecord2
Definition: nc_utils.hpp:228
@ eDebugWrongCacheFound2
Definition: nc_utils.hpp:245
@ eDebugCacheWrongMgr
Definition: nc_utils.hpp:242
@ eDebugSaveOneMapImpl2
Definition: nc_utils.hpp:223
@ eDebugCacheDeleted3
Definition: nc_utils.hpp:238
@ eDebugCacheFailedMgrAttach
Definition: nc_utils.hpp:240
@ eDebugWriteBlobInfoFailed
Definition: nc_utils.hpp:202
@ eDebugDeleteNextData2
Definition: nc_utils.hpp:220
@ eDebugOrphanRecordFound2
Definition: nc_utils.hpp:201
@ eDebugCacheFailedMgrDetach
Definition: nc_utils.hpp:241
@ eDebugUpdateUpCoords2
Definition: nc_utils.hpp:207
@ eDebugReadMapInfo1
Definition: nc_utils.hpp:213
@ eDebugSyncAborted2
Definition: nc_utils.hpp:247
@ eDebugUpdateUpCoords1
Definition: nc_utils.hpp:206
@ eDiskSpaceCritical
Free disk space is below critical threshold.
Definition: nc_utils.hpp:197
@ eDatabaseTooLarge
Database is too large (warning)
Definition: nc_utils.hpp:194
@ eDebugWriteBlobInfo3
Definition: nc_utils.hpp:210
@ eDiskSpaceLow
Free disk space is below threshold.
Definition: nc_utils.hpp:196
@ eDebugConnAdjusted2
Definition: nc_utils.hpp:249
@ eDebugDbFileNotFound
Definition: nc_utils.hpp:250
@ eDebugCacheWrong
Definition: nc_utils.hpp:243
@ eStartAfterCrash
InstanceGuard file was present on startup.
Definition: nc_utils.hpp:188
@ eDebugWriteChunkData2
Definition: nc_utils.hpp:225
@ eDebugWrongCacheFound1
Definition: nc_utils.hpp:244
@ eDebugMoveDataToGarbage
Definition: nc_utils.hpp:212
@ eDatabaseOverLimit
Database size exceeded its limit (error, stop write)
Definition: nc_utils.hpp:195
@ eDebugReadChunkData4
Definition: nc_utils.hpp:218
@ eDebugWriteBlobInfo2
Definition: nc_utils.hpp:209
@ eDebugReadChunkData2
Definition: nc_utils.hpp:216
@ eDiskSpaceNormal
Free disk space is back to normal.
Definition: nc_utils.hpp:193
@ ePeerIpChanged
Peer IP address changed.
Definition: nc_utils.hpp:192
@ eDebugReleaseCacheData1
Definition: nc_utils.hpp:231
@ eDebugSaveOneMapImpl1
Definition: nc_utils.hpp:222
@ eDebugCacheDeleted1
Definition: nc_utils.hpp:236
@ eDebugCacheDeleted2
Definition: nc_utils.hpp:237
@ eDebugDeleteCNCBlobVerManager
Definition: nc_utils.hpp:234
@ eDebugMoveRecord0
Definition: nc_utils.hpp:226
@ ePidFileFailed
Reporting Pid failed.
Definition: nc_utils.hpp:187
@ eStartupConfigChanged
Configuration file changed.
Definition: nc_utils.hpp:186
@ eDebugConnAdjusted1
Definition: nc_utils.hpp:248
@ eDebugWriteChunkData1
Definition: nc_utils.hpp:224
@ eDebugReadChunkData1
Definition: nc_utils.hpp:215
@ eDebugMoveRecord3
Definition: nc_utils.hpp:229
@ eDebugReleaseCacheData2
Definition: nc_utils.hpp:232
@ eDebugReadBlobInfoFailed1
Definition: nc_utils.hpp:204
@ eDebugReadBlobInfoFailed2
Definition: nc_utils.hpp:205
@ eDebugReadChunkData3
Definition: nc_utils.hpp:217
@ eDebugCacheDeleted4
Definition: nc_utils.hpp:239
@ eDebugReadMapInfo2
Definition: nc_utils.hpp:214
@ eDebugDeleteNextData1
Definition: nc_utils.hpp:219
@ eDebugWriteBlobInfo4
Definition: nc_utils.hpp:211
@ eDebugDeleteSNCBlobVerData
Definition: nc_utils.hpp:233
@ eDebugDeleteVersionData
Definition: nc_utils.hpp:221
Reallocable memory buffer (no memory copy overhead) Mimics vector<>, without the overhead of explicit...
CSimpleBufferT & append(const void *buf, size_t len)
Task controlling a socket.
TNCBufferType & WriteNumber(NumType num)
Definition: nc_utils.hpp:75
TNCBufferType & WriteText(const char *buf)
Definition: nc_utils.hpp:66
TNCBufferType & WriteText(const string &buf)
Definition: nc_utils.hpp:70
TNCBufferType & WriteBool(bool b)
Definition: nc_utils.hpp:78
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:673
char * buf
static const char *const kNCPeerClientName
Definition: nc_utils.hpp:56
map< string, string > TStringMap
Definition: nc_utils.hpp:59
EHTTPStatus
Statuses of commands to be set in diagnostics' request context Additional statuses can be taken from ...
Definition: nc_utils.hpp:98
@ eStatus_RaceCond
SETVALID cannot be executed as new version was already written.
Definition: nc_utils.hpp:138
@ eStatus_Created
New resource has been created.
Definition: nc_utils.hpp:117
@ eStatus_CmdTimeout
Command timeout is exceeded.
Definition: nc_utils.hpp:136
@ eStatus_BadCmd
Command is incorrect.
Definition: nc_utils.hpp:123
@ eStatus_ServerError
Internal server error.
Definition: nc_utils.hpp:150
@ eStatus_PeerError
Command should be proxied to peers but it's impossible to connect to any.
Definition: nc_utils.hpp:163
@ eStatus_SyncEvents
SYNC_START: synchronization by events will be performed.
Definition: nc_utils.hpp:103
@ eStatus_SyncBusy
Synchronization cannot start because server is busy doing cleaning or some other synchronization on t...
Definition: nc_utils.hpp:108
@ eStatus_Disabled
Client was disabled in configuration.
Definition: nc_utils.hpp:127
@ eStatus_BlobTooBig
Blob size exceeds the allowed maximum.
Definition: nc_utils.hpp:145
@ eStatus_ServiceUnavailable
Definition: nc_utils.hpp:158
@ eStatus_SyncAborted
Synchronization is aborted because something went wrong.
Definition: nc_utils.hpp:167
@ eStatus_PrematureClose
Connection was closed too early (client didn't send all data or didn't get confirmation about success...
Definition: nc_utils.hpp:148
@ eStatus_NotFound
Blob was not found.
Definition: nc_utils.hpp:129
@ eStatus_NoDiskSpace
There's not enough disk space to execute the command.
Definition: nc_utils.hpp:165
@ eStatus_NewerBlob
Same or newer blob is present on the server.
Definition: nc_utils.hpp:105
@ eStatus_CrossSync
Synchronization is rejected because both servers tried to start it simultaneously.
Definition: nc_utils.hpp:111
@ eStatus_NeedAdmin
Command requires admin privileges.
Definition: nc_utils.hpp:134
@ eStatus_OK
Command is ok and execution is good.
Definition: nc_utils.hpp:115
@ eStatus_NoImpl
Command is not implemented.
Definition: nc_utils.hpp:153
@ eStatus_CondFailed
Precondition stated in command has failed (size of blob was given but data has a different size)
Definition: nc_utils.hpp:143
@ eStatus_NotAllowed
Operation not allowed with current settings (e.g.
Definition: nc_utils.hpp:132
@ eStatus_PartialContent
The server is delivering only part of the resource.
Definition: nc_utils.hpp:119
@ sStatus_BlobVersion
Blob version did not match.
Definition: nc_utils.hpp:140
@ eStatus_CmdAborted
Definition: nc_utils.hpp:151
@ eStatus_BadPassword
Bad password for accessing the blob.
Definition: nc_utils.hpp:125
@ eStatus_BadPeer
Peer returned something wrong.
Definition: nc_utils.hpp:155
@ eStatus_SyncBList
SYNC_START: synchronization by blobs list will be performed.
Definition: nc_utils.hpp:101
@ eStatus_JustStarted
Command cannot be executed because NetCache didn't cache the database contents yet.
Definition: nc_utils.hpp:161
@ eStatus_ShuttingDown
operation canceled because server needs to shutdown.
Definition: nc_utils.hpp:157
const string & GetMessageByStatus(EHTTPStatus sts)
Definition: nc_utils.cpp:97
ENCAccessType
Type of access to NetCache blob.
Definition: nc_utils.hpp:85
@ eNCReadData
Read blob data.
Definition: nc_utils.hpp:88
@ eNCCopyCreate
(Re-)write blob from another NetCache (as opposed to writing from client)
Definition: nc_utils.hpp:90
@ eNCRead
Read meta information only.
Definition: nc_utils.hpp:87
@ eNCNone
Definition: nc_utils.hpp:86
@ eNCCreate
Create blob or re-write its contents.
Definition: nc_utils.hpp:89
map< Uint8, string > TNCPeerList
Definition: nc_utils.hpp:60
vector< Uint8 > TServersList
Definition: nc_utils.hpp:61
EHTTPStatus GetStatusByMessage(const string &msg, EHTTPStatus def)
Definition: nc_utils.cpp:84
void InitClientMessages(void)
Initializes maps between status codes and error texts sent to client to explain these codes.
Definition: nc_utils.cpp:74
#define NCBI_PACKED_ENUM_END()
Definition: ncbiconf_msvc.h:74
EAlertAckResult
Definition: ns_alert.hpp:63
EAlertType
Definition: ns_alert.hpp:46
Definition: type.c:6
Modified on Wed Apr 17 13:10:20 2024 by modify_doxy.py rev. 669887