SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
AppContext.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 #ifndef swcdb_manager_AppContext_h
7 #define swcdb_manager_AppContext_h
8 
9 
14 
15 #include "swcdb/fs/Interface.h"
17 
19 #include "swcdb/manager/MngrEnv.h"
20 
37 
38 
39 namespace SWC { namespace Manager {
40 
41 
42 class AppContext final : public Comm::AppContext {
43  public:
44 
45  typedef std::shared_ptr<AppContext> Ptr;
46 
48  : Comm::AppContext(
49  Env::Config::settings()->get<Config::Property::Value_enum_g>(
50  "swc.mngr.comm.encoder")
51  ),
52  m_srv(nullptr), m_metrics(nullptr) {
53  auto settings = Env::Config::settings();
54 
55  settings->parse_file(
56  settings->get_str("swc.mngr.cfg", ""),
57  "swc.mngr.cfg.dyn"
58  );
59 
62  *settings,
64  "Clients",
65  settings->get_bool("swc.mngr.concurrency.relative"),
66  settings->get_i32("swc.mngr.clients.handlers")
67  ),
71  new client::ContextRanger(*settings)),
73  new client::ContextBroker(*settings))
74  )->init()
75  );
76  Env::Clients::get()->set_flags__schemas_via_default();
77 
79  settings,
80  FS::fs_type(settings->get_str("swc.fs"))
81  );
82  }
83 
84  void init(const std::string& host,
85  const Comm::EndPoints& endpoints) override {
86  Env::Mngr::init(endpoints);
87 
88  auto period = Env::Config::settings()
89  ->get<Config::Property::Value_int32_g>("swc.cfg.dyn.period");
90  if(period->get()) {
91  Env::Mngr::io()->set_periodic_timer(
92  period,
93  []() noexcept { Env::Config::settings()->check_dynamic_files(); }
94  );
95  }
96 
97  int sig = 0;
98  Env::Mngr::io()->set_signals();
99  shutting_down(std::error_code(), sig);
100 
102  m_metrics->configure_mngr(host.c_str(), endpoints);
103  m_metrics->start();
104  }
105  }
106 
108  m_srv = srv;
109  }
110 
111  virtual ~AppContext() noexcept { }
112 
114  m_srv->connection_add(conn);
115  if(m_metrics)
116  m_metrics->net->connected(conn);
117  }
118 
119  void handle_disconnect(Comm::ConnHandlerPtr conn) noexcept override {
120  m_srv->connection_del(conn);
122  conn->endpoint_remote, conn->endpoint_local, true);
123  if(m_metrics)
124  m_metrics->net->disconnected(conn);
125  }
126 
127  void handle(Comm::ConnHandlerPtr conn, const Comm::Event::Ptr& ev) override {
128  // SWC_LOG_OUT(LOG_DEBUG, ev->print(SWC_LOG_OSTREAM << "handle: "); );
129 
130  #if defined(SWC_ENABLE_SANITIZER)
131  if(!Env::Mngr::role()->running())
132  return conn->do_close();
133  #endif
134 
135  if(ev->error) {
136  m_metrics->net->error(conn);
137  return;
138  }
139 
140  switch(ev->header.command) {
141 
145  break;
146 
149  break;
150 
153  break;
154 
158  break;
159 
162  break;
163 
166  break;
167 
170  break;
171 
175  break;
176 
180  break;
181 
184  break;
185 
189  break;
190 
193  break;
194 
198  break;
199 
203  break;
204 
207  break;
208 
209  //&Comm::Protocol::Mngr::Handler::debug,
210  //&Comm::Protocol::Mngr::Handler::status,
211  //&Comm::Protocol::Mngr::Handler::shutdown
212 
213  default: {
215  if(m_metrics)
216  m_metrics->net->error(conn);
217  return;
218  }
219  }
220 
221  if(m_metrics)
222  m_metrics->net->command(conn, ev->header.command);
223  }
224 
225  void net_bytes_sent(const Comm::ConnHandlerPtr& conn, size_t b)
226  noexcept override {
227  if(m_metrics)
228  m_metrics->net->sent(conn, b);
229  }
230 
231  void net_bytes_received(const Comm::ConnHandlerPtr& conn, size_t b)
232  noexcept override {
233  if(m_metrics)
234  m_metrics->net->received(conn, b);
235  }
236 
237  void net_accepted(const Comm::EndPoint& endpoint, bool secure)
238  noexcept override {
239  if(m_metrics)
240  m_metrics->net->accepted(endpoint, secure);
241  }
242 
243  void shutting_down(const std::error_code &ec, const int &sig) {
244  if(!sig) { // set signals listener
245  struct Task {
246  AppContext* ptr;
248  Task(AppContext* a_ptr) noexcept : ptr(a_ptr) { }
249  void operator()(const std::error_code& ec, const int &sig) {
250  if(ec == asio::error::operation_aborted)
251  return;
252  SWC_LOGF(LOG_INFO, "Received signal, sig=%d ec=%s",
253  sig, ec.message().c_str());
254  ptr->shutting_down(ec, sig);
255  }
256  };
257  Env::Mngr::io()->signals->async_wait(Task(this));
258 
259  SWC_LOGF(LOG_INFO, "Listening for Shutdown signal, set at sig=%d ec=%s",
260  sig, ec.message().c_str());
261  return;
262  }
263 
264  SWC_LOGF(LOG_INFO, "Shutdown signal, sig=%d ec=%s", sig, ec.message().c_str());
265 
266  if(!m_srv) {
267  SWC_LOG(LOG_INFO, "Exit");
268  SWC_QUICK_EXIT(EXIT_SUCCESS);
269  }
270 
271  std::shared_ptr<std::thread> d(new std::thread);
272  *d.get() = std::thread([d, ptr=shared_from_this()]{ ptr->stop(); });
273  d->detach();
274  }
275 
276  void stop() override {
277 
278  auto guard = m_srv->stop_accepting(); // no further requests accepted
279 
280  Env::Mngr::stop();
281 
282  #if defined(SWC_ENABLE_SANITIZER)
283  if(m_metrics)
284  m_metrics->wait();
285  #endif
286 
287  Env::Clients::get()->stop();
288 
289  Env::FsInterface::interface()->stop();
290 
291  Env::Mngr::io()->stop();
292 
293  m_srv->shutdown();
294 
295  #if defined(SWC_ENABLE_SANITIZER)
296  std::this_thread::sleep_for(std::chrono::seconds(2));
297  m_metrics = nullptr;
298  m_srv = nullptr;
302  #endif
303 
304  guard = nullptr;
305  }
306 
307  private:
310 
311 };
312 
313 
314 }}
315 
316 
317 
318 #endif // swcdb_manager_AppContext_h
SWC::Env::FsInterface::init
static void init(const SWC::Config::Settings::Ptr &settings, FS::Type typ)
Definition: Interface.cc:513
SWC::Manager::AppContext::net_bytes_sent
void net_bytes_sent(const Comm::ConnHandlerPtr &conn, size_t b) noexcept override
Definition: AppContext.h:225
RangeUnloaded.h
SWC::Comm::Protocol::Rgr::REPORT
@ REPORT
Definition: Commands.h:36
SWC::FS::fs_type
Type fs_type(const std::string &fs_name)
Definition: FileSystem.cc:19
SWC::Comm::Protocol::Mngr::RANGE_CREATE
@ RANGE_CREATE
Definition: Commands.h:72
Clients.h
SWC::Env::Clients::get
static SWC_CAN_INLINE client::Clients::Ptr & get() noexcept
Definition: Clients.h:299
SWC_LOGF
#define SWC_LOGF(priority, fmt,...)
Definition: Logger.h:188
SWC::Manager::Metric::Reporting::Ptr
std::shared_ptr< Reporting > Ptr
Definition: MetricsReporting.h:26
ColumnList.h
ColumnMng.h
SWC::Env::Mngr::post
static SWC_CAN_INLINE void post(T_Handler &&handler)
Definition: MngrEnv.h:39
RangeCreate.h
SWC::Manager::AppContext::m_metrics
Metric::Reporting::Ptr m_metrics
Definition: AppContext.h:309
SWC::Env::Config::settings
static SWC::Config::Settings::Ptr & settings()
Definition: Settings.h:128
SWC::Comm::EndPoint
asio::ip::tcp::endpoint EndPoint
Definition: Resolver.h:19
SWC::Env::FsInterface::interface
static SWC_CAN_INLINE FS::Interface::Ptr & interface() noexcept
Definition: Interface.h:150
SWC::LOG_INFO
@ LOG_INFO
Definition: Logger.h:35
SWC::Manager::AppContext::Ptr
std::shared_ptr< AppContext > Ptr
Definition: AppContext.h:45
SWC::client::ContextBroker::Ptr
std::shared_ptr< ContextBroker > Ptr
Definition: ContextBroker.h:18
Report.h
MngrEnv.h
SWC::Manager::AppContext::set_srv
void set_srv(Comm::server::SerializedServer::Ptr srv)
Definition: AppContext.h:107
SWC::Manager::AppContext::init
void init(const std::string &host, const Comm::EndPoints &endpoints) override
Definition: AppContext.h:84
SWC::Manager::AppContext::~AppContext
virtual ~AppContext() noexcept
Definition: AppContext.h:111
SWC::Comm::Protocol::Mngr::DO_ECHO
@ DO_ECHO
Definition: Commands.h:76
SWC::Comm::Protocol::Mngr::MNGR_STATE
@ MNGR_STATE
Definition: Commands.h:62
SWC::Env::Clients::init
static void init(const client::Clients::Ptr &clients)
Definition: Clients.cc:182
SWC::Comm::Protocol::Mngr::Handler::rgr_get
void rgr_get(const ConnHandlerPtr &conn, const Event::Ptr &ev)
Definition: RgrGet.h:17
RgrMngId.h
SWC::Comm::AppContext::Ptr
std::shared_ptr< AppContext > Ptr
Definition: AppContext.h:23
AppHandler.h
Echo.h
SWC::Comm::Protocol::Rgr::COLUMN_COMPACT
@ COLUMN_COMPACT
Definition: Commands.h:28
SWC::Manager::AppContext::net_bytes_received
void net_bytes_received(const Comm::ConnHandlerPtr &conn, size_t b) noexcept override
Definition: AppContext.h:231
SWC::Manager::AppContext::net_accepted
void net_accepted(const Comm::EndPoint &endpoint, bool secure) noexcept override
Definition: AppContext.h:237
AppContext.h
SWC::Comm::Protocol::Mngr::Handler::RangeRemove
Definition: RangeRemove.h:17
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC_LOG
#define SWC_LOG(priority, message)
Definition: Logger.h:191
MngrState.h
SWC::Comm::Protocol::Mngr::Handler::MngrState
Definition: MngrState.h:15
SWC::Manager::AppContext
Definition: AppContext.h:42
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Comm::Protocol::Mngr::Handler::column_get
void column_get(const ConnHandlerPtr &conn, const Event::Ptr &ev)
Definition: ColumnGet.h:113
ColumnCompact.h
SWC::Comm::AppContext
Definition: AppContext.h:21
SWC::Comm::Protocol::Common::Handler::not_implemented
void not_implemented(const ConnHandlerPtr &conn, const Event::Ptr &ev)
Definition: NotImplemented.h:16
SWC::Comm::server::SerializedServer::Ptr
std::shared_ptr< SerializedServer > Ptr
Definition: SerializedServer.h:62
SWC::Comm::Protocol::Mngr::Handler::ColumnUpdate
Definition: ColumnUpdate.h:17
SWC::Comm::Protocol::Mngr::MNGR_ACTIVE
@ MNGR_ACTIVE
Definition: Commands.h:63
SWC::Env::Mngr::metrics_track
static SWC_CAN_INLINE Manager::Metric::Reporting::Ptr & metrics_track() noexcept
Definition: MngrEnv.h:44
SWC::Comm::Protocol::Mngr::Handler::column_mng
void column_mng(const ConnHandlerPtr &conn, const Event::Ptr &ev)
Definition: ColumnMng.h:17
SWC::Env::Mngr::stop
static void stop()
Definition: MngrEnv.h:157
SWC::Comm::Protocol::Mngr::Handler::range_unloaded
void range_unloaded(const ConnHandlerPtr &conn, const Event::Ptr &ev)
Definition: RangeUnloaded.h:17
SWC::Manager::AppContext::shutting_down
void shutting_down(const std::error_code &ec, const int &sig)
Definition: AppContext.h:243
SWC::Manager::AppContext::stop
void stop() override
Definition: AppContext.h:276
SWC::Comm::ConnHandlerPtr
std::shared_ptr< ConnHandler > ConnHandlerPtr
Definition: AppContext.h:17
RgrGet.h
SWC::Comm::Protocol::Mngr::Handler::do_echo
void do_echo(const ConnHandlerPtr &conn, const Event::Ptr &ev)
Definition: Echo.h:15
SWC::Manager::MngrRole::disconnection
void disconnection(const Comm::EndPoint &endpoint_server, const Comm::EndPoint &endpoint_client, bool srv=false)
Definition: MngrRole.cc:285
SWC::Manager::AppContext::handle
void handle(Comm::ConnHandlerPtr conn, const Comm::Event::Ptr &ev) override
Definition: AppContext.h:127
SWC::Core::Vector< EndPoint >
SWC::Comm::Protocol::Mngr::RGR_GET
@ RGR_GET
Definition: Commands.h:71
SWC_QUICK_EXIT
#define SWC_QUICK_EXIT(_CODE_)
Definition: Compat.h:184
SWC::client::ContextRanger::Ptr
std::shared_ptr< ContextRanger > Ptr
Definition: ContextRanger.h:18
SWC::Env::Mngr::reset
static void reset() noexcept
Definition: MngrEnv.h:83
SWC::Manager::AppContext::m_srv
Comm::server::SerializedServer::Ptr m_srv
Definition: AppContext.h:308
SWC::Comm::Protocol::Mngr::COLUMN_GET
@ COLUMN_GET
Definition: Commands.h:66
RgrUpdate.h
SWC::Comm::Protocol::Mngr::RANGE_REMOVE
@ RANGE_REMOVE
Definition: Commands.h:74
SWC::client::ContextBroker
Definition: ContextBroker.h:15
ClientContextManager.h
NotImplemented.h
SWC::Comm::Protocol::Mngr::Handler::Report
Definition: Report.h:17
SWC::Comm::Protocol::Mngr::RANGE_UNLOADED
@ RANGE_UNLOADED
Definition: Commands.h:73
ColumnGet.h
SWC::Comm::Protocol::Mngr::COLUMN_LIST
@ COLUMN_LIST
Definition: Commands.h:67
RangeRemove.h
SWC::Comm::Protocol::Mngr::Handler::column_list
void column_list(const ConnHandlerPtr &conn, const Event::Ptr &ev)
Definition: ColumnList.h:16
SWC::Env::Clients::reset
static void reset() noexcept
Definition: Clients.h:308
SWC::Manager::AppContext::handle_disconnect
void handle_disconnect(Comm::ConnHandlerPtr conn) noexcept override
Definition: AppContext.h:119
SWC::Manager::AppContext::AppContext
AppContext()
Definition: AppContext.h:47
SWC::client::Mngr::ContextManager
Definition: ClientContextManager.h:16
SWC::Comm::Protocol::Mngr::Handler::RangeCreate
Definition: RangeCreate.h:17
SWC::client::Clients::make
static Ptr make(const Config::Settings &settings, const Comm::IoContextPtr &io_ctx, const ContextManager::Ptr &mngr_ctx, const ContextRanger::Ptr &rgr_ctx, const ContextBroker::Ptr &bkr_ctx)
Definition: Clients.cc:13
SWC::Env::Mngr::role
static SWC_CAN_INLINE Manager::MngrRole * role() noexcept
Definition: MngrEnv.h:64
MngrActive.h
SWC::Comm::Protocol::Mngr::Handler::RgrMngId
Definition: RgrMngId.h:16
SWC::Env::Mngr::init
static void init(const Comm::EndPoints &endpoints)
Definition: MngrEnv.h:27
SWC::Comm::Event::Ptr
std::shared_ptr< Event > Ptr
Definition: Event.h:33
SWC::Comm::Protocol::Mngr::COLUMN_UPDATE
@ COLUMN_UPDATE
Definition: Commands.h:65
SWC::Env::Mngr::io
static SWC_CAN_INLINE Comm::IoContextPtr io() noexcept
Definition: MngrEnv.h:33
ResponseCallback.h
DispatchHandler.h
SWC::Comm::Protocol::Mngr::Handler::mngr_active
void mngr_active(const ConnHandlerPtr &conn, const Event::Ptr &ev)
Definition: MngrActive.h:17
SWC::Comm::IoContext::make
static IoContextPtr make(std::string &&_name, int32_t size)
Definition: IoContext.h:41
SWC::Comm::Protocol::Mngr::COLUMN_MNG
@ COLUMN_MNG
Definition: Commands.h:64
Interface.h
SWC::Comm::Protocol::Mngr::Handler::RgrUpdate
Definition: RgrUpdate.h:17
SWC::Manager::AppContext::handle_established
void handle_established(Comm::ConnHandlerPtr conn) override
Definition: AppContext.h:113
SWC::Comm::Protocol::Mngr::RGR_MNG_ID
@ RGR_MNG_ID
Definition: Commands.h:69
SWC::Config::Property::Value_int32_g
Definition: Property.h:586
SWC::Env::FsInterface::reset
static void reset() noexcept
Definition: Interface.h:159
SWC::client::ContextRanger
Definition: ContextRanger.h:15
SWC::Comm::Protocol::Mngr::RGR_UPDATE
@ RGR_UPDATE
Definition: Commands.h:70
SWC::Comm::Protocol::Mngr::Handler::column_compact
void column_compact(const ConnHandlerPtr &conn, const Event::Ptr &ev)
Definition: ColumnCompact.h:17
ColumnUpdate.h