patroni.dcs.etcd module¶
-
class
patroni.dcs.etcd.
AbstractEtcd
(config: Dict[str, Any], mpp: patroni.postgresql.mpp.AbstractMPP, client_cls: Type[patroni.dcs.etcd.AbstractEtcdClientWithFailover], retry_errors_cls: Union[Type[Exception], Tuple[Type[Exception], ...]])¶ Bases:
patroni.dcs.AbstractDCS
-
__init__
(config: Dict[str, Any], mpp: patroni.postgresql.mpp.AbstractMPP, client_cls: Type[patroni.dcs.etcd.AbstractEtcdClientWithFailover], retry_errors_cls: Union[Type[Exception], Tuple[Type[Exception], ...]]) → None¶ Prepare DCS paths, MPP object, 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_data object>¶
-
_client
¶ return correct type of etcd client
-
_handle_exception
(e: Exception, name: str = '', do_sleep: bool = False, raise_ex: Optional[Exception] = None) → None¶
-
_run_and_handle_exceptions
(method: Callable[[...], Any], *args, **kwargs) → Any¶
-
get_etcd_client
(config: Dict[str, Any], client_cls: Type[patroni.dcs.etcd.AbstractEtcdClientWithFailover]) → patroni.dcs.etcd.AbstractEtcdClientWithFailover¶
-
handle_etcd_exceptions
(func: Callable[[...], Any], *args, **kwargs) → Any¶
-
reload_config
(config: Union[Config, Dict[str, Any]]) → None¶ Load and set relevant values from configuration.
Sets
loop_wait
,ttl
andretry_timeout
properties.Parameters: config – Loaded configuration information object or dictionary of key value pairs.
-
retry
(method: Callable[[...], Any], *args, **kwargs) → Any¶
-
set_retry_timeout
(retry_timeout: int) → None¶ Set the new value for retry_timeout.
-
set_socket_options
(sock: socket.socket, socket_options: Optional[Collection[Tuple[int, int, int]]]) → None¶
-
set_ttl
(ttl: int) → Optional[bool]¶ Set the new ttl value for DCS keys.
-
ttl
¶ Get current
ttl
value.
-
-
class
patroni.dcs.etcd.
AbstractEtcdClientWithFailover
(config: Dict[str, Any], dns_resolver: patroni.dcs.etcd.DnsCachingResolver, cache_ttl: int = 300)¶ Bases:
abc.ABC
,etcd.client.Client
-
__init__
(config: Dict[str, Any], dns_resolver: patroni.dcs.etcd.DnsCachingResolver, cache_ttl: int = 300) → None¶ Initialize self. See help(type(self)) for accurate signature.
-
_abc_impl
= <_abc_data object>¶
-
_calculate_timeouts
(etcd_nodes: int, timeout: Optional[float] = None) → Tuple[int, float, int]¶ Calculate a request timeout and number of retries per single etcd node. In case if the timeout per node is too small (less than one second) we will reduce the number of nodes. For the cluster with only one node we will try to do 2 retries. For clusters with 2 nodes we will try to do 1 retry for every node. No retries for clusters with 3 or more nodes. We better rely on switching to a different node.
-
_do_http_request
(retry: Optional[patroni.utils.Retry], machines_cache: List[str], request_executor: Callable[[...], urllib3.response.HTTPResponse], method: str, path: str, fields: Optional[Dict[str, Any]] = None, **kwargs) → urllib3.response.HTTPResponse¶
-
_get_headers
() → Dict[str, str]¶
-
_get_machines_cache_from_config
() → List[str]¶
-
_get_machines_cache_from_dns
(host: str, port: int) → List[str]¶ One host might be resolved into multiple ip addresses. We will make list out of it
-
_get_machines_cache_from_srv
(srv: str, srv_suffix: Optional[str] = None) → List[str]¶ Fetch list of etcd-cluster member by resolving _etcd-server._tcp. SRV record. This record should contain list of host and peer ports which could be used to run ‘GET http://{host}:{port}/members’ request (peer protocol)
-
_get_machines_list
(machines_cache: List[str]) → List[str]¶ Gets list of members from Etcd cluster using API
Parameters: machines_cache – initial list of Etcd members Returns: list of clientURLs retrieved from Etcd cluster Raises: EtcdConnectionFailed – if failed
-
_get_members
(base_uri: str, **kwargs) → List[str]¶ returns: list of clientURLs
-
_load_machines_cache
() → bool¶ This method should fill up _machines_cache from scratch. It could happen only in two cases: 1. During class initialization 2. When all etcd members failed
-
_prepare_common_parameters
(etcd_nodes: int, timeout: Optional[float] = None) → Dict[str, Any]¶
-
_prepare_get_members
(etcd_nodes: int) → Dict[str, Any]¶ returns: request parameters
-
_prepare_request
(kwargs: Dict[str, Any], params: Optional[Dict[str, Any]] = None, method: Optional[str] = None) → Callable[[...], urllib3.response.HTTPResponse]¶ returns: request_executor
-
_refresh_machines_cache
(machines_cache: Optional[List[str]] = None) → bool¶ Get etcd cluster topology using Etcd API and put it to self._machines_cache
Parameters: machines_cache – the list of nodes we want to run through executing API request in addition to values stored in the self._machines_cache Returns: True if self._machines_cache was updated with new values Raises: EtcdException – if failed to get topology and machines_cache was specified. The self._machines_cache will not be updated if nodes from the list are not accessible or if they are not returning correct results.
-
static
_update_dns_cache
(func: Callable[[str, int], None], machines: List[str]) → None¶
-
api_execute
(path: str, method: str, params: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None) → Any¶ Executes the query.
-
static
get_srv_record
(host: str) → List[Tuple[str, int]]¶
-
machines
¶ Original machines method(property) of etcd.Client class raise exception when it failed to get list of etcd cluster members. This method is being called only when request failed on one of the etcd members during api_execute call. For us it’s more important to execute original request rather then get new topology of etcd cluster. So we will catch this exception and return empty list of machines. Later, during next api_execute call we will forcefully update machines_cache.
Also this method implements the same timeout-retry logic as api_execute, because the original method was retrying 2 times with the read_timeout on each node.
After the next refactoring the whole logic was moved to the _get_machines_list() method.
-
machines_cache
¶
-
reload_config
(config: Dict[str, Any]) → None¶
-
set_base_uri
(value: str) → None¶
-
set_machines_cache_ttl
(cache_ttl: int) → None¶
-
set_read_timeout
(timeout: float) → None¶
-
-
class
patroni.dcs.etcd.
DnsCachingResolver
(cache_time: float = 600.0, cache_fail_time: float = 30.0)¶ Bases:
threading.Thread
-
__init__
(cache_time: float = 600.0, cache_fail_time: float = 30.0) → None¶ This constructor should always be called with keyword arguments. Arguments are:
group should be None; reserved for future extension when a ThreadGroup class is implemented.
target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.
name is the thread name. By default, a unique name is constructed of the form “Thread-N” where N is a small decimal number.
args is the argument tuple for the target invocation. Defaults to ().
kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.
If a subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing anything else to the thread.
-
static
_do_resolve
(host: str, port: int) → List[Tuple[socket.AddressFamily, socket.SocketKind, int, str, Union[Tuple[str, int], Tuple[str, int, int, int]]]]¶
-
remove
(host: str, port: int) → None¶
-
resolve
(host: str, port: int) → List[Tuple[socket.AddressFamily, socket.SocketKind, int, str, Union[Tuple[str, int], Tuple[str, int, int, int]]]]¶
-
resolve_async
(host: str, port: int, attempt: int = 0) → None¶
-
run
() → None¶ Method representing the thread’s activity.
You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.
-
-
class
patroni.dcs.etcd.
Etcd
(config: Dict[str, Any], mpp: patroni.postgresql.mpp.AbstractMPP)¶ Bases:
patroni.dcs.etcd.AbstractEtcd
-
__init__
(config: Dict[str, Any], mpp: patroni.postgresql.mpp.AbstractMPP) → None¶ Prepare DCS paths, MPP object, 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_data object>¶
-
_client
¶ return correct type of etcd client
-
_cluster_from_nodes
(etcd_index: int, nodes: Dict[str, etcd.EtcdResult]) → patroni.dcs.Cluster¶
-
_delete_leader
(*args, **kwargs) → Any¶
-
_do_attempt_to_acquire_leader
() → bool¶
-
_do_update_leader
() → bool¶
-
_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
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 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: DCSError
in 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 andCluster
objects as values.
-
_postgresql_cluster_loader
(path: str) → patroni.dcs.Cluster¶ Load and build the
Cluster
object from DCS, which represents a single PostgreSQL cluster.Parameters: path – the path in DCS where to load Cluster
from.Returns: Cluster
instance.
-
_update_leader
(**kwargs)¶
-
_write_failsafe
(*args, **kwargs) → Any¶
-
_write_leader_optime
(*args, **kwargs) → Any¶
-
_write_status
(*args, **kwargs) → Any¶
-
attempt_to_acquire_leader
(**kwargs)¶
-
cancel_initialization
(*args, **kwargs) → Any¶
-
delete_cluster
(*args, **kwargs) → Any¶
-
delete_sync_state
(*args, **kwargs) → Any¶
-
initialize
(*args, **kwargs) → Any¶
-
static
member
(node: etcd.EtcdResult) → patroni.dcs.Member¶
-
set_config_value
(*args, **kwargs) → Any¶
-
set_failover_value
(*args, **kwargs) → Any¶
-
set_history_value
(*args, **kwargs) → Any¶
-
set_sync_state_value
(*args, **kwargs) → Any¶
-
set_ttl
(ttl: int) → Optional[bool]¶ Set the new ttl value for DCS keys.
-
take_leader
(*args, **kwargs) → Any¶
-
touch_member
(*args, **kwargs) → Any¶
-
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.
-
-
class
patroni.dcs.etcd.
EtcdClient
(config: Dict[str, Any], dns_resolver: patroni.dcs.etcd.DnsCachingResolver, cache_ttl: int = 300)¶ Bases:
patroni.dcs.etcd.AbstractEtcdClientWithFailover
-
__init__
(config: Dict[str, Any], dns_resolver: patroni.dcs.etcd.DnsCachingResolver, cache_ttl: int = 300) → None¶ Initialize self. See help(type(self)) for accurate signature.
-
_abc_impl
= <_abc_data object>¶
-
_get_members
(base_uri: str, **kwargs) → List[str]¶ returns: list of clientURLs
-
_prepare_get_members
(etcd_nodes: int) → Dict[str, Any]¶ returns: request parameters
-
_prepare_request
(kwargs: Dict[str, Any], params: Optional[Dict[str, Any]] = None, method: Optional[str] = None) → Callable[[...], urllib3.response.HTTPResponse]¶ returns: request_executor
-
-
exception
patroni.dcs.etcd.
EtcdError
(value: Any)¶ Bases:
patroni.exceptions.DCSError
-
exception
patroni.dcs.etcd.
EtcdRaftInternal
(message=None, payload=None)¶ Bases:
etcd.EtcdException
Raft Internal Error
-
patroni.dcs.etcd.
catch_etcd_errors
(func: Callable[[...], Any]) → Any¶