]> git.proxmox.com Git - ceph.git/blob - ceph/src/mgr/PyModuleRegistry.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / mgr / PyModuleRegistry.h
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) 2017 John Spray <john.spray@redhat.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
15 #pragma once
16
17 // First because it includes Python.h
18 #include "PyModule.h"
19
20 #include <string>
21 #include <map>
22 #include <set>
23 #include <memory>
24
25 #include "common/LogClient.h"
26
27 #include "ActivePyModules.h"
28 #include "StandbyPyModules.h"
29
30 class MgrSession;
31
32 /**
33 * This class is responsible for setting up the python runtime environment
34 * and importing the python modules.
35 *
36 * It is *not* responsible for constructing instances of their BaseMgrModule
37 * subclasses: that is the job of ActiveMgrModule, which consumes the class
38 * references that we load here.
39 */
40 class PyModuleRegistry
41 {
42 private:
43 mutable ceph::mutex lock = ceph::make_mutex("PyModuleRegistry::lock");
44 LogChannelRef clog;
45
46 std::map<std::string, PyModuleRef> modules;
47 std::multimap<std::string, entity_addrvec_t> clients;
48
49 std::unique_ptr<ActivePyModules> active_modules;
50 std::unique_ptr<StandbyPyModules> standby_modules;
51
52 PyThreadState *pMainThreadState;
53
54 // We have our own copy of MgrMap, because we are constructed
55 // before ClusterState exists.
56 MgrMap mgr_map;
57
58 /**
59 * Discover python modules from local disk
60 */
61 std::set<std::string> probe_modules(const std::string &path) const;
62
63 PyModuleConfig module_config;
64
65 public:
66 void handle_config(const std::string &k, const std::string &v);
67 void handle_config_notify();
68
69 /**
70 * Get references to all modules (whether they have loaded and/or
71 * errored) or not.
72 */
73 auto get_modules() const
74 {
75 std::vector<PyModuleRef> modules_out;
76 std::lock_guard l(lock);
77 for (const auto &i : modules) {
78 modules_out.push_back(i.second);
79 }
80
81 return modules_out;
82 }
83
84 explicit PyModuleRegistry(LogChannelRef clog_)
85 : clog(clog_)
86 {}
87
88 /**
89 * @return true if the mgrmap has changed such that the service needs restart
90 */
91 bool handle_mgr_map(const MgrMap &mgr_map_);
92
93 void init();
94
95 void upgrade_config(
96 MonClient *monc,
97 const std::map<std::string, std::string> &old_config);
98
99 void active_start(
100 DaemonStateIndex &ds, ClusterState &cs,
101 const std::map<std::string, std::string> &kv_store,
102 MonClient &mc, LogChannelRef clog_, LogChannelRef audit_clog_,
103 Objecter &objecter_, Client &client_, Finisher &f,
104 DaemonServer &server);
105 void standby_start(MonClient &mc, Finisher &f);
106
107 bool is_standby_running() const
108 {
109 return standby_modules != nullptr;
110 }
111
112 void active_shutdown();
113 void shutdown();
114
115 std::vector<MonCommand> get_commands() const;
116 std::vector<ModuleCommand> get_py_commands() const;
117
118 /**
119 * Get the specified module. The module does not have to be
120 * loaded or runnable.
121 *
122 * Returns an empty reference if it does not exist.
123 */
124 PyModuleRef get_module(const std::string &module_name)
125 {
126 std::lock_guard l(lock);
127 auto module_iter = modules.find(module_name);
128 if (module_iter == modules.end()) {
129 return {};
130 }
131 return module_iter->second;
132 }
133
134 /**
135 * Pass through command to the named module for execution.
136 *
137 * The command must exist in the COMMANDS reported by the module. If it
138 * doesn't then this will abort.
139 *
140 * If ActivePyModules has not been instantiated yet then this will
141 * return EAGAIN.
142 */
143 int handle_command(
144 const ModuleCommand& module_command,
145 const MgrSession& session,
146 const cmdmap_t &cmdmap,
147 const bufferlist &inbuf,
148 std::stringstream *ds,
149 std::stringstream *ss);
150
151 /**
152 * Pass through health checks reported by modules, and report any
153 * modules that have failed (i.e. unhandled exceptions in serve())
154 */
155 void get_health_checks(health_check_map_t *checks);
156
157 void get_progress_events(map<std::string,ProgressEvent> *events) {
158 if (active_modules) {
159 active_modules->get_progress_events(events);
160 }
161 }
162
163 // FIXME: breaking interface so that I don't have to go rewrite all
164 // the places that call into these (for now)
165 // >>>
166 void notify_all(const std::string &notify_type,
167 const std::string &notify_id)
168 {
169 if (active_modules) {
170 active_modules->notify_all(notify_type, notify_id);
171 }
172 }
173
174 void notify_all(const LogEntry &log_entry)
175 {
176 if (active_modules) {
177 active_modules->notify_all(log_entry);
178 }
179 }
180
181 std::map<std::string, std::string> get_services() const
182 {
183 ceph_assert(active_modules);
184 return active_modules->get_services();
185 }
186
187 void register_client(std::string_view name, entity_addrvec_t addrs)
188 {
189 clients.emplace(std::string(name), std::move(addrs));
190 }
191 void unregister_client(std::string_view name, const entity_addrvec_t& addrs)
192 {
193 auto itp = clients.equal_range(std::string(name));
194 for (auto it = itp.first; it != itp.second; ++it) {
195 if (it->second == addrs) {
196 it = clients.erase(it);
197 }
198 }
199 }
200
201 auto get_clients() const
202 {
203 std::scoped_lock l(lock);
204 std::vector<entity_addrvec_t> v;
205 for (const auto& p : clients) {
206 v.push_back(p.second);
207 }
208 return v;
209 }
210
211 // <<< (end of ActivePyModules cheeky call-throughs)
212 };