# Class FastLeaderElection

java.lang.Object
org.apache.zookeeper.server.quorum.FastLeaderElection

All Implemented Interfaces:
  `Election`

public class FastLeaderElection extends 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 final String`
`MAX_NOTIFICATION_INTERVAL`
Maximum notification interval, default is 60s
`static final String`
`MIN_NOTIFICATION_INTERVAL`
Minimum notification interval, default is equal to finalizeWait

## Constructor Summary

Constructors
Constructor
Description
`FastLeaderElection(QuorumPeer self, QuorumCnxManager manager)`
Constructor of FastLeaderElection.

## Method Summary

Modifier and Type
Method
Description
`protected boolean`
`checkLeader(Map<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(Map<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 Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### MIN_NOTIFICATION_INTERVAL

public static final String MIN_NOTIFICATION_INTERVAL
Minimum notification interval, default is equal to finalizeWait

See Also:
  - Constant Field Values

### MAX_NOTIFICATION_INTERVAL

public static final String MAX_NOTIFICATION_INTERVAL
Maximum notification interval, default is 60s

See Also:
  - Constant Field Values

## Constructor Details

### 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 Details

### 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.

### getCnxManager

public QuorumCnxManager getCnxManager()

### 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(Map<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(Map<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 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:
  `InterruptedException`