From: Sage Weil Date: Wed, 14 Oct 2009 05:34:53 +0000 (-0700) Subject: mon: clean up 'mon ...' command processing, add 'mon getmap' X-Git-Tag: v0.17~44 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=3ce86d04ed7015d79df9e965872b6279e3815b4d;p=ceph.git mon: clean up 'mon ...' command processing, add 'mon getmap' --- diff --git a/src/mon/Monitor.cc b/src/mon/Monitor.cc index 3c9f314a8a14..5f5af3d9835d 100644 --- a/src/mon/Monitor.cc +++ b/src/mon/Monitor.cc @@ -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 args(2); - args[0] = "_injectargs"; - args[1] = m->cmd[3]; - if (m->cmd[2] == "*") { - for (unsigned i=0; isize(); 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) diff --git a/src/mon/MonmapMonitor.cc b/src/mon/MonmapMonitor.cc index 96adf1f002af..69e17dc5d8a7 100644 --- a/src/mon/MonmapMonitor.cc +++ b/src/mon/MonmapMonitor.cc @@ -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 args(2); + args[0] = "_injectargs"; + args[1] = m->cmd[3]; + if (m->cmd[2] == "*") { + for (unsigned i=0; imonmap->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) diff --git a/src/mon/MonmapMonitor.h b/src/mon/MonmapMonitor.h index d8ff389e8872..fbb7fa5d8b80 100644 --- a/src/mon/MonmapMonitor.h +++ b/src/mon/MonmapMonitor.h @@ -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.