]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/DaemonServer: extract reply out of handle_command()
authorKefu Chai <kchai@redhat.com>
Sat, 8 Apr 2017 10:17:40 +0000 (18:17 +0800)
committerKefu Chai <kchai@redhat.com>
Mon, 10 Apr 2017 15:20:16 +0000 (23:20 +0800)
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/mgr/DaemonServer.cc
src/mgr/DaemonServer.h

index f4f301193ae6e108bfbe33180b5f8c927a213c63..8ec2dd4484f04d2e3e56cf72491dbd85273972c1 100644 (file)
@@ -398,8 +398,7 @@ bool DaemonServer::handle_command(MCommand *m)
   map<string,string> param_str_map;
 
   if (!cmdmap_from_json(m->cmd, &cmdmap, ss)) {
-    r = -EINVAL;
-    goto out;
+    return _reply(m, -EINVAL, ss.str(), odata);
   }
 
   {
@@ -441,7 +440,8 @@ bool DaemonServer::handle_command(MCommand *m)
     }
 #endif
     f.close_section(); // command_descriptions
-    goto out;
+    f.flush(odata);
+    return _reply(m, r, ss.str(), odata);
   }
 
   // lookup command
@@ -449,9 +449,7 @@ bool DaemonServer::handle_command(MCommand *m)
                                               ARRAY_SIZE(mgr_commands));
   _generate_command_map(cmdmap, param_str_map);
   if (!mgr_cmd) {
-    ss << "command not supported";
-    r = -EINVAL;
-    goto out;
+    return _reply(m, -EINVAL, "command not supported", odata);
   }
 
   // validate user's permissions for requested command
@@ -461,9 +459,7 @@ bool DaemonServer::handle_command(MCommand *m)
     audit_clog->info() << "from='" << session->inst << "' "
                       << "entity='" << session->entity_name << "' "
                       << "cmd=" << m->cmd << ":  access denied";
-    ss << "access denied";
-    r = -EACCES;
-    goto out;
+    return _reply(m, -EACCES, "access denied", odata);
   }
 
   audit_clog->debug()
@@ -483,8 +479,7 @@ bool DaemonServer::handle_command(MCommand *m)
     cmd_getval(g_ceph_context, cmdmap, "pgid", pgidstr);
     if (!pgid.parse(pgidstr.c_str())) {
       ss << "invalid pgid '" << pgidstr << "'";
-      r = -EINVAL;
-      goto out;
+      return _reply(m, -EINVAL, ss.str(), odata);
     }
     bool pg_exists = false;
     cluster_state.with_osdmap([&](const OSDMap& osdmap) {
@@ -492,8 +487,7 @@ bool DaemonServer::handle_command(MCommand *m)
       });
     if (!pg_exists) {
       ss << "pg " << pgid << " dne";
-      r = -ENOENT;
-      goto out;
+      return _reply(m, -ENOENT, ss.str(), odata);
     }
     int acting_primary = -1;
     entity_inst_t inst;
@@ -505,8 +499,7 @@ bool DaemonServer::handle_command(MCommand *m)
       });
     if (acting_primary == -1) {
       ss << "pg " << pgid << " has no primary osd";
-      r = -EAGAIN;
-      goto out;
+      return _reply(m, -EAGAIN, ss.str(), odata);
     }
     vector<pg_t> pgs = { pgid };
     msgr->send_message(new MOSDScrub(monc->get_fsid(),
@@ -516,10 +509,8 @@ bool DaemonServer::handle_command(MCommand *m)
                       inst);
     ss << "instructing pg " << pgid << " on osd." << acting_primary
        << " (" << inst << ") to " << scrubop;
-    r = 0;
-  }
-
-  else {
+    return _reply(m, 0, ss.str(), odata);
+  } else {
     cluster_state.with_pgmap(
       [&](const PGMap& pg_map) {
        cluster_state.with_osdmap([&](const OSDMap& osdmap) {
@@ -528,9 +519,10 @@ bool DaemonServer::handle_command(MCommand *m)
          });
       });
   }
-
+  if (r != -EOPNOTSUPP)
+      return _reply(m, r, ss.str(), odata);
   // fall back to registered python handlers
-  if (r == -EOPNOTSUPP) {
+  else {
     // Let's find you a handler!
     MgrPyModule *handler = nullptr;
     auto py_commands = py_modules.get_commands();
@@ -546,8 +538,7 @@ bool DaemonServer::handle_command(MCommand *m)
     if (handler == nullptr) {
       ss << "No handler found for '" << prefix << "'";
       dout(4) << "No handler found for '" << prefix << "'" << dendl;
-      r = -EINVAL;
-      goto out;
+      return _reply(m, -EINVAL, ss.str(), odata);
     }
 
     // FIXME: go run this python part in another thread, not inline
@@ -557,27 +548,29 @@ bool DaemonServer::handle_command(MCommand *m)
     stringstream ds;
     r = handler->handle_command(cmdmap, &ds, &ss);
     odata.append(ds);
-    goto out;
-  }
-
- out:
-
-  // Let the connection drop as soon as we've sent our response
-  if (m->get_connection()) {
-    m->get_connection()->mark_disposable();
+    return _reply(m, 0, ss.str(), odata);
   }
+}
 
-  std::string rs;
-  rs = ss.str();
-  dout(1) << "do_command r=" << r << " " << rs << dendl;
-  if (con) {
-    MCommandReply *reply = new MCommandReply(r, rs);
-    reply->set_tid(m->get_tid());
-    reply->set_data(odata);
-    con->send_message(reply);
+bool DaemonServer::_reply(MCommand* m,
+                         int ret,
+                         const std::string& s,
+                         const bufferlist& payload)
+{
+  dout(1) << __func__ << " r=" << ret << " " << s << dendl;
+  auto con = m->get_connection();
+  if (!con) {
+    dout(10) << __func__ << " connection dropped for command" << dendl;
+    m->put();
+    return true;
   }
+  // Let the connection drop as soon as we've sent our response
+  con->mark_disposable();
 
+  auto response = new MCommandReply(ret, s);
+  response->set_tid(m->get_tid());
+  response->set_data(payload);
+  con->send_message(response);
   m->put();
   return true;
 }
-
index 4337d727dd59b98d9941d0539ea2db3712da7c69..4b4bafd0a9d42c667d3b03dfdd8df3ee603b7d04 100644 (file)
@@ -73,6 +73,10 @@ protected:
     const map<string,string>& param_str_map,
     const MgrCommand *this_cmd);
 
+private:
+  bool _reply(MCommand* m,
+             int ret, const std::string& s, const bufferlist& payload);
+
 public:
   int init(uint64_t gid, entity_addr_t client_addr);
   void shutdown();