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) 2004-2006 Sage Weil <sage@newdream.net>
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.
15 #include <boost/type_traits.hpp>
17 #include "common/ceph_argparse.h"
18 #include "common/common_init.h"
19 #include "common/config.h"
20 #include "common/config_obs.h"
21 #include "include/str_list.h"
22 #include "include/stringify.h"
23 #include "osd/osd_types.h"
24 #include "common/errno.h"
25 #include "common/hostname.h"
26 #include "common/dout.h"
28 /* Don't use standard Ceph logging in this file.
29 * We can't use logging until it's initialized, and a lot of the necessary
30 * initialization happens here.
38 #define dout_subsys ceph_subsys_monc
42 using std::ostringstream
;
46 static const char *CEPH_CONF_FILE_DEFAULT
= "$data_dir/config, /etc/ceph/$cluster.conf, $home/.ceph/$cluster.conf, $cluster.conf"
47 #if defined(__FreeBSD__)
48 ", /usr/local/etc/ceph/$cluster.conf"
53 #define STRINGIFY(x) _STR(x)
55 const char *ceph_conf_level_name(int level
)
58 case CONF_DEFAULT
: return "default"; // built-in default
59 case CONF_MON
: return "mon"; // monitor config database
60 case CONF_ENV
: return "env"; // process environment (CEPH_ARGS)
61 case CONF_FILE
: return "file"; // ceph.conf file
62 case CONF_CMDLINE
: return "cmdline"; // process command line args
63 case CONF_OVERRIDE
: return "override"; // injectargs or 'config set' at runtime
64 case CONF_FINAL
: return "final";
65 default: return "???";
69 int ceph_resolve_file_search(const std::string
& filename_list
,
73 get_str_list(filename_list
, ls
);
76 list
<string
>::iterator iter
;
77 for (iter
= ls
.begin(); iter
!= ls
.end(); ++iter
) {
78 int fd
= ::open(iter
->c_str(), O_RDONLY
|O_CLOEXEC
);
91 static int conf_stringify(const Option::value_t
& v
, string
*out
)
93 if (boost::get
<boost::blank
>(&v
)) {
96 *out
= Option::to_str(v
);
100 md_config_t::md_config_t(ConfigValues
& values
,
101 const ConfigTracker
& tracker
,
103 : is_daemon(is_daemon
)
105 // Load the compile-time list of Option into
106 // a map so that we can resolve keys quickly.
107 for (const auto &i
: ceph_options
) {
108 if (schema
.count(i
.name
)) {
109 // We may be instantiated pre-logging so send
110 std::cerr
<< "Duplicate config key in schema: '" << i
.name
<< "'"
114 schema
.emplace(i
.name
, i
);
117 // Define the debug_* options as well.
118 subsys_options
.reserve(values
.subsys
.get_num());
119 for (unsigned i
= 0; i
< values
.subsys
.get_num(); ++i
) {
120 string name
= string("debug_") + values
.subsys
.get_name(i
);
121 subsys_options
.push_back(
122 Option(name
, Option::TYPE_STR
, Option::LEVEL_ADVANCED
));
123 Option
& opt
= subsys_options
.back();
124 opt
.set_default(stringify(values
.subsys
.get_log_level(i
)) + "/" +
125 stringify(values
.subsys
.get_gather_level(i
)));
126 string desc
= string("Debug level for ") + values
.subsys
.get_name(i
);
127 opt
.set_description(desc
.c_str());
128 opt
.set_flag(Option::FLAG_RUNTIME
);
129 opt
.set_long_description("The value takes the form 'N' or 'N/M' where N and M are values between 0 and 99. N is the debug level to log (all values below this are included), and M is the level to gather and buffer in memory. In the event of a crash, the most recent items <= M are dumped to the log file.");
131 opt
.set_validator([](std::string
*value
, std::string
*error_message
) {
133 int r
= sscanf(value
->c_str(), "%d/%d", &m
, &n
);
135 if (m
< 0 || m
> 99) {
136 *error_message
= "value must be in range [0, 99]";
140 if (n
< 0 || n
> 99) {
141 *error_message
= "value must be in range [0, 99]";
147 *value
= stringify(m
) + "/" + stringify(n
);
150 *error_message
= "value must take the form N or N/M, where N and M are integers";
156 for (auto& opt
: subsys_options
) {
157 schema
.emplace(opt
.name
, opt
);
160 // Populate list of legacy_values according to the OPTION() definitions
161 // Note that this is just setting up our map of name->member ptr. The
162 // default values etc will get loaded in along with new-style data,
163 // as all loads write to both the values map, and the legacy
164 // members if present.
166 #define OPTION(name, type) \
167 {STRINGIFY(name), &ConfigValues::name},
168 #define SAFE_OPTION(name, type) OPTION(name, type)
169 #include "common/legacy_config_opts.h"
176 // Validate default values from the schema
177 for (const auto &i
: schema
) {
178 const Option
&opt
= i
.second
;
179 if (opt
.type
== Option::TYPE_STR
) {
180 bool has_daemon_default
= !boost::get
<boost::blank
>(&opt
.daemon_value
);
181 Option::value_t default_val
;
182 if (is_daemon
&& has_daemon_default
) {
183 default_val
= opt
.daemon_value
;
185 default_val
= opt
.value
;
187 // We call pre_validate as a sanity check, but also to get any
188 // side effect (value modification) from the validator.
189 std::string
*def_str
= boost::get
<std::string
>(&default_val
);
190 std::string val
= *def_str
;
192 if (opt
.pre_validate(&val
, &err
) != 0) {
193 std::cerr
<< "Default value " << opt
.name
<< "=" << *def_str
<< " is "
194 "invalid: " << err
<< std::endl
;
196 // This is the compiled-in default that is failing its own option's
197 // validation, so this is super-invalid and should never make it
198 // past a pull request: crash out.
201 if (val
!= *def_str
) {
202 // if the validator normalizes the string into a different form than
203 // what was compiled in, use that.
204 set_val_default(values
, tracker
, opt
.name
, val
);
209 // Copy out values (defaults) into any legacy (C struct member) fields
210 update_legacy_vals(values
);
213 md_config_t::~md_config_t()
218 * Sanity check schema. Assert out on failures, to ensure any bad changes
219 * cannot possibly pass any testing and make it into a release.
221 void md_config_t::validate_schema()
223 for (const auto &i
: schema
) {
224 const auto &opt
= i
.second
;
225 for (const auto &see_also_key
: opt
.see_also
) {
226 if (schema
.count(see_also_key
) == 0) {
227 std::cerr
<< "Non-existent see-also key '" << see_also_key
228 << "' on option '" << opt
.name
<< "'" << std::endl
;
234 for (const auto &i
: legacy_values
) {
235 if (schema
.count(i
.first
) == 0) {
236 std::cerr
<< "Schema is missing legacy field '" << i
.first
<< "'"
243 const Option
*md_config_t::find_option(const std::string_view name
) const
245 auto p
= schema
.find(name
);
246 if (p
!= schema
.end()) {
252 void md_config_t::set_val_default(ConfigValues
& values
,
253 const ConfigTracker
& tracker
,
254 const string_view name
, const std::string
& val
)
256 const Option
*o
= find_option(name
);
259 int r
= _set_val(values
, tracker
, val
, *o
, CONF_DEFAULT
, &err
);
263 int md_config_t::set_mon_vals(CephContext
*cct
,
264 ConfigValues
& values
,
265 const ConfigTracker
& tracker
,
266 const map
<string
,string
,less
<>>& kv
,
267 config_callback config_cb
)
269 ignored_mon_values
.clear();
272 ldout(cct
, 4) << __func__
<< " no callback set" << dendl
;
277 if (config_cb(i
.first
, i
.second
)) {
278 ldout(cct
, 4) << __func__
<< " callback consumed " << i
.first
<< dendl
;
281 ldout(cct
, 4) << __func__
<< " callback ignored " << i
.first
<< dendl
;
283 const Option
*o
= find_option(i
.first
);
285 ldout(cct
,10) << __func__
<< " " << i
.first
<< " = " << i
.second
286 << " (unrecognized option)" << dendl
;
289 if (o
->has_flag(Option::FLAG_NO_MON_UPDATE
)) {
290 ignored_mon_values
.emplace(i
);
294 int r
= _set_val(values
, tracker
, i
.second
, *o
, CONF_MON
, &err
);
296 ldout(cct
, 4) << __func__
<< " failed to set " << i
.first
<< " = "
297 << i
.second
<< ": " << err
<< dendl
;
298 ignored_mon_values
.emplace(i
);
299 } else if (r
== ConfigValues::SET_NO_CHANGE
||
300 r
== ConfigValues::SET_NO_EFFECT
) {
301 ldout(cct
,20) << __func__
<< " " << i
.first
<< " = " << i
.second
302 << " (no change)" << dendl
;
303 } else if (r
== ConfigValues::SET_HAVE_EFFECT
) {
304 ldout(cct
,10) << __func__
<< " " << i
.first
<< " = " << i
.second
<< dendl
;
309 values
.for_each([&] (auto name
, auto configs
) {
310 auto config
= configs
.find(CONF_MON
);
311 if (config
== configs
.end()) {
314 if (kv
.find(name
) != kv
.end()) {
317 ldout(cct
,10) << __func__
<< " " << name
318 << " cleared (was " << Option::to_str(config
->second
) << ")"
320 values
.rm_val(name
, CONF_MON
);
321 // if this is a debug option, it needs to propagate to teh subsys;
322 // this isn't covered by update_legacy_vals() below. similarly,
323 // we want to trigger a config notification for these items.
324 const Option
*o
= find_option(name
);
325 _refresh(values
, *o
);
328 update_legacy_vals(values
);
332 int md_config_t::parse_config_files(ConfigValues
& values
,
333 const ConfigTracker
& tracker
,
334 const char *conf_files_str
,
335 std::ostream
*warnings
,
339 if (safe_to_start_threads
)
342 if (!values
.cluster
.size() && !conf_files_str
) {
344 * set the cluster name to 'ceph' when neither cluster name nor
345 * configuration file are specified.
347 values
.cluster
= "ceph";
350 if (!conf_files_str
) {
351 const char *c
= getenv("CEPH_CONF");
356 if (flags
& CINIT_FLAG_NO_DEFAULT_CONFIG_FILE
)
358 conf_files_str
= CEPH_CONF_FILE_DEFAULT
;
362 std::list
<std::string
> conf_files
;
363 get_str_list(conf_files_str
, conf_files
);
364 auto p
= conf_files
.begin();
365 while (p
!= conf_files
.end()) {
367 if (s
.find("$data_dir") != string::npos
&&
368 data_dir_option
.empty()) {
369 // useless $data_dir item, skip
370 p
= conf_files
.erase(p
);
372 early_expand_meta(values
, s
, warnings
);
378 list
<string
>::const_iterator c
;
379 for (c
= conf_files
.begin(); c
!= conf_files
.end(); ++c
) {
383 int ret
= cf
.parse_file(fn
.c_str(), &oss
);
384 parse_error
= oss
.str();
391 // it must have been all ENOENTs, that's the only way we got here
392 if (c
== conf_files
.end())
395 if (values
.cluster
.size() == 0) {
397 * If cluster name is not set yet, use the prefix of the
398 * basename of configuration file as cluster name.
400 auto start
= c
->rfind('/') + 1;
401 auto end
= c
->find(".conf", start
);
402 if (end
== c
->npos
) {
404 * If the configuration file does not follow $cluster.conf
405 * convention, we do the last try and assign the cluster to
408 values
.cluster
= "ceph";
410 values
.cluster
= c
->substr(start
, end
- start
);
414 std::vector
<std::string
> my_sections
;
415 _get_my_sections(values
, my_sections
);
416 for (const auto &i
: schema
) {
417 const auto &opt
= i
.second
;
419 int ret
= _get_val_from_conf_file(my_sections
, opt
.name
, val
);
421 std::string error_message
;
422 int r
= _set_val(values
, tracker
, val
, opt
, CONF_FILE
, &error_message
);
423 if (warnings
!= nullptr && (r
< 0 || !error_message
.empty())) {
424 *warnings
<< "parse error setting '" << opt
.name
<< "' to '" << val
426 if (!error_message
.empty()) {
427 *warnings
<< " (" << error_message
<< ")";
429 *warnings
<< std::endl
;
434 // Warn about section names that look like old-style section names
435 std::deque
< std::string
> old_style_section_names
;
436 for (auto& [name
, section
] : cf
) {
437 if (((name
.find("mds") == 0) || (name
.find("mon") == 0) ||
438 (name
.find("osd") == 0)) && (name
.size() > 3) && (name
[3] != '.')) {
439 old_style_section_names
.push_back(name
);
442 if (!old_style_section_names
.empty()) {
444 cerr
<< "ERROR! old-style section name(s) found: ";
446 for (std::deque
< std::string
>::const_iterator os
= old_style_section_names
.begin();
447 os
!= old_style_section_names
.end(); ++os
) {
451 cerr
<< ". Please use the new style section names that include a period.";
454 update_legacy_vals(values
);
459 void md_config_t::parse_env(unsigned entity_type
,
460 ConfigValues
& values
,
461 const ConfigTracker
& tracker
,
462 const char *args_var
)
464 if (safe_to_start_threads
)
467 args_var
= "CEPH_ARGS";
469 if (auto s
= getenv("CEPH_KEYRING"); s
) {
471 _set_val(values
, tracker
, s
, *find_option("keyring"), CONF_ENV
, &err
);
473 if (auto dir
= getenv("CEPH_LIB"); dir
) {
474 for (auto name
: { "erasure_code_dir", "plugin_dir", "osd_class_dir" }) {
476 const Option
*o
= find_option(name
);
478 _set_val(values
, tracker
, dir
, *o
, CONF_ENV
, &err
);
482 // Apply pod memory limits:
484 // There are two types of resource requests: `limits` and `requests`.
486 // - Requests: Used by the K8s scheduler to determine on which nodes to
487 // schedule the pods. This helps spread the pods to different nodes. This
488 // value should be conservative in order to make sure all the pods are
489 // schedulable. This corresponds to POD_MEMORY_REQUEST (set by the Rook
490 // CRD) and is the target memory utilization we try to maintain for daemons
493 // If POD_MEMORY_REQUEST is present, we use it as the target.
495 // - Limits: At runtime, the container runtime (and Linux) will use the
496 // limits to see if the pod is using too many resources. In that case, the
497 // pod will be killed/restarted automatically if the pod goes over the limit.
498 // This should be higher than what is specified for requests (potentially
499 // much higher). This corresponds to the cgroup memory limit that will
500 // trigger the Linux OOM killer.
502 // If POD_MEMORY_LIMIT is present, we use it as the /default/ value for
503 // the target, which means it will only apply if the *_memory_target option
504 // isn't set via some other path (e.g., POD_MEMORY_REQUEST, or the cluster
505 // config, or whatever.)
507 // Here are the documented best practices:
508 // https://kubernetes.io/docs/tasks/configure-pod-container/assign-cpu-resource/#motivation-for-cpu-requests-and-limits
510 // When the operator creates the CephCluster CR, it will need to generate the
511 // desired requests and limits. As long as we are conservative in our choice
512 // for requests and generous with the limits we should be in a good place to
515 // The support in Rook is already there for applying the limits as seen in
518 // Rook docs on the resource requests and limits:
519 // https://rook.io/docs/rook/v1.0/ceph-cluster-crd.html#cluster-wide-resources-configuration-settings
520 // Example CR settings:
521 // https://github.com/rook/rook/blob/6d2ef936698593036185aabcb00d1d74f9c7bfc1/cluster/examples/kubernetes/ceph/cluster.yaml#L90
523 uint64_t pod_limit
= 0, pod_request
= 0;
524 if (auto pod_lim
= getenv("POD_MEMORY_LIMIT"); pod_lim
) {
526 uint64_t v
= atoll(pod_lim
);
528 switch (entity_type
) {
529 case CEPH_ENTITY_TYPE_OSD
:
531 double cgroup_ratio
= get_val
<double>(
532 values
, "osd_memory_target_cgroup_limit_ratio");
533 if (cgroup_ratio
> 0.0) {
534 pod_limit
= v
* cgroup_ratio
;
535 // set osd_memory_target *default* based on cgroup limit, so that
536 // it can be overridden by any explicit settings elsewhere.
537 set_val_default(values
, tracker
,
538 "osd_memory_target", stringify(pod_limit
));
544 if (auto pod_req
= getenv("POD_MEMORY_REQUEST"); pod_req
) {
545 if (uint64_t v
= atoll(pod_req
); v
) {
549 if (pod_request
&& pod_limit
) {
550 // If both LIMIT and REQUEST are set, ensure that we use the
551 // min of request and limit*ratio. This is important
552 // because k8s set set LIMIT == REQUEST if only LIMIT is
553 // specified, and we want to apply the ratio in that case,
554 // even though REQUEST is present.
555 pod_request
= std::min
<uint64_t>(pod_request
, pod_limit
);
559 switch (entity_type
) {
560 case CEPH_ENTITY_TYPE_OSD
:
561 _set_val(values
, tracker
, stringify(pod_request
),
562 *find_option("osd_memory_target"),
568 if (getenv(args_var
)) {
569 vector
<const char *> env_args
;
570 env_to_vec(env_args
, args_var
);
571 parse_argv(values
, tracker
, env_args
, CONF_ENV
);
575 void md_config_t::show_config(const ConfigValues
& values
,
576 std::ostream
& out
) const
578 _show_config(values
, &out
, nullptr);
581 void md_config_t::show_config(const ConfigValues
& values
,
584 _show_config(values
, nullptr, f
);
587 void md_config_t::config_options(Formatter
*f
) const
589 f
->open_array_section("options");
590 for (const auto& i
: schema
) {
591 f
->dump_object("option", i
.second
);
596 void md_config_t::_show_config(const ConfigValues
& values
,
597 std::ostream
*out
, Formatter
*f
) const
600 *out
<< "name = " << values
.name
<< std::endl
;
601 *out
<< "cluster = " << values
.cluster
<< std::endl
;
604 f
->dump_string("name", stringify(values
.name
));
605 f
->dump_string("cluster", values
.cluster
);
607 for (const auto& i
: schema
) {
608 const Option
&opt
= i
.second
;
610 conf_stringify(_get_val(values
, opt
), &val
);
612 *out
<< opt
.name
<< " = " << val
<< std::endl
;
615 f
->dump_string(opt
.name
.c_str(), val
);
620 int md_config_t::parse_argv(ConfigValues
& values
,
621 const ConfigTracker
& tracker
,
622 std::vector
<const char*>& args
, int level
)
624 if (safe_to_start_threads
) {
628 // In this function, don't change any parts of the configuration directly.
629 // Instead, use set_val to set them. This will allow us to send the proper
630 // observer notifications later.
632 for (std::vector
<const char*>::iterator i
= args
.begin(); i
!= args
.end(); ) {
633 if (strcmp(*i
, "--") == 0) {
634 /* Normally we would use ceph_argparse_double_dash. However, in this
635 * function we *don't* want to remove the double dash, because later
636 * argument parses will still need to see it. */
639 else if (ceph_argparse_flag(args
, i
, "--show_conf", (char*)NULL
)) {
640 cerr
<< cf
<< std::endl
;
643 else if (ceph_argparse_flag(args
, i
, "--show_config", (char*)NULL
)) {
644 do_show_config
= true;
646 else if (ceph_argparse_witharg(args
, i
, &val
, "--show_config_value", (char*)NULL
)) {
647 do_show_config_value
= val
;
649 else if (ceph_argparse_flag(args
, i
, "--no-mon-config", (char*)NULL
)) {
650 values
.no_mon_config
= true;
652 else if (ceph_argparse_flag(args
, i
, "--log-early", (char*)NULL
)) {
653 values
.log_early
= true;
655 else if (ceph_argparse_flag(args
, i
, "--mon-config", (char*)NULL
)) {
656 values
.no_mon_config
= false;
658 else if (ceph_argparse_flag(args
, i
, "--foreground", "-f", (char*)NULL
)) {
659 set_val_or_die(values
, tracker
, "daemonize", "false");
661 else if (ceph_argparse_flag(args
, i
, "-d", (char*)NULL
)) {
662 set_val_or_die(values
, tracker
, "daemonize", "false");
663 set_val_or_die(values
, tracker
, "log_file", "");
664 set_val_or_die(values
, tracker
, "log_to_stderr", "true");
665 set_val_or_die(values
, tracker
, "err_to_stderr", "true");
666 set_val_or_die(values
, tracker
, "log_to_syslog", "false");
668 // Some stuff that we wanted to give universal single-character options for
669 // Careful: you can burn through the alphabet pretty quickly by adding
671 else if (ceph_argparse_witharg(args
, i
, &val
, "--monmap", "-M", (char*)NULL
)) {
672 set_val_or_die(values
, tracker
, "monmap", val
.c_str());
674 else if (ceph_argparse_witharg(args
, i
, &val
, "--mon_host", "-m", (char*)NULL
)) {
675 set_val_or_die(values
, tracker
, "mon_host", val
.c_str());
677 else if (ceph_argparse_witharg(args
, i
, &val
, "--bind", (char*)NULL
)) {
678 set_val_or_die(values
, tracker
, "public_addr", val
.c_str());
680 else if (ceph_argparse_witharg(args
, i
, &val
, "--keyfile", "-K", (char*)NULL
)) {
685 r
= bl
.read_fd(STDIN_FILENO
, 1024);
687 r
= bl
.read_file(val
.c_str(), &err
);
690 string
k(bl
.c_str(), bl
.length());
691 set_val_or_die(values
, tracker
, "key", k
.c_str());
694 else if (ceph_argparse_witharg(args
, i
, &val
, "--keyring", "-k", (char*)NULL
)) {
695 set_val_or_die(values
, tracker
, "keyring", val
.c_str());
697 else if (ceph_argparse_witharg(args
, i
, &val
, "--client_mountpoint", "-r", (char*)NULL
)) {
698 set_val_or_die(values
, tracker
, "client_mountpoint", val
.c_str());
701 int r
= parse_option(values
, tracker
, args
, i
, NULL
, level
);
707 // meta expands could have modified anything. Copy it all out again.
708 update_legacy_vals(values
);
712 void md_config_t::do_argv_commands(const ConfigValues
& values
) const
715 if (do_show_config
) {
716 _show_config(values
, &cout
, NULL
);
720 if (do_show_config_value
.size()) {
722 int r
= conf_stringify(_get_val(values
, do_show_config_value
, 0, &cerr
),
726 std::cerr
<< "failed to get config option '"
727 << do_show_config_value
<< "': option not found" << std::endl
;
729 std::cerr
<< "failed to get config option '"
730 << do_show_config_value
<< "': " << cpp_strerror(r
)
734 std::cout
<< val
<< std::endl
;
739 int md_config_t::parse_option(ConfigValues
& values
,
740 const ConfigTracker
& tracker
,
741 std::vector
<const char*>& args
,
742 std::vector
<const char*>::iterator
& i
,
750 std::string option_name
;
751 std::string error_message
;
753 for (const auto& opt_iter
: schema
) {
754 const Option
&opt
= opt_iter
.second
;
756 std::string
as_option("--");
757 as_option
+= opt
.name
;
758 option_name
= opt
.name
;
759 if (ceph_argparse_witharg(
761 string(string("--default-") + opt
.name
).c_str(), (char*)NULL
)) {
762 if (!err
.str().empty()) {
763 error_message
= err
.str();
767 ret
= _set_val(values
, tracker
, val
, opt
, CONF_DEFAULT
, &error_message
);
769 } else if (opt
.type
== Option::TYPE_BOOL
) {
771 if (ceph_argparse_binary_flag(args
, i
, &res
, oss
, as_option
.c_str(),
774 ret
= _set_val(values
, tracker
, "false", opt
, level
, &error_message
);
776 ret
= _set_val(values
, tracker
, "true", opt
, level
, &error_message
);
781 std::string
no("--no-");
783 if (ceph_argparse_flag(args
, i
, no
.c_str(), (char*)NULL
)) {
784 ret
= _set_val(values
, tracker
, "false", opt
, level
, &error_message
);
788 } else if (ceph_argparse_witharg(args
, i
, &val
, err
,
789 as_option
.c_str(), (char*)NULL
)) {
790 if (!err
.str().empty()) {
791 error_message
= err
.str();
795 ret
= _set_val(values
, tracker
, val
, opt
, level
, &error_message
);
801 if (ret
< 0 || !error_message
.empty()) {
802 ceph_assert(!option_name
.empty());
804 *oss
<< "Parse error setting " << option_name
<< " to '"
805 << val
<< "' using injectargs";
806 if (!error_message
.empty()) {
807 *oss
<< " (" << error_message
<< ")";
811 cerr
<< "parse error setting '" << option_name
<< "' to '"
813 if (!error_message
.empty()) {
814 cerr
<< " (" << error_message
<< ")";
816 cerr
<< "\n" << std::endl
;
820 if (o
== schema
.size()) {
824 return ret
>= 0 ? 0 : ret
;
827 int md_config_t::parse_injectargs(ConfigValues
& values
,
828 const ConfigTracker
& tracker
,
829 std::vector
<const char*>& args
,
833 for (std::vector
<const char*>::iterator i
= args
.begin(); i
!= args
.end(); ) {
834 int r
= parse_option(values
, tracker
, args
, i
, oss
, CONF_OVERRIDE
);
841 void md_config_t::set_safe_to_start_threads()
843 safe_to_start_threads
= true;
846 void md_config_t::_clear_safe_to_start_threads()
848 safe_to_start_threads
= false;
851 int md_config_t::injectargs(ConfigValues
& values
,
852 const ConfigTracker
& tracker
,
853 const std::string
& s
, std::ostream
*oss
)
856 char b
[s
.length()+1];
857 strcpy(b
, s
.c_str());
858 std::vector
<const char*> nargs
;
862 while (*p
&& *p
!= ' ') p
++;
866 while (*p
&& *p
== ' ') p
++;
868 ret
= parse_injectargs(values
, tracker
, nargs
, oss
);
869 if (!nargs
.empty()) {
870 *oss
<< " failed to parse arguments: ";
872 for (std::vector
<const char*>::const_iterator i
= nargs
.begin();
873 i
!= nargs
.end(); ++i
) {
874 *oss
<< prefix
<< *i
;
880 update_legacy_vals(values
);
884 void md_config_t::set_val_or_die(ConfigValues
& values
,
885 const ConfigTracker
& tracker
,
886 const std::string_view key
,
887 const std::string
&val
)
889 std::stringstream err
;
890 int ret
= set_val(values
, tracker
, key
, val
, &err
);
892 std::cerr
<< "set_val_or_die(" << key
<< "): " << err
.str();
894 ceph_assert(ret
== 0);
897 int md_config_t::set_val(ConfigValues
& values
,
898 const ConfigTracker
& tracker
,
899 const std::string_view key
, const char *val
,
900 std::stringstream
*err_ss
)
903 if (err_ss
) *err_ss
<< "No key specified";
912 string
k(ConfFile::normalize_key_name(key
));
914 const auto &opt_iter
= schema
.find(k
);
915 if (opt_iter
!= schema
.end()) {
916 const Option
&opt
= opt_iter
->second
;
917 std::string error_message
;
918 int r
= _set_val(values
, tracker
, v
, opt
, CONF_OVERRIDE
, &error_message
);
920 if (err_ss
) *err_ss
<< "Set " << opt
.name
<< " to " << v
;
923 if (err_ss
) *err_ss
<< error_message
;
928 if (err_ss
) *err_ss
<< "Configuration option not found: '" << key
<< "'";
932 int md_config_t::rm_val(ConfigValues
& values
, const std::string_view key
)
934 return _rm_val(values
, key
, CONF_OVERRIDE
);
937 void md_config_t::get_defaults_bl(const ConfigValues
& values
,
940 if (defaults_bl
.length() == 0) {
943 for (const auto &i
: schema
) {
945 encode(i
.second
.name
, bl
);
946 auto [value
, found
] = values
.get_value(i
.second
.name
, CONF_DEFAULT
);
948 encode(Option::to_str(value
), bl
);
951 conf_stringify(_get_val_default(i
.second
), &val
);
955 encode(n
, defaults_bl
);
956 defaults_bl
.claim_append(bl
);
961 void md_config_t::get_config_bl(
962 const ConfigValues
& values
,
963 uint64_t have_version
,
965 uint64_t *got_version
)
967 if (values_bl
.length() == 0) {
970 values
.for_each([&](auto& name
, auto& configs
) {
971 if (name
== "fsid" ||
977 encode((uint32_t)configs
.size(), bl
);
978 for (auto& j
: configs
) {
980 encode(Option::to_str(j
.second
), bl
);
983 // make sure overridden items appear, and include the default value
984 for (auto& i
: ignored_mon_values
) {
985 if (values
.contains(i
.first
)) {
988 if (i
.first
== "fsid" ||
992 const Option
*opt
= find_option(i
.first
);
998 encode((uint32_t)1, bl
);
999 encode((int32_t)CONF_DEFAULT
, bl
);
1001 conf_stringify(_get_val_default(*opt
), &val
);
1004 encode(n
, values_bl
);
1005 values_bl
.claim_append(bl
);
1006 encode(ignored_mon_values
, values_bl
);
1007 ++values_bl_version
;
1009 if (have_version
!= values_bl_version
) {
1011 *got_version
= values_bl_version
;
1015 int md_config_t::get_val(const ConfigValues
& values
,
1016 const std::string_view key
, char **buf
, int len
) const
1018 string
k(ConfFile::normalize_key_name(key
));
1019 return _get_val_cstr(values
, k
, buf
, len
);
1022 int md_config_t::get_val(
1023 const ConfigValues
& values
,
1024 const std::string_view key
,
1025 std::string
*val
) const
1027 return conf_stringify(get_val_generic(values
, key
), val
);
1030 Option::value_t
md_config_t::get_val_generic(
1031 const ConfigValues
& values
,
1032 const std::string_view key
) const
1034 return _get_val(values
, key
);
1037 Option::value_t
md_config_t::_get_val(
1038 const ConfigValues
& values
,
1039 const std::string_view key
,
1040 expand_stack_t
*stack
,
1041 std::ostream
*err
) const
1044 return Option::value_t(boost::blank());
1047 // In key names, leading and trailing whitespace are not significant.
1048 string
k(ConfFile::normalize_key_name(key
));
1050 const Option
*o
= find_option(k
);
1052 // not a valid config option
1053 return Option::value_t(boost::blank());
1056 return _get_val(values
, *o
, stack
, err
);
1059 Option::value_t
md_config_t::_get_val(
1060 const ConfigValues
& values
,
1062 expand_stack_t
*stack
,
1063 std::ostream
*err
) const
1065 expand_stack_t a_stack
;
1069 return _expand_meta(values
,
1070 _get_val_nometa(values
, o
),
1074 Option::value_t
md_config_t::_get_val_nometa(const ConfigValues
& values
,
1075 const Option
& o
) const
1077 if (auto [value
, found
] = values
.get_value(o
.name
, -1); found
) {
1080 return _get_val_default(o
);
1084 const Option::value_t
& md_config_t::_get_val_default(const Option
& o
) const
1086 bool has_daemon_default
= !boost::get
<boost::blank
>(&o
.daemon_value
);
1087 if (is_daemon
&& has_daemon_default
) {
1088 return o
.daemon_value
;
1094 void md_config_t::early_expand_meta(
1095 const ConfigValues
& values
,
1097 std::ostream
*err
) const
1099 expand_stack_t stack
;
1100 Option::value_t v
= _expand_meta(values
,
1101 Option::value_t(val
),
1102 nullptr, &stack
, err
);
1103 conf_stringify(v
, &val
);
1106 bool md_config_t::finalize_reexpand_meta(ConfigValues
& values
,
1107 const ConfigTracker
& tracker
)
1109 for (auto& [name
, value
] : may_reexpand_meta
) {
1110 set_val(values
, tracker
, name
, value
);
1113 if (!may_reexpand_meta
.empty()) {
1114 // meta expands could have modified anything. Copy it all out again.
1115 update_legacy_vals(values
);
1122 Option::value_t
md_config_t::_expand_meta(
1123 const ConfigValues
& values
,
1124 const Option::value_t
& in
,
1126 expand_stack_t
*stack
,
1127 std::ostream
*err
) const
1129 //cout << __func__ << " in '" << in << "' stack " << stack << std::endl;
1133 const std::string
*str
= boost::get
<const std::string
>(&in
);
1139 auto pos
= str
->find('$');
1140 if (pos
== std::string::npos
) {
1141 // no substitutions!
1146 stack
->push_back(make_pair(o
, &in
));
1149 decltype(pos
) last_pos
= 0;
1150 while (pos
!= std::string::npos
) {
1151 ceph_assert((*str
)[pos
] == '$');
1152 if (pos
> last_pos
) {
1153 out
+= str
->substr(last_pos
, pos
- last_pos
);
1156 // try to parse the variable name into var, either \$\{(.+)\} or
1158 const char *valid_chars
= "abcdefghijklmnopqrstuvwxyz_";
1161 if ((*str
)[pos
+1] == '{') {
1163 endpos
= str
->find_first_not_of(valid_chars
, pos
+ 2);
1164 if (endpos
!= std::string::npos
&&
1165 (*str
)[endpos
] == '}') {
1166 var
= str
->substr(pos
+ 2, endpos
- pos
- 2);
1171 endpos
= str
->find_first_not_of(valid_chars
, pos
+ 1);
1172 if (endpos
!= std::string::npos
)
1173 var
= str
->substr(pos
+ 1, endpos
- pos
- 1);
1175 var
= str
->substr(pos
+ 1);
1182 //cout << " found var " << var << std::endl;
1183 // special metavariable?
1184 if (var
== "type") {
1185 out
+= values
.name
.get_type_name();
1186 } else if (var
== "cluster") {
1187 out
+= values
.cluster
;
1188 } else if (var
== "name") {
1189 out
+= values
.name
.to_cstr();
1190 } else if (var
== "host") {
1191 if (values
.host
== "") {
1192 out
+= ceph_get_short_hostname();
1196 } else if (var
== "num") {
1197 out
+= values
.name
.get_id().c_str();
1198 } else if (var
== "id") {
1199 out
+= values
.name
.get_id();
1200 } else if (var
== "pid") {
1201 out
+= stringify(getpid());
1203 may_reexpand_meta
[o
->name
] = *str
;
1205 } else if (var
== "cctid") {
1206 out
+= stringify((unsigned long long)this);
1207 } else if (var
== "home") {
1208 const char *home
= getenv("HOME");
1209 out
= home
? std::string(home
) : std::string();
1211 if (var
== "data_dir") {
1212 var
= data_dir_option
;
1214 const Option
*o
= find_option(var
);
1216 out
+= str
->substr(pos
, endpos
- pos
);
1218 auto match
= std::find_if(
1219 stack
->begin(), stack
->end(),
1220 [o
](pair
<const Option
*,const Option::value_t
*>& item
) {
1221 return item
.first
== o
;
1223 if (match
!= stack
->end()) {
1224 // substitution loop; break the cycle
1226 *err
<< "variable expansion loop at " << var
<< "="
1227 << Option::to_str(*match
->second
) << "\n"
1228 << "expansion stack:\n";
1229 for (auto i
= stack
->rbegin(); i
!= stack
->rend(); ++i
) {
1230 *err
<< i
->first
->name
<< "="
1231 << Option::to_str(*i
->second
) << "\n";
1234 return Option::value_t(std::string("$") + o
->name
);
1236 // recursively evaluate!
1238 conf_stringify(_get_val(values
, *o
, stack
, err
), &n
);
1244 pos
= str
->find('$', last_pos
);
1246 if (last_pos
!= std::string::npos
) {
1247 out
+= str
->substr(last_pos
);
1253 return Option::value_t(out
);
1256 int md_config_t::_get_val_cstr(
1257 const ConfigValues
& values
,
1258 const std::string
& key
, char **buf
, int len
) const
1264 if (conf_stringify(_get_val(values
, key
), &val
) == 0) {
1265 int l
= val
.length() + 1;
1267 *buf
= (char*)malloc(l
);
1270 strncpy(*buf
, val
.c_str(), l
);
1273 snprintf(*buf
, len
, "%s", val
.c_str());
1274 return (l
> len
) ? -ENAMETOOLONG
: 0;
1277 // couldn't find a configuration option with key 'k'
1281 void md_config_t::get_all_keys(std::vector
<std::string
> *keys
) const {
1282 const std::string
negative_flag_prefix("no_");
1285 keys
->reserve(schema
.size());
1286 for (const auto &i
: schema
) {
1287 const Option
&opt
= i
.second
;
1288 keys
->push_back(opt
.name
);
1289 if (opt
.type
== Option::TYPE_BOOL
) {
1290 keys
->push_back(negative_flag_prefix
+ opt
.name
);
1295 /* The order of the sections here is important. The first section in the
1296 * vector is the "highest priority" section; if we find it there, we'll stop
1297 * looking. The lowest priority section is the one we look in only if all
1298 * others had nothing. This should always be the global section.
1300 void md_config_t::get_my_sections(const ConfigValues
& values
,
1301 std::vector
<std::string
> §ions
) const
1303 _get_my_sections(values
, sections
);
1306 void md_config_t::_get_my_sections(const ConfigValues
& values
,
1307 std::vector
<std::string
> §ions
) const
1309 sections
.push_back(values
.name
.to_str());
1311 sections
.push_back(values
.name
.get_type_name());
1313 sections
.push_back("global");
1316 // Return a list of all sections
1317 int md_config_t::get_all_sections(std::vector
<std::string
> §ions
) const
1319 for (auto [section_name
, section
] : cf
) {
1320 sections
.push_back(section_name
);
1321 std::ignore
= section
;
1326 int md_config_t::get_val_from_conf_file(
1327 const ConfigValues
& values
,
1328 const std::vector
<std::string
> §ions
,
1329 const std::string_view key
,
1333 int r
= _get_val_from_conf_file(sections
, key
, out
);
1338 expand_stack_t stack
;
1339 auto v
= _expand_meta(values
, Option::value_t(out
), nullptr, &stack
, nullptr);
1340 conf_stringify(v
, &out
);
1345 int md_config_t::_get_val_from_conf_file(
1346 const std::vector
<std::string
> §ions
,
1347 const std::string_view key
,
1348 std::string
&out
) const
1350 for (auto &s
: sections
) {
1351 int ret
= cf
.read(s
.c_str(), std::string
{key
}, out
);
1354 } else if (ret
!= -ENOENT
) {
1361 int md_config_t::_set_val(
1362 ConfigValues
& values
,
1363 const ConfigTracker
& observers
,
1364 const std::string
&raw_val
,
1367 std::string
*error_message
)
1369 Option::value_t new_value
;
1370 ceph_assert(error_message
);
1371 int r
= opt
.parse_value(raw_val
, &new_value
, error_message
);
1376 // unsafe runtime change?
1377 if (!opt
.can_update_at_runtime() &&
1378 safe_to_start_threads
&&
1379 !observers
.is_tracking(opt
.name
)) {
1380 // accept value if it is not actually a change
1381 if (new_value
!= _get_val_nometa(values
, opt
)) {
1382 *error_message
= string("Configuration option '") + opt
.name
+
1383 "' may not be modified at runtime";
1388 // Apply the value to its entry in the `values` map
1389 auto result
= values
.set_value(opt
.name
, std::move(new_value
), level
);
1391 case ConfigValues::SET_NO_CHANGE
:
1393 case ConfigValues::SET_NO_EFFECT
:
1396 case ConfigValues::SET_HAVE_EFFECT
:
1398 _refresh(values
, opt
);
1404 void md_config_t::_refresh(ConfigValues
& values
, const Option
& opt
)
1406 // Apply the value to its legacy field, if it has one
1407 auto legacy_ptr_iter
= legacy_values
.find(std::string(opt
.name
));
1408 if (legacy_ptr_iter
!= legacy_values
.end()) {
1409 update_legacy_val(values
, opt
, legacy_ptr_iter
->second
);
1411 // Was this a debug_* option update?
1412 if (opt
.subsys
>= 0) {
1414 conf_stringify(_get_val(values
, opt
), &actual_val
);
1415 values
.set_logging(opt
.subsys
, actual_val
.c_str());
1417 // normal option, advertise the change.
1418 values
.changed
.insert(opt
.name
);
1422 int md_config_t::_rm_val(ConfigValues
& values
,
1423 const std::string_view key
,
1426 if (schema
.count(key
) == 0) {
1429 auto ret
= values
.rm_val(std::string
{key
}, level
);
1433 if (ret
== ConfigValues::SET_HAVE_EFFECT
) {
1434 _refresh(values
, *find_option(key
));
1441 template<typename Size
>
1442 struct get_size_visitor
: public boost::static_visitor
<Size
>
1444 get_size_visitor() {}
1446 template<typename T
>
1447 Size
operator()(const T
&) const {
1450 Size
operator()(const Option::size_t& sz
) const {
1451 return static_cast<Size
>(sz
.value
);
1453 Size
operator()(const Size
& v
) const {
1459 * Handles assigning from a variant-of-types to a variant-of-pointers-to-types
1461 template<class Config
>
1462 class assign_visitor
: public boost::static_visitor
<>
1465 Option::value_t val
;
1468 assign_visitor(Config
*conf_
, Option::value_t val_
)
1469 : conf(conf_
), val(val_
)
1472 template <typename T
>
1473 void operator()(T
Config::* ptr
) const
1475 T
*member
= const_cast<T
*>(&(conf
->*(boost::get
<const T
Config::*>(ptr
))));
1477 *member
= boost::get
<T
>(val
);
1479 void operator()(uint64_t Config::* ptr
) const
1482 auto member
= const_cast<T
*>(&(conf
->*(boost::get
<const T
Config::*>(ptr
))));
1483 *member
= boost::apply_visitor(get_size_visitor
<T
>{}, val
);
1485 void operator()(int64_t Config::* ptr
) const
1488 auto member
= const_cast<T
*>(&(conf
->*(boost::get
<const T
Config::*>(ptr
))));
1489 *member
= boost::apply_visitor(get_size_visitor
<T
>{}, val
);
1492 } // anonymous namespace
1494 void md_config_t::update_legacy_vals(ConfigValues
& values
)
1496 for (const auto &i
: legacy_values
) {
1497 const auto &name
= i
.first
;
1498 const auto &option
= schema
.at(name
);
1499 auto ptr
= i
.second
;
1500 update_legacy_val(values
, option
, ptr
);
1504 void md_config_t::update_legacy_val(ConfigValues
& values
,
1506 md_config_t::member_ptr_t member_ptr
)
1508 Option::value_t v
= _get_val(values
, opt
);
1509 boost::apply_visitor(assign_visitor(&values
, v
), member_ptr
);
1512 static void dump(Formatter
*f
, int level
, Option::value_t in
)
1514 if (const bool *v
= boost::get
<const bool>(&in
)) {
1515 f
->dump_bool(ceph_conf_level_name(level
), *v
);
1516 } else if (const int64_t *v
= boost::get
<const int64_t>(&in
)) {
1517 f
->dump_int(ceph_conf_level_name(level
), *v
);
1518 } else if (const uint64_t *v
= boost::get
<const uint64_t>(&in
)) {
1519 f
->dump_unsigned(ceph_conf_level_name(level
), *v
);
1520 } else if (const double *v
= boost::get
<const double>(&in
)) {
1521 f
->dump_float(ceph_conf_level_name(level
), *v
);
1523 f
->dump_stream(ceph_conf_level_name(level
)) << Option::to_str(in
);
1527 void md_config_t::diff(
1528 const ConfigValues
& values
,
1532 values
.for_each([this, f
, &values
] (auto& name
, auto& configs
) {
1533 if (configs
.empty()) {
1536 f
->open_object_section(std::string
{name
}.c_str());
1537 const Option
*o
= find_option(name
);
1538 if (configs
.size() &&
1539 configs
.begin()->first
!= CONF_DEFAULT
) {
1540 // show compiled-in default only if an override default wasn't provided
1541 dump(f
, CONF_DEFAULT
, _get_val_default(*o
));
1543 for (auto& j
: configs
) {
1544 dump(f
, j
.first
, j
.second
);
1546 dump(f
, CONF_FINAL
, _get_val(values
, *o
));
1551 void md_config_t::complain_about_parse_error(CephContext
*cct
)
1553 ::complain_about_parse_error(cct
, parse_error
);