From b83f1c0f440eb748f103f3e282102a85ef90e49c Mon Sep 17 00:00:00 2001 From: Mark Pekala Date: Fri, 11 Aug 2023 01:30:20 -0700 Subject: [PATCH] [Refactor] Rename _is_leader to _leader_expiry (#2807) --- patroni/ha.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/patroni/ha.py b/patroni/ha.py index 0aa9b9d0..e4b68fcf 100644 --- a/patroni/ha.py +++ b/patroni/ha.py @@ -147,8 +147,8 @@ class Ha(object): self.cluster = Cluster.empty() self.global_config = self.patroni.config.get_global_config(None) self.old_cluster = Cluster.empty() - self._is_leader = False - self._is_leader_lock = RLock() + self._leader_expiry = 0 + self._leader_expiry_lock = RLock() self._failsafe = Failsafe(patroni.dcs) self._was_paused = False self._leader_timeline = None @@ -193,12 +193,20 @@ class Ha(object): return self.global_config.is_standby_cluster def is_leader(self) -> bool: - with self._is_leader_lock: - return self._is_leader > time.time() + """:returns: `True` if the current node is the leader, based on expiration set when it last held the key.""" + with self._leader_expiry_lock: + return self._leader_expiry > time.time() def set_is_leader(self, value: bool) -> None: - with self._is_leader_lock: - self._is_leader = time.time() + self.dcs.ttl if value else 0 + """Update the current node's view of it's own leadership status. + + Will update the expiry timestamp to match the dcs ttl if setting leadership to true, + otherwise will set the expiry to the past to immediately invalidate. + + :param value: is the current node the leader. + """ + with self._leader_expiry_lock: + self._leader_expiry = time.time() + self.dcs.ttl if value else 0 def sync_mode_is_active(self) -> bool: """Check whether synchronous replication is requested and already active.