]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2014 John Spray <john.spray@inktank.com> | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | */ | |
13 | ||
14 | // Include this first to get python headers earlier | |
31f18b77 | 15 | #include "Gil.h" |
7c673cae | 16 | |
7c673cae FG |
17 | #include "common/errno.h" |
18 | #include "include/stringify.h" | |
19 | ||
20 | #include "PyFormatter.h" | |
21 | ||
22 | #include "osd/OSDMap.h" | |
23 | #include "mon/MonMap.h" | |
24 | ||
25 | #include "mgr/MgrContext.h" | |
26 | ||
3efd9988 | 27 | // For ::config_prefix |
11fdf7f2 | 28 | #include "PyModule.h" |
3efd9988 FG |
29 | #include "PyModuleRegistry.h" |
30 | ||
31 | #include "ActivePyModules.h" | |
9f95a23c | 32 | #include "DaemonKey.h" |
11fdf7f2 | 33 | #include "DaemonServer.h" |
7c673cae FG |
34 | |
35 | #define dout_context g_ceph_context | |
36 | #define dout_subsys ceph_subsys_mgr | |
7c673cae | 37 | #undef dout_prefix |
9f95a23c | 38 | #define dout_prefix *_dout << "mgr " << __func__ << " " |
7c673cae | 39 | |
11fdf7f2 TL |
40 | ActivePyModules::ActivePyModules(PyModuleConfig &module_config_, |
41 | std::map<std::string, std::string> store_data, | |
3efd9988 | 42 | DaemonStateIndex &ds, ClusterState &cs, |
11fdf7f2 TL |
43 | MonClient &mc, LogChannelRef clog_, |
44 | LogChannelRef audit_clog_, Objecter &objecter_, | |
45 | Client &client_, Finisher &f, DaemonServer &server, | |
46 | PyModuleRegistry &pmr) | |
47 | : module_config(module_config_), daemon_state(ds), cluster_state(cs), | |
48 | monc(mc), clog(clog_), audit_clog(audit_clog_), objecter(objecter_), | |
49 | client(client_), finisher(f), | |
81eedcae | 50 | cmd_finisher(g_ceph_context, "cmd_finisher", "cmdfin"), |
9f95a23c | 51 | server(server), py_module_registry(pmr) |
11fdf7f2 TL |
52 | { |
53 | store_cache = std::move(store_data); | |
81eedcae | 54 | cmd_finisher.start(); |
11fdf7f2 | 55 | } |
7c673cae | 56 | |
3efd9988 | 57 | ActivePyModules::~ActivePyModules() = default; |
7c673cae | 58 | |
3efd9988 | 59 | void ActivePyModules::dump_server(const std::string &hostname, |
7c673cae FG |
60 | const DaemonStateCollection &dmc, |
61 | Formatter *f) | |
62 | { | |
63 | f->dump_string("hostname", hostname); | |
64 | f->open_array_section("services"); | |
65 | std::string ceph_version; | |
66 | ||
9f95a23c TL |
67 | for (const auto &[key, state] : dmc) { |
68 | std::lock_guard l(state->lock); | |
7c673cae FG |
69 | // TODO: pick the highest version, and make sure that |
70 | // somewhere else (during health reporting?) we are | |
71 | // indicating to the user if we see mixed versions | |
9f95a23c TL |
72 | auto ver_iter = state->metadata.find("ceph_version"); |
73 | if (ver_iter != state->metadata.end()) { | |
74 | ceph_version = state->metadata.at("ceph_version"); | |
7c673cae FG |
75 | } |
76 | ||
77 | f->open_object_section("service"); | |
9f95a23c TL |
78 | f->dump_string("type", key.type); |
79 | f->dump_string("id", key.name); | |
7c673cae FG |
80 | f->close_section(); |
81 | } | |
82 | f->close_section(); | |
83 | ||
84 | f->dump_string("ceph_version", ceph_version); | |
85 | } | |
86 | ||
87 | ||
88 | ||
3efd9988 | 89 | PyObject *ActivePyModules::get_server_python(const std::string &hostname) |
7c673cae FG |
90 | { |
91 | PyThreadState *tstate = PyEval_SaveThread(); | |
11fdf7f2 | 92 | std::lock_guard l(lock); |
7c673cae FG |
93 | PyEval_RestoreThread(tstate); |
94 | dout(10) << " (" << hostname << ")" << dendl; | |
95 | ||
96 | auto dmc = daemon_state.get_by_server(hostname); | |
97 | ||
98 | PyFormatter f; | |
99 | dump_server(hostname, dmc, &f); | |
100 | return f.get(); | |
101 | } | |
102 | ||
103 | ||
3efd9988 | 104 | PyObject *ActivePyModules::list_servers_python() |
7c673cae | 105 | { |
11fdf7f2 | 106 | PyFormatter f(false, true); |
7c673cae | 107 | PyThreadState *tstate = PyEval_SaveThread(); |
7c673cae FG |
108 | dout(10) << " >" << dendl; |
109 | ||
11fdf7f2 | 110 | daemon_state.with_daemons_by_server([this, &f, &tstate] |
3efd9988 | 111 | (const std::map<std::string, DaemonStateCollection> &all) { |
11fdf7f2 TL |
112 | PyEval_RestoreThread(tstate); |
113 | ||
3efd9988 FG |
114 | for (const auto &i : all) { |
115 | const auto &hostname = i.first; | |
7c673cae | 116 | |
3efd9988 FG |
117 | f.open_object_section("server"); |
118 | dump_server(hostname, i.second, &f); | |
119 | f.close_section(); | |
120 | } | |
121 | }); | |
7c673cae FG |
122 | |
123 | return f.get(); | |
124 | } | |
125 | ||
3efd9988 FG |
126 | PyObject *ActivePyModules::get_metadata_python( |
127 | const std::string &svc_type, | |
224ce89b | 128 | const std::string &svc_id) |
7c673cae | 129 | { |
9f95a23c | 130 | auto metadata = daemon_state.get(DaemonKey{svc_type, svc_id}); |
3efd9988 FG |
131 | if (metadata == nullptr) { |
132 | derr << "Requested missing service " << svc_type << "." << svc_id << dendl; | |
133 | Py_RETURN_NONE; | |
134 | } | |
135 | ||
11fdf7f2 | 136 | std::lock_guard l(metadata->lock); |
7c673cae FG |
137 | PyFormatter f; |
138 | f.dump_string("hostname", metadata->hostname); | |
139 | for (const auto &i : metadata->metadata) { | |
140 | f.dump_string(i.first.c_str(), i.second); | |
141 | } | |
142 | ||
143 | return f.get(); | |
144 | } | |
145 | ||
3efd9988 FG |
146 | PyObject *ActivePyModules::get_daemon_status_python( |
147 | const std::string &svc_type, | |
224ce89b WB |
148 | const std::string &svc_id) |
149 | { | |
9f95a23c | 150 | auto metadata = daemon_state.get(DaemonKey{svc_type, svc_id}); |
3efd9988 FG |
151 | if (metadata == nullptr) { |
152 | derr << "Requested missing service " << svc_type << "." << svc_id << dendl; | |
153 | Py_RETURN_NONE; | |
154 | } | |
155 | ||
11fdf7f2 | 156 | std::lock_guard l(metadata->lock); |
224ce89b WB |
157 | PyFormatter f; |
158 | for (const auto &i : metadata->service_status) { | |
159 | f.dump_string(i.first.c_str(), i.second); | |
160 | } | |
161 | return f.get(); | |
162 | } | |
7c673cae | 163 | |
3efd9988 | 164 | PyObject *ActivePyModules::get_python(const std::string &what) |
7c673cae | 165 | { |
11fdf7f2 TL |
166 | PyFormatter f; |
167 | ||
168 | // Drop the GIL, as most of the following blocks will block on | |
169 | // a mutex -- they are all responsible for re-taking the GIL before | |
170 | // touching the PyFormatter instance or returning from the function. | |
7c673cae | 171 | PyThreadState *tstate = PyEval_SaveThread(); |
7c673cae FG |
172 | |
173 | if (what == "fs_map") { | |
11fdf7f2 TL |
174 | cluster_state.with_fsmap([&f, &tstate](const FSMap &fsmap) { |
175 | PyEval_RestoreThread(tstate); | |
7c673cae FG |
176 | fsmap.dump(&f); |
177 | }); | |
178 | return f.get(); | |
179 | } else if (what == "osdmap_crush_map_text") { | |
180 | bufferlist rdata; | |
11fdf7f2 TL |
181 | cluster_state.with_osdmap([&rdata, &tstate](const OSDMap &osd_map){ |
182 | PyEval_RestoreThread(tstate); | |
183 | osd_map.crush->encode(rdata, CEPH_FEATURES_SUPPORTED_DEFAULT); | |
7c673cae FG |
184 | }); |
185 | std::string crush_text = rdata.to_str(); | |
9f95a23c | 186 | return PyUnicode_FromString(crush_text.c_str()); |
7c673cae | 187 | } else if (what.substr(0, 7) == "osd_map") { |
11fdf7f2 TL |
188 | cluster_state.with_osdmap([&f, &what, &tstate](const OSDMap &osd_map){ |
189 | PyEval_RestoreThread(tstate); | |
7c673cae FG |
190 | if (what == "osd_map") { |
191 | osd_map.dump(&f); | |
192 | } else if (what == "osd_map_tree") { | |
193 | osd_map.print_tree(&f, nullptr); | |
194 | } else if (what == "osd_map_crush") { | |
195 | osd_map.crush->dump(&f); | |
196 | } | |
197 | }); | |
198 | return f.get(); | |
eafe8130 TL |
199 | } else if (what == "modified_config_options") { |
200 | PyEval_RestoreThread(tstate); | |
201 | auto all_daemons = daemon_state.get_all(); | |
202 | set<string> names; | |
203 | for (auto& [key, daemon] : all_daemons) { | |
204 | std::lock_guard l(daemon->lock); | |
205 | for (auto& [name, valmap] : daemon->config) { | |
206 | names.insert(name); | |
207 | } | |
208 | } | |
209 | f.open_array_section("options"); | |
210 | for (auto& name : names) { | |
211 | f.dump_string("name", name); | |
212 | } | |
213 | f.close_section(); | |
214 | return f.get(); | |
1adf2230 | 215 | } else if (what.substr(0, 6) == "config") { |
11fdf7f2 | 216 | PyEval_RestoreThread(tstate); |
1adf2230 | 217 | if (what == "config_options") { |
11fdf7f2 | 218 | g_conf().config_options(&f); |
1adf2230 | 219 | } else if (what == "config") { |
11fdf7f2 | 220 | g_conf().show_config(&f); |
1adf2230 | 221 | } |
7c673cae FG |
222 | return f.get(); |
223 | } else if (what == "mon_map") { | |
7c673cae | 224 | cluster_state.with_monmap( |
11fdf7f2 TL |
225 | [&f, &tstate](const MonMap &monmap) { |
226 | PyEval_RestoreThread(tstate); | |
7c673cae FG |
227 | monmap.dump(&f); |
228 | } | |
229 | ); | |
230 | return f.get(); | |
224ce89b | 231 | } else if (what == "service_map") { |
224ce89b | 232 | cluster_state.with_servicemap( |
11fdf7f2 TL |
233 | [&f, &tstate](const ServiceMap &service_map) { |
234 | PyEval_RestoreThread(tstate); | |
224ce89b WB |
235 | service_map.dump(&f); |
236 | } | |
237 | ); | |
238 | return f.get(); | |
7c673cae | 239 | } else if (what == "osd_metadata") { |
224ce89b | 240 | auto dmc = daemon_state.get_by_service("osd"); |
11fdf7f2 TL |
241 | PyEval_RestoreThread(tstate); |
242 | ||
9f95a23c TL |
243 | for (const auto &[key, state] : dmc) { |
244 | std::lock_guard l(state->lock); | |
245 | f.open_object_section(key.name.c_str()); | |
246 | f.dump_string("hostname", state->hostname); | |
247 | for (const auto &[name, val] : state->metadata) { | |
248 | f.dump_string(name.c_str(), val); | |
249 | } | |
250 | f.close_section(); | |
251 | } | |
252 | return f.get(); | |
253 | } else if (what == "mds_metadata") { | |
254 | auto dmc = daemon_state.get_by_service("mds"); | |
255 | PyEval_RestoreThread(tstate); | |
256 | ||
257 | for (const auto &[key, state] : dmc) { | |
258 | std::lock_guard l(state->lock); | |
259 | f.open_object_section(key.name.c_str()); | |
260 | f.dump_string("hostname", state->hostname); | |
261 | for (const auto &[name, val] : state->metadata) { | |
262 | f.dump_string(name.c_str(), val); | |
7c673cae FG |
263 | } |
264 | f.close_section(); | |
265 | } | |
266 | return f.get(); | |
267 | } else if (what == "pg_summary") { | |
7c673cae | 268 | cluster_state.with_pgmap( |
11fdf7f2 TL |
269 | [&f, &tstate](const PGMap &pg_map) { |
270 | PyEval_RestoreThread(tstate); | |
271 | ||
7c673cae FG |
272 | std::map<std::string, std::map<std::string, uint32_t> > osds; |
273 | std::map<std::string, std::map<std::string, uint32_t> > pools; | |
274 | std::map<std::string, uint32_t> all; | |
275 | for (const auto &i : pg_map.pg_stat) { | |
276 | const auto pool = i.first.m_pool; | |
277 | const std::string state = pg_state_string(i.second.state); | |
278 | // Insert to per-pool map | |
279 | pools[stringify(pool)][state]++; | |
280 | for (const auto &osd_id : i.second.acting) { | |
281 | osds[stringify(osd_id)][state]++; | |
282 | } | |
283 | all[state]++; | |
284 | } | |
285 | f.open_object_section("by_osd"); | |
286 | for (const auto &i : osds) { | |
287 | f.open_object_section(i.first.c_str()); | |
288 | for (const auto &j : i.second) { | |
289 | f.dump_int(j.first.c_str(), j.second); | |
290 | } | |
291 | f.close_section(); | |
292 | } | |
293 | f.close_section(); | |
294 | f.open_object_section("by_pool"); | |
295 | for (const auto &i : pools) { | |
296 | f.open_object_section(i.first.c_str()); | |
297 | for (const auto &j : i.second) { | |
298 | f.dump_int(j.first.c_str(), j.second); | |
299 | } | |
300 | f.close_section(); | |
301 | } | |
302 | f.close_section(); | |
303 | f.open_object_section("all"); | |
304 | for (const auto &i : all) { | |
305 | f.dump_int(i.first.c_str(), i.second); | |
306 | } | |
307 | f.close_section(); | |
28e407b8 AA |
308 | f.open_object_section("pg_stats_sum"); |
309 | pg_map.pg_sum.dump(&f); | |
310 | f.close_section(); | |
7c673cae FG |
311 | } |
312 | ); | |
313 | return f.get(); | |
3efd9988 | 314 | } else if (what == "pg_status") { |
3efd9988 | 315 | cluster_state.with_pgmap( |
11fdf7f2 TL |
316 | [&f, &tstate](const PGMap &pg_map) { |
317 | PyEval_RestoreThread(tstate); | |
3efd9988 FG |
318 | pg_map.print_summary(&f, nullptr); |
319 | } | |
320 | ); | |
321 | return f.get(); | |
322 | } else if (what == "pg_dump") { | |
11fdf7f2 TL |
323 | cluster_state.with_pgmap( |
324 | [&f, &tstate](const PGMap &pg_map) { | |
325 | PyEval_RestoreThread(tstate); | |
9f95a23c | 326 | pg_map.dump(&f, false); |
11fdf7f2 TL |
327 | } |
328 | ); | |
329 | return f.get(); | |
330 | } else if (what == "devices") { | |
331 | daemon_state.with_devices2( | |
332 | [&tstate, &f]() { | |
333 | PyEval_RestoreThread(tstate); | |
334 | f.open_array_section("devices"); | |
335 | }, | |
336 | [&f] (const DeviceState& dev) { | |
337 | f.dump_object("device", dev); | |
338 | }); | |
339 | f.close_section(); | |
340 | return f.get(); | |
341 | } else if (what.size() > 7 && | |
342 | what.substr(0, 7) == "device ") { | |
343 | string devid = what.substr(7); | |
eafe8130 TL |
344 | if (!daemon_state.with_device( |
345 | devid, | |
346 | [&f, &tstate] (const DeviceState& dev) { | |
347 | PyEval_RestoreThread(tstate); | |
348 | f.dump_object("device", dev); | |
349 | })) { | |
350 | // device not found | |
351 | PyEval_RestoreThread(tstate); | |
352 | } | |
11fdf7f2 TL |
353 | return f.get(); |
354 | } else if (what == "io_rate") { | |
355 | cluster_state.with_pgmap( | |
356 | [&f, &tstate](const PGMap &pg_map) { | |
357 | PyEval_RestoreThread(tstate); | |
358 | pg_map.dump_delta(&f); | |
359 | } | |
3efd9988 FG |
360 | ); |
361 | return f.get(); | |
7c673cae | 362 | } else if (what == "df") { |
11fdf7f2 | 363 | cluster_state.with_osdmap_and_pgmap( |
9f95a23c | 364 | [&f, &tstate]( |
11fdf7f2 TL |
365 | const OSDMap& osd_map, |
366 | const PGMap &pg_map) { | |
367 | PyEval_RestoreThread(tstate); | |
368 | pg_map.dump_cluster_stats(nullptr, &f, true); | |
31f18b77 | 369 | pg_map.dump_pool_stats_full(osd_map, nullptr, &f, true); |
7c673cae | 370 | }); |
7c673cae | 371 | return f.get(); |
9f95a23c TL |
372 | } else if (what == "pg_stats") { |
373 | cluster_state.with_pgmap( | |
374 | [&f, &tstate](const PGMap &pg_map) { | |
375 | PyEval_RestoreThread(tstate); | |
376 | pg_map.dump_pg_stats(&f, false); | |
377 | }); | |
378 | return f.get(); | |
379 | } else if (what == "pool_stats") { | |
380 | cluster_state.with_pgmap( | |
381 | [&f, &tstate](const PGMap &pg_map) { | |
382 | PyEval_RestoreThread(tstate); | |
383 | pg_map.dump_pool_stats(&f); | |
384 | }); | |
385 | return f.get(); | |
386 | } else if (what == "pg_ready") { | |
387 | PyEval_RestoreThread(tstate); | |
388 | server.dump_pg_ready(&f); | |
389 | return f.get(); | |
7c673cae | 390 | } else if (what == "osd_stats") { |
7c673cae | 391 | cluster_state.with_pgmap( |
11fdf7f2 TL |
392 | [&f, &tstate](const PGMap &pg_map) { |
393 | PyEval_RestoreThread(tstate); | |
ded94939 | 394 | pg_map.dump_osd_stats(&f, false); |
7c673cae FG |
395 | }); |
396 | return f.get(); | |
9f95a23c TL |
397 | } else if (what == "osd_ping_times") { |
398 | cluster_state.with_pgmap( | |
399 | [&f, &tstate](const PGMap &pg_map) { | |
400 | PyEval_RestoreThread(tstate); | |
401 | pg_map.dump_osd_ping_times(&f); | |
402 | }); | |
403 | return f.get(); | |
11fdf7f2 TL |
404 | } else if (what == "osd_pool_stats") { |
405 | int64_t poolid = -ENOENT; | |
11fdf7f2 TL |
406 | cluster_state.with_osdmap_and_pgmap([&](const OSDMap& osdmap, |
407 | const PGMap& pg_map) { | |
408 | PyEval_RestoreThread(tstate); | |
409 | f.open_array_section("pool_stats"); | |
410 | for (auto &p : osdmap.get_pools()) { | |
411 | poolid = p.first; | |
412 | pg_map.dump_pool_stats_and_io_rate(poolid, osdmap, &f, nullptr); | |
413 | } | |
414 | f.close_section(); | |
415 | }); | |
416 | return f.get(); | |
7c673cae | 417 | } else if (what == "health" || what == "mon_status") { |
7c673cae FG |
418 | bufferlist json; |
419 | if (what == "health") { | |
420 | json = cluster_state.get_health(); | |
421 | } else if (what == "mon_status") { | |
422 | json = cluster_state.get_mon_status(); | |
423 | } else { | |
11fdf7f2 | 424 | ceph_abort(); |
7c673cae | 425 | } |
11fdf7f2 TL |
426 | |
427 | PyEval_RestoreThread(tstate); | |
7c673cae FG |
428 | f.dump_string("json", json.to_str()); |
429 | return f.get(); | |
224ce89b | 430 | } else if (what == "mgr_map") { |
11fdf7f2 TL |
431 | cluster_state.with_mgrmap([&f, &tstate](const MgrMap &mgr_map) { |
432 | PyEval_RestoreThread(tstate); | |
224ce89b WB |
433 | mgr_map.dump(&f); |
434 | }); | |
435 | return f.get(); | |
7c673cae FG |
436 | } else { |
437 | derr << "Python module requested unknown data '" << what << "'" << dendl; | |
11fdf7f2 | 438 | PyEval_RestoreThread(tstate); |
7c673cae FG |
439 | Py_RETURN_NONE; |
440 | } | |
441 | } | |
442 | ||
11fdf7f2 | 443 | void ActivePyModules::start_one(PyModuleRef py_module) |
7c673cae | 444 | { |
11fdf7f2 TL |
445 | std::lock_guard l(lock); |
446 | ||
11fdf7f2 | 447 | const auto name = py_module->get_name(); |
9f95a23c TL |
448 | auto em = modules.emplace(name, |
449 | std::make_shared<ActivePyModule>(py_module, clog)); | |
450 | ceph_assert(em.second); // actually inserted | |
451 | auto& active_module = em.first->second; | |
11fdf7f2 TL |
452 | |
453 | // Send all python calls down a Finisher to avoid blocking | |
454 | // C++ code, and avoid any potential lock cycles. | |
9f95a23c | 455 | finisher.queue(new LambdaContext([this, active_module, name](int) { |
11fdf7f2 TL |
456 | int r = active_module->load(this); |
457 | if (r != 0) { | |
458 | derr << "Failed to run module in active mode ('" << name << "')" | |
459 | << dendl; | |
460 | std::lock_guard l(lock); | |
461 | modules.erase(name); | |
462 | } else { | |
463 | dout(4) << "Starting thread for " << name << dendl; | |
464 | active_module->thread.create(active_module->get_thread_name()); | |
465 | } | |
466 | })); | |
7c673cae FG |
467 | } |
468 | ||
3efd9988 | 469 | void ActivePyModules::shutdown() |
7c673cae | 470 | { |
11fdf7f2 | 471 | std::lock_guard locker(lock); |
7c673cae FG |
472 | |
473 | // Signal modules to drop out of serve() and/or tear down resources | |
9f95a23c TL |
474 | for (auto& [name, module] : modules) { |
475 | lock.unlock(); | |
3efd9988 | 476 | dout(10) << "calling module " << name << " shutdown()" << dendl; |
7c673cae | 477 | module->shutdown(); |
3efd9988 | 478 | dout(10) << "module " << name << " shutdown() returned" << dendl; |
9f95a23c | 479 | lock.lock(); |
7c673cae FG |
480 | } |
481 | ||
482 | // For modules implementing serve(), finish the threads where we | |
483 | // were running that. | |
9f95a23c TL |
484 | for (auto& [name, module] : modules) { |
485 | lock.unlock(); | |
486 | dout(10) << "joining module " << name << dendl; | |
487 | module->thread.join(); | |
488 | dout(10) << "joined module " << name << dendl; | |
489 | lock.lock(); | |
7c673cae | 490 | } |
7c673cae | 491 | |
81eedcae TL |
492 | cmd_finisher.wait_for_empty(); |
493 | cmd_finisher.stop(); | |
494 | ||
7c673cae | 495 | modules.clear(); |
7c673cae FG |
496 | } |
497 | ||
3efd9988 | 498 | void ActivePyModules::notify_all(const std::string ¬ify_type, |
7c673cae FG |
499 | const std::string ¬ify_id) |
500 | { | |
11fdf7f2 | 501 | std::lock_guard l(lock); |
7c673cae FG |
502 | |
503 | dout(10) << __func__ << ": notify_all " << notify_type << dendl; | |
9f95a23c | 504 | for (auto& [name, module] : modules) { |
7c673cae FG |
505 | // Send all python calls down a Finisher to avoid blocking |
506 | // C++ code, and avoid any potential lock cycles. | |
9f95a23c TL |
507 | dout(15) << "queuing notify to " << name << dendl; |
508 | // workaround for https://bugs.llvm.org/show_bug.cgi?id=35984 | |
509 | finisher.queue(new LambdaContext([module=module, notify_type, notify_id] | |
510 | (int r){ | |
511 | module->notify(notify_type, notify_id); | |
7c673cae FG |
512 | })); |
513 | } | |
514 | } | |
515 | ||
3efd9988 | 516 | void ActivePyModules::notify_all(const LogEntry &log_entry) |
7c673cae | 517 | { |
11fdf7f2 | 518 | std::lock_guard l(lock); |
7c673cae FG |
519 | |
520 | dout(10) << __func__ << ": notify_all (clog)" << dendl; | |
9f95a23c | 521 | for (auto& [name, module] : modules) { |
7c673cae FG |
522 | // Send all python calls down a Finisher to avoid blocking |
523 | // C++ code, and avoid any potential lock cycles. | |
524 | // | |
525 | // Note intentional use of non-reference lambda binding on | |
526 | // log_entry: we take a copy because caller's instance is | |
527 | // probably ephemeral. | |
9f95a23c TL |
528 | dout(15) << "queuing notify (clog) to " << name << dendl; |
529 | // workaround for https://bugs.llvm.org/show_bug.cgi?id=35984 | |
530 | finisher.queue(new LambdaContext([module=module, log_entry](int r){ | |
7c673cae FG |
531 | module->notify_clog(log_entry); |
532 | })); | |
533 | } | |
534 | } | |
535 | ||
11fdf7f2 | 536 | bool ActivePyModules::get_store(const std::string &module_name, |
7c673cae FG |
537 | const std::string &key, std::string *val) const |
538 | { | |
11fdf7f2 TL |
539 | PyThreadState *tstate = PyEval_SaveThread(); |
540 | std::lock_guard l(lock); | |
541 | PyEval_RestoreThread(tstate); | |
542 | ||
543 | const std::string global_key = PyModule::config_prefix | |
3efd9988 | 544 | + module_name + "/" + key; |
31f18b77 | 545 | |
11fdf7f2 | 546 | dout(4) << __func__ << " key: " << global_key << dendl; |
7c673cae | 547 | |
11fdf7f2 TL |
548 | auto i = store_cache.find(global_key); |
549 | if (i != store_cache.end()) { | |
550 | *val = i->second; | |
551 | return true; | |
552 | } else { | |
553 | return false; | |
554 | } | |
555 | } | |
556 | ||
557 | PyObject *ActivePyModules::dispatch_remote( | |
558 | const std::string &other_module, | |
559 | const std::string &method, | |
560 | PyObject *args, | |
561 | PyObject *kwargs, | |
562 | std::string *err) | |
563 | { | |
564 | auto mod_iter = modules.find(other_module); | |
565 | ceph_assert(mod_iter != modules.end()); | |
566 | ||
567 | return mod_iter->second->dispatch_remote(method, args, kwargs, err); | |
568 | } | |
a8e16298 | 569 | |
11fdf7f2 TL |
570 | bool ActivePyModules::get_config(const std::string &module_name, |
571 | const std::string &key, std::string *val) const | |
572 | { | |
573 | const std::string global_key = PyModule::config_prefix | |
574 | + module_name + "/" + key; | |
575 | ||
9f95a23c | 576 | dout(20) << " key: " << global_key << dendl; |
11fdf7f2 TL |
577 | |
578 | std::lock_guard lock(module_config.lock); | |
9f95a23c | 579 | |
11fdf7f2 TL |
580 | auto i = module_config.config.find(global_key); |
581 | if (i != module_config.config.end()) { | |
582 | *val = i->second; | |
7c673cae FG |
583 | return true; |
584 | } else { | |
585 | return false; | |
586 | } | |
587 | } | |
588 | ||
11fdf7f2 TL |
589 | PyObject *ActivePyModules::get_typed_config( |
590 | const std::string &module_name, | |
591 | const std::string &key, | |
592 | const std::string &prefix) const | |
593 | { | |
594 | PyThreadState *tstate = PyEval_SaveThread(); | |
595 | std::string value; | |
596 | std::string final_key; | |
597 | bool found = false; | |
598 | if (prefix.size()) { | |
599 | final_key = prefix + "/" + key; | |
600 | found = get_config(module_name, final_key, &value); | |
601 | } | |
602 | if (!found) { | |
603 | final_key = key; | |
604 | found = get_config(module_name, final_key, &value); | |
605 | } | |
606 | if (found) { | |
607 | PyModuleRef module = py_module_registry.get_module(module_name); | |
608 | PyEval_RestoreThread(tstate); | |
609 | if (!module) { | |
610 | derr << "Module '" << module_name << "' is not available" << dendl; | |
611 | Py_RETURN_NONE; | |
612 | } | |
613 | dout(10) << __func__ << " " << final_key << " found: " << value << dendl; | |
614 | return module->get_typed_option_value(key, value); | |
615 | } | |
616 | PyEval_RestoreThread(tstate); | |
617 | if (prefix.size()) { | |
9f95a23c | 618 | dout(10) << " [" << prefix << "/]" << key << " not found " |
11fdf7f2 TL |
619 | << dendl; |
620 | } else { | |
9f95a23c | 621 | dout(10) << " " << key << " not found " << dendl; |
11fdf7f2 TL |
622 | } |
623 | Py_RETURN_NONE; | |
624 | } | |
625 | ||
626 | PyObject *ActivePyModules::get_store_prefix(const std::string &module_name, | |
31f18b77 FG |
627 | const std::string &prefix) const |
628 | { | |
629 | PyThreadState *tstate = PyEval_SaveThread(); | |
11fdf7f2 TL |
630 | std::lock_guard l(lock); |
631 | std::lock_guard lock(module_config.lock); | |
31f18b77 FG |
632 | PyEval_RestoreThread(tstate); |
633 | ||
11fdf7f2 | 634 | const std::string base_prefix = PyModule::config_prefix |
3efd9988 | 635 | + module_name + "/"; |
31f18b77 | 636 | const std::string global_prefix = base_prefix + prefix; |
11fdf7f2 | 637 | dout(4) << __func__ << " prefix: " << global_prefix << dendl; |
31f18b77 FG |
638 | |
639 | PyFormatter f; | |
9f95a23c | 640 | |
11fdf7f2 TL |
641 | for (auto p = store_cache.lower_bound(global_prefix); |
642 | p != store_cache.end() && p->first.find(global_prefix) == 0; | |
31f18b77 FG |
643 | ++p) { |
644 | f.dump_string(p->first.c_str() + base_prefix.size(), p->second); | |
645 | } | |
646 | return f.get(); | |
647 | } | |
648 | ||
11fdf7f2 | 649 | void ActivePyModules::set_store(const std::string &module_name, |
d2e6a577 | 650 | const std::string &key, const boost::optional<std::string>& val) |
7c673cae | 651 | { |
11fdf7f2 | 652 | const std::string global_key = PyModule::config_prefix |
3efd9988 | 653 | + module_name + "/" + key; |
9f95a23c | 654 | |
7c673cae FG |
655 | Command set_cmd; |
656 | { | |
11fdf7f2 | 657 | std::lock_guard l(lock); |
d2e6a577 | 658 | if (val) { |
11fdf7f2 | 659 | store_cache[global_key] = *val; |
d2e6a577 | 660 | } else { |
11fdf7f2 | 661 | store_cache.erase(global_key); |
d2e6a577 | 662 | } |
7c673cae FG |
663 | |
664 | std::ostringstream cmd_json; | |
7c673cae FG |
665 | JSONFormatter jf; |
666 | jf.open_object_section("cmd"); | |
d2e6a577 FG |
667 | if (val) { |
668 | jf.dump_string("prefix", "config-key set"); | |
669 | jf.dump_string("key", global_key); | |
670 | jf.dump_string("val", *val); | |
671 | } else { | |
672 | jf.dump_string("prefix", "config-key del"); | |
673 | jf.dump_string("key", global_key); | |
674 | } | |
7c673cae FG |
675 | jf.close_section(); |
676 | jf.flush(cmd_json); | |
7c673cae FG |
677 | set_cmd.run(&monc, cmd_json.str()); |
678 | } | |
679 | set_cmd.wait(); | |
680 | ||
681 | if (set_cmd.r != 0) { | |
c07f9fc5 | 682 | // config-key set will fail if mgr's auth key has insufficient |
7c673cae FG |
683 | // permission to set config keys |
684 | // FIXME: should this somehow raise an exception back into Python land? | |
c07f9fc5 | 685 | dout(0) << "`config-key set " << global_key << " " << val << "` failed: " |
7c673cae FG |
686 | << cpp_strerror(set_cmd.r) << dendl; |
687 | dout(0) << "mon returned " << set_cmd.r << ": " << set_cmd.outs << dendl; | |
688 | } | |
689 | } | |
690 | ||
11fdf7f2 TL |
691 | void ActivePyModules::set_config(const std::string &module_name, |
692 | const std::string &key, const boost::optional<std::string>& val) | |
c07f9fc5 | 693 | { |
11fdf7f2 | 694 | module_config.set_config(&monc, module_name, key, val); |
c07f9fc5 FG |
695 | } |
696 | ||
3efd9988 | 697 | std::map<std::string, std::string> ActivePyModules::get_services() const |
7c673cae | 698 | { |
3efd9988 | 699 | std::map<std::string, std::string> result; |
11fdf7f2 | 700 | std::lock_guard l(lock); |
9f95a23c | 701 | for (const auto& [name, module] : modules) { |
3efd9988 FG |
702 | std::string svc_str = module->get_uri(); |
703 | if (!svc_str.empty()) { | |
9f95a23c | 704 | result[name] = svc_str; |
3efd9988 FG |
705 | } |
706 | } | |
7c673cae | 707 | |
3efd9988 | 708 | return result; |
7c673cae FG |
709 | } |
710 | ||
11fdf7f2 TL |
711 | PyObject* ActivePyModules::with_perf_counters( |
712 | std::function<void(PerfCounterInstance& counter_instance, PerfCounterType& counter_type, PyFormatter& f)> fct, | |
224ce89b | 713 | const std::string &svc_name, |
7c673cae | 714 | const std::string &svc_id, |
11fdf7f2 | 715 | const std::string &path) const |
7c673cae FG |
716 | { |
717 | PyThreadState *tstate = PyEval_SaveThread(); | |
11fdf7f2 | 718 | std::lock_guard l(lock); |
7c673cae FG |
719 | PyEval_RestoreThread(tstate); |
720 | ||
721 | PyFormatter f; | |
722 | f.open_array_section(path.c_str()); | |
723 | ||
9f95a23c | 724 | auto metadata = daemon_state.get(DaemonKey{svc_name, svc_id}); |
7c673cae | 725 | if (metadata) { |
11fdf7f2 | 726 | std::lock_guard l2(metadata->lock); |
7c673cae FG |
727 | if (metadata->perf_counters.instances.count(path)) { |
728 | auto counter_instance = metadata->perf_counters.instances.at(path); | |
28e407b8 | 729 | auto counter_type = metadata->perf_counters.types.at(path); |
11fdf7f2 | 730 | fct(counter_instance, counter_type, f); |
7c673cae FG |
731 | } else { |
732 | dout(4) << "Missing counter: '" << path << "' (" | |
11fdf7f2 | 733 | << svc_name << "." << svc_id << ")" << dendl; |
7c673cae FG |
734 | dout(20) << "Paths are:" << dendl; |
735 | for (const auto &i : metadata->perf_counters.instances) { | |
736 | dout(20) << i.first << dendl; | |
737 | } | |
738 | } | |
739 | } else { | |
740 | dout(4) << "No daemon state for " | |
11fdf7f2 | 741 | << svc_name << "." << svc_id << ")" << dendl; |
7c673cae FG |
742 | } |
743 | f.close_section(); | |
744 | return f.get(); | |
745 | } | |
746 | ||
11fdf7f2 TL |
747 | PyObject* ActivePyModules::get_counter_python( |
748 | const std::string &svc_name, | |
749 | const std::string &svc_id, | |
750 | const std::string &path) | |
751 | { | |
752 | auto extract_counters = []( | |
753 | PerfCounterInstance& counter_instance, | |
754 | PerfCounterType& counter_type, | |
755 | PyFormatter& f) | |
756 | { | |
757 | if (counter_type.type & PERFCOUNTER_LONGRUNAVG) { | |
758 | const auto &avg_data = counter_instance.get_data_avg(); | |
759 | for (const auto &datapoint : avg_data) { | |
760 | f.open_array_section("datapoint"); | |
9f95a23c | 761 | f.dump_float("t", datapoint.t); |
11fdf7f2 TL |
762 | f.dump_unsigned("s", datapoint.s); |
763 | f.dump_unsigned("c", datapoint.c); | |
764 | f.close_section(); | |
765 | } | |
766 | } else { | |
767 | const auto &data = counter_instance.get_data(); | |
768 | for (const auto &datapoint : data) { | |
769 | f.open_array_section("datapoint"); | |
9f95a23c | 770 | f.dump_float("t", datapoint.t); |
11fdf7f2 TL |
771 | f.dump_unsigned("v", datapoint.v); |
772 | f.close_section(); | |
773 | } | |
774 | } | |
775 | }; | |
776 | return with_perf_counters(extract_counters, svc_name, svc_id, path); | |
777 | } | |
778 | ||
779 | PyObject* ActivePyModules::get_latest_counter_python( | |
780 | const std::string &svc_name, | |
781 | const std::string &svc_id, | |
782 | const std::string &path) | |
783 | { | |
784 | auto extract_latest_counters = []( | |
785 | PerfCounterInstance& counter_instance, | |
786 | PerfCounterType& counter_type, | |
787 | PyFormatter& f) | |
788 | { | |
789 | if (counter_type.type & PERFCOUNTER_LONGRUNAVG) { | |
790 | const auto &datapoint = counter_instance.get_latest_data_avg(); | |
9f95a23c | 791 | f.dump_float("t", datapoint.t); |
11fdf7f2 TL |
792 | f.dump_unsigned("s", datapoint.s); |
793 | f.dump_unsigned("c", datapoint.c); | |
794 | } else { | |
795 | const auto &datapoint = counter_instance.get_latest_data(); | |
9f95a23c | 796 | f.dump_float("t", datapoint.t); |
11fdf7f2 TL |
797 | f.dump_unsigned("v", datapoint.v); |
798 | } | |
799 | }; | |
800 | return with_perf_counters(extract_latest_counters, svc_name, svc_id, path); | |
801 | } | |
802 | ||
3efd9988 | 803 | PyObject* ActivePyModules::get_perf_schema_python( |
11fdf7f2 | 804 | const std::string &svc_type, |
c07f9fc5 FG |
805 | const std::string &svc_id) |
806 | { | |
807 | PyThreadState *tstate = PyEval_SaveThread(); | |
11fdf7f2 | 808 | std::lock_guard l(lock); |
c07f9fc5 FG |
809 | PyEval_RestoreThread(tstate); |
810 | ||
3efd9988 | 811 | DaemonStateCollection daemons; |
c07f9fc5 FG |
812 | |
813 | if (svc_type == "") { | |
11fdf7f2 | 814 | daemons = daemon_state.get_all(); |
c07f9fc5 | 815 | } else if (svc_id.empty()) { |
11fdf7f2 | 816 | daemons = daemon_state.get_by_service(svc_type); |
c07f9fc5 | 817 | } else { |
9f95a23c | 818 | auto key = DaemonKey{svc_type, svc_id}; |
c07f9fc5 | 819 | // so that the below can be a loop in all cases |
3efd9988 FG |
820 | auto got = daemon_state.get(key); |
821 | if (got != nullptr) { | |
822 | daemons[key] = got; | |
c07f9fc5 FG |
823 | } |
824 | } | |
825 | ||
826 | PyFormatter f; | |
3efd9988 | 827 | if (!daemons.empty()) { |
9f95a23c TL |
828 | for (auto& [key, state] : daemons) { |
829 | f.open_object_section(ceph::to_string(key).c_str()); | |
c07f9fc5 | 830 | |
11fdf7f2 | 831 | std::lock_guard l(state->lock); |
3efd9988 FG |
832 | for (auto ctr_inst_iter : state->perf_counters.instances) { |
833 | const auto &counter_name = ctr_inst_iter.first; | |
834 | f.open_object_section(counter_name.c_str()); | |
835 | auto type = state->perf_counters.types[counter_name]; | |
c07f9fc5 FG |
836 | f.dump_string("description", type.description); |
837 | if (!type.nick.empty()) { | |
838 | f.dump_string("nick", type.nick); | |
839 | } | |
840 | f.dump_unsigned("type", type.type); | |
3efd9988 | 841 | f.dump_unsigned("priority", type.priority); |
1adf2230 | 842 | f.dump_unsigned("units", type.unit); |
c07f9fc5 FG |
843 | f.close_section(); |
844 | } | |
845 | f.close_section(); | |
846 | } | |
847 | } else { | |
848 | dout(4) << __func__ << ": No daemon state found for " | |
849 | << svc_type << "." << svc_id << ")" << dendl; | |
850 | } | |
c07f9fc5 FG |
851 | return f.get(); |
852 | } | |
853 | ||
3efd9988 | 854 | PyObject *ActivePyModules::get_context() |
7c673cae FG |
855 | { |
856 | PyThreadState *tstate = PyEval_SaveThread(); | |
11fdf7f2 | 857 | std::lock_guard l(lock); |
7c673cae FG |
858 | PyEval_RestoreThread(tstate); |
859 | ||
860 | // Construct a capsule containing ceph context. | |
861 | // Not incrementing/decrementing ref count on the context because | |
862 | // it's the global one and it has process lifetime. | |
863 | auto capsule = PyCapsule_New(g_ceph_context, nullptr, nullptr); | |
864 | return capsule; | |
865 | } | |
866 | ||
3efd9988 FG |
867 | /** |
868 | * Helper for our wrapped types that take a capsule in their constructor. | |
869 | */ | |
870 | PyObject *construct_with_capsule( | |
871 | const std::string &module_name, | |
872 | const std::string &clsname, | |
873 | void *wrapped) | |
224ce89b | 874 | { |
3efd9988 FG |
875 | // Look up the OSDMap type which we will construct |
876 | PyObject *module = PyImport_ImportModule(module_name.c_str()); | |
877 | if (!module) { | |
878 | derr << "Failed to import python module:" << dendl; | |
879 | derr << handle_pyerror() << dendl; | |
224ce89b | 880 | } |
11fdf7f2 | 881 | ceph_assert(module); |
3efd9988 FG |
882 | |
883 | PyObject *wrapper_type = PyObject_GetAttrString( | |
884 | module, (const char*)clsname.c_str()); | |
885 | if (!wrapper_type) { | |
886 | derr << "Failed to get python type:" << dendl; | |
887 | derr << handle_pyerror() << dendl; | |
224ce89b | 888 | } |
11fdf7f2 | 889 | ceph_assert(wrapper_type); |
3efd9988 FG |
890 | |
891 | // Construct a capsule containing an OSDMap. | |
892 | auto wrapped_capsule = PyCapsule_New(wrapped, nullptr, nullptr); | |
11fdf7f2 | 893 | ceph_assert(wrapped_capsule); |
3efd9988 FG |
894 | |
895 | // Construct the python OSDMap | |
896 | auto pArgs = PyTuple_Pack(1, wrapped_capsule); | |
897 | auto wrapper_instance = PyObject_CallObject(wrapper_type, pArgs); | |
898 | if (wrapper_instance == nullptr) { | |
899 | derr << "Failed to construct python OSDMap:" << dendl; | |
900 | derr << handle_pyerror() << dendl; | |
901 | } | |
11fdf7f2 | 902 | ceph_assert(wrapper_instance != nullptr); |
3efd9988 FG |
903 | Py_DECREF(pArgs); |
904 | Py_DECREF(wrapped_capsule); | |
905 | ||
906 | Py_DECREF(wrapper_type); | |
907 | Py_DECREF(module); | |
908 | ||
909 | return wrapper_instance; | |
224ce89b WB |
910 | } |
911 | ||
3efd9988 | 912 | PyObject *ActivePyModules::get_osdmap() |
224ce89b | 913 | { |
3efd9988 FG |
914 | OSDMap *newmap = new OSDMap; |
915 | ||
11fdf7f2 TL |
916 | PyThreadState *tstate = PyEval_SaveThread(); |
917 | { | |
918 | std::lock_guard l(lock); | |
919 | cluster_state.with_osdmap([&](const OSDMap& o) { | |
920 | newmap->deepish_copy_from(o); | |
921 | }); | |
922 | } | |
923 | PyEval_RestoreThread(tstate); | |
3efd9988 FG |
924 | |
925 | return construct_with_capsule("mgr_module", "OSDMap", (void*)newmap); | |
224ce89b | 926 | } |
c07f9fc5 | 927 | |
3efd9988 | 928 | void ActivePyModules::set_health_checks(const std::string& module_name, |
c07f9fc5 FG |
929 | health_check_map_t&& checks) |
930 | { | |
11fdf7f2 TL |
931 | bool changed = false; |
932 | ||
9f95a23c | 933 | lock.lock(); |
3efd9988 | 934 | auto p = modules.find(module_name); |
c07f9fc5 | 935 | if (p != modules.end()) { |
11fdf7f2 | 936 | changed = p->second->set_health_checks(std::move(checks)); |
c07f9fc5 | 937 | } |
9f95a23c | 938 | lock.unlock(); |
11fdf7f2 TL |
939 | |
940 | // immediately schedule a report to be sent to the monitors with the new | |
941 | // health checks that have changed. This is done asynchronusly to avoid | |
942 | // blocking python land. ActivePyModules::lock needs to be dropped to make | |
943 | // lockdep happy: | |
944 | // | |
945 | // send_report callers: DaemonServer::lock -> PyModuleRegistery::lock | |
946 | // active_start: PyModuleRegistry::lock -> ActivePyModules::lock | |
947 | // | |
948 | // if we don't release this->lock before calling schedule_tick a cycle is | |
949 | // formed with the addition of ActivePyModules::lock -> DaemonServer::lock. | |
950 | // This is still correct as send_report is run asynchronously under | |
951 | // DaemonServer::lock. | |
952 | if (changed) | |
953 | server.schedule_tick(0); | |
954 | } | |
955 | ||
956 | int ActivePyModules::handle_command( | |
92f5a8d4 TL |
957 | const ModuleCommand& module_command, |
958 | const MgrSession& session, | |
11fdf7f2 TL |
959 | const cmdmap_t &cmdmap, |
960 | const bufferlist &inbuf, | |
961 | std::stringstream *ds, | |
962 | std::stringstream *ss) | |
963 | { | |
92f5a8d4 TL |
964 | lock.lock(); |
965 | auto mod_iter = modules.find(module_command.module_name); | |
11fdf7f2 | 966 | if (mod_iter == modules.end()) { |
92f5a8d4 TL |
967 | *ss << "Module '" << module_command.module_name << "' is not available"; |
968 | lock.unlock(); | |
11fdf7f2 TL |
969 | return -ENOENT; |
970 | } | |
971 | ||
92f5a8d4 TL |
972 | lock.unlock(); |
973 | return mod_iter->second->handle_command(module_command, session, cmdmap, | |
974 | inbuf, ds, ss); | |
c07f9fc5 FG |
975 | } |
976 | ||
3efd9988 | 977 | void ActivePyModules::get_health_checks(health_check_map_t *checks) |
c07f9fc5 | 978 | { |
11fdf7f2 | 979 | std::lock_guard l(lock); |
9f95a23c TL |
980 | for (auto& [name, module] : modules) { |
981 | dout(15) << "getting health checks for " << name << dendl; | |
982 | module->get_health_checks(checks); | |
c07f9fc5 FG |
983 | } |
984 | } | |
3efd9988 | 985 | |
11fdf7f2 TL |
986 | void ActivePyModules::update_progress_event( |
987 | const std::string& evid, | |
988 | const std::string& desc, | |
989 | float progress) | |
990 | { | |
991 | std::lock_guard l(lock); | |
992 | auto& pe = progress_events[evid]; | |
993 | pe.message = desc; | |
994 | pe.progress = progress; | |
995 | } | |
996 | ||
997 | void ActivePyModules::complete_progress_event(const std::string& evid) | |
998 | { | |
999 | std::lock_guard l(lock); | |
1000 | progress_events.erase(evid); | |
1001 | } | |
1002 | ||
1003 | void ActivePyModules::clear_all_progress_events() | |
1004 | { | |
1005 | std::lock_guard l(lock); | |
1006 | progress_events.clear(); | |
1007 | } | |
1008 | ||
1009 | void ActivePyModules::get_progress_events(std::map<std::string,ProgressEvent> *events) | |
1010 | { | |
1011 | std::lock_guard l(lock); | |
1012 | *events = progress_events; | |
1013 | } | |
1014 | ||
1015 | void ActivePyModules::config_notify() | |
1016 | { | |
1017 | std::lock_guard l(lock); | |
9f95a23c | 1018 | for (auto& [name, module] : modules) { |
11fdf7f2 TL |
1019 | // Send all python calls down a Finisher to avoid blocking |
1020 | // C++ code, and avoid any potential lock cycles. | |
9f95a23c TL |
1021 | dout(15) << "notify (config) " << name << dendl; |
1022 | // workaround for https://bugs.llvm.org/show_bug.cgi?id=35984 | |
1023 | finisher.queue(new LambdaContext([module=module](int r){ | |
1024 | module->config_notify(); | |
1025 | })); | |
11fdf7f2 TL |
1026 | } |
1027 | } | |
1028 | ||
3efd9988 FG |
1029 | void ActivePyModules::set_uri(const std::string& module_name, |
1030 | const std::string &uri) | |
1031 | { | |
11fdf7f2 | 1032 | std::lock_guard l(lock); |
3efd9988 FG |
1033 | |
1034 | dout(4) << " module " << module_name << " set URI '" << uri << "'" << dendl; | |
1035 | ||
9f95a23c | 1036 | modules.at(module_name)->set_uri(uri); |
3efd9988 FG |
1037 | } |
1038 | ||
9f95a23c | 1039 | MetricQueryID ActivePyModules::add_osd_perf_query( |
11fdf7f2 TL |
1040 | const OSDPerfMetricQuery &query, |
1041 | const std::optional<OSDPerfMetricLimit> &limit) | |
1042 | { | |
1043 | return server.add_osd_perf_query(query, limit); | |
1044 | } | |
1045 | ||
9f95a23c | 1046 | void ActivePyModules::remove_osd_perf_query(MetricQueryID query_id) |
11fdf7f2 TL |
1047 | { |
1048 | int r = server.remove_osd_perf_query(query_id); | |
1049 | if (r < 0) { | |
1050 | dout(0) << "remove_osd_perf_query for query_id=" << query_id << " failed: " | |
1051 | << cpp_strerror(r) << dendl; | |
1052 | } | |
1053 | } | |
1054 | ||
9f95a23c | 1055 | PyObject *ActivePyModules::get_osd_perf_counters(MetricQueryID query_id) |
11fdf7f2 TL |
1056 | { |
1057 | std::map<OSDPerfMetricKey, PerformanceCounters> counters; | |
1058 | ||
1059 | int r = server.get_osd_perf_counters(query_id, &counters); | |
1060 | if (r < 0) { | |
1061 | dout(0) << "get_osd_perf_counters for query_id=" << query_id << " failed: " | |
1062 | << cpp_strerror(r) << dendl; | |
1063 | Py_RETURN_NONE; | |
1064 | } | |
1065 | ||
1066 | PyFormatter f; | |
1067 | ||
1068 | f.open_array_section("counters"); | |
1069 | for (auto &it : counters) { | |
1070 | auto &key = it.first; | |
1071 | auto &instance_counters = it.second; | |
1072 | f.open_object_section("i"); | |
1073 | f.open_array_section("k"); | |
1074 | for (auto &sub_key : key) { | |
1075 | f.open_array_section("s"); | |
1076 | for (size_t i = 0; i < sub_key.size(); i++) { | |
1077 | f.dump_string(stringify(i).c_str(), sub_key[i]); | |
1078 | } | |
1079 | f.close_section(); // s | |
1080 | } | |
1081 | f.close_section(); // k | |
1082 | f.open_array_section("c"); | |
1083 | for (auto &c : instance_counters) { | |
1084 | f.open_array_section("p"); | |
1085 | f.dump_unsigned("0", c.first); | |
1086 | f.dump_unsigned("1", c.second); | |
1087 | f.close_section(); // p | |
1088 | } | |
1089 | f.close_section(); // c | |
1090 | f.close_section(); // i | |
1091 | } | |
1092 | f.close_section(); // counters | |
1093 | ||
1094 | return f.get(); | |
1095 | } | |
1096 | ||
1097 | void ActivePyModules::cluster_log(const std::string &channel, clog_type prio, | |
1098 | const std::string &message) | |
1099 | { | |
1100 | std::lock_guard l(lock); | |
1101 | ||
9f95a23c TL |
1102 | auto cl = monc.get_log_client()->create_channel(channel); |
1103 | map<string,string> log_to_monitors; | |
1104 | map<string,string> log_to_syslog; | |
1105 | map<string,string> log_channel; | |
1106 | map<string,string> log_prio; | |
1107 | map<string,string> log_to_graylog; | |
1108 | map<string,string> log_to_graylog_host; | |
1109 | map<string,string> log_to_graylog_port; | |
1110 | uuid_d fsid; | |
1111 | string host; | |
1112 | if (parse_log_client_options(g_ceph_context, log_to_monitors, log_to_syslog, | |
1113 | log_channel, log_prio, log_to_graylog, | |
1114 | log_to_graylog_host, log_to_graylog_port, | |
1115 | fsid, host) == 0) | |
1116 | cl->update_config(log_to_monitors, log_to_syslog, | |
1117 | log_channel, log_prio, log_to_graylog, | |
1118 | log_to_graylog_host, log_to_graylog_port, | |
1119 | fsid, host); | |
1120 | cl->do_log(prio, message); | |
1121 | } | |
1122 | ||
1123 | void ActivePyModules::register_client(std::string_view name, std::string addrs) | |
1124 | { | |
1125 | std::lock_guard l(lock); | |
1126 | ||
1127 | entity_addrvec_t addrv; | |
1128 | addrv.parse(addrs.data()); | |
1129 | ||
1130 | dout(7) << "registering msgr client handle " << addrv << dendl; | |
1131 | py_module_registry.register_client(name, std::move(addrv)); | |
1132 | } | |
1133 | ||
1134 | void ActivePyModules::unregister_client(std::string_view name, std::string addrs) | |
1135 | { | |
1136 | std::lock_guard l(lock); | |
1137 | ||
1138 | entity_addrvec_t addrv; | |
1139 | addrv.parse(addrs.data()); | |
1140 | ||
1141 | dout(7) << "unregistering msgr client handle " << addrv << dendl; | |
1142 | py_module_registry.unregister_client(name, addrv); | |
11fdf7f2 | 1143 | } |