|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import os |
| 16 | +import time |
16 | 17 |
|
17 | 18 | from kazoo.client import KazooClient |
18 | 19 | from kazoo.security import make_digest_acl, make_acl |
| 20 | +from kazoo.exceptions import ConnectionLoss |
19 | 21 | from robot.api import logger |
20 | 22 | from robot.libraries.BuiltIn import BuiltIn |
21 | 23 |
|
@@ -156,21 +158,34 @@ def set_access_control_lists(self, zk, node_path, acls): |
156 | 158 | """ |
157 | 159 | zk.set_acls(node_path, acls) |
158 | 160 |
|
159 | | - def create_node(self, zk, node_path, data=None): |
| 161 | + def create_node(self, zk, node_path, data=None, retries=3, delay=5): |
160 | 162 | """ |
161 | 163 | Creates ZooKeeper node. |
162 | 164 | *Args:*\n |
163 | 165 | _zk_ (KazooClient) - ZooKeeper client;\n |
164 | 166 | _node_path_ (str) - path of the node;\n |
165 | 167 | _data_ (str) - initial bytes value of node;\n |
| 168 | + _retries_ (int, optional) - number of retry attempts (default: 3);\n |
| 169 | + _delay_ (int, optional) - initial delay between retries in seconds (default: 5);\n |
166 | 170 | *Example:*\n |
167 | | - | Create Node | zk | /zookeeper_crud | Creation data | |
| 171 | + | Create Node | zk | /zookeeper_crud | Creation data | retries=3 | delay=1 | |
168 | 172 | """ |
169 | | - if data: |
170 | | - zk.create(node_path, value=data.encode()) |
171 | | - else: |
172 | | - zk.create(node_path) |
173 | | - logger.debug('Node "{}" is created.'.format(node_path)) |
| 173 | + for attempt in range(1, retries + 1): |
| 174 | + try: |
| 175 | + if data: |
| 176 | + zk.create(node_path, value=data.encode()) |
| 177 | + else: |
| 178 | + zk.create(node_path) |
| 179 | + logger.debug('Node "{}" is created.'.format(node_path)) |
| 180 | + return |
| 181 | + |
| 182 | + except ConnectionLoss as e: |
| 183 | + msg = (f'Attempt {attempt}/{retries}: cannot create node "{node_path}" ' |
| 184 | + f'due to ConnectionLoss') |
| 185 | + logger.warn(msg) |
| 186 | + if attempt == retries: |
| 187 | + self.builtin.fail(f'Failed to create node "{node_path}" after {retries} attempts: {e}') |
| 188 | + time.sleep(delay) |
174 | 189 |
|
175 | 190 | def create_node_with_children(self, zk, node_path, children_number: int, data): |
176 | 191 | zk.create(node_path) |
@@ -264,17 +279,35 @@ def update_node_value(self, zk, node_path, new_value): |
264 | 279 | stat = zk.set(node_path, new_value.encode()) |
265 | 280 | logger.debug('Node "{}" is updated: {}'.format(node_path, stat)) |
266 | 281 |
|
267 | | - def delete_node(self, zk, node_path): |
| 282 | + def delete_node(self, zk, node_path, retries=3, delay=5): |
268 | 283 | """ |
269 | 284 | Delete the node. |
270 | 285 | *Args:*\n |
271 | 286 | _zk_ (KazooClient) - ZooKeeper client;\n |
272 | 287 | _node_path_ (str) - path of the node;\n |
| 288 | + _retries_ (int, optional) - number of retry attempts (default: 5);\n |
| 289 | + _delay_ (int, optional) - initial delay between retries in seconds (default: 2);\n |
| 290 | +
|
273 | 291 | *Example:*\n |
274 | | - | Delete Node | zk | /zookeeper_crud/tests | |
| 292 | + | Delete Node | zk | /zookeeper_crud/tests | retries=3 | delay=1 | |
275 | 293 | """ |
276 | | - zk.delete(node_path, recursive=True) |
277 | | - logger.debug('Node "{}" is deleted.'.format(node_path)) |
| 294 | + for attempt in range(1, retries + 1): |
| 295 | + try: |
| 296 | + if zk.exists(node_path): |
| 297 | + zk.delete(node_path, recursive=True) |
| 298 | + logger.debug(f'Node "{node_path}" is deleted.') |
| 299 | + return |
| 300 | + else: |
| 301 | + logger.debug(f'Node "{node_path}" does not exist.') |
| 302 | + return |
| 303 | + |
| 304 | + except ConnectionLoss as e: |
| 305 | + msg = (f'Attempt {attempt}/{retries}: cannot delete node "{node_path}" ' |
| 306 | + f'due to ConnectionLoss') |
| 307 | + logger.warn(msg) |
| 308 | + if attempt == retries: |
| 309 | + self.builtin.fail(f'Failed to delete node "{node_path}" after {retries} attempts: {e}') |
| 310 | + time.sleep(delay) |
278 | 311 |
|
279 | 312 | def find_minimum(self, first, second): |
280 | 313 | """ |
|
0 commit comments