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
, "fuse_debug", "true");
663 set_val_or_die(values
, tracker
, "daemonize", "false");
664 set_val_or_die(values
, tracker
, "log_file", "");
665 set_val_or_die(values
, tracker
, "log_to_stderr", "true");
666 set_val_or_die(values
, tracker
, "err_to_stderr", "true");
667 set_val_or_die(values
, tracker
, "log_to_syslog", "false");
669 // Some stuff that we wanted to give universal single-character options for
670 // Careful: you can burn through the alphabet pretty quickly by adding
672 else if (ceph_argparse_witharg(args
, i
, &val
, "--monmap", "-M", (char*)NULL
)) {
673 set_val_or_die(values
, tracker
, "monmap", val
.c_str());
675 else if (ceph_argparse_witharg(args
, i
, &val
, "--mon_host", "-m", (char*)NULL
)) {
676 set_val_or_die(values
, tracker
, "mon_host", val
.c_str());
678 else if (ceph_argparse_witharg(args
, i
, &val
, "--bind", (char*)NULL
)) {
679 set_val_or_die(values
, tracker
, "public_addr", val
.c_str());
681 else if (ceph_argparse_witharg(args
, i
, &val
, "--keyfile", "-K", (char*)NULL
)) {
686 r
= bl
.read_fd(STDIN_FILENO
, 1024);
688 r
= bl
.read_file(val
.c_str(), &err
);
691 string
k(bl
.c_str(), bl
.length());
692 set_val_or_die(values
, tracker
, "key", k
.c_str());
695 else if (ceph_argparse_witharg(args
, i
, &val
, "--keyring", "-k", (char*)NULL
)) {
696 set_val_or_die(values
, tracker
, "keyring", val
.c_str());
698 else if (ceph_argparse_witharg(args
, i
, &val
, "--client_mountpoint", "-r", (char*)NULL
)) {
699 set_val_or_die(values
, tracker
, "client_mountpoint", val
.c_str());
702 int r
= parse_option(values
, tracker
, args
, i
, NULL
, level
);
708 // meta expands could have modified anything. Copy it all out again.
709 update_legacy_vals(values
);
713 void md_config_t::do_argv_commands(const ConfigValues
& values
) const
716 if (do_show_config
) {
717 _show_config(values
, &cout
, NULL
);
721 if (do_show_config_value
.size()) {
723 int r
= conf_stringify(_get_val(values
, do_show_config_value
, 0, &cerr
),
727 std::cerr
<< "failed to get config option '"
728 << do_show_config_value
<< "': option not found" << std::endl
;
730 std::cerr
<< "failed to get config option '"
731 << do_show_config_value
<< "': " << cpp_strerror(r
)
735 std::cout
<< val
<< std::endl
;
740 int md_config_t::parse_option(ConfigValues
& values
,
741 const ConfigTracker
& tracker
,
742 std::vector
<const char*>& args
,
743 std::vector
<const char*>::iterator
& i
,
751 std::string option_name
;
752 std::string error_message
;
754 for (const auto& opt_iter
: schema
) {
755 const Option
&opt
= opt_iter
.second
;
757 std::string
as_option("--");
758 as_option
+= opt
.name
;
759 option_name
= opt
.name
;
760 if (ceph_argparse_witharg(
762 string(string("--default-") + opt
.name
).c_str(), (char*)NULL
)) {
763 if (!err
.str().empty()) {
764 error_message
= err
.str();
768 ret
= _set_val(values
, tracker
, val
, opt
, CONF_DEFAULT
, &error_message
);
770 } else if (opt
.type
== Option::TYPE_BOOL
) {
772 if (ceph_argparse_binary_flag(args
, i
, &res
, oss
, as_option
.c_str(),
775 ret
= _set_val(values
, tracker
, "false", opt
, level
, &error_message
);
777 ret
= _set_val(values
, tracker
, "true", opt
, level
, &error_message
);
782 std::string
no("--no-");
784 if (ceph_argparse_flag(args
, i
, no
.c_str(), (char*)NULL
)) {
785 ret
= _set_val(values
, tracker
, "false", opt
, level
, &error_message
);
789 } else if (ceph_argparse_witharg(args
, i
, &val
, err
,
790 as_option
.c_str(), (char*)NULL
)) {
791 if (!err
.str().empty()) {
792 error_message
= err
.str();
796 ret
= _set_val(values
, tracker
, val
, opt
, level
, &error_message
);
802 if (ret
< 0 || !error_message
.empty()) {
803 ceph_assert(!option_name
.empty());
805 *oss
<< "Parse error setting " << option_name
<< " to '"
806 << val
<< "' using injectargs";
807 if (!error_message
.empty()) {
808 *oss
<< " (" << error_message
<< ")";
812 cerr
<< "parse error setting '" << option_name
<< "' to '"
814 if (!error_message
.empty()) {
815 cerr
<< " (" << error_message
<< ")";
817 cerr
<< "\n" << std::endl
;
821 if (o
== schema
.size()) {
825 return ret
>= 0 ? 0 : ret
;
828 int md_config_t::parse_injectargs(ConfigValues
& values
,
829 const ConfigTracker
& tracker
,
830 std::vector
<const char*>& args
,
834 for (std::vector
<const char*>::iterator i
= args
.begin(); i
!= args
.end(); ) {
835 int r
= parse_option(values
, tracker
, args
, i
, oss
, CONF_OVERRIDE
);
842 void md_config_t::set_safe_to_start_threads()
844 safe_to_start_threads
= true;
847 void md_config_t::_clear_safe_to_start_threads()
849 safe_to_start_threads
= false;
852 int md_config_t::injectargs(ConfigValues
& values
,
853 const ConfigTracker
& tracker
,
854 const std::string
& s
, std::ostream
*oss
)
857 char b
[s
.length()+1];
858 strcpy(b
, s
.c_str());
859 std::vector
<const char*> nargs
;
863 while (*p
&& *p
!= ' ') p
++;
867 while (*p
&& *p
== ' ') p
++;
869 ret
= parse_injectargs(values
, tracker
, nargs
, oss
);
870 if (!nargs
.empty()) {
871 *oss
<< " failed to parse arguments: ";
873 for (std::vector
<const char*>::const_iterator i
= nargs
.begin();
874 i
!= nargs
.end(); ++i
) {
875 *oss
<< prefix
<< *i
;
881 update_legacy_vals(values
);
885 void md_config_t::set_val_or_die(ConfigValues
& values
,
886 const ConfigTracker
& tracker
,
887 const std::string_view key
,
888 const std::string
&val
)
890 std::stringstream err
;
891 int ret
= set_val(values
, tracker
, key
, val
, &err
);
893 std::cerr
<< "set_val_or_die(" << key
<< "): " << err
.str();
895 ceph_assert(ret
== 0);
898 int md_config_t::set_val(ConfigValues
& values
,
899 const ConfigTracker
& tracker
,
900 const std::string_view key
, const char *val
,
901 std::stringstream
*err_ss
)
904 if (err_ss
) *err_ss
<< "No key specified";
913 string
k(ConfFile::normalize_key_name(key
));
915 const auto &opt_iter
= schema
.find(k
);
916 if (opt_iter
!= schema
.end()) {
917 const Option
&opt
= opt_iter
->second
;
918 std::string error_message
;
919 int r
= _set_val(values
, tracker
, v
, opt
, CONF_OVERRIDE
, &error_message
);
921 if (err_ss
) *err_ss
<< "Set " << opt
.name
<< " to " << v
;
924 if (err_ss
) *err_ss
<< error_message
;
929 if (err_ss
) *err_ss
<< "Configuration option not found: '" << key
<< "'";
933 int md_config_t::rm_val(ConfigValues
& values
, const std::string_view key
)
935 return _rm_val(values
, key
, CONF_OVERRIDE
);
938 void md_config_t::get_defaults_bl(const ConfigValues
& values
,
941 if (defaults_bl
.length() == 0) {
944 for (const auto &i
: schema
) {
946 encode(i
.second
.name
, bl
);
947 auto [value
, found
] = values
.get_value(i
.second
.name
, CONF_DEFAULT
);
949 encode(Option::to_str(value
), bl
);
952 conf_stringify(_get_val_default(i
.second
), &val
);
956 encode(n
, defaults_bl
);
957 defaults_bl
.claim_append(bl
);
962 void md_config_t::get_config_bl(
963 const ConfigValues
& values
,
964 uint64_t have_version
,
966 uint64_t *got_version
)
968 if (values_bl
.length() == 0) {
971 values
.for_each([&](auto& name
, auto& configs
) {
972 if (name
== "fsid" ||
978 encode((uint32_t)configs
.size(), bl
);
979 for (auto& j
: configs
) {
981 encode(Option::to_str(j
.second
), bl
);
984 // make sure overridden items appear, and include the default value
985 for (auto& i
: ignored_mon_values
) {
986 if (values
.contains(i
.first
)) {
989 if (i
.first
== "fsid" ||
993 const Option
*opt
= find_option(i
.first
);
999 encode((uint32_t)1, bl
);
1000 encode((int32_t)CONF_DEFAULT
, bl
);
1002 conf_stringify(_get_val_default(*opt
), &val
);
1005 encode(n
, values_bl
);
1006 values_bl
.claim_append(bl
);
1007 encode(ignored_mon_values
, values_bl
);
1008 ++values_bl_version
;
1010 if (have_version
!= values_bl_version
) {
1012 *got_version
= values_bl_version
;
1016 int md_config_t::get_val(const ConfigValues
& values
,
1017 const std::string_view key
, char **buf
, int len
) const
1019 string
k(ConfFile::normalize_key_name(key
));
1020 return _get_val_cstr(values
, k
, buf
, len
);
1023 int md_config_t::get_val(
1024 const ConfigValues
& values
,
1025 const std::string_view key
,
1026 std::string
*val
) const
1028 return conf_stringify(get_val_generic(values
, key
), val
);
1031 Option::value_t
md_config_t::get_val_generic(
1032 const ConfigValues
& values
,
1033 const std::string_view key
) const
1035 return _get_val(values
, key
);
1038 Option::value_t
md_config_t::_get_val(
1039 const ConfigValues
& values
,
1040 const std::string_view key
,
1041 expand_stack_t
*stack
,
1042 std::ostream
*err
) const
1045 return Option::value_t(boost::blank());
1048 // In key names, leading and trailing whitespace are not significant.
1049 string
k(ConfFile::normalize_key_name(key
));
1051 const Option
*o
= find_option(k
);
1053 // not a valid config option
1054 return Option::value_t(boost::blank());
1057 return _get_val(values
, *o
, stack
, err
);
1060 Option::value_t
md_config_t::_get_val(
1061 const ConfigValues
& values
,
1063 expand_stack_t
*stack
,
1064 std::ostream
*err
) const
1066 expand_stack_t a_stack
;
1070 return _expand_meta(values
,
1071 _get_val_nometa(values
, o
),
1075 Option::value_t
md_config_t::_get_val_nometa(const ConfigValues
& values
,
1076 const Option
& o
) const
1078 if (auto [value
, found
] = values
.get_value(o
.name
, -1); found
) {
1081 return _get_val_default(o
);
1085 const Option::value_t
& md_config_t::_get_val_default(const Option
& o
) const
1087 bool has_daemon_default
= !boost::get
<boost::blank
>(&o
.daemon_value
);
1088 if (is_daemon
&& has_daemon_default
) {
1089 return o
.daemon_value
;
1095 void md_config_t::early_expand_meta(
1096 const ConfigValues
& values
,
1098 std::ostream
*err
) const
1100 expand_stack_t stack
;
1101 Option::value_t v
= _expand_meta(values
,
1102 Option::value_t(val
),
1103 nullptr, &stack
, err
);
1104 conf_stringify(v
, &val
);
1107 bool md_config_t::finalize_reexpand_meta(ConfigValues
& values
,
1108 const ConfigTracker
& tracker
)
1110 for (auto& [name
, value
] : may_reexpand_meta
) {
1111 set_val(values
, tracker
, name
, value
);
1114 if (!may_reexpand_meta
.empty()) {
1115 // meta expands could have modified anything. Copy it all out again.
1116 update_legacy_vals(values
);
1123 Option::value_t
md_config_t::_expand_meta(
1124 const ConfigValues
& values
,
1125 const Option::value_t
& in
,
1127 expand_stack_t
*stack
,
1128 std::ostream
*err
) const
1130 //cout << __func__ << " in '" << in << "' stack " << stack << std::endl;
1134 const std::string
*str
= boost::get
<const std::string
>(&in
);
1140 auto pos
= str
->find('$');
1141 if (pos
== std::string::npos
) {
1142 // no substitutions!
1147 stack
->push_back(make_pair(o
, &in
));
1150 decltype(pos
) last_pos
= 0;
1151 while (pos
!= std::string::npos
) {
1152 ceph_assert((*str
)[pos
] == '$');
1153 if (pos
> last_pos
) {
1154 out
+= str
->substr(last_pos
, pos
- last_pos
);
1157 // try to parse the variable name into var, either \$\{(.+)\} or
1159 const char *valid_chars
= "abcdefghijklmnopqrstuvwxyz_";
1162 if ((*str
)[pos
+1] == '{') {
1164 endpos
= str
->find_first_not_of(valid_chars
, pos
+ 2);
1165 if (endpos
!= std::string::npos
&&
1166 (*str
)[endpos
] == '}') {
1167 var
= str
->substr(pos
+ 2, endpos
- pos
- 2);
1172 endpos
= str
->find_first_not_of(valid_chars
, pos
+ 1);
1173 if (endpos
!= std::string::npos
)
1174 var
= str
->substr(pos
+ 1, endpos
- pos
- 1);
1176 var
= str
->substr(pos
+ 1);
1183 //cout << " found var " << var << std::endl;
1184 // special metavariable?
1185 if (var
== "type") {
1186 out
+= values
.name
.get_type_name();
1187 } else if (var
== "cluster") {
1188 out
+= values
.cluster
;
1189 } else if (var
== "name") {
1190 out
+= values
.name
.to_cstr();
1191 } else if (var
== "host") {
1192 if (values
.host
== "") {
1193 out
+= ceph_get_short_hostname();
1197 } else if (var
== "num") {
1198 out
+= values
.name
.get_id().c_str();
1199 } else if (var
== "id") {
1200 out
+= values
.name
.get_id();
1201 } else if (var
== "pid") {
1202 out
+= stringify(getpid());
1204 may_reexpand_meta
[o
->name
] = *str
;
1206 } else if (var
== "cctid") {
1207 out
+= stringify((unsigned long long)this);
1208 } else if (var
== "home") {
1209 const char *home
= getenv("HOME");
1210 out
= home
? std::string(home
) : std::string();
1212 if (var
== "data_dir") {
1213 var
= data_dir_option
;
1215 const Option
*o
= find_option(var
);
1217 out
+= str
->substr(pos
, endpos
- pos
);
1219 auto match
= std::find_if(
1220 stack
->begin(), stack
->end(),
1221 [o
](pair
<const Option
*,const Option::value_t
*>& item
) {
1222 return item
.first
== o
;
1224 if (match
!= stack
->end()) {
1225 // substitution loop; break the cycle
1227 *err
<< "variable expansion loop at " << var
<< "="
1228 << Option::to_str(*match
->second
) << "\n"
1229 << "expansion stack:\n";
1230 for (auto i
= stack
->rbegin(); i
!= stack
->rend(); ++i
) {
1231 *err
<< i
->first
->name
<< "="
1232 << Option::to_str(*i
->second
) << "\n";
1235 return Option::value_t(std::string("$") + o
->name
);
1237 // recursively evaluate!
1239 conf_stringify(_get_val(values
, *o
, stack
, err
), &n
);
1245 pos
= str
->find('$', last_pos
);
1247 if (last_pos
!= std::string::npos
) {
1248 out
+= str
->substr(last_pos
);
1254 return Option::value_t(out
);
1257 int md_config_t::_get_val_cstr(
1258 const ConfigValues
& values
,
1259 const std::string
& key
, char **buf
, int len
) const
1265 if (conf_stringify(_get_val(values
, key
), &val
) == 0) {
1266 int l
= val
.length() + 1;
1268 *buf
= (char*)malloc(l
);
1271 strncpy(*buf
, val
.c_str(), l
);
1274 snprintf(*buf
, len
, "%s", val
.c_str());
1275 return (l
> len
) ? -ENAMETOOLONG
: 0;
1278 // couldn't find a configuration option with key 'k'
1282 void md_config_t::get_all_keys(std::vector
<std::string
> *keys
) const {
1283 const std::string
negative_flag_prefix("no_");
1286 keys
->reserve(schema
.size());
1287 for (const auto &i
: schema
) {
1288 const Option
&opt
= i
.second
;
1289 keys
->push_back(opt
.name
);
1290 if (opt
.type
== Option::TYPE_BOOL
) {
1291 keys
->push_back(negative_flag_prefix
+ opt
.name
);
1296 /* The order of the sections here is important. The first section in the
1297 * vector is the "highest priority" section; if we find it there, we'll stop
1298 * looking. The lowest priority section is the one we look in only if all
1299 * others had nothing. This should always be the global section.
1301 void md_config_t::get_my_sections(const ConfigValues
& values
,
1302 std::vector
<std::string
> §ions
) const
1304 _get_my_sections(values
, sections
);
1307 void md_config_t::_get_my_sections(const ConfigValues
& values
,
1308 std::vector
<std::string
> §ions
) const
1310 sections
.push_back(values
.name
.to_str());
1312 sections
.push_back(values
.name
.get_type_name());
1314 sections
.push_back("global");
1317 // Return a list of all sections
1318 int md_config_t::get_all_sections(std::vector
<std::string
> §ions
) const
1320 for (auto [section_name
, section
] : cf
) {
1321 sections
.push_back(section_name
);
1322 std::ignore
= section
;
1327 int md_config_t::get_val_from_conf_file(
1328 const ConfigValues
& values
,
1329 const std::vector
<std::string
> §ions
,
1330 const std::string_view key
,
1334 int r
= _get_val_from_conf_file(sections
, key
, out
);
1339 expand_stack_t stack
;
1340 auto v
= _expand_meta(values
, Option::value_t(out
), nullptr, &stack
, nullptr);
1341 conf_stringify(v
, &out
);
1346 int md_config_t::_get_val_from_conf_file(
1347 const std::vector
<std::string
> §ions
,
1348 const std::string_view key
,
1349 std::string
&out
) const
1351 for (auto &s
: sections
) {
1352 int ret
= cf
.read(s
.c_str(), std::string
{key
}, out
);
1355 } else if (ret
!= -ENOENT
) {
1362 int md_config_t::_set_val(
1363 ConfigValues
& values
,
1364 const ConfigTracker
& observers
,
1365 const std::string
&raw_val
,
1368 std::string
*error_message
)
1370 Option::value_t new_value
;
1371 ceph_assert(error_message
);
1372 int r
= opt
.parse_value(raw_val
, &new_value
, error_message
);
1377 // unsafe runtime change?
1378 if (!opt
.can_update_at_runtime() &&
1379 safe_to_start_threads
&&
1380 !observers
.is_tracking(opt
.name
)) {
1381 // accept value if it is not actually a change
1382 if (new_value
!= _get_val_nometa(values
, opt
)) {
1383 *error_message
= string("Configuration option '") + opt
.name
+
1384 "' may not be modified at runtime";
1389 // Apply the value to its entry in the `values` map
1390 auto result
= values
.set_value(opt
.name
, std::move(new_value
), level
);
1392 case ConfigValues::SET_NO_CHANGE
:
1394 case ConfigValues::SET_NO_EFFECT
:
1397 case ConfigValues::SET_HAVE_EFFECT
:
1399 _refresh(values
, opt
);
1405 void md_config_t::_refresh(ConfigValues
& values
, const Option
& opt
)
1407 // Apply the value to its legacy field, if it has one
1408 auto legacy_ptr_iter
= legacy_values
.find(std::string(opt
.name
));
1409 if (legacy_ptr_iter
!= legacy_values
.end()) {
1410 update_legacy_val(values
, opt
, legacy_ptr_iter
->second
);
1412 // Was this a debug_* option update?
1413 if (opt
.subsys
>= 0) {
1415 conf_stringify(_get_val(values
, opt
), &actual_val
);
1416 values
.set_logging(opt
.subsys
, actual_val
.c_str());
1418 // normal option, advertise the change.
1419 values
.changed
.insert(opt
.name
);
1423 int md_config_t::_rm_val(ConfigValues
& values
,
1424 const std::string_view key
,
1427 if (schema
.count(key
) == 0) {
1430 auto ret
= values
.rm_val(std::string
{key
}, level
);
1434 if (ret
== ConfigValues::SET_HAVE_EFFECT
) {
1435 _refresh(values
, *find_option(key
));
1442 template<typename Size
>
1443 struct get_size_visitor
: public boost::static_visitor
<Size
>
1445 get_size_visitor() {}
1447 template<typename T
>
1448 Size
operator()(const T
&) const {
1451 Size
operator()(const Option::size_t& sz
) const {
1452 return static_cast<Size
>(sz
.value
);
1454 Size
operator()(const Size
& v
) const {
1460 * Handles assigning from a variant-of-types to a variant-of-pointers-to-types
1462 template<class Config
>
1463 class assign_visitor
: public boost::static_visitor
<>
1466 Option::value_t val
;
1469 assign_visitor(Config
*conf_
, Option::value_t val_
)
1470 : conf(conf_
), val(val_
)
1473 template <typename T
>
1474 void operator()(T
Config::* ptr
) const
1476 T
*member
= const_cast<T
*>(&(conf
->*(boost::get
<const T
Config::*>(ptr
))));
1478 *member
= boost::get
<T
>(val
);
1480 void operator()(uint64_t Config::* ptr
) const
1483 auto member
= const_cast<T
*>(&(conf
->*(boost::get
<const T
Config::*>(ptr
))));
1484 *member
= boost::apply_visitor(get_size_visitor
<T
>{}, val
);
1486 void operator()(int64_t Config::* ptr
) const
1489 auto member
= const_cast<T
*>(&(conf
->*(boost::get
<const T
Config::*>(ptr
))));
1490 *member
= boost::apply_visitor(get_size_visitor
<T
>{}, val
);
1493 } // anonymous namespace
1495 void md_config_t::update_legacy_vals(ConfigValues
& values
)
1497 for (const auto &i
: legacy_values
) {
1498 const auto &name
= i
.first
;
1499 const auto &option
= schema
.at(name
);
1500 auto ptr
= i
.second
;
1501 update_legacy_val(values
, option
, ptr
);
1505 void md_config_t::update_legacy_val(ConfigValues
& values
,
1507 md_config_t::member_ptr_t member_ptr
)
1509 Option::value_t v
= _get_val(values
, opt
);
1510 boost::apply_visitor(assign_visitor(&values
, v
), member_ptr
);
1513 static void dump(Formatter
*f
, int level
, Option::value_t in
)
1515 if (const bool *v
= boost::get
<const bool>(&in
)) {
1516 f
->dump_bool(ceph_conf_level_name(level
), *v
);
1517 } else if (const int64_t *v
= boost::get
<const int64_t>(&in
)) {
1518 f
->dump_int(ceph_conf_level_name(level
), *v
);
1519 } else if (const uint64_t *v
= boost::get
<const uint64_t>(&in
)) {
1520 f
->dump_unsigned(ceph_conf_level_name(level
), *v
);
1521 } else if (const double *v
= boost::get
<const double>(&in
)) {
1522 f
->dump_float(ceph_conf_level_name(level
), *v
);
1524 f
->dump_stream(ceph_conf_level_name(level
)) << Option::to_str(in
);
1528 void md_config_t::diff(
1529 const ConfigValues
& values
,
1533 values
.for_each([this, f
, &values
] (auto& name
, auto& configs
) {
1534 if (configs
.empty()) {
1537 f
->open_object_section(std::string
{name
}.c_str());
1538 const Option
*o
= find_option(name
);
1539 if (configs
.size() &&
1540 configs
.begin()->first
!= CONF_DEFAULT
) {
1541 // show compiled-in default only if an override default wasn't provided
1542 dump(f
, CONF_DEFAULT
, _get_val_default(*o
));
1544 for (auto& j
: configs
) {
1545 dump(f
, j
.first
, j
.second
);
1547 dump(f
, CONF_FINAL
, _get_val(values
, *o
));
1552 void md_config_t::complain_about_parse_error(CephContext
*cct
)
1554 ::complain_about_parse_error(cct
, parse_error
);