Skip to content

Commit

Permalink
Add configurable timeout for zk_lock retry (#73)
Browse files Browse the repository at this point in the history
* Add configurable timeout for zk_lock retry

* Fix linter

* Added exception handling in _zk_flock

* Rebased onto lastest main and fixed linting

* Restoring _exit_code functioning. Fixing if statement in case of timeout

---------

Co-authored-by: Maria Dyuldina <[email protected]>
Co-authored-by: Filippo Monari <[email protected]>
  • Loading branch information
3 people authored Nov 9, 2023
1 parent e126f91 commit 3c28cf5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions ch_backup/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def _as_seconds(t: str) -> int:
"flock_path": "/tmp/flock.lock",
"zk_flock_path": "/tmp/zk_flock.lock",
"exitcode": 0,
"lock_timeout": _as_seconds("1 min"),
},
}

Expand Down
12 changes: 10 additions & 2 deletions ch_backup/logic/lock_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from types import TracebackType
from typing import IO, Optional, Type

from kazoo.exceptions import LockTimeout
from kazoo.recipe.lock import Lock

from ch_backup import logging
Expand All @@ -18,7 +19,7 @@ class LockManager:
Lock manager class
"""

# pylint: disable=consider-using-with
# pylint: disable=consider-using-with,too-many-instance-attributes

_lock_conf: dict
_process_lockfile_path: str
Expand All @@ -41,6 +42,7 @@ def __init__(self, lock_conf: dict, zk_ctl: Optional[ZookeeperCTL]) -> None:
self._exitcode = lock_conf.get("exitcode")
self._distributed = zk_ctl is None
self._disabled = False
self._lock_timeout = lock_conf.get("lock_timeout")

def __call__(self, distributed: bool = True, disabled: bool = False): # type: ignore
"""
Expand Down Expand Up @@ -106,7 +108,13 @@ def _zk_flock(self) -> None:
if not client.exists(self._process_zk_lockfile_path):
client.create(self._process_zk_lockfile_path, makepath=True)
self._zk_lock = client.Lock(self._process_zk_lockfile_path)
if not self._zk_lock.acquire(blocking=False):
try:
_ = self._zk_lock.acquire(blocking=True, timeout=self._lock_timeout)
logging.debug("Lock was acquired")
except LockTimeout:
logging.error(
"Lock was not acquired due to timeout error.", exc_info=True
)
sys.exit(self._exitcode)
else:
raise RuntimeError("ZK flock enabled, but zookeeper is not configured")

0 comments on commit 3c28cf5

Please sign in to comment.