kazoo.recipe.lease

Zookeeper lease implementations

Maintainer:Lars Albertsson <lars.albertsson@gmail.com>
Maintainer:Jyrki Pulliainen <jyrki@spotify.com>
Status:Beta

Public API

class kazoo.recipe.lease.NonBlockingLease(client, path, duration, identifier=None, utcnow=<built-in method utcnow of type object at 0x939c20>)[source]

Exclusive lease that does not block.

An exclusive lease ensures that only one client at a time owns the lease. The client may renew the lease without losing it by obtaining a new lease with the same path and same identity. The lease object evaluates to True if the lease was obtained.

A common use case is a situation where a task should only run on a single host. In this case, the clients that did not obtain the lease should exit without performing the protected task.

The lease stores time stamps using client clocks, and will therefore only work if client clocks are roughly synchronised. It uses UTC, and works across time zones and daylight savings.

Example usage: with a KazooClient instance:

zk = KazooClient()
# Hold lease over an hour in order to keep job on same machine, with failover if it dies.
lease = zk.NonBlockingLease("/db_leases/hourly_cleanup", datetime.timedelta(minutes = 70),
  identifier = "DB hourly cleanup on " + socket.gethostname())
if lease:
    do_hourly_database_cleanup()
__init__(client, path, duration, identifier=None, utcnow=<built-in method utcnow of type object at 0x939c20>)[source]

Create a non-blocking lease.

Parameters:
  • client – A KazooClient instance.
  • path – The lease path to use.
  • duration – Duration during which the lease is reserved. A timedelta instance.
  • identifier – Unique name to use for this lease holder. Reuse in order to renew the lease. Defaults do socket.gethostname().
  • utcnow – Clock function, by default returning datetime.datetime.utcnow(). Used for testing.
class kazoo.recipe.lease.MultiNonBlockingLease(client, count, path, duration, identifier=None, utcnow=<built-in method utcnow of type object at 0x939c20>)[source]

Exclusive lease for multiple clients.

This type of lease is useful when a limited set of hosts should run a particular task. It will attempt to obtain leases trying a sequence of ZooKeeper lease paths.

Parameters:
  • client – A KazooClient instance.
  • count – Number of host leases allowed.
  • path – ZooKeeper path under which lease files are stored.
  • duration – Duration during which the lease is reserved. A timedelta instance.
  • identifier – Unique name to use for this lease holder. Reuse in order to renew the lease. Defaults do socket.gethostname().
  • utcnow – Clock function, by default returning datetime.datetime.utcnow(). Used for testing.
__init__(client, count, path, duration, identifier=None, utcnow=<built-in method utcnow of type object at 0x939c20>)[source]