Submodules
- patroni.dcs.consul module
-
patroni.dcs.etcd module
-
AbstractEtcd-
AbstractEtcd.__init__() -
AbstractEtcd._abc_impl -
AbstractEtcd._client -
AbstractEtcd._handle_exception() -
AbstractEtcd._run_and_handle_exceptions() -
AbstractEtcd.get_etcd_client() -
AbstractEtcd.handle_etcd_exceptions() -
AbstractEtcd.reload_config() -
AbstractEtcd.retry() -
AbstractEtcd.set_retry_timeout() -
AbstractEtcd.set_socket_options() -
AbstractEtcd.set_ttl() -
AbstractEtcd.ttl
-
-
AbstractEtcdClientWithFailover-
AbstractEtcdClientWithFailover.ERROR_CLS -
AbstractEtcdClientWithFailover.__init__() -
AbstractEtcdClientWithFailover._abc_impl -
AbstractEtcdClientWithFailover._calculate_timeouts() -
AbstractEtcdClientWithFailover._do_http_request() -
AbstractEtcdClientWithFailover._get_headers() -
AbstractEtcdClientWithFailover._get_machines_cache_from_config() -
AbstractEtcdClientWithFailover._get_machines_cache_from_dns() -
AbstractEtcdClientWithFailover._get_machines_cache_from_srv() -
AbstractEtcdClientWithFailover._get_machines_list() -
AbstractEtcdClientWithFailover._get_members() -
AbstractEtcdClientWithFailover._load_machines_cache() -
AbstractEtcdClientWithFailover._prepare_common_parameters() -
AbstractEtcdClientWithFailover._prepare_get_members() -
AbstractEtcdClientWithFailover._prepare_request() -
AbstractEtcdClientWithFailover._refresh_machines_cache() -
AbstractEtcdClientWithFailover._update_dns_cache() -
AbstractEtcdClientWithFailover.api_execute() -
AbstractEtcdClientWithFailover.get_srv_record() -
AbstractEtcdClientWithFailover.machines -
AbstractEtcdClientWithFailover.machines_cache -
AbstractEtcdClientWithFailover.reload_config() -
AbstractEtcdClientWithFailover.set_base_uri() -
AbstractEtcdClientWithFailover.set_machines_cache_ttl() -
AbstractEtcdClientWithFailover.set_read_timeout()
-
-
DnsCachingResolver -
Etcd-
Etcd.__init__() -
Etcd._abc_impl -
Etcd._citus_cluster_loader() -
Etcd._client -
Etcd._cluster_from_nodes() -
Etcd._cluster_loader() -
Etcd._delete_leader() -
Etcd._do_attempt_to_acquire_leader() -
Etcd._do_update_leader() -
Etcd._load_cluster() -
Etcd._update_leader() -
Etcd._write_failsafe() -
Etcd._write_leader_optime() -
Etcd._write_status() -
Etcd.attempt_to_acquire_leader() -
Etcd.cancel_initialization() -
Etcd.delete_cluster() -
Etcd.delete_sync_state() -
Etcd.initialize() -
Etcd.member() -
Etcd.set_config_value() -
Etcd.set_failover_value() -
Etcd.set_history_value() -
Etcd.set_sync_state_value() -
Etcd.set_ttl() -
Etcd.take_leader() -
Etcd.touch_member() -
Etcd.watch()
-
-
EtcdClient -
EtcdError -
EtcdRaftInternal -
catch_etcd_errors()
-
-
patroni.dcs.etcd3 module
-
AuthFailed -
AuthNotEnabled -
AuthOldRevision -
DeadlineExceeded -
Etcd3-
Etcd3.__init__() -
Etcd3._abc_impl -
Etcd3._citus_cluster_loader() -
Etcd3._client -
Etcd3._cluster_from_nodes() -
Etcd3._cluster_loader() -
Etcd3._delete_leader() -
Etcd3._do_attempt_to_acquire_leader() -
Etcd3._do_refresh_lease() -
Etcd3._load_cluster() -
Etcd3._update_leader() -
Etcd3._write_failsafe() -
Etcd3._write_leader_optime() -
Etcd3._write_status() -
Etcd3.attempt_to_acquire_leader() -
Etcd3.cancel_initialization() -
Etcd3.cluster_prefix -
Etcd3.create_lease() -
Etcd3.delete_cluster() -
Etcd3.delete_sync_state() -
Etcd3.initialize() -
Etcd3.member() -
Etcd3.refresh_lease() -
Etcd3.set_config_value() -
Etcd3.set_failover_value() -
Etcd3.set_history_value() -
Etcd3.set_socket_options() -
Etcd3.set_sync_state_value() -
Etcd3.set_ttl() -
Etcd3.take_leader() -
Etcd3.touch_member() -
Etcd3.watch()
-
-
Etcd3Client-
Etcd3Client.ERROR_CLS -
Etcd3Client.__init__() -
Etcd3Client._abc_impl -
Etcd3Client._ensure_version_prefix() -
Etcd3Client._get_headers() -
Etcd3Client._get_members() -
Etcd3Client._handle_server_response() -
Etcd3Client._prepare_get_members() -
Etcd3Client._prepare_request() -
Etcd3Client.authenticate() -
Etcd3Client.call_rpc() -
Etcd3Client.deleteprefix() -
Etcd3Client.deleterange() -
Etcd3Client.handle_auth_errors() -
Etcd3Client.lease_grant() -
Etcd3Client.lease_keepalive() -
Etcd3Client.prefix() -
Etcd3Client.put() -
Etcd3Client.range() -
Etcd3Client.txn() -
Etcd3Client.watchprefix() -
Etcd3Client.watchrange()
-
-
Etcd3ClientError -
Etcd3Error -
Etcd3Exception -
FailedPrecondition -
GRPCCode-
GRPCCode.Aborted -
GRPCCode.AlreadyExists -
GRPCCode.Canceled -
GRPCCode.DataLoss -
GRPCCode.DeadlineExceeded -
GRPCCode.FailedPrecondition -
GRPCCode.Internal -
GRPCCode.InvalidArgument -
GRPCCode.NotFound -
GRPCCode.OK -
GRPCCode.OutOfRange -
GRPCCode.PermissionDenied -
GRPCCode.ResourceExhausted -
GRPCCode.Unauthenticated -
GRPCCode.Unavailable -
GRPCCode.Unimplemented -
GRPCCode.Unknown
-
-
InvalidArgument -
InvalidAuthToken -
KVCache -
LeaseNotFound -
NotFound -
PatroniEtcd3Client-
PatroniEtcd3Client.__init__() -
PatroniEtcd3Client._abc_impl -
PatroniEtcd3Client._restart_watcher() -
PatroniEtcd3Client._wait_cache() -
PatroniEtcd3Client.authenticate() -
PatroniEtcd3Client.call_rpc() -
PatroniEtcd3Client.configure() -
PatroniEtcd3Client.get_cluster() -
PatroniEtcd3Client.set_base_uri() -
PatroniEtcd3Client.start_watcher() -
PatroniEtcd3Client.txn()
-
-
PermissionDenied -
ReAuthenticateMode -
Unavailable -
Unknown -
UnsupportedEtcdVersion -
UserEmpty -
_handle_auth_errors() -
_raise_for_data() -
base64_decode() -
base64_encode() -
build_range_request() -
prefix_range_end() -
to_bytes()
-
- patroni.dcs.exhibitor module
-
patroni.dcs.kubernetes module
-
CoreV1ApiProxy -
K8sClient-
K8sClient.ApiClient-
K8sClient.ApiClient._API_URL_PREFIX -
K8sClient.ApiClient.__init__() -
K8sClient.ApiClient._calculate_timeouts() -
K8sClient.ApiClient._do_http_request() -
K8sClient.ApiClient._get_api_servers() -
K8sClient.ApiClient._handle_server_response() -
K8sClient.ApiClient._load_api_servers_cache() -
K8sClient.ApiClient._make_headers() -
K8sClient.ApiClient._refresh_api_servers_cache() -
K8sClient.ApiClient.api_servers_cache -
K8sClient.ApiClient.call_api() -
K8sClient.ApiClient.refresh_api_servers_cache() -
K8sClient.ApiClient.request() -
K8sClient.ApiClient.set_api_servers_cache_ttl() -
K8sClient.ApiClient.set_base_uri() -
K8sClient.ApiClient.set_read_timeout()
-
-
K8sClient.CoreV1Api -
K8sClient._K8sObjectTemplate -
K8sClient.__init__() -
K8sClient.rest
-
-
K8sConfig -
K8sConnectionFailed -
K8sException -
K8sObject -
Kubernetes-
Kubernetes._CITUS_LABEL -
Kubernetes.__init__() -
Kubernetes.__load_cluster() -
Kubernetes.__target_ref() -
Kubernetes._abc_impl -
Kubernetes._citus_cluster_loader() -
Kubernetes._cluster_from_nodes() -
Kubernetes._cluster_loader() -
Kubernetes._config_resource_version -
Kubernetes._create_config_service() -
Kubernetes._delete_leader() -
Kubernetes._load_cluster() -
Kubernetes._map_subsets() -
Kubernetes._patch_or_create() -
Kubernetes._update_leader() -
Kubernetes._update_leader_with_retry() -
Kubernetes._wait_caches() -
Kubernetes._write_failsafe() -
Kubernetes._write_leader_optime() -
Kubernetes._write_status() -
Kubernetes.attempt_to_acquire_leader() -
Kubernetes.cancel_initialization() -
Kubernetes.client_path() -
Kubernetes.compare_ports() -
Kubernetes.delete_cluster() -
Kubernetes.delete_leader() -
Kubernetes.delete_sync_state() -
Kubernetes.get_citus_coordinator() -
Kubernetes.initialize() -
Kubernetes.leader_path -
Kubernetes.manual_failover() -
Kubernetes.member() -
Kubernetes.patch_or_create() -
Kubernetes.patch_or_create_config() -
Kubernetes.reload_config() -
Kubernetes.retry() -
Kubernetes.set_config_value() -
Kubernetes.set_failover_value() -
Kubernetes.set_history_value() -
Kubernetes.set_retry_timeout() -
Kubernetes.set_sync_state_value() -
Kubernetes.set_ttl() -
Kubernetes.subsets_changed() -
Kubernetes.take_leader() -
Kubernetes.touch_member() -
Kubernetes.ttl -
Kubernetes.update_leader() -
Kubernetes.watch() -
Kubernetes.write_leader_optime() -
Kubernetes.write_sync_state()
-
-
KubernetesError -
KubernetesRetriableException -
ObjectCache-
ObjectCache.__init__() -
ObjectCache._build_cache() -
ObjectCache._do_watch() -
ObjectCache._finish_response() -
ObjectCache._list() -
ObjectCache._process_event() -
ObjectCache._watch() -
ObjectCache.copy() -
ObjectCache.delete() -
ObjectCache.get() -
ObjectCache.is_ready() -
ObjectCache.kill_stream() -
ObjectCache.run() -
ObjectCache.set()
-
-
_cleanup_temp_files() -
_create_temp_file() -
_run_and_handle_exceptions() -
catch_kubernetes_errors() -
to_camel_case()
-
-
patroni.dcs.raft module
-
DynMemberSyncObj -
KVStoreTTL-
KVStoreTTL.__check_requirements() -
KVStoreTTL.__expire_keys() -
KVStoreTTL.__init__() -
KVStoreTTL.__pop() -
KVStoreTTL.__values_match() -
KVStoreTTL._autoTickThread() -
KVStoreTTL._delete() -
KVStoreTTL._delete_v0() -
KVStoreTTL._expire() -
KVStoreTTL._expire_v0() -
KVStoreTTL._onTick() -
KVStoreTTL._set() -
KVStoreTTL._set_v0() -
KVStoreTTL.delete() -
KVStoreTTL.destroy() -
KVStoreTTL.get() -
KVStoreTTL.retry() -
KVStoreTTL.set() -
KVStoreTTL.set_retry_timeout() -
KVStoreTTL.startAutoTick()
-
-
Raft-
Raft.__init__() -
Raft._abc_impl -
Raft._citus_cluster_loader() -
Raft._cluster_from_nodes() -
Raft._cluster_loader() -
Raft._delete_leader() -
Raft._load_cluster() -
Raft._on_delete() -
Raft._on_set() -
Raft._update_leader() -
Raft._write_failsafe() -
Raft._write_leader_optime() -
Raft._write_status() -
Raft.attempt_to_acquire_leader() -
Raft.cancel_initialization() -
Raft.delete_cluster() -
Raft.delete_sync_state() -
Raft.initialize() -
Raft.member() -
Raft.reload_config() -
Raft.set_config_value() -
Raft.set_failover_value() -
Raft.set_history_value() -
Raft.set_retry_timeout() -
Raft.set_sync_state_value() -
Raft.set_ttl() -
Raft.take_leader() -
Raft.touch_member() -
Raft.ttl -
Raft.watch()
-
-
RaftError -
SyncObjUtility -
_TCPTransport -
resolve_host()
-
-
patroni.dcs.zookeeper module
-
PatroniKazooClient -
PatroniSequentialThreadingHandler -
ZooKeeper-
ZooKeeper.__init__() -
ZooKeeper._abc_impl -
ZooKeeper._cancel_initialization() -
ZooKeeper._citus_cluster_loader() -
ZooKeeper._cluster_loader() -
ZooKeeper._create() -
ZooKeeper._delete_leader() -
ZooKeeper._kazoo_connect() -
ZooKeeper._load_cluster() -
ZooKeeper._set_or_create() -
ZooKeeper._update_leader() -
ZooKeeper._watcher() -
ZooKeeper._write_failsafe() -
ZooKeeper._write_leader_optime() -
ZooKeeper._write_status() -
ZooKeeper.attempt_to_acquire_leader() -
ZooKeeper.cancel_initialization() -
ZooKeeper.delete_cluster() -
ZooKeeper.delete_sync_state() -
ZooKeeper.get_children() -
ZooKeeper.get_node() -
ZooKeeper.get_status() -
ZooKeeper.initialize() -
ZooKeeper.load_members() -
ZooKeeper.member() -
ZooKeeper.reload_config() -
ZooKeeper.set_config_value() -
ZooKeeper.set_failover_value() -
ZooKeeper.set_history_value() -
ZooKeeper.set_retry_timeout() -
ZooKeeper.set_sync_state_value() -
ZooKeeper.set_ttl() -
ZooKeeper.take_leader() -
ZooKeeper.touch_member() -
ZooKeeper.ttl -
ZooKeeper.watch()
-
-
ZooKeeperError
-
Module contents
Abstract classes for Distributed Configuration Store.
- class patroni.dcs. AbstractDCS ( config : Dict [ str , Any ] ) View on GitHub
-
Bases:
ABCAbstract representation of DCS modules.
Implementations of a concrete DCS class, using appropriate backend client interfaces, must include the following methods and properties.
Functional methods that are critical in their timing, required to complete within
retry_timeoutperiod in order to prevent the DCS considered inaccessible, each perform construction of complex data objects:-
-
_cluster_loader(): -
method which processes the structure of data stored in the DCS used to build the
Clusterobject with all relevant associated data.
-
-
-
_citus_cluster_loader(): -
Similar to above but specifically representing Citus group and workers information.
-
-
-
_load_cluster(): -
main method for calling specific
loadermethod to build theClusterobject representing the state and topology of the cluster.
-
Functional methods that are critical in their timing and must be written with ACID transaction properties in mind:
-
-
attempt_to_acquire_leader(): -
method used in the leader race to attempt to acquire the leader lock by creating the leader key in the DCS, if it does not exist.
-
-
-
_update_leader(): -
method to update
leaderkey in DCS. Relies on Compare-And-Set to ensure the Primary lock key is updated. If this fails to update within theretry_timeoutwindow the Primary will be demoted.
-
Functional method that relies on Compare-And-Create to ensure only one member creates the relevant key:
-
-
initialize(): -
method used in the race for cluster initialization which creates the
initializekey in the DCS.
-
DCS backend getter and setter methods and properties:
-
take_leader(): method to create a new leader key in the DCS. -
set_ttl(): method for setting TTL value in DCS. -
ttl(): property which returns the current TTL. -
set_retry_timeout(): method for settingretry_timeoutin DCS backend. -
_write_leader_optime(): compatibility method to write WAL LSN to DCS. -
_write_status(): method to write WAL LSN for slots to the DCS. -
_write_failsafe(): method to write cluster topology to the DCS, used by failsafe mechanism. -
touch_member(): method to update individual member key in the DCS. -
set_history_value(): method to set thehistorykey in the DCS.
DCS setter methods using Compare-And-Set which although important are less critical if they fail, attempts can be retried or may result in warning log messages:
-
set_failover_value(): method to create and/or update thefailoverkey in the DCS. -
set_config_value(): method to create and/or update thefailoverkey in the DCS. -
set_sync_state_value(): method to set the synchronous statesynckey in the DCS.
DCS data and key removal methods:
-
-
delete_sync_state(): -
likewise, a method to remove synchronous state
synckey from the DCS.
-
-
-
delete_cluster(): -
method which will remove cluster information from the DCS. Used only from
patronictl.
-
-
-
_delete_leader(): -
method relies on CAS, used by a member that is the current leader, to remove the
leaderkey in the DCS.
-
-
-
cancel_initialization(): -
method to remove the
initializekey for the cluster from the DCS.
-
If either of the
sync_stateset or delete methods fail, although not critical, this may result inSynchronous replication key updated by someone elsemessages being logged.Care should be taken to consult each abstract method for any additional information and requirements such as expected exceptions that should be raised in certain conditions and the object types for arguments and return from methods and properties.
- _CONFIG = 'config'
- _FAILOVER = 'failover'
- _FAILSAFE = 'failsafe'
- _HISTORY = 'history'
- _INITIALIZE = 'initialize'
- _LEADER = 'leader'
- _LEADER_OPTIME = 'optime/leader'
- _MEMBERS = 'members/'
- _OPTIME = 'optime'
- _STATUS = 'status'
- _SYNC = 'sync'
- __get_patroni_cluster ( path : str | None = None ) Cluster
-
Low level method to load a
Clusterobject from DCS.- Parameters :
-
path – optional client path in DCS backend to load from.
- Returns :
-
a loaded
Clusterinstance.
- __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.:zookeeperfor zookeeper,etcdfor etcd, etc…
- _abc_impl = <_abc._abc_data object>
- abstract _citus_cluster_loader ( path : Any ) Dict [ int , Cluster ] View on GitHub
-
Load and build all Patroni clusters from a single Citus cluster.
- abstract _cluster_loader ( path : Any ) Cluster View on GitHub
-
Load and build the
Clusterobject from DCS, which represents a single Patroni or Citus cluster.- Parameters :
-
path – the path in DCS where to load Cluster(s) from.
- Returns :
-
Clusterinstance.
- abstract _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 –
Leaderobject with information about the leader. - Returns :
-
Trueif successfully committed to DCS.
- _get_citus_cluster ( ) Cluster View on GitHub
-
Load Citus cluster from DCS.
- Returns :
-
A Citus
Clusterinstance for the coordinator with workers clusters in theCluster.workersdict.
- abstract _load_cluster ( path : str , loader : Callable [ [ Any ] , Cluster | Dict [ int , Cluster ] ] ) Cluster | Dict [ int , Cluster ] View on GitHub
-
Main abstract method that implements the loading of
Clusterinstance.Note
Internally this method should call the loader method that will build
Clusterobject which represents current state and topology of the cluster in DCS. This method supposed to be called only by theget_cluster()method.- Parameters :
-
-
path – the path in DCS where to load Cluster(s) from.
-
loader – one of
_cluster_loader()or_citus_cluster_loader().
-
- Raise :
-
DCSErrorin case of communication problems with DCS. If the current node was running as a primary and exception raised, instance would be demoted.
- _set_loop_wait ( loop_wait : int ) None View on GitHub
-
Set new loop_wait value.
- Parameters :
-
loop_wait – value to set.
- abstract _update_leader ( leader : Leader ) bool View on GitHub
-
Update
leaderkey (or session) ttl.Note
You have to use CAS (Compare And Swap) operation in order to update leader key, for example for etcd
prevValueparameter must be used.If update fails due to DCS not being accessible or because it is not able to process requests (hopefully temporary), the
DCSErrorexception should be raised.- Parameters :
-
leader – a reference to a current
leaderobject. - Returns :
-
Trueifleaderkey (or session) has been updated successfully.
- abstract _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 :
-
Trueif successfully committed to DCS.
- abstract _write_leader_optime ( last_lsn : str ) bool View on GitHub
-
Write current WAL LSN into
/optime/leaderkey in DCS.- Parameters :
-
last_lsn – absolute WAL LSN in bytes.
- Returns :
-
Trueif successfully committed to DCS.
- abstract _write_status ( value : str ) bool View on GitHub
-
Write current WAL LSN and
confirmed_flush_lsnof permanent slots into the/statuskey in DCS.- Parameters :
-
value – status serialized in JSON format.
- Returns :
-
Trueif successfully committed to DCS.
- abstract attempt_to_acquire_leader ( ) bool View on GitHub
-
Attempt to acquire leader lock.
Note
This method should create
/leaderkey with the value_name.The key must be created atomically. In case the key already exists it should not be overwritten and
Falsemust be returned.If key creation fails due to DCS not being accessible or because it is not able to process requests (hopefully temporary), the
DCSErrorexception should be raised.- Returns :
-
Trueif key has been created successfully.
- abstract cancel_initialization ( ) bool View on GitHub
-
Removes the
initializekey for a cluster.- Returns :
-
Trueif successfully committed to DCS.
- client_path ( path : str ) str View on GitHub
-
Construct the absolute key name from appropriate parts for the DCS type.
- Parameters :
-
path – The key name within the current Patroni cluster.
- Returns :
-
absolute key name for the current Patroni cluster.
- property cluster : Cluster | None View on GitHub
-
Cached DCS cluster information that has not yet expired.
- property config_path : str View on GitHub
-
Get the client path for
config.
- abstract delete_cluster ( ) bool View on GitHub
-
Delete cluster from DCS.
- Returns :
-
Trueif successfully committed to DCS.
- delete_leader ( leader : Leader | None , last_lsn : int | None = None ) bool View on GitHub
-
Update
optime/leaderand voluntarily remove leader key from DCS.This method should remove leader key if current instance is the leader.
- Parameters :
-
-
leader –
Leaderobject with information about the leader. -
last_lsn – latest checkpoint location in bytes.
-
- Returns :
-
boolean result of called abstract
_delete_leader().
- abstract delete_sync_state ( version : Any | None = None ) bool View on GitHub
-
Delete the synchronous state from DCS.
- Parameters :
-
version – for conditional deletion of the key/object.
- Returns :
-
Trueif delete successful.
- property failover_path : str View on GitHub
-
Get the client path for
failover.
- property failsafe : Dict [ str , str ] | None View on GitHub
-
Stored value of
_last_failsafe.
- property failsafe_path : str View on GitHub
-
Get the client path for
failsafe.
- get_citus_coordinator ( ) Cluster | None View on GitHub
-
Load the Patroni cluster for the Citus Coordinator.
Note
This method is only executed on the worker nodes (
group!=0) to find the coordinator.- Returns :
-
Select
Clusterinstance associated with the Citus Coordinator group ID.
- get_cluster ( ) Cluster View on GitHub
-
Retrieve a fresh view of DCS.
Note
Stores copy of time, status and failsafe values for comparison in DCS update decisions. Caching is required to avoid overhead placed upon the REST API.
Returns either a Citus or Patroni implementation of
Clusterdepending on availability.- Returns :
-
- property history_path : str View on GitHub
-
Get the client path for
history.
- abstract initialize ( create_new : bool = True , sysid : str = '' ) bool View on GitHub
-
Race for cluster initialization.
This method should atomically create
initializekey and returnTrue, otherwise it should returnFalse.- Parameters :
-
-
create_new –
Falseif 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 :
-
Trueif key has been created successfully.
- property initialize_path : str View on GitHub
-
Get the client path for
initialize.
- is_citus_coordinator ( ) bool View on GitHub
-
Clusterinstance has a Citus Coordinator group ID.- Returns :
-
Trueif the given node is running as Citus Coordinator (group=0).
- property last_seen : int View on GitHub
-
The time recorded when the DCS was last reachable.
- property leader_optime_path : str View on GitHub
-
Get the client path for
optime/leader(legacy key, superseded bystatus).
- property leader_path : str View on GitHub
-
Get the client path for
leader.
- property loop_wait : int View on GitHub
-
The recorded value for cluster HA loop wait time in seconds.
- manual_failover ( leader : str | None , candidate : str | None , scheduled_at : datetime | None = None , version : Any | None = None ) bool View on GitHub
-
Prepare dictionary with given values and set
/failoverkey in DCS.- Parameters :
-
-
leader – value to set for
leader. -
candidate – value to set for
member. -
scheduled_at – value converted to ISO date format for
scheduled_at. -
version – for conditional update of the key/object.
-
- Returns :
-
Trueif successfully committed to DCS.
- property member_path : str View on GitHub
-
Get the client path for
memberrepresenting this node.
- property members_path : str View on GitHub
-
Get the client path for
members.
- reload_config ( config : Config | Dict [ str , Any ] ) None View on GitHub
-
Load and set relevant values from configuration.
Sets
loop_wait,ttlandretry_timeoutproperties.- Parameters :
-
config – Loaded configuration information object or dictionary of key value pairs.
- reset_cluster ( ) None View on GitHub
-
Clear cached state of DCS.
- abstract set_config_value ( value : str , version : Any | None = None ) bool View on GitHub
-
Create or update
/configkey in DCS.- Parameters :
-
-
value – new value to set in the
configkey. -
version – for conditional update of the key/object.
-
- Returns :
-
Trueif successfully committed to DCS.
- abstract set_failover_value ( value : str , version : Any | None = None ) bool View on GitHub
-
Create or update
/failoverkey.- Parameters :
-
-
value – value to set.
-
version – for conditional update of the key/object.
-
- Returns :
-
Trueif successfully committed to DCS.
- abstract set_history_value ( value : str ) bool View on GitHub
-
Set value for
historyin DCS.- Parameters :
-
value – new value of
historykey/object. - Returns :
-
Trueif successfully committed to DCS.
- abstract set_retry_timeout ( retry_timeout : int ) None View on GitHub
-
Set the new value for retry_timeout .
- abstract set_sync_state_value ( value : str , version : Any | None = None ) Any | bool View on GitHub
-
Set synchronous state in DCS.
- Parameters :
-
-
value – the new value of
/synckey. -
version – for conditional update of the key/object.
-
- Returns :
-
version of the new object or
Falsein case of error.
- abstract set_ttl ( ttl : int ) bool | None View on GitHub
-
Set the new ttl value for DCS keys.
- property status_path : str View on GitHub
-
Get the client path for
status.
- property sync_path : str View on GitHub
-
Get the client path for
sync.
- static sync_state ( leader : str | None , sync_standby : Collection [ str ] | None ) Dict [ str , Any ] View on GitHub
-
Build
sync_statedictionary.- Parameters :
-
-
leader – name of the leader node that manages
/synckey. -
sync_standby – collection of currently known synchronous standby node names.
-
- Returns :
-
dictionary that later could be serialized to JSON or saved directly to DCS.
- abstract take_leader ( ) bool View on GitHub
-
Establish a new leader in DCS.
Note
This method should create leader key with value of
_nameandttlofttl.Since it could be called only on initial cluster bootstrap it could create this key regardless, overwriting the key if necessary.
- Returns :
-
Trueif successfully committed to DCS.
- abstract 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/+_nameand the value of data in a given DCS.- Parameters :
-
data – information about an instance (including connection strings).
- Returns :
-
Trueif successfully committed to DCS.
- abstract property ttl : int View on GitHub
-
Get current
ttlvalue.
- update_leader ( leader : Leader , last_lsn : int | None , slots : Dict [ str , int ] | None = None , failsafe : Dict [ str , str ] | None = None ) bool View on GitHub
-
Update
leaderkey (or session) ttl and optime/leader.- Parameters :
-
-
leader –
Leaderobject with information about the leader. -
last_lsn – absolute WAL LSN in bytes.
-
slots – dictionary with permanent slots
confirmed_flush_lsn. -
failsafe – if defined dictionary passed to
write_failsafe().
-
- Returns :
-
Trueifleaderkey (or session) has been updated successfully.
- watch ( leader_version : Any | 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
Truethis will reschedule the next run of the HA cycle.
- write_failsafe ( value : Dict [ str , str ] ) None View on GitHub
-
Write the
/failsafekey in DCS.- Parameters :
-
value – dictionary value to set, consisting of the
nameandapi_urlof members.
- write_leader_optime ( last_lsn : int ) None View on GitHub
-
Write value for WAL LSN to
optime/leaderkey in DCS.Note
This method abstracts away the required data structure of
write_status(), so it is not needed in the caller. However, theoptime/leaderis only written inwrite_status()when the cluster has members with a Patroni version that is old enough to require it (i.e. the old Patroni version doesn’t understand the new format).- Parameters :
-
last_lsn – absolute WAL LSN in bytes.
- write_status ( value : Dict [ str , Any ] ) None View on GitHub
-
Write cluster status to DCS if changed.
Note
The DCS key
/statuswas introduced in Patroni version 2.1.0. Previous to this the position of last known leader LSN was stored inoptime/leader. This method has detection for backwards compatibility of members with a version older than this.- Parameters :
-
value – JSON serializable dictionary with current WAL LSN and
confirmed_flush_lsnof permanent slots.
- write_sync_state ( leader : str | None , sync_standby : Collection [ str ] | None , version : Any | None = None ) SyncState | None View on GitHub
-
Write the new synchronous state to DCS.
Calls
sync_state()to build a dictionary and then calls DCS specificset_sync_state_value().- Parameters :
-
-
leader – name of the leader node that manages
/synckey. -
sync_standby – collection of currently known synchronous standby node names.
-
version – for conditional update of the key/object.
-
- Returns :
-
the new
SyncStateobject orNone.
-
- class patroni.dcs. Cluster ( * args : Any , ** kwargs : Any ) View on GitHub
-
Bases:
ClusterImmutable object (namedtuple) which represents PostgreSQL or Citus cluster.
Note
We are using an old-style attribute declaration here because otherwise it is not possible to override
__new__method. Without it the workers by default gets always the samedictobject that could be mutated.Consists of the following fields:
- Variables :
-
-
initialize – shows whether this cluster has initialization key stored in DC or not.
-
config – global dynamic configuration, reference to
ClusterConfigobject. -
leader –
Leaderobject which represents current leader of the cluster. -
status –
Statusobject which represents the/statuskey. -
members – list of:class:` Member` objects, all PostgreSQL cluster members including leader
-
failover – reference to
Failoverobject. -
sync – reference to
SyncStateobject, last observed synchronous replication state. -
history – reference to
TimelineHistoryobject. -
failsafe – failsafe topology. Node is allowed to become the leader only if its name is found in this list.
-
workers – dictionary of workers of the Citus cluster, optional. Each key is an
intrepresenting the group, and the corresponding value is aClusterinstance.
-
- property __permanent_logical_slots : Dict [ str , Any ]
-
Dictionary of permanent
logicalreplication slots.
- property __permanent_physical_slots : Dict [ str , Any ]
-
Dictionary of permanent
physicalreplication slots.
- property __permanent_slots : Dict [ str , Dict [ str , Any ] | Any ]
-
Dictionary of permanent replication slots with their known LSN.
- _get_members_slots ( my_name : str , role : str ) Dict [ str , Dict [ str , str ] ] View on GitHub
-
Get physical replication slots configuration for members that sourcing from this node.
If the
replicatefromtag is set on the member - we should not create the replication slot for it on the current primary, because that member would replicate from elsewhere. We still create the slot if thereplicatefromdestination member is currently not a member of the cluster (fallback to the primary), or ifreplicatefromdestination member happens to be the current primary.Will log an error if:
-
Conflicting slot names between members are found
- Parameters :
-
-
my_name – name of this node.
-
role – role of this node, if this is a
primaryorstandby_leaderreturn list of members replicating from this node. If not then return a list of members replicating as cascaded replicas from this node.
-
- Returns :
-
dictionary of physical replication slots that should exist on a given node.
-
- _get_permanent_slots ( * , is_standby_cluster : bool , role : str , nofailover : bool , major_version : int ) Dict [ str , Any ] View on GitHub
-
Get configured permanent replication slots.
Note
Permanent replication slots are only considered if
use_slotsconfiguration is enabled. A node that is not supposed to become a leader ( nofailover ) will not have permanent replication slots.In a standby cluster we only support physical replication slots.
The returned dictionary for a non-standby cluster always contains permanent logical replication slots in order to show a warning if they are not supported by PostgreSQL before v11.
- Parameters :
-
-
is_standby_cluster –
Trueif it is known that this is a standby cluster. We pass the value from the outside because we want to protect from the/configkey removal. -
role – role of this node –
primary,standby_leaderorreplica. -
nofailover –
Trueif this node is tagged to not be a failover candidate. -
major_version – postgresql major version.
-
- Returns :
-
dictionary of permanent slot names mapped to attributes.
- _has_permanent_logical_slots ( my_name : str , nofailover : bool ) bool View on GitHub
-
Check if the given member node has permanent
logicalreplication slots configured.- Parameters :
-
-
my_name – name of the member node to check.
-
nofailover –
Trueif this node is tagged to not be a failover candidate.
-
- Returns :
-
Trueif any detected replications slots arelogical, otherwiseFalse.
- _merge_permanent_slots ( slots : Dict [ str , Dict [ str , str ] ] , permanent_slots : Dict [ str , Any ] , my_name : str , major_version : int ) List [ str ] View on GitHub
-
Merge replication slots for members with permanent_slots .
Perform validation of configured permanent slot name, skipping invalid names.
Will update slots in-line based on
typeof slot,physicalorlogical, and name of node. Type is assumed to bephysicalif there are no attributes stored as the slot value.- Parameters :
-
-
slots – Slot names with existing attributes if known.
-
my_name – name of this node.
-
permanent_slots – dictionary containing slot name key and slot information values.
-
major_version – postgresql major version.
-
- Returns :
-
List of disabled permanent, logical slot names, if postgresql version < 11.
- static empty ( ) Cluster View on GitHub
-
Produce an empty
Clusterinstance.
- filter_permanent_slots ( slots : Dict [ str , int ] , is_standby_cluster : bool , major_version : int ) Dict [ str , int ] View on GitHub
-
Filter out all non-permanent slots from provided slots dict.
- Parameters :
-
-
slots – slot names with LSN values
-
is_standby_cluster –
Trueif it is known that this is a standby cluster. We pass the value from the outside because we want to protect from the/configkey removal. -
major_version – postgresql major version.
-
- Returns :
-
a
dictobject that contains only slots that are known to be permanent.
- get_clone_member ( exclude_name : str ) Member | Leader | None View on GitHub
-
Get member or leader object to use as clone source.
- Parameters :
-
exclude_name – name of a member name to exclude.
- Returns :
-
a randomly selected candidate member from available running members that are configured to as viable sources for cloning (has tag
clonefromin configuration). If no member is appropriate the current leader is used.
- get_member ( member_name : str , fallback_to_leader : bool = True ) Member | Leader | None View on GitHub
- get_my_slot_name_on_primary ( my_name : str , replicatefrom : str | None ) str View on GitHub
-
Canonical slot name for physical replication.
Note
P <– I <– L
In case of cascading replication we have to check not our physical slot, but slot of the replica that connects us to the primary.
- Parameters :
-
-
my_name – the member node name that is replicating.
-
replicatefrom – the Intermediate member name that is configured to replicate for cascading replication.
-
- Returns :
-
The slot name that is in use for physical replication on this no`de.
- get_replication_slots ( my_name : str , role : str , nofailover : bool , major_version : int , * , is_standby_cluster : bool = False , show_error : bool = False ) Dict [ str , Dict [ str , Any ] ] View on GitHub
-
Lookup configured slot names in the DCS, report issues found and merge with permanent slots.
Will log an error if:
-
Any logical slots are disabled, due to version compatibility, and show_error is
True.
- Parameters :
-
-
my_name – name of this node.
-
role – role of this node.
-
nofailover –
Trueif this node is tagged to not be a failover candidate. -
major_version – postgresql major version.
-
is_standby_cluster –
Trueif it is known that this is a standby cluster. We pass the value from the outside because we want to protect from the/configkey removal. -
show_error – if
Truereport error if any disabled logical slots or conflicting slot names are found.
-
- Returns :
-
final dictionary of slot names, after merging with permanent slots and performing sanity checks.
-
- has_member ( member_name : str ) bool View on GitHub
-
Check if the given member name is present in the cluster.
- Parameters :
-
member_name – name to look up in the
members. - Returns :
-
Trueif the member name is found.
- has_permanent_slots ( my_name : str , * , is_standby_cluster : bool = False , nofailover : bool = False , major_version : int = 110000 ) bool View on GitHub
-
Check if the given member node has permanent replication slots configured.
- Parameters :
-
-
my_name – name of the member node to check.
-
is_standby_cluster –
Trueif it is known that this is a standby cluster. We pass the value from the outside because we want to protect from the/configkey removal. -
nofailover –
Trueif this node is tagged to not be a failover candidate. -
major_version – postgresql major version.
-
- Returns :
-
Trueif there are permanent replication slots configured, otherwiseFalse.
- is_empty ( ) View on GitHub
-
Validate definition of all attributes of this
Clusterinstance.- Returns :
-
Trueif all attributes of the currentClusterare unpopulated.
- static is_logical_slot ( value : Any | Dict [ str , Any ] ) bool View on GitHub
-
Check whether provided configuration is for permanent logical replication slot.
- Parameters :
-
value – configuration of the permanent replication slot.
- Returns :
-
Trueif value is a logical replication slot, otherwiseFalse.
- static is_physical_slot ( value : Any | Dict [ str , Any ] ) bool View on GitHub
-
Check whether provided configuration is for permanent physical replication slot.
- Parameters :
-
value – configuration of the permanent replication slot.
- Returns :
-
Trueif value is a physical replication slot, otherwiseFalse.
- is_unlocked ( ) bool View on GitHub
-
Check if the cluster does not have the leader.
- Returns :
-
Trueif a leader name is not defined.
- property last_lsn : int View on GitHub
-
Last known leader LSN.
- property leader_name : str | None View on GitHub
-
The name of the leader if defined otherwise
None.
- property min_version : Tuple [ int , ... ] | None View on GitHub
-
Lowest Patroni software version found in known members of the cluster.
- should_enforce_hot_standby_feedback ( my_name : str , nofailover : bool ) bool View on GitHub
-
Determine whether
hot_standby_feedbackshould be enabled for the given member.The
hot_standby_feedbackmust be enabled if the current replica haslogicalslots, or it is working as a cascading replica for the other node that haslogicalslots.- Parameters :
-
-
my_name – name of the member node to check.
-
nofailover –
Trueif this node is tagged to not be a failover candidate.
-
- Returns :
-
Trueif this node or any member replicating from this node has permanent logical slots, otherwiseFalse.
- property slots : Dict [ str , int ] | None View on GitHub
-
{"slot_name": int}.- Type :
-
State of permanent replication slots on the primary in the format
- property timeline : int View on GitHub
-
Get the cluster history index from the
history.- Returns :
-
If the recorded history is empty assume timeline is
1, if it is not defined or the stored history is not formatted as expected0is returned and an error will be logged. Otherwise, the last number stored incremented by 1 is returned. - Example :
-
No history provided: >>> Cluster(0, 0, 0, Status.empty(), 0, 0, 0, 0, None, {}).timeline 0
Empty history assume timeline is
1: >>> Cluster(0, 0, 0, Status.empty(), 0, 0, 0, TimelineHistory.from_node(1, ‘[]’), None, {}).timeline 1Invalid history format, a string of
a, returns0: >>> Cluster(0, 0, 0, Status.empty(), 0, 0, 0, TimelineHistory.from_node(1, ‘[["a"]]’), None, {}).timeline 0History as a list of strings: >>> history = TimelineHistory.from_node(1, ‘[["3", "2", "1"]]’) >>> Cluster(0, 0, 0, Status.empty(), 0, 0, 0, history, None, {}).timeline 4
- property use_slots : bool View on GitHub
-
Trueif cluster is configured to use replication slots.
- class patroni.dcs. ClusterConfig ( version : int | str , data : Dict [ str , Any ] , modify_version : int | str ) View on GitHub
-
Bases:
NamedTupleImmutable object (namedtuple) which represents cluster configuration.
- Variables :
-
-
version – version number for the object.
-
data – dictionary of configuration information.
-
modify_version – modified version number.
-
- _asdict ( )
-
Return a new dict which maps field names to their values.
- _field_defaults = {}
- _fields = ('version', 'data', 'modify_version')
- classmethod _make ( iterable )
-
Make a new ClusterConfig object from a sequence or iterable
- _replace ( ** kwds )
-
Return a new ClusterConfig object replacing specified fields with new values
- static from_node ( version : int | str , value : str , modify_version : int | str | None = None ) ClusterConfig View on GitHub
-
Factory method to parse value as configuration information.
- Parameters :
-
-
version – version number for object.
-
value – raw JSON serialized data, if not parsable replaced with an empty dictionary.
-
modify_version – optional modify version number, use version if not provided.
-
- Returns :
-
constructed
ClusterConfiginstance. - Example :
-
>>> ClusterConfig.from_node(1, '{') is None False
- property ignore_slots_matchers : List [ Dict [ str , Any ] ] View on GitHub
-
The value for
ignore_slotsfromdataif defined or an empty list.
- property max_timelines_history : int View on GitHub
-
The value for
max_timelines_historyfromdataif defined or0.
- property permanent_slots : Dict [ str , Any ] View on GitHub
-
Dictionary of permanent slots information looked up from
data.
- class patroni.dcs. Failover ( version : int | str , leader : str | None , candidate : str | None , scheduled_at : datetime | None ) View on GitHub
-
Bases:
NamedTupleImmutable object (namedtuple) representing configuration information required for failover/switchover capability.
- Variables :
-
-
version – version of the object.
-
leader – name of the leader. If value isn’t empty we treat it as a switchover from the specified node.
-
candidate – the name of the member node to be considered as a failover candidate.
-
scheduled_at – in the case of a switchover the
datetimeobject to perform the scheduled switchover.
-
- Example :
-
>>> 'Failover' in str(Failover.from_node(1, '{"leader": "cluster_leader"}')) True
>>> 'Failover' in str(Failover.from_node(1, {"leader": "cluster_leader"})) True
>>> 'Failover' in str(Failover.from_node(1, '{"leader": "cluster_leader", "member": "cluster_candidate"}')) True
>>> Failover.from_node(1, 'null') is None False
>>> n = '''{"leader": "cluster_leader", "member": "cluster_candidate", ... "scheduled_at": "2016-01-14T10:09:57.1394Z"}'''
>>> 'tzinfo=' in str(Failover.from_node(1, n)) True
>>> Failover.from_node(1, None) is None False
>>> Failover.from_node(1, '{}') is None False
>>> 'abc' in Failover.from_node(1, 'abc:def') True
- _asdict ( )
-
Return a new dict which maps field names to their values.
- _field_defaults = {}
- _fields = ('version', 'leader', 'candidate', 'scheduled_at')
- classmethod _make ( iterable )
-
Make a new Failover object from a sequence or iterable
- _replace ( ** kwds )
-
Return a new Failover object replacing specified fields with new values
- static from_node ( version : int | str , value : str | Dict [ str , str ] ) Failover View on GitHub
-
Factory method to parse value as failover configuration.
- Parameters :
-
-
version – version number for the object.
-
value – JSON serialized data or a dictionary of configuration. Can also be a colon
:delimited list of leader, followed by candidate name (legacy format). Ifscheduled_atkey is defined the value will be parsed bydateutil.parser.parse().
-
- Returns :
-
constructed
Failoverinformation object
- class patroni.dcs. Leader ( version : int | str , session : int | float | str | None , member : Member ) View on GitHub
-
Bases:
NamedTupleImmutable object (namedtuple) which represents leader key.
Consists of the following fields:
- Variables :
-
-
version – modification version of a leader key in a Configuration Store
-
session – either session id or just ttl in seconds
-
member – reference to a
Memberobject which represents current leader (seeCluster.members)
-
- _asdict ( )
-
Return a new dict which maps field names to their values.
- _field_defaults = {}
- _fields = ('version', 'session', 'member')
- classmethod _make ( iterable )
-
Make a new Leader object from a sequence or iterable
- _replace ( ** kwds )
-
Return a new Leader object replacing specified fields with new values
- property checkpoint_after_promote : bool | None View on GitHub
-
Determine whether a checkpoint has occurred for this leader after promotion.
- Returns :
-
Trueif the role ismasterorprimaryandcheckpoint_after_promoteis not set,Falseif not amasterorprimaryor if the checkpoint hasn’t occurred. If the version of Patroni is older than 1.5.6, returnNone. - Example :
-
>>> Leader(1, '', Member.from_node(1, '', '', '{"version":"z"}')).checkpoint_after_promote
- conn_kwargs ( auth : Dict [ str , str ] | None = None ) Dict [ str , str ] View on GitHub
-
Connection keyword arguments.
- Parameters :
-
auth – an optional dictionary containing authentication information.
- Returns :
-
the result of the called
Member.conn_kwargs()method.
- property conn_url : str | None View on GitHub
-
Connection URL value of the
Memberinstance.
- property data : Dict [ str , Any ] View on GitHub
-
Data value of the
Memberinstance.
- member : Member
-
Alias for field number 2
- property name : str View on GitHub
-
The leader "member" name.
- property timeline : int | None View on GitHub
-
Timeline value of
data.
- class patroni.dcs. Member ( version : int | str , name : str , session : int | float | str | None , data : Dict [ str , Any ] ) View on GitHub
-
Immutable object (namedtuple) which represents single member of PostgreSQL cluster.
Note
We are using an old-style attribute declaration here because otherwise it is not possible to override
__new__method in theRemoteMemberclass.Note
These two keys in data are always written to the DCS, but care is taken to maintain consistency and resilience from data that is read:
conn_url: connection string containing host, user and password which could be used to access this member.api_url: REST API url of patroni instanceConsists of the following fields:
- Variables :
-
-
version – modification version of a given member key in a Configuration Store.
-
name – name of PostgreSQL cluster member.
-
session – either session id or just ttl in seconds.
-
data – dictionary containing arbitrary data i.e.
conn_url,api_url,xlog_location,state,role,tags, etc…
-
- _abc_impl = <_abc._abc_data object>
- property api_url : str | None View on GitHub
-
The
api_urlvalue fromdataif defined.
- property clonefrom : bool View on GitHub
-
Trueif bothclonefromtag isTrueand a connection URL is defined.
- conn_kwargs ( auth : Any | Dict [ str , Any ] | None = None ) Dict [ str , Any ] View on GitHub
-
Give keyword arguments used for PostgreSQL connection settings.
- Parameters :
-
auth – Authentication properties - can be defined as anything supported by the
psycopg2orpsycopgmodules. Converts a key ofusernametouserif supplied. - Returns :
-
A dictionary containing a merge of default parameter keys
host,portanddbname, with the contents ofdataconn_kwargskey. If those are not defined will parse and reform connection parameters fromconn_url. One of these two attributes needs to have data defined to construct the output dictionary. Finally, auth parameters are merged with the dictionary before returned.
- property conn_url : str | None View on GitHub
-
The
conn_urlvalue fromdataif defined or constructed fromconn_kwargs.
- static from_node ( version : int | str , name : str , session : int | float | str | None , value : str ) Member View on GitHub
-
Factory method for instantiating
Memberfrom a JSON serialised string or object.- Parameters :
-
-
version – modification version of a given member key in a Configuration Store.
-
name – name of PostgreSQL cluster member.
-
session – either session id or just ttl in seconds.
-
value – JSON encoded string containing arbitrary data i.e.
conn_url,api_url,xlog_location,state,role,tags, etc. OR a connection URL starting withpostgres://.
-
- Returns :
-
an
Memberinstance built with the given arguments. - Example :
-
>>> Member.from_node(-1, '', '', '{"conn_url": "postgres://foo@bar/postgres"}') is not None True
>>> Member.from_node(-1, '', '', '{') Member(version=-1, name='', session='', data={})
- property is_running : bool View on GitHub
-
Trueif the memberstateisrunning.
- property lsn : int | None View on GitHub
-
Current LSN (receive/flush/replay).
- property patroni_version : Tuple [ int , ... ] | None View on GitHub
-
The
versionstring value fromdataconverted to tuple.- Example :
-
>>> Member.from_node(1, '', '', '{"version":"1.2.3"}').patroni_version (1, 2, 3)
- property state : str View on GitHub
-
The
statevalue ofdata.
- property tags : Dict [ str , Any ] View on GitHub
-
The
tagsvalue fromdataif defined, otherwise an empty dictionary.
- class patroni.dcs. RemoteMember ( name : str , data : Dict [ str , Any ] ) View on GitHub
-
Bases:
MemberRepresents a remote member (typically a primary) for a standby cluster.
- Variables :
-
ALLOWED_KEYS – Controls access to relevant key names that could be in stored
data.
- ALLOWED_KEYS : Tuple [ str , ... ] = ('primary_slot_name', 'create_replica_methods', 'restore_command', 'archive_cleanup_command', 'recovery_min_apply_delay', 'no_replication_slot')
- _abc_impl = <_abc._abc_data object>
- exception patroni.dcs. ReturnFalseException View on GitHub
-
Bases:
ExceptionException to be caught by the
catch_return_false_exception()decorator.
- class patroni.dcs. Status ( last_lsn : int , slots : Dict [ str , int ] | None ) View on GitHub
-
Bases:
NamedTupleImmutable object (namedtuple) which represents
/statuskey.Consists of the following fields:
- Variables :
-
-
last_lsn –
intobject containing position of last known leader LSN. -
slots – state of permanent replication slots on the primary in the format:
{"slot_name": int}.
-
- _asdict ( )
-
Return a new dict which maps field names to their values.
- _field_defaults = {}
- _fields = ('last_lsn', 'slots')
- classmethod _make ( iterable )
-
Make a new Status object from a sequence or iterable
- _replace ( ** kwds )
-
Return a new Status object replacing specified fields with new values
- static empty ( ) Status View on GitHub
-
Construct an empty
Statusinstance.- Returns :
-
empty
Statusobject.
- static from_node ( value : str | Dict [ str , Any ] | None ) Status View on GitHub
-
Factory method to parse value as
Statusobject.- Parameters :
-
value – JSON serialized string
- Returns :
-
constructed
Statusobject.
- last_lsn : int
-
Alias for field number 0
- class patroni.dcs. SyncState ( version : int | str | None , leader : str | None , sync_standby : str | None ) View on GitHub
-
Bases:
NamedTupleImmutable object (namedtuple) which represents last observed synchronous replication state.
- Variables :
-
-
version – modification version of a synchronization key in a Configuration Store.
-
leader – reference to member that was leader.
-
sync_standby – synchronous standby list (comma delimited) which are last synchronized to leader.
-
- _asdict ( )
-
Return a new dict which maps field names to their values.
- _field_defaults = {}
- _fields = ('version', 'leader', 'sync_standby')
- classmethod _make ( iterable )
-
Make a new SyncState object from a sequence or iterable
- _replace ( ** kwds )
-
Return a new SyncState object replacing specified fields with new values
- static _str_to_list ( value : str ) List [ str ] View on GitHub
-
Splits a string by comma and returns list of strings.
- Parameters :
-
value – a comma separated string.
- Returns :
-
list of non-empty strings after splitting an input value by comma.
- static empty ( version : int | str | None = None ) SyncState View on GitHub
-
Construct an empty
SyncStateinstance.- Parameters :
-
version – optional version number.
- Returns :
-
empty synchronisation state object.
- static from_node ( version : int | str | None , value : str | Dict [ str , Any ] | None ) SyncState View on GitHub
-
Factory method to parse value as synchronisation state information.
- Parameters :
-
-
version – optional version number for the object.
-
value – (optionally JSON serialised) sychronisation state information
-
- Returns :
-
constructed
SyncStateobject. - Example :
-
>>> SyncState.from_node(1, None).leader is None True
>>> SyncState.from_node(1, '{}').leader is None True
>>> SyncState.from_node(1, '{').leader is None True
>>> SyncState.from_node(1, '[]').leader is None True
>>> SyncState.from_node(1, '{"leader": "leader"}').leader == "leader" True
>>> SyncState.from_node(1, {"leader": "leader"}).leader == "leader" True
- property is_empty : bool View on GitHub
-
Trueif/synckey is not valid (doesn’t have a leader).
- leader_matches ( name : str | None ) bool View on GitHub
-
Compare the given name to stored leader value.
- Returns :
-
Trueif name is matching theleadervalue.
- matches ( name : str | None , check_leader : bool = False ) bool View on GitHub
-
Checks if node is presented in the /sync state.
Since PostgreSQL does case-insensitive checks for synchronous_standby_name we do it also.
- Parameters :
-
-
name – name of the node.
-
check_leader – by default the name is searched for only in members, a value of
Truewill include the leader to list.
-
- Returns :
-
Trueif the/synckey notis_empty()and the given name is among those presented in the sync state. - Example :
-
>>> s = SyncState(1, 'foo', 'bar,zoo')
>>> s.matches('foo') False
>>> s.matches('fOo', True) True
>>> s.matches('Bar') True
>>> s.matches('zoO') True
>>> s.matches('baz') False
>>> s.matches(None) False
>>> SyncState.empty(1).matches('foo') False
- property members : List [ str ] View on GitHub
-
sync_standbyas list or an empty list if undefined or object consideredempty.
- class patroni.dcs. TimelineHistory ( version : int | str , value : Any , lines : List [ Tuple [ int , int , str ] | Tuple [ int , int , str , str ] | Tuple [ int , int , str , str , str ] ] ) View on GitHub
-
Bases:
NamedTupleObject representing timeline history file.
Note
The content held in lines deserialized from value are lines parsed from PostgreSQL timeline history files, consisting of the timeline number, the LSN where the timeline split and any other string held in the file. The files are parsed by
parse_history().- Variables :
-
-
version – version number of the file.
-
value – raw JSON serialised data consisting of parsed lines from history files.
-
lines –
ListofTupleparsed lines from history files.
-
- _asdict ( )
-
Return a new dict which maps field names to their values.
- _field_defaults = {}
- _fields = ('version', 'value', 'lines')
- classmethod _make ( iterable )
-
Make a new TimelineHistory object from a sequence or iterable
- _replace ( ** kwds )
-
Return a new TimelineHistory object replacing specified fields with new values
- static from_node ( version : int | str , value : str ) TimelineHistory View on GitHub
-
Parse the given JSON serialized string as a list of timeline history lines.
- Parameters :
-
-
version – version number
-
value – JSON serialized string, consisting of parsed lines of PostgreSQL timeline history files, see
TimelineHistory.
-
- Returns :
-
composed timeline history object using parsed lines.
- Example :
-
If the passed value argument is not parsed an empty list of lines is returned:
>>> h = TimelineHistory.from_node(1, 2)
>>> h.lines []
- lines : List [ Tuple [ int , int , str ] | Tuple [ int , int , str , str ] | Tuple [ int , int , str , str , str ] ]
-
Alias for field number 2
- value : Any
-
Alias for field number 1
- patroni.dcs. catch_return_false_exception ( func : Callable [ [ ... ] , Any ] ) Any View on GitHub
-
Decorator function for catching functions raising
ReturnFalseException.- Parameters :
-
func – function to be wrapped.
- Returns :
-
wrapped function.
- patroni.dcs. dcs_modules ( ) List [ str ] View on GitHub
-
Get names of DCS modules, depending on execution environment.
Note
If being packaged with PyInstaller, modules aren’t discoverable dynamically by scanning source directory because
importlib.machinery.FrozenImporterdoesn’t implementiter_modules(). But it is still possible to find all potential DCS modules by iterating throughtoc, which contains list of all "frozen" resources.- Returns :
-
list of known module names with absolute python module path namespace, e.g.
patroni.dcs.etcd.
- patroni.dcs. find_dcs_class_in_module ( module : ModuleType ) Type [ AbstractDCS ] | None View on GitHub
-
Try to find the implementation of
AbstractDCSinterface in module matching the module name.- Parameters :
-
module – Imported DCS module.
- Returns :
-
class with a name matching the name of module that implements
AbstractDCSorNoneif not found.
- patroni.dcs. get_dcs ( config : Config | Dict [ str , Any ] ) AbstractDCS View on GitHub
-
Attempt to load a Distributed Configuration Store from known available implementations.
Note
Using the list of available DCS classes returned by
iter_dcs_classes()attempt to dynamically instantiate the class that implements a DCS using the abstract classAbstractDCS.Basic top-level configuration parameters retrieved from config are propagated to the DCS specific config before being passed to the module DCS class.
If no module is found to satisfy configuration then report and log an error. This will cause Patroni to exit.
:raises
PatroniFatalException: if a load of all available DCS modules have been tried and none succeeded.- Parameters :
-
config – object or dictionary with Patroni configuration. This is normally a representation of the main Patroni
- Returns :
-
The first successfully loaded DCS module which is an implementation of
AbstractDCS.
- patroni.dcs. iter_dcs_classes ( config : Config | Dict [ str , Any ] | None = None ) Iterator [ Tuple [ str , Type [ AbstractDCS ] ] ] View on GitHub
-
Attempt to import DCS modules that are present in the given configuration.
Note
If a module successfully imports we can assume that all its requirements are installed.
- Parameters :
-
config – configuration information with possible DCS names as keys. If given, only attempt to import DCS modules defined in the configuration. Else, if
None, attempt to import any supported DCS module. - Yields :
-
a tuple containing the module
nameand the imported DCS class object.
- patroni.dcs. parse_connection_string ( value : str ) Tuple [ str , str | None ] View on GitHub
-
Split and rejoin a URL string into a connection URL and an API URL.
Note
Original Governor stores connection strings for each cluster members in a following format:
postgres://{username}:{password}@{connect_address}/postgres
Since each of our patroni instances provides their own REST API endpoint, it’s good to store this information in DCS along with PostgreSQL connection string. In order to not introduce new keys and be compatible with original Governor we decided to extend original connection string in a following way:
postgres://{username}:{password}@{connect_address}/postgres?application_name={api_url}
This way original Governor could use such connection string as it is, because of feature of
libpqlibrary.- Parameters :
-
value – The URL string to split.
- Returns :
-
the connection string stored in DCS split into two parts,
conn_urlandapi_url.
- patroni.dcs. slot_name_from_member_name ( member_name : str ) str View on GitHub
-
Translate member name to valid PostgreSQL slot name.
Note
PostgreSQL’s replication slot names must be valid PostgreSQL names. This function maps the wider space of member names to valid PostgreSQL names. Names have their case lowered, dashes and periods common in hostnames are replaced with underscores, other characters are encoded as their unicode codepoint. Name is truncated to 64 characters. Multiple different member names may map to a single slot name.
- Parameters :
-
member_name – The string to convert to a slot name.
- Returns :
-
The string converted using the rules described above.