From 98eb401025381ae5ff4b8921db39c0e44e66a1af Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Tue, 4 Aug 2020 14:27:35 +0800 Subject: [PATCH] crimson/admin: move config hooks into admin_socket.cc they are not specific to OSD, let's move them into admin_socket.cc Signed-off-by: Kefu Chai --- src/crimson/admin/admin_socket.cc | 101 ++++++++++++++++++++++++++++++ src/crimson/admin/osd_admin.cc | 99 ----------------------------- src/crimson/admin/osd_admin.h | 3 - src/crimson/osd/osd.cc | 3 - 4 files changed, 101 insertions(+), 105 deletions(-) diff --git a/src/crimson/admin/admin_socket.cc b/src/crimson/admin/admin_socket.cc index 07244fed84b49..21d97b8093b18 100644 --- a/src/crimson/admin/admin_socket.cc +++ b/src/crimson/admin/admin_socket.cc @@ -415,6 +415,104 @@ public: } }; +/** + * listing the configuration values + */ +class ConfigShowHook : public AdminSocketHook { +public: + explicit ConfigShowHook() : + AdminSocketHook("config show", + "config show", + "dump current config settings") + {} + seastar::future call(const cmdmap_t&, + std::string_view format, + ceph::bufferlist&& input) const final + { + unique_ptr f{Formatter::create(format, "json-pretty", "json-pretty")}; + f->open_object_section("config_show"); + local_conf().show_config(f.get()); + f->close_section(); + return seastar::make_ready_future(f.get()); + } +}; + +/** + * fetching the value of a specific configuration item + */ +class ConfigGetHook : public AdminSocketHook { +public: + ConfigGetHook() : + AdminSocketHook("config get", + "config get name=var,type=CephString", + "config get : get the config value") + {} + seastar::future call(const cmdmap_t& cmdmap, + std::string_view format, + ceph::bufferlist&& input) const final + { + std::string var; + if (!cmd_getval(cmdmap, "var", var)) { + // should have been caught by 'validate()' + return seastar::make_ready_future( + tell_result_t{-EINVAL, "syntax error: 'config get '"}); + } + try { + unique_ptr f{Formatter::create(format, + "json-pretty", + "json-pretty")}; + f->open_object_section("config_get"); + std::string conf_val = + local_conf().get_val(var); + f->dump_string(var.c_str(), conf_val.c_str()); + f->close_section(); + return seastar::make_ready_future(f.get()); + } catch (const boost::bad_get&) { + return seastar::make_ready_future( + tell_result_t{-EINVAL, fmt::format("unrecognized option {}", var)}); + } + } +}; + +/** + * setting the value of a specific configuration item (an example: + * {"prefix": "config set", "var":"debug_osd", "val": ["30/20"]} ) + */ +class ConfigSetHook : public AdminSocketHook { +public: + ConfigSetHook() + : AdminSocketHook("config set", + "config set" + " name=var,type=CephString" + " name=val,type=CephString,n=N", + "config set [ ...]: set a config variable") + {} + seastar::future call(const cmdmap_t& cmdmap, + std::string_view format, + ceph::bufferlist&&) const final + { + std::string var; + std::vector new_val; + if (!cmd_getval(cmdmap, "var", var) || + !cmd_getval(cmdmap, "val", new_val)) { + return seastar::make_ready_future( + tell_result_t{-EINVAL, "syntax error: 'config set '"}); + } + // val may be multiple words + const std::string joined_values = boost::algorithm::join(new_val, " "); + return local_conf().set_val(var, joined_values).then([format] { + unique_ptr f{Formatter::create(format, "json-pretty", "json-pretty")}; + f->open_object_section("config_set"); + f->dump_string("success", ""); + f->close_section(); + return seastar::make_ready_future(f.get()); + }).handle_exception_type([](std::invalid_argument& e) { + return seastar::make_ready_future( + tell_result_t{-EINVAL, e.what()}); + }); + } +}; + /// the hooks that are served directly by the admin_socket server seastar::future<> AdminSocket::register_admin_commands() { @@ -423,6 +521,9 @@ seastar::future<> AdminSocket::register_admin_commands() register_command(std::make_unique()), register_command(std::make_unique(*this)), register_command(std::make_unique(*this)), + register_command(std::make_unique()), + register_command(std::make_unique()), + register_command(std::make_unique()), register_command(std::make_unique())); } diff --git a/src/crimson/admin/osd_admin.cc b/src/crimson/admin/osd_admin.cc index 4675d8d2230c8..e4f5f401df4f0 100644 --- a/src/crimson/admin/osd_admin.cc +++ b/src/crimson/admin/osd_admin.cc @@ -79,105 +79,6 @@ private: template std::unique_ptr make_asok_hook(crimson::osd::OSD& osd); -/** - * A CephContext admin hook: listing the configuration values - */ -class ConfigShowHook : public AdminSocketHook { -public: - explicit ConfigShowHook() : - AdminSocketHook("config show", - "config show", - "dump current config settings") - {} - seastar::future call(const cmdmap_t&, - std::string_view format, - ceph::bufferlist&& input) const final - { - unique_ptr f{Formatter::create(format, "json-pretty", "json-pretty")}; - f->open_object_section("config_show"); - local_conf().show_config(f.get()); - f->close_section(); - return seastar::make_ready_future(f.get()); - } -}; -template std::unique_ptr make_asok_hook(); - -/** - * A CephContext admin hook: fetching the value of a specific - * configuration item - */ -class ConfigGetHook : public AdminSocketHook { -public: - ConfigGetHook() : - AdminSocketHook("config get", - "config get name=var,type=CephString", - "config get : get the config value") - {} - seastar::future call(const cmdmap_t& cmdmap, - std::string_view format, - ceph::bufferlist&& input) const final - { - std::string var; - if (!cmd_getval(cmdmap, "var", var)) { - // should have been caught by 'validate()' - return seastar::make_ready_future( - tell_result_t{-EINVAL, "syntax error: 'config get '"}); - } - try { - unique_ptr f{Formatter::create(format, "json-pretty", "json-pretty")}; - f->open_object_section("config_get"); - std::string conf_val = - local_conf().get_val(var); - f->dump_string(var.c_str(), conf_val.c_str()); - f->close_section(); - return seastar::make_ready_future(f.get()); - } catch (const boost::bad_get&) { - return seastar::make_ready_future( - tell_result_t{-EINVAL, fmt::format("unrecognized option {}", var)}); - } - } -}; -template std::unique_ptr make_asok_hook(); - -/** - * A CephContext admin hook: setting the value of a specific configuration - * item (a real example: {"prefix": "config set", "var":"debug_osd", "val": - * ["30/20"]} ) - */ -class ConfigSetHook : public AdminSocketHook { -public: - ConfigSetHook() : - AdminSocketHook("config set", - "config set name=var,type=CephString name=val,type=CephString,n=N", - "config set [ ...]: set a config variable") - {} - seastar::future call(const cmdmap_t& cmdmap, - std::string_view format, - ceph::bufferlist&&) const final - { - std::string var; - std::vector new_val; - if (!cmd_getval(cmdmap, "var", var) || - !cmd_getval(cmdmap, "val", new_val)) { - return seastar::make_ready_future( - tell_result_t{-EINVAL, "syntax error: 'config set '"}); - } - // val may be multiple words - string valstr = str_join(new_val, " "); - return local_conf().set_val(var, valstr).then([format] { - unique_ptr f{Formatter::create(format, "json-pretty", "json-pretty")}; - f->open_object_section("config_set"); - f->dump_string("success", ""); - f->close_section(); - return seastar::make_ready_future(f.get()); - }).handle_exception_type([](std::invalid_argument& e) { - return seastar::make_ready_future( - tell_result_t{-EINVAL, e.what()}); - }); - } -}; -template std::unique_ptr make_asok_hook(); - /** * send the latest pg stats to mgr */ diff --git a/src/crimson/admin/osd_admin.h b/src/crimson/admin/osd_admin.h index 6a0b8b230ba70..3f750fbcc019f 100644 --- a/src/crimson/admin/osd_admin.h +++ b/src/crimson/admin/osd_admin.h @@ -9,9 +9,6 @@ namespace crimson::admin { class AssertAlwaysHook; -class ConfigShowHook; -class ConfigGetHook; -class ConfigSetHook; class FlushPgStatsHook; class OsdStatusHook; class SendBeaconHook; diff --git a/src/crimson/osd/osd.cc b/src/crimson/osd/osd.cc index 87409c242f7c9..8454780f4ca1d 100644 --- a/src/crimson/osd/osd.cc +++ b/src/crimson/osd/osd.cc @@ -429,9 +429,6 @@ seastar::future<> OSD::start_asok_admin() asok->register_admin_commands(), asok->register_command(make_asok_hook(*this)), asok->register_command(make_asok_hook(*this)), - asok->register_command(make_asok_hook()), - asok->register_command(make_asok_hook()), - asok->register_command(make_asok_hook()), asok->register_command(make_asok_hook(*this))); }); } -- 2.39.5