]> git.proxmox.com Git - ceph.git/blob - ceph/src/mgr/ActivePyModules.h
218497c1e8982dd574fc3105c5176158e9787c11
[ceph.git] / ceph / src / mgr / ActivePyModules.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) 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 #pragma once
15
16 #include "ActivePyModule.h"
17
18 #include "common/Finisher.h"
19 #include "common/ceph_mutex.h"
20
21 #include "PyFormatter.h"
22
23 #include "osdc/Objecter.h"
24 #include "client/Client.h"
25 #include "common/LogClient.h"
26 #include "mon/MgrMap.h"
27 #include "mon/MonCommand.h"
28 #include "mon/mon_types.h"
29 #include "mon/ConfigMap.h"
30 #include "mgr/TTLCache.h"
31
32 #include "DaemonState.h"
33 #include "ClusterState.h"
34 #include "OSDPerfMetricTypes.h"
35
36 class health_check_map_t;
37 class DaemonServer;
38 class MgrSession;
39 class ModuleCommand;
40 class PyModuleRegistry;
41
42 class ActivePyModules
43 {
44 // module class instances not yet created
45 std::set<std::string, std::less<>> pending_modules;
46 // module class instances already created
47 std::map<std::string, std::shared_ptr<ActivePyModule>> modules;
48 PyModuleConfig &module_config;
49 bool have_local_config_map = false;
50 std::map<std::string, std::string> store_cache;
51 ConfigMap config_map; ///< derived from store_cache config/ keys
52 DaemonStateIndex &daemon_state;
53 ClusterState &cluster_state;
54 MonClient &monc;
55 LogChannelRef clog, audit_clog;
56 Objecter &objecter;
57 Client &client;
58 Finisher &finisher;
59 TTLCache<string, PyObject*> ttl_cache;
60 public:
61 Finisher cmd_finisher;
62 private:
63 DaemonServer &server;
64 PyModuleRegistry &py_module_registry;
65
66 map<std::string,ProgressEvent> progress_events;
67
68 mutable ceph::mutex lock = ceph::make_mutex("ActivePyModules::lock");
69
70 public:
71 ActivePyModules(
72 PyModuleConfig &module_config,
73 std::map<std::string, std::string> store_data,
74 bool mon_provides_kv_sub,
75 DaemonStateIndex &ds, ClusterState &cs, MonClient &mc,
76 LogChannelRef clog_, LogChannelRef audit_clog_, Objecter &objecter_, Client &client_,
77 Finisher &f, DaemonServer &server, PyModuleRegistry &pmr);
78
79 ~ActivePyModules();
80
81 // FIXME: wrap for send_command?
82 MonClient &get_monc() {return monc;}
83 Objecter &get_objecter() {return objecter;}
84 Client &get_client() {return client;}
85 PyObject *cacheable_get_python(const std::string &what);
86 PyObject *get_python(const std::string &what);
87 PyObject *get_server_python(const std::string &hostname);
88 PyObject *list_servers_python();
89 PyObject *get_metadata_python(
90 const std::string &svc_type, const std::string &svc_id);
91 PyObject *get_daemon_status_python(
92 const std::string &svc_type, const std::string &svc_id);
93 PyObject *get_counter_python(
94 const std::string &svc_type,
95 const std::string &svc_id,
96 const std::string &path);
97 PyObject *get_latest_counter_python(
98 const std::string &svc_type,
99 const std::string &svc_id,
100 const std::string &path);
101 PyObject *get_perf_schema_python(
102 const std::string &svc_type,
103 const std::string &svc_id);
104 PyObject *get_rocksdb_version();
105 PyObject *get_context();
106 PyObject *get_osdmap();
107 /// @note @c fct is not allowed to acquire locks when holding GIL
108 PyObject *with_perf_counters(
109 std::function<void(
110 PerfCounterInstance& counter_instance,
111 PerfCounterType& counter_type,
112 PyFormatter& f)> fct,
113 const std::string &svc_name,
114 const std::string &svc_id,
115 const std::string &path) const;
116
117 MetricQueryID add_osd_perf_query(
118 const OSDPerfMetricQuery &query,
119 const std::optional<OSDPerfMetricLimit> &limit);
120 void remove_osd_perf_query(MetricQueryID query_id);
121 PyObject *get_osd_perf_counters(MetricQueryID query_id);
122
123 MetricQueryID add_mds_perf_query(
124 const MDSPerfMetricQuery &query,
125 const std::optional<MDSPerfMetricLimit> &limit);
126 void remove_mds_perf_query(MetricQueryID query_id);
127 void reregister_mds_perf_queries();
128 PyObject *get_mds_perf_counters(MetricQueryID query_id);
129
130 bool get_store(const std::string &module_name,
131 const std::string &key, std::string *val) const;
132 PyObject *get_store_prefix(const std::string &module_name,
133 const std::string &prefix) const;
134 void set_store(const std::string &module_name,
135 const std::string &key, const std::optional<std::string> &val);
136
137 bool get_config(const std::string &module_name,
138 const std::string &key, std::string *val) const;
139 std::pair<int, std::string> set_config(const std::string &module_name,
140 const std::string &key, const std::optional<std::string> &val);
141
142 PyObject *get_typed_config(const std::string &module_name,
143 const std::string &key,
144 const std::string &prefix = "") const;
145 PyObject *get_foreign_config(
146 const std::string& who,
147 const std::string& name);
148
149 void set_health_checks(const std::string& module_name,
150 health_check_map_t&& checks);
151 void get_health_checks(health_check_map_t *checks);
152
153 void update_progress_event(const std::string& evid,
154 const std::string& desc,
155 float progress,
156 bool add_to_ceph_s);
157 void complete_progress_event(const std::string& evid);
158 void clear_all_progress_events();
159 void get_progress_events(std::map<std::string,ProgressEvent>* events);
160
161 void register_client(std::string_view name, std::string addrs);
162 void unregister_client(std::string_view name, std::string addrs);
163
164 void config_notify();
165
166 void set_uri(const std::string& module_name, const std::string &uri);
167 void set_device_wear_level(const std::string& devid, float wear_level);
168
169 int handle_command(
170 const ModuleCommand& module_command,
171 const MgrSession& session,
172 const cmdmap_t &cmdmap,
173 const bufferlist &inbuf,
174 std::stringstream *ds,
175 std::stringstream *ss);
176
177 std::map<std::string, std::string> get_services() const;
178
179 void update_kv_data(
180 const std::string prefix,
181 bool incremental,
182 const map<std::string, std::optional<bufferlist>, std::less<>>& data);
183 void _refresh_config_map();
184
185 // Public so that MonCommandCompletion can use it
186 // FIXME: for send_command completion notifications,
187 // send it to only the module that sent the command, not everyone
188 void notify_all(const std::string &notify_type,
189 const std::string &notify_id);
190 void notify_all(const LogEntry &log_entry);
191
192 bool is_pending(std::string_view name) const {
193 return pending_modules.count(name) > 0;
194 }
195 bool module_exists(const std::string &name) const
196 {
197 return modules.count(name) > 0;
198 }
199
200 bool method_exists(
201 const std::string &module_name,
202 const std::string &method_name) const
203 {
204 return modules.at(module_name)->method_exists(method_name);
205 }
206
207 PyObject *dispatch_remote(
208 const std::string &other_module,
209 const std::string &method,
210 PyObject *args,
211 PyObject *kwargs,
212 std::string *err);
213
214 int init();
215 void shutdown();
216
217 void start_one(PyModuleRef py_module);
218
219 void dump_server(const std::string &hostname,
220 const DaemonStateCollection &dmc,
221 Formatter *f);
222
223 void cluster_log(const std::string &channel, clog_type prio,
224 const std::string &message);
225
226 bool inject_python_on() const;
227 void update_cache_metrics();
228 };
229