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

Go to the SVN repository for this file.

1 #ifndef NETCACHE__PEER_CONTROL__HPP
2 #define NETCACHE__PEER_CONTROL__HPP
3 /* $Id: peer_control.hpp 79325 2017-08-31 13:38:24Z 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 
34 
35 #include "sync_log.hpp"
36 #include "nc_db_info.hpp"
37 #include "nc_stat.hpp"
38 #include <set>
39 
40 
42 
43 
44 class CNCActiveHandler;
45 class CNCActiveClientHub;
46 class CNCBlobAccessor;
48 
49 struct SActiveList_tag;
50 typedef intr::list_base_hook<intr::tag<SActiveList_tag> > TActiveListHook;
51 typedef intr::list<CNCActiveHandler,
52  intr::base_hook<TActiveListHook>,
53  intr::constant_time_size<false> > TNCPeerConnsList;
54 typedef list<CNCActiveClientHub*> TNCClientHubsList;
55 typedef list<CNCActiveSyncControl*> TNCActiveSyncList;
56 typedef TNCActiveSyncList::iterator TNCActiveSyncListIt;
57 
58 
60 {
65 
66 
68  Uint2 slot_,
69  const CNCBlobKeyLight& key_,
70  Uint8 rec_no)
71  : evt_type(typ),
72  slot(slot_),
73  key(key_),
74  orig_rec_no(rec_no)
75  {}
76 };
77 
79 {
82 
83 
85  Uint2 slot_,
86  const CNCBlobKeyLight& key_,
87  Uint8 rec_no,
88  Uint8 tm,
89  const CNCBlobAccessor* accessor);
90 };
91 
92 typedef list<SNCMirrorEvent*> TNCMirrorQueue;
93 
94 
95 
96 class CNCPeerControl : public CSrvTask
97 {
98 public:
99  static bool Initialize(void);
100  static void Finalize(void);
101 
102  static Uint4 FindIPbyAlias(Uint4 alias);
103  static Uint4 FindIPbyName(const string& alias);
104  static bool HasPeerInThrottle(void);
105 
106  static void SetServersForInitSync(Uint4 cnt_servers);
107  static void ResetServersForInitSync(void);
108  static void ReconfServersForInitSync(Uint4 cnt_servers);
109  static bool HasServersForInitSync(void);
110  static void PrintState(CSrvSocketTask& task);
111 
112  static CNCPeerControl* Peer(Uint8 srv_id);
113  static string GetPeerNameOrEmpty(Uint8 srv_id);
114 
115  static void MirrorUpdate(const CNCBlobKeyLight& key,
116  Uint2 slot,
117  Uint8 update_time);
118  static void MirrorRemove(const CNCBlobKeyLight& key,
119  Uint2 slot,
120  Uint8 update_time);
121  static void MirrorWrite(const CNCBlobKeyLight& key,
122  Uint2 slot,
123  Uint8 orig_rec_no,
124  Uint8 size,
125  const TServersList& mirrors_done);
126  static void MirrorProlong(const CNCBlobKeyLight& key,
127  Uint2 slot,
128  Uint8 orig_rec_no,
129  Uint8 orig_time,
130  const CNCBlobAccessor* accessor);
131  static Uint8 GetMirrorQueueSize(void);
132  static Uint8 GetMirrorQueueSize(Uint8 srv_id);
133  static void ReadCurState(SNCStateStat& state);
134 
135  void PeerHandshake(void);
136  void SetSlotsForInitSync(Uint2 cnt_slots);
137  void ResetSlotsForInitSync();
138  void ReconfSlotsForInitSync(Uint2 cnt_slots);
139  void AddInitiallySyncedSlot(void);
140  void RegisterSyncStop(bool is_passive,
141  Uint8& next_sync_time,
142  Uint8 next_sync_delay);
143 #ifdef _DEBUG
144  void RegisterSyncStat(bool is_passive, bool is_by_blobs, int result, int hint);
145  static void PrintSyncStat(CSrvSocketTask& task);
146 #endif
147  Uint8 GetNextSyncTime(void);
148 
149  Uint8 GetSrvId(void) const;
150  CNCActiveHandler* GetBGConn(bool silent = false);
151  bool StartActiveSync(void);
152  bool AddSyncControl(CNCActiveSyncControl* sync_ctrl);
153  void RemoveSyncControl(CNCActiveSyncControl* sync_ctrl);
154  bool FinishSync(CNCActiveSyncControl* sync_ctrl);
155 
156  void RegisterConnError(void);
157  void RegisterConnSuccess(void);
158  void ConnOk(void);
164 
165  bool GetReadyForShutdown(void);
166 
167 
170 
171  void AbortInitialSync(void);
172  void SetHostProtocol(Uint8 ver);
173  void SetTrustLevel(Uint8 trust);
174  Uint8 GetTrustLevel(void) const;
175  Uint8 GetRawTrustLevel(void) const;
176 
177  bool AcceptsSyncUpdate(void) const;
178  bool AcceptsSyncRemove(void) const;
179  bool AcceptsBlobKey(const CNCBlobKeyLight& key) const;
180  bool AcceptsBList(void) const;
181  bool AcceptsBList2(void) const;
182  bool AcceptsUserFlags(void) const;
183  bool AcceptsPurge2(void) const;
184 
185 private:
186  CNCPeerControl(Uint8 srv_id);
189 
190  virtual void ExecuteSlice(TSrvThreadNum thr_num);
191 
192  void x_SrvInitiallySynced(bool succeeded);
193  void x_SlotsInitiallySynced(Uint2 cnt_slots, bool aborted=false);
195  bool x_ReserveBGConn(void);
196  bool x_ReserveBGConnNow(void);
197  void x_UnreserveBGConn(void);
198  void x_IncBGConns(void);
199  void x_DecBGConns(void);
201  void x_DecActiveConns(void);
202  CNCActiveHandler* x_CreateNewConn(bool for_bg);
206  void x_DeleteMirrorEvent(SNCMirrorEvent* event);
210  void x_UpdateHasTasks(void);
211 
212 
215  string m_HostIPname;
216  string m_Hostname;
241 #ifdef _DEBUG
243 #endif
244 };
245 
246 
248 {
249 public:
250  CNCPeerShutdown(void);
251  virtual ~CNCPeerShutdown(void);
252 
253  virtual bool ReadyForShutdown(void);
254 };
255 
256 
257 
258 //////////////////////////////////////////////////////////////////////////
259 // Inline functions
260 //////////////////////////////////////////////////////////////////////////
261 
262 inline void
264 {
267 }
268 
269 inline void
271 {
273 }
274 
275 inline void
277 {
278  m_OrigSlotsToInitSync = cnt_slots;
279 }
280 
281 inline Uint8
283 {
284  return m_SrvId;
285 }
286 
287 inline Uint8
289 {
290  return m_NextSyncTime;
291 }
292 
293 inline bool
295 {
296  return m_HostProtocol >= 60700;
297 }
298 inline bool
300 {
301  return m_HostProtocol >= 60806;
302 }
303 inline bool
305 {
306  return m_HostProtocol >= 60700 || key.KeyVersion() < 3;
307 }
308 
309 inline bool
311 {
312  return m_HostProtocol >= 60900;
313 }
314 
315 inline bool
317 {
318  return m_HostProtocol > 61100;
319 }
320 
321 inline bool
323 {
324  return m_HostProtocol >= 61100;
325 }
326 
327 inline bool
329 {
330  return m_HostProtocol >= 61107;
331 }
332 
333 inline void
335 {
336  m_MaybeThrottle = false;
337 }
338 inline void
340  m_TrustLevel = trust;
341 }
342 inline Uint8
344  return m_TrustLevel & 0xF;
345 }
346 inline Uint8
348 {
349  return m_TrustLevel;
350 }
351 
352 
354 
355 #endif /* NETCACHE__PEER_CONTROL__HPP */
CAtomicCounter –.
Definition: ncbicntr.hpp:71
Mutex created to have minimum possible size (its size is 4 bytes) and to sleep using kernel capabilit...
Definition: srv_sync.hpp:193
static void Finalize(void)
static void MirrorRemove(const CNCBlobKeyLight &key, Uint2 slot, Uint8 update_time)
static bool HasPeerInThrottle(void)
void x_DecBGConns(void)
Uint2 m_OrigSlotsToInitSync
bool x_DoReleaseConn(CNCActiveHandler *conn)
CNCActiveHandler * x_GetBGConnImpl(void)
void AbortInitialSync(void)
Uint8 GetNextSyncTime(void)
TNCPeerConnsList m_PooledConns
TNCActiveSyncList m_SyncList
static void SetServersForInitSync(Uint4 cnt_servers)
TNCPeerConnsList m_BusyConns
void x_DeleteMirrorEvent(SNCMirrorEvent *event)
TNCMirrorQueue m_SmallMirror
void x_ProcessMirrorEvent(CNCActiveHandler *conn, SNCMirrorEvent *event)
void RegisterConnError(void)
Uint8 GetSrvId(void) const
void x_DecActiveConns(void)
CNCPeerControl(Uint8 srv_id)
void RegisterConnSuccess(void)
void PutConnToPool(CNCActiveHandler *conn)
void AddInitiallySyncedSlot(void)
bool x_ReserveBGConnNow(void)
TNCClientHubsList m_Clients
TNCMirrorQueue m_BigMirror
static Uint4 FindIPbyName(const string &alias)
CNCActiveHandler * GetPooledConn(void)
bool FinishSync(CNCActiveSyncControl *sync_ctrl)
Uint8 GetTrustLevel(void) const
bool AcceptsUserFlags(void) const
static void MirrorWrite(const CNCBlobKeyLight &key, Uint2 slot, Uint8 orig_rec_no, Uint8 size, const TServersList &mirrors_done)
void RegisterSyncStat(bool is_passive, bool is_by_blobs, int result, int hint)
bool AddSyncControl(CNCActiveSyncControl *sync_ctrl)
void x_UnreserveBGConn(void)
bool GetReadyForShutdown(void)
bool StartActiveSync(void)
bool AcceptsSyncUpdate(void) const
void ReleaseConn(CNCActiveHandler *conn)
static Uint8 GetMirrorQueueSize(void)
static CAtomicCounter sm_TotalCopyRequests
void x_ProcessUpdateEvent(SNCMirrorEvent *event)
static void MirrorProlong(const CNCBlobKeyLight &key, Uint2 slot, Uint8 orig_rec_no, Uint8 orig_time, const CNCBlobAccessor *accessor)
bool CreateNewSocket(CNCActiveHandler *conn)
CNCActiveHandler * GetBGConn(bool silent=false)
Uint8 GetRawTrustLevel(void) const
void x_IncBGConns(void)
virtual void ExecuteSlice(TSrvThreadNum thr_num)
This is the main method to do all work this task should do.
void SetHostProtocol(Uint8 ver)
bool AcceptsPurge2(void) const
static void ReconfServersForInitSync(Uint4 cnt_servers)
void ConnOk(void)
CNCPeerControl(const CNCPeerControl &)
void SetSlotsForInitSync(Uint2 cnt_slots)
void AssignClientConn(CNCActiveClientHub *hub)
void ResetSlotsForInitSync()
void RemoveSyncControl(CNCActiveSyncControl *sync_ctrl)
static void PrintState(CSrvSocketTask &task)
void x_AddMirrorEvent(SNCMirrorEvent *event, Uint8 size)
bool AcceptsBList2(void) const
void x_UpdateHasTasks(void)
void RegisterSyncStop(bool is_passive, Uint8 &next_sync_time, Uint8 next_sync_delay)
static bool Initialize(void)
static void ReadCurState(SNCStateStat &state)
bool x_AssignClientConn(CNCActiveClientHub *hub, CNCActiveHandler *conn)
static string GetPeerNameOrEmpty(Uint8 srv_id)
static bool HasServersForInitSync(void)
map< size_t, size_t > m_SyncStat
CNCActiveHandler * x_CreateNewConn(bool for_bg)
bool AcceptsBList(void) const
CNCActiveHandler * x_GetPooledConnImpl(void)
static CNCPeerControl * Peer(Uint8 srv_id)
CMiniMutex m_ObjLock
static void PrintSyncStat(CSrvSocketTask &task)
TNCActiveSyncListIt m_NextTaskSync
static Uint4 FindIPbyAlias(Uint4 alias)
void PeerHandshake(void)
static void ResetServersForInitSync(void)
void SetTrustLevel(Uint8 trust)
void ReconfSlotsForInitSync(Uint2 cnt_slots)
void x_SlotsInitiallySynced(Uint2 cnt_slots, bool aborted=false)
static CAtomicCounter sm_CopyReqsRejected
bool AcceptsBlobKey(const CNCBlobKeyLight &key) const
bool AcceptsSyncRemove(void) const
CNCPeerControl & operator=(const CNCPeerControl &)
static void MirrorUpdate(const CNCBlobKeyLight &key, Uint2 slot, Uint8 update_time)
void x_SrvInitiallySynced(bool succeeded)
bool x_ReserveBGConn(void)
virtual ~CNCPeerShutdown(void)
virtual bool ReadyForShutdown(void)
Method called if server is ready to shutdown.
Interface for an object wishing to be notified when server is going to shutdown.
Task controlling a socket.
Main working entity in TaskServer.
Definition: srv_tasks.hpp:88
static CS_CONNECTION * conn
Definition: ct_dynamic.c:25
static int silent
Definition: done_handling.c:30
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
const struct ncbi::grid::netcache::search::fields::SIZE size
const struct ncbi::grid::netcache::search::fields::KEY key
vector< Uint8 > TServersList
Definition: nc_utils.hpp:61
intr::list_base_hook< intr::tag< SActiveList_tag > > TActiveListHook
intr::list< CNCActiveHandler, intr::base_hook< TActiveListHook >, intr::constant_time_size< false > > TNCPeerConnsList
list< SNCMirrorEvent * > TNCMirrorQueue
TNCActiveSyncList::iterator TNCActiveSyncListIt
list< CNCActiveClientHub * > TNCClientHubsList
list< CNCActiveSyncControl * > TNCActiveSyncList
SNCMirrorEvent(ENCSyncEvent typ, Uint2 slot_, const CNCBlobKeyLight &key_, Uint8 rec_no)
CNCBlobKeyLight key
ENCSyncEvent evt_type
SNCBlobSummary blob_sum
SNCMirrorProlong(ENCSyncEvent typ, Uint2 slot_, const CNCBlobKeyLight &key_, Uint8 rec_no, Uint8 tm, const CNCBlobAccessor *accessor)
ENCSyncEvent
Event types to log.
Definition: sync_log.hpp:43
Uint2 TSrvThreadNum
Type for thread number in TaskServer.
Definition: task_server.hpp:42
else result
Definition: token2.c:20
Modified on Fri Apr 12 17:19:15 2024 by modify_doxy.py rev. 669887