From 8b8e7522982476d25685ad055d4a89f6173041f8 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Tue, 29 Jun 2021 18:46:36 -0400 Subject: [PATCH] mgr: add get() for standby modules Fixes: https://tracker.ceph.com/issues/51446 Signed-off-by: Sage Weil --- src/mgr/BaseMgrStandbyModule.cc | 40 +++++++++++++++++++++++++++++++++ src/mgr/StandbyPyModules.h | 3 +++ src/pybind/mgr/ceph_module.pyi | 1 + src/pybind/mgr/mgr_module.py | 3 +++ 4 files changed, 47 insertions(+) diff --git a/src/mgr/BaseMgrStandbyModule.cc b/src/mgr/BaseMgrStandbyModule.cc index 84c9b79f2f7..6f35088d030 100644 --- a/src/mgr/BaseMgrStandbyModule.cc +++ b/src/mgr/BaseMgrStandbyModule.cc @@ -15,6 +15,7 @@ #include "BaseMgrStandbyModule.h" #include "StandbyPyModules.h" +#include "PyFormatter.h" #define dout_context g_ceph_context @@ -164,7 +165,46 @@ ceph_log(BaseMgrStandbyModule *self, PyObject *args) Py_RETURN_NONE; } +static PyObject* +ceph_standby_state_get(BaseMgrStandbyModule *self, PyObject *args) +{ + char *whatc = NULL; + if (!PyArg_ParseTuple(args, "s:ceph_state_get", &whatc)) { + return NULL; + } + std::string what(whatc); + + PyFormatter f; + + // Drop the GIL, as most of the following blocks will block on + // a mutex -- they are all responsible for re-taking the GIL before + // touching the PyFormatter instance or returning from the function. + without_gil_t no_gil; + + if (what == "mgr_ips") { + entity_addrvec_t myaddrs = self->this_module->get_myaddrs(); + with_gil_t with_gil{no_gil}; + f.open_array_section("ips"); + std::set did; + for (auto& i : myaddrs.v) { + std::string ip = i.ip_only_to_str(); + if (auto [where, inserted] = did.insert(ip); inserted) { + f.dump_string("ip", ip); + } + } + f.close_section(); + return f.get(); + } else { + derr << "Python module requested unknown data '" << what << "'" << dendl; + with_gil_t with_gil{no_gil}; + Py_RETURN_NONE; + } +} + + PyMethodDef BaseMgrStandbyModule_methods[] = { + {"_ceph_get", (PyCFunction)ceph_standby_state_get, METH_VARARGS, + "Get a cluster object (standby)"}, {"_ceph_get_mgr_id", (PyCFunction)ceph_get_mgr_id, METH_NOARGS, "Get the name of the Mgr daemon where we are running"}, diff --git a/src/mgr/StandbyPyModules.h b/src/mgr/StandbyPyModules.h index b434c917c7a..501dfc8c7e4 100644 --- a/src/mgr/StandbyPyModules.h +++ b/src/mgr/StandbyPyModules.h @@ -93,6 +93,9 @@ class StandbyPyModule : public PyModuleRunner bool get_config(const std::string &key, std::string *value) const; bool get_store(const std::string &key, std::string *value) const; std::string get_active_uri() const; + entity_addrvec_t get_myaddrs() const { + return state.get_monc().get_myaddrs(); + } int load(); }; diff --git a/src/pybind/mgr/ceph_module.pyi b/src/pybind/mgr/ceph_module.pyi index 75575aeca15..4a5bac33f3e 100644 --- a/src/pybind/mgr/ceph_module.pyi +++ b/src/pybind/mgr/ceph_module.pyi @@ -39,6 +39,7 @@ class BasePyCRUSH(object): class BaseMgrStandbyModule(object): def __init__(self, capsule): pass + def _ceph_get(self, data_name: str) -> Dict[str, Any]: ... def _ceph_get_mgr_id(self):... def _ceph_get_module_option(self, key, prefix=None):... def _ceph_get_option(self, key):... diff --git a/src/pybind/mgr/mgr_module.py b/src/pybind/mgr/mgr_module.py index 610fb4eb911..e6e0945b6bb 100644 --- a/src/pybind/mgr/mgr_module.py +++ b/src/pybind/mgr/mgr_module.py @@ -822,6 +822,9 @@ class MgrStandbyModule(ceph_module.BaseMgrStandbyModule, MgrModuleLoggingMixin): def get_active_uri(self) -> str: return self._ceph_get_active_uri() + def get(self, data_name: str): + return self._ceph_get(data_name) + def get_mgr_ip(self) -> str: # we don't have get() for standby modules; make do with the hostname return socket.gethostname() -- 2.39.5