]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
crush,osd: add operator<<(ostream, const CrushLocation&)
authorKefu Chai <kchai@redhat.com>
Fri, 21 Jun 2019 11:07:06 +0000 (19:07 +0800)
committerKefu Chai <kchai@redhat.com>
Fri, 21 Jun 2019 12:31:46 +0000 (20:31 +0800)
* 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 <kchai@redhat.com>
src/crush/CrushLocation.cc
src/crush/CrushLocation.h
src/osd/OSD.cc

index 39f89f323e24701c7ad863137bc5a2f1a00e5f09..0e5a0855cd6a0b9b84fa472e22d92c990c87c646 100644 (file)
@@ -34,7 +34,7 @@ int CrushLocation::_parse(const std::string& s)
               << loc << dendl;
     return -EINVAL;
   }
-  std::lock_guard<std::mutex> 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<std::mutex> l(lock);
+  std::lock_guard l(lock);
   loc.clear();
   loc.insert(std::make_pair<std::string,std::string>("host", hostname));
   loc.insert(std::make_pair<std::string,std::string>("root", "default"));
   lgeneric_dout(cct, 10) << "crush_location is (default) " << loc << dendl;
   return 0;
 }
+
+std::multimap<std::string,std::string> 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;
+}
index 6a099689379883210beacb91cb155efa8f53cac7..36789bd782b5fde602da51ccd492c2985c153cdd 100644 (file)
@@ -4,19 +4,15 @@
 #ifndef CEPH_CRUSH_LOCATION_H
 #define CEPH_CRUSH_LOCATION_H
 
+#include <iosfwd>
 #include <map>
-#include <mutex>
 #include <string>
 
+#include "common/ceph_mutex.h"
+
 class CephContext;
 
 class CrushLocation {
-  CephContext *cct;
-  std::multimap<std::string,std::string> 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<std::string,std::string> get_location() {
-    std::lock_guard<std::mutex> l(lock);
-    return loc;
-  }
+  std::multimap<std::string,std::string> get_location() const;
+
+private:
+  int _parse(const std::string& s);
+  CephContext *cct;
+  std::multimap<std::string,std::string> loc;
+  mutable ceph::mutex lock = ceph::make_mutex("CrushLocation");
 };
 
+std::ostream& operator<<(std::ostream& os, const CrushLocation& loc);
+
 #endif
index 155ddd3b827ed600e108486f264d5dc97af3773b..473954699efe9e0cb9a94fdcdff574eff3fecfc9 100644 (file)
@@ -3589,21 +3589,13 @@ int OSD::update_crush_location()
                      double(1ull << 40 /* TB */)));
   }
 
-  std::multimap<string,string> 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<string,string>::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);
 }