Class FastLeaderElection

  • All Implemented Interfaces:
    Election

    public class FastLeaderElection
    extends java.lang.Object
    implements Election
    Implementation of leader election using TCP. It uses an object of the class QuorumCnxManager to manage connections. Otherwise, the algorithm is push-based as with the other UDP implementations. There are a few parameters that can be tuned to change its behavior. First, finalizeWait determines the amount of time to wait until deciding upon a leader. This is part of the leader election algorithm.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected class  FastLeaderElection.Messenger
      Multi-threaded implementation of message handler.
      static class  FastLeaderElection.Notification
      Notifications are messages that let other peers know that a given peer has changed its vote, either because it has joined leader election or because it learned of another peer with higher zxid or same zxid and higher server id
      static class  FastLeaderElection.ToSend
      Messages that a peer wants to send to other peers.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String MAX_NOTIFICATION_INTERVAL
      Maximum notification interval, default is 60s
      static java.lang.String MIN_NOTIFICATION_INTERVAL
      Minimum notification interval, default is equal to finalizeWait
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected boolean checkLeader​(java.util.Map<java.lang.Long,​Vote> votes, long leader, long electionEpoch)
      In the case there is a leader elected, and a quorum supporting this leader, we have to check if the leader has voted and acked that it is leading.
      QuorumCnxManager getCnxManager()  
      long getLogicalClock()
      Returns the current value of the logical clock counter
      Vote getVote()  
      protected SyncedLearnerTracker getVoteTracker​(java.util.Map<java.lang.Long,​Vote> votes, Vote vote)
      Given a set of votes, return the SyncedLearnerTracker which is used to determines if have sufficient to declare the end of the election round.
      Vote lookForLeader()
      Starts a new round of leader election.
      void shutdown()  
      void start()
      This method starts the sender and receiver threads.
      protected boolean totalOrderPredicate​(long newId, long newZxid, long newEpoch, long curId, long curZxid, long curEpoch)
      Check if a pair (server id, zxid) succeeds our current vote.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • MIN_NOTIFICATION_INTERVAL

        public static final java.lang.String MIN_NOTIFICATION_INTERVAL
        Minimum notification interval, default is equal to finalizeWait
        See Also:
        Constant Field Values
      • MAX_NOTIFICATION_INTERVAL

        public static final java.lang.String MAX_NOTIFICATION_INTERVAL
        Maximum notification interval, default is 60s
        See Also:
        Constant Field Values
    • Constructor Detail

      • FastLeaderElection

        public FastLeaderElection​(QuorumPeer self,
                                  QuorumCnxManager manager)
        Constructor of FastLeaderElection. It takes two parameters, one is the QuorumPeer object that instantiated this object, and the other is the connection manager. Such an object should be created only once by each peer during an instance of the ZooKeeper service.
        Parameters:
        self - QuorumPeer that created this object
        manager - Connection manager
    • Method Detail

      • getLogicalClock

        public long getLogicalClock()
        Returns the current value of the logical clock counter
      • start

        public void start()
        This method starts the sender and receiver threads.
      • shutdown

        public void shutdown()
        Specified by:
        shutdown in interface Election
      • totalOrderPredicate

        protected boolean totalOrderPredicate​(long newId,
                                              long newZxid,
                                              long newEpoch,
                                              long curId,
                                              long curZxid,
                                              long curEpoch)
        Check if a pair (server id, zxid) succeeds our current vote.
      • getVoteTracker

        protected SyncedLearnerTracker getVoteTracker​(java.util.Map<java.lang.Long,​Vote> votes,
                                                      Vote vote)
        Given a set of votes, return the SyncedLearnerTracker which is used to determines if have sufficient to declare the end of the election round.
        Parameters:
        votes - Set of votes
        vote - Identifier of the vote received last
        Returns:
        the SyncedLearnerTracker with vote details
      • checkLeader

        protected boolean checkLeader​(java.util.Map<java.lang.Long,​Vote> votes,
                                      long leader,
                                      long electionEpoch)
        In the case there is a leader elected, and a quorum supporting this leader, we have to check if the leader has voted and acked that it is leading. We need this check to avoid that peers keep electing over and over a peer that has crashed and it is no longer leading.
        Parameters:
        votes - set of votes
        leader - leader id
        electionEpoch - epoch id
      • getVote

        public Vote getVote()
      • lookForLeader

        public Vote lookForLeader()
                           throws java.lang.InterruptedException
        Starts a new round of leader election. Whenever our QuorumPeer changes its state to LOOKING, this method is invoked, and it sends notifications to all other peers.
        Specified by:
        lookForLeader in interface Election
        Throws:
        java.lang.InterruptedException