Zookeeper Locking
This module provides a ZkLock, which should look familiar to anyone that has used Python’s threading.Lock class. In addition to normal locking behavior, revokable shared read/write locks with are also supported. All of the locks can be revoked as desired. This requires the current lock holder(s) to release their lock(s).
Shared Read/Write Locks
Also known in the Zookeeper Recipes as Revocable Shared Locks with Freaking Laser Beams, ZkReadLock and ZkWriteLock locks have been implemented. A read lock can be acquired as long as no write locks are active, while a write-lock can only be acquired when there are no other read or write locks active.
Using the Lock Command Line Interface
zktools comes with a CLI to easily see current locks, details of each lock, and remove empty locks called zooky.
Usage:
$ zooky list
LOCK STATUS
fred Locked
zkLockTest Free
$ zooky show fred
LOCK HOLDER DATA INFO
write-0000000002 0 {'pzxid': 152321L, 'ctime': 1326417195365L, 'aversion': 0, 'mzxid': 152321L, 'numChildren': 0,
'ephemeralOwner': 86927055090548768L, 'version': 0, 'dataLength': 1, 'mtime': 1326417195365L,
'cversion': 0, 'modifed_ago': 16, 'created_ago': 16, 'czxid': 152321L}
The modifed_ago and created_ago fields in INFO show how many seconds ago the lock was created and modified.
Flag used to declare that revokation should occur immediately. Other lock-holders will not be given time to release their lock.
Zookeeper Lock
Implements a Zookeeper based lock optionally with lock revocation should locks be idle for more than a specific set of time.
Example:
from zktools.connection import ZkConnection
from zktools.locking import ZkLock
# Create a connection and a lock
conn = ZkConnection()
my_lock = ZkLock(conn, "my_lock_name")
my_lock.acquire() # wait to acquire lock
# do something with the lock
my_lock.release() # release our lock
Create a Zookeeper lock object
Parameters: |
|
---|
Acquire a lock
Parameters: |
|
---|---|
Returns: | True if the lock was acquired, False otherwise |
Return type: | bool |
Release a lock
Returns: | True if the lock was released, or False if it is no longer valid. |
---|---|
Return type: | bool |
Indicate if this shared lock has been revoked
Returns: | True if the lock has been revoked, False otherwise. |
---|---|
Return type: | bool |
Revoke any existing locks, gently
Unlike clear(), this asks all existing locks to release, rather than forcibly revoking them.
Returns: | True if existing locks were present, False if there were no existing locks. |
---|---|
Return type: | bool |
Check with Zookeeper to see if the lock is acquired
Returns: | Whether the lock is acquired or not |
---|---|
Return type: | bool |
Clear out a lock
Warning
You must be sure this is a dead lock, as clearing it will forcably release it by deleting all lock nodes.
Returns: | True if the lock was cleared, or False if it is no longer valid. |
---|---|
Return type: | bool |
Base lock implementation for subclasses
Create a Zookeeper lock object
Parameters: |
|
---|
Acquire a lock
Internal function used by read/write lock
Parameters: |
|
---|---|
Returns: | True if the lock was acquired, False otherwise |
Return type: | bool |
Release a lock
Returns: | True if the lock was released, or False if it is no longer valid. |
---|---|
Return type: | bool |
Indicate if this shared lock has been revoked
Returns: | True if the lock has been revoked, False otherwise. |
---|---|
Return type: | bool |