]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr: load ceph_{logger,module} globally
authorKefu Chai <kchai@redhat.com>
Thu, 8 Feb 2018 11:01:19 +0000 (19:01 +0800)
committerKefu Chai <kchai@redhat.com>
Sun, 11 Feb 2018 06:12:15 +0000 (14:12 +0800)
no need to add them for each sub-interpreter. extensions are shared
among them.

Fixes: http://tracker.ceph.com/issues/22880
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/mgr/PyModule.cc
src/mgr/PyModule.h
src/mgr/PyModuleRegistry.cc

index f3fede8de6e50386d8c4301ce34f09337f173ca5..fcb0b8107679fb06b6478526d4ca2374a2a95e3c 100644 (file)
@@ -151,12 +151,75 @@ std::string PyModule::get_site_packages()
   return site_packages.str();
 }
 
+#if PY_MAJOR_VERSION >= 3
+PyObject* PyModule::init_ceph_logger()
+{
+  auto py_logger = PyModule_Create(&ceph_logger_module);
+  PySys_SetObject("stderr", py_logger);
+  PySys_SetObject("stdout", py_logger);
+  return py_logger;
+}
+#else
+void PyModule::init_ceph_logger()
+{
+  auto py_logger = Py_InitModule("ceph_logger", log_methods);
+  PySys_SetObject(const_cast<char*>("stderr"), py_logger);
+  PySys_SetObject(const_cast<char*>("stdout"), py_logger);
+}
+#endif
+
+#if PY_MAJOR_VERSION >= 3
+PyObject* PyModule::init_ceph_module()
+#else
+void PyModule::init_ceph_module()
+#endif
+{
+  static PyMethodDef module_methods[] = {
+    {nullptr, nullptr, 0, nullptr}
+  };
+#if PY_MAJOR_VERSION >= 3
+  static PyModuleDef ceph_module_def = {
+    PyModuleDef_HEAD_INIT,
+    "ceph_module",
+    nullptr,
+    -1,
+    module_methods,
+    nullptr,
+    nullptr,
+    nullptr,
+    nullptr
+  };
+  PyObject *ceph_module = PyModule_Create(&ceph_module_def);
+#else
+  PyObject *ceph_module = Py_InitModule("ceph_module", module_methods);
+#endif
+  assert(ceph_module != nullptr);
+  std::map<const char*, PyTypeObject*> classes{
+    {{"BaseMgrModule", &BaseMgrModuleType},
+     {"BaseMgrStandbyModule", &BaseMgrStandbyModuleType},
+     {"BasePyOSDMap", &BasePyOSDMapType},
+     {"BasePyOSDMapIncremental", &BasePyOSDMapIncrementalType},
+     {"BasePyCRUSH", &BasePyCRUSHType}}
+  };
+  for (auto [name, type] : classes) {
+    type->tp_new = PyType_GenericNew;
+    if (PyType_Ready(type) < 0) {
+      assert(0);
+    }
+    Py_INCREF(type);
+
+    PyModule_AddObject(ceph_module, name, (PyObject *)type);
+  }
+#if PY_MAJOR_VERSION >= 3
+  return ceph_module;
+#endif
+}
 
 int PyModule::load(PyThreadState *pMainThreadState)
 {
   assert(pMainThreadState != nullptr);
 
-  // Configure sub-interpreter and construct C++-generated python classes
+  // Configure sub-interpreter
   {
     SafeThreadState sts(pMainThreadState);
     Gil gil(sts);
@@ -176,19 +239,6 @@ int PyModule::load(PyThreadState *pMainThreadState)
       const char *argv[] = {"ceph-mgr"};
       PySys_SetArgv(1, (char**)argv);
 #endif
-
-      if (g_conf->daemonize) {
-#if PY_MAJOR_VERSION >= 3
-        auto py_logger = PyModule_Create(&ceph_logger_module);
-        PySys_SetObject("stderr", py_logger);
-        PySys_SetObject("stdout", py_logger);
-#else
-        auto py_logger = Py_InitModule("ceph_logger", log_methods);
-        PySys_SetObject(const_cast<char*>("stderr"), py_logger);
-        PySys_SetObject(const_cast<char*>("stdout"), py_logger);
-#endif
-      }
-
       // Configure sys.path to include mgr_module_path
       string paths = (":" + get_site_packages() +
                      ":" + g_conf->get_val<std::string>("mgr_module_path"));
@@ -203,46 +253,7 @@ int PyModule::load(PyThreadState *pMainThreadState)
       dout(10) << "Computed sys.path '" << sys_path << "'" << dendl;
 #endif
     }
-
-    static PyMethodDef module_methods[] = {
-      {nullptr}
-    };
-#if PY_MAJOR_VERSION >= 3
-    static PyModuleDef ceph_module_def = {
-      PyModuleDef_HEAD_INIT,
-      "ceph_module",
-      nullptr,
-      -1,
-      module_methods,
-    };
-#endif
-
-    // Initialize module
-#if PY_MAJOR_VERSION >= 3
-    PyObject *ceph_module = PyModule_Create(&ceph_module_def);
-#else
-    PyObject *ceph_module = Py_InitModule("ceph_module", module_methods);
-#endif
-    assert(ceph_module != nullptr);
-
-    auto load_class = [ceph_module](const char *name, PyTypeObject *type)
-    {
-      type->tp_new = PyType_GenericNew;
-      if (PyType_Ready(type) < 0) {
-          assert(0);
-      }
-      Py_INCREF(type);
-
-      PyModule_AddObject(ceph_module, name, (PyObject *)type);
-    };
-
-    load_class("BaseMgrModule", &BaseMgrModuleType);
-    load_class("BaseMgrStandbyModule", &BaseMgrStandbyModuleType);
-    load_class("BasePyOSDMap", &BasePyOSDMapType);
-    load_class("BasePyOSDMapIncremental", &BasePyOSDMapIncrementalType);
-    load_class("BasePyCRUSH", &BasePyCRUSHType);
   }
-
   // Environment is all good, import the external module
   {
     Gil gil(pMyThreadState);
index 14f418e446eab46e3999521d82c22b384957f88e..a25792f5a67e5c7d69d0a74997271cc55d25234e 100644 (file)
@@ -77,7 +77,13 @@ public:
   ~PyModule();
 
   int load(PyThreadState *pMainThreadState);
-
+#if PY_MAJOR_VERSION >= 3
+  static PyObject* init_ceph_logger();
+  static PyObject* init_ceph_module();
+#else
+  static void init_ceph_logger();
+  static void init_ceph_module();
+#endif
 
   /**
    * Extend `out` with the contents of `this->commands`
index 867814bfa16f146fb4bb2f141d04091d2534c7a0..82060f483f7f0932850d6d43958c476bc0772f3c 100644 (file)
@@ -57,6 +57,11 @@ int PyModuleRegistry::init(const MgrMap &map)
 #else
   Py_SetProgramName(const_cast<char*>(PYTHON_EXECUTABLE));
 #endif
+  // Add more modules
+  if (g_conf->get_val<bool>("daemonize")) {
+    PyImport_AppendInittab("ceph_logger", PyModule::init_ceph_logger);
+  }
+  PyImport_AppendInittab("ceph_module", PyModule::init_ceph_module);
   Py_InitializeEx(0);
 
   // Let CPython know that we will be calling it back from other