SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
MngrActive.cc
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 
8 
9 
10 namespace SWC { namespace Comm { namespace Protocol {
11 namespace Mngr { namespace Req {
12 
13 
14 
15 void MngrActive::run_within(uint32_t t_ms) {
16  if(!hdlr->valid() ||
17  clients->stopping() ||
18  !clients->get_mngr_io()->running) {
19  hdlr->run();
20  return;
21  }
22  timer.cancel();
23  timer.expires_after(std::chrono::milliseconds(t_ms));
24  struct TimerTask {
27  TimerTask(DispatchHandler::Ptr&& a_ptr) noexcept
28  : ptr(std::move(a_ptr)) { }
30  TimerTask(TimerTask&& other) noexcept : ptr(std::move(other.ptr)) { }
31  TimerTask(const TimerTask&) = delete;
32  TimerTask& operator=(TimerTask&&) = delete;
33  TimerTask& operator=(const TimerTask&) = delete;
34  ~TimerTask() noexcept { }
35  void operator()(const asio::error_code& ec) {
36  if(ec != asio::error::operation_aborted)
37  ptr->run();
38  }
39  };
40  timer.async_wait(TimerTask(shared_from_this()));
41 }
42 
45  "MngrActive(role=%d cid=" SWC_FMT_LU " req=%s) no-conn",
46  role, cid, Core::type_name(*hdlr.get()));
47  if(!hdlr->valid() ||
48  clients->stopping() ||
49  !clients->get_mngr_io()->running) {
50  hdlr->run();
51  } else if(hosts.size() == ++nxt) {
52  nxt = 0;
53  hosts.clear();
54  run_within(1000);
55  } else {
56  run();
57  }
58 }
59 
61  if(hosts.empty()) {
62  clients->managers.groups->hosts(role, cid, hosts, group_host);
63  if(hosts.empty()) {
65  "Empty cfg of mngr.host for role=%d cid=" SWC_FMT_LU " req=%s",
66  role, cid, Core::type_name(*hdlr.get()));
67  run_within(5000);
68  return false;
69  }
70  if(nxt >= hosts.size())
71  nxt = 0;
72  }
73  clients->get_mngr_queue(hosts[nxt])->put(req());
74  return true;
75 }
76 
78  // SWC_LOGF(LOG_DEBUG, " handle: %s", ev->to_str().c_str());
79 
80  if(!ev->error) {
81  try {
82  const uint8_t *ptr = ev->data.base;
83  size_t remain = ev->data.size;
84 
85  Params::MngrActiveRsp params;
86  params.decode(&ptr, &remain);
87 
88  if(params.endpoints.size()) {
89  group_host.endpoints = std::move(params.endpoints);
90  clients->managers.groups->add(std::move(group_host));
91  hdlr->run();
92  return;
93  }
94 
95  } catch(...) {
97  }
98  }
99 
100  run_within(500);
101 }
102 
103 
104 }}}}}
SWC::Comm::client::ConnQueueReqBase::req
SWC_CAN_INLINE Ptr req() noexcept
Definition: ClientConnQueue.h:39
SWC_LOGF
#define SWC_LOGF(priority, fmt,...)
Definition: Logger.h:188
SWC::Core::Vector::clear
SWC_CAN_INLINE void clear() noexcept(_NoExceptDestructor)
Definition: Vector.h:120
SWC::Comm::Protocol::Mngr::Req::MngrActive::hdlr
DispatchHandler::Ptr hdlr
Definition: MngrActive.h:72
SWC::Comm::Protocol::Mngr::Req::MngrActive::group_host
SWC::client::Mngr::Groups::GroupHost group_host
Definition: MngrActive.h:75
SWC::Comm::Protocol::Mngr::Req::MngrActive::cid
const cid_t cid
Definition: MngrActive.h:71
SWC::Comm::Protocol::Mngr::Req::MngrActive::run_within
void run_within(uint32_t t_ms=1000)
Definition: MngrActive.cc:15
SWC::Comm::Protocol::Mngr::Req::MngrActive::clients
SWC::client::Clients::Ptr clients
Definition: MngrActive.h:69
SWC::Comm::Protocol::Mngr::Params::MngrActiveRsp::endpoints
EndPoints endpoints
Definition: MngrActive.h:58
SWC::Core::Vector::empty
constexpr SWC_CAN_INLINE bool empty() const noexcept
Definition: Vector.h:168
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::LOG_DEBUG
@ LOG_DEBUG
Definition: Logger.h:36
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Comm::Protocol::Mngr::Req::MngrActive::run
bool run() override
Definition: MngrActive.cc:60
SWC::Comm::Protocol::Mngr::Req::MngrActive::role
const uint8_t role
Definition: MngrActive.h:70
SWC_FMT_LU
#define SWC_FMT_LU
Definition: Compat.h:98
SWC::Comm::ConnHandlerPtr
std::shared_ptr< ConnHandler > ConnHandlerPtr
Definition: AppContext.h:17
SWC::Comm::Protocol::Mngr::Req::MngrActive::handle_no_conn
void handle_no_conn() override
Definition: MngrActive.cc:43
SWC::Comm::Protocol::Mngr::Req::MngrActive::timer
asio::high_resolution_timer timer
Definition: MngrActive.h:76
MngrActive.h
SWC::Core::type_name
SWC_CAN_INLINE const char * type_name(const T &obj) noexcept
Definition: Compat.h:204
SWC::Comm::Serializable::decode
void decode(const uint8_t **bufp, size_t *remainp)
Definition: Serializable.h:59
SWC::LOG_WARN
@ LOG_WARN
Definition: Logger.h:33
SWC::Comm::Protocol::Mngr::Req::MngrActive::nxt
size_t nxt
Definition: MngrActive.h:73
SWC::Comm::Event::Ptr
std::shared_ptr< Event > Ptr
Definition: Event.h:33
SWC::client::Mngr::Groups::GroupHost::endpoints
Comm::EndPoints endpoints
Definition: Groups.h:69
SWC::Comm::Protocol::Mngr::Params::MngrActiveRsp
Definition: MngrActive.h:45
SWC::Comm::Protocol::Mngr::Req::MngrActive::handle
void handle(ConnHandlerPtr conn, const Event::Ptr &ev) override
Definition: MngrActive.cc:77
SWC::Comm::DispatchHandler::Ptr
std::shared_ptr< DispatchHandler > Ptr
Definition: DispatchHandler.h:20
SWC::Comm::Protocol::Mngr::Req::MngrActive::hosts
SWC::client::Mngr::Hosts hosts
Definition: MngrActive.h:74
SWC::Core::Vector::size
constexpr SWC_CAN_INLINE size_type size() const noexcept
Definition: Vector.h:189
SWC_LOG_CURRENT_EXCEPTION
#define SWC_LOG_CURRENT_EXCEPTION(_s_)
Definition: Exception.h:144