Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable timeout for zk_lock retry #73

Merged
merged 5 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
dstaroff marked this conversation as resolved.
Show resolved Hide resolved

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")