SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Managers.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 
11 
12 namespace SWC { namespace client {
13 
14 
16  Comm::IoContextPtr ioctx,
17  const ContextManager::Ptr& mngr_ctx)
18  : queues(new Comm::client::ConnQueues(
19  Comm::client::Serialized::Ptr(new Comm::client::Serialized(
20  settings,
21  "MANAGER",
22  ioctx,
23  mngr_ctx
24  ? mngr_ctx
25  : ContextManager::Ptr(new ContextManager(settings))
26  )),
27  settings.get<Config::Property::Value_int32_g>(
28  "swc.client.Mngr.connection.timeout"),
29  settings.get<Config::Property::Value_uint16_g>(
30  "swc.client.Mngr.connection.probes"),
31  settings.get<Config::Property::Value_int32_g>(
32  "swc.client.Mngr.connection.keepalive"),
33  settings.get<Config::Property::Value_int32_g>(
34  "swc.client.request.again.delay")
35  )),
36  groups(Mngr::Groups::Ptr(new Mngr::Groups(settings))->init()),
37  master_ranges_cache(settings) {
38 }
39 
40 bool Managers::put(const ClientsPtr& clients,
41  const cid_t& cid, Comm::EndPoints& endpoints,
43  if(endpoints.empty()) {
44  groups->select(cid, endpoints);
45  if(endpoints.empty()) {
46  if(clients->stopping() || !req->valid()) {
47  req->handle_no_conn();
48  } else {
50  clients, cid, req)->run();
51  }
52  return false;
53  }
54  }
55  queues->get(endpoints)->put(req);
56  return true;
57 }
58 
60  const ClientsPtr& clients,
61  Comm::EndPoints& endpoints,
63  if(endpoints.empty()) {
64  groups->select(DB::Types::MngrRole::SCHEMAS, endpoints);
65  if(endpoints.empty()) {
66  if(clients->stopping() || !req->valid()) {
67  req->handle_no_conn();
68  } else {
70  clients, DB::Types::MngrRole::SCHEMAS, req)->run();
71  }
72  return false;
73  }
74  }
75  queues->get(endpoints)->put(req);
76  return true;
77 }
78 
79 
81  int64_t ms = Time::now_ms() - expiry_ms->get();
83  for(auto it = cbegin(); it != cend(); ) {
84  if(it->ts > ms) {
85  ++it;
86  } else {
87  erase(it);
88  }
89  }
90 }
91 
93  Core::MutexSptd::scope lock(m_mutex);
94  for(auto it = cbegin(); it != cend(); ++it) {
95  if(rid == it->rid) {
96  erase(it);
97  return;
98  }
99  }
100 }
101 
103  const rid_t rid,
104  const DB::Cell::Key& range_begin,
105  const DB::Cell::Key& range_end,
106  const Comm::EndPoints& endpoints,
107  const int64_t revision) {
108  int64_t ts = Time::now_ms();
109  Core::MutexSptd::scope lock(m_mutex);
110  for(auto it = cbegin(); it != cend(); ++it) {
111  if(rid == it->rid) {
112  erase(it);
113  break;
114  }
115  }
116  for(auto it = begin(); it != cend(); ++it) {
117  switch(DB::KeySeq::compare(key_seq, range_end, it->key_end)) {
118  case Condition::EQ:
119  it->change(ts, rid, range_begin, range_end, endpoints, revision);
120  return;
121  case Condition::GT:
122  insert(it, ts, rid, range_begin, range_end, endpoints, revision);
123  return;
124  default:
125  break;
126  }
127  }
128  push_back(ts, rid, range_begin, range_end, endpoints, revision);
129 }
130 
132  const DB::Cell::Key& range_begin,
133  const DB::Cell::Key& range_end,
134  rid_t& rid,
135  DB::Cell::Key& offset,
136  bool& is_end,
137  Comm::EndPoints& endpoints,
138  int64_t& revision) {
139  Core::MutexSptd::scope lock(m_mutex);
140  for(auto it = cbegin(); it != cend(); ++it) {
141  if((it->key_begin.empty() || range_end.empty() ||
142  DB::KeySeq::compare(key_seq, range_end, it->key_begin)
143  != Condition::GT)
144  &&
145  (it->key_end.empty() || range_begin.empty() ||
146  DB::KeySeq::compare(key_seq, range_begin, it->key_end)
147  != Condition::LT) ) {
148  if(Time::now_ms() - it->ts > expiry_ms->get())
149  break;
150  rid = it->rid;
151  offset.copy(it->key_begin);
152  endpoints = it->endpoints;
153  revision = it->revision;
154  is_end = it->key_end.empty();
155  return true;
156  }
157  }
158  return false;
159 }
160 
162  const DB::Cell::Key& key,
163  rid_t& rid,
164  DB::Cell::Key& key_end,
165  Comm::EndPoints& endpoints,
166  int64_t& revision) {
167  Core::MutexSptd::scope lock(m_mutex);
168  for(auto it = cbegin(); it != cend(); ++it) {
169  // it->key_begin needs to be previous range_end (key Condition::GT)
170  if(it->key_end.empty() || key.empty() ||
171  DB::KeySeq::compare(key_seq, key, it->key_end)
172  != Condition::LT ) {
173  if(Time::now_ms() - it->ts > expiry_ms->get())
174  break;
175  rid = it->rid;
176  key_end.copy(it->key_end);
177  endpoints = it->endpoints;
178  revision = it->revision;
179  return true;
180  }
181  }
182  return false;
183 }
184 
185 
186 
187 }} //namespace SWC::client
188 
SWC::Core::Vector< Range >::erase
SWC_CAN_INLINE iterator erase(size_type offset) noexcept(_NoExceptMoveAssign &&_NoExceptDestructor)
Definition: Vector.h:464
Managers.h
Clients.h
SWC::client::Managers::queues
const Comm::client::ConnQueuesPtr queues
Definition: Managers.h:227
SWC::client::ContextManager::Ptr
std::shared_ptr< ContextManager > Ptr
Definition: ContextManager.h:18
SWC::Condition::GT
@ GT
Definition: Comparators.h:30
SWC::Comm::IoContextPtr
std::shared_ptr< IoContext > IoContextPtr
Definition: IoContext.h:16
SWC::client::Managers::MasterRangesCache::Column::m_mutex
Core::MutexSptd m_mutex
Definition: Managers.h:88
SWC::Core::MutexSptd::scope
Definition: MutexSptd.h:96
SWC::Condition::LT
@ LT
Definition: Comparators.h:34
SWC::Config::Property::Value_int32_g::get
SWC_CAN_INLINE int32_t get() const noexcept
Definition: Property.h:610
SWC::DB::Cell::Key
Definition: CellKey.h:24
SWC::client::Managers::MasterRangesCache::Column::set
void set(const rid_t rid, const DB::Cell::Key &range_begin, const DB::Cell::Key &range_end, const Comm::EndPoints &endpoints, const int64_t revision)
Definition: Managers.cc:102
SWC::client::ClientsPtr
std::shared_ptr< Clients > ClientsPtr
Definition: Clients.h:23
SWC::DB::Types::MngrRole::SCHEMAS
const uint8_t SCHEMAS
Definition: MngrRole.h:15
SWC::DB::Cell::Key::empty
constexpr SWC_CAN_INLINE bool empty() const noexcept
Definition: CellKey.h:186
SWC::Core::Vector::empty
constexpr SWC_CAN_INLINE bool empty() const noexcept
Definition: Vector.h:168
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Condition::EQ
@ EQ
Definition: Comparators.h:32
SWC::client::Managers::MasterRangesCache::Column::get_write
bool get_write(const DB::Cell::Key &key, rid_t &rid, DB::Cell::Key &key_end, Comm::EndPoints &endpoints, int64_t &revision)
Definition: Managers.cc:161
SWC::DB::Cell::Key::copy
void copy(const Key &other)
Definition: CellKey.h:314
SWC::client::Managers::Managers
SWC_CAN_INLINE Managers() noexcept
Definition: Managers.h:208
SWC::client::ContextManager
Definition: ContextManager.h:15
SWC::Config::Settings
Definition: Settings.h:25
SWC::Comm::Protocol::Mngr::Req::MngrActive::make
static SWC_CAN_INLINE Ptr make(const SWC::client::Clients::Ptr &clients, const cid_t &a_cid, const DispatchHandler::Ptr &hdlr, uint32_t timeout_ms=60000)
Definition: MngrActive.h:25
SWC::client::Managers::MasterRangesCache::Column::expiry_ms
Config::Property::Value_int32_g::Ptr expiry_ms
Definition: Managers.h:86
SWC::cid_t
uint64_t cid_t
Definition: Identifiers.h:16
SWC::client::Managers::put
bool put(const ClientsPtr &clients, const cid_t &cid, Comm::EndPoints &endpoints, const Comm::client::ConnQueue::ReqBase::Ptr &req)
Definition: Managers.cc:40
SWC::Core::Vector< EndPoint >
SWC::client::Managers::MasterRangesCache::Column::clear_expired
void clear_expired() noexcept
Definition: Managers.cc:80
MngrActive.h
SWC::Core::Vector< Range >::cend
constexpr SWC_CAN_INLINE const_iterator cend() const noexcept
Definition: Vector.h:232
SWC::rid_t
uint64_t rid_t
Definition: Identifiers.h:17
SWC::client::Managers::MasterRangesCache::Column::get_read
bool get_read(const DB::Cell::Key &range_begin, const DB::Cell::Key &range_end, rid_t &rid, DB::Cell::Key &offset, bool &is_end, Comm::EndPoints &endpoints, int64_t &revision)
Definition: Managers.cc:131
SWC::Time::now_ms
SWC_CAN_INLINE int64_t now_ms() noexcept
Definition: Time.h:36
SWC::client::Managers::groups
const Mngr::Groups::Ptr groups
Definition: Managers.h:228
SWC::client::Managers::put_role_schemas
bool put_role_schemas(const ClientsPtr &clients, Comm::EndPoints &endpoints, const Comm::client::ConnQueue::ReqBase::Ptr &req)
Definition: Managers.cc:59
SWC::Comm::client::ConnQueueReqBase::Ptr
std::shared_ptr< ConnQueueReqBase > Ptr
Definition: ClientConnQueue.h:25
SWC::client::Managers::MasterRangesCache::Column::remove
void remove(const rid_t rid) noexcept
Definition: Managers.cc:92
KeyComparator.h
SWC::Core::Vector< Range >::cbegin
constexpr SWC_CAN_INLINE const_iterator cbegin() const noexcept
Definition: Vector.h:216
SWC::DB::KeySeq::compare
Condition::Comp compare(const Types::KeySeq seq, const Cell::Key &key, const Cell::Key &other) SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: KeyComparator.h:294