MySQL 9.3.0
Source Code Documentation
plugin_monitor.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_REST_MRS_SRC_HELPER_PLUGIN_MONITOR_H_
27#define ROUTER_SRC_REST_MRS_SRC_HELPER_PLUGIN_MONITOR_H_
28
29#include <algorithm>
30#include <set>
31#include <string>
32
36
38
40
41namespace helper {
42
44 public:
45 using ServiceName = std::string;
46 using Services = std::set<ServiceName>;
49
50 PluginMonitor(PluginState *ps = PluginState::get_instance())
51 : ps_{ps}, observer_{new ServiceObserver(this)} {
52 observer_id_ = ps_->push_back_observer(observer_);
53 }
54
56 if (PluginState::k_invalid_id_ != observer_id_) {
57 ps_->remove_observer(observer_id_);
58 }
59
60 observer_->reset();
61 }
62
64 log_debug("wait_for_services: '%s'",
66 bool result{false};
67 observer_->wait_for_services_.wait([&services,
68 &result](PluginMonitor *state) {
69 if (nullptr == state) return true;
70
71 result = std::all_of(services.begin(), services.end(),
72 [state](const ServiceName &name) {
73 return 0 != state->active_services_.count(name);
74 });
75
76 if (result) return result;
77
78 if (std::all_of(services.begin(), services.end(),
79 [state](const ServiceName &name) {
80 return 0 !=
81 state->active_and_stopped_services_.count(name);
82 })) {
83 // result is false, and we are breaking the "wait" call.
84 return true;
85 }
86
87 return result;
88 });
89 log_debug("wait_for_services ended with '%s'",
90 (result ? "success" : "failure"));
91
92 return result;
93 }
94
95 void abort() {
96 if (observer_) observer_->reset();
97 }
98
99 Services get_active_services() { return active_services_; }
100
101 private:
103 public:
104 ServiceObserver(PluginMonitor *parent) : wait_for_services_{parent} {}
105
107 const std::vector<std::string> &active_plugins,
108 const std::vector<std::string> &stopped_plugins) override {
109 wait_for_services_.serialize_with_cv(
110 [&active_plugins, &stopped_plugins](PluginMonitor *ptr,
111 [[maybe_unused]] auto &cv) {
112 if (!ptr) return;
113
114 ptr->active_services_.clear();
115 for (auto &p : active_plugins) {
116 ptr->active_services_.insert(p);
117 ptr->active_and_stopped_services_.insert(p);
118 }
119 for (auto &p : stopped_plugins) {
120 ptr->active_and_stopped_services_.insert(p);
121 }
122 });
123 }
124
125 void on_plugin_startup([[maybe_unused]] const PluginState *state,
126 const std::string &name) override {
127 log_debug("on_plugin_startup %s", name.c_str());
128 wait_for_services_.serialize_with_cv(
129 [&name](PluginMonitor *ptr, auto &cv) {
130 if (!ptr) return;
131
132 ptr->active_services_.insert(name);
134 cv.notify_all();
135 });
136 }
137 void on_plugin_shutdown([[maybe_unused]] const PluginState *state,
138 const std::string &name) override {
139 log_debug("on_plugin_shutdown %s", name.c_str());
140 wait_for_services_.serialize_with_cv(
141 [&name](PluginMonitor *ptr, auto &cv) {
142 if (!ptr) return;
143
144 ptr->active_services_.erase(name);
146 cv.notify_all();
147 });
148 }
149
150 void reset() {
151 wait_for_services_.serialize_with_cv([](PluginMonitor *&ptr, auto &cv) {
152 ptr = nullptr;
153 cv.notify_all();
154 });
155 }
156
158 };
159
161 PluginState::ObserverId observer_id_{PluginState::k_invalid_id_};
162 std::shared_ptr<ServiceObserver> observer_;
165};
166
167} // namespace helper
168
169#endif // ROUTER_SRC_REST_MRS_SRC_HELPER_PLUGIN_MONITOR_H_
Monitor can be waited for.
Definition: monitor.h:62
Definition: plugin_monitor.h:102
WaitableMonitor< PluginMonitor * > wait_for_services_
Definition: plugin_monitor.h:157
ServiceObserver(PluginMonitor *parent)
Definition: plugin_monitor.h:104
void on_plugin_shutdown(const PluginState *state, const std::string &name) override
Definition: plugin_monitor.h:137
void on_begin_observation(const std::vector< std::string > &active_plugins, const std::vector< std::string > &stopped_plugins) override
Definition: plugin_monitor.h:106
void reset()
Definition: plugin_monitor.h:150
void on_plugin_startup(const PluginState *state, const std::string &name) override
Definition: plugin_monitor.h:125
Definition: plugin_monitor.h:43
std::shared_ptr< ServiceObserver > observer_
Definition: plugin_monitor.h:162
~PluginMonitor()
Definition: plugin_monitor.h:55
Services active_services_
Definition: plugin_monitor.h:163
void abort()
Definition: plugin_monitor.h:95
std::string ServiceName
Definition: plugin_monitor.h:45
Services get_active_services()
Definition: plugin_monitor.h:99
bool wait_for_services(const Services &services)
Definition: plugin_monitor.h:63
std::set< ServiceName > Services
Definition: plugin_monitor.h:46
Services active_and_stopped_services_
Definition: plugin_monitor.h:164
PluginState * ps_
Definition: plugin_monitor.h:160
PluginMonitor(PluginState *ps=PluginState::get_instance())
Definition: plugin_monitor.h:50
Definition: plugin_state_observer.h:36
Definition: plugin_state.h:41
uint32_t ObserverId
Definition: plugin_state.h:45
Log log_debug(std::cout, "DEBUG")
const char * p
Definition: ctype-mb.cc:1227
Logging interface for using and extending the logging subsystem.
#define IMPORT_LOG_FUNCTIONS()
convenience macro to avoid common boilerplate
Definition: logging.h:323
std::string to_string(const std::pair< PairFirst, PairSecond > &pair)
Definition: to_string.h:46
Definition: cache.h:33
Definition: services.cc:40
struct result result
Definition: result.h:34
case opt name
Definition: sslopt-case.h:29
Definition: result.h:30