From: Kotresh HR Date: Thu, 21 May 2020 11:37:00 +0000 (+0530) Subject: libcephfs/pybind: Add get_layout, get_default_pool X-Git-Tag: v16.1.0~2070^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=7aed26ba65d3fe266d030347a4254b734f86d77b;p=ceph.git libcephfs/pybind: Add get_layout, get_default_pool Fixes: https://tracker.ceph.com/issues/44171 Signed-off-by: Kotresh HR --- diff --git a/src/pybind/cephfs/cephfs.pyx b/src/pybind/cephfs/cephfs.pyx index c1244bbe53f..9ec9fffd6a0 100644 --- a/src/pybind/cephfs/cephfs.pyx +++ b/src/pybind/cephfs/cephfs.pyx @@ -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 = 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 = 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) diff --git a/src/test/pybind/test_cephfs.py b/src/test/pybind/test_cephfs.py index c7eeb6c7985..d1ec73b9df1 100644 --- a/src/test/pybind/test_cephfs.py +++ b/src/test/pybind/test_cephfs.py @@ -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)