From f27a5dc6155440f0add91213d68bb690d8bf9cd7 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Sat, 17 Nov 2018 07:15:19 -0600 Subject: [PATCH] mgr: call config_notify method when mgr's config has changed This provides a hook for the module to refresh any config values it has cached in module-local variables. Signed-off-by: Sage Weil --- src/mgr/ActivePyModule.cc | 12 +++++++++++- src/mgr/ActivePyModule.h | 1 + src/mgr/ActivePyModules.cc | 13 +++++++++++++ src/mgr/ActivePyModules.h | 2 ++ src/mgr/MgrStandby.cc | 3 +++ src/mgr/PyModuleRegistry.cc | 8 ++++++++ src/mgr/PyModuleRegistry.h | 1 + src/pybind/mgr/mgr_module.py | 8 ++++++++ 8 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/mgr/ActivePyModule.cc b/src/mgr/ActivePyModule.cc index d8a416bfde578..4d07222b7d7fe 100644 --- a/src/mgr/ActivePyModule.cc +++ b/src/mgr/ActivePyModule.cc @@ -155,7 +155,17 @@ PyObject *ActivePyModule::dispatch_remote( return remoteResult; } - +void ActivePyModule::config_notify() +{ + Gil gil(py_module->pMyThreadState, true); + dout(20) << "Calling " << py_module->get_name() << ".config_notify..." + << dendl; + auto remoteResult = PyObject_CallMethod(pClassInstance, "config_notify", + (char*)NULL); + if (remoteResult != nullptr) { + Py_DECREF(remoteResult); + } +} int ActivePyModule::handle_command( const cmdmap_t &cmdmap, diff --git a/src/mgr/ActivePyModule.h b/src/mgr/ActivePyModule.h index 149d297faf13e..c3780269ee047 100644 --- a/src/mgr/ActivePyModule.h +++ b/src/mgr/ActivePyModule.h @@ -76,6 +76,7 @@ public: return changed; } void get_health_checks(health_check_map_t *checks); + void config_notify(); void set_uri(const std::string &str) { diff --git a/src/mgr/ActivePyModules.cc b/src/mgr/ActivePyModules.cc index 81dbe30705ce5..e8fcf9fa725e0 100644 --- a/src/mgr/ActivePyModules.cc +++ b/src/mgr/ActivePyModules.cc @@ -880,6 +880,19 @@ void ActivePyModules::get_health_checks(health_check_map_t *checks) } } +void ActivePyModules::config_notify() +{ + std::lock_guard l(lock); + for (auto& i : modules) { + auto module = i.second.get(); + // Send all python calls down a Finisher to avoid blocking + // C++ code, and avoid any potential lock cycles. + finisher.queue(new FunctionContext([module](int r){ + module->config_notify(); + })); + } +} + void ActivePyModules::set_uri(const std::string& module_name, const std::string &uri) { diff --git a/src/mgr/ActivePyModules.h b/src/mgr/ActivePyModules.h index ae834039717db..bccf821aee0b8 100644 --- a/src/mgr/ActivePyModules.h +++ b/src/mgr/ActivePyModules.h @@ -111,6 +111,8 @@ public: health_check_map_t&& checks); void get_health_checks(health_check_map_t *checks); + void config_notify(); + void set_uri(const std::string& module_name, const std::string &uri); int handle_command( diff --git a/src/mgr/MgrStandby.cc b/src/mgr/MgrStandby.cc index 9265c4ff1d5ef..70e504c701001 100644 --- a/src/mgr/MgrStandby.cc +++ b/src/mgr/MgrStandby.cc @@ -138,6 +138,9 @@ int MgrStandby::init() } return false; }); + monc.register_config_notify_callback([this]() { + py_module_registry.handle_config_notify(); + }); dout(4) << "Registered monc callback" << dendl; int r = monc.init(); diff --git a/src/mgr/PyModuleRegistry.cc b/src/mgr/PyModuleRegistry.cc index 87bf6a1d53216..d7cea7fddedc5 100644 --- a/src/mgr/PyModuleRegistry.cc +++ b/src/mgr/PyModuleRegistry.cc @@ -422,6 +422,14 @@ void PyModuleRegistry::handle_config(const std::string &k, const std::string &v) } } +void PyModuleRegistry::handle_config_notify() +{ + std::lock_guard l(lock); + if (active_modules) { + active_modules->config_notify(); + } +} + void PyModuleRegistry::upgrade_config( MonClient *monc, const std::map &old_config) diff --git a/src/mgr/PyModuleRegistry.h b/src/mgr/PyModuleRegistry.h index d0cbe1881a9ce..8c46ab2923dc0 100644 --- a/src/mgr/PyModuleRegistry.h +++ b/src/mgr/PyModuleRegistry.h @@ -61,6 +61,7 @@ private: public: void handle_config(const std::string &k, const std::string &v); + void handle_config_notify(); /** * Get references to all modules (whether they have loaded and/or diff --git a/src/pybind/mgr/mgr_module.py b/src/pybind/mgr/mgr_module.py index 3c9510fbdabb7..a657515368d20 100644 --- a/src/pybind/mgr/mgr_module.py +++ b/src/pybind/mgr/mgr_module.py @@ -349,6 +349,14 @@ class MgrModule(ceph_module.BaseMgrModule): """ pass + def config_notify(self): + """ + Called by the ceph-mgr service to notify the Python plugin + that the configuration may have changed. Modules will want to + refresh any configuration values stored in config variables. + """ + pass + def serve(self): """ Called by the ceph-mgr service to start any server that -- 2.39.5