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

Go to the SVN repository for this file.

1 #ifndef NETCACHE__TASK_SERVER__HPP
2 #define NETCACHE__TASK_SERVER__HPP
3 /* $Id: task_server.hpp 76997 2017-03-17 13:19:30Z 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:
31  * Declaration of the main class in TaskServer infrastructure and some
32  * utility classes that can be used with it.
33  */
34 
35 
36 namespace intr = boost::intrusive;
37 
38 
40 
41 /// Type for thread number in TaskServer
43 
44 /// Type for flags in CSrvTask. It's needed only for internal use, shouldn't
45 /// be necessary outside task_server library.
47  fTaskRunnable = 1 << 0,
48  fTaskQueued = 1 << 1,
49  fTaskRunning = 1 << 2,
50  fTaskOnTimer = 1 << 3,
52  fTaskTerminated = 1 << 5
53 };
55 
56 
57 class CNcbiRegistry;
58 class CSrvSocketFactory;
59 class CSrvTime;
60 class CSrvSocketTask;
61 
62 
63 // Helper types to use in CSrvRCUUser
64 struct SSrvRCUList_tag;
65 typedef intr::slist_member_hook<intr::tag<SSrvRCUList_tag> > TSrvRCUListHook;
66 
67 /// Class to derive from to use RCU mechanism. For detailed explanation of
68 /// RCU go to http://lse.sourceforge.net/locking/rcupdate.html. In short:
69 /// if you want to do something which can race with other threads but you know
70 /// when other threads exit current functions to return to TaskServer it will
71 /// be already safe to do your job RCU is what you need. The most common use
72 /// is deletion of an object pointer to which is obtained from somewhere and
73 /// used locally in the same function and not remembered for later use.
75 {
76 public:
77  /// Method to be called to schedule call of ExecuteRCU() at appropriate
78  /// time. CallRCU() cannot be called twice on the same object unless
79  /// ExecuteRCU() was called in between those calls (it is safe to call
80  /// CallRCU() from inside ExecutRCU() ).
81  void CallRCU(void);
82  /// Method implementing RCU job that was scheduled earlier by CallRCU().
83  virtual void ExecuteRCU(void) = 0;
84 
85  CSrvRCUUser(void);
86  virtual ~CSrvRCUUser(void);
87 
88 private:
91 
92 // Consider this section private as it's public for internal use only
93 // to minimize implementation-specific clutter in headers.
94 public:
96 };
97 
98 // Helper types to be used only inside TaskServer. It's placed here only to
99 // keep all relevant pieces in one place.
100 typedef intr::member_hook<CSrvRCUUser,
103 typedef intr::slist<CSrvRCUUser,
105  intr::cache_last<true>,
106  intr::constant_time_size<true> > TSrvRCUList;
107 
108 
109 // Helper types to be used in CSrvShutdownCallback.
110 struct SSrvShutdownList_tag;
111 typedef intr::slist_member_hook<intr::tag<SSrvShutdownList_tag> > TSrvSDListHook;
112 
113 /// Interface for an object wishing to be notified when server is going to
114 /// shutdown. Implementation can postpone shutdown for a while if it thinks
115 /// some operations should be finished first. To be notified object
116 /// implementing this interface should be registered with
117 /// CTaskServer::AddShutdownCallback().
119 {
120 public:
121  CSrvShutdownCallback(void);
122  virtual ~CSrvShutdownCallback(void);
123 
124  /// Method called if server is ready to shutdown. Method should return
125  /// TRUE if this object is ready for shutdown too, FALSE if shutdown
126  /// should be delayed and this method should be called later.
127  virtual bool ReadyForShutdown(void) = 0;
128 
129 // Consider this section private as it's public for internal use only
130 // to minimize implementation-specific clutter in headers.
131 public:
133 };
134 
135 // Helper types to be used only inside TaskServer. It's placed here only to
136 // keep all relevant pieces in one place.
137 typedef intr::member_hook<CSrvShutdownCallback,
140 typedef intr::slist<CSrvShutdownCallback,
142  intr::constant_time_size<false>,
143  intr::cache_last<false> > TShutdownList;
144 
145 
146 /// Types of server shutdown procedures that one can request from CTaskServer.
148  // "Slow" shutdown means TaskServer gives more time for current operations
149  // to complete. Server stays in "soft" shutdown phase for up to
150  // [task_server]/slow_shutdown_timeout seconds from ini-file.
152  // "Fast" shutdown means TaskSerer should stop quicker. With this shutdown
153  // type server will stay in "soft" shutdown phase for up to
154  // [task_server]/fast_shutdown_timeout seconds from ini-file.
156 };
157 
158 
159 /// Main class representing TaskServer infrastructure. This is just a common
160 /// place for some global methods. There's no instance of this class anywhere.
162 {
163 public:
164  /// Initializes TaskServer infrastructure from given command line
165  /// arguments. Call to this method should be virtually the first call
166  /// in the main() function. Returns TRUE if initialization was successful,
167  /// FALSE otherwise. In case of unsuccessful initialization application
168  /// should exit asap.
169  static bool Initialize(int& argc, const char** argv);
170  /// Load (re-read) configuration from current conf file into another registry
171  /// It is used in RECONF command to find changes in the configuration
172  static bool ReadConfiguration( CNcbiRegistry*& reg);
173 
174  static void WriteSetup(CSrvSocketTask& task);
175  static bool ReConfig(const CNcbiRegistry& new_reg, string& err_message);
176  /// Finalizes TaskServer infrastructure. Method should be called even if
177  /// Initialize() returned FALSE to give TaskServer a chance to finalize
178  /// parts that were already initialized.
179  static void Finalize(void);
180  /// Run all TaskServer machinery. Method doesn't return until server was
181  /// asked to stop via RequestShutdown() method or SIGINT/SIGTERM signals.
182  /// After receiving those requests to stop method still doesn't return
183  /// immediately - it waits for all operations to complete (server to
184  /// become idle), all sockets to close and all shutdown callbacks to show
185  /// readiness for shutdown.
186  static void Run(void);
187 
188  /// Obtains reference to registry read from application's ini-file.
189  static const CNcbiRegistry& GetConfRegistry(void);
190  /// Used to modify original registry (to reflect RECONF modifications)
191  static CNcbiRegistry& SetConfRegistry(void);
192  /// Returns time when this server application was started (when
193  /// Initialize() method was called).
194  static CSrvTime GetStartTime(void);
195 
196  /// Adds port for TaskServer to listen to. factory will create
197  /// CSrvSocketTask objects for all sockets connecting to this port.
198  /// TaskServer has a limit of 16 ports to listen to, attempts to call
199  /// method to add more ports will result in FALSE returned. Also if this
200  /// method is called while Run() is executing it can return FALSE if port
201  /// is busy or some other network-related error occurred. In all other
202  /// cases method returns TRUE (port is actually bound and opened
203  /// for listening only inside Run().
204  static bool AddListeningPort(Uint2 port, CSrvSocketFactory* factory);
205  /// Adds new object wishing to receive callbacks when server is going to
206  /// shutdown and wishing to influence this behavior.
207  static void AddShutdownCallback(CSrvShutdownCallback* callback);
208  /// Asks server to start shutdown procedures. These procedures include
209  /// closing all listening sockets, notifying all sockets (so that they
210  /// could start appropriate closing procedures) and waiting for everything
211  /// to calm down, for all sockets to close and for all shutdown callbacks
212  /// to signal their readiness.
213  static void RequestShutdown(ESrvShutdownType shutdown_type);
214 
215  /// Checks if TaskServer is running now, i.e. Run() method is called and
216  /// shutdown is not requested.
217  static bool IsRunning(void);
218  /// Checks if TaskServer received request to shutdown.
219  static bool IsInShutdown(void);
220  /// Checks if TaskServer currently is in "soft" shutdown phase when
221  /// client-related operations still can proceed to finish.
222  static bool IsInSoftShutdown(void);
223  /// Checks if TaskServer currently is in "hard" shutdown phase when
224  /// no operation is allowed to proceed unless there's no way to stop it.
225  static bool IsInHardShutdown(void);
226 
227  /// Returns maximum number of worker threads allowed
228  /// ([task_server]/max_threads parameter in ini-file).
229  static TSrvThreadNum GetMaxRunningThreads(void);
230  /// Returns number of current worker thread. Number is 0-based.
231  static TSrvThreadNum GetCurThreadNum(void);
232 
233  /// Returns name of server this application is executing on.
234  static const string& GetHostName(void);
235  /// Converts server name (or IP address written as string) to encoded
236  /// 4-byte IP address.
237  static Uint4 GetIPByHost(const string& host);
238  /// Converts 4-byte encoded IP address into its string representation.
239  static string IPToString(Uint4 ip);
240  /// Converts 4-byte encoded IP address into server name.
241  static string GetHostByIP(Uint4 ip);
242 
243 private:
245 };
246 
247 
249 
250 #endif /* NETCACHE__TASK_SERVER__HPP */
CNcbiRegistry –.
Definition: ncbireg.hpp:913
Class to derive from to use RCU mechanism.
Definition: task_server.hpp:75
CSrvRCUUser(const CSrvRCUUser &)
CSrvRCUUser & operator=(const CSrvRCUUser &)
CSrvRCUUser(void)
Definition: rcu.cpp:187
void CallRCU(void)
Method to be called to schedule call of ExecuteRCU() at appropriate time.
Definition: rcu.cpp:194
virtual ~CSrvRCUUser(void)
Definition: rcu.cpp:190
TSrvRCUListHook m_RCUListHook
Definition: task_server.hpp:95
virtual void ExecuteRCU(void)=0
Method implementing RCU job that was scheduled earlier by CallRCU().
Interface for an object wishing to be notified when server is going to shutdown.
TSrvSDListHook m_SDListHook
virtual bool ReadyForShutdown(void)=0
Method called if server is ready to shutdown.
virtual ~CSrvShutdownCallback(void)
Factory that creates CSrvSocketTask-derived object for each connection coming to listening port which...
Task controlling a socket.
Class incorporating convenient methods to work with struct timespec.
Definition: srv_time.hpp:61
Main class representing TaskServer infrastructure.
static const CNcbiRegistry & GetConfRegistry(void)
Obtains reference to registry read from application's ini-file.
static TSrvThreadNum GetMaxRunningThreads(void)
Returns maximum number of worker threads allowed ([task_server]/max_threads parameter in ini-file).
CTaskServer(const CTaskServer &)
static CNcbiRegistry & SetConfRegistry(void)
Used to modify original registry (to reflect RECONF modifications)
static const string & GetHostName(void)
Returns name of server this application is executing on.
static void RequestShutdown(ESrvShutdownType shutdown_type)
Asks server to start shutdown procedures.
static string GetHostByIP(Uint4 ip)
Converts 4-byte encoded IP address into server name.
static bool AddListeningPort(Uint2 port, CSrvSocketFactory *factory)
Adds port for TaskServer to listen to.
static bool ReadConfiguration(CNcbiRegistry *&reg)
Load (re-read) configuration from current conf file into another registry It is used in RECONF comman...
static void AddShutdownCallback(CSrvShutdownCallback *callback)
Adds new object wishing to receive callbacks when server is going to shutdown and wishing to influenc...
Definition: server_core.cpp:85
static TSrvThreadNum GetCurThreadNum(void)
Returns number of current worker thread. Number is 0-based.
Definition: threads_man.cpp:92
static CSrvTime GetStartTime(void)
Returns time when this server application was started (when Initialize() method was called).
static bool IsRunning(void)
Checks if TaskServer is running now, i.e.
static void Finalize(void)
Finalizes TaskServer infrastructure.
static bool ReConfig(const CNcbiRegistry &new_reg, string &err_message)
static string IPToString(Uint4 ip)
Converts 4-byte encoded IP address into its string representation.
static void WriteSetup(CSrvSocketTask &task)
static bool IsInShutdown(void)
Checks if TaskServer received request to shutdown.
static Uint4 GetIPByHost(const string &host)
Converts server name (or IP address written as string) to encoded 4-byte IP address.
static bool Initialize(int &argc, const char **argv)
Initializes TaskServer infrastructure from given command line arguments.
static bool IsInSoftShutdown(void)
Checks if TaskServer currently is in "soft" shutdown phase when client-related operations still can p...
static bool IsInHardShutdown(void)
Checks if TaskServer currently is in "hard" shutdown phase when no operation is allowed to proceed un...
static void Run(void)
Run all TaskServer machinery.
static const char ip[]
Definition: des.c:75
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
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
intr::member_hook< CSrvRCUUser, TSrvRCUListHook, &CSrvRCUUser::m_RCUListHook > TSrvRCUListOpt
Uint2 TSrvThreadNum
Type for thread number in TaskServer.
Definition: task_server.hpp:42
intr::slist_member_hook< intr::tag< SSrvRCUList_tag > > TSrvRCUListHook
Definition: task_server.hpp:64
intr::member_hook< CSrvShutdownCallback, TSrvSDListHook, &CSrvShutdownCallback::m_SDListHook > TSDListHookOpt
intr::slist< CSrvRCUUser, TSrvRCUListOpt, intr::cache_last< true >, intr::constant_time_size< true > > TSrvRCUList
Uint1 TSrvTaskFlags
Definition: task_server.hpp:54
intr::slist_member_hook< intr::tag< SSrvShutdownList_tag > > TSrvSDListHook
ESrvTaskFlags
Type for flags in CSrvTask.
Definition: task_server.hpp:46
@ fTaskRunnable
Definition: task_server.hpp:47
@ fTaskQueued
Definition: task_server.hpp:48
@ fTaskNeedTermination
Definition: task_server.hpp:51
@ fTaskTerminated
Definition: task_server.hpp:52
@ fTaskOnTimer
Definition: task_server.hpp:50
@ fTaskRunning
Definition: task_server.hpp:49
ESrvShutdownType
Types of server shutdown procedures that one can request from CTaskServer.
@ eSrvFastShutdown
@ eSrvSlowShutdown
intr::slist< CSrvShutdownCallback, TSDListHookOpt, intr::constant_time_size< false >, intr::cache_last< false > > TShutdownList
Modified on Wed Jun 12 11:16:49 2024 by modify_doxy.py rev. 669887