]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr: enable python modules to advertise their service URI
authorJohn Spray <john.spray@redhat.com>
Thu, 27 Jul 2017 15:50:23 +0000 (11:50 -0400)
committerJohn Spray <john.spray@redhat.com>
Wed, 1 Nov 2017 12:20:20 +0000 (08:20 -0400)
Fixes: http://tracker.ceph.com/issues/17460
Signed-off-by: John Spray <john.spray@redhat.com>
src/mgr/Mgr.cc
src/mgr/Mgr.h
src/mgr/MgrPyModule.h
src/mgr/MgrStandby.cc
src/mgr/PyModules.cc
src/mgr/PyModules.h
src/mgr/PyState.cc
src/pybind/mgr/mgr_module.py

index de60b4c771fa95bffdd43c936e2643d78aa8b603..782710c9ae2e90a6db7b289c68248f35ad70a43e 100644 (file)
@@ -658,3 +658,10 @@ std::vector<MonCommand> Mgr::get_command_set() const
   return commands;
 }
 
+std::map<std::string, std::string> Mgr::get_services() const
+{
+  Mutex::Locker l(lock);
+
+  return py_modules.get_services();
+}
+
index 68f2b40b4616d62fdfef22f7f7fad093234586c1..e37b1b9bee6f6020d583e5566aa7aad334539ea7 100644 (file)
@@ -100,6 +100,7 @@ public:
   void shutdown();
 
   std::vector<MonCommand> get_command_set() const;
+  std::map<std::string, std::string> get_services() const;
 };
 
 #endif
index 232d4ebac9233e1432e97cedfabd5f77652be4c9..bb1dc5b62146762d0da888736cc1a63f306eddd8 100644 (file)
@@ -57,6 +57,9 @@ private:
 
   int load_commands();
 
+  // Optional, URI exposed by plugins that implement serve()
+  std::string uri;
+
 public:
   MgrPyModule(const std::string &module_name, const std::string &sys_path, PyThreadState *main_ts);
   ~MgrPyModule();
@@ -86,6 +89,16 @@ public:
     health_checks = std::move(c);
   }
   void get_health_checks(health_check_map_t *checks);
+
+  void set_uri(const std::string &str)
+  {
+    uri = str;
+  }
+
+  std::string get_uri() const
+  {
+    return uri;
+  }
 };
 
 std::string handle_pyerror();
index 9ea5276ad99b33bc4794b9ff69556011f7df6f3e..3fb663a7db92cb7fd0487518469fbd29db486cf0 100644 (file)
@@ -172,13 +172,17 @@ void MgrStandby::send_beacon()
                                 modules,
                                 std::move(metadata));
 
-  if (available && !available_in_map) {
-    // We are informing the mon that we are done initializing: inform
-    // it of our command set.  This has to happen after init() because
-    // it needs the python modules to have loaded.
-    m->set_command_descs(active_mgr->get_command_set());
-    dout(4) << "going active, including " << m->get_command_descs().size()
-            << " commands in beacon" << dendl;
+  if (available) {
+    if (!available_in_map) {
+      // We are informing the mon that we are done initializing: inform
+      // it of our command set.  This has to happen after init() because
+      // it needs the python modules to have loaded.
+      m->set_command_descs(active_mgr->get_command_set());
+      dout(4) << "going active, including " << m->get_command_descs().size()
+              << " commands in beacon" << dendl;
+    }
+
+    m->set_services(active_mgr->get_services());
   }
                                  
   monc.send_mon_message(m);
index 87d4b873372c8587d8bdc05db3f75ae110d67d57..401ec504a10a1988de8fe12e69e59de524849497 100644 (file)
@@ -667,6 +667,22 @@ std::vector<MonCommand> PyModules::get_commands() const
   return result;
 }
 
+
+std::map<std::string, std::string> PyModules::get_services() const
+{
+  std::map<std::string, std::string> result;
+  Mutex::Locker l(lock);
+  for (const auto& i : modules) {
+    const auto &module = i.second.get();
+    std::string svc_str = module->get_uri();
+    if (!svc_str.empty()) {
+      result[module->get_name()] = svc_str;
+    }
+  }
+
+  return result;
+}
+
 void PyModules::insert_config(const std::map<std::string,
                               std::string> &new_config)
 {
@@ -865,3 +881,14 @@ void PyModules::get_health_checks(health_check_map_t *checks)
     p.second->get_health_checks(checks);
   }
 }
+
+void PyModules::set_uri(const std::string& module_name,
+                        const std::string &uri)
+{
+  Mutex::Locker l(lock);
+
+  dout(4) << " module " << module_name << " set URI '" << uri << "'" << dendl;
+
+  modules[module_name]->set_uri(uri);
+}
+
index 521ee757db0f8cbfc8cc5205c0616baa5946cf5f..17553541ee5ad1b40ea30787a43a1f31d1fb42ca 100644 (file)
@@ -92,6 +92,8 @@ public:
                         health_check_map_t&& checks);
   void get_health_checks(health_check_map_t *checks);
 
+  void set_uri(const std::string& module_name, const std::string &uri);
+
   void log(const std::string &module_name,
            int level, const std::string &record);
 
@@ -105,6 +107,8 @@ public:
 
   void insert_config(const std::map<std::string, std::string> &new_config);
 
+  std::map<std::string, std::string> get_services() const;
+
   // Public so that MonCommandCompletion can use it
   // FIXME: for send_command completion notifications,
   // send it to only the module that sent the command, not everyone
index d919d5af1fa099cbac95b660a4ca28001946c3b2..a0112c5e49967614e16bd755a4fbdbd210fd7e51 100644 (file)
@@ -460,13 +460,28 @@ get_perf_schema(BaseMgrModule *self, PyObject *args)
   return self->py_modules->get_perf_schema_python(type_str, svc_id);
 }
 
-
 static PyObject *
 ceph_get_osdmap(BaseMgrModule *self, PyObject *args)
 {
   return self->py_modules->get_osdmap();
 }
 
+static PyObject*
+ceph_set_uri(BaseMgrModule *self, PyObject *args)
+{
+  char *svc_str = nullptr;
+  if (!PyArg_ParseTuple(args, "s:ceph_advertize_service",
+        &svc_str)) {
+    return nullptr;
+  }
+
+  // We call down into PyModules even though we have a MgrPyModule
+  // reference here, because MgrPyModule's fields are protected
+  // by PyModules' lock.
+  self->py_modules->set_uri(self->this_module->get_name(), svc_str);
+
+  Py_RETURN_NONE;
+}
 
 
 PyMethodDef BaseMgrModule_methods[] = {
@@ -518,6 +533,9 @@ PyMethodDef BaseMgrModule_methods[] = {
   {"_ceph_get_osdmap", (PyCFunction)ceph_get_osdmap, METH_NOARGS,
     "Get an OSDMap* in a python capsule"},
 
+  {"_ceph_set_uri", (PyCFunction)ceph_set_uri, METH_VARARGS,
+    "Advertize a service URI served by this module"},
+
   {NULL, NULL, 0, NULL}
 };
 
index fdcd24a92030cb2926b5596fa25be5dcdb433c38..aaad609e6192f0c59036a41475c1c3ee9bc45128 100644 (file)
@@ -433,7 +433,7 @@ class MgrModule(ceph_module.BaseMgrModule):
         OSDMap.
         :return: OSDMap
         """
-        return OSDMap(ceph_state.get_osdmap())
+        return OSDMap(self._ceph_get_osdmap())
 
     def get_all_perf_counters(self, prio_limit=PRIO_USEFUL):
         """
@@ -491,3 +491,12 @@ class MgrModule(ceph_module.BaseMgrModule):
         self.log.debug("returning {0} counter".format(len(result)))
 
         return result
+
+    def set_uri(self, uri):
+        """
+        If the module exposes a service, then call this to publish the
+        address once it is available.
+
+        :return: a string
+        """
+        return self._ceph_set_uri(uri)