patroni.dcs.raft module
- class patroni.dcs.raft.DynMemberSyncObj(selfAddress: Optional[str], partnerAddrs: Collection[str], conf: pysyncobj.config.SyncObjConf, retry_timeout: int = 10)
Bases:
pysyncobj.syncobj.SyncObj- __init__(selfAddress: Optional[str], partnerAddrs: Collection[str], conf: pysyncobj.config.SyncObjConf, retry_timeout: int = 10) None
Main SyncObj class, you should inherit your own class from it.
- Parameters
selfNode (Node or str) – object representing the self-node or address of the current node server ‘host:port’
otherNodes (iterable of Node or iterable of str) – objects representing the other nodes or addresses of partner nodes [‘host1:port1’, ‘host2:port2’, …]
conf (SyncObjConf) – configuration object
consumers (list of SyncObjConsumer inherited objects) – objects to be replicated
nodeClass (class) – class used for representation of nodes
transport (Transport or None) – transport object; if None, transportClass is used to initialise such an object
transportClass (class) – the Transport subclass to be used for transferring messages to and from other nodes
- class patroni.dcs.raft.KVStoreTTL(on_ready: Optional[Callable[[...], Any]], on_set: Optional[Callable[[str, Dict[str, Any]], None]], on_delete: Optional[Callable[[str], None]], **config: Any)
Bases:
patroni.dcs.raft.DynMemberSyncObj- __init__(on_ready: Optional[Callable[[...], Any]], on_set: Optional[Callable[[str, Dict[str, Any]], None]], on_delete: Optional[Callable[[str], None]], **config: Any) None
Main SyncObj class, you should inherit your own class from it.
- Parameters
selfNode (Node or str) – object representing the self-node or address of the current node server ‘host:port’
otherNodes (iterable of Node or iterable of str) – objects representing the other nodes or addresses of partner nodes [‘host1:port1’, ‘host2:port2’, …]
conf (SyncObjConf) – configuration object
consumers (list of SyncObjConsumer inherited objects) – objects to be replicated
nodeClass (class) – class used for representation of nodes
transport (Transport or None) – transport object; if None, transportClass is used to initialise such an object
transportClass (class) – the Transport subclass to be used for transferring messages to and from other nodes
- retry(func: Callable[[...], Any], *args: Any, **kwargs: Any) Any
- class patroni.dcs.raft.Raft(config: Dict[str, Any], mpp: patroni.postgresql.mpp.AbstractMPP)
Bases:
patroni.dcs.AbstractDCS- __init__(config: Dict[str, Any], mpp: patroni.postgresql.mpp.AbstractMPP) None
Prepare DCS paths, MPP object, initial values for state information and processing dependencies.
- Parameters
config –
dict, reference to config section of selected DCS. i.e.:zookeeperfor zookeeper,etcdfor etcd, etc…mpp – an object implementing
AbstractMPPinterface.
- _abc_impl = <_abc._abc_data object>
- _cluster_from_nodes(nodes: Dict[str, Any]) patroni.dcs.Cluster
- _delete_leader(leader: patroni.dcs.Leader) bool
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.
- _load_cluster(path: str, loader: Callable[[str], Union[patroni.dcs.Cluster, Dict[int, patroni.dcs.Cluster]]]) Union[patroni.dcs.Cluster, Dict[int, patroni.dcs.Cluster]]
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
_postgresql_cluster_loader()or_mpp_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.
- _mpp_cluster_loader(path: str) Dict[int, patroni.dcs.Cluster]
Load and build all PostgreSQL clusters from a single MPP cluster.
- Parameters
path – the path in DCS where to load Cluster(s) from.
- Returns
all MPP groups as
dict, with group IDs as keys andClusterobjects as values.
- _postgresql_cluster_loader(path: str) patroni.dcs.Cluster
Load and build the
Clusterobject from DCS, which represents a single PostgreSQL cluster.- Parameters
path – the path in DCS where to load
Clusterfrom.- Returns
Clusterinstance.
- _update_leader(leader: patroni.dcs.Leader) bool
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.
- _write_failsafe(value: str) bool
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.
- _write_leader_optime(last_lsn: str) bool
Write current WAL LSN into
/optime/leaderkey in DCS.- Parameters
last_lsn – absolute WAL LSN in bytes.
- Returns
Trueif successfully committed to DCS.
- _write_status(value: str) bool
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.
- attempt_to_acquire_leader() bool
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.
- cancel_initialization() bool
Removes the
initializekey for a cluster.- Returns
Trueif 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
Trueif delete successful.
- initialize(create_new: bool = True, sysid: str = '') bool
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.
- static member(key: str, value: Dict[str, Any]) patroni.dcs.Member
- reload_config(config: Union[Config, Dict[str, Any]]) None
Load and set relevant values from configuration.
Sets
loop_wait,ttlandretry_timeoutproperties.- Parameters
config – Loaded configuration information object or dictionary of key value pairs.
- set_config_value(value: str, version: Optional[int] = None) bool
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.
- set_failover_value(value: str, version: Optional[int] = None) bool
Create or update
/failoverkey.- Parameters
value – value to set.
version – for conditional update of the key/object.
- Returns
Trueif successfully committed to DCS.
- set_history_value(value: str) bool
Set value for
historyin DCS.- Parameters
value – new value of
historykey/object.- Returns
Trueif successfully committed to DCS.
- set_sync_state_value(value: str, version: Optional[int] = None) Union[int, bool]
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.
- take_leader() bool
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.
- 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/+_nameand the value of data in a given DCS.- Parameters
data – information about an instance (including connection strings).
- Returns
Trueif successfully committed to DCS.
- 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
Truethis will reschedule the next run of the HA cycle.
- exception patroni.dcs.raft.RaftError(value: Any)
Bases:
patroni.exceptions.DCSError
- class patroni.dcs.raft.SyncObjUtility(otherNodes: Collection[Union[str, pysyncobj.node.TCPNode]], conf: pysyncobj.config.SyncObjConf, retry_timeout: int = 10)
Bases:
object- __init__(otherNodes: Collection[Union[str, pysyncobj.node.TCPNode]], conf: pysyncobj.config.SyncObjConf, retry_timeout: int = 10) None
- executeCommand(command: List[Any]) Any
- class patroni.dcs.raft._TCPTransport(syncObj: patroni.dcs.raft.DynMemberSyncObj, selfNode: Optional[pysyncobj.node.TCPNode], otherNodes: Collection[pysyncobj.node.TCPNode])
Bases:
pysyncobj.transport.TCPTransport- __init__(syncObj: patroni.dcs.raft.DynMemberSyncObj, selfNode: Optional[pysyncobj.node.TCPNode], otherNodes: Collection[pysyncobj.node.TCPNode]) None
Initialise the TCP transport. On normal (non-read-only) nodes, this will start a TCP server. On all nodes, it will initiate relevant connections to other nodes.
- Parameters
syncObj (SyncObj) – SyncObj
selfNode (TCPNode or None) – current node (None if this is a read-only node)
otherNodes (iterable of TCPNode) – partner nodes