Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
try:
yield
except self._client.handler.timeout_exception:
exc.raise_with_cause(exc.StorageFailure,
"Storage backend timeout")
except k_exc.SessionExpiredError:
exc.raise_with_cause(exc.StorageFailure,
"Storage backend session has expired")
except k_exc.NoNodeError:
exc.raise_with_cause(exc.NotFound,
"Storage backend node not found")
except k_exc.NodeExistsError:
exc.raise_with_cause(exc.Duplicate,
"Storage backend duplicate node")
except (k_exc.KazooException, k_exc.ZookeeperError):
exc.raise_with_cause(exc.StorageFailure,
"Storage backend internal error")
def __call__(self, func):
"""Callable version for use as a decorator
:param func: Function to call initially and every time the
data changes. `func` will be called with a
tuple, the value of the node and a
:class:`~kazoo.client.ZnodeStat` instance.
:type func: callable
"""
if self._used:
raise KazooException(
"A function has already been associated with this "
"DataWatch instance.")
self._func = func
self._used = True
self._client.add_listener(self._session_watcher)
self._get_data()
return func
def ping(self):
'''
Simple command to test if the zookeeper session is able to connect
at this very moment
'''
try:
# dummy ping to ensure we are still connected
self.zoo_client.server_version()
return True
except KazooException:
return False
def exists_completion(result):
try:
stat = result.get()
except self.DISCONNECT_EXCEPTIONS:
self._once(KazooState.CONNECTED, wait_exists)
return
except ke.NoNodeError:
wait_exists()
return
except ke.KazooException as e:
log.warning('Unexpected exists_completion result: (%s)%s' % (type(e), e))
return
if stat:
do_monitor()
def __call__(self, func):
"""Callable version for use as a decorator"""
if self._used:
raise kazoo.exceptions.KazooException(
'A function has already been associated with this '
'ExistingDataWatch instance.')
self._func = func
self._used = True
self._client.add_listener(self._session_watcher)
self._get_data()
return func
"""Kazoo Exceptions"""
from collections import defaultdict
class KazooException(Exception):
"""Base Kazoo exception that all other kazoo library exceptions
inherit from"""
class ZookeeperError(KazooException):
"""Base Zookeeper exception for errors originating from the
Zookeeper server"""
class CancelledError(KazooException):
"""Raised when a process is cancelled by another thread"""
class ConfigurationError(KazooException):
"""Raised if the configuration arguments to an object are
invalid"""
class ZookeeperStoppedError(KazooException):
"""Raised when the kazoo client stopped (and thus not connected)"""
class ConnectionDropped(KazooException):
"""Internal error for jumping out of loops"""
pass
else:
valid_certs.append(cert)
if valid_certs:
return valid_certs
# If there are no valid certificates, try to generate a a new one.
if self._key is None:
raise UnknownError('A private key is not configured')
new_cert = PublicCertificate.from_key(self._key, self.project_id)
try:
self._zk_client.create('{}/cert'.format(self._certs_node),
new_cert.to_json(), sequence=True)
except KazooException:
raise UnknownError('Unable to create new certificate')
return [new_cert]
class ZookeeperStoppedError(KazooException):
"""Raised when the kazoo client stopped (and thus not connected)"""
class ConnectionDropped(KazooException):
"""Internal error for jumping out of loops"""
class LockTimeout(KazooException):
"""Raised if failed to acquire a lock.
.. versionadded:: 1.1
"""
class WriterNotClosedException(KazooException):
"""Raised if the writer is unable to stop closing when requested.
.. versionadded:: 1.2
"""
class SASLException(KazooException):
"""Raised if SASL encountered a (local) error.
.. versionadded:: 2.7.0
"""
def _invalid_error_code():
raise RuntimeError('Invalid error code')
def _best_effort_cleanup(self):
""" Attempt to delete nodes that this lock has created. """
try:
nodes = self._find_nodes()
self._delete_nodes(nodes)
except KazooException:
pass
def _release_locks(self):
"""Attempt to completely remove all the locks"""
self._acquire_event.clear()
for lock in self._locks[:]:
try:
lock.release()
except KazooException: # pragma: nocover
# We proceed to remove as many as possible, and leave
# the ones we couldn't remove
pass
else:
self._locks.remove(lock)