]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mon/OSDMonitor: add "osd reweightn" command
authorKefu Chai <kchai@redhat.com>
Sat, 8 Apr 2017 07:12:31 +0000 (15:12 +0800)
committerKefu Chai <kchai@redhat.com>
Mon, 10 Apr 2017 15:20:16 +0000 (23:20 +0800)
the "weights" parameter is an escaped serialized json dict, where double
quote is replaced with single quote.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/mon/MonCommands.h
src/mon/OSDMonitor.cc

index 0d741c2d5c9a1f31fcedd8f8cbb5904003bc810d..00eac7a93eb0e3e9ca16ed755b895d809e1d9efe 100644 (file)
@@ -622,6 +622,10 @@ COMMAND("osd reweight " \
        "name=id,type=CephOsdName " \
        "type=CephFloat,name=weight,range=0.0|1.0", \
        "reweight osd to 0.0 < <weight> < 1.0", "osd", "rw", "cli,rest")
+COMMAND("osd reweightn " \
+       "name=weights,type=CephString",
+       "reweight osds with {<id>: <weight>,...})",
+       "osd", "rw", "cli,rest")
 COMMAND("osd pg-temp " \
        "name=pgid,type=CephPgid " \
        "name=id,type=CephOsdName,n=N,req=false", \
index bb708fa4e7921404c7d3148587b66eb50c55f1ae..a9187c4f208dd96f8a3bf2858e54080348dbfb61 100644 (file)
@@ -70,6 +70,8 @@
 #include "include/str_list.h"
 #include "include/str_map.h"
 
+#include "json_spirit/json_spirit_reader.h"
+
 #define dout_subsys ceph_subsys_mon
 #define OSD_PG_CREATING_PREFIX "osd_pg_creating"
 
@@ -5837,6 +5839,42 @@ bool OSDMonitor::prepare_command(MonOpRequestRef op)
   return prepare_command_impl(op, cmdmap);
 }
 
+static int parse_reweights(CephContext *cct,
+                          const map<string,cmd_vartype> &cmdmap,
+                          const OSDMap& osdmap,
+                          map<int32_t, uint32_t>* weights)
+{
+  string weights_str;
+  if (!cmd_getval(g_ceph_context, cmdmap, "weights", weights_str)) {
+    return -EINVAL;
+  }
+  std::replace(begin(weights_str), end(weights_str), '\'', '"');
+  json_spirit::mValue json_value;
+  if (!json_spirit::read(weights_str, json_value)) {
+    return -EINVAL;
+  }
+  if (json_value.type() != json_spirit::obj_type) {
+    return -EINVAL;
+  }
+  const auto obj = json_value.get_obj();
+  try {
+    for (auto& osd_weight : obj) {
+      auto osd_id = std::stoi(osd_weight.first);
+      if (!osdmap.exists(osd_id)) {
+       return -ENOENT;
+      }
+      if (osd_weight.second.type() != json_spirit::str_type) {
+       return -EINVAL;
+      }
+      auto weight = std::stoul(osd_weight.second.get_str());
+      weights->insert({osd_id, weight});
+    }
+  } catch (const std::logic_error& e) {
+    return -EINVAL;
+  }
+  return 0;
+}
+
 bool OSDMonitor::prepare_command_impl(MonOpRequestRef op,
                                      map<string,cmd_vartype> &cmdmap)
 {
@@ -7338,6 +7376,19 @@ bool OSDMonitor::prepare_command_impl(MonOpRequestRef op,
       err = -ENOENT;
       goto reply;
     }
+  } else if (prefix == "osd reweightn") {
+    map<int32_t, uint32_t> weights;
+    err = parse_reweights(g_ceph_context, cmdmap, osdmap, &weights);
+    if (err) {
+      ss << "unable to parse 'weights' value '"
+         << cmd_vartype_stringify(cmdmap["weights"]) << "'";
+      goto reply;
+    }
+    pending_inc.new_weight = std::move(weights);
+    wait_for_finished_proposal(
+       op,
+       new Monitor::C_Command(mon, op, 0, rs, rdata, get_last_committed() + 1));
+      return true;
   } else if (prefix == "osd lost") {
     int64_t id;
     if (!cmd_getval(g_ceph_context, cmdmap, "id", id)) {