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.
16 #include "common/ceph_argparse.h"
17 #include "common/common_init.h"
18 #include "common/config.h"
19 #include "common/config_obs.h"
20 #include "include/str_list.h"
21 #include "include/stringify.h"
22 #include "osd/osd_types.h"
23 #include "common/errno.h"
24 #include "common/hostname.h"
25 #include "common/dout.h"
27 /* Don't use standard Ceph logging in this file.
28 * We can't use logging until it's initialized, and a lot of the necessary
29 * initialization happens here.
37 #define dout_subsys ceph_subsys_monc
39 namespace fs
= std::filesystem
;
47 using std::ostringstream
;
50 using std::string_view
;
53 using ceph::bufferlist
;
56 using ceph::Formatter
;
58 static const char *CEPH_CONF_FILE_DEFAULT
= "$data_dir/config,/etc/ceph/$cluster.conf,$home/.ceph/$cluster.conf,$cluster.conf"
59 #if defined(__FreeBSD__)
60 ",/usr/local/etc/ceph/$cluster.conf"
62 ",$programdata/ceph/$cluster.conf"
67 #define STRINGIFY(x) _STR(x)
69 const char *ceph_conf_level_name(int level
)
72 case CONF_DEFAULT
: return "default"; // built-in default
73 case CONF_MON
: return "mon"; // monitor config database
74 case CONF_ENV
: return "env"; // process environment (CEPH_ARGS)
75 case CONF_FILE
: return "file"; // ceph.conf file
76 case CONF_CMDLINE
: return "cmdline"; // process command line args
77 case CONF_OVERRIDE
: return "override"; // injectargs or 'config set' at runtime
78 case CONF_FINAL
: return "final";
79 default: return "???";
83 int ceph_resolve_file_search(const std::string
& filename_list
,
87 get_str_list(filename_list
, ";,", ls
);
90 list
<string
>::iterator iter
;
91 for (iter
= ls
.begin(); iter
!= ls
.end(); ++iter
) {
92 int fd
= ::open(iter
->c_str(), O_RDONLY
|O_CLOEXEC
);
105 static int conf_stringify(const Option::value_t
& v
, string
*out
)
107 if (v
== Option::value_t
{}) {
110 *out
= Option::to_str(v
);
114 md_config_t::md_config_t(ConfigValues
& values
,
115 const ConfigTracker
& tracker
,
117 : is_daemon(is_daemon
)
119 // Load the compile-time list of Option into
120 // a map so that we can resolve keys quickly.
121 for (const auto &i
: ceph_options
) {
122 if (schema
.count(i
.name
)) {
123 // We may be instantiated pre-logging so send
124 std::cerr
<< "Duplicate config key in schema: '" << i
.name
<< "'"
128 schema
.emplace(i
.name
, i
);
131 // Define the debug_* options as well.
132 subsys_options
.reserve(values
.subsys
.get_num());
133 for (unsigned i
= 0; i
< values
.subsys
.get_num(); ++i
) {
134 string name
= string("debug_") + values
.subsys
.get_name(i
);
135 subsys_options
.push_back(
136 Option(name
, Option::TYPE_STR
, Option::LEVEL_ADVANCED
));
137 Option
& opt
= subsys_options
.back();
138 opt
.set_default(stringify(values
.subsys
.get_log_level(i
)) + "/" +
139 stringify(values
.subsys
.get_gather_level(i
)));
140 string desc
= string("Debug level for ") + values
.subsys
.get_name(i
);
141 opt
.set_description(desc
.c_str());
142 opt
.set_flag(Option::FLAG_RUNTIME
);
143 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.");
145 opt
.set_validator([](std::string
*value
, std::string
*error_message
) {
147 int r
= sscanf(value
->c_str(), "%d/%d", &m
, &n
);
149 if (m
< 0 || m
> 99) {
150 *error_message
= "value must be in range [0, 99]";
154 if (n
< 0 || n
> 99) {
155 *error_message
= "value must be in range [0, 99]";
161 *value
= stringify(m
) + "/" + stringify(n
);
164 *error_message
= "value must take the form N or N/M, where N and M are integers";
170 for (auto& opt
: subsys_options
) {
171 schema
.emplace(opt
.name
, opt
);
174 // Populate list of legacy_values according to the OPTION() definitions
175 // Note that this is just setting up our map of name->member ptr. The
176 // default values etc will get loaded in along with new-style data,
177 // as all loads write to both the values map, and the legacy
178 // members if present.
180 #define OPTION(name, type) \
181 {STRINGIFY(name), &ConfigValues::name},
182 #define SAFE_OPTION(name, type) OPTION(name, type)
183 #include "options/legacy_config_opts.h"
190 // Validate default values from the schema
191 for (const auto &i
: schema
) {
192 const Option
&opt
= i
.second
;
193 if (opt
.type
== Option::TYPE_STR
) {
194 bool has_daemon_default
= (opt
.daemon_value
!= Option::value_t
{});
195 Option::value_t default_val
;
196 if (is_daemon
&& has_daemon_default
) {
197 default_val
= opt
.daemon_value
;
199 default_val
= opt
.value
;
201 // We call pre_validate as a sanity check, but also to get any
202 // side effect (value modification) from the validator.
203 auto* def_str
= std::get_if
<std::string
>(&default_val
);
204 std::string val
= *def_str
;
206 if (opt
.pre_validate(&val
, &err
) != 0) {
207 std::cerr
<< "Default value " << opt
.name
<< "=" << *def_str
<< " is "
208 "invalid: " << err
<< std::endl
;
210 // This is the compiled-in default that is failing its own option's
211 // validation, so this is super-invalid and should never make it
212 // past a pull request: crash out.
215 if (val
!= *def_str
) {
216 // if the validator normalizes the string into a different form than
217 // what was compiled in, use that.
218 set_val_default(values
, tracker
, opt
.name
, val
);
223 // Copy out values (defaults) into any legacy (C struct member) fields
224 update_legacy_vals(values
);
227 md_config_t::~md_config_t()
232 * Sanity check schema. Assert out on failures, to ensure any bad changes
233 * cannot possibly pass any testing and make it into a release.
235 void md_config_t::validate_schema()
237 for (const auto &i
: schema
) {
238 const auto &opt
= i
.second
;
239 for (const auto &see_also_key
: opt
.see_also
) {
240 if (schema
.count(see_also_key
) == 0) {
241 std::cerr
<< "Non-existent see-also key '" << see_also_key
242 << "' on option '" << opt
.name
<< "'" << std::endl
;
248 for (const auto &i
: legacy_values
) {
249 if (schema
.count(i
.first
) == 0) {
250 std::cerr
<< "Schema is missing legacy field '" << i
.first
<< "'"
257 const Option
*md_config_t::find_option(const std::string_view name
) const
259 auto p
= schema
.find(name
);
260 if (p
!= schema
.end()) {
266 void md_config_t::set_val_default(ConfigValues
& values
,
267 const ConfigTracker
& tracker
,
268 const string_view name
, const std::string
& val
)
270 const Option
*o
= find_option(name
);
273 int r
= _set_val(values
, tracker
, val
, *o
, CONF_DEFAULT
, &err
);
277 int md_config_t::set_mon_vals(CephContext
*cct
,
278 ConfigValues
& values
,
279 const ConfigTracker
& tracker
,
280 const map
<string
,string
,less
<>>& kv
,
281 config_callback config_cb
)
283 ignored_mon_values
.clear();
286 ldout(cct
, 4) << __func__
<< " no callback set" << dendl
;
291 if (config_cb(i
.first
, i
.second
)) {
292 ldout(cct
, 4) << __func__
<< " callback consumed " << i
.first
<< dendl
;
295 ldout(cct
, 4) << __func__
<< " callback ignored " << i
.first
<< dendl
;
297 const Option
*o
= find_option(i
.first
);
299 ldout(cct
,10) << __func__
<< " " << i
.first
<< " = " << i
.second
300 << " (unrecognized option)" << dendl
;
303 if (o
->has_flag(Option::FLAG_NO_MON_UPDATE
)) {
304 ignored_mon_values
.emplace(i
);
308 int r
= _set_val(values
, tracker
, i
.second
, *o
, CONF_MON
, &err
);
310 ldout(cct
, 4) << __func__
<< " failed to set " << i
.first
<< " = "
311 << i
.second
<< ": " << err
<< dendl
;
312 ignored_mon_values
.emplace(i
);
313 } else if (r
== ConfigValues::SET_NO_CHANGE
||
314 r
== ConfigValues::SET_NO_EFFECT
) {
315 ldout(cct
,20) << __func__
<< " " << i
.first
<< " = " << i
.second
316 << " (no change)" << dendl
;
317 } else if (r
== ConfigValues::SET_HAVE_EFFECT
) {
318 ldout(cct
,10) << __func__
<< " " << i
.first
<< " = " << i
.second
<< dendl
;
323 values
.for_each([&] (auto name
, auto configs
) {
324 auto config
= configs
.find(CONF_MON
);
325 if (config
== configs
.end()) {
328 if (kv
.find(name
) != kv
.end()) {
331 ldout(cct
,10) << __func__
<< " " << name
332 << " cleared (was " << Option::to_str(config
->second
) << ")"
334 values
.rm_val(name
, CONF_MON
);
335 // if this is a debug option, it needs to propagate to teh subsys;
336 // this isn't covered by update_legacy_vals() below. similarly,
337 // we want to trigger a config notification for these items.
338 const Option
*o
= find_option(name
);
339 _refresh(values
, *o
);
342 update_legacy_vals(values
);
346 int md_config_t::parse_config_files(ConfigValues
& values
,
347 const ConfigTracker
& tracker
,
348 const char *conf_files_str
,
349 std::ostream
*warnings
,
352 if (safe_to_start_threads
)
355 if (values
.cluster
.empty() && !conf_files_str
) {
356 values
.cluster
= get_cluster_name(nullptr);
359 for (auto& fn
: get_conffile_paths(values
, conf_files_str
, warnings
, flags
)) {
362 if (bl
.read_file(fn
.c_str(), &error
)) {
367 int ret
= parse_buffer(values
, tracker
, bl
.c_str(), bl
.length(), &oss
);
373 parse_error
= oss
.str();
374 if (ret
!= -ENOENT
) {
378 // it must have been all ENOENTs, that's the only way we got here
379 if (conf_path
.empty()) {
382 if (values
.cluster
.empty()) {
383 values
.cluster
= get_cluster_name(conf_path
.c_str());
385 update_legacy_vals(values
);
390 md_config_t::parse_buffer(ConfigValues
& values
,
391 const ConfigTracker
& tracker
,
392 const char* buf
, size_t len
,
393 std::ostream
* warnings
)
395 if (!cf
.parse_buffer(string_view
{buf
, len
}, warnings
)) {
398 const auto my_sections
= get_my_sections(values
);
399 for (const auto &i
: schema
) {
400 const auto &opt
= i
.second
;
402 if (_get_val_from_conf_file(my_sections
, opt
.name
, val
)) {
405 std::string error_message
;
406 if (_set_val(values
, tracker
, val
, opt
, CONF_FILE
, &error_message
) < 0) {
407 if (warnings
!= nullptr) {
408 *warnings
<< "parse error setting " << std::quoted(opt
.name
)
409 << " to " << std::quoted(val
);
410 if (!error_message
.empty()) {
411 *warnings
<< " (" << error_message
<< ")";
417 cf
.check_old_style_section_names({"mds", "mon", "osd"}, cerr
);
421 std::list
<std::string
>
422 md_config_t::get_conffile_paths(const ConfigValues
& values
,
423 const char *conf_files_str
,
424 std::ostream
*warnings
,
427 if (!conf_files_str
) {
428 const char *c
= getenv("CEPH_CONF");
432 if (flags
& CINIT_FLAG_NO_DEFAULT_CONFIG_FILE
)
434 conf_files_str
= CEPH_CONF_FILE_DEFAULT
;
438 std::list
<std::string
> paths
;
439 get_str_list(conf_files_str
, ";,", paths
);
440 for (auto i
= paths
.begin(); i
!= paths
.end(); ) {
442 if (path
.find("$data_dir") != path
.npos
&&
443 data_dir_option
.empty()) {
444 // useless $data_dir item, skip
447 early_expand_meta(values
, path
, warnings
);
454 std::string
md_config_t::get_cluster_name(const char* conffile
)
457 // If cluster name is not set yet, use the prefix of the
458 // basename of configuration file as cluster name.
459 if (fs::path path
{conffile
}; path
.extension() == ".conf") {
460 return path
.stem().string();
462 // If the configuration file does not follow $cluster.conf
463 // convention, we do the last try and assign the cluster to
468 // set the cluster name to 'ceph' when configuration file is not specified.
473 void md_config_t::parse_env(unsigned entity_type
,
474 ConfigValues
& values
,
475 const ConfigTracker
& tracker
,
476 const char *args_var
)
478 if (safe_to_start_threads
)
481 args_var
= "CEPH_ARGS";
483 if (auto s
= getenv("CEPH_KEYRING"); s
) {
485 _set_val(values
, tracker
, s
, *find_option("keyring"), CONF_ENV
, &err
);
487 if (auto dir
= getenv("CEPH_LIB"); dir
) {
488 for (auto name
: { "erasure_code_dir", "plugin_dir", "osd_class_dir" }) {
490 const Option
*o
= find_option(name
);
492 _set_val(values
, tracker
, dir
, *o
, CONF_ENV
, &err
);
496 // Apply pod memory limits:
498 // There are two types of resource requests: `limits` and `requests`.
500 // - Requests: Used by the K8s scheduler to determine on which nodes to
501 // schedule the pods. This helps spread the pods to different nodes. This
502 // value should be conservative in order to make sure all the pods are
503 // schedulable. This corresponds to POD_MEMORY_REQUEST (set by the Rook
504 // CRD) and is the target memory utilization we try to maintain for daemons
507 // If POD_MEMORY_REQUEST is present, we use it as the target.
509 // - Limits: At runtime, the container runtime (and Linux) will use the
510 // limits to see if the pod is using too many resources. In that case, the
511 // pod will be killed/restarted automatically if the pod goes over the limit.
512 // This should be higher than what is specified for requests (potentially
513 // much higher). This corresponds to the cgroup memory limit that will
514 // trigger the Linux OOM killer.
516 // If POD_MEMORY_LIMIT is present, we use it as the /default/ value for
517 // the target, which means it will only apply if the *_memory_target option
518 // isn't set via some other path (e.g., POD_MEMORY_REQUEST, or the cluster
519 // config, or whatever.)
521 // Here are the documented best practices:
522 // https://kubernetes.io/docs/tasks/configure-pod-container/assign-cpu-resource/#motivation-for-cpu-requests-and-limits
524 // When the operator creates the CephCluster CR, it will need to generate the
525 // desired requests and limits. As long as we are conservative in our choice
526 // for requests and generous with the limits we should be in a good place to
529 // The support in Rook is already there for applying the limits as seen in
532 // Rook docs on the resource requests and limits:
533 // https://rook.io/docs/rook/v1.0/ceph-cluster-crd.html#cluster-wide-resources-configuration-settings
534 // Example CR settings:
535 // https://github.com/rook/rook/blob/6d2ef936698593036185aabcb00d1d74f9c7bfc1/cluster/examples/kubernetes/ceph/cluster.yaml#L90
537 uint64_t pod_limit
= 0, pod_request
= 0;
538 if (auto pod_lim
= getenv("POD_MEMORY_LIMIT"); pod_lim
) {
540 uint64_t v
= atoll(pod_lim
);
542 switch (entity_type
) {
543 case CEPH_ENTITY_TYPE_OSD
:
545 double cgroup_ratio
= get_val
<double>(
546 values
, "osd_memory_target_cgroup_limit_ratio");
547 if (cgroup_ratio
> 0.0) {
548 pod_limit
= v
* cgroup_ratio
;
549 // set osd_memory_target *default* based on cgroup limit, so that
550 // it can be overridden by any explicit settings elsewhere.
551 set_val_default(values
, tracker
,
552 "osd_memory_target", stringify(pod_limit
));
558 if (auto pod_req
= getenv("POD_MEMORY_REQUEST"); pod_req
) {
559 if (uint64_t v
= atoll(pod_req
); v
) {
563 if (pod_request
&& pod_limit
) {
564 // If both LIMIT and REQUEST are set, ensure that we use the
565 // min of request and limit*ratio. This is important
566 // because k8s set set LIMIT == REQUEST if only LIMIT is
567 // specified, and we want to apply the ratio in that case,
568 // even though REQUEST is present.
569 pod_request
= std::min
<uint64_t>(pod_request
, pod_limit
);
573 switch (entity_type
) {
574 case CEPH_ENTITY_TYPE_OSD
:
575 _set_val(values
, tracker
, stringify(pod_request
),
576 *find_option("osd_memory_target"),
582 if (getenv(args_var
)) {
583 vector
<const char *> env_args
;
584 env_to_vec(env_args
, args_var
);
585 parse_argv(values
, tracker
, env_args
, CONF_ENV
);
589 void md_config_t::show_config(const ConfigValues
& values
,
590 std::ostream
& out
) const
592 _show_config(values
, &out
, nullptr);
595 void md_config_t::show_config(const ConfigValues
& values
,
598 _show_config(values
, nullptr, f
);
601 void md_config_t::config_options(Formatter
*f
) const
603 f
->open_array_section("options");
604 for (const auto& i
: schema
) {
605 f
->dump_object("option", i
.second
);
610 void md_config_t::_show_config(const ConfigValues
& values
,
611 std::ostream
*out
, Formatter
*f
) const
614 *out
<< "name = " << values
.name
<< std::endl
;
615 *out
<< "cluster = " << values
.cluster
<< std::endl
;
618 f
->dump_string("name", stringify(values
.name
));
619 f
->dump_string("cluster", values
.cluster
);
621 for (const auto& i
: schema
) {
622 const Option
&opt
= i
.second
;
624 conf_stringify(_get_val(values
, opt
), &val
);
626 *out
<< opt
.name
<< " = " << val
<< std::endl
;
629 f
->dump_string(opt
.name
.c_str(), val
);
634 int md_config_t::parse_argv(ConfigValues
& values
,
635 const ConfigTracker
& tracker
,
636 std::vector
<const char*>& args
, int level
)
638 if (safe_to_start_threads
) {
642 // In this function, don't change any parts of the configuration directly.
643 // Instead, use set_val to set them. This will allow us to send the proper
644 // observer notifications later.
646 for (std::vector
<const char*>::iterator i
= args
.begin(); i
!= args
.end(); ) {
647 if (strcmp(*i
, "--") == 0) {
648 /* Normally we would use ceph_argparse_double_dash. However, in this
649 * function we *don't* want to remove the double dash, because later
650 * argument parses will still need to see it. */
653 else if (ceph_argparse_flag(args
, i
, "--show_conf", (char*)NULL
)) {
654 cerr
<< cf
<< std::endl
;
657 else if (ceph_argparse_flag(args
, i
, "--show_config", (char*)NULL
)) {
658 do_show_config
= true;
660 else if (ceph_argparse_witharg(args
, i
, &val
, "--show_config_value", (char*)NULL
)) {
661 do_show_config_value
= val
;
663 else if (ceph_argparse_flag(args
, i
, "--no-mon-config", (char*)NULL
)) {
664 values
.no_mon_config
= true;
666 else if (ceph_argparse_flag(args
, i
, "--mon-config", (char*)NULL
)) {
667 values
.no_mon_config
= false;
669 else if (ceph_argparse_flag(args
, i
, "--foreground", "-f", (char*)NULL
)) {
670 set_val_or_die(values
, tracker
, "daemonize", "false");
672 else if (ceph_argparse_flag(args
, i
, "-d", (char*)NULL
)) {
673 set_val_or_die(values
, tracker
, "fuse_debug", "true");
674 set_val_or_die(values
, tracker
, "daemonize", "false");
675 set_val_or_die(values
, tracker
, "log_file", "");
676 set_val_or_die(values
, tracker
, "log_to_stderr", "true");
677 set_val_or_die(values
, tracker
, "err_to_stderr", "true");
678 set_val_or_die(values
, tracker
, "log_to_syslog", "false");
680 // Some stuff that we wanted to give universal single-character options for
681 // Careful: you can burn through the alphabet pretty quickly by adding
683 else if (ceph_argparse_witharg(args
, i
, &val
, "--monmap", "-M", (char*)NULL
)) {
684 set_val_or_die(values
, tracker
, "monmap", val
.c_str());
686 else if (ceph_argparse_witharg(args
, i
, &val
, "--mon_host", "-m", (char*)NULL
)) {
687 set_val_or_die(values
, tracker
, "mon_host", val
.c_str());
689 else if (ceph_argparse_witharg(args
, i
, &val
, "--bind", (char*)NULL
)) {
690 set_val_or_die(values
, tracker
, "public_addr", val
.c_str());
692 else if (ceph_argparse_witharg(args
, i
, &val
, "--keyfile", "-K", (char*)NULL
)) {
697 r
= bl
.read_fd(STDIN_FILENO
, 1024);
699 r
= bl
.read_file(val
.c_str(), &err
);
702 string
k(bl
.c_str(), bl
.length());
703 set_val_or_die(values
, tracker
, "key", k
.c_str());
706 else if (ceph_argparse_witharg(args
, i
, &val
, "--keyring", "-k", (char*)NULL
)) {
707 set_val_or_die(values
, tracker
, "keyring", val
.c_str());
709 else if (ceph_argparse_witharg(args
, i
, &val
, "--client_mountpoint", "-r", (char*)NULL
)) {
710 set_val_or_die(values
, tracker
, "client_mountpoint", val
.c_str());
713 int r
= parse_option(values
, tracker
, args
, i
, NULL
, level
);
719 // meta expands could have modified anything. Copy it all out again.
720 update_legacy_vals(values
);
724 void md_config_t::do_argv_commands(const ConfigValues
& values
) const
727 if (do_show_config
) {
728 _show_config(values
, &cout
, NULL
);
732 if (do_show_config_value
.size()) {
734 int r
= conf_stringify(_get_val(values
, do_show_config_value
, 0, &cerr
),
738 std::cerr
<< "failed to get config option '"
739 << do_show_config_value
<< "': option not found" << std::endl
;
741 std::cerr
<< "failed to get config option '"
742 << do_show_config_value
<< "': " << cpp_strerror(r
)
746 std::cout
<< val
<< std::endl
;
751 int md_config_t::parse_option(ConfigValues
& values
,
752 const ConfigTracker
& tracker
,
753 std::vector
<const char*>& args
,
754 std::vector
<const char*>::iterator
& i
,
762 std::string option_name
;
763 std::string error_message
;
765 for (const auto& opt_iter
: schema
) {
766 const Option
&opt
= opt_iter
.second
;
768 std::string
as_option("--");
769 as_option
+= opt
.name
;
770 option_name
= opt
.name
;
771 if (ceph_argparse_witharg(
773 string(string("--default-") + opt
.name
).c_str(), (char*)NULL
)) {
774 if (!err
.str().empty()) {
775 error_message
= err
.str();
779 ret
= _set_val(values
, tracker
, val
, opt
, CONF_DEFAULT
, &error_message
);
781 } else if (opt
.type
== Option::TYPE_BOOL
) {
783 if (ceph_argparse_binary_flag(args
, i
, &res
, oss
, as_option
.c_str(),
786 ret
= _set_val(values
, tracker
, "false", opt
, level
, &error_message
);
788 ret
= _set_val(values
, tracker
, "true", opt
, level
, &error_message
);
793 std::string
no("--no-");
795 if (ceph_argparse_flag(args
, i
, no
.c_str(), (char*)NULL
)) {
796 ret
= _set_val(values
, tracker
, "false", opt
, level
, &error_message
);
800 } else if (ceph_argparse_witharg(args
, i
, &val
, err
,
801 as_option
.c_str(), (char*)NULL
)) {
802 if (!err
.str().empty()) {
803 error_message
= err
.str();
807 ret
= _set_val(values
, tracker
, val
, opt
, level
, &error_message
);
813 if (ret
< 0 || !error_message
.empty()) {
814 ceph_assert(!option_name
.empty());
816 *oss
<< "Parse error setting " << option_name
<< " to '"
817 << val
<< "' using injectargs";
818 if (!error_message
.empty()) {
819 *oss
<< " (" << error_message
<< ")";
823 cerr
<< "parse error setting '" << option_name
<< "' to '"
825 if (!error_message
.empty()) {
826 cerr
<< " (" << error_message
<< ")";
828 cerr
<< "\n" << std::endl
;
832 if (o
== schema
.size()) {
836 return ret
>= 0 ? 0 : ret
;
839 int md_config_t::parse_injectargs(ConfigValues
& values
,
840 const ConfigTracker
& tracker
,
841 std::vector
<const char*>& args
,
845 for (std::vector
<const char*>::iterator i
= args
.begin(); i
!= args
.end(); ) {
846 int r
= parse_option(values
, tracker
, args
, i
, oss
, CONF_OVERRIDE
);
853 void md_config_t::set_safe_to_start_threads()
855 safe_to_start_threads
= true;
858 void md_config_t::_clear_safe_to_start_threads()
860 safe_to_start_threads
= false;
863 int md_config_t::injectargs(ConfigValues
& values
,
864 const ConfigTracker
& tracker
,
865 const std::string
& s
, std::ostream
*oss
)
868 char b
[s
.length()+1];
869 strcpy(b
, s
.c_str());
870 std::vector
<const char*> nargs
;
874 while (*p
&& *p
!= ' ') p
++;
878 while (*p
&& *p
== ' ') p
++;
880 ret
= parse_injectargs(values
, tracker
, nargs
, oss
);
881 if (!nargs
.empty()) {
882 *oss
<< " failed to parse arguments: ";
884 for (std::vector
<const char*>::const_iterator i
= nargs
.begin();
885 i
!= nargs
.end(); ++i
) {
886 *oss
<< prefix
<< *i
;
892 update_legacy_vals(values
);
896 void md_config_t::set_val_or_die(ConfigValues
& values
,
897 const ConfigTracker
& tracker
,
898 const std::string_view key
,
899 const std::string
&val
)
901 std::stringstream err
;
902 int ret
= set_val(values
, tracker
, key
, val
, &err
);
904 std::cerr
<< "set_val_or_die(" << key
<< "): " << err
.str();
906 ceph_assert(ret
== 0);
909 int md_config_t::set_val(ConfigValues
& values
,
910 const ConfigTracker
& tracker
,
911 const std::string_view key
, const char *val
,
912 std::stringstream
*err_ss
)
915 if (err_ss
) *err_ss
<< "No key specified";
924 string
k(ConfFile::normalize_key_name(key
));
926 const auto &opt_iter
= schema
.find(k
);
927 if (opt_iter
!= schema
.end()) {
928 const Option
&opt
= opt_iter
->second
;
929 std::string error_message
;
930 int r
= _set_val(values
, tracker
, v
, opt
, CONF_OVERRIDE
, &error_message
);
932 if (err_ss
) *err_ss
<< "Set " << opt
.name
<< " to " << v
;
935 if (err_ss
) *err_ss
<< error_message
;
940 if (err_ss
) *err_ss
<< "Configuration option not found: '" << key
<< "'";
944 int md_config_t::rm_val(ConfigValues
& values
, const std::string_view key
)
946 return _rm_val(values
, key
, CONF_OVERRIDE
);
949 void md_config_t::get_defaults_bl(const ConfigValues
& values
,
952 if (defaults_bl
.length() == 0) {
955 for (const auto &i
: schema
) {
957 encode(i
.second
.name
, bl
);
958 auto [value
, found
] = values
.get_value(i
.second
.name
, CONF_DEFAULT
);
960 encode(Option::to_str(value
), bl
);
963 conf_stringify(_get_val_default(i
.second
), &val
);
967 encode(n
, defaults_bl
);
968 defaults_bl
.claim_append(bl
);
973 void md_config_t::get_config_bl(
974 const ConfigValues
& values
,
975 uint64_t have_version
,
977 uint64_t *got_version
)
979 if (values_bl
.length() == 0) {
982 values
.for_each([&](auto& name
, auto& configs
) {
983 if (name
== "fsid" ||
989 encode((uint32_t)configs
.size(), bl
);
990 for (auto& j
: configs
) {
992 encode(Option::to_str(j
.second
), bl
);
995 // make sure overridden items appear, and include the default value
996 for (auto& i
: ignored_mon_values
) {
997 if (values
.contains(i
.first
)) {
1000 if (i
.first
== "fsid" ||
1001 i
.first
== "host") {
1004 const Option
*opt
= find_option(i
.first
);
1009 encode(i
.first
, bl
);
1010 encode((uint32_t)1, bl
);
1011 encode((int32_t)CONF_DEFAULT
, bl
);
1013 conf_stringify(_get_val_default(*opt
), &val
);
1016 encode(n
, values_bl
);
1017 values_bl
.claim_append(bl
);
1018 encode(ignored_mon_values
, values_bl
);
1019 ++values_bl_version
;
1021 if (have_version
!= values_bl_version
) {
1023 *got_version
= values_bl_version
;
1027 std::optional
<std::string
> md_config_t::get_val_default(std::string_view key
)
1030 const Option
*opt
= find_option(key
);
1031 if (opt
&& (conf_stringify(_get_val_default(*opt
), &val
) == 0)) {
1032 return std::make_optional(std::move(val
));
1034 return std::nullopt
;
1037 int md_config_t::get_val(const ConfigValues
& values
,
1038 const std::string_view key
, char **buf
, int len
) const
1040 string
k(ConfFile::normalize_key_name(key
));
1041 return _get_val_cstr(values
, k
, buf
, len
);
1044 int md_config_t::get_val(
1045 const ConfigValues
& values
,
1046 const std::string_view key
,
1047 std::string
*val
) const
1049 return conf_stringify(get_val_generic(values
, key
), val
);
1052 Option::value_t
md_config_t::get_val_generic(
1053 const ConfigValues
& values
,
1054 const std::string_view key
) const
1056 return _get_val(values
, key
);
1059 Option::value_t
md_config_t::_get_val(
1060 const ConfigValues
& values
,
1061 const std::string_view key
,
1062 expand_stack_t
*stack
,
1063 std::ostream
*err
) const
1069 // In key names, leading and trailing whitespace are not significant.
1070 string
k(ConfFile::normalize_key_name(key
));
1072 const Option
*o
= find_option(k
);
1074 // not a valid config option
1078 return _get_val(values
, *o
, stack
, err
);
1081 Option::value_t
md_config_t::_get_val(
1082 const ConfigValues
& values
,
1084 expand_stack_t
*stack
,
1085 std::ostream
*err
) const
1087 expand_stack_t a_stack
;
1091 return _expand_meta(values
,
1092 _get_val_nometa(values
, o
),
1096 Option::value_t
md_config_t::_get_val_nometa(const ConfigValues
& values
,
1097 const Option
& o
) const
1099 if (auto [value
, found
] = values
.get_value(o
.name
, -1); found
) {
1102 return _get_val_default(o
);
1106 const Option::value_t
& md_config_t::_get_val_default(const Option
& o
) const
1108 bool has_daemon_default
= (o
.daemon_value
!= Option::value_t
{});
1109 if (is_daemon
&& has_daemon_default
) {
1110 return o
.daemon_value
;
1116 void md_config_t::early_expand_meta(
1117 const ConfigValues
& values
,
1119 std::ostream
*err
) const
1121 expand_stack_t stack
;
1122 Option::value_t v
= _expand_meta(values
,
1123 Option::value_t(val
),
1124 nullptr, &stack
, err
);
1125 conf_stringify(v
, &val
);
1128 bool md_config_t::finalize_reexpand_meta(ConfigValues
& values
,
1129 const ConfigTracker
& tracker
)
1131 std::vector
<std::string
> reexpands
;
1132 reexpands
.swap(may_reexpand_meta
);
1133 for (auto& name
: reexpands
) {
1134 // always refresh the options if they are in the may_reexpand_meta
1135 // map, because the options may have already been expanded with old
1137 const auto &opt_iter
= schema
.find(name
);
1138 ceph_assert(opt_iter
!= schema
.end());
1139 const Option
&opt
= opt_iter
->second
;
1140 _refresh(values
, opt
);
1143 return !may_reexpand_meta
.empty();
1146 Option::value_t
md_config_t::_expand_meta(
1147 const ConfigValues
& values
,
1148 const Option::value_t
& in
,
1150 expand_stack_t
*stack
,
1151 std::ostream
*err
) const
1153 //cout << __func__ << " in '" << in << "' stack " << stack << std::endl;
1157 const auto str
= std::get_if
<std::string
>(&in
);
1163 auto pos
= str
->find('$');
1164 if (pos
== std::string::npos
) {
1165 // no substitutions!
1170 stack
->push_back(make_pair(o
, &in
));
1173 decltype(pos
) last_pos
= 0;
1174 while (pos
!= std::string::npos
) {
1175 ceph_assert((*str
)[pos
] == '$');
1176 if (pos
> last_pos
) {
1177 out
+= str
->substr(last_pos
, pos
- last_pos
);
1180 // try to parse the variable name into var, either \$\{(.+)\} or
1182 const char *valid_chars
= "abcdefghijklmnopqrstuvwxyz_";
1185 if ((*str
)[pos
+1] == '{') {
1187 endpos
= str
->find_first_not_of(valid_chars
, pos
+ 2);
1188 if (endpos
!= std::string::npos
&&
1189 (*str
)[endpos
] == '}') {
1190 var
= str
->substr(pos
+ 2, endpos
- pos
- 2);
1195 endpos
= str
->find_first_not_of(valid_chars
, pos
+ 1);
1196 if (endpos
!= std::string::npos
)
1197 var
= str
->substr(pos
+ 1, endpos
- pos
- 1);
1199 var
= str
->substr(pos
+ 1);
1206 //cout << " found var " << var << std::endl;
1207 // special metavariable?
1208 if (var
== "type") {
1209 out
+= values
.name
.get_type_name();
1210 } else if (var
== "cluster") {
1211 out
+= values
.cluster
;
1212 } else if (var
== "name") {
1213 out
+= values
.name
.to_cstr();
1214 } else if (var
== "host") {
1215 if (values
.host
== "") {
1216 out
+= ceph_get_short_hostname();
1220 } else if (var
== "num") {
1221 out
+= values
.name
.get_id().c_str();
1222 } else if (var
== "id") {
1223 out
+= values
.name
.get_id();
1224 } else if (var
== "pid") {
1225 char *_pid
= getenv("PID");
1229 out
+= stringify(getpid());
1232 may_reexpand_meta
.push_back(o
->name
);
1234 } else if (var
== "cctid") {
1235 out
+= stringify((unsigned long long)this);
1236 } else if (var
== "home") {
1237 const char *home
= getenv("HOME");
1238 out
= home
? std::string(home
) : std::string();
1239 } else if (var
== "programdata") {
1240 const char *home
= getenv("ProgramData");
1241 out
= home
? std::string(home
) : std::string();
1243 if (var
== "data_dir") {
1244 var
= data_dir_option
;
1246 const Option
*o
= find_option(var
);
1248 out
+= str
->substr(pos
, endpos
- pos
);
1250 auto match
= std::find_if(
1251 stack
->begin(), stack
->end(),
1252 [o
](pair
<const Option
*,const Option::value_t
*>& item
) {
1253 return item
.first
== o
;
1255 if (match
!= stack
->end()) {
1256 // substitution loop; break the cycle
1258 *err
<< "variable expansion loop at " << var
<< "="
1259 << Option::to_str(*match
->second
) << "\n"
1260 << "expansion stack:\n";
1261 for (auto i
= stack
->rbegin(); i
!= stack
->rend(); ++i
) {
1262 *err
<< i
->first
->name
<< "="
1263 << Option::to_str(*i
->second
) << "\n";
1266 return Option::value_t(std::string("$") + o
->name
);
1268 // recursively evaluate!
1270 conf_stringify(_get_val(values
, *o
, stack
, err
), &n
);
1276 pos
= str
->find('$', last_pos
);
1278 if (last_pos
!= std::string::npos
) {
1279 out
+= str
->substr(last_pos
);
1285 return Option::value_t(out
);
1288 int md_config_t::_get_val_cstr(
1289 const ConfigValues
& values
,
1290 const std::string
& key
, char **buf
, int len
) const
1296 if (conf_stringify(_get_val(values
, key
), &val
) == 0) {
1297 int l
= val
.length() + 1;
1299 *buf
= (char*)malloc(l
);
1302 strncpy(*buf
, val
.c_str(), l
);
1305 snprintf(*buf
, len
, "%s", val
.c_str());
1306 return (l
> len
) ? -ENAMETOOLONG
: 0;
1309 // couldn't find a configuration option with key 'k'
1313 void md_config_t::get_all_keys(std::vector
<std::string
> *keys
) const {
1314 const std::string
negative_flag_prefix("no_");
1317 keys
->reserve(schema
.size());
1318 for (const auto &i
: schema
) {
1319 const Option
&opt
= i
.second
;
1320 keys
->push_back(opt
.name
);
1321 if (opt
.type
== Option::TYPE_BOOL
) {
1322 keys
->push_back(negative_flag_prefix
+ opt
.name
);
1327 /* The order of the sections here is important. The first section in the
1328 * vector is the "highest priority" section; if we find it there, we'll stop
1329 * looking. The lowest priority section is the one we look in only if all
1330 * others had nothing. This should always be the global section.
1332 std::vector
<std::string
>
1333 md_config_t::get_my_sections(const ConfigValues
& values
) const
1335 return {values
.name
.to_str(),
1336 values
.name
.get_type_name().data(),
1340 // Return a list of all sections
1341 int md_config_t::get_all_sections(std::vector
<std::string
> §ions
) const
1343 for (auto [section_name
, section
] : cf
) {
1344 sections
.push_back(section_name
);
1345 std::ignore
= section
;
1350 int md_config_t::get_val_from_conf_file(
1351 const ConfigValues
& values
,
1352 const std::vector
<std::string
> §ions
,
1353 const std::string_view key
,
1357 int r
= _get_val_from_conf_file(sections
, key
, out
);
1362 expand_stack_t stack
;
1363 auto v
= _expand_meta(values
, Option::value_t(out
), nullptr, &stack
, nullptr);
1364 conf_stringify(v
, &out
);
1369 int md_config_t::_get_val_from_conf_file(
1370 const std::vector
<std::string
> §ions
,
1371 const std::string_view key
,
1372 std::string
&out
) const
1374 for (auto &s
: sections
) {
1375 int ret
= cf
.read(s
, key
, out
);
1378 } else if (ret
!= -ENOENT
) {
1385 int md_config_t::_set_val(
1386 ConfigValues
& values
,
1387 const ConfigTracker
& observers
,
1388 const std::string
&raw_val
,
1391 std::string
*error_message
)
1393 Option::value_t new_value
;
1394 ceph_assert(error_message
);
1395 int r
= opt
.parse_value(raw_val
, &new_value
, error_message
);
1400 // unsafe runtime change?
1401 if (!opt
.can_update_at_runtime() &&
1402 safe_to_start_threads
&&
1403 !observers
.is_tracking(opt
.name
)) {
1404 // accept value if it is not actually a change
1405 if (new_value
!= _get_val_nometa(values
, opt
)) {
1406 *error_message
= string("Configuration option '") + opt
.name
+
1407 "' may not be modified at runtime";
1412 // Apply the value to its entry in the `values` map
1413 auto result
= values
.set_value(opt
.name
, std::move(new_value
), level
);
1415 case ConfigValues::SET_NO_CHANGE
:
1417 case ConfigValues::SET_NO_EFFECT
:
1420 case ConfigValues::SET_HAVE_EFFECT
:
1422 _refresh(values
, opt
);
1428 void md_config_t::_refresh(ConfigValues
& values
, const Option
& opt
)
1430 // Apply the value to its legacy field, if it has one
1431 auto legacy_ptr_iter
= legacy_values
.find(std::string(opt
.name
));
1432 if (legacy_ptr_iter
!= legacy_values
.end()) {
1433 update_legacy_val(values
, opt
, legacy_ptr_iter
->second
);
1435 // Was this a debug_* option update?
1436 if (opt
.subsys
>= 0) {
1438 conf_stringify(_get_val(values
, opt
), &actual_val
);
1439 values
.set_logging(opt
.subsys
, actual_val
.c_str());
1441 // normal option, advertise the change.
1442 values
.changed
.insert(opt
.name
);
1446 int md_config_t::_rm_val(ConfigValues
& values
,
1447 const std::string_view key
,
1450 if (schema
.count(key
) == 0) {
1453 auto ret
= values
.rm_val(std::string
{key
}, level
);
1457 if (ret
== ConfigValues::SET_HAVE_EFFECT
) {
1458 _refresh(values
, *find_option(key
));
1465 template<typename Size
>
1466 struct get_size_visitor
1468 get_size_visitor() {}
1470 template<typename T
>
1471 Size
operator()(const T
&) const {
1474 Size
operator()(const Option::size_t& sz
) const {
1475 return static_cast<Size
>(sz
.value
);
1477 Size
operator()(const Size
& v
) const {
1483 * Handles assigning from a variant-of-types to a variant-of-pointers-to-types
1485 class assign_visitor
1488 Option::value_t val
;
1491 assign_visitor(ConfigValues
*conf_
, Option::value_t val_
)
1492 : conf(conf_
), val(val_
)
1495 template <typename T
>
1496 void operator()(T
ConfigValues::* ptr
) const
1498 T
*member
= const_cast<T
*>(&(conf
->*(ptr
)));
1500 *member
= std::get
<T
>(val
);
1502 void operator()(uint64_t ConfigValues::* ptr
) const
1505 auto member
= const_cast<T
*>(&(conf
->*(ptr
)));
1506 *member
= std::visit(get_size_visitor
<T
>{}, val
);
1508 void operator()(int64_t ConfigValues::* ptr
) const
1511 auto member
= const_cast<T
*>(&(conf
->*(ptr
)));
1512 *member
= std::visit(get_size_visitor
<T
>{}, val
);
1515 } // anonymous namespace
1517 void md_config_t::update_legacy_vals(ConfigValues
& values
)
1519 for (const auto &i
: legacy_values
) {
1520 const auto &name
= i
.first
;
1521 const auto &option
= schema
.at(name
);
1522 auto ptr
= i
.second
;
1523 update_legacy_val(values
, option
, ptr
);
1527 void md_config_t::update_legacy_val(ConfigValues
& values
,
1529 md_config_t::member_ptr_t member_ptr
)
1531 Option::value_t v
= _get_val(values
, opt
);
1532 std::visit(assign_visitor(&values
, v
), member_ptr
);
1535 static void dump(Formatter
*f
, int level
, Option::value_t in
)
1537 if (const auto v
= std::get_if
<bool>(&in
)) {
1538 f
->dump_bool(ceph_conf_level_name(level
), *v
);
1539 } else if (const auto v
= std::get_if
<int64_t>(&in
)) {
1540 f
->dump_int(ceph_conf_level_name(level
), *v
);
1541 } else if (const auto v
= std::get_if
<uint64_t>(&in
)) {
1542 f
->dump_unsigned(ceph_conf_level_name(level
), *v
);
1543 } else if (const auto v
= std::get_if
<double>(&in
)) {
1544 f
->dump_float(ceph_conf_level_name(level
), *v
);
1546 f
->dump_stream(ceph_conf_level_name(level
)) << Option::to_str(in
);
1550 void md_config_t::diff(
1551 const ConfigValues
& values
,
1555 values
.for_each([this, f
, &values
] (auto& name
, auto& configs
) {
1556 if (configs
.empty()) {
1559 f
->open_object_section(std::string
{name
}.c_str());
1560 const Option
*o
= find_option(name
);
1561 if (configs
.size() &&
1562 configs
.begin()->first
!= CONF_DEFAULT
) {
1563 // show compiled-in default only if an override default wasn't provided
1564 dump(f
, CONF_DEFAULT
, _get_val_default(*o
));
1566 for (auto& j
: configs
) {
1567 dump(f
, j
.first
, j
.second
);
1569 dump(f
, CONF_FINAL
, _get_val(values
, *o
));
1574 void md_config_t::complain_about_parse_error(CephContext
*cct
)
1576 ::complain_about_parse_error(cct
, parse_error
);