-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
python-rados: Add object lock support
This change adds to the python binding the support of: - rados_lock_exclusive - rados_lock_shared - rados_unlock http://tracker.ceph.com/issues/6114 Refs: #6114 Signed-off-by: Mehdi Abaakouk <sileht@redhat.com>
- Loading branch information
Showing
2 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
""" | ||
from ctypes import CDLL, c_char_p, c_size_t, c_void_p, c_char, c_int, c_long, \ | ||
c_ulong, create_string_buffer, byref, Structure, c_uint64, c_ubyte, \ | ||
pointer, CFUNCTYPE, c_int64 | ||
pointer, CFUNCTYPE, c_int64, c_uint8 | ||
from ctypes.util import find_library | ||
import ctypes | ||
import errno | ||
|
@@ -41,6 +41,10 @@ class ObjectExists(Error): | |
""" `ObjectExists` class, derived from `Error` """ | ||
pass | ||
|
||
class ObjectBusy(Error): | ||
""" `ObjectBusy` class, derived from `Error` """ | ||
pass | ||
|
||
class IOError(Error): | ||
""" `IOError` class, derived from `Error` """ | ||
pass | ||
|
@@ -90,6 +94,7 @@ def make_ex(ret, msg): | |
errno.EIO : IOError, | ||
errno.ENOSPC : NoSpace, | ||
errno.EEXIST : ObjectExists, | ||
errno.EBUSY : ObjectBusy, | ||
errno.ENODATA : NoData, | ||
errno.EINTR : InterruptedOrTimeoutError, | ||
errno.ETIMEDOUT : TimedOut | ||
|
@@ -122,6 +127,10 @@ class rados_cluster_stat_t(Structure): | |
("kb_avail", c_uint64), | ||
("num_objects", c_uint64)] | ||
|
||
class timeval(Structure): | ||
_fields_ = [("tv_sec", c_long), ("tv_usec", c_long)] | ||
|
||
|
||
class Version(object): | ||
""" Version information """ | ||
def __init__(self, major, minor, extra): | ||
|
@@ -1777,6 +1786,126 @@ def get_last_version(self): | |
self.require_ioctx_open() | ||
return run_in_thread(self.librados.rados_get_last_version, (self.io,)) | ||
|
||
def lock_exclusive(self, key, name, cookie, desc="", duration=None, flags=0): | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
tchaikov
Contributor
|
||
|
||
""" | ||
Take an exclusive lock on an object | ||
:param key: name of the object | ||
:type key: str | ||
:param name: name of the lock | ||
:type name: str | ||
:param cookie: cookie of the lock | ||
:type cookie: str | ||
:param desc: description of the lock | ||
:type desc: str | ||
:param duration: duration of the lock in seconds | ||
:type duration: int | ||
:param flags: flags | ||
:type flags: int | ||
:raises: :class:`TypeError` | ||
:raises: :class:`Error` | ||
""" | ||
self.require_ioctx_open() | ||
if not isinstance(key, str): | ||
raise TypeError('key must be a string') | ||
if not isinstance(name, str): | ||
raise TypeError('name must be a string') | ||
if not isinstance(cookie, str): | ||
raise TypeError('cookie must be a string') | ||
if not isinstance(desc, str): | ||
raise TypeError('desc must be a string') | ||
if duration is not None and not isinstance(duration, int): | ||
raise TypeError('duration must be a integer') | ||
if not isinstance(flags, int): | ||
raise TypeError('flags must be a integer') | ||
|
||
ret = run_in_thread(self.librados.rados_lock_exclusive, | ||
(self.io, c_char_p(key), c_char_p(name), c_char_p(cookie), | ||
c_char_p(desc), | ||
timeval(duration, None) if duration is None else None, | ||
c_uint8(flags))) | ||
if ret < 0: | ||
raise make_ex(ret, "Ioctx.rados_lock_exclusive(%s): failed to set lock %s on %s" % (self.name, name, key)) | ||
|
||
def lock_shared(self, key, name, cookie, tag, desc="", duration=None, flags=0): | ||
|
||
""" | ||
Take a shared lock on an object | ||
:param key: name of the object | ||
:type key: str | ||
:param name: name of the lock | ||
:type name: str | ||
:param cookie: cookie of the lock | ||
:type cookie: str | ||
:param tag: tag of the lock | ||
:type tag: str | ||
:param desc: description of the lock | ||
:type desc: str | ||
:param duration: duration of the lock in seconds | ||
:type duration: int | ||
:param flags: flags | ||
:type flags: int | ||
:raises: :class:`TypeError` | ||
:raises: :class:`Error` | ||
""" | ||
self.require_ioctx_open() | ||
if not isinstance(key, str): | ||
raise TypeError('key must be a string') | ||
if not isinstance(name, str): | ||
raise TypeError('name must be a string') | ||
if not isinstance(cookie, str): | ||
raise TypeError('cookie must be a string') | ||
if not isinstance(tag, str): | ||
raise TypeError('tag must be a string') | ||
if not isinstance(desc, str): | ||
raise TypeError('desc must be a string') | ||
if duration is not None and not isinstance(duration, int): | ||
raise TypeError('duration must be a integer') | ||
if not isinstance(flags, int): | ||
raise TypeError('flags must be a integer') | ||
|
||
ret = run_in_thread(self.librados.rados_lock_shared, | ||
(self.io, c_char_p(key), c_char_p(name), c_char_p(cookie), | ||
c_char_p(tag), c_char_p(desc), | ||
timeval(duration, None) if duration is None else None, | ||
c_uint8(flags))) | ||
if ret < 0: | ||
raise make_ex(ret, "Ioctx.rados_lock_exclusive(%s): failed to set lock %s on %s" % (self.name, name, key)) | ||
|
||
def unlock(self, key, name, cookie): | ||
|
||
""" | ||
Release a shared or exclusive lock on an object | ||
:param key: name of the object | ||
:type key: str | ||
:param name: name of the lock | ||
:type name: str | ||
:param cookie: cookie of the lock | ||
:type cookie: str | ||
:raises: :class:`TypeError` | ||
:raises: :class:`Error` | ||
""" | ||
self.require_ioctx_open() | ||
if not isinstance(key, str): | ||
raise TypeError('key must be a string') | ||
if not isinstance(name, str): | ||
raise TypeError('name must be a string') | ||
if not isinstance(cookie, str): | ||
raise TypeError('cookie must be a string') | ||
|
||
ret = run_in_thread(self.librados.rados_unlock, | ||
(self.io, c_char_p(key), c_char_p(name), c_char_p(cookie))) | ||
if ret < 0: | ||
raise make_ex(ret, "Ioctx.rados_lock_exclusive(%s): failed to set lock %s on %s" % (self.name, name, key)) | ||
|
||
|
||
|
||
def set_object_locator(func): | ||
def retfunc(self, *args, **kwargs): | ||
if self.locator_key is not None: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
could extract the type checking code into a decorator