]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
crush/CrushLocation: add class to manage crush_location
authorSage Weil <sage@redhat.com>
Tue, 29 Mar 2016 16:49:16 +0000 (12:49 -0400)
committerSage Weil <sage@redhat.com>
Mon, 9 May 2016 12:54:44 +0000 (08:54 -0400)
The crush_location can come from an explicitly set config,
a hook, or a simple fabricated default (root=default host=...).

Signed-off-by: Sage Weil <sage@redhat.com>
src/CMakeLists.txt
src/common/config_opts.h
src/crush/CrushLocation.cc [new file with mode: 0644]
src/crush/CrushLocation.h [new file with mode: 0644]
src/crush/Makefile.am

index 1cf18b27b035c8a74f6aa6a44927d28e6aa7121a..13d42237175d15ff33c969c2329e89faa266b049 100644 (file)
@@ -184,7 +184,8 @@ set(crush_srcs
   crush/hash.c
   crush/CrushWrapper.cc
   crush/CrushCompiler.cc
-  crush/CrushTester.cc)
+  crush/CrushTester.cc
+  crush/CrushLocation.cc)
 
 add_library(crush STATIC ${crush_srcs})
 
index 185673c70b5b93a0c68a80bd15b441ff7613b686..0ec74a5f8acb9a267e26fd7f496b3370be4d19b4 100644 (file)
@@ -417,6 +417,8 @@ OPTION(client_use_faked_inos, OPT_BOOL, false)
 OPTION(client_mds_namespace, OPT_INT, -1)
 
 OPTION(crush_location, OPT_STR, "")       // whitespace-separated list of key=value pairs describing crush location
+OPTION(crush_location_hook, OPT_STR, "")
+OPTION(crush_location_hook_timeout, OPT_INT, 10)
 
 OPTION(objecter_tick_interval, OPT_DOUBLE, 5.0)
 OPTION(objecter_timeout, OPT_DOUBLE, 10.0)    // before we ask for a map
diff --git a/src/crush/CrushLocation.cc b/src/crush/CrushLocation.cc
new file mode 100644 (file)
index 0000000..5a32c43
--- /dev/null
@@ -0,0 +1,105 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "CrushLocation.h"
+#include "CrushWrapper.h"
+#include "common/config.h"
+#include "include/str_list.h"
+#include "common/debug.h"
+
+#include <common/SubProcess.h>
+
+#include <vector>
+
+int CrushLocation::update_from_conf()
+{
+  if (cct->_conf->crush_location.length())
+    return _parse(cct->_conf->crush_location);
+  return 0;
+}
+
+int CrushLocation::_parse(const std::string& s)
+{
+  std::multimap<std::string,std::string> new_crush_location;
+  std::vector<std::string> lvec;
+  get_str_vec(s, ";, \t", lvec);
+  int r = CrushWrapper::parse_loc_multimap(lvec, &new_crush_location);
+  if (r < 0) {
+    lderr(cct) << "warning: crush_location '" << cct->_conf->crush_location
+              << "' does not parse, keeping original crush_location "
+              << loc << dendl;
+    return -EINVAL;
+  }
+  std::lock_guard<std::mutex> l(lock);
+  loc.swap(new_crush_location);
+  lgeneric_dout(cct, 10) << "crush_location is " << loc << dendl;
+  return 0;
+}
+
+int CrushLocation::update_from_hook()
+{
+  if (cct->_conf->crush_location_hook.length() == 0)
+    return 0;
+
+  SubProcessTimed hook(
+    cct->_conf->crush_location_hook.c_str(),
+    SubProcess::CLOSE, SubProcess::PIPE, SubProcess::PIPE,
+    cct->_conf->crush_location_hook_timeout);
+  hook.add_cmd_args(
+    "--cluster", cct->_conf->cluster.c_str(),
+    "--id", cct->_conf->name.get_id().c_str(),
+    "--type", cct->_conf->name.get_type_str(),
+    NULL);
+  int ret = hook.spawn();
+  if (ret != 0) {
+    lderr(cct) << "error: failed run " << cct->_conf->crush_location_hook << ": "
+              << hook.err() << dendl;
+    return ret;
+  }
+
+  bufferlist bl;
+  ret = bl.read_fd(hook.get_stdout(), 100 * 1024);
+  if (ret < 0) {
+    lderr(cct) << "error: failed read stdout from "
+              << cct->_conf->crush_location_hook
+              << ": " << cpp_strerror(-ret) << dendl;
+    bufferlist err;
+    err.read_fd(hook.get_stderr(), 100 * 1024);
+    lderr(cct) << "stderr:\n";
+    err.hexdump(*_dout);
+    *_dout << dendl;
+    return ret;
+  }
+
+  if (hook.join() != 0) {
+    lderr(cct) << "error: failed to join: " << hook.err() << dendl;
+    return -EINVAL;
+  }
+
+  std::string out;
+  bl.copy(0, bl.length(), out);
+  out.erase(out.find_last_not_of(" \n\r\t")+1);
+  return _parse(out);
+}
+
+int CrushLocation::init_on_startup()
+{
+  if (cct->_conf->crush_location.length()) {
+    return update_from_conf();
+  }
+  if (cct->_conf->crush_location_hook.length()) {
+    return update_from_hook();
+  }
+
+  // start with a sane default
+  char hostname[HOST_NAME_MAX + 1];
+  int r = gethostname(hostname, sizeof(hostname)-1);
+  if (r < 0)
+    strcpy(hostname, "unknown_host");
+  std::lock_guard<std::mutex> l(lock);
+  loc.clear();
+  loc.insert(make_pair<std::string,std::string>("host", hostname));
+  loc.insert(make_pair<std::string,std::string>("root", "default"));
+  lgeneric_dout(cct, 10) << "crush_location is (default) " << loc << dendl;
+  return 0;
+}
diff --git a/src/crush/CrushLocation.h b/src/crush/CrushLocation.h
new file mode 100644 (file)
index 0000000..b3241ef
--- /dev/null
@@ -0,0 +1,35 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#ifndef CEPH_CRUSH_LOCATION_H
+#define CEPH_CRUSH_LOCATION_H
+
+#include <map>
+#include <mutex>
+#include <string>
+
+class CephContext;
+
+class CrushLocation {
+  CephContext *cct;
+  std::multimap<std::string,std::string> loc;
+  std::mutex lock;
+
+  int _parse(const std::string& s);
+
+public:
+  CrushLocation(CephContext *c) : cct(c) {
+    update_from_conf();
+  }
+
+  int update_from_conf();  ///< refresh from config
+  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;
+  }
+};
+
+#endif
index 5da6f17f280ec59ecd4b085dd3f217c24d09d51d..dafdc37bd5ae84d3d14198f9b3f61c6662a2d70e 100644 (file)
@@ -5,11 +5,13 @@ libcrush_la_SOURCES = \
        crush/hash.c \
        crush/CrushWrapper.cc \
        crush/CrushCompiler.cc \
-       crush/CrushTester.cc
+       crush/CrushTester.cc \
+       crush/CrushLocation.cc
 noinst_LTLIBRARIES += libcrush.la
 
 noinst_HEADERS += \
        crush/CrushCompiler.h \
+       crush/CrushLocation.h \
        crush/CrushTester.h \
        crush/CrushTreeDumper.h \
        crush/CrushWrapper.h \