]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mon: clean up 'mon ...' command processing, add 'mon getmap'
authorSage Weil <sage@newdream.net>
Wed, 14 Oct 2009 05:34:53 +0000 (22:34 -0700)
committerSage Weil <sage@newdream.net>
Wed, 14 Oct 2009 05:34:53 +0000 (22:34 -0700)
src/mon/Monitor.cc
src/mon/MonmapMonitor.cc
src/mon/MonmapMonitor.h

index 3c9f314a8a14eb1e3f73d6a4fe45dde658bdb6d6..5f5af3d9835d353588790dad381ef7ab70d4e250 100644 (file)
@@ -235,6 +235,7 @@ void Monitor::handle_command(MMonCommand *m)
   }
 
   dout(0) << "handle_command " << *m << dendl;
+  bufferlist rdata;
   string rs;
   int r = -EINVAL;
   if (!m->cmd.empty()) {
@@ -278,34 +279,11 @@ void Monitor::handle_command(MMonCommand *m)
       classmon()->dispatch(m);
       return;
     }
-    if (m->cmd[0] == "mon") {
-      if (m->cmd[1] == "injectargs" && m->cmd.size() == 4) {
-       vector<string> args(2);
-       args[0] = "_injectargs";
-       args[1] = m->cmd[3];
-       if (m->cmd[2] == "*") {
-         for (unsigned i=0; i<monmap->size(); i++)
-           inject_args(monmap->get_inst(i), args, 0);
-         r = 0;
-         rs = "ok bcast";
-       } else {
-         errno = 0;
-         int who = strtol(m->cmd[2].c_str(), 0, 10);
-         if (!errno && who >= 0) {
-           inject_args(monmap->get_inst(who), args, 0);
-           r = 0;
-           rs = "ok";
-         } else 
-           rs = "specify mon number or *";
-       }
-      } else
-       rs = "unrecognized mon command";
-    } else 
-      rs = "unrecognized subsystem";
+    rs = "unrecognized subsystem";
   } else 
     rs = "no command";
 
-  reply_command(m, r, rs, 0);
+  reply_command(m, r, rs, rdata, 0);
 }
 
 void Monitor::reply_command(MMonCommand *m, int rc, const string &rs, version_t version)
index 96adf1f002afd7a7d4889b2ab169d0c42626e769..69e17dc5d8a7125ea2bc90138307051787c8028e 100644 (file)
@@ -79,32 +79,112 @@ void MonmapMonitor::encode_pending(bufferlist& bl)
 
 bool MonmapMonitor::preprocess_query(PaxosServiceMessage *m)
 {
-  return false;
+  switch (m->get_type()) {
+    // READs
+  case MSG_MON_COMMAND:
+    return preprocess_command((MMonCommand*)m);
+  default:
+    assert(0);
+    delete m;
+    return true;
+  }
 }
 
-bool MonmapMonitor::prepare_update(PaxosServiceMessage *message)
+bool MonmapMonitor::preprocess_command(MMonCommand *m)
 {
-  MMonCommand *m = (MMonCommand *) message;
-  if (m->cmd[1] != "add") {
-    dout(0) << "Unrecognized MonmapMonitor command!" << dendl;
-    delete message;
-    return false;
-  }
-  entity_addr_t addr;
-  parse_ip_port(m->cmd[2].c_str(), addr);
+  int r = -1;
   bufferlist rdata;
-  if (!pending_map.contains(addr)) {
-    pending_map.add(addr);
-    pending_map.last_changed = g_clock.now();
-    mon->reply_command(m, 0, "added mon to map",
-                      rdata, paxos->get_version());
+  stringstream ss;
+
+  if (m->cmd.size() > 1) {
+    if (m->cmd[1] == "stat") {
+      mon->monmap->print_summary(ss);
+      r = 0;
+    }
+    else if (m->cmd.size() == 2 && m->cmd[1] == "getmap") {
+      mon->monmap->encode(rdata);
+      r = 0;
+      ss << "got latest monmap";
+    }
+    else if (m->cmd[1] == "injectargs" && m->cmd.size() == 4) {
+      vector<string> args(2);
+      args[0] = "_injectargs";
+      args[1] = m->cmd[3];
+      if (m->cmd[2] == "*") {
+       for (unsigned i=0; i<mon->monmap->size(); i++)
+         mon->inject_args(mon->monmap->get_inst(i), args, 0);
+       r = 0;
+       ss << "ok bcast";
+      } else {
+       errno = 0;
+       int who = strtol(m->cmd[2].c_str(), 0, 10);
+       if (!errno && who >= 0) {
+         mon->inject_args(mon->monmap->get_inst(who), args, 0);
+         r = 0;
+         ss << "ok";
+       } else 
+         ss << "specify mon number or *";
+      }
+    } 
   }
-  else {
-    mon->reply_command(m, -EINVAL, "mon already exists",
-                      rdata, paxos->get_version());
+
+  if (r != -1) {
+    string rs;
+    getline(ss, rs);
+    mon->reply_command(m, r, rs, rdata, paxos->get_version());
+    return true;
+  } else
+    return false;
+}
+
+
+bool MonmapMonitor::prepare_update(PaxosServiceMessage *m)
+{
+  dout(7) << "prepare_update " << *m << " from " << m->get_orig_source_inst() << dendl;
+  
+  switch (m->get_type()) {
+  case MSG_MON_COMMAND:
+    return prepare_command((MMonCommand*)m);
+  default:
+    assert(0);
+    delete m;
   }
-  delete message;
-  return true;
+
+  return false;
+}
+
+bool MonmapMonitor::prepare_command(MMonCommand *m)
+{
+  stringstream ss;
+  string rs;
+  int err = -EINVAL;
+  if (m->cmd.size() > 1) {
+    if (m->cmd.size() == 2 && m->cmd[1] == "add") {
+      entity_addr_t addr;
+      parse_ip_port(m->cmd[2].c_str(), addr);
+      bufferlist rdata;
+      if (pending_map.contains(addr)) {
+       err = -EEXIST;
+       ss << "mon " << addr << " already exists";
+       goto out;
+      }
+
+      pending_map.add(addr);
+      pending_map.last_changed = g_clock.now();
+      ss << "added mon " << addr;
+      getline(ss, rs);
+      paxos->wait_for_commit(new Monitor::C_Command(mon, m, 0, rs, paxos->get_version()));
+      return true;
+    }
+    else
+      ss << "unknown command " << m->cmd[1];
+  } else
+    ss << "no command?";
+  
+out:
+  getline(ss, rs);
+  mon->reply_command(m, err, rs, paxos->get_version());
+  return false;
 }
 
 bool MonmapMonitor::should_propose(double& delay)
index d8ff389e8872624eed8b354af257a2168f5e7a19..fbb7fa5d8b80f9d6e2b2d6fca1ddf935269ce804 100644 (file)
@@ -32,6 +32,7 @@ using namespace std;
 
 class MMonGetMap;
 class MMonMap;
+class MMonCommand;
 
 class MonmapMonitor : public PaxosService {
  public:
@@ -48,9 +49,12 @@ class MonmapMonitor : public PaxosService {
 
 
   bool preprocess_query(PaxosServiceMessage *m);
-
   bool prepare_update(PaxosServiceMessage *m);
 
+  bool preprocess_command(MMonCommand *m);
+  bool prepare_command(MMonCommand *m);
+
+
   /*
    * Since monitors are pretty
    * important, this implementation will just write 0.0.