SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
BrokerEnv.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_broker_BrokerEnv_h
8 #define swcdb_broker_BrokerEnv_h
9 
10 
12 
18 
19 
20 namespace SWC { namespace Env {
21 
22 class Bkr final {
23  public:
24 
25  static void init() {
26  SWC_ASSERT(!m_env);
27  m_env.reset(new Bkr());
28  }
29 
30  static void start();
31 
32  static void shuttingdown() {
33  m_env->_shuttingdown();
34  }
35 
37  static bool is_accepting() noexcept {
38  return !m_env->m_not_accepting;
39  }
40 
42  static bool can_process() noexcept {
43  if(m_env->m_not_accepting)
44  return false;
45  m_env->m_in_process.fetch_add(1);
46  return true;
47  }
48 
50  static void in_process(int64_t count) noexcept {
51  m_env->m_in_process.fetch_add(count);
52  }
53 
55  static void processed() noexcept {
56  m_env->m_in_process.fetch_sub(1);
57  }
58 
60  static Bkr* get() noexcept {
61  return m_env.get();
62  }
63 
65  static Comm::IoContextPtr io() noexcept {
66  return m_env->app_io;
67  }
68 
69  template <typename T_Handler>
71  static void post(T_Handler&& handler) {
72  m_env->app_io->post(std::move(handler));
73  }
74 
76  static System::Resources& res() noexcept {
77  return m_env->_resources;
78  }
79 
82  return m_env->_reporting;
83  }
84 
85  static void reset() noexcept {
86  m_env = nullptr;
87  }
88 
92 
93  explicit Bkr();
94 
95  ~Bkr() noexcept;
96 
97  private:
98 
99  void _shuttingdown();
100 
101  void wait_if_in_process();
102 
103  inline static std::shared_ptr<Bkr> m_env = nullptr;
104 
106  Core::Atomic<int64_t> m_in_process;
107 
108 };
109 
110 
111 }} // namespace SWC::Env
112 
113 
114 
115 
116 namespace SWC { namespace Env {
117 
119  : app_io(
120  Comm::IoContext::make(
121  "Broker",
122  SWC::Env::Config::settings()->get_bool(
123  "swc.bkr.concurrency.relative"),
124  SWC::Env::Config::settings()->get_i32("swc.bkr.handlers")
125  )
126  ),
127  _reporting(
128  SWC::Env::Config::settings()->get_bool("swc.bkr.metrics.enabled")
129  ? new Broker::Metric::Reporting(
130  app_io,
131  SWC::Env::Config::settings()
132  ->get<SWC::Config::Property::Value_int32_g>(
133  "swc.bkr.metrics.report.interval"))
134  : nullptr
135  ),
136  _resources(
137  app_io,
138  SWC::Env::Config::settings()
139  ->get<SWC::Config::Property::Value_int32_g>(
140  "swc.bkr.ram.allowed.percent"),
141  SWC::Env::Config::settings()
142  ->get<SWC::Config::Property::Value_int32_g>(
143  "swc.bkr.ram.reserved.percent"),
144  SWC::Env::Config::settings()
145  ->get<SWC::Config::Property::Value_int32_g>(
146  "swc.bkr.ram.release.rate"),
147  _reporting ? &_reporting->system : nullptr,
148  nullptr
149  //[this](size_t bytes) noexcept { (void)this; (void)bytes; return 0; }
150  ),
151  m_not_accepting(false), m_in_process(0) {
152 }
153 
154 Bkr::~Bkr() noexcept { }
155 
156 void Bkr::start() {
157 }
158 
160  m_not_accepting.store(true);
161 
162  if(_reporting)
163  _reporting->stop();
164 
166 
167  _resources.stop();
168 }
169 
171  size_t n = 0;
172  while(m_in_process) {
173  std::this_thread::sleep_for(std::chrono::milliseconds(200));
174  if(!(++n % 10))
175  SWC_LOGF(LOG_WARN, "In-process=" SWC_FMT_LD " check=" SWC_FMT_LU,
176  m_in_process.load(), n);
177  }
178 }
179 
180 
181 }} // namespace SWC::Env
182 
183 
185 
186 
187 #endif // swcdb_broker_BrokerEnv_h
SWC::Env::Bkr::in_process
static SWC_CAN_INLINE void in_process(int64_t count) noexcept
Definition: BrokerEnv.h:50
SWC::System::Resources
Definition: Resources.h:50
SWC::Env::Bkr::app_io
Comm::IoContextPtr app_io
Definition: BrokerEnv.h:89
SWC::Env::Bkr::m_env
static std::shared_ptr< Bkr > m_env
Definition: BrokerEnv.h:103
MetricsReporting.h
SWC_LOGF
#define SWC_LOGF(priority, fmt,...)
Definition: Logger.h:188
SWC::Env::Bkr::shuttingdown
static void shuttingdown()
Definition: BrokerEnv.h:32
SWC::Env::Bkr::get
static SWC_CAN_INLINE Bkr * get() noexcept
Definition: BrokerEnv.h:60
SWC::System::Resources::stop
void stop()
Definition: Resources.h:148
SWC::Env::Bkr::wait_if_in_process
void wait_if_in_process()
Definition: BrokerEnv.h:170
SWC::Comm::IoContextPtr
std::shared_ptr< IoContext > IoContextPtr
Definition: IoContext.h:16
SWC::Env::Config
Definition: Settings.h:103
SWC::Env::Bkr::init
static void init()
Definition: BrokerEnv.h:25
SWC::Env::Bkr::is_accepting
static SWC_CAN_INLINE bool is_accepting() noexcept
Definition: BrokerEnv.h:37
SWC::Env::Bkr::_resources
System::Resources _resources
Definition: BrokerEnv.h:91
SWC::Core::AtomicBase::store
constexpr SWC_CAN_INLINE void store(T v) noexcept
Definition: Atomic.h:37
Scanner.h
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::Env::Bkr::res
static SWC_CAN_INLINE System::Resources & res() noexcept
Definition: BrokerEnv.h:76
SWC::Env::Bkr::reset
static void reset() noexcept
Definition: BrokerEnv.h:85
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Env::Bkr::m_in_process
Core::Atomic< int64_t > m_in_process
Definition: BrokerEnv.h:106
SWC::Env::Bkr::metrics_track
static SWC_CAN_INLINE Broker::Metric::Reporting::Ptr & metrics_track() noexcept
Definition: BrokerEnv.h:81
Commands.h
SWC_FMT_LU
#define SWC_FMT_LU
Definition: Compat.h:98
SWC::Broker::Metric::Reporting::Ptr
std::shared_ptr< Reporting > Ptr
Definition: MetricsReporting.h:26
SWC::Env::Bkr::m_not_accepting
Core::AtomicBool m_not_accepting
Definition: BrokerEnv.h:105
SWC::Env::Bkr::~Bkr
~Bkr() noexcept
Definition: BrokerEnv.h:154
Committer.h
SWC::Core::AtomicBase::load
constexpr SWC_CAN_INLINE T load() const noexcept
Definition: Atomic.h:42
SWC::Env::Bkr::_reporting
Broker::Metric::Reporting::Ptr _reporting
Definition: BrokerEnv.h:90
MetricsReporting.cc
SWC::Env::Bkr::can_process
static SWC_CAN_INLINE bool can_process() noexcept
Definition: BrokerEnv.h:42
SWC::Env::Bkr
Definition: BrokerEnv.h:22
SWC::Env::Bkr::post
static SWC_CAN_INLINE void post(T_Handler &&handler)
Definition: BrokerEnv.h:71
SWC::Env::Bkr::Bkr
Bkr()
Definition: BrokerEnv.h:118
SWC::LOG_WARN
@ LOG_WARN
Definition: Logger.h:33
SWC::Env::Bkr::io
static SWC_CAN_INLINE Comm::IoContextPtr io() noexcept
Definition: BrokerEnv.h:65
SWC::Env::Bkr::_shuttingdown
void _shuttingdown()
Definition: BrokerEnv.h:159
SWC::Env::Bkr::start
static void start()
Definition: BrokerEnv.h:156
SWC_ASSERT
#define SWC_ASSERT(_e_)
Definition: Exception.h:165
SWC_FMT_LD
#define SWC_FMT_LD
Definition: Compat.h:99
SWC::Env::Bkr::processed
static SWC_CAN_INLINE void processed() noexcept
Definition: BrokerEnv.h:55
Resources.h
Common.h
SWC::Core::AtomicBool
AtomicBase< bool > AtomicBool
Definition: Atomic.h:63