Python kazoo.exceptions.LockTimeout() Examples
The following are 7
code examples of kazoo.exceptions.LockTimeout().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
kazoo.exceptions
, or try the search function
.
Example #1
Source File: bounce_lib.py From paasta with Apache License 2.0 | 6 votes |
def bounce_lock_zookeeper( name: str, system_paasta_config: Optional[SystemPaastaConfig] = None ) -> Iterator: """Acquire a bounce lock in zookeeper for the name given. The name should generally be the service namespace being bounced. This is a contextmanager. Please use it via 'with bounce_lock(name):'. :param name: The lock name to acquire""" if system_paasta_config is None: system_paasta_config = load_system_paasta_config() zk = KazooClient( hosts=system_paasta_config.get_zk_hosts(), timeout=ZK_LOCK_CONNECT_TIMEOUT_S, ) zk.start() lock = zk.Lock(f"{ZK_LOCK_PATH}/{name}") try: lock.acquire(timeout=1) # timeout=0 throws some other strange exception yield except LockTimeout: raise LockHeldException("Service %s is already being bounced!" % name) else: lock.release() finally: zk.stop() zk.close()
Example #2
Source File: lock.py From panoptes with Apache License 2.0 | 5 votes |
def _get_lock(self): """ A wrapper around the Kazoo library's lock. On successful acquisition of the lock, sets self._lock and self._locked Returns: None """ logger = self._logger logger.info(u'Creating lock for {}'.format(str(self))) try: lock = self._context.zookeeper_client.Lock(self._path, self._identifier) except: logger.exception(u'Failed to create lock object') return tries = 0 while (self._retries == 0) or (tries < self._retries): tries += 1 logger.info(u'Trying to acquire lock for {}. Other contenders: {}'.format(str(self), lock.contenders())) try: lock.acquire(timeout=self._timeout) except LockTimeout: logger.info(u'Timed out after {} seconds trying to acquire lock for {}'.format(self._timeout, str(self))) except Exception as e: logger.info(u'Error in acquiring lock for {}: {}'.format(str(self), repr(e))) if lock.is_acquired: break if not lock.is_acquired: logger.warn(u'Failed to acquire lock for {} after {} tries'.format(str(self), tries)) else: logger.info(u'Lock acquired for {}. Other contenders: {}'.format(str(self), lock.contenders())) self._locked = True self._context.zookeeper_client.add_listener(self._lock_listener) self._lock = lock
Example #3
Source File: zklock.py From pykit with MIT License | 5 votes |
def acquire_loop(self, timeout=None): if timeout is None: timeout = self.timeout expire_at = time.time() + timeout while True: # Even if timeout is smaller than 0, try-loop continue on until # maybe_available is not ready. # # There is a chance that: # - Failed to create lock node(lock is occupied by other) # - Failed to get lock node(just deleted) # - Failed to create lock node(lock is occupied by other) # - Failed to get lock node(just deleted) # - ... if not self.maybe_available.wait(timeout=expire_at - time.time()): logger.debug('lock is still held by others: ' + str(self)) if time.time() > expire_at: raise LockTimeout('lock: ' + str(self.lock_path)) # Always proceed the "get" phase, in order to add a watch handler. # To watch node change event. self._create() self._acquire_by_get() if self.is_locked(): return # If it is possible to acquire the lock in next retry, do not yield if self.maybe_available.is_set(): continue else: yield self.lock_holder[0], self.lock_holder[1]
Example #4
Source File: zklock.py From pykit with MIT License | 5 votes |
def try_acquire(self): try: self.acquire(timeout=-1) except LockTimeout: pass # if_locked, lock holder identifier, holder version return self.is_locked(), self.lock_holder[0], self.lock_holder[1]
Example #5
Source File: zookeeper.py From data_pipeline with Apache License 2.0 | 5 votes |
def __enter__(self): try: self.lock.acquire(timeout=self.timeout) except LockTimeout: log.warning("Already one instance running against this source! exit. See y/oneandonly for help.") self.close() sys.exit(1)
Example #6
Source File: zookeeper_test.py From data_pipeline with Apache License 2.0 | 5 votes |
def bad_lock(self): bad_lock = mock.Mock() bad_lock.acquire = mock.Mock(side_effect=LockTimeout('Test exception')) return bad_lock
Example #7
Source File: etcd_discovery.py From dcos with Apache License 2.0 | 4 votes |
def zk_lock(zk: KazooClient, lock_path: str, contender_id: str, timeout: int) -> Generator: """ This contextmanager takes a ZooKeeper lock, yields, then releases the lock. This lock behaves like an interprocess mutex lock. ZooKeeper allows one to read values without holding a lock, but there is no guarantee that you will read the latest value. To read the latest value, you must call `sync()` on a ZNode before calling `get()`. Args: zk: The client to use to communicate with ZooKeeper. lock_path: The ZNode path to use as prefix for the locking recipe. contender_id: The contender id to identify the current client in the locking recipe. timeout: Time in seconds to wait for the lock to be acquired. If this time elapses before the lock is acquired, a `kazoo.exceptions.LockTimeout` exception is raised. Raises: kazoo.exceptions.LockTimeout: If the `timeout` is exceeded without the lock being acquired. """ lock = zk.Lock(lock_path, contender_id) try: log.info("Acquiring ZooKeeper lock.") lock.acquire(blocking=True, timeout=timeout, ephemeral=True) except (ConnectionLoss, SessionExpiredError) as e: msg_fmt = "Failed to acquire lock: {}" msg = msg_fmt.format(e.__class__.__name__) log.exception(msg) raise e except LockTimeout as e: msg_fmt = "Failed to acquire lock in `{}` seconds" msg = msg_fmt.format(timeout) log.exception(msg) raise e else: log.info("ZooKeeper lock acquired.") try: yield finally: log.info("Releasing ZooKeeper lock") lock.release() log.info("ZooKeeper lock released.")