zktools.locking

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.

Constants

zktools.locking.IMMEDIATE

Flag used to declare that revokation should occur immediately. Other lock-holders will not be given time to release their lock.

Lock Class

class zktools.locking.ZkLock(connection, lock_name, lock_root='/ZktoolsLocks', logfile=None)[source]

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
__init__(connection, lock_name, lock_root='/ZktoolsLocks', logfile=None)

Create a Zookeeper lock object

Parameters:
  • connection (ZkConnection instance) – zookeeper connection object
  • lock_root (string) – Path to the root lock node to create the locks under
  • logfile (string) – Path to a file to log the zookeeper stream to
acquire(timeout=None, revoke=False)[source]

Acquire a lock

Parameters:
  • timeout (int) – How long to wait to acquire the lock, set to 0 to get non-blocking behavior.
  • revoke (bool or IMMEDIATE) – Whether prior locks should be revoked. Can be set to True to request and wait for prior locks to release their lock, or IMMEDIATE to destroy the blocking read/write locks and attempt to acquire a write lock.
Returns:

True if the lock was acquired, False otherwise

Return type:

bool

release()

Release a lock

Returns:True if the lock was released, or False if it is no longer valid.
Return type:bool
revoked

Indicate if this shared lock has been revoked

Returns:True if the lock has been revoked, False otherwise.
Return type:bool
revoke_all()

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
has_lock()

Check with Zookeeper to see if the lock is acquired

Returns:Whether the lock is acquired or not
Return type:bool
clear()

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

Shared Read/Write Lock Classes

class zktools.locking.ZkReadLock(connection, lock_name, lock_root='/ZktoolsLocks', logfile=None)[source]

Shared Zookeeper Read Lock

A read-lock is considered succesful if there are no active write locks.

This class takes the same initialization parameters as ZkLock.

__init__(connection, lock_name, lock_root='/ZktoolsLocks', logfile=None)

Create a Zookeeper lock object

Parameters:
  • connection (ZkConnection instance) – zookeeper connection object
  • lock_root (string) – Path to the root lock node to create the locks under
  • logfile (string) – Path to a file to log the zookeeper stream to
acquire(timeout=None, revoke=False)[source]

Acquire a shared read lock

Parameters:
  • timeout (int) – How long to wait to acquire the lock, set to 0 to get non-blocking behavior.
  • revoke (bool or IMMEDIATE) – Whether prior locks should be revoked. Can be set to True to request and wait for prior locks to release their lock, or IMMEDIATE to destroy the blocking write locks and attempt to acquire a read lock.
Returns:

True if the lock was acquired, False otherwise

Return type:

bool

revoked

Indicate if this shared lock has been revoked

Returns:True if the lock has been revoked, False otherwise.
Return type:bool
has_lock()

Check with Zookeeper to see if the lock is acquired

Returns:Whether the lock is acquired or not
Return type:bool
revoke_all()

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
release()

Release a lock

Returns:True if the lock was released, or False if it is no longer valid.
Return type:bool
clear()

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
class zktools.locking.ZkWriteLock(connection, lock_name, lock_root='/ZktoolsLocks', logfile=None)[source]

Shared Zookeeper Write Lock

A write-lock is only succesful if there are no read or write locks active.

This class takes the same initialization parameters as ZkLock.

__init__(connection, lock_name, lock_root='/ZktoolsLocks', logfile=None)

Create a Zookeeper lock object

Parameters:
  • connection (ZkConnection instance) – zookeeper connection object
  • lock_root (string) – Path to the root lock node to create the locks under
  • logfile (string) – Path to a file to log the zookeeper stream to
acquire(timeout=None, revoke=False)[source]

Acquire a shared write lock

Parameters:
  • timeout (int) – How long to wait to acquire the lock, set to 0 to get non-blocking behavior.
  • revoke (bool or IMMEDIATE) – Whether prior locks should be revoked. Can be set to True to request and wait for prior locks to release their lock, or IMMEDIATE to destroy the blocking read/write locks and attempt to acquire a write lock.
Returns:

True if the lock was acquired, False otherwise

Return type:

bool

revoked

Indicate if this shared lock has been revoked

Returns:True if the lock has been revoked, False otherwise.
Return type:bool
has_lock()

Check with Zookeeper to see if the lock is acquired

Returns:Whether the lock is acquired or not
Return type:bool
revoke_all()

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
release()

Release a lock

Returns:True if the lock was released, or False if it is no longer valid.
Return type:bool
clear()

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

Private Lock Base Class

class zktools.locking._LockBase(connection, lock_name, lock_root='/ZktoolsLocks', logfile=None)[source]

Base lock implementation for subclasses

__init__(connection, lock_name, lock_root='/ZktoolsLocks', logfile=None)[source]

Create a Zookeeper lock object

Parameters:
  • connection (ZkConnection instance) – zookeeper connection object
  • lock_root (string) – Path to the root lock node to create the locks under
  • logfile (string) – Path to a file to log the zookeeper stream to
_acquire_lock(node_name, timeout=None, revoke=False)[source]

Acquire a lock

Internal function used by read/write lock

Parameters:
  • node_name (str) – Name of the node to use for the lock
  • timeout (int) – How long to wait to acquire the lock, set to 0 to get non-blocking behavior.
  • revoke (bool or :obj:IMMEDIATE) – Whether prior locks should be revoked. Can be set to True to request and wait for prior locks to release their lock, or IMMEDIATE to destroy the blocking read/write locks and attempt to acquire a write lock.
Returns:

True if the lock was acquired, False otherwise

Return type:

bool

release()[source]

Release a lock

Returns:True if the lock was released, or False if it is no longer valid.
Return type:bool
revoked[source]

Indicate if this shared lock has been revoked

Returns:True if the lock has been revoked, False otherwise.
Return type:bool
has_lock()[source]

Check with Zookeeper to see if the lock is acquired

Returns:Whether the lock is acquired or not
Return type:bool
clear()[source]

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

Internal Utility Functions

zktools.locking.has_read_lock(keyname, children)[source]

Determines if this keyname has a valid read lock

Parameters:
  • keyname (str) – The keyname without full path prefix of the current node being examined
  • children (list) – List of the children nodes at this lock point
zktools.locking.has_write_lock(keyname, children)[source]

Determines if this keyname has a valid write lock

Parameters:
  • keyname (str) – The keyname without full path prefix of the current node being examined
  • children (list) – List of the children nodes at this lock point

Table Of Contents

Previous topic

zktools.connection

Next topic

zktools.node

This Page