Stop making tooz.utils depending on tooz.coordination

Change-Id: I5fdb1174ae1a2a84412f3b3159ad5835b2f7359f
This commit is contained in:
Julien Danjou 2017-01-01 18:30:51 +01:00
parent d8ac66f0d9
commit f3dddcffdd
11 changed files with 168 additions and 161 deletions

View File

@ -79,4 +79,4 @@ Exceptions
.. autoclass:: tooz.coordination.MemberAlreadyExist
.. autoclass:: tooz.coordination.MemberNotJoined
.. autoclass:: tooz.coordination.GroupNotEmpty
.. autofunction:: tooz.coordination.raise_with_cause
.. autofunction:: tooz.utils.raise_with_cause

View File

@ -29,6 +29,7 @@ from stevedore import driver
import tooz
from tooz import _retry
from tooz import utils
LOG = logging.getLogger(__name__)
@ -814,28 +815,7 @@ class WatchCallbackNotFound(tooz.ToozError):
(callback.__name__, group_id))
class SerializationError(tooz.ToozError):
"Exception raised when serialization or deserialization breaks."
def raise_with_cause(exc_cls, message, *args, **kwargs):
"""Helper to raise + chain exceptions (when able) and associate a *cause*.
**For internal usage only.**
NOTE(harlowja): Since in py3.x exceptions can be chained (due to
:pep:`3134`) we should try to raise the desired exception with the given
*cause*.
:param exc_cls: the :py:class:`~tooz.ToozError` class to raise.
:param message: the text/str message that will be passed to
the exceptions constructor as its first positional
argument.
:param args: any additional positional arguments to pass to the
exceptions constructor.
:param kwargs: any additional keyword arguments to pass to the
exceptions constructor.
"""
if not issubclass(exc_cls, tooz.ToozError):
raise ValueError("Subclass of tooz error is required")
excutils.raise_with_cause(exc_cls, message, *args, **kwargs)
# TODO(harlowja,jd): We'll have to figure out a way to remove this 'alias' at
# some point in the future (when we have a better way to tell people it has
# moved without messing up their exception catching hierarchy).
SerializationError = utils.SerializationError

View File

@ -39,13 +39,13 @@ def _translate_failures(func):
return func(*args, **kwargs)
except ValueError as e:
# Typically json decoding failed for some reason.
coordination.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
except requests.exceptions.RequestException as e:
coordination.raise_with_cause(coordination.ToozConnectionError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.ToozConnectionError,
encodeutils.exception_to_unicode(e),
cause=e)
return wrapper

View File

@ -55,9 +55,9 @@ def _translate_failures():
try:
yield
except (EnvironmentError, voluptuous.Invalid) as e:
coordination.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
def _convert_from_old_format(data):
@ -505,9 +505,9 @@ class FileFutureResult(coordination.CoordAsyncResult):
with _translate_failures():
return self._fut.result(timeout=timeout)
except futures.TimeoutError as e:
coordination.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
def done(self):
return self._fut.done()

View File

@ -254,9 +254,9 @@ class IPCFutureResult(coordination.CoordAsyncResult):
try:
return self._fut.result(timeout=timeout)
except futures.TimeoutError as e:
coordination.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
def done(self):
return self._fut.done()

View File

@ -44,9 +44,9 @@ def _translate_failures(func):
try:
return func(*args, **kwargs)
except pymemcache_client.MemcacheUnexpectedCloseError as e:
coordination.raise_with_cause(coordination.ToozConnectionError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.ToozConnectionError,
encodeutils.exception_to_unicode(e),
cause=e)
except (socket.timeout, socket.error,
socket.gaierror, socket.herror) as e:
# TODO(harlowja): get upstream pymemcache to produce a better
@ -56,12 +56,12 @@ def _translate_failures(func):
if e.errno is not None:
msg += " (with errno %s [%s])" % (errno.errorcode[e.errno],
e.errno)
coordination.raise_with_cause(coordination.ToozConnectionError,
msg, cause=e)
utils.raise_with_cause(coordination.ToozConnectionError,
msg, cause=e)
except pymemcache_client.MemcacheError as e:
coordination.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
return wrapper
@ -511,7 +511,7 @@ class MemcachedFutureResult(coordination.CoordAsyncResult):
try:
return self._fut.result(timeout=timeout)
except futures.TimeoutError as e:
coordination.raise_with_cause(
utils.raise_with_cause(
coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)

View File

@ -63,7 +63,7 @@ class MySQLLock(locking.Lock):
self.acquired = True
return True
except pymysql.MySQLError as e:
coordination.raise_with_cause(
utils.raise_with_cause(
tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
@ -84,9 +84,9 @@ class MySQLLock(locking.Lock):
self.acquired = False
return True
except pymysql.MySQLError as e:
coordination.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
def __del__(self):
if self.acquired:
@ -178,6 +178,6 @@ class MySQLDriver(coordination.CoordinationDriver):
passwd=password,
database=dbname)
except (pymysql.err.OperationalError, pymysql.err.InternalError) as e:
coordination.raise_with_cause(coordination.ToozConnectionError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.ToozConnectionError,
encodeutils.exception_to_unicode(e),
cause=e)

View File

@ -85,9 +85,9 @@ def _translating_cursor(conn):
with conn.cursor() as cur:
yield cur
except psycopg2.Error as e:
coordination.raise_with_cause(tooz.ToozError,
_format_exception(e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
_format_exception(e),
cause=e)
class PostgresLock(locking.Lock):
@ -226,6 +226,6 @@ class PostgresDriver(coordination.CoordinationDriver):
password=password,
database=dbname)
except psycopg2.Error as e:
coordination.raise_with_cause(coordination.ToozConnectionError,
_format_exception(e),
cause=e)
utils.raise_with_cause(coordination.ToozConnectionError,
_format_exception(e),
cause=e)

View File

@ -46,13 +46,13 @@ def _translate_failures():
try:
yield
except (exceptions.ConnectionError, exceptions.TimeoutError) as e:
coordination.raise_with_cause(coordination.ToozConnectionError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.ToozConnectionError,
encodeutils.exception_to_unicode(e),
cause=e)
except exceptions.RedisError as e:
coordination.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
class RedisLock(locking.Lock):
@ -431,9 +431,9 @@ return 1
self._client = self._make_client(self._parsed_url, self._options,
self.timeout)
except exceptions.RedisError as e:
coordination.raise_with_cause(coordination.ToozConnectionError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.ToozConnectionError,
encodeutils.exception_to_unicode(e),
cause=e)
else:
# Ensure that the server is alive and not dead, this does not
# ensure the server will always be alive, but does insure that it
@ -759,9 +759,9 @@ class RedisFutureResult(coordination.CoordAsyncResult):
with _translate_failures():
return self._fut.result(timeout=timeout)
except futures.TimeoutError as e:
coordination.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
def done(self):
return self._fut.done()

View File

@ -52,9 +52,9 @@ class ZooKeeperLock(locking.Lock):
exceptions.NoNodeError):
return False
except exceptions.KazooException as e:
coordination.raise_with_cause(tooz.ToozError,
"operation error: %s" % (e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
"operation error: %s" % (e),
cause=e)
def acquire(self, blocking=True):
blocking, timeout = utils.convert_blocking(blocking)
@ -145,16 +145,16 @@ class KazooDriver(coordination.CoordinationDriverCachedRunWatchers):
self._coord.start(timeout=self.timeout)
except self._coord.handler.timeout_exception as e:
e_msg = encodeutils.exception_to_unicode(e)
coordination.raise_with_cause(coordination.ToozConnectionError,
"Operational error: %s" % e_msg,
cause=e)
utils.raise_with_cause(coordination.ToozConnectionError,
"Operational error: %s" % e_msg,
cause=e)
try:
self._coord.ensure_path(self._paths_join("/", self._namespace))
except exceptions.KazooException as e:
e_msg = encodeutils.exception_to_unicode(e)
coordination.raise_with_cause(tooz.ToozError,
"Operational error: %s" % e_msg,
cause=e)
utils.raise_with_cause(tooz.ToozError,
"Operational error: %s" % e_msg,
cause=e)
self._leader_locks = {}
def _stop(self):
@ -173,20 +173,20 @@ class KazooDriver(coordination.CoordinationDriverCachedRunWatchers):
try:
async_result.get(block=True, timeout=timeout)
except timeout_exception as e:
coordination.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
except exceptions.NodeExistsError:
raise coordination.GroupAlreadyExist(group_id)
except exceptions.NoNodeError as e:
coordination.raise_with_cause(tooz.ToozError,
"Tooz namespace '%s' has not"
" been created" % self._namespace,
cause=e)
utils.raise_with_cause(tooz.ToozError,
"Tooz namespace '%s' has not"
" been created" % self._namespace,
cause=e)
except exceptions.ZookeeperError as e:
coordination.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
def create_group(self, group_id):
group_path = self._path_group(group_id)
@ -201,17 +201,17 @@ class KazooDriver(coordination.CoordinationDriverCachedRunWatchers):
try:
async_result.get(block=True, timeout=timeout)
except timeout_exception as e:
coordination.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
except exceptions.NoNodeError:
raise coordination.GroupNotCreated(group_id)
except exceptions.NotEmptyError:
raise coordination.GroupNotEmpty(group_id)
except exceptions.ZookeeperError as e:
coordination.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
def delete_group(self, group_id):
group_path = self._path_group(group_id)
@ -226,17 +226,17 @@ class KazooDriver(coordination.CoordinationDriverCachedRunWatchers):
try:
async_result.get(block=True, timeout=timeout)
except timeout_exception as e:
coordination.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
except exceptions.NodeExistsError:
raise coordination.MemberAlreadyExist(group_id, member_id)
except exceptions.NoNodeError:
raise coordination.GroupNotCreated(group_id)
except exceptions.ZookeeperError as e:
coordination.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
def join_group(self, group_id, capabilities=b""):
member_path = self._path_member(group_id, self._member_id)
@ -254,15 +254,15 @@ class KazooDriver(coordination.CoordinationDriverCachedRunWatchers):
try:
async_result.get(block=True, timeout=timeout)
except timeout_exception as e:
coordination.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
except exceptions.NoNodeError:
raise coordination.MemberNotJoined(group_id, member_id)
except exceptions.ZookeeperError as e:
coordination.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
def heartbeat(self):
# Just fetch the base path (and do nothing with it); this will
@ -272,15 +272,15 @@ class KazooDriver(coordination.CoordinationDriverCachedRunWatchers):
try:
self._coord.get(base_path)
except self._timeout_exception as e:
coordination.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
except exceptions.NoNodeError:
pass
except exceptions.ZookeeperError as e:
coordination.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
return self.timeout
def leave_group(self, group_id):
@ -296,15 +296,15 @@ class KazooDriver(coordination.CoordinationDriverCachedRunWatchers):
try:
members_ids = async_result.get(block=True, timeout=timeout)
except timeout_exception as e:
coordination.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
except exceptions.NoNodeError:
raise coordination.GroupNotCreated(group_id)
except exceptions.ZookeeperError as e:
coordination.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
else:
return set(m.encode('ascii') for m in members_ids)
@ -321,15 +321,15 @@ class KazooDriver(coordination.CoordinationDriverCachedRunWatchers):
try:
async_result.get(block=True, timeout=timeout)
except timeout_exception as e:
coordination.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
except exceptions.NoNodeError:
raise coordination.MemberNotJoined(group_id, member_id)
except exceptions.ZookeeperError as e:
coordination.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
def update_capabilities(self, group_id, capabilities):
member_path = self._path_member(group_id, self._member_id)
@ -346,15 +346,15 @@ class KazooDriver(coordination.CoordinationDriverCachedRunWatchers):
try:
capabilities = async_result.get(block=True, timeout=timeout)[0]
except timeout_exception as e:
coordination.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
except exceptions.NoNodeError:
raise coordination.MemberNotJoined(group_id, member_id)
except exceptions.ZookeeperError as e:
coordination.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
else:
return cls._loads(capabilities)
@ -374,15 +374,15 @@ class KazooDriver(coordination.CoordinationDriverCachedRunWatchers):
capabilities, znode_stats = async_result.get(block=True,
timeout=timeout)
except timeout_exception as e:
coordination.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
except exceptions.NoNodeError:
raise coordination.MemberNotJoined(group_id, member_id)
except exceptions.ZookeeperError as e:
coordination.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
else:
member_info = {
'capabilities': cls._loads(capabilities),
@ -403,18 +403,18 @@ class KazooDriver(coordination.CoordinationDriverCachedRunWatchers):
try:
group_ids = async_result.get(block=True, timeout=timeout)
except timeout_exception as e:
coordination.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(coordination.OperationTimedOut,
encodeutils.exception_to_unicode(e),
cause=e)
except exceptions.NoNodeError as e:
coordination.raise_with_cause(tooz.ToozError,
"Tooz namespace '%s' has not"
" been created" % self._namespace,
cause=e)
utils.raise_with_cause(tooz.ToozError,
"Tooz namespace '%s' has not"
" been created" % self._namespace,
cause=e)
except exceptions.ZookeeperError as e:
coordination.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
utils.raise_with_cause(tooz.ToozError,
encodeutils.exception_to_unicode(e),
cause=e)
else:
return set(g.encode('ascii') for g in group_ids)

View File

@ -23,10 +23,10 @@ import futurist
import msgpack
from oslo_serialization import msgpackutils
from oslo_utils import encodeutils
from oslo_utils import excutils
import six
import tooz
from tooz import coordination
class Base64LockEncoder(object):
@ -173,26 +173,53 @@ def to_binary(text, encoding='ascii'):
return text
def dumps(data, excp_cls=coordination.SerializationError):
class SerializationError(tooz.ToozError):
"Exception raised when serialization or deserialization breaks."
def dumps(data, excp_cls=SerializationError):
"""Serializes provided data using msgpack into a byte string."""
try:
return msgpackutils.dumps(data)
except (msgpack.PackException, ValueError) as e:
coordination.raise_with_cause(excp_cls,
encodeutils.exception_to_unicode(e),
cause=e)
raise_with_cause(excp_cls,
encodeutils.exception_to_unicode(e),
cause=e)
def loads(blob, excp_cls=coordination.SerializationError):
def loads(blob, excp_cls=SerializationError):
"""Deserializes provided data using msgpack (from a prior byte string)."""
try:
return msgpackutils.loads(blob)
except (msgpack.UnpackException, ValueError) as e:
coordination.raise_with_cause(excp_cls,
encodeutils.exception_to_unicode(e),
cause=e)
raise_with_cause(excp_cls,
encodeutils.exception_to_unicode(e),
cause=e)
def millis_to_datetime(milliseconds):
"""Converts number of milliseconds (from epoch) into a datetime object."""
return datetime.datetime.fromtimestamp(float(milliseconds) / 1000)
def raise_with_cause(exc_cls, message, *args, **kwargs):
"""Helper to raise + chain exceptions (when able) and associate a *cause*.
**For internal usage only.**
NOTE(harlowja): Since in py3.x exceptions can be chained (due to
:pep:`3134`) we should try to raise the desired exception with the given
*cause*.
:param exc_cls: the :py:class:`~tooz.ToozError` class to raise.
:param message: the text/str message that will be passed to
the exceptions constructor as its first positional
argument.
:param args: any additional positional arguments to pass to the
exceptions constructor.
:param kwargs: any additional keyword arguments to pass to the
exceptions constructor.
"""
if not issubclass(exc_cls, tooz.ToozError):
raise ValueError("Subclass of tooz error is required")
excutils.raise_with_cause(exc_cls, message, *args, **kwargs)