k3zkutil
Helper functions for ZooKeeper with kazoo. Provides distributed locking, configuration management, ACL utilities, and CAS operations.
k3zkutil is a component of pykit3 project: a python3 toolkit set.
Installation
pip install k3zkutil
Quick Start
import k3zkutil
# Create a distributed lock
with k3zkutil.ZKLock('my_lock', zk_client):
# Do something with exclusive access
pass
API Reference
k3zkutil
Some helper function to make life easier with zookeeper.
CachedReader
Bases: dict
Source code in k3zkutil/cached_reader.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
__init__(zk, path, callback=None)
:param zk: is the connection argument, which can be:
- Comma separated host list, such as
"127.0.0.1:2181,127.0.0.2:2181".
-
A
zkutil.ZKConfinstance specifying connection argument with other config. -
A plain
dictto create azkutil.ZKConfinstance. :param path: the path of the node in zookeeper. :param callback: give a callback when the node change. Defaults toNone. It has 3 arguments(path, old_dict, new_dict).
Source code in k3zkutil/cached_reader.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
close()
Stop the zkutil.CachedReader.watch and the callback.
:return: nothing
Source code in k3zkutil/cached_reader.py
83 84 85 86 87 88 89 90 91 92 93 | |
watch(timeout=None)
Wait until the node change and return a list [old_dict, new_dict].
If timeout, raise a ZKWaitTimeout.
:param timeout: specifies the time(in second) to wait.
By default it is None which means to wait for a year
:return: If close the CachedReader by zkutil.CachedReader.close(), it return None.
If the node change, it return a list [old_dict, new_dict]
Source code in k3zkutil/cached_reader.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
ZKConf
Bases: object
It is a config wrapper, provding several method for accessing config.
If one of the config field is not spedified when initializing this class, it
falls back to using config.zk_<field>.
Classes in this module relies on this class to access config.
E.g.:
Source code in k3zkutil/zkconf.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
ZKLock
Bases: object
ZKLock implements a zookeeper based distributed lock.
Source code in k3zkutil/zklock.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
release()
Release the lock if it has been locked. Otherwise return silently.
If this lock initiated a connection by itself, it will be closed.
:return: Nothing
Source code in k3zkutil/zklock.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
try_acquire()
Try to acquire the lock and return result.
It never blocks.
:return: a tuple of result, lock holder and lock holder version.
Such as (True, "aa-xx-bb", -1) or (False, "aa-xx-cc", 12)
Source code in k3zkutil/zklock.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
try_release()
Release lock if current lock holder is this lock.
It never blocks.
:return: a tuple of result, lock holder and lock holder version.
Such as (True, "aa-xx-bb", -1) or (False, "aa-xx-cc", 12)
Source code in k3zkutil/zklock.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
cas_loop(zkclient, path, json=True)
A helper generator for doing CAS(check and set or compare and swap) on zk. See CAS
A general CAS loop is like following(check the version when update):
:param zkclient: is a KazooClient instance connected to zk.
It can also be a string, in which case it is treated as a comma separated
hosts list, and a zkclient is created with default setting.
It can also be a dict or an instance of ZKConf, in which case it create
a zkclient with ZKConf defined setting.
:param path: is the zk-node path to get and set.
:param json: whether to do a json load after reading the value from zk and to do a json dump
before updating the value to zk.
:return: a generator yields a tuple of 2 element:
Source code in k3zkutil/zkacid.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
close_zk(zk)
Stop and close a zk client.
:param zk: a KazooClient or KazooClientExt instance, otherwise raise a TypeError.
:return: nothing
Source code in k3zkutil/zkutil.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
export_hierarchy(zkcli, zkpath)
Exporting a zookeeper node in a tree structure, and you can import the data into zookeeper
using zkutil.init_hierarchy
:param zkcli: is a KazooClient instance connected to zk.
:param zkpath: is zookeeper root path that you want export
:return:
Source code in k3zkutil/zkutil.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | |
get_next(zkclient, path, version=-1)
Wait until zk-node path version becomes greater than version then return
node value and zstat.
:param version: the version that path version must be greater than.
:return: zk node value and zstat.
Source code in k3zkutil/zkutil.py
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 | |
init_hierarchy(hosts, hierarchy, users, auth)
It initialize a zookeeper cluster, including initializing the tree structure,
setting value and permissions for each node.
:param hosts: comma-separated list of hosts to connect to, such as '127.0.0.1:2181,127.0.0.1:2182,[::1]:2183'.
:param hierarchy: a dict of zk node structure with value or acl optional for each node.
For example,
Source code in k3zkutil/zkutil.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | |
is_backward_locking(locked_keys, key)
Check if the operation of locking key is a backward-locking.
:param locked_keys: is a collection support in operator that contains already locked keys.
:param key: is the key to lock.
:return: a bool indicate if locking key would be a backward-locking.
Source code in k3zkutil/zkutil.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
kazoo_client_ext(zk, json=True)
return zkclient created or original zkclient, and if zkclient is created
Source code in k3zkutil/zkconf.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
lock_id(node_id=None)
It builds a string used as zk lock node value, to describe lock holder's information.
The result string is in form:
e.g. web-192.168.0.2-1233-0000000001
ip is chosen from all local ipv4.
If there is an intra net ip, use it.
Otherwise, use an public ip it found.
:param node_id: is an arbitrary string representing a host.
If it is None, config.zk_node_id is used.
:return: a string for lock identifier.
Source code in k3zkutil/zkutil.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
make_acl_entry(username, password, permissions)
It concat username, digest and permissions to a string as acl entry.
:param username: the name of zookeeper user.
:param password: the password of zookeeper user.
:param permissions: a string, a list or a tuple.
each element in permissions is a char who should be included in cdrwa. If permissions contains extra element, PermTypeError will be raised.
:return: a string in form:
"digest:digest is a string build by zkutil.make_digest()
Source code in k3zkutil/zkutil.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
make_digest(acc)
It make a digest for string acc
:param acc: The acc string is in form:
:return: a digest string.
Source code in k3zkutil/zkutil.py
152 153 154 155 156 157 158 159 160 161 162 | |
make_kazoo_digest_acl(acl)
:param acl: acl in tuple or list.
:return: a list of kazoo.security.ACL.
Source code in k3zkutil/zkutil.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
parse_kazoo_acl(acls)
Convert kazoo style acls in list/tuple to a list in form [(<scheme>, <user>, <perm>)].
:param acls: a list/tuple of kazoo.security.ACL.
:return: a list of acl in form [(<scheme>, <user>, <perm>)]
Source code in k3zkutil/zkutil.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
parse_lock_id(data_str)
It parse string built by zkutil.lock_id() to an dictionary.
:param data_str: string built by zkutil.lock_id().
Source code in k3zkutil/zkutil.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
perm_to_long(short, lower=True)
Convert short style zookeeper permissions(cdrwa or CDRWA)
to standard permission list(['create', 'delete', 'read', 'write', 'admin']).
:param short: is iterable of short permissions that can be used in for-loop.
Such as "cdrw" or ['c', 'd']
:param lower: specifies if convert result to lower or upper case.
By default it is True, for lower case.
:return: a list of standard permission.
Source code in k3zkutil/zkutil.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
perm_to_short(lst, lower=True)
The reverse of perm_to_long:
It convert a list of standard permissions back to a short permission string,
such as cdrw.
:param lst: is a list of standard permissions, such as ['create', 'read'].
:param lower: specifies if convert result to lower or upper case.
By default it is True, for lower case.
:return: a string of short permissions.
Source code in k3zkutil/zkutil.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
wait_absent(zkclient, path)
Wait at most timeout seconds for zk-node path to be absent.
If path does not exist, it returns at once.
:param zkclient:
:param path:
:return:
Source code in k3zkutil/zkutil.py
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | |
License
The MIT License (MIT) - Copyright (c) 2015 Zhang Yanpo (张炎泼)