def _is_healthiest_node(self, members: Collection[Member],
                            check_replication_lag: bool = True,
                            leader: Optional[Leader] = None) -> bool:
        """Determine whether the current node is healthy enough to become a new leader candidate.

        :param members: the list of nodes to check against
        :param check_replication_lag: whether to take the replication lag into account.
                                      If the lag exceeds configured threshold the node disqualifies itself.
        :param leader: the old cluster leader, it will be used to ignore its ``failover_priority`` value.
        :returns: ``True`` if the node is eligible to become the new leader. Since this method is executed
                  on multiple nodes independently it is possible that multiple nodes could count
                  themselves as the healthiest because they received/replayed up to the same LSN,
                  but this is totally fine.
        """
        my_wal_position = self.state_handler.last_operation()
        if check_replication_lag and self.is_lagging(my_wal_position):
            logger.info('My wal position exceeds maximum replication lag')
            return False  # Too far behind last reported wal position on primary

        if not self.is_standby_cluster() and self.check_timeline():
            cluster_timeline = self.cluster.timeline
            my_timeline = self.state_handler.replica_cached_timeline(cluster_timeline)
            if my_timeline is None:
                logger.info('Can not figure out my timeline')
                return False
            if my_timeline < cluster_timeline:
                logger.info('My timeline %s is behind last known cluster timeline %s', my_timeline, cluster_timeline)
                return False

        if self.quorum_commit_mode_is_active():
            quorum = self.cluster.sync.quorum
            voting_set = CaseInsensitiveSet(self.cluster.sync.members)
        else:
            quorum = 0
            voting_set = CaseInsensitiveSet()

        # Prepare list of nodes to run check against. If quorum commit is enabled
        # we also include members with nofailover tag if they are listed in voters.
        members = [m for m in members if m.name != self.state_handler.name
                   and m.api_url and (not m.nofailover or m.name in voting_set)]

        # If there is a quorum active then at least one of the quorum contains latest commit. A quorum member saying
        # their WAL position is not ahead counts as a vote saying we may become new leader. Note that a node doesn't
        # have to be a member of the voting set to gather the necessary votes.

        # Regardless of voting, if we observe a node that can become a leader and is ahead, we defer to that node.
        # This can lead to failure to act on quorum if there is asymmetric connectivity.
        quorum_votes = 0 if self.state_handler.name in voting_set else -1
        nodes_ahead = 0

        # we need to know the name of the former leader to ignore it if it has higher failover_priority
        if self.sync_mode_is_active():
            leader_name = self.cluster.sync.leader
        else:
            leader_name = leader and leader.name

        for st in self.fetch_nodes_statuses(members):
            if st.failover_limitation() is None:
                if st.in_recovery is False:
                    logger.warning('Primary (%s) is still alive', st.member.name)
                    return False
                if my_wal_position < st.wal_position:
                    nodes_ahead += 1
                    logger.info('Wal position of %s is ahead of my wal position', st.member.name)
                    # In synchronous mode the former leader might be still accessible and even be ahead of us.
                    # We should not disqualify himself from the leader race in such a situation.
                    if not self.sync_mode_is_active() or not self.cluster.sync.leader_matches(st.member.name):
                        return False
                    logger.info('Ignoring the former leader being ahead of us')
                elif st.wal_position > 0:  # we want to count votes only from nodes with postgres up and running!
                    quorum_vote = st.member.name in voting_set
                    low_priority = my_wal_position == st.wal_position \
                        and self.patroni.failover_priority < st.failover_priority

                    if low_priority and leader_name and leader_name == st.member.name:
                        logger.info('Ignoring former leader %s having priority %s higher than this nodes %s priority',
                                    leader_name, st.failover_priority, self.patroni.failover_priority)
                        low_priority = False

                    if low_priority and (not self.sync_mode_is_active() or quorum_vote):
                        # There's a higher priority non-lagging replica
                        logger.info(
                            '%s has equally tolerable WAL position and priority %s, while this node has priority %s',
                            st.member.name, st.failover_priority, self.patroni.failover_priority)
                        return False

                    if quorum_vote:
                        logger.info('Got quorum vote from %s', st.member.name)
                        quorum_votes += 1

        # When not in quorum commit we just want to return `True`.
        # In quorum commit the former leader is special and counted healthy even when there are no other nodes.
        # Otherwise check that the number of votes exceeds the quorum field from the /sync key.
        return not self.quorum_commit_mode_is_active() or quorum_votes >= quorum\
            or nodes_ahead == 0 and self.cluster.sync.leader == self.state_handler.name
Determine whether the current node is healthy enough to become a new leader candidate.

        :param members: the list of nodes to check against
        :param check_replication_lag: whether to take the replication lag into account.
                                      If the lag exceeds configured threshold the node disqualifies itself.
        :param leader: the old cluster leader, it will be used to ignore its ``failover_priority`` value.
        :returns: ``True`` if the node is eligible to become the new leader. Since this method is executed
                  on multiple nodes independently it is possible that multiple nodes could count
                  themselves as the healthiest because they received/replayed up to the same LSN,
                  but this is totally fine.
        
patroni/ha.py