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