Skip to content

Commit e84938a

Browse files
phlogistonjohnmergify[bot]
authored andcommitted
sambacc: add methods for writing and locking RADOSObjectRef
These will be used later to support cluster meta stored in rados. Signed-off-by: John Mulligan <[email protected]>
1 parent fb4d773 commit e84938a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

sambacc/rados_opener.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io
2222
import json
2323
import logging
24+
import time
2425
import typing
2526
import urllib.request
2627

@@ -93,13 +94,16 @@ def __init__(
9394
self._pool = pool
9495
self._ns = ns
9596
self._key = key
97+
self._lock_description = "sambacc RADOS library"
98+
self._lock_duration = None
9699

97100
self._open(interface)
98101
if must_exist:
99102
self._test()
100103

101104
def _open(self, interface: _RADOSInterface) -> None:
102105
# TODO: connection caching
106+
self._api = interface.api
103107
self._conn = interface.Rados()
104108
self._conn.connect()
105109
self._connected = True
@@ -205,6 +209,38 @@ def write(self, s: typing.Any) -> int:
205209
def writelines(self, ls: typing.Iterable[typing.Any]) -> None:
206210
raise NotImplementedError()
207211

212+
def write_full(self, data: bytes) -> None:
213+
"""Write the object such that its contents are exactly `data`."""
214+
self._ioctx.write_full(self._key, data)
215+
216+
def _lock_exclusive(self, name: str, cookie: str) -> None:
217+
self._ioctx.lock_exclusive(
218+
self._key,
219+
name,
220+
cookie,
221+
desc=self._lock_description,
222+
duration=self._lock_duration,
223+
)
224+
225+
def _acquire_lock_exclusive(
226+
self, name: str, cookie: str, *, delay: int = 1
227+
) -> None:
228+
while True:
229+
try:
230+
self._lock_exclusive(name, cookie)
231+
return
232+
except self._api.ObjectBusy:
233+
_logger.debug(
234+
"lock failed: %r, %r, %r: object busy",
235+
self._key,
236+
name,
237+
cookie,
238+
)
239+
time.sleep(delay)
240+
241+
def _unlock(self, name: str, cookie: str) -> None:
242+
self._ioctx.unlock(self._key, name, cookie)
243+
208244

209245
def _get_mon_config_key(interface: _RADOSInterface, key: str) -> io.BytesIO:
210246
mcmd = json.dumps(

0 commit comments

Comments
 (0)