From: Kefu Chai Date: Fri, 5 May 2017 07:39:39 +0000 (+0800) Subject: mgr: optimize DaemonStateIndex::cull() a little bit X-Git-Tag: v12.1.0~10^2~93^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=0e7137aa6a1f1d9f6d1276acc2cd89f5787ecce3;p=ceph.git mgr: optimize DaemonStateIndex::cull() a little bit if the size of the cluster is quite large, we don't need to walk through all daemons to do the cull. * smaller scope protected by the lock * check smaller range in "all" * use vector for holding victims: all of them are of "daemon_type". Signed-off-by: Kefu Chai --- diff --git a/src/mgr/DaemonState.cc b/src/mgr/DaemonState.cc index f83f9749de6c..290fde651345 100644 --- a/src/mgr/DaemonState.cc +++ b/src/mgr/DaemonState.cc @@ -30,18 +30,20 @@ void DaemonStateIndex::insert(DaemonStatePtr dm) all[dm->key] = dm; } -void DaemonStateIndex::_erase(DaemonKey dmk) +void DaemonStateIndex::_erase(const DaemonKey& dmk) { assert(lock.is_locked_by_me()); - const auto dm = all.at(dmk); + const auto to_erase = all.find(dmk); + assert(to_erase != all.end()); + const auto dm = to_erase->second; auto &server_collection = by_server[dm->hostname]; server_collection.erase(dm->key); if (server_collection.empty()) { by_server.erase(dm->hostname); } - all.erase(dmk); + all.erase(to_erase); } DaemonStateCollection DaemonStateIndex::get_by_type(uint8_t type) const @@ -85,25 +87,25 @@ DaemonStatePtr DaemonStateIndex::get(const DaemonKey &key) } void DaemonStateIndex::cull(entity_type_t daemon_type, - std::set names_exist) + const std::set& names_exist) { - Mutex::Locker l(lock); - - std::set victims; - - for (const auto &i : all) { - if (i.first.first != daemon_type) { - continue; - } + std::vector victims; - if (names_exist.count(i.first.second) == 0) { - victims.insert(i.first); + Mutex::Locker l(lock); + auto begin = all.lower_bound({daemon_type, ""}); + auto end = all.end(); + for (auto &i = begin; i != end; ++i) { + const auto& daemon_key = i->first; + if (daemon_key.first != daemon_type) + break; + if (names_exist.count(daemon_key.second) == 0) { + victims.push_back(daemon_key.second); } } - for (const auto &i : victims) { + for (auto &i : victims) { dout(4) << "Removing data for " << i << dendl; - _erase(i); + _erase({daemon_type, i}); } } diff --git a/src/mgr/DaemonState.h b/src/mgr/DaemonState.h index 78fd036525fd..91160d7f082c 100644 --- a/src/mgr/DaemonState.h +++ b/src/mgr/DaemonState.h @@ -141,7 +141,7 @@ class DaemonStateIndex PerfCounterTypes types; void insert(DaemonStatePtr dm); - void _erase(DaemonKey dmk); + void _erase(const DaemonKey& dmk); bool exists(const DaemonKey &key) const; DaemonStatePtr get(const DaemonKey &key); @@ -164,7 +164,7 @@ 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, std::set names_exist); + void cull(entity_type_t daemon_type, const std::set& names_exist); }; #endif