]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
libcephfs/pybind: Add get_layout, get_default_pool
authorKotresh HR <khiremat@redhat.com>
Thu, 21 May 2020 11:37:00 +0000 (17:07 +0530)
committerKotresh HR <khiremat@redhat.com>
Thu, 21 May 2020 11:38:08 +0000 (17:08 +0530)
Fixes: https://tracker.ceph.com/issues/44171
Signed-off-by: Kotresh HR <khiremat@redhat.com>
src/pybind/cephfs/cephfs.pyx
src/test/pybind/test_cephfs.py

index c1244bbe53fafb803c95aa14ca0c3dbe8206bde9..9ec9fffd6a07353cfa2fe57d42b6c09c316ba3b9 100644 (file)
@@ -252,6 +252,9 @@ cdef extern from "cephfs/libcephfs.h" nogil:
     uint32_t ceph_get_cap_return_timeout(ceph_mount_info *cmount)
     void ceph_set_uuid(ceph_mount_info *cmount, const char *uuid)
     void ceph_set_session_timeout(ceph_mount_info *cmount, unsigned timeout)
+    int ceph_get_file_layout(ceph_mount_info *cmount, int fh, int *stripe_unit, int *stripe_count, int *object_size, int *pg_pool)
+    int ceph_get_file_pool_name(ceph_mount_info *cmount, int fh, char *buf, size_t buflen)
+    int ceph_get_default_data_pool_name(ceph_mount_info *cmount, char *buf, size_t buflen)
 
 
 class Error(Exception):
@@ -2586,3 +2589,81 @@ cdef class LibCephFS(object):
 
         with nogil:
             ceph_set_session_timeout(self.cluster, _timeout)
+
+    def get_layout(self, fd):
+        """
+        Set ceph client session timeout. Must be called before mount.
+
+        :param fd: file descriptor of the file/directory for which to get the layout
+        """
+
+        if not isinstance(fd, int):
+            raise TypeError('fd must be an int')
+
+        cdef:
+            int _fd = fd
+            int stripe_unit
+            int stripe_count
+            int object_size
+            int pool_id
+            char *buf = NULL
+            int buflen = 256
+            dict_result = dict()
+
+        with nogil:
+            ret = ceph_get_file_layout(self.cluster, _fd, &stripe_unit, &stripe_count, &object_size, &pool_id)
+        if ret < 0:
+            raise make_ex(stripe_unit, "error in get_file_layout")
+        dict_result["stripe_unit"] = stripe_unit
+        dict_result["stripe_count"] = stripe_count
+        dict_result["object_size"] = object_size
+        dict_result["pool_id"] = pool_id
+
+        try:
+            while True:
+                buf = <char *>realloc_chk(buf, buflen)
+                with nogil:
+                    ret = ceph_get_file_pool_name(self.cluster, _fd, buf, buflen)
+                if ret > 0:
+                    dict_result["pool_name"] = decode_cstr(buf)
+                    return dict_result
+                elif ret == -errno.ERANGE:
+                    buflen = buflen * 2
+                else:
+                    raise make_ex(ret, "error in get_file_pool_name")
+        finally:
+           free(buf)
+
+
+    def get_default_pool(self):
+        """
+        Get the default pool name and id of cephfs. This returns dict{pool_name, pool_id}.
+        """
+
+        cdef:
+            char *buf = NULL
+            int buflen = 256
+            dict_result = dict()
+
+        try:
+            while True:
+                buf = <char *>realloc_chk(buf, buflen)
+                with nogil:
+                    ret = ceph_get_default_data_pool_name(self.cluster, buf, buflen)
+                if ret > 0:
+                    dict_result["pool_name"] = decode_cstr(buf)
+                    break
+                elif ret == -errno.ERANGE:
+                    buflen = buflen * 2
+                else:
+                    raise make_ex(ret, "error in get_default_data_pool_name")
+
+            with nogil:
+                ret = ceph_get_pool_id(self.cluster, buf)
+            if ret < 0:
+                raise make_ex(ret, "error in get_pool_id")
+            dict_result["pool_id"] = ret
+            return dict_result
+
+        finally:
+           free(buf)
index c7eeb6c7985d163b5d8dbfe00e1fd9f72138334a..d1ec73b9df1aa7cfd5637288ee3573619bdf757a 100644 (file)
@@ -721,3 +721,35 @@ def test_fsetattrx():
     assert_equal(10, st1["size"])
     cephfs.close(fd)
     cephfs.unlink(b'file-fsetattrx')
+
+@with_setup(setup_test)
+def test_get_layout():
+    fd = cephfs.open(b'file-get-layout', 'w', 0o755)
+    cephfs.write(fd, b"1111", 0)
+    assert_raises(TypeError, cephfs.get_layout, "fd")
+    l_dict = cephfs.get_layout(fd)
+    assert('stripe_unit' in l_dict.keys())
+    assert('stripe_count' in l_dict.keys())
+    assert('object_size' in l_dict.keys())
+    assert('pool_id' in l_dict.keys())
+    assert('pool_name' in l_dict.keys())
+
+    cephfs.close(fd)
+    cephfs.unlink(b'file-get-layout')
+
+@with_setup(setup_test)
+def test_get_default_pool():
+    dp_dict = cephfs.get_default_pool()
+    assert('pool_id' in dp_dict.keys())
+    assert('pool_name' in dp_dict.keys())
+
+@with_setup(setup_test)
+def test_get_pool():
+    dp_dict = cephfs.get_default_pool()
+    assert('pool_id' in dp_dict.keys())
+    assert('pool_name' in dp_dict.keys())
+    assert_equal(cephfs.get_pool_id(dp_dict["pool_name"]), dp_dict["pool_id"])
+    get_rep_cnt_cmd = "ceph osd pool get " + dp_dict["pool_name"] + " size"
+    s=os.popen(get_rep_cnt_cmd).read().strip('\n')
+    size=int(s.split(" ")[-1])
+    assert_equal(cephfs.get_pool_replication(dp_dict["pool_id"]), size)