]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/ceph_context.h
update source to Ceph Pacific 16.2.2
[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 <map>
20 #include <memory>
21 #include <mutex>
22 #include <set>
23 #include <string>
24 #include <string_view>
25 #include <typeinfo>
26 #include <typeindex>
27
28 #include <boost/intrusive_ptr.hpp>
29
30 #include "include/any.h"
31 #include "include/common_fwd.h"
32 #include "include/compat.h"
33
34 #include "common/cmdparse.h"
35 #include "common/code_environment.h"
36 #include "msg/msg_types.h"
37 #if defined(WITH_SEASTAR) && !defined(WITH_ALIEN)
38 #include "crimson/common/config_proxy.h"
39 #include "crimson/common/perf_counters_collection.h"
40 #else
41 #include "common/config_proxy.h"
42 #include "include/spinlock.h"
43 #include "common/perf_counters_collection.h"
44 #endif
45
46
47 #include "crush/CrushLocation.h"
48
49 class AdminSocket;
50 class CryptoHandler;
51 class CryptoRandom;
52 class MonMap;
53
54 namespace ceph::common {
55 class CephContextServiceThread;
56 class CephContextObs;
57 class CephContextHook;
58 }
59
60 namespace ceph {
61 class PluginRegistry;
62 class HeartbeatMap;
63 namespace logging {
64 class Log;
65 }
66 }
67
68 #if defined(WITH_SEASTAR) && !defined(WITH_ALIEN)
69 namespace crimson::common {
70 class CephContext {
71 public:
72 CephContext();
73 CephContext(uint32_t,
74 code_environment_t=CODE_ENVIRONMENT_UTILITY,
75 int = 0)
76 : CephContext{}
77 {}
78 CephContext(CephContext&&) = default;
79 ~CephContext();
80
81 uint32_t get_module_type() const;
82 bool check_experimental_feature_enabled(const std::string& feature) {
83 // everything crimson is experimental...
84 return true;
85 }
86 CryptoRandom* random() const;
87 PerfCountersCollectionImpl* get_perfcounters_collection();
88 crimson::common::ConfigProxy& _conf;
89 crimson::common::PerfCountersCollection& _perf_counters_collection;
90 CephContext* get();
91 void put();
92 private:
93 std::unique_ptr<CryptoRandom> _crypto_random;
94 unsigned nref;
95 };
96 }
97 #else
98 #ifdef __cplusplus
99 namespace ceph::common {
100 #endif
101 /* A CephContext represents the context held by a single library user.
102 * There can be multiple CephContexts in the same process.
103 *
104 * For daemons and utility programs, there will be only one CephContext. The
105 * CephContext contains the configuration, the dout object, and anything else
106 * that you might want to pass to libcommon with every function call.
107 */
108 class CephContext {
109 public:
110 CephContext(uint32_t module_type_,
111 enum code_environment_t code_env=CODE_ENVIRONMENT_UTILITY,
112 int init_flags_ = 0);
113
114 CephContext(const CephContext&) = delete;
115 CephContext& operator =(const CephContext&) = delete;
116 CephContext(CephContext&&) = delete;
117 CephContext& operator =(CephContext&&) = delete;
118
119 bool _finished = false;
120 ~CephContext();
121
122 // ref count!
123 private:
124 std::atomic<unsigned> nref;
125 public:
126 CephContext *get() {
127 ++nref;
128 return this;
129 }
130 void put();
131
132 ConfigProxy _conf;
133 ceph::logging::Log *_log;
134
135 /* init ceph::crypto */
136 void init_crypto();
137
138 /// shutdown crypto (should match init_crypto calls)
139 void shutdown_crypto();
140
141 /* Start the Ceph Context's service thread */
142 void start_service_thread();
143
144 /* Reopen the log files */
145 void reopen_logs();
146
147 /* Get the module type (client, mon, osd, mds, etc.) */
148 uint32_t get_module_type() const;
149
150 // this is here only for testing purposes!
151 void _set_module_type(uint32_t t) {
152 _module_type = t;
153 }
154
155 void set_init_flags(int flags);
156 int get_init_flags() const;
157
158 /* Get the PerfCountersCollection of this CephContext */
159 PerfCountersCollection *get_perfcounters_collection();
160
161 ceph::HeartbeatMap *get_heartbeat_map() {
162 return _heartbeat_map;
163 }
164
165 /**
166 * Get the admin socket associated with this CephContext.
167 *
168 * Currently there is always an admin socket object,
169 * so this will never return NULL.
170 *
171 * @return the admin socket
172 */
173 AdminSocket *get_admin_socket();
174
175 /**
176 * process an admin socket command
177 */
178 int do_command(std::string_view command, const cmdmap_t& cmdmap,
179 Formatter *f,
180 std::ostream& errss,
181 ceph::bufferlist *out);
182 int _do_command(std::string_view command, const cmdmap_t& cmdmap,
183 Formatter *f,
184 std::ostream& errss,
185 ceph::bufferlist *out);
186
187 static constexpr std::size_t largest_singleton = 8 * 72;
188
189 template<typename T, typename... Args>
190 T& lookup_or_create_singleton_object(std::string_view name,
191 bool drop_on_fork,
192 Args&&... args) {
193 static_assert(sizeof(T) <= largest_singleton,
194 "Please increase largest singleton.");
195 std::lock_guard lg(associated_objs_lock);
196 std::type_index type = typeid(T);
197
198 auto i = associated_objs.find(std::make_pair(name, type));
199 if (i == associated_objs.cend()) {
200 if (drop_on_fork) {
201 associated_objs_drop_on_fork.insert(std::string(name));
202 }
203 i = associated_objs.emplace_hint(
204 i,
205 std::piecewise_construct,
206 std::forward_as_tuple(name, type),
207 std::forward_as_tuple(std::in_place_type<T>,
208 std::forward<Args>(args)...));
209 }
210 return ceph::any_cast<T&>(i->second);
211 }
212
213 /**
214 * get a crypto handler
215 */
216 CryptoHandler *get_crypto_handler(int type);
217
218 CryptoRandom* random() const { return _crypto_random.get(); }
219
220 /// check if experimental feature is enable, and emit appropriate warnings
221 bool check_experimental_feature_enabled(const std::string& feature);
222 bool check_experimental_feature_enabled(const std::string& feature,
223 std::ostream *message);
224
225 ceph::PluginRegistry *get_plugin_registry() {
226 return _plugin_registry;
227 }
228
229 void set_uid_gid(uid_t u, gid_t g) {
230 _set_uid = u;
231 _set_gid = g;
232 }
233 uid_t get_set_uid() const {
234 return _set_uid;
235 }
236 gid_t get_set_gid() const {
237 return _set_gid;
238 }
239
240 void set_uid_gid_strings(const std::string &u, const std::string &g) {
241 _set_uid_string = u;
242 _set_gid_string = g;
243 }
244 std::string get_set_uid_string() const {
245 return _set_uid_string;
246 }
247 std::string get_set_gid_string() const {
248 return _set_gid_string;
249 }
250
251 class ForkWatcher {
252 public:
253 virtual ~ForkWatcher() {}
254 virtual void handle_pre_fork() = 0;
255 virtual void handle_post_fork() = 0;
256 };
257
258 void register_fork_watcher(ForkWatcher *w) {
259 std::lock_guard lg(_fork_watchers_lock);
260 _fork_watchers.push_back(w);
261 }
262
263 void notify_pre_fork();
264 void notify_post_fork();
265
266 /**
267 * update CephContext with a copy of the passed in MonMap mon addrs
268 *
269 * @param mm MonMap to extract and update mon addrs
270 */
271 void set_mon_addrs(const MonMap& mm);
272 void set_mon_addrs(const std::vector<entity_addrvec_t>& in) {
273 auto ptr = std::make_shared<std::vector<entity_addrvec_t>>(in);
274 atomic_store_explicit(&_mon_addrs, std::move(ptr), std::memory_order_relaxed);
275 }
276 std::shared_ptr<std::vector<entity_addrvec_t>> get_mon_addrs() const {
277 auto ptr = atomic_load_explicit(&_mon_addrs, std::memory_order_relaxed);
278 return ptr;
279 }
280
281 private:
282
283
284 /* Stop and join the Ceph Context's service thread */
285 void join_service_thread();
286
287 uint32_t _module_type;
288
289 int _init_flags;
290
291 uid_t _set_uid; ///< uid to drop privs to
292 gid_t _set_gid; ///< gid to drop privs to
293 std::string _set_uid_string;
294 std::string _set_gid_string;
295
296 int _crypto_inited;
297
298 std::shared_ptr<std::vector<entity_addrvec_t>> _mon_addrs;
299
300 /* libcommon service thread.
301 * SIGHUP wakes this thread, which then reopens logfiles */
302 friend class CephContextServiceThread;
303 CephContextServiceThread *_service_thread;
304
305 using md_config_obs_t = ceph::md_config_obs_impl<ConfigProxy>;
306
307 md_config_obs_t *_log_obs;
308
309 /* The admin socket associated with this context */
310 AdminSocket *_admin_socket;
311
312 /* lock which protects service thread creation, destruction, etc. */
313 ceph::spinlock _service_thread_lock;
314
315 /* The collection of profiling loggers associated with this context */
316 PerfCountersCollection *_perf_counters_collection;
317
318 md_config_obs_t *_perf_counters_conf_obs;
319
320 CephContextHook *_admin_hook;
321
322 ceph::HeartbeatMap *_heartbeat_map;
323
324 ceph::spinlock associated_objs_lock;
325
326 struct associated_objs_cmp {
327 using is_transparent = std::true_type;
328 template<typename T, typename U>
329 bool operator ()(const std::pair<T, std::type_index>& l,
330 const std::pair<U, std::type_index>& r) const noexcept {
331 return ((l.first < r.first) ||
332 (l.first == r.first && l.second < r.second));
333 }
334 };
335
336 std::map<std::pair<std::string, std::type_index>,
337 ceph::immobile_any<largest_singleton>,
338 associated_objs_cmp> associated_objs;
339 std::set<std::string> associated_objs_drop_on_fork;
340
341 ceph::spinlock _fork_watchers_lock;
342 std::vector<ForkWatcher*> _fork_watchers;
343
344 // crypto
345 CryptoHandler *_crypto_none;
346 CryptoHandler *_crypto_aes;
347 std::unique_ptr<CryptoRandom> _crypto_random;
348
349 // experimental
350 CephContextObs *_cct_obs;
351 ceph::spinlock _feature_lock;
352 std::set<std::string> _experimental_features;
353
354 ceph::PluginRegistry* _plugin_registry;
355
356 md_config_obs_t *_lockdep_obs;
357
358 public:
359 TOPNSPC::crush::CrushLocation crush_location;
360 private:
361
362 enum {
363 l_cct_first,
364 l_cct_total_workers,
365 l_cct_unhealthy_workers,
366 l_cct_last
367 };
368 enum {
369 l_mempool_first = 873222,
370 l_mempool_bytes,
371 l_mempool_items,
372 l_mempool_last
373 };
374 PerfCounters *_cct_perf = nullptr;
375 PerfCounters* _mempool_perf = nullptr;
376 std::vector<std::string> _mempool_perf_names, _mempool_perf_descriptions;
377
378 /**
379 * Enable the performance counters.
380 */
381 void _enable_perf_counter();
382
383 /**
384 * Disable the performance counter.
385 */
386 void _disable_perf_counter();
387
388 /**
389 * Refresh perf counter values.
390 */
391 void _refresh_perf_values();
392
393 friend class CephContextObs;
394 };
395 #ifdef __cplusplus
396 }
397 #endif
398 #endif // WITH_SEASTAR
399
400 #if !(defined(WITH_SEASTAR) && !defined(WITH_ALIEN)) && defined(__cplusplus)
401 namespace ceph::common {
402 inline void intrusive_ptr_add_ref(CephContext* cct)
403 {
404 cct->get();
405 }
406
407 inline void intrusive_ptr_release(CephContext* cct)
408 {
409 cct->put();
410 }
411 }
412 #endif // !(defined(WITH_SEASTAR) && !defined(WITH_ALIEN)) && defined(__cplusplus)
413 #endif