From d56f77bf8946922a71727e1f3f5195886fe75454 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 21 Jun 2019 19:07:06 +0800 Subject: [PATCH] crush,osd: add operator<<(ostream, const CrushLocation&) * add operator<<(ostream&, const CrushLocation&) out from OSD.cc, so it can be reused by crimson-osd * update OSD.cc to use operator<<(ostream&, const CrushLocation&) * use ceph::mutex instead of std::mutex. so CrushLocation can be used by crimson-osd. Signed-off-by: Kefu Chai --- src/crush/CrushLocation.cc | 24 ++++++++++++++++++++++-- src/crush/CrushLocation.h | 23 ++++++++++++----------- src/osd/OSD.cc | 16 ++++------------ 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/crush/CrushLocation.cc b/src/crush/CrushLocation.cc index 39f89f323e2..0e5a0855cd6 100644 --- a/src/crush/CrushLocation.cc +++ b/src/crush/CrushLocation.cc @@ -34,7 +34,7 @@ int CrushLocation::_parse(const std::string& s) << loc << dendl; return -EINVAL; } - std::lock_guard l(lock); + std::lock_guard l(lock); loc.swap(new_crush_location); lgeneric_dout(cct, 10) << "crush_location is " << loc << dendl; return 0; @@ -115,10 +115,30 @@ int CrushLocation::init_on_startup() break; } } - std::lock_guard l(lock); + std::lock_guard l(lock); loc.clear(); loc.insert(std::make_pair("host", hostname)); loc.insert(std::make_pair("root", "default")); lgeneric_dout(cct, 10) << "crush_location is (default) " << loc << dendl; return 0; } + +std::multimap CrushLocation::get_location() const +{ + std::lock_guard l(lock); + return loc; +} + +std::ostream& operator<<(std::ostream& os, const CrushLocation& loc) +{ + bool first = true; + for (auto& [type, pos] : loc.get_location()) { + if (first) { + first = false; + } else { + os << ", "; + } + os << '"' << type << '=' << pos << '"'; + } + return os; +} diff --git a/src/crush/CrushLocation.h b/src/crush/CrushLocation.h index 6a099689379..36789bd782b 100644 --- a/src/crush/CrushLocation.h +++ b/src/crush/CrushLocation.h @@ -4,19 +4,15 @@ #ifndef CEPH_CRUSH_LOCATION_H #define CEPH_CRUSH_LOCATION_H +#include #include -#include #include +#include "common/ceph_mutex.h" + class CephContext; class CrushLocation { - CephContext *cct; - std::multimap loc; - std::mutex lock; - - int _parse(const std::string& s); - public: explicit CrushLocation(CephContext *c) : cct(c) { init_on_startup(); @@ -26,10 +22,15 @@ public: int update_from_hook(); ///< call hook, if present int init_on_startup(); - std::multimap get_location() { - std::lock_guard l(lock); - return loc; - } + std::multimap get_location() const; + +private: + int _parse(const std::string& s); + CephContext *cct; + std::multimap loc; + mutable ceph::mutex lock = ceph::make_mutex("CrushLocation"); }; +std::ostream& operator<<(std::ostream& os, const CrushLocation& loc); + #endif diff --git a/src/osd/OSD.cc b/src/osd/OSD.cc index 155ddd3b827..473954699ef 100644 --- a/src/osd/OSD.cc +++ b/src/osd/OSD.cc @@ -3589,21 +3589,13 @@ int OSD::update_crush_location() double(1ull << 40 /* TB */))); } - std::multimap loc = cct->crush_location.get_location(); - dout(10) << __func__ << " crush location is " << loc << dendl; + dout(10) << __func__ << " crush location is " << cct->crush_location << dendl; string cmd = string("{\"prefix\": \"osd crush create-or-move\", ") + - string("\"id\": ") + stringify(whoami) + string(", ") + - string("\"weight\":") + weight + string(", ") + - string("\"args\": ["); - for (multimap::iterator p = loc.begin(); p != loc.end(); ++p) { - if (p != loc.begin()) - cmd += ", "; - cmd += "\"" + p->first + "=" + p->second + "\""; - } - cmd += "]}"; - + string("\"id\": ") + stringify(whoami) + ", " + + string("\"weight\":") + weight + ", " + + string("\"args\": [") + stringify(cct->crush_location) + "]}"; return mon_cmd_maybe_osd_create(cmd); } -- 2.39.5