| 1 |
# Copyright (C) 2014 AT&T Labs All Rights Reserved. |
| 2 |
# Copyright (C) 2015 Rackspace Hosting All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
import contextlib |
| 17 |
|
| 18 |
from kazoo import exceptions as k_exc |
| 19 |
from kazoo.protocol import paths |
| 20 |
from oslo_serialization import jsonutils |
| 21 |
from oslo_utils import strutils |
| 22 |
|
| 23 |
from taskflow import exceptions as exc |
| 24 |
from taskflow.persistence import path_based |
| 25 |
from taskflow.utils import kazoo_utils as k_utils |
| 26 |
from taskflow.utils import misc |
| 27 |
|
| 28 |
|
| 29 |
MIN_ZK_VERSION = (3, 4, 0) |
| 30 |
|
| 31 |
|
| 32 |
class ZkBackend(path_based.PathBasedBackend): |
| 33 |
"""A zookeeper-backed backend. |
| 34 |
|
| 35 |
Example configuration:: |
| 36 |
|
| 37 |
conf = { |
| 38 |
"hosts": "192.168.0.1:2181,192.168.0.2:2181,192.168.0.3:2181", |
| 39 |
"path": "/taskflow", |
| 40 |
} |
| 41 |
|
| 42 |
Do note that the creation of a kazoo client is achieved |
| 43 |
by :py:func:`~taskflow.utils.kazoo_utils.make_client` and the transfer |
| 44 |
of this backend configuration to that function to make a |
| 45 |
client may happen at ``__init__`` time. This implies that certain |
| 46 |
parameters from this backend configuration may be provided to |
| 47 |
:py:func:`~taskflow.utils.kazoo_utils.make_client` such |
| 48 |
that if a client was not provided by the caller one will be created |
| 49 |
according to :py:func:`~taskflow.utils.kazoo_utils.make_client`'s |
| 50 |
specification |
| 51 |
""" |
| 52 |
|
| 53 |
#: Default path used when none is provided. |
| 54 |
DEFAULT_PATH = '/taskflow' |
| 55 |
|
| 56 |
def __init__(self, conf, client=None): |
| 57 |
super().__init__(conf) |
| 58 |
if not paths.isabs(self._path): |
| 59 |
raise ValueError("Zookeeper path must be absolute") |
| 60 |
if client is not None: |
| 61 |
self._client = client |
| 62 |
self._owned = False |
| 63 |
else: |
| 64 |
self._client = k_utils.make_client(self._conf) |
| 65 |
self._owned = True |
| 66 |
self._validated = False |
| 67 |
|
| 68 |
def get_connection(self): |
| 69 |
conn = ZkConnection(self, self._client, self._conf) |
| 70 |
if not self._validated: |
| 71 |
conn.validate() |
| 72 |
self._validated = True |
| 73 |
return conn |
| 74 |
|
| 75 |
def close(self): |
| 76 |
self._validated = False |
| 77 |
if not self._owned: |
| 78 |
return |
| 79 |
try: |
| 80 |
k_utils.finalize_client(self._client) |
| 81 |
except (k_exc.KazooException, k_exc.ZookeeperError): |
| 82 |
exc.raise_with_cause(exc.StorageFailure, |
| 83 |
"Unable to finalize client") |
| 84 |
|
| 85 |
|
| 86 |
class ZkConnection(path_based.PathBasedConnection): |
| 87 |
def __init__(self, backend, client, conf): |
| 88 |
super().__init__(backend) |
| 89 |
self._conf = conf |
| 90 |
self._client = client |
| 91 |
with self._exc_wrapper(): |
| 92 |
# NOOP if already started. |
| 93 |
self._client.start() |
| 94 |
|
| 95 |
@contextlib.contextmanager |
| 96 |
def _exc_wrapper(self): |
| 97 |
"""Exception context-manager which wraps kazoo exceptions. |
| 98 |
|
| 99 |
This is used to capture and wrap any kazoo specific exceptions and |
| 100 |
then group them into corresponding taskflow exceptions (not doing |
| 101 |
that would expose the underlying kazoo exception model). |
| 102 |
""" |
| 103 |
try: |
| 104 |
yield |
| 105 |
except self._client.handler.timeout_exception: |
| 106 |
exc.raise_with_cause(exc.StorageFailure, |
| 107 |
"Storage backend timeout") |
| 108 |
except k_exc.SessionExpiredError: |
| 109 |
exc.raise_with_cause(exc.StorageFailure, |
| 110 |
"Storage backend session has expired") |
| 111 |
except k_exc.NoNodeError: |
| 112 |
exc.raise_with_cause(exc.NotFound, |
| 113 |
"Storage backend node not found") |
| 114 |
except k_exc.NodeExistsError: |
| 115 |
exc.raise_with_cause(exc.Duplicate, |
| 116 |
"Storage backend duplicate node") |
| 117 |
except (k_exc.KazooException, k_exc.ZookeeperError): |
| 118 |
exc.raise_with_cause(exc.StorageFailure, |
| 119 |
"Storage backend internal error") |
| 120 |
|
| 121 |
def _join_path(self, *parts): |
| 122 |
return paths.join(*parts) |
| 123 |
|
| 124 |
def _get_item(self, path): |
| 125 |
with self._exc_wrapper(): |
| 126 |
data, _ = self._client.get(path) |
| 127 |
return misc.decode_json(data) |
| 128 |
|
| 129 |
def _set_item(self, path, value, transaction): |
| 130 |
data = misc.binary_encode(jsonutils.dumps(value)) |
| 131 |
if not self._client.exists(path): |
| 132 |
transaction.create(path, data) |
| 133 |
else: |
| 134 |
transaction.set_data(path, data) |
| 135 |
|
| 136 |
def _del_tree(self, path, transaction): |
| 137 |
for child in self._get_children(path): |
| 138 |
self._del_tree(self._join_path(path, child), transaction) |
| 139 |
transaction.delete(path) |
| 140 |
|
| 141 |
def _get_children(self, path): |
| 142 |
with self._exc_wrapper(): |
| 143 |
return self._client.get_children(path) |
| 144 |
|
| 145 |
def _ensure_path(self, path): |
| 146 |
with self._exc_wrapper(): |
| 147 |
self._client.ensure_path(path) |
| 148 |
|
| 149 |
def _create_link(self, src_path, dest_path, transaction): |
| 150 |
if not self._client.exists(dest_path): |
| 151 |
transaction.create(dest_path) |
| 152 |
|
| 153 |
@contextlib.contextmanager |
| 154 |
def _transaction(self): |
| 155 |
transaction = self._client.transaction() |
| 156 |
with self._exc_wrapper(): |
| 157 |
yield transaction |
| 158 |
k_utils.checked_commit(transaction) |
| 159 |
|
| 160 |
def validate(self): |
| 161 |
with self._exc_wrapper(): |
| 162 |
try: |
| 163 |
if strutils.bool_from_string( |
| 164 |
self._conf.get('check_compatible'), default=True): |
| 165 |
k_utils.check_compatible(self._client, MIN_ZK_VERSION) |
| 166 |
except exc.IncompatibleVersion: |
| 167 |
exc.raise_with_cause(exc.StorageFailure, "Backend storage is" |
| 168 |
" not a compatible version") |