]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/ceph_context.h
update sources to v12.1.0
[ceph.git] / ceph / src / common / ceph_context.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) 2011 New Dream Network
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 #ifndef CEPH_CEPHCONTEXT_H
16 #define CEPH_CEPHCONTEXT_H
17
18 #include <atomic>
19 #include <set>
20 #include <boost/noncopyable.hpp>
21
22 #include "common/cmdparse.h"
23 #include "crush/CrushLocation.h"
24 #include "include/Spinlock.h"
25
26 class AdminSocket;
27 class CephContextServiceThread;
28 class PerfCountersCollection;
29 class PerfCounters;
30 class md_config_obs_t;
31 struct md_config_t;
32 class CephContextHook;
33 class CephContextObs;
34 class CryptoHandler;
35
36 namespace ceph {
37 class PluginRegistry;
38 class HeartbeatMap;
39 namespace logging {
40 class Log;
41 }
42 }
43
44 /* A CephContext represents the context held by a single library user.
45 * There can be multiple CephContexts in the same process.
46 *
47 * For daemons and utility programs, there will be only one CephContext. The
48 * CephContext contains the configuration, the dout object, and anything else
49 * that you might want to pass to libcommon with every function call.
50 */
51 class CephContext {
52 public:
53 CephContext(uint32_t module_type_, int init_flags_ = 0);
54
55 // ref count!
56 private:
57 ~CephContext();
58 std::atomic<unsigned> nref;
59 public:
60 CephContext *get() {
61 ++nref;
62 return this;
63 }
64 void put();
65
66 md_config_t *_conf;
67 ceph::logging::Log *_log;
68
69 /* init ceph::crypto */
70 void init_crypto();
71
72 /* Start the Ceph Context's service thread */
73 void start_service_thread();
74
75 /* Reopen the log files */
76 void reopen_logs();
77
78 /* Get the module type (client, mon, osd, mds, etc.) */
79 uint32_t get_module_type() const;
80
81 void set_init_flags(int flags);
82 int get_init_flags() const;
83
84 /* Get the PerfCountersCollection of this CephContext */
85 PerfCountersCollection *get_perfcounters_collection();
86
87 ceph::HeartbeatMap *get_heartbeat_map() {
88 return _heartbeat_map;
89 }
90
91 /**
92 * Enable the performance counter, currently we only have counter for the
93 * number of total/unhealthy workers.
94 */
95 void enable_perf_counter();
96
97 /**
98 * Disable the performance counter.
99 */
100 void disable_perf_counter();
101
102 /**
103 * Refresh perf counter values.
104 */
105 void refresh_perf_values();
106
107 /**
108 * Get the admin socket associated with this CephContext.
109 *
110 * Currently there is always an admin socket object,
111 * so this will never return NULL.
112 *
113 * @return the admin socket
114 */
115 AdminSocket *get_admin_socket();
116
117 /**
118 * process an admin socket command
119 */
120 void do_command(std::string command, cmdmap_t& cmdmap, std::string format,
121 ceph::bufferlist *out);
122
123 template<typename T>
124 void lookup_or_create_singleton_object(T*& p, const std::string &name) {
125 ceph_spin_lock(&_associated_objs_lock);
126 if (!_associated_objs.count(name)) {
127 p = new T(this);
128 _associated_objs[name] = new TypedSingletonWrapper<T>(p);
129 } else {
130 TypedSingletonWrapper<T> *wrapper =
131 dynamic_cast<TypedSingletonWrapper<T> *>(_associated_objs[name]);
132 assert(wrapper != NULL);
133 p = wrapper->singleton;
134 }
135 ceph_spin_unlock(&_associated_objs_lock);
136 }
137 /**
138 * get a crypto handler
139 */
140 CryptoHandler *get_crypto_handler(int type);
141
142 /// check if experimental feature is enable, and emit appropriate warnings
143 bool check_experimental_feature_enabled(const std::string& feature);
144 bool check_experimental_feature_enabled(const std::string& feature,
145 std::ostream *message);
146
147 PluginRegistry *get_plugin_registry() {
148 return _plugin_registry;
149 }
150
151 void set_uid_gid(uid_t u, gid_t g) {
152 _set_uid = u;
153 _set_gid = g;
154 }
155 uid_t get_set_uid() const {
156 return _set_uid;
157 }
158 gid_t get_set_gid() const {
159 return _set_gid;
160 }
161
162 void set_uid_gid_strings(std::string u, std::string g) {
163 _set_uid_string = u;
164 _set_gid_string = g;
165 }
166 std::string get_set_uid_string() const {
167 return _set_uid_string;
168 }
169 std::string get_set_gid_string() const {
170 return _set_gid_string;
171 }
172
173 class ForkWatcher {
174 public:
175 virtual ~ForkWatcher() {}
176 virtual void handle_pre_fork() = 0;
177 virtual void handle_post_fork() = 0;
178 };
179
180 void register_fork_watcher(ForkWatcher *w) {
181 ceph_spin_lock(&_fork_watchers_lock);
182 _fork_watchers.push_back(w);
183 ceph_spin_unlock(&_fork_watchers_lock);
184 }
185
186 void notify_pre_fork() {
187 ceph_spin_lock(&_fork_watchers_lock);
188 for (auto &&t : _fork_watchers)
189 t->handle_pre_fork();
190 ceph_spin_unlock(&_fork_watchers_lock);
191 }
192
193 void notify_post_fork() {
194 ceph_spin_lock(&_fork_watchers_lock);
195 for (auto &&t : _fork_watchers)
196 t->handle_post_fork();
197 ceph_spin_unlock(&_fork_watchers_lock);
198 }
199
200 private:
201 struct SingletonWrapper : boost::noncopyable {
202 virtual ~SingletonWrapper() {}
203 };
204
205 template <typename T>
206 struct TypedSingletonWrapper : public SingletonWrapper {
207 TypedSingletonWrapper(T *p) : singleton(p) {
208 }
209 ~TypedSingletonWrapper() override {
210 delete singleton;
211 }
212
213 T *singleton;
214 };
215
216 CephContext(const CephContext &rhs);
217 CephContext &operator=(const CephContext &rhs);
218
219 /* Stop and join the Ceph Context's service thread */
220 void join_service_thread();
221
222 uint32_t _module_type;
223
224 int _init_flags;
225
226 uid_t _set_uid; ///< uid to drop privs to
227 gid_t _set_gid; ///< gid to drop privs to
228 std::string _set_uid_string;
229 std::string _set_gid_string;
230
231 bool _crypto_inited;
232
233 /* libcommon service thread.
234 * SIGHUP wakes this thread, which then reopens logfiles */
235 friend class CephContextServiceThread;
236 CephContextServiceThread *_service_thread;
237
238 md_config_obs_t *_log_obs;
239
240 /* The admin socket associated with this context */
241 AdminSocket *_admin_socket;
242
243 /* lock which protects service thread creation, destruction, etc. */
244 ceph_spinlock_t _service_thread_lock;
245
246 /* The collection of profiling loggers associated with this context */
247 PerfCountersCollection *_perf_counters_collection;
248
249 md_config_obs_t *_perf_counters_conf_obs;
250
251 CephContextHook *_admin_hook;
252
253 ceph::HeartbeatMap *_heartbeat_map;
254
255 ceph_spinlock_t _associated_objs_lock;
256 std::map<std::string, SingletonWrapper*> _associated_objs;
257
258 ceph_spinlock_t _fork_watchers_lock;
259 std::vector<ForkWatcher*> _fork_watchers;
260
261 // crypto
262 CryptoHandler *_crypto_none;
263 CryptoHandler *_crypto_aes;
264
265 // experimental
266 CephContextObs *_cct_obs;
267 ceph_spinlock_t _feature_lock;
268 std::set<std::string> _experimental_features;
269
270 PluginRegistry *_plugin_registry;
271
272 md_config_obs_t *_lockdep_obs;
273
274 public:
275 CrushLocation crush_location;
276 private:
277
278 enum {
279 l_cct_first,
280 l_cct_total_workers,
281 l_cct_unhealthy_workers,
282 l_cct_last
283 };
284 PerfCounters *_cct_perf;
285 ceph_spinlock_t _cct_perf_lock;
286
287 friend class CephContextObs;
288 };
289
290 #endif