SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
RangersResources.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_RangersResources_h
7 #define swcdb_manager_RangersResources_h
8 
10 
11 namespace SWC { namespace Manager {
12 
13 
15 
17  RangerResources(rgrid_t a_rgrid = 0,
18  uint32_t a_mem = 0, uint32_t a_cpu = 0, size_t a_ranges = 0)
19  noexcept
20  : rgrid(a_rgrid), mem(a_mem), cpu(a_cpu), ranges(a_ranges),
21  load_scale(0), rebalance(0) {
22  }
23 
24  RangerResources(RangerResources&&) noexcept = default;
25  RangerResources& operator=(RangerResources&&) noexcept = default;
26 
27  RangerResources(const RangerResources&) = delete;
28  RangerResources& operator=(const RangerResources&) = delete;
29 
30  ~RangerResources() noexcept { }
31 
33  uint32_t mem;
34  uint32_t cpu;
35  size_t ranges;
36 
37  uint16_t load_scale;
38  uint8_t rebalance;
39 
40  void print(std::ostream& out) const {
41  out << "Res(load_scale=" << load_scale
42  << " rebalance=" << int16_t(rebalance)
43  << " mem=" << mem << "MB cpu=" << cpu << "Mhz ranges=" << ranges
44  << " rgrid=" << rgrid << ')';
45  }
46 };
47 
48 
49 class RangersResources final : private Core::Vector<RangerResources> {
50 
51  public:
52 
55 
57  cfg_check(
58  Env::Config::settings()->get<Config::Property::Value_int32_g>(
59  "swc.mngr.rangers.resource.interval.check")),
61  Env::Config::settings()->get<Config::Property::Value_uint8_g>(
62  "swc.mngr.rangers.range.rebalance.max")),
63  m_mutex(), m_due(0), m_last_check(0) {
64  }
65 
70 
71  ~RangersResources() noexcept { }
72 
73  void print(std::ostream& out) {
74  out << "RangersResources(rangers=";
76  out << size() << " [";
77  for(auto& r : *this)
78  r.print(out << "\n ");
79  out << "\n])";
80  }
81 
82  int64_t check(const RangerList& rangers) {
83  int64_t chk = cfg_check->get();
84  bool support;
85  if(m_mutex.try_full_lock(support)) {
86  if(!m_due) {
87  int64_t ts = Time::now_ms();
88 
89  if(ts - m_last_check >= chk) {
90  m_last_check = ts;
91  for(auto& rgr : rangers) {
92  if(rgr->state == RangerState::ACK ||
93  rgr->state == RangerState::MARKED_OFFLINE) {
96  ));
97  ++m_due;
98  }
99  }
100  } else {
101  chk -= ts - m_last_check;
102  }
103  }
104  m_mutex.unlock(support);
105  }
106  return chk;
107  }
108 
109  bool add_and_more(rgrid_t rgrid, int err,
112  if(!err && m_due) {
113  auto& res = emplace_back(rgrid, rsp.mem, rsp.cpu, rsp.ranges);
114  if(!res.mem || !res.cpu) {
116  res.print(SWC_LOG_OSTREAM << "received zero(resource) "); );
117  }
118  }
119  return m_due ? --m_due : true;
120  }
121 
122  void evaluate() {
123  // load_scale = (UINT16_MAX free , 1 overloaded)
124  size_t max_mem = 1;
125  size_t max_cpu = 1;
126  size_t max_ranges = 1;
127  uint16_t max_scale = 1;
128 
129  size_t total_ranges = 0;
130  uint16_t max_load_scale = 0;
131  uint8_t balanceable = cfg_rebalance_max->get();
132 
133  {
135  for(auto& res : *this) {
136  if(res.mem > max_mem)
137  max_mem = res.mem;
138  if(res.cpu > max_cpu)
139  max_cpu = res.cpu;
140  if(res.ranges > max_ranges)
141  max_ranges = res.ranges;
142  total_ranges += res.ranges;
143  }
144 
145  for(auto& res : *this) {
146  size_t ranges_scale = (res.ranges * 100) / max_ranges;
147  size_t load_scale = (
148  ( (res.mem * 100 * INT16_MAX) / max_mem +
149  (res.cpu * 100 * INT16_MAX) / max_cpu )
150  ) / (ranges_scale ? ranges_scale : 1);
151  res.load_scale = load_scale > UINT16_MAX
152  ? UINT16_MAX : (load_scale ? load_scale : 1);
153  if(res.load_scale > max_scale)
154  max_scale = res.load_scale;
155  }
156  for(auto& res : *this) {
157  res.load_scale = (size_t(res.load_scale) * UINT16_MAX) / max_scale;
158  if(max_load_scale < res.load_scale)
159  max_load_scale = res.load_scale;
160  }
161 
162  if(balanceable) {
163  std::sort(begin(), end(),
164  [](const RangerResources& rr1, const RangerResources& rr2) {
165  return rr1.load_scale < rr2.load_scale; });
166 
167  if(total_ranges > size() * 4 && (max_load_scale >>= 1)) {
168  for(auto& res : *this) {
169  if(res.ranges && res.load_scale < max_load_scale) {
170  uint16_t chk = max_load_scale / res.load_scale;
171  uint8_t balance = chk > balanceable ? balanceable : chk;
172  if(balance) {
173  balanceable -= balance;
174  res.rebalance = balance;
175  }
176  }
177  if(!balanceable)
178  break;
179  }
180  }
181  }
182 
183  }
184  }
185 
186  void changes(const RangerList& rangers, RangerList& changed) {
187  SWC_LOG_OUT(LOG_DEBUG, print(SWC_LOG_OSTREAM << "changes "); );
188 
190  for(auto& res : *this) {
191  for(auto& rgr : rangers) {
192  if(rgr->rgrid != res.rgrid)
193  continue;
194 
195  uint8_t balance = res.rebalance;
196  if(balance && rgr->state == RangerState::ACK) {
197  rgr->rebalance(balance);
198  } else if((balance = rgr->rebalance())) {
199  rgr->rebalance(0);
200  }
201 
202  if(balance || rgr->load_scale != res.load_scale) {
203  rgr->load_scale.store(res.load_scale);
204  changed.push_back(rgr);
205  }
206  rgr->interm_ranges.store(0);
207  break;
208  }
209  }
211  }
212 
213  private:
214 
216  size_t m_due;
217  int64_t m_last_check;
218 
219 };
220 
221 }}
222 
223 #endif // swcdb_manager_RangersResources_h
SWC::Manager::RangersResources::operator=
RangersResources & operator=(RangersResources &&)=delete
SWC::Config::Property::Value_uint8_g::get
SWC_CAN_INLINE uint8_t get() const noexcept
Definition: Property.h:535
SWC_LOG_OSTREAM
#define SWC_LOG_OSTREAM
Definition: Logger.h:44
SWC::Manager::RangerResources::RangerResources
SWC_CAN_INLINE RangerResources(rgrid_t a_rgrid=0, uint32_t a_mem=0, uint32_t a_cpu=0, size_t a_ranges=0) noexcept
Definition: RangersResources.h:17
SWC::Manager::RangerResources::load_scale
uint16_t load_scale
Definition: RangersResources.h:37
SWC::Manager::RangersResources::m_last_check
int64_t m_last_check
Definition: RangersResources.h:217
SWC::Core::Vector::clear
SWC_CAN_INLINE void clear() noexcept(_NoExceptDestructor)
Definition: Vector.h:120
SWC_LOG_OUT
#define SWC_LOG_OUT(pr, _code_)
Definition: Logger.h:178
SWC::Manager::RangersResources::add_and_more
bool add_and_more(rgrid_t rgrid, int err, const Comm::Protocol::Rgr::Params::Report::RspRes &rsp)
Definition: RangersResources.h:109
SWC::Core::MutexSptd::scope
Definition: MutexSptd.h:96
SWC::Manager::RangersResources::m_due
size_t m_due
Definition: RangersResources.h:216
SWC::Core::MutexSptd::unlock
SWC_CAN_INLINE void unlock(const bool &support) noexcept
Definition: MutexSptd.h:71
SWC::Config::Property::Value_int32_g::get
SWC_CAN_INLINE int32_t get() const noexcept
Definition: Property.h:610
SWC::Comm::Protocol::Rgr::Req::ReportRes
Definition: Report.h:49
SWC::Manager::RangersResources::changes
void changes(const RangerList &rangers, RangerList &changed)
Definition: RangersResources.h:186
SWC::DB::Types::MngrRangerState::ACK
const uint8_t ACK
Definition: MngrRangerState.h:18
SWC::Manager::RangersResources::m_mutex
Core::MutexSptd m_mutex
Definition: RangersResources.h:215
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::LOG_DEBUG
@ LOG_DEBUG
Definition: Logger.h:36
SWC::Core::Vector< RangerResources >::end
constexpr SWC_CAN_INLINE iterator end() noexcept
Definition: Vector.h:227
SWC::Manager::RangersResources::RangersResources
RangersResources(RangersResources &&)=delete
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Comm::Protocol::Rgr::Req::ReportRes::Ptr
std::shared_ptr< ReportRes > Ptr
Definition: Report.h:55
SWC::Comm::Protocol::Rgr::Params::Report::RspRes::ranges
size_t ranges
Definition: Report.h:65
SWC::Manager::RangersResources::cfg_check
const Config::Property::Value_int32_g::Ptr cfg_check
Definition: RangersResources.h:53
SWC::Manager::RangersResources::print
void print(std::ostream &out)
Definition: RangersResources.h:73
SWC::Manager::RangersResources::check
int64_t check(const RangerList &rangers)
Definition: RangersResources.h:82
SWC::Comm::Resolver::sort
void sort(const Networks &nets, const EndPoints &endpoints, EndPoints &sorted)
Definition: Resolver.cc:243
SWC::rgrid_t
uint64_t rgrid_t
Definition: Identifiers.h:18
SWC::DB::Types::MngrRangerState::MARKED_OFFLINE
const uint8_t MARKED_OFFLINE
Definition: MngrRangerState.h:20
SWC::Config::Property::Value_uint8_g
Definition: Property.h:511
ReportRes.h
SWC::Core::Vector
Definition: Vector.h:14
SWC::Manager::RangerResources::mem
uint32_t mem
Definition: RangersResources.h:33
SWC::Manager::RangerResources::cpu
uint32_t cpu
Definition: RangersResources.h:34
SWC::Manager::RangersResources::evaluate
void evaluate()
Definition: RangersResources.h:122
SWC::Manager::RangerResources::ranges
size_t ranges
Definition: RangersResources.h:35
SWC::Manager::RangerResources::rgrid
rgrid_t rgrid
Definition: RangersResources.h:32
SWC::Comm::Protocol::Rgr::Params::Report::RspRes::mem
uint32_t mem
Definition: Report.h:63
SWC::Manager::RangersResources::cfg_rebalance_max
const Config::Property::Value_uint8_g::Ptr cfg_rebalance_max
Definition: RangersResources.h:54
SWC::Manager::RangerResources::rebalance
uint8_t rebalance
Definition: RangersResources.h:38
SWC::Core::MutexSptd
Definition: MutexSptd.h:16
SWC::Time::now_ms
SWC_CAN_INLINE int64_t now_ms() noexcept
Definition: Time.h:36
SWC::Manager::RangersResources::RangersResources
RangersResources()
Definition: RangersResources.h:56
SWC::Manager::RangersResources::~RangersResources
~RangersResources() noexcept
Definition: RangersResources.h:71
SWC::LOG_WARN
@ LOG_WARN
Definition: Logger.h:33
SWC::Manager::RangersResources
Definition: RangersResources.h:49
SWC::Manager::RangerResources::RangerResources
RangerResources(RangerResources &&) noexcept=default
SWC::Manager::RangersResources::operator=
RangersResources & operator=(const RangersResources &)=delete
SWC::Manager::RangerResources
Definition: RangersResources.h:14
SWC::Core::Vector::push_back
SWC_CAN_INLINE void push_back(ArgsT &&... args)
Definition: Vector.h:331
SWC::Comm::Protocol::Rgr::Params::Report::RspRes
Definition: Report.h:53
SWC::Core::Vector< RangerResources >::size
constexpr SWC_CAN_INLINE size_type size() const noexcept
Definition: Vector.h:189
SWC::Core::MutexSptd::try_full_lock
bool try_full_lock(bool &support) noexcept
Definition: MutexSptd.h:55
SWC::Core::Vector< RangerResources >::emplace_back
SWC_CAN_INLINE reference emplace_back(ArgsT &&... args)
Definition: Vector.h:349
SWC::Manager::RangerResources::print
void print(std::ostream &out) const
Definition: RangersResources.h:40
SWC::Manager::RangersResources::RangersResources
RangersResources(const RangersResources &)=delete
SWC::Config::Property::Value_int32_g
Definition: Property.h:586
SWC::Comm::Protocol::Rgr::Params::Report::RspRes::cpu
uint32_t cpu
Definition: Report.h:64
SWC::Core::Vector< RangerResources >::begin
constexpr SWC_CAN_INLINE iterator begin() noexcept
Definition: Vector.h:211