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 ) View on GitHub

Bases: KazooClient

_call ( request : Tuple [ Any ] , async_object : AsyncResult ) bool | None View on GitHub

Ensure the client is in CONNECTED or SUSPENDED state and put the request in the queue if it is.

Returns False if the call short circuits due to AUTH_FAILED, CLOSED, or EXPIRED_SESSION state.

class patroni.dcs.zookeeper. PatroniSequentialThreadingHandler ( connect_timeout : int | float ) View on GitHub

Bases: SequentialThreadingHandler

__init__ ( connect_timeout : int | float ) None View on GitHub

Create a SequentialThreadingHandler instance

create_connection ( * args : Any , ** kwargs : Any ) socket View on GitHub
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 View on GitHub

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 : int | float ) None View on GitHub
class patroni.dcs.zookeeper. ZooKeeper ( config : Dict [ str , Any ] ) View on GitHub

Bases: AbstractDCS

__init__ ( config : Dict [ str , Any ] ) None View on GitHub

Prepare DCS paths, Citus group ID, initial values for state information and processing dependencies.

Variables :

config dict , reference to config section of selected DCS. i.e.: zookeeper for zookeeper, etcd for etcd, etc…

_abc_impl = <_abc._abc_data object>
_cancel_initialization ( ) None View on GitHub
_citus_cluster_loader ( path : str ) Dict [ int , Cluster ] View on GitHub

Load and build all Patroni clusters from a single Citus cluster.

Parameters :

path – the path in DCS where to load Cluster(s) from.

Returns :

all Citus groups as dict , with group IDs as keys and Cluster objects as values or a Cluster object representing the coordinator with filled Cluster.workers attribute.

_cluster_loader ( path : str ) Cluster View on GitHub

Load and build the Cluster object from DCS, which represents a single Patroni or Citus cluster.

Parameters :

path – the path in DCS where to load Cluster(s) from.

Returns :

Cluster instance.

_create ( path : str , value : bytes , retry : bool = False , ephemeral : bool = False ) bool View on GitHub
_delete_leader ( leader : Leader ) bool View on GitHub

Remove leader key from DCS.

This method should remove leader key if current instance is the leader.

Parameters :

leader Leader object with information about the leader.

Returns :

True if successfully committed to DCS.

_kazoo_connect ( * args : Any ) Tuple [ int | float , int | float ] View on GitHub

Kazoo is using Ping’s to determine health of connection to zookeeper. If there is no response on Ping after Ping interval (1/2 from read_timeout) it will consider current connection dead and try to connect to another node. Without this "magic" it was taking up to 2/3 from session timeout (ttl) to figure out that connection was dead and we had only small time for reconnect and retry.

This method is needed to return different value of read_timeout, which is not calculated from negotiated session timeout but from value of loop_wait . And it is 2 sec smaller than loop_wait, because we can spend up to 2 seconds when calling touch_member() and write_leader_optime() methods, which also may hang…

_load_cluster ( path : str , loader : Callable [ [ str ] , Cluster | Dict [ int , Cluster ] ] ) Cluster | Dict [ int , Cluster ] View on GitHub

Main abstract method that implements the loading of Cluster instance.

Note

Internally this method should call the loader method that will build Cluster object which represents current state and topology of the cluster in DCS. This method supposed to be called only by the get_cluster() method.

Parameters :
  • path – the path in DCS where to load Cluster(s) from.

  • loader – one of _cluster_loader() or _citus_cluster_loader() .

Raise :

DCSError in case of communication problems with DCS. If the current node was running as a primary and exception raised, instance would be demoted.

_set_or_create ( key : str , value : str , version : int | None = None , retry : bool = False , do_not_create_empty : bool = False ) int | bool View on GitHub
_update_leader ( leader : Leader ) bool View on GitHub

Update leader key (or session) ttl.

Note

You have to use CAS (Compare And Swap) operation in order to update leader key, for example for etcd prevValue parameter must be used.

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

Parameters :

leader – a reference to a current leader object.

Returns :

True if leader key (or session) has been updated successfully.

_watcher ( event : WatchedEvent ) None View on GitHub
_write_failsafe ( value : str ) bool View on GitHub

Write current cluster topology to DCS that will be used by failsafe mechanism (if enabled).

Parameters :

value – failsafe topology serialized in JSON format.

Returns :

True if successfully committed to DCS.

_write_leader_optime ( last_lsn : str ) bool View on GitHub

Write current WAL LSN into /optime/leader key in DCS.

Parameters :

last_lsn – absolute WAL LSN in bytes.

Returns :

True if successfully committed to DCS.

_write_status ( value : str ) bool View on GitHub

Write current WAL LSN and confirmed_flush_lsn of permanent slots into the /status key in DCS.

Parameters :

value – status serialized in JSON format.

Returns :

True if successfully committed to DCS.

attempt_to_acquire_leader ( ) bool View on GitHub

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 View on GitHub

Removes the initialize key for a cluster.

Returns :

True if successfully committed to DCS.

delete_cluster ( ) bool View on GitHub

Delete cluster from DCS.

Returns :

True if successfully committed to DCS.

delete_sync_state ( version : int | None = None ) bool View on GitHub

Delete the synchronous state from DCS.

Parameters :

version – for conditional deletion of the key/object.

Returns :

True if delete successful.

get_children ( key : str ) List [ str ] View on GitHub
get_node ( key : str , watch : Callable [ [ WatchedEvent ] , None ] | None = None ) Tuple [ str , ZnodeStat ] | None View on GitHub
get_status ( path : str , leader : Leader | None ) Status View on GitHub
initialize ( create_new : bool = True , sysid : str = '' ) bool View on GitHub

Race for cluster initialization.

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

Parameters :
  • create_new False 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 [ Member ] View on GitHub
static member ( name : str , value : str , znode : ZnodeStat ) Member View on GitHub
reload_config ( config : Config | Dict [ str , Any ] ) None View on GitHub

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.

set_config_value ( value : str , version : int | None = None ) bool View on GitHub

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 : int | None = None ) bool View on GitHub

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 View on GitHub

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 View on GitHub

Set the new value for retry_timeout .

set_sync_state_value ( value : str , version : int | None = None ) int | bool View on GitHub

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 ) bool | None View on GitHub

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).

take_leader ( ) bool View on GitHub

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 View on GitHub

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 View on GitHub

Get current ttl value.

watch ( leader_version : int | None , timeout : float ) bool View on GitHub

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 ) View on GitHub

Bases: DCSError