From a22b256bad1f6a4fcb9ffc10015b17bab7216afb Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Thu, 22 Jun 2017 14:19:17 -0400 Subject: [PATCH] mgr: index daemon state by string service type If we use a string we can allow for other service names like 'rgw' and 'rbd-mirror' and 'iscsigw' and so on. Signed-off-by: Sage Weil --- src/mgr/DaemonServer.cc | 4 ++-- src/mgr/DaemonState.cc | 13 ++++++------ src/mgr/DaemonState.h | 7 ++++--- src/mgr/Mgr.cc | 25 +++++++++++----------- src/mgr/PyModules.cc | 22 ++++++++++---------- src/mgr/PyModules.h | 13 +++++++----- src/mgr/PyState.cc | 40 ++++++------------------------------ src/pybind/mgr/mgr_module.py | 2 +- 8 files changed, 51 insertions(+), 75 deletions(-) diff --git a/src/mgr/DaemonServer.cc b/src/mgr/DaemonServer.cc index e6b3100f09d88..b2e40b41659ee 100644 --- a/src/mgr/DaemonServer.cc +++ b/src/mgr/DaemonServer.cc @@ -262,7 +262,7 @@ void DaemonServer::shutdown() bool DaemonServer::handle_open(MMgrOpen *m) { uint32_t type = m->get_connection()->get_peer_type(); - DaemonKey key(type, m->daemon_name); + DaemonKey key(ceph_entity_type_name(type), m->daemon_name); dout(4) << "from " << m->get_connection() << " name " << ceph_entity_type_name(type) << "." << m->daemon_name << dendl; @@ -288,7 +288,7 @@ bool DaemonServer::handle_open(MMgrOpen *m) bool DaemonServer::handle_report(MMgrReport *m) { uint32_t type = m->get_connection()->get_peer_type(); - DaemonKey key(type, m->daemon_name); + DaemonKey key(ceph_entity_type_name(type), m->daemon_name); dout(4) << "from " << m->get_connection() << " name " << ceph_entity_type_name(type) << "." << m->daemon_name << dendl; diff --git a/src/mgr/DaemonState.cc b/src/mgr/DaemonState.cc index 290fde6513453..2c7f52f30fa75 100644 --- a/src/mgr/DaemonState.cc +++ b/src/mgr/DaemonState.cc @@ -46,14 +46,15 @@ void DaemonStateIndex::_erase(const DaemonKey& dmk) all.erase(to_erase); } -DaemonStateCollection DaemonStateIndex::get_by_type(uint8_t type) const +DaemonStateCollection DaemonStateIndex::get_by_service( + const std::string& svc) const { Mutex::Locker l(lock); DaemonStateCollection result; for (const auto &i : all) { - if (i.first.first == type) { + if (i.first.first == svc) { result[i.first] = i.second; } } @@ -86,17 +87,17 @@ DaemonStatePtr DaemonStateIndex::get(const DaemonKey &key) return all.at(key); } -void DaemonStateIndex::cull(entity_type_t daemon_type, +void DaemonStateIndex::cull(const std::string& svc_name, const std::set& names_exist) { std::vector victims; Mutex::Locker l(lock); - auto begin = all.lower_bound({daemon_type, ""}); + auto begin = all.lower_bound({svc_name, ""}); auto end = all.end(); for (auto &i = begin; i != end; ++i) { const auto& daemon_key = i->first; - if (daemon_key.first != daemon_type) + if (daemon_key.first != svc_name) break; if (names_exist.count(daemon_key.second) == 0) { victims.push_back(daemon_key.second); @@ -105,7 +106,7 @@ void DaemonStateIndex::cull(entity_type_t daemon_type, for (auto &i : victims) { dout(4) << "Removing data for " << i << dendl; - _erase({daemon_type, i}); + _erase({svc_name, i}); } } diff --git a/src/mgr/DaemonState.h b/src/mgr/DaemonState.h index 91160d7f082cd..bad83bb8fec77 100644 --- a/src/mgr/DaemonState.h +++ b/src/mgr/DaemonState.h @@ -29,7 +29,7 @@ // Unique reference to a daemon within a cluster -typedef std::pair DaemonKey; +typedef std::pair DaemonKey; // An instance of a performance counter type, within // a particular daemon. @@ -146,7 +146,7 @@ class DaemonStateIndex bool exists(const DaemonKey &key) const; DaemonStatePtr get(const DaemonKey &key); DaemonStateCollection get_by_server(const std::string &hostname) const; - DaemonStateCollection get_by_type(uint8_t type) const; + DaemonStateCollection get_by_service(const std::string &svc_name) const; const DaemonStateCollection &get_all() const {return all;} const std::map &get_all_servers() const @@ -164,7 +164,8 @@ class DaemonStateIndex * a cluster map and want to ensure that anything absent in the map * is also absent in this class. */ - void cull(entity_type_t daemon_type, const std::set& names_exist); + void cull(const std::string& svc_name, + const std::set& names_exist); }; #endif diff --git a/src/mgr/Mgr.cc b/src/mgr/Mgr.cc index ffc8e008ab025..e5ed5eedaa9e3 100644 --- a/src/mgr/Mgr.cc +++ b/src/mgr/Mgr.cc @@ -93,14 +93,13 @@ public: { daemon_state.clear_updating(key); if (r == 0) { - if (key.first == CEPH_ENTITY_TYPE_MDS) { + if (key.first == "mds") { json_spirit::mValue json_result; bool read_ok = json_spirit::read( outbl.to_str(), json_result); if (!read_ok) { dout(1) << "mon returned invalid JSON for " - << ceph_entity_type_name(key.first) - << "." << key.second << dendl; + << key.first << "." << key.second << dendl; return; } @@ -134,14 +133,14 @@ public: daemon_state.insert(state); } - } else if (key.first == CEPH_ENTITY_TYPE_OSD) { + } else if (key.first == "osd") { } else { ceph_abort(); } } else { dout(1) << "mon failed to return metadata for " - << ceph_entity_type_name(key.first) - << "." << key.second << ": " << cpp_strerror(r) << dendl; + << key.first << "." << key.second << ": " + << cpp_strerror(r) << dendl; } } }; @@ -260,7 +259,7 @@ void Mgr::load_all_metadata() } DaemonStatePtr dm = std::make_shared(daemon_state.types); - dm->key = DaemonKey(CEPH_ENTITY_TYPE_MDS, + dm->key = DaemonKey("mds", daemon_meta.at("name").get_str()); dm->hostname = daemon_meta.at("hostname").get_str(); @@ -282,7 +281,7 @@ void Mgr::load_all_metadata() } DaemonStatePtr dm = std::make_shared(daemon_state.types); - dm->key = DaemonKey(CEPH_ENTITY_TYPE_MON, + dm->key = DaemonKey("mon", daemon_meta.at("name").get_str()); dm->hostname = daemon_meta.at("hostname").get_str(); @@ -305,7 +304,7 @@ void Mgr::load_all_metadata() dout(4) << osd_metadata.at("hostname").get_str() << dendl; DaemonStatePtr dm = std::make_shared(daemon_state.types); - dm->key = DaemonKey(CEPH_ENTITY_TYPE_OSD, + dm->key = DaemonKey("osd", stringify(osd_metadata.at("id").get_int())); dm->hostname = osd_metadata.at("hostname").get_str(); @@ -401,7 +400,7 @@ void Mgr::handle_osd_map() // Consider whether to update the daemon metadata (new/restarted daemon) bool update_meta = false; - const auto k = DaemonKey(CEPH_ENTITY_TYPE_OSD, stringify(osd_id)); + const auto k = DaemonKey("osd", stringify(osd_id)); if (daemon_state.is_updating(k)) { continue; } @@ -446,7 +445,7 @@ void Mgr::handle_osd_map() }); // TODO: same culling for MonMap - daemon_state.cull(CEPH_ENTITY_TYPE_OSD, names_exist); + daemon_state.cull("osd", names_exist); } void Mgr::handle_log(MLog *m) @@ -525,7 +524,7 @@ void Mgr::handle_fs_map(MFSMap* m) // Remember which MDS exists so that we can cull any that don't names_exist.insert(info.name); - const auto k = DaemonKey(CEPH_ENTITY_TYPE_MDS, info.name); + const auto k = DaemonKey("mds", info.name); if (daemon_state.is_updating(k)) { continue; } @@ -565,7 +564,7 @@ void Mgr::handle_fs_map(MFSMap* m) {}, &c->outbl, &c->outs, c); } } - daemon_state.cull(CEPH_ENTITY_TYPE_MDS, names_exist); + daemon_state.cull("mds", names_exist); } bool Mgr::got_mgr_map(const MgrMap& m) diff --git a/src/mgr/PyModules.cc b/src/mgr/PyModules.cc index 96b2698e3c39f..e561c9e481a33 100644 --- a/src/mgr/PyModules.cc +++ b/src/mgr/PyModules.cc @@ -58,7 +58,7 @@ void PyModules::dump_server(const std::string &hostname, for (const auto &i : dmc) { const auto &key = i.first; - const std::string str_type = ceph_entity_type_name(key.first); + const std::string &str_type = key.first; const std::string &svc_name = key.second; // TODO: pick the highest version, and make sure that @@ -116,10 +116,12 @@ PyObject *PyModules::list_servers_python() return f.get(); } -PyObject *PyModules::get_metadata_python(std::string const &handle, - entity_type_t svc_type, const std::string &svc_id) +PyObject *PyModules::get_metadata_python( + std::string const &handle, + const std::string &svc_name, + const std::string &svc_id) { - auto metadata = daemon_state.get(DaemonKey(svc_type, svc_id)); + auto metadata = daemon_state.get(DaemonKey(svc_name, svc_id)); PyFormatter f; f.dump_string("hostname", metadata->hostname); for (const auto &i : metadata->metadata) { @@ -175,7 +177,7 @@ PyObject *PyModules::get_python(const std::string &what) return f.get(); } else if (what == "osd_metadata") { PyFormatter f; - auto dmc = daemon_state.get_by_type(CEPH_ENTITY_TYPE_OSD); + auto dmc = daemon_state.get_by_service("osd"); for (const auto &i : dmc) { f.open_object_section(i.first.second.c_str()); f.dump_string("hostname", i.second->hostname); @@ -617,7 +619,7 @@ void PyModules::log(const std::string &handle, PyObject* PyModules::get_counter_python( const std::string &handle, - entity_type_t svc_type, + const std::string &svc_name, const std::string &svc_id, const std::string &path) { @@ -628,7 +630,7 @@ PyObject* PyModules::get_counter_python( PyFormatter f; f.open_array_section(path.c_str()); - auto metadata = daemon_state.get(DaemonKey(svc_type, svc_id)); + auto metadata = daemon_state.get(DaemonKey(svc_name, svc_id)); // FIXME: this is unsafe, I need to either be inside DaemonStateIndex's // lock or put a lock on individual DaemonStates @@ -645,8 +647,7 @@ PyObject* PyModules::get_counter_python( } } else { dout(4) << "Missing counter: '" << path << "' (" - << ceph_entity_type_name(svc_type) << "." - << svc_id << ")" << dendl; + << svc_name << "." << svc_id << ")" << dendl; dout(20) << "Paths are:" << dendl; for (const auto &i : metadata->perf_counters.instances) { dout(20) << i.first << dendl; @@ -654,8 +655,7 @@ PyObject* PyModules::get_counter_python( } } else { dout(4) << "No daemon state for " - << ceph_entity_type_name(svc_type) << "." - << svc_id << ")" << dendl; + << svc_name << "." << svc_id << ")" << dendl; } f.close_section(); return f.get(); diff --git a/src/mgr/PyModules.h b/src/mgr/PyModules.h index 93cbd5be8518a..80f8813d5ee0c 100644 --- a/src/mgr/PyModules.h +++ b/src/mgr/PyModules.h @@ -64,11 +64,14 @@ public: PyObject *get_python(const std::string &what); PyObject *get_server_python(const std::string &hostname); PyObject *list_servers_python(); - PyObject *get_metadata_python(std::string const &handle, - entity_type_t svc_type, const std::string &svc_id); - PyObject *get_counter_python(std::string const &handle, - entity_type_t svc_type, const std::string &svc_id, - const std::string &path); + PyObject *get_metadata_python( + std::string const &handle, + const std::string &svc_name, const std::string &svc_id); + PyObject *get_counter_python( + std::string const &handle, + const std::string &svc_name, + const std::string &svc_id, + const std::string &path); PyObject *get_context(); std::map config_cache; diff --git a/src/mgr/PyState.cc b/src/mgr/PyState.cc index 963526fc519d7..dbde1e2252867 100644 --- a/src/mgr/PyState.cc +++ b/src/mgr/PyState.cc @@ -266,37 +266,16 @@ ceph_config_set(PyObject *self, PyObject *args) Py_RETURN_NONE; } -static entity_type_t svc_type_from_str(const std::string &type_str) -{ - if (type_str == std::string("mds")) { - return CEPH_ENTITY_TYPE_MDS; - } else if (type_str == std::string("osd")) { - return CEPH_ENTITY_TYPE_OSD; - } else if (type_str == std::string("mon")) { - return CEPH_ENTITY_TYPE_MON; - } else { - return CEPH_ENTITY_TYPE_ANY; - } -} - static PyObject* get_metadata(PyObject *self, PyObject *args) { char *handle = nullptr; - char *type_str = NULL; + char *svc_name = NULL; char *svc_id = NULL; - if (!PyArg_ParseTuple(args, "sss:get_metadata", &handle, &type_str, &svc_id)) { - return nullptr; - } - - entity_type_t svc_type = svc_type_from_str(type_str); - if (svc_type == CEPH_ENTITY_TYPE_ANY) { - // FIXME: form a proper exception + if (!PyArg_ParseTuple(args, "sss:get_metadata", &handle, &svc_name, &svc_id)) { return nullptr; } - - - return global_handle->get_metadata_python(handle, svc_type, svc_id); + return global_handle->get_metadata_python(handle, svc_name, svc_id); } static PyObject* @@ -330,22 +309,15 @@ static PyObject* get_counter(PyObject *self, PyObject *args) { char *handle = nullptr; - char *type_str = nullptr; + char *svc_name = nullptr; char *svc_id = nullptr; char *counter_path = nullptr; - if (!PyArg_ParseTuple(args, "ssss:get_counter", &handle, &type_str, + if (!PyArg_ParseTuple(args, "ssss:get_counter", &handle, &svc_name, &svc_id, &counter_path)) { return nullptr; } - - entity_type_t svc_type = svc_type_from_str(type_str); - if (svc_type == CEPH_ENTITY_TYPE_ANY) { - // FIXME: form a proper exception - return nullptr; - } - return global_handle->get_counter_python( - handle, svc_type, svc_id, counter_path); + handle, svc_name, svc_id, counter_path); } PyMethodDef CephStateMethods[] = { diff --git a/src/pybind/mgr/mgr_module.py b/src/pybind/mgr/mgr_module.py index 2e4ba6b142c9c..8d5b0f9de6f5b 100644 --- a/src/pybind/mgr/mgr_module.py +++ b/src/pybind/mgr/mgr_module.py @@ -134,7 +134,7 @@ class MgrModule(object): """ Fetch the metadata for a particular service. - :param svc_type: one of 'mds', 'osd', 'mon' + :param svc_type: string (e.g., 'mds', 'osd', 'mon') :param svc_id: string :return: dict """ -- 2.39.5