SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
RgrMngId.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 
7 #ifndef swcdb_manager_Protocol_handlers_RgrMngId_h
8 #define swcdb_manager_Protocol_handlers_RgrMngId_h
9 
11 
12 
13 namespace SWC { namespace Comm { namespace Protocol {
14 namespace Mngr { namespace Handler {
15 
16 struct RgrMngId {
19 
22  const Comm::Event::Ptr& a_ev) noexcept
23  : conn(a_conn), ev(a_ev) {
24  }
25 
27  RgrMngId(RgrMngId&& other) noexcept
28  : conn(std::move(other.conn)), ev(std::move(other.ev)) {
29  }
30 
31  RgrMngId(const RgrMngId&) = delete;
32  RgrMngId& operator=(RgrMngId&&) = delete;
33  RgrMngId& operator=(const RgrMngId&) = delete;
34 
35  ~RgrMngId() noexcept { }
36 
37  void operator()() {
38  if(ev->expired())
39  return;
40 
41  int err = Error::OK;
42  Params::RgrMngId rsp_params;
43 
44  try {
45  const uint8_t *ptr = ev->data.base;
46  size_t remain = ev->data.size;
47 
48  Params::RgrMngId req_params;
49  req_params.decode(&ptr, &remain);
50 
51  if(!Env::Mngr::role()->is_active_role(DB::Types::MngrRole::RANGERS)) {
53  SWC_LOG_OSTREAM << "MNGR NOT ACTIVE, flag=" << req_params.flag
54  << " rgrid=" << req_params.rgrid;
55  req_params.print(SWC_LOG_OSTREAM << ' ');
56  );
58  goto send_response;
59  }
60 
61  auto rangers = Env::Mngr::rangers();
62  switch(req_params.flag) {
63 
64  case Params::RgrMngId::Flag::RGR_REQ: {
65  rsp_params.rgrid = rangers->rgr_set_id(req_params.endpoints);
66  rsp_params.flag = Params::RgrMngId::Flag::MNGR_ASSIGNED;
67  rsp_params.fs = Env::FsInterface::interface()->get_type_underlying();
68 
70  SWC_LOG_OSTREAM << "RGR_REQ, rgrid=" << rsp_params.rgrid
71  << " fs=" << FS::to_string(rsp_params.fs);
72  req_params.print(SWC_LOG_OSTREAM << ' ');
73  );
74  break;
75  }
76 
77  case Params::RgrMngId::Flag::RGR_ACK: {
78  if(rangers->rgr_ack_id(req_params.rgrid, req_params.endpoints)) {
81  << "RGR_ACK, rgrid=" << req_params.rgrid;
82  req_params.print(SWC_LOG_OSTREAM << ' ');
83  );
84  rsp_params.flag = Params::RgrMngId::Flag::MNGR_ACK;
85 
86  } else {
89  << "RGR_ACK(MNGR_REREQ), rgrid=" << req_params.rgrid;
90  req_params.print(SWC_LOG_OSTREAM << ' ');
91  );
92  rsp_params.flag = Params::RgrMngId::Flag::MNGR_REREQ;
93  }
94  break;
95  }
96 
97  case Params::RgrMngId::Flag::RGR_DISAGREE: {
98  rsp_params.rgrid = rangers->rgr_had_id(
99  req_params.rgrid, req_params.endpoints);
100 
101  rsp_params.flag = rsp_params.rgrid
102  ? Params::RgrMngId::Flag::MNGR_REASSIGN
103  : Params::RgrMngId::Flag::MNGR_ACK;
104 
106  SWC_LOG_OSTREAM << "RGR_DISAGREE, rgr_had_id=" << req_params.rgrid
107  << " > rgrid=" << rsp_params.rgrid;
108  req_params.print(SWC_LOG_OSTREAM << ' ');
109  );
110  break;
111  }
112 
113  case Params::RgrMngId::Flag::RGR_SHUTTINGDOWN: {
114  rangers->rgr_shutdown(req_params.rgrid, req_params.endpoints);
115 
117  SWC_LOG_OSTREAM << "RGR_SHUTTINGDOWN, rgrid=" << req_params.rgrid;
118  req_params.print(SWC_LOG_OSTREAM << ' ');
119  );
120 
121  rsp_params.flag = Params::RgrMngId::Flag::RGR_SHUTTINGDOWN;
122  rsp_params.rgrid = req_params.rgrid;
123  break;
124  }
125 
126  default: {
129  SWC_LOG_OSTREAM << "NOT_IMPLEMENTED flag=" << req_params.flag;
130  );
131  break;
132  }
133  }
134 
135  } catch(...) {
138  err = e.code();
139  }
140 
141  send_response:
142  auto cbp = err ? Buffers::make(ev, 4) : Buffers::make(ev, rsp_params, 4);
143  cbp->append_i32(err);
144  conn->send_response(cbp);
145 
146  }
147 
148 };
149 
150 
151 }}}}}
152 
153 #endif // swcdb_manager_Protocol_handlers_RgrMngId
SWC::Error::Exception::code
constexpr SWC_CAN_INLINE int code() const noexcept
Definition: Exception.h:51
SWC_LOG_OSTREAM
#define SWC_LOG_OSTREAM
Definition: Logger.h:44
SWC_LOG_OUT
#define SWC_LOG_OUT(pr, _code_)
Definition: Logger.h:178
SWC::Comm::Protocol::Mngr::Params::RgrMngId::flag
Flag flag
Definition: RgrMngId.h:59
SWC::Env::FsInterface::interface
static SWC_CAN_INLINE FS::Interface::Ptr & interface() noexcept
Definition: Interface.h:150
SWC::Comm::Protocol::Mngr::Params::RgrMngId::rgrid
rgrid_t rgrid
Definition: RgrMngId.h:58
SWC::Comm::Protocol::Mngr::Handler::RgrMngId::operator()
void operator()()
Definition: RgrMngId.h:37
SWC::Comm::Protocol::Mngr::Handler::RgrMngId::conn
Comm::ConnHandlerPtr conn
Definition: RgrMngId.h:17
SWC::Comm::Protocol::Mngr::Handler::RgrMngId::ev
Comm::Event::Ptr ev
Definition: RgrMngId.h:18
SWC::Comm::Protocol::Mngr::Handler::RgrMngId::RgrMngId
SWC_CAN_INLINE RgrMngId(const Comm::ConnHandlerPtr &a_conn, const Comm::Event::Ptr &a_ev) noexcept
Definition: RgrMngId.h:21
SWC::Error::OK
@ OK
Definition: Error.h:45
SWC::Comm::Protocol::Mngr::Handler::RgrMngId::operator=
RgrMngId & operator=(RgrMngId &&)=delete
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::LOG_DEBUG
@ LOG_DEBUG
Definition: Logger.h:36
SWC_CURRENT_EXCEPTION
#define SWC_CURRENT_EXCEPTION(_msg_)
Definition: Exception.h:119
RgrMngId.h
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::LOG_ERROR
@ LOG_ERROR
Definition: Logger.h:32
SWC::Comm::Protocol::Mngr::Handler::RgrMngId::operator=
RgrMngId & operator=(const RgrMngId &)=delete
SWC::Comm::ConnHandlerPtr
std::shared_ptr< ConnHandler > ConnHandlerPtr
Definition: AppContext.h:17
SWC::Comm::Protocol::Mngr::Params::RgrMngId::fs
FS::Type fs
Definition: RgrMngId.h:60
SWC::Comm::Protocol::Mngr::Params::RgrMngId::print
void print(std::ostream &out) const
Definition: RgrMngId.h:48
SWC::Comm::Protocol::Mngr::Params::RgrMngId::endpoints
EndPoints endpoints
Definition: RgrMngId.h:57
SWC::Comm::Buffers::make
static SWC_CAN_INLINE Ptr make(uint32_t reserve=0)
Definition: Buffers.h:27
SWC::Error::NOT_IMPLEMENTED
@ NOT_IMPLEMENTED
Definition: Error.h:74
SWC::Comm::Protocol::Mngr::Params::RgrMngId
Definition: RgrMngId.h:17
SWC::Comm::Serializable::decode
void decode(const uint8_t **bufp, size_t *remainp)
Definition: Serializable.h:59
SWC::Env::Mngr::role
static SWC_CAN_INLINE Manager::MngrRole * role() noexcept
Definition: MngrEnv.h:64
SWC::Comm::Protocol::Mngr::Handler::RgrMngId
Definition: RgrMngId.h:16
SWC::LOG_WARN
@ LOG_WARN
Definition: Logger.h:33
SWC::Comm::Event::Ptr
std::shared_ptr< Event > Ptr
Definition: Event.h:33
SWC::Comm::Protocol::Mngr::Handler::RgrMngId::RgrMngId
SWC_CAN_INLINE RgrMngId(RgrMngId &&other) noexcept
Definition: RgrMngId.h:27
SWC::Error::MNGR_NOT_ACTIVE
@ MNGR_NOT_ACTIVE
Definition: Error.h:87
SWC::Comm::Protocol::Mngr::Handler::RgrMngId::~RgrMngId
~RgrMngId() noexcept
Definition: RgrMngId.h:35
SWC::DB::Types::MngrRole::RANGERS
const uint8_t RANGERS
Definition: MngrRole.h:16
SWC::Comm::Protocol::Mngr::Handler::RgrMngId::RgrMngId
RgrMngId(const RgrMngId &)=delete
SWC::Error::Exception
Definition: Exception.h:21
SWC::Env::Mngr::rangers
static SWC_CAN_INLINE Manager::Rangers * rangers() noexcept
Definition: MngrEnv.h:69
SWC::FS::to_string
const char *SWC_CONST_FUNC to_string(Type typ) noexcept
Definition: FileSystem.cc:47