1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 New Dream Network
7 * Copyright (C) 2017 OVH
9 * This is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License version 2.1, as published by the Free Software
12 * Foundation. See file COPYING.
18 #include <boost/algorithm/string.hpp>
20 #include "include/mempool.h"
21 #include "common/admin_socket.h"
22 #include "common/perf_counters.h"
23 #include "common/Thread.h"
24 #include "common/code_environment.h"
25 #include "common/ceph_context.h"
26 #include "common/ceph_crypto.h"
27 #include "common/config.h"
28 #include "common/debug.h"
29 #include "common/HeartbeatMap.h"
30 #include "common/errno.h"
31 #include "common/lockdep.h"
32 #include "common/Formatter.h"
33 #include "common/Graylog.h"
35 #include "auth/Crypto.h"
36 #include "include/str_list.h"
37 #include "common/Mutex.h"
38 #include "common/Cond.h"
39 #include "common/PluginRegistry.h"
40 #include "common/valgrind.h"
45 #include "include/Spinlock.h"
47 using ceph::HeartbeatMap
;
51 class LockdepObs
: public md_config_obs_t
{
53 explicit LockdepObs(CephContext
*cct
) : m_cct(cct
), m_registered(false) {
55 ~LockdepObs() override
{
57 lockdep_unregister_ceph_context(m_cct
);
61 const char** get_tracked_conf_keys() const override
{
62 static const char *KEYS
[] = {"lockdep", NULL
};
66 void handle_conf_change(const md_config_t
*conf
,
67 const std::set
<std::string
> &changed
) override
{
68 if (conf
->lockdep
&& !m_registered
) {
69 lockdep_register_ceph_context(m_cct
);
71 } else if (!conf
->lockdep
&& m_registered
) {
72 lockdep_unregister_ceph_context(m_cct
);
81 class MempoolObs
: public md_config_obs_t
,
82 public AdminSocketHook
{
86 explicit MempoolObs(CephContext
*cct
) : cct(cct
) {
87 cct
->_conf
->add_observer(this);
88 int r
= cct
->get_admin_socket()->register_command(
95 ~MempoolObs() override
{
96 cct
->_conf
->remove_observer(this);
97 cct
->get_admin_socket()->unregister_command("dump_mempools");
101 const char** get_tracked_conf_keys() const override
{
102 static const char *KEYS
[] = {
109 void handle_conf_change(const md_config_t
*conf
,
110 const std::set
<std::string
> &changed
) override
{
111 if (changed
.count("mempool_debug")) {
112 mempool::set_debug_mode(cct
->_conf
->mempool_debug
);
117 bool call(std::string command
, cmdmap_t
& cmdmap
, std::string format
,
118 bufferlist
& out
) override
{
119 if (command
== "dump_mempools") {
120 std::unique_ptr
<Formatter
> f(Formatter::create(format
));
121 f
->open_object_section("mempools");
122 mempool::dump(f
.get());
131 } // anonymous namespace
133 class CephContextServiceThread
: public Thread
136 explicit CephContextServiceThread(CephContext
*cct
)
137 : _lock("CephContextServiceThread::_lock"),
138 _reopen_logs(false), _exit_thread(false), _cct(cct
)
142 ~CephContextServiceThread() override
{}
144 void *entry() override
147 Mutex::Locker
l(_lock
);
149 if (_cct
->_conf
->heartbeat_interval
) {
150 utime_t
interval(_cct
->_conf
->heartbeat_interval
, 0);
151 _cond
.WaitInterval(_lock
, interval
);
160 _cct
->_log
->reopen_log_file();
161 _reopen_logs
= false;
163 _cct
->_heartbeat_map
->check_touch_file();
165 // refresh the perf coutners
166 _cct
->refresh_perf_values();
173 Mutex::Locker
l(_lock
);
180 Mutex::Locker
l(_lock
);
195 * observe logging config changes
197 * The logging subsystem sits below most of the ceph code, including
198 * the config subsystem, to keep it simple and self-contained. Feed
199 * logging-related config changes to the log.
201 class LogObs
: public md_config_obs_t
{
202 ceph::logging::Log
*log
;
205 explicit LogObs(ceph::logging::Log
*l
) : log(l
) {}
207 const char** get_tracked_conf_keys() const override
{
208 static const char *KEYS
[] = {
227 void handle_conf_change(const md_config_t
*conf
,
228 const std::set
<std::string
> &changed
) override
{
230 if (changed
.count("log_to_stderr") || changed
.count("err_to_stderr")) {
231 int l
= conf
->log_to_stderr
? 99 : (conf
->err_to_stderr
? -1 : -2);
232 log
->set_stderr_level(l
, l
);
236 if (changed
.count("log_to_syslog")) {
237 int l
= conf
->log_to_syslog
? 99 : (conf
->err_to_syslog
? -1 : -2);
238 log
->set_syslog_level(l
, l
);
242 if (changed
.count("log_file")) {
243 log
->set_log_file(conf
->log_file
);
244 log
->reopen_log_file();
247 if (changed
.count("log_max_new")) {
249 log
->set_max_new(conf
->log_max_new
);
252 if (changed
.count("log_max_recent")) {
253 log
->set_max_recent(conf
->log_max_recent
);
257 if (changed
.count("log_to_graylog") || changed
.count("err_to_graylog")) {
258 int l
= conf
->log_to_graylog
? 99 : (conf
->err_to_graylog
? -1 : -2);
259 log
->set_graylog_level(l
, l
);
261 if (conf
->log_to_graylog
|| conf
->err_to_graylog
) {
262 log
->start_graylog();
263 } else if (! (conf
->log_to_graylog
&& conf
->err_to_graylog
)) {
268 if (log
->graylog() && (changed
.count("log_graylog_host") || changed
.count("log_graylog_port"))) {
269 log
->graylog()->set_destination(conf
->log_graylog_host
, conf
->log_graylog_port
);
273 if (log
->graylog() && changed
.count("host")) {
274 log
->graylog()->set_hostname(conf
->host
);
277 if (log
->graylog() && changed
.count("fsid")) {
278 log
->graylog()->set_fsid(conf
->fsid
);
284 // cct config watcher
285 class CephContextObs
: public md_config_obs_t
{
289 explicit CephContextObs(CephContext
*cct
) : cct(cct
) {}
291 const char** get_tracked_conf_keys() const override
{
292 static const char *KEYS
[] = {
293 "enable_experimental_unrecoverable_data_corrupting_features",
300 void handle_conf_change(const md_config_t
*conf
,
301 const std::set
<std::string
> &changed
) override
{
303 "enable_experimental_unrecoverable_data_corrupting_features")) {
304 ceph_spin_lock(&cct
->_feature_lock
);
306 conf
->enable_experimental_unrecoverable_data_corrupting_features
,
307 cct
->_experimental_features
);
308 ceph_spin_unlock(&cct
->_feature_lock
);
309 if (getenv("CEPH_DEV") == NULL
) {
310 if (!cct
->_experimental_features
.empty()) {
311 if (cct
->_experimental_features
.count("*")) {
312 lderr(cct
) << "WARNING: all dangerous and experimental features are enabled." << dendl
;
314 lderr(cct
) << "WARNING: the following dangerous and experimental features are enabled: "
315 << cct
->_experimental_features
<< dendl
;
321 if (changed
.count("crush_location")) {
322 cct
->crush_location
.update_from_conf();
327 bool CephContext::check_experimental_feature_enabled(const std::string
& feat
)
329 stringstream message
;
330 bool enabled
= check_experimental_feature_enabled(feat
, &message
);
331 lderr(this) << message
.str() << dendl
;
335 bool CephContext::check_experimental_feature_enabled(const std::string
& feat
,
336 std::ostream
*message
)
338 ceph_spin_lock(&_feature_lock
);
339 bool enabled
= (_experimental_features
.count(feat
) ||
340 _experimental_features
.count("*"));
341 ceph_spin_unlock(&_feature_lock
);
344 (*message
) << "WARNING: experimental feature '" << feat
<< "' is enabled\n";
345 (*message
) << "Please be aware that this feature is experimental, untested,\n";
346 (*message
) << "unsupported, and may result in data corruption, data loss,\n";
347 (*message
) << "and/or irreparable damage to your cluster. Do not use\n";
348 (*message
) << "feature with important data.\n";
350 (*message
) << "*** experimental feature '" << feat
<< "' is not enabled ***\n";
351 (*message
) << "This feature is marked as experimental, which means it\n";
352 (*message
) << " - is untested\n";
353 (*message
) << " - is unsupported\n";
354 (*message
) << " - may corrupt your data\n";
355 (*message
) << " - may break your cluster is an unrecoverable fashion\n";
356 (*message
) << "To enable this feature, add this to your ceph.conf:\n";
357 (*message
) << " enable experimental unrecoverable data corrupting features = " << feat
<< "\n";
364 class CephContextHook
: public AdminSocketHook
{
368 explicit CephContextHook(CephContext
*cct
) : m_cct(cct
) {}
370 bool call(std::string command
, cmdmap_t
& cmdmap
, std::string format
,
371 bufferlist
& out
) override
{
372 m_cct
->do_command(command
, cmdmap
, format
, &out
);
377 void CephContext::do_command(std::string command
, cmdmap_t
& cmdmap
,
378 std::string format
, bufferlist
*out
)
380 Formatter
*f
= Formatter::create(format
, "json-pretty", "json-pretty");
382 for (cmdmap_t::iterator it
= cmdmap
.begin(); it
!= cmdmap
.end(); ++it
) {
383 if (it
->first
!= "prefix") {
384 ss
<< it
->first
<< ":" << cmd_vartype_stringify(it
->second
) << " ";
387 lgeneric_dout(this, 1) << "do_command '" << command
<< "' '"
388 << ss
.str() << dendl
;
389 if (command
== "perfcounters_dump" || command
== "1" ||
390 command
== "perf dump") {
393 cmd_getval(this, cmdmap
, "logger", logger
);
394 cmd_getval(this, cmdmap
, "counter", counter
);
395 _perf_counters_collection
->dump_formatted(f
, false, logger
, counter
);
397 else if (command
== "perfcounters_schema" || command
== "2" ||
398 command
== "perf schema") {
399 _perf_counters_collection
->dump_formatted(f
, true);
401 else if (command
== "perf histogram dump") {
404 cmd_getval(this, cmdmap
, "logger", logger
);
405 cmd_getval(this, cmdmap
, "counter", counter
);
406 _perf_counters_collection
->dump_formatted_histograms(f
, false, logger
,
409 else if (command
== "perf histogram schema") {
410 _perf_counters_collection
->dump_formatted_histograms(f
, true);
412 else if (command
== "perf reset") {
414 string section
= command
;
415 f
->open_object_section(section
.c_str());
416 if (!cmd_getval(this, cmdmap
, "var", var
)) {
417 f
->dump_string("error", "syntax error: 'perf reset <var>'");
419 if(!_perf_counters_collection
->reset(var
))
420 f
->dump_stream("error") << "Not find: " << var
;
422 f
->dump_string("success", command
+ ' ' + var
);
427 string section
= command
;
428 boost::replace_all(section
, " ", "_");
429 f
->open_object_section(section
.c_str());
430 if (command
== "config show") {
431 _conf
->show_config(f
);
433 else if (command
== "config set") {
435 std::vector
<std::string
> val
;
437 if (!(cmd_getval(this, cmdmap
, "var", var
)) ||
438 !(cmd_getval(this, cmdmap
, "val", val
))) {
439 f
->dump_string("error", "syntax error: 'config set <var> <value>'");
441 // val may be multiple words
442 string valstr
= str_join(val
, " ");
443 int r
= _conf
->set_val(var
.c_str(), valstr
.c_str());
445 f
->dump_stream("error") << "error setting '" << var
<< "' to '" << valstr
<< "': " << cpp_strerror(r
);
448 _conf
->apply_changes(&ss
);
449 f
->dump_string("success", ss
.str());
452 } else if (command
== "config get") {
454 if (!cmd_getval(this, cmdmap
, "var", var
)) {
455 f
->dump_string("error", "syntax error: 'config get <var>'");
458 memset(buf
, 0, sizeof(buf
));
460 int r
= _conf
->get_val(var
.c_str(), &tmp
, sizeof(buf
));
462 f
->dump_stream("error") << "error getting '" << var
<< "': " << cpp_strerror(r
);
464 f
->dump_string(var
.c_str(), buf
);
467 } else if (command
== "config diff") {
468 md_config_t def_conf
;
469 def_conf
.set_val("cluster", _conf
->cluster
);
470 def_conf
.name
= _conf
->name
;
471 def_conf
.set_val("host", _conf
->host
);
472 def_conf
.apply_changes(NULL
);
474 map
<string
,pair
<string
,string
> > diff
;
476 def_conf
.diff(_conf
, &diff
, &unknown
);
477 f
->open_object_section("diff");
479 f
->open_object_section("current");
480 for (map
<string
,pair
<string
,string
> >::iterator p
= diff
.begin();
481 p
!= diff
.end(); ++p
) {
482 f
->dump_string(p
->first
.c_str(), p
->second
.second
);
484 f
->close_section(); // current
485 f
->open_object_section("defaults");
486 for (map
<string
,pair
<string
,string
> >::iterator p
= diff
.begin();
487 p
!= diff
.end(); ++p
) {
488 f
->dump_string(p
->first
.c_str(), p
->second
.first
);
490 f
->close_section(); // defaults
491 f
->close_section(); // diff
493 f
->open_array_section("unknown");
494 for (set
<string
>::iterator p
= unknown
.begin();
495 p
!= unknown
.end(); ++p
) {
496 f
->dump_string("option", *p
);
498 f
->close_section(); // unknown
499 } else if (command
== "log flush") {
502 else if (command
== "log dump") {
505 else if (command
== "log reopen") {
506 _log
->reopen_log_file();
509 assert(0 == "registered under wrong command?");
515 lgeneric_dout(this, 1) << "do_command '" << command
<< "' '" << ss
.str()
516 << "result is " << out
->length() << " bytes" << dendl
;
520 CephContext::CephContext(uint32_t module_type_
, int init_flags_
)
522 _conf(new md_config_t()),
524 _module_type(module_type_
),
525 _init_flags(init_flags_
),
530 _crypto_inited(false),
531 _service_thread(NULL
),
534 _perf_counters_collection(NULL
),
535 _perf_counters_conf_obs(NULL
),
536 _heartbeat_map(NULL
),
539 _plugin_registry(NULL
),
541 crush_location(this),
544 ceph_spin_init(&_service_thread_lock
);
545 ceph_spin_init(&_associated_objs_lock
);
546 ceph_spin_init(&_fork_watchers_lock
);
547 ceph_spin_init(&_feature_lock
);
548 ceph_spin_init(&_cct_perf_lock
);
550 _log
= new ceph::logging::Log(&_conf
->subsys
);
553 _log_obs
= new LogObs(_log
);
554 _conf
->add_observer(_log_obs
);
556 _cct_obs
= new CephContextObs(this);
557 _conf
->add_observer(_cct_obs
);
559 _lockdep_obs
= new LockdepObs(this);
560 _conf
->add_observer(_lockdep_obs
);
562 _perf_counters_collection
= new PerfCountersCollection(this);
564 _admin_socket
= new AdminSocket(this);
565 _heartbeat_map
= new HeartbeatMap(this);
567 _plugin_registry
= new PluginRegistry(this);
569 _admin_hook
= new CephContextHook(this);
570 _admin_socket
->register_command("perfcounters_dump", "perfcounters_dump", _admin_hook
, "");
571 _admin_socket
->register_command("1", "1", _admin_hook
, "");
572 _admin_socket
->register_command("perf dump", "perf dump name=logger,type=CephString,req=false name=counter,type=CephString,req=false", _admin_hook
, "dump perfcounters value");
573 _admin_socket
->register_command("perfcounters_schema", "perfcounters_schema", _admin_hook
, "");
574 _admin_socket
->register_command("perf histogram dump", "perf histogram dump name=logger,type=CephString,req=false name=counter,type=CephString,req=false", _admin_hook
, "dump perf histogram values");
575 _admin_socket
->register_command("2", "2", _admin_hook
, "");
576 _admin_socket
->register_command("perf schema", "perf schema", _admin_hook
, "dump perfcounters schema");
577 _admin_socket
->register_command("perf histogram schema", "perf histogram schema", _admin_hook
, "dump perf histogram schema");
578 _admin_socket
->register_command("perf reset", "perf reset name=var,type=CephString", _admin_hook
, "perf reset <name>: perf reset all or one perfcounter name");
579 _admin_socket
->register_command("config show", "config show", _admin_hook
, "dump current config settings");
580 _admin_socket
->register_command("config set", "config set name=var,type=CephString name=val,type=CephString,n=N", _admin_hook
, "config set <field> <val> [<val> ...]: set a config variable");
581 _admin_socket
->register_command("config get", "config get name=var,type=CephString", _admin_hook
, "config get <field>: get the config value");
582 _admin_socket
->register_command("config diff",
583 "config diff", _admin_hook
,
584 "dump diff of current config and default config");
585 _admin_socket
->register_command("log flush", "log flush", _admin_hook
, "flush log entries to log file");
586 _admin_socket
->register_command("log dump", "log dump", _admin_hook
, "dump recent log entries to log file");
587 _admin_socket
->register_command("log reopen", "log reopen", _admin_hook
, "reopen log file");
589 _crypto_none
= CryptoHandler::create(CEPH_CRYPTO_NONE
);
590 _crypto_aes
= CryptoHandler::create(CEPH_CRYPTO_AES
);
592 MempoolObs
*mempool_obs
= 0;
593 lookup_or_create_singleton_object(mempool_obs
, "mempool_obs");
596 CephContext::~CephContext()
598 join_service_thread();
600 for (map
<string
, SingletonWrapper
*>::iterator it
= _associated_objs
.begin();
601 it
!= _associated_objs
.end(); ++it
)
605 _perf_counters_collection
->remove(_cct_perf
);
610 delete _plugin_registry
;
612 _admin_socket
->unregister_command("perfcounters_dump");
613 _admin_socket
->unregister_command("perf dump");
614 _admin_socket
->unregister_command("1");
615 _admin_socket
->unregister_command("perfcounters_schema");
616 _admin_socket
->unregister_command("perf schema");
617 _admin_socket
->unregister_command("2");
618 _admin_socket
->unregister_command("perf reset");
619 _admin_socket
->unregister_command("config show");
620 _admin_socket
->unregister_command("config set");
621 _admin_socket
->unregister_command("config get");
622 _admin_socket
->unregister_command("config diff");
623 _admin_socket
->unregister_command("log flush");
624 _admin_socket
->unregister_command("log dump");
625 _admin_socket
->unregister_command("log reopen");
627 delete _admin_socket
;
629 delete _heartbeat_map
;
631 delete _perf_counters_collection
;
632 _perf_counters_collection
= NULL
;
634 delete _perf_counters_conf_obs
;
635 _perf_counters_conf_obs
= NULL
;
637 _conf
->remove_observer(_log_obs
);
641 _conf
->remove_observer(_cct_obs
);
645 _conf
->remove_observer(_lockdep_obs
);
654 ceph_spin_destroy(&_service_thread_lock
);
655 ceph_spin_destroy(&_fork_watchers_lock
);
656 ceph_spin_destroy(&_associated_objs_lock
);
657 ceph_spin_destroy(&_feature_lock
);
658 ceph_spin_destroy(&_cct_perf_lock
);
663 ceph::crypto::shutdown(g_code_env
== CODE_ENVIRONMENT_LIBRARY
);
666 void CephContext::put() {
667 if (nref
.dec() == 0) {
668 ANNOTATE_HAPPENS_AFTER(&nref
);
669 ANNOTATE_HAPPENS_BEFORE_FORGET_ALL(&nref
);
672 ANNOTATE_HAPPENS_BEFORE(&nref
);
676 void CephContext::init_crypto()
678 if (!_crypto_inited
) {
679 ceph::crypto::init(this);
680 _crypto_inited
= true;
684 void CephContext::start_service_thread()
686 ceph_spin_lock(&_service_thread_lock
);
687 if (_service_thread
) {
688 ceph_spin_unlock(&_service_thread_lock
);
691 _service_thread
= new CephContextServiceThread(this);
692 _service_thread
->create("service");
693 ceph_spin_unlock(&_service_thread_lock
);
695 // make logs flush on_exit()
696 if (_conf
->log_flush_on_exit
)
697 _log
->set_flush_on_exit();
699 // Trigger callbacks on any config observers that were waiting for
700 // it to become safe to start threads.
701 _conf
->set_val("internal_safe_to_start_threads", "true");
702 _conf
->call_all_observers();
704 // start admin socket
705 if (_conf
->admin_socket
.length())
706 _admin_socket
->init(_conf
->admin_socket
);
709 void CephContext::reopen_logs()
711 ceph_spin_lock(&_service_thread_lock
);
713 _service_thread
->reopen_logs();
714 ceph_spin_unlock(&_service_thread_lock
);
717 void CephContext::join_service_thread()
719 ceph_spin_lock(&_service_thread_lock
);
720 CephContextServiceThread
*thread
= _service_thread
;
722 ceph_spin_unlock(&_service_thread_lock
);
725 _service_thread
= NULL
;
726 ceph_spin_unlock(&_service_thread_lock
);
728 thread
->exit_thread();
733 uint32_t CephContext::get_module_type() const
738 void CephContext::set_init_flags(int flags
)
743 int CephContext::get_init_flags() const
748 PerfCountersCollection
*CephContext::get_perfcounters_collection()
750 return _perf_counters_collection
;
753 void CephContext::enable_perf_counter()
755 PerfCountersBuilder
plb(this, "cct", l_cct_first
, l_cct_last
);
756 plb
.add_u64(l_cct_total_workers
, "total_workers", "Total workers");
757 plb
.add_u64(l_cct_unhealthy_workers
, "unhealthy_workers", "Unhealthy workers");
758 PerfCounters
*perf_tmp
= plb
.create_perf_counters();
760 ceph_spin_lock(&_cct_perf_lock
);
761 assert(_cct_perf
== NULL
);
762 _cct_perf
= perf_tmp
;
763 ceph_spin_unlock(&_cct_perf_lock
);
765 _perf_counters_collection
->add(_cct_perf
);
768 void CephContext::disable_perf_counter()
770 _perf_counters_collection
->remove(_cct_perf
);
772 ceph_spin_lock(&_cct_perf_lock
);
775 ceph_spin_unlock(&_cct_perf_lock
);
778 void CephContext::refresh_perf_values()
780 ceph_spin_lock(&_cct_perf_lock
);
782 _cct_perf
->set(l_cct_total_workers
, _heartbeat_map
->get_total_workers());
783 _cct_perf
->set(l_cct_unhealthy_workers
, _heartbeat_map
->get_unhealthy_workers());
785 ceph_spin_unlock(&_cct_perf_lock
);
788 AdminSocket
*CephContext::get_admin_socket()
790 return _admin_socket
;
793 CryptoHandler
*CephContext::get_crypto_handler(int type
)
796 case CEPH_CRYPTO_NONE
:
798 case CEPH_CRYPTO_AES
: