patroni.dcs.zookeeper module

class patroni.dcs.zookeeper.PatroniKazooClient(hosts='127.0.0.1:2181', timeout=10.0, client_id=None, handler=None, default_acl=None, auth_data=None, sasl_options=None, read_only=None, randomize_hosts=True, connection_retry=None, command_retry=None, logger=None, keyfile=None, keyfile_password=None, certfile=None, ca=None, use_ssl=False, verify_certs=True, **kwargs)

Bases: kazoo.client.KazooClient

class patroni.dcs.zookeeper.PatroniSequentialThreadingHandler(connect_timeout: Union[int, float])

Bases: kazoo.handlers.threading.SequentialThreadingHandler

create_connection(*args: Any, **kwargs: Any) socket.socket
This method is trying to establish connection with one of the zookeeper nodes.

Somehow strategy “fail earlier and retry more often” works way better comparing to the original strategy “try to connect with specified timeout”. Since we want to try connect to zookeeper more often (with the smaller connect_timeout), he have to override create_connection method in the SequentialThreadingHandler class (which is used by kazoo.Client).

Parameters

args – always contains tuple(host, port) as the first element and could contain connect_timeout (negotiated session timeout) as the second element.

select(*args: Any, **kwargs: Any) Any

Python 3.XY may raise following exceptions if select/poll are called with an invalid socket: - ValueError: because fd == -1 - TypeError: Invalid file descriptor: -1 (starting from kazoo 2.9) Python 2.7 may raise the IOError instead of socket.error (starting from kazoo 2.9)

When it is appropriate we map these exceptions to socket.error.

set_connect_timeout(connect_timeout: Union[int, float]) None
class patroni.dcs.zookeeper.ZooKeeper(config: Dict[str, Any])

Bases: patroni.dcs.AbstractDCS

attempt_to_acquire_leader() bool

Attempt to acquire leader lock.

Note

This method should create /leader key with the value _name.

The key must be created atomically. In case the key already exists it should not be overwritten and False must be returned.

If key creation fails due to DCS not being accessible or because it is not able to process requests (hopefully temporary), the DCSError exception should be raised.

Returns

True if key has been created successfully.

cancel_initialization() bool

Removes the initialize key for a cluster.

Returns

True if successfully committed to DCS.

cluster_watcher(event: Optional[kazoo.protocol.states.WatchedEvent]) None
delete_cluster() bool

Delete cluster from DCS.

Returns

True if successfully committed to DCS.

delete_sync_state(version: Optional[int] = None) bool

Delete the synchronous state from DCS.

Parameters

version – for conditional deletion of the key/object.

Returns

True if delete successful.

get_children(key: str, watch: Optional[Callable[[kazoo.protocol.states.WatchedEvent], None]] = None) List[str]
get_node(key: str, watch: Optional[Callable[[kazoo.protocol.states.WatchedEvent], None]] = None) Optional[Tuple[str, kazoo.protocol.states.ZnodeStat]]
get_status(path: str, leader: Optional[patroni.dcs.Leader]) Tuple[int, Optional[Dict[str, int]]]
initialize(create_new: bool = True, sysid: str = '') bool

Race for cluster initialization.

This method should atomically create initialize key and return True, otherwise it should return False.

Parameters
  • create_newFalse if the key should already exist (in the case we are setting the system_id).

  • sysid – PostgreSQL cluster system identifier, if specified, is written to the key.

Returns

True if key has been created successfully.

load_members(path: str) List[patroni.dcs.Member]
static member(name: str, value: str, znode: kazoo.protocol.states.ZnodeStat) patroni.dcs.Member
reload_config(config: Union[Config, Dict[str, Any]]) None

Load and set relevant values from configuration.

Sets loop_wait, ttl and retry_timeout properties.

Parameters

config – Loaded configuration information object or dictionary of key value pairs.

session_listener(state: str) None
set_config_value(value: str, version: Optional[int] = None) bool

Create or update /config key in DCS.

Parameters
  • value – new value to set in the config key.

  • version – for conditional update of the key/object.

Returns

True if successfully committed to DCS.

set_failover_value(value: str, version: Optional[int] = None) bool

Create or update /failover key.

Parameters
  • value – value to set.

  • version – for conditional update of the key/object.

Returns

True if successfully committed to DCS.

set_history_value(value: str) bool

Set value for history in DCS.

Parameters

value – new value of history key/object.

Returns

True if successfully committed to DCS.

set_retry_timeout(retry_timeout: int) None

Set the new value for retry_timeout.

set_sync_state_value(value: str, version: Optional[int] = None) Union[int, bool]

Set synchronous state in DCS.

Parameters
  • value – the new value of /sync key.

  • version – for conditional update of the key/object.

Returns

version of the new object or False in case of error.

set_ttl(ttl: int) Optional[bool]

It is not possible to change ttl (session_timeout) in zookeeper without destroying old session and creating the new one. This method returns !True if session_timeout has been changed (restart() has been called).

status_watcher(event: Optional[kazoo.protocol.states.WatchedEvent]) None
take_leader() bool

Establish a new leader in DCS.

Note

This method should create leader key with value of _name and ttl of ttl.

Since it could be called only on initial cluster bootstrap it could create this key regardless, overwriting the key if necessary.

Returns

True if successfully committed to DCS.

touch_member(data: Dict[str, Any]) bool

Update member key in DCS.

Note

This method should create or update key with the name with /members/ + _name and the value of data in a given DCS.

Parameters

data – information about an instance (including connection strings).

Returns

True if successfully committed to DCS.

property ttl: int

Get current ttl value.

watch(leader_version: Optional[int], timeout: float) bool

Sleep if the current node is a leader, otherwise, watch for changes of leader key with a given timeout.

Parameters
  • leader_version – version of a leader key.

  • timeout – timeout in seconds.

Returns

if True this will reschedule the next run of the HA cycle.

exception patroni.dcs.zookeeper.ZooKeeperError(value: Any)

Bases: patroni.exceptions.DCSError