Class Leader


  • public class Leader
    extends LearnerMaster
    This class has the control logic for the Leader.
    • Field Detail

      • quorumFormed

        protected boolean quorumFormed
      • LEADERINFO

        public static final int LEADERINFO
        This message is the first that a follower receives from the leader. It has the protocol version and the epoch of the leader.
        See Also:
        Constant Field Values
      • ACKEPOCH

        public static final int ACKEPOCH
        This message is used by the follow to ack a proposed epoch.
        See Also:
        Constant Field Values
      • PROPOSAL

        public static final int PROPOSAL
        This message type is sent by a leader to propose a mutation.
        See Also:
        Constant Field Values
      • connectingFollowers

        protected final Set<Long> connectingFollowers
      • electingFollowers

        protected final Set<Long> electingFollowers
      • electionFinished

        protected boolean electionFinished
    • Method Detail

      • setAckLoggingFrequency

        public static void setAckLoggingFrequency​(int frequency)
      • getAckLoggingFrequency

        public static int getAckLoggingFrequency()
      • getProposalStats

        public BufferStats getProposalStats()
      • getLearners

        public List<LearnerHandler> getLearners()
        Returns a copy of the current learner snapshot
      • getForwardingFollowers

        public List<LearnerHandler> getForwardingFollowers()
        Returns a copy of the current forwarding follower snapshot
      • getObservingLearners

        public List<LearnerHandler> getObservingLearners()
        Returns a copy of the current observer snapshot
      • resetObserverConnectionStats

        public void resetObserverConnectionStats()
      • getNumPendingSyncs

        public int getNumPendingSyncs()
      • addLearnerHandler

        public void addLearnerHandler​(LearnerHandler learner)
        Adds peer to the leader.
        Parameters:
        learner - instance of learner handle
      • removeLearnerHandler

        public void removeLearnerHandler​(LearnerHandler peer)
        Remove the learner from the learner list
        Parameters:
        peer -
      • isQuorumSynced

        public boolean isQuorumSynced​(QuorumVerifier qv)
        Returns true if a quorum in qv is connected and synced with the leader and false otherwise
        Parameters:
        qv - is a QuorumVerifier
      • getUptime

        public long getUptime()
      • tryToCommit

        public boolean tryToCommit​(Leader.Proposal p,
                                   long zxid,
                                   SocketAddress followerAddr)
        Returns:
        True if committed, otherwise false.
      • processAck

        public void processAck​(long sid,
                               long zxid,
                               SocketAddress followerAddr)
        Keep a count of acks that are received by the leader for a particular proposal
        Parameters:
        sid - is the id of the server that sent the ack
        zxid - is the zxid of the proposal sent out
        followerAddr -
      • commit

        public void commit​(long zxid)
        Create a commit packet and send it to all the members of the quorum
        Parameters:
        zxid -
      • commitAndActivate

        public void commitAndActivate​(long zxid,
                                      long designatedLeader)
      • inform

        public void inform​(Leader.Proposal proposal)
        Create an inform packet and send it to all observers.
      • buildInformAndActivePacket

        public static QuorumPacket buildInformAndActivePacket​(long zxid,
                                                              long designatedLeader,
                                                              byte[] proposalData)
      • informAndActivate

        public void informAndActivate​(Leader.Proposal proposal,
                                      long designatedLeader)
        Create an inform and activate packet and send it to all observers.
      • getLastProposed

        public long getLastProposed()
      • getEpoch

        public long getEpoch()
        Returns the current epoch of the leader.
      • processSync

        public void processSync​(LearnerSyncRequest r)
        Process sync requests
        Parameters:
        r - the request
      • sendSync

        public void sendSync​(LearnerSyncRequest r)
        Sends a sync message to the appropriate server
      • startForwarding

        public long startForwarding​(LearnerHandler handler,
                                    long lastSeenZxid)
        lets the leader know that a follower is capable of following and is done syncing
        Parameters:
        handler - handler of the follower
        Returns:
        last proposed zxid
      • setMaxTimeToWaitForEpoch

        public static void setMaxTimeToWaitForEpoch​(int maxTimeToWaitForEpoch)
      • reportLookingSid

        public void reportLookingSid​(long sid)
      • getZKDatabase

        public ZKDatabase getZKDatabase()
      • waitForNewLeaderAck

        public void waitForNewLeaderAck​(long sid,
                                        long zxid)
                                 throws InterruptedException
        Process NEWLEADER ack of a given sid and wait until the leader receives sufficient acks.
        Parameters:
        sid -
        Throws:
        InterruptedException
      • getPacketType

        public static String getPacketType​(int packetType)
        Get string representation of a given packet type
        Parameters:
        packetType -
        Returns:
        string representing the packet type
      • getCurrentTick

        public int getCurrentTick()
      • syncTimeout

        public int syncTimeout()
      • getTickOfNextAckDeadline

        public int getTickOfNextAckDeadline()
      • getTickOfInitialAckDeadline

        public int getTickOfInitialAckDeadline()
      • getAndDecrementFollowerCounter

        public long getAndDecrementFollowerCounter()
      • touch

        public void touch​(long sess,
                          int to)
      • submitLearnerRequest

        public void submitLearnerRequest​(Request si)
      • getQuorumVerifierVersion

        public long getQuorumVerifierVersion()
      • getPeerInfo

        public String getPeerInfo​(long sid)
      • getQuorumVerifierBytes

        public byte[] getQuorumVerifierBytes()
      • registerLearnerHandlerBean

        public void registerLearnerHandlerBean​(LearnerHandler learnerHandler,
                                               Socket socket)
      • unregisterLearnerHandlerBean

        public void unregisterLearnerHandlerBean​(LearnerHandler learnerHandler)