]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr,pybind/mgr: add OSDMap.build_simple() method
authorKefu Chai <kchai@redhat.com>
Fri, 18 Jun 2021 14:23:40 +0000 (22:23 +0800)
committerKefu Chai <kchai@redhat.com>
Fri, 18 Jun 2021 15:02:21 +0000 (23:02 +0800)
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
<mgr_module.OSDMap object at 0x7f0d201c0b00>

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/mgr/PyOSDMap.cc
src/pybind/mgr/ceph_module.pyi
src/pybind/mgr/mgr_module.py

index 44be8e9423ceeb5ad98722f29f35755ed914ee94..782b3bc6a047e418beeb8eb8622094b987eb06a0 100644 (file)
@@ -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<char**>(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<void*>(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}
 };
 
index 75575aeca150f2df17349c65f139428a5ed476b9..2d0260c3dddde3ce330896446f5f183d6998a784 100644 (file)
@@ -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):...
index 610fb4eb911d6c7f16fcc78325a78047a684556c..695204ee91c45c0e916bf3f5a51a7ea6eaf5b91d 100644 (file)
@@ -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()