From: Kefu Chai Date: Thu, 8 Feb 2018 11:01:19 +0000 (+0800) Subject: mgr: load ceph_{logger,module} globally X-Git-Tag: v13.0.2~257^2~10 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=410ce76f62c151810a61ba528f7547b8e0147d08;p=ceph.git mgr: load ceph_{logger,module} globally 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 --- diff --git a/src/mgr/PyModule.cc b/src/mgr/PyModule.cc index f3fede8de6e5..fcb0b8107679 100644 --- a/src/mgr/PyModule.cc +++ b/src/mgr/PyModule.cc @@ -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("stderr"), py_logger); + PySys_SetObject(const_cast("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 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("stderr"), py_logger); - PySys_SetObject(const_cast("stdout"), py_logger); -#endif - } - // Configure sys.path to include mgr_module_path string paths = (":" + get_site_packages() + ":" + g_conf->get_val("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); diff --git a/src/mgr/PyModule.h b/src/mgr/PyModule.h index 14f418e446ea..a25792f5a67e 100644 --- a/src/mgr/PyModule.h +++ b/src/mgr/PyModule.h @@ -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` diff --git a/src/mgr/PyModuleRegistry.cc b/src/mgr/PyModuleRegistry.cc index 867814bfa16f..82060f483f7f 100644 --- a/src/mgr/PyModuleRegistry.cc +++ b/src/mgr/PyModuleRegistry.cc @@ -57,6 +57,11 @@ int PyModuleRegistry::init(const MgrMap &map) #else Py_SetProgramName(const_cast(PYTHON_EXECUTABLE)); #endif + // Add more modules + if (g_conf->get_val("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