From: Josh Durgin Date: Fri, 13 Nov 2015 03:32:42 +0000 (-0800) Subject: Merge branch 'pybind3' of https://github.com/dcoles/ceph into wip-pybind3 X-Git-Tag: v10.0.1~80^2~7 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=b7de34bc7e4237fd407dc6c0d714deec026d0594;p=ceph.git Merge branch 'pybind3' of https://github.com/dcoles/ceph into wip-pybind3 pybind: Add Python 3 support for rados and rbd modules Reviewed-by: Josh Durgin Conflicts: src/pybind/rbd.py (new create args, minor fix to work with py3) --- b7de34bc7e4237fd407dc6c0d714deec026d0594 diff --cc src/pybind/rbd.py index 8ff074a9ff89,ca45534511e4..78860c8c0d23 --- a/src/pybind/rbd.py +++ b/src/pybind/rbd.py @@@ -65,13 -65,17 +65,23 @@@ RBD_FEATURES_SINGLE_CLIENT = (RBD_FEATU RBD_FLAG_OBJECT_MAP_INVALID = 1 +RBD_IMAGE_OPTION_FORMAT = 0 +RBD_IMAGE_OPTION_FEATURES = 1 +RBD_IMAGE_OPTION_ORDER = 2 +RBD_IMAGE_OPTION_STRIPE_UNIT = 3 +RBD_IMAGE_OPTION_STRIPE_COUNT = 4 + + # Are we running Python 2.x + _python2 = sys.hexversion < 0x03000000 + + + if _python2: + str_type = basestring + else: + str_type = str + + class Error(Exception): pass @@@ -268,33 -271,8 +278,33 @@@ class RBD(object) if (stripe_unit != 0 or stripe_count != 0) and not has_create3: raise FunctionNotSupported('installed version of librbd does' ' not support stripe unit or count') - if has_create3: + if has_create4: + format = old_format and 1 or 2 + opts = c_void_p() + self.librbd.rbd_image_options_create(byref(opts)) + self.librbd.rbd_image_options_set_uint64(opts, + RBD_IMAGE_OPTION_FORMAT, + c_uint64(format)) + self.librbd.rbd_image_options_set_uint64(opts, + RBD_IMAGE_OPTION_FEATURES, + c_uint64(features)) + self.librbd.rbd_image_options_set_uint64(opts, + RBD_IMAGE_OPTION_ORDER, + c_uint64(order)) + self.librbd.rbd_image_options_set_uint64(opts, + RBD_IMAGE_OPTION_STRIPE_UNIT, + c_uint64(stripe_unit)) + self.librbd.rbd_image_options_set_uint64(opts, + RBD_IMAGE_OPTION_STRIPE_COUNT, + c_uint64(stripe_count)) - ret = self.librbd.rbd_create4(ioctx.io, c_char_p(name), ++ ret = self.librbd.rbd_create4(ioctx.io, cstr(name), + c_uint64(size), opts) + self.librbd.rbd_image_options_get_uint64(opts, + RBD_IMAGE_OPTION_ORDER, + byref(c_uint64(order))) + self.librbd.rbd_image_options_destroy(opts) + elif has_create3: - ret = self.librbd.rbd_create3(ioctx.io, c_char_p(name), + ret = self.librbd.rbd_create3(ioctx.io, cstr(name), c_uint64(size), c_uint64(features), byref(c_int(order)), @@@ -339,44 -313,16 +349,44 @@@ """ if order is None: order = 0 - if not isinstance(p_snapname, str) or not isinstance(p_name, str): + if not isinstance(p_snapname, str_type) or not isinstance(p_name, str_type): raise TypeError('parent name and snapname must be strings') - if not isinstance(c_name, str): + if not isinstance(c_name, str_type): raise TypeError('child name must be a string') - ret = self.librbd.rbd_clone(p_ioctx.io, cstr(p_name), - cstr(p_snapname), - c_ioctx.io, cstr(c_name), - c_uint64(features), - byref(c_int(order))) + has_clone3 = hasattr(self.librbd, 'rbd_clone3') + if (stripe_unit != 0 or stripe_count != 0) and not has_clone3: + raise FunctionNotSupported('installed version of librbd does' + ' not support stripe unit or count') + if has_clone3: + opts = c_void_p() + self.librbd.rbd_image_options_create(byref(opts)) + self.librbd.rbd_image_options_set_uint64(opts, + RBD_IMAGE_OPTION_FEATURES, + c_uint64(features)) + self.librbd.rbd_image_options_set_uint64(opts, + RBD_IMAGE_OPTION_ORDER, + c_uint64(order)) + self.librbd.rbd_image_options_set_uint64(opts, + RBD_IMAGE_OPTION_STRIPE_UNIT, + c_uint64(stripe_unit)) + self.librbd.rbd_image_options_set_uint64(opts, + RBD_IMAGE_OPTION_STRIPE_COUNT, + c_uint64(stripe_count)) - ret = self.librbd.rbd_clone3(p_ioctx.io, c_char_p(p_name), - c_char_p(p_snapname), - c_ioctx.io, c_char_p(c_name), ++ ret = self.librbd.rbd_clone3(p_ioctx.io, cstr(p_name), ++ cstr(p_snapname), ++ c_ioctx.io, cstr(c_name), + opts) + self.librbd.rbd_image_options_get_uint64(opts, + RBD_IMAGE_OPTION_ORDER, + byref(c_uint64(order))) + self.librbd.rbd_image_options_destroy(opts) + else: - ret = self.librbd.rbd_clone(p_ioctx.io, c_char_p(p_name), - c_char_p(p_snapname), - c_ioctx.io, c_char_p(c_name), ++ ret = self.librbd.rbd_clone(p_ioctx.io, cstr(p_name), ++ cstr(p_snapname), ++ c_ioctx.io, cstr(c_name), + c_uint64(features), + byref(c_int(order))) if ret < 0: raise make_ex(ret, 'error creating clone') @@@ -705,51 -651,11 +716,51 @@@ class Image(object) :type dest_ioctx: :class:`rados.Ioctx` :param dest_name: the name of the copy :type dest_name: str + :param features: bitmask of features to enable; if set, must include layering + :type features: int + :param order: the image is split into (2**order) byte objects + :type order: int + :param stripe_unit: stripe unit in bytes (default 0 for object size) + :type stripe_unit: int + :param stripe_count: objects to stripe over before looping + :type stripe_count: int + :raises: :class:`TypeError` + :raises: :class:`InvalidArgument` :raises: :class:`ImageExists` + :raises: :class:`FunctionNotSupported` + :raises: :class:`ArgumentOutOfRange` """ + if order is None: + order = 0 - if not isinstance(dest_name, str): + if not isinstance(dest_name, str_type): raise TypeError('dest_name must be a string') - ret = self.librbd.rbd_copy(self.image, dest_ioctx.io, cstr(dest_name)) + has_copy3 = hasattr(self.librbd, 'rbd_copy3') + if (stripe_unit != 0 or stripe_count != 0) and not has_copy3: + raise FunctionNotSupported('installed version of librbd does' + ' not support stripe unit or count') + if has_copy3: + opts = c_void_p() + self.librbd.rbd_image_options_create(byref(opts)) + self.librbd.rbd_image_options_set_uint64(opts, + RBD_IMAGE_OPTION_FEATURES, + c_uint64(features)) + self.librbd.rbd_image_options_set_uint64(opts, + RBD_IMAGE_OPTION_ORDER, + c_uint64(order)) + self.librbd.rbd_image_options_set_uint64(opts, + RBD_IMAGE_OPTION_STRIPE_UNIT, + c_uint64(stripe_unit)) + self.librbd.rbd_image_options_set_uint64(opts, + RBD_IMAGE_OPTION_STRIPE_COUNT, + c_uint64(stripe_count)) + ret = self.librbd.rbd_copy3(self.image, dest_ioctx.io, - c_char_p(dest_name), opts) ++ cstr(dest_name), opts) + self.librbd.rbd_image_options_get_uint64(opts, + RBD_IMAGE_OPTION_ORDER, + byref(c_uint64(order))) + self.librbd.rbd_image_options_destroy(opts) + else: - ret = self.librbd.rbd_copy(self.image, dest_ioctx.io, c_char_p(dest_name)) ++ ret = self.librbd.rbd_copy(self.image, dest_ioctx.io, cstr(dest_name)) if ret < 0: raise make_ex(ret, 'error copying image %s to %s' % (self.name, dest_name)) diff --cc src/test/pybind/test_rbd.py index 1e8d95c1bfc9,1498524ee9b6..9320d43295a8 --- a/src/test/pybind/test_rbd.py +++ b/src/test/pybind/test_rbd.py @@@ -300,29 -298,11 +298,29 @@@ class TestImage(object) self.image.update_features(RBD_FEATURE_EXCLUSIVE_LOCK, True) eq(features | RBD_FEATURE_EXCLUSIVE_LOCK, self.image.features()) + @require_features([RBD_FEATURE_STRIPINGV2]) + def test_create_with_params(self): + global features + image_name = get_temp_image_name() + order = 20 + stripe_unit = 1 << 20 + stripe_count = 10 + self.rbd.create(ioctx, image_name, IMG_SIZE, order, + False, features, stripe_unit, stripe_count) + image = Image(ioctx, image_name) + info = image.stat() + check_stat(info, IMG_SIZE, order) + eq(image.features(), features) + eq(image.stripe_unit(), stripe_unit) + eq(image.stripe_count(), stripe_count) + image.close() + RBD().remove(ioctx, image_name) + def test_invalidate_cache(self): - self.image.write('abc', 0) - eq('abc', self.image.read(0, 3)) + self.image.write(b'abc', 0) + eq(b'abc', self.image.read(0, 3)) self.image.invalidate_cache() - eq('abc', self.image.read(0, 3)) + eq(b'abc', self.image.read(0, 3)) def test_stat(self): info = self.image.stat() @@@ -392,27 -372,26 +390,27 @@@ self.image.remove_snap('snap2') def test_resize_down(self): - new_size = IMG_SIZE / 2 + new_size = IMG_SIZE // 2 data = rand_data(256) - self.image.write(data, IMG_SIZE / 2); + self.image.write(data, IMG_SIZE // 2); self.image.resize(new_size) self.image.resize(IMG_SIZE) - read = self.image.read(IMG_SIZE / 2, 256) - eq('\0' * 256, read) + read = self.image.read(IMG_SIZE // 2, 256) + eq(b'\0' * 256, read) def test_resize_bytes(self): - new_size = IMG_SIZE / 2 - 5 + new_size = IMG_SIZE // 2 - 5 data = rand_data(256) - self.image.write(data, IMG_SIZE / 2 - 10); + self.image.write(data, IMG_SIZE // 2 - 10); self.image.resize(new_size) self.image.resize(IMG_SIZE) - read = self.image.read(IMG_SIZE / 2 - 10, 5) + read = self.image.read(IMG_SIZE // 2 - 10, 5) eq(data[:5], read) - read = self.image.read(IMG_SIZE / 2 - 5, 251) - eq('\0' * 251, read) + read = self.image.read(IMG_SIZE // 2 - 5, 251) + eq(b'\0' * 251, read) - def test_copy(self): + def _test_copy(self, features=None, order=None, stripe_unit=None, + stripe_count=None): global ioctx data = rand_data(256) self.image.write(data, 256)