From f5b1b0116bcf63279fba549a69a4692dec87d0be Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 18 Jun 2021 22:23:40 +0800 Subject: [PATCH] mgr,pybind/mgr: add OSDMap.build_simple() method to ease the testing a typical session looks like: [mgr self-test eval] >>> osdmap = OSDMap.build_simple(epoch=1, num_osd=2) [mgr self-test eval] >>> osdmap Signed-off-by: Kefu Chai --- src/mgr/PyOSDMap.cc | 31 +++++++++++++++++++++++++++++++ src/pybind/mgr/ceph_module.pyi | 2 ++ src/pybind/mgr/mgr_module.py | 4 ++++ 3 files changed, 37 insertions(+) diff --git a/src/mgr/PyOSDMap.cc b/src/mgr/PyOSDMap.cc index 44be8e9423c..782b3bc6a04 100644 --- a/src/mgr/PyOSDMap.cc +++ b/src/mgr/PyOSDMap.cc @@ -276,6 +276,35 @@ static PyObject *osdmap_pool_raw_used_rate(BasePyOSDMap *self, PyObject *args) return PyFloat_FromDouble(rate); } +static PyObject *osdmap_build_simple(PyObject *cls, PyObject *args, PyObject *kwargs) +{ + static const char *kwlist[] = {"epoch", "uuid", "num_osd", nullptr}; + int epoch = 1; + char* uuid_str = nullptr; + int num_osd = -1; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "izi", + const_cast(kwlist), + &epoch, &uuid_str, &num_osd)) { + Py_RETURN_NONE; + } + uuid_d uuid; + if (uuid_str) { + if (!uuid.parse(uuid_str)) { + PyErr_Format(PyExc_ValueError, "bad uuid %s", uuid_str); + Py_RETURN_NONE; + } + } else { + uuid.generate_random(); + } + + auto osdmap = without_gil([&] { + OSDMap* osdmap = new OSDMap(); + // negative osd is allowed, in that case i just count all osds in ceph.conf + osdmap->build_simple(g_ceph_context, epoch, uuid, num_osd); + return osdmap; + }); + return construct_with_capsule("mgr_module", "OSDMap", reinterpret_cast(osdmap)); +} PyMethodDef BasePyOSDMap_methods[] = { {"_get_epoch", (PyCFunction)osdmap_get_epoch, METH_NOARGS, "Get OSDMap epoch"}, @@ -297,6 +326,8 @@ PyMethodDef BasePyOSDMap_methods[] = { "Calculate up+acting OSDs for a PG ID"}, {"_pool_raw_used_rate", (PyCFunction)osdmap_pool_raw_used_rate, METH_VARARGS, "Get raw space to logical space ratio"}, + {"_build_simple", (PyCFunction)osdmap_build_simple, METH_VARARGS | METH_CLASS, + "Create a simple OSDMap"}, {NULL, NULL, 0, NULL} }; diff --git a/src/pybind/mgr/ceph_module.pyi b/src/pybind/mgr/ceph_module.pyi index 75575aeca15..2d0260c3ddd 100644 --- a/src/pybind/mgr/ceph_module.pyi +++ b/src/pybind/mgr/ceph_module.pyi @@ -22,6 +22,8 @@ class BasePyOSDMap(object): def _map_pool_pgs_up(self, poolid):... def _pg_to_up_acting_osds(self, pool_id, ps):... def _pool_raw_used_rate(self, pool_id):... + @classmethod + def _build_simple(cls, epoch: int, uuid: Optional[str], num_osd: int) -> 'BasePyOSDMap' :... class BasePyOSDMapIncremental(object): def _get_epoch(self):... diff --git a/src/pybind/mgr/mgr_module.py b/src/pybind/mgr/mgr_module.py index 610fb4eb911..695204ee91c 100644 --- a/src/pybind/mgr/mgr_module.py +++ b/src/pybind/mgr/mgr_module.py @@ -177,6 +177,10 @@ class OSDMap(ceph_module.BasePyOSDMap): def pool_raw_used_rate(self, pool_id: int) -> float: return self._pool_raw_used_rate(pool_id) + @classmethod + def build_simple(cls, epoch: int = 1, uuid: Optional[str] = None, num_osd: int = -1): + return cls._build_simple(epoch, uuid, num_osd) + def get_ec_profile(self, name: str) -> Optional[List[Dict[str, str]]]: # FIXME: efficient implementation d = self._dump() -- 2.39.5