Class FastLeaderElection
- java.lang.Object
-
- org.apache.zookeeper.server.quorum.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 idstatic class
FastLeaderElection.ToSend
Messages that a peer wants to send to other peers.
-
Constructor Summary
Constructors Constructor Description FastLeaderElection(QuorumPeer self, QuorumCnxManager manager)
Constructor of FastLeaderElection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected boolean
checkLeader(java.util.HashMap<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 vlue of the logical clock counterVote
lookForLeader()
Starts a new round of leader election.protected boolean
ooePredicate(java.util.HashMap<java.lang.Long,Vote> recv, java.util.HashMap<java.lang.Long,Vote> ooe, FastLeaderElection.Notification n)
This predicate checks that a leader has been elected.void
shutdown()
protected boolean
termPredicate(java.util.HashMap<java.lang.Long,Vote> votes, Vote vote)
Termination predicate.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.
-
-
-
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 objectmanager
- Connection manager
-
-
Method Detail
-
getLogicalClock
public long getLogicalClock()
Returns the current vlue of the logical clock counter
-
getCnxManager
public QuorumCnxManager getCnxManager()
-
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.- Parameters:
id
- Server identifierzxid
- Last zxid observed by the issuer of this vote
-
termPredicate
protected boolean termPredicate(java.util.HashMap<java.lang.Long,Vote> votes, Vote vote)
Termination predicate. Given a set of votes, determines if have sufficient to declare the end of the election round.- Parameters:
votes
- Set of votesl
- Identifier of the vote received lastzxid
- zxid of the the vote received last
-
checkLeader
protected boolean checkLeader(java.util.HashMap<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 votesleader
- leader idelectionEpoch
- epoch id
-
ooePredicate
protected boolean ooePredicate(java.util.HashMap<java.lang.Long,Vote> recv, java.util.HashMap<java.lang.Long,Vote> ooe, FastLeaderElection.Notification n)
This predicate checks that a leader has been elected. It doesn't make a lot of sense without context (check lookForLeader) and it has been separated for testing purposes.- Parameters:
recv
- map of received votesooe
- map containing out of election votes (LEADING or FOLLOWING)n
- Notification- Returns:
-
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 interfaceElection
- Throws:
java.lang.InterruptedException
-
-