Skip to content

Commit

Permalink
lock: rename SpinLock to more accurate RetryLock
Browse files Browse the repository at this point in the history
  • Loading branch information
orndorffgrant authored and lucasmoura committed Jan 30, 2024
1 parent d8935ee commit 0a6a410
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/reboot_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def main(cfg: config.UAConfig) -> int:

LOG.debug("Running reboot commands...")
try:
with lock.SpinLock(cfg=cfg, lock_holder="pro-reboot-cmds"):
with lock.RetryLock(cfg=cfg, lock_holder="pro-reboot-cmds"):
fix_pro_pkg_holds(cfg)
refresh_contract(cfg)
upgrade_lts_contract.process_contract_delta_after_apt_lock(cfg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,15 @@ def test_error_full_auto_attach_fail(
assert 4 == enable_ent_by_name.call_count

@mock.patch(
"uaclient.lock.SpinLock.__enter__",
"uaclient.lock.RetryLock.__enter__",
side_effect=[
exceptions.LockHeldError(
lock_request="request", lock_holder="holder", pid=10
),
],
)
def test_lock_held(
self, _m_spinlock_enter, _notice_remove, _notice_read, FakeConfig
self, _m_retrylock_enter, _notice_remove, _notice_read, FakeConfig
):
with pytest.raises(exceptions.LockHeldError):
_full_auto_attach(FullAutoAttachOptions, FakeConfig())
Expand Down
2 changes: 1 addition & 1 deletion uaclient/api/u/pro/attach/auto/full_auto_attach/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _full_auto_attach(
mode: event_logger.EventLoggerMode = event_logger.EventLoggerMode.JSON
) -> FullAutoAttachResult:
try:
with lock.SpinLock(
with lock.RetryLock(
cfg=cfg,
lock_holder="pro.api.u.pro.attach.auto.full_auto_attach.v1",
):
Expand Down
4 changes: 3 additions & 1 deletion uaclient/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ def assert_lock_file(lock_holder=None):
def wrapper(f):
@wraps(f)
def new_f(*args, cfg, **kwargs):
with lock.SpinLock(cfg=cfg, lock_holder=lock_holder, sleep_time=1):
with lock.RetryLock(
cfg=cfg, lock_holder=lock_holder, sleep_time=1
):
retval = f(*args, cfg=cfg, **kwargs)
return retval

Expand Down
2 changes: 1 addition & 1 deletion uaclient/daemon/poll_for_pro_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

def attempt_auto_attach(cfg: UAConfig, cloud: AutoAttachCloudInstance):
try:
with lock.SpinLock(
with lock.RetryLock(
cfg=cfg, lock_holder="pro.daemon.attempt_auto_attach"
):
actions.auto_attach(cfg, cloud)
Expand Down
2 changes: 1 addition & 1 deletion uaclient/daemon/retry_auto_attach.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def retry_auto_attach(cfg: UAConfig) -> None:
"\n" + auto_attach_status_msg + "\n\n",
)
try:
with lock.SpinLock(
with lock.RetryLock(
cfg=cfg,
lock_holder="pro.daemon.retry_auto_attach.notice_updates",
):
Expand Down
2 changes: 1 addition & 1 deletion uaclient/daemon/tests/test_poll_for_pro_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _time_mock_side_effect():

@mock.patch(M_PATH + "LOG.debug")
@mock.patch(M_PATH + "actions.auto_attach")
@mock.patch(M_PATH + "lock.SpinLock")
@mock.patch(M_PATH + "lock.RetryLock")
class TestAttemptAutoAttach:
def test_success(
self, m_spin_lock, m_auto_attach, m_log_debug, FakeConfig
Expand Down
6 changes: 3 additions & 3 deletions uaclient/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ def clear_lock_file_if_present():
clear_lock_file()


class SpinLock:
class RetryLock:
"""
Context manager for gaining exclusive access to the lock file.
Create a lock file if absent. The lock file will contain a pid of the
running process, and a customer-visible description of the lock holder.
The SpinLock will try several times to acquire the lock before giving up.
The RetryLock will try several times to acquire the lock before giving up.
The number of times to try and how long to sleep in between tries is
configurable.
Expand Down Expand Up @@ -82,7 +82,7 @@ def __enter__(self):
break
except exceptions.LockHeldError as e:
LOG.debug(
"SpinLock Attempt %d. %s. Spinning...", tries + 1, e.msg
"RetryLock Attempt %d. %s. Spinning...", tries + 1, e.msg
)
tries += 1
if tries >= self.max_retries:
Expand Down
16 changes: 8 additions & 8 deletions uaclient/tests/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

from uaclient.exceptions import LockHeldError
from uaclient.files.notices import Notice
from uaclient.lock import SpinLock
from uaclient.lock import RetryLock
from uaclient.messages import LOCK_HELD

M_PATH = "uaclient.lock."
M_PATH_UACONFIG = "uaclient.config.UAConfig."


class TestSpinLock:
class TestRetryLock:
@mock.patch("os.getpid", return_value=123)
@mock.patch(M_PATH_UACONFIG + "delete_cache_key")
@mock.patch("uaclient.files.notices.NoticesManager.add")
Expand All @@ -30,7 +30,7 @@ def test_function(arg):
assert arg == mock.sentinel.arg
return mock.sentinel.success

with SpinLock(cfg=cfg, lock_holder="some operation"):
with RetryLock(cfg=cfg, lock_holder="some operation"):
ret = test_function(arg)

assert mock.sentinel.success == ret
Expand Down Expand Up @@ -61,7 +61,7 @@ def test_function():
raise RuntimeError("test")

with pytest.raises(RuntimeError) as exc:
with SpinLock(cfg=cfg, lock_holder="some operation"):
with RetryLock(cfg=cfg, lock_holder="some operation"):
test_function()

assert "test" == str(exc.value)
Expand All @@ -76,7 +76,7 @@ def test_function():

@mock.patch(M_PATH + "time.sleep")
@mock.patch(
M_PATH + "SpinLock.grab_lock",
M_PATH + "RetryLock.grab_lock",
side_effect=[
LockHeldError(
lock_request="request", lock_holder="holder", pid=10
Expand All @@ -92,7 +92,7 @@ def test_spins_when_lock_held(
):
cfg = FakeConfig()

with SpinLock(
with RetryLock(
cfg=cfg, lock_holder="request", sleep_time=1, max_retries=3
):
pass
Expand All @@ -106,7 +106,7 @@ def test_spins_when_lock_held(

@mock.patch(M_PATH + "time.sleep")
@mock.patch(
M_PATH + "SpinLock.grab_lock",
M_PATH + "RetryLock.grab_lock",
side_effect=[
LockHeldError(
lock_request="request", lock_holder="holder", pid=10
Expand All @@ -123,7 +123,7 @@ def test_raises_lock_held_after_max_retries(
cfg = FakeConfig()

with pytest.raises(LockHeldError) as exc:
with SpinLock(
with RetryLock(
cfg=cfg, lock_holder="request", sleep_time=1, max_retries=2
):
pass
Expand Down
2 changes: 1 addition & 1 deletion uaclient/tests/test_reboot_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_fix_pro_pkg_holds(
) # noqa: E501
@mock.patch("lib.reboot_cmds.refresh_contract")
@mock.patch("lib.reboot_cmds.fix_pro_pkg_holds")
@mock.patch("uaclient.lock.SpinLock")
@mock.patch("uaclient.lock.RetryLock")
@mock.patch("lib.reboot_cmds._is_attached")
@mock.patch(
"uaclient.files.state_files.reboot_cmd_marker_file",
Expand Down

0 comments on commit 0a6a410

Please sign in to comment.