(Viii) zookeeper's SyncedLearnerTracker Leader Elections and voting Affairs and Proposal

  • Total votes is: SyncedLearnerTracker
    main role of this class is to provide some statistical functions for the current round of elections or voting matters, as follows:
    1. Add vote validator and initialization ackset, ackset is a zookeeper service sends ack response list
    2. Add ack in response to the appropriate list
    3. verify that the specified serverId current cache of voting validator vote roles
    4. verify that the current round of elections by voting or transaction
  • Proposal: Proposal
    static public class Proposal  extends SyncedLearnerTracker {
        public QuorumPacket packet;
        public Request request;

        @Override
        public String toString() {
            return packet.getType() + ", " + packet.getZxid() + ", " + request;
        }
    }

This class inherits from SyncedLearnerTracker, but more than two member variables, the first one is to interact with the data packets between Leader Follower, is the second transaction request body.

  • Class of internal SyncedLearnerTracker: QuorumVerifierAcksetPair
    public static class QuorumVerifierAcksetPair {
        private final QuorumVerifier qv;
        private final HashSet<Long> ackset;

        public QuorumVerifierAcksetPair(QuorumVerifier qv, HashSet<Long> ackset) {
            this.qv = qv;
            this.ackset = ackset;
        }

        public QuorumVerifier getQuorumVerifier() {
            return this.qv;
        }

        public HashSet<Long> getAckset() {
            return this.ackset;
        }
    }

This is a validator QuorumVerifier vote pairing and answering a list of ackset

Core methods:
    public void addQuorumVerifier(QuorumVerifier qv) {
        qvAcksetPairs.add(new QuorumVerifierAcksetPair(qv,
                new HashSet<Long>(qv.getVotingMembers().size())));
    }

    public boolean addAck(Long sid) {
        boolean change = false;
        for (QuorumVerifierAcksetPair qvAckset : qvAcksetPairs) {
            if (qvAckset.getQuorumVerifier().getVotingMembers().containsKey(sid)) {
                qvAckset.getAckset().add(sid);
                change = true;
            }
        }
        return change;
    }

    public boolean hasSid(long sid) {
        for (QuorumVerifierAcksetPair qvAckset : qvAcksetPairs) {
            if (!qvAckset.getQuorumVerifier().getVotingMembers().containsKey(sid)) {
                return false;
            }
        }
        return true;
    }

    public boolean hasAllQuorums() {
        for (QuorumVerifierAcksetPair qvAckset : qvAcksetPairs) {
            if (!qvAckset.getQuorumVerifier().containsQuorum(qvAckset.getAckset()))
                return false;
        }
        return true;
    }
  • addQuorumVerifier (QuorumVerifier qv)
    add to vote validator QuorumVerifier and initialization ackset, when the size of the initialization ackset is to vote in accordance with the size of the validator QuorumVerifier in the list of vote
  • addAck (Long sid)
    added ack response to a corresponding list
  • hasSid (long sid)
    whether there is a vote in the role of designated serverId verify the current cache vote validator
  • hasAllQuorums ()
    checks whether the current round of elections by voting or affairs, to note here is that as long as SyncedLearnerTracker cache validator QuorumVerifier vote, there is a no vote, you would think that this round of voting failed

Guess you like

Origin blog.csdn.net/long9870/article/details/93737545