Skip to content

Commit

Permalink
libcephfs/pybind: Add get_layout, get_default_pool
Browse files Browse the repository at this point in the history
Fixes: https://tracker.ceph.com/issues/44171
Signed-off-by: Kotresh HR <khiremat@redhat.com>
  • Loading branch information
kotreshhr committed Mar 10, 2020
1 parent aa1f222 commit 075289b
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/pybind/cephfs/cephfs.pyx
Expand Up @@ -256,6 +256,9 @@ cdef extern from "cephfs/libcephfs.h" nogil:
void ceph_set_session_timeout(ceph_mount_info *cmount, unsigned timeout)
int ceph_start_reclaim(ceph_mount_info *cmount, const char *uuid, unsigned flags)
void ceph_finish_reclaim(ceph_mount_info *cmount)
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):
Expand Down Expand Up @@ -2663,3 +2666,81 @@ cdef class LibCephFS(object):

with nogil:
ceph_finish_reclaim(self.cluster)

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)
29 changes: 29 additions & 0 deletions src/test/pybind/test_cephfs.py
Expand Up @@ -740,3 +740,32 @@ 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"])
assert_greater(cephfs.get_pool_replication(dp_dict["pool_id"]), 1)

0 comments on commit 075289b

Please sign in to comment.