Class ELECTION
- All Implemented Interfaces:
org.jgroups.Lifecycle
Performs leader election. This implementation takes full advantage of JGroup's membership events with View.
When the current node is the view coordinator, it starts a voting thread to ask all members to send their information.
The voting thread stops when a new leader is elected.
The process that starts the voting thread is not trying to elect itself. The process running the voting process
increases its term and asks all nodes about their term and log index information to select the new leader, in the
form of VoteResponse. For safety reasons, only the nodes with the most
up-to-date log can be elected a leader. With a response from the majority processes, the leader with the higher term
and log index is elected. The oldest process (view coordinator) in the system has a priority. Once decided, the
process sends a message reliably to everyone identifying the new leader, with the
LeaderElected message.
After a leader is elected, a new election round starts on view changes only if the leader left the cluster. In case of losing a majority, the leader steps down.
This implementation is more robust than building with heartbeats, leading to fewer disruptions in the cluster with unnecessary (competing) election rounds. This also means the leader is capable of stepping down. Referred to in ยง6.2 of Ongaro's dissertation to prevent stale leadership information.
More information is available in the design docs.
- Since:
- 0.1
- Author:
- Bela Ban
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final shortstatic final shortprotected intstatic final shortstatic final shortprotected RAFTprotected org.jgroups.Viewstatic final shortstatic final shortprotected longFields inherited from class org.jgroups.stack.Protocol
after_creation_hook, down_prot, ergonomics, id, local_addr, log, policies, preview_warning, stack, stats, up_prot -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected org.jgroups.AddressdetermineLeader(org.jgroups.Address exclude) down(org.jgroups.Event evt) doubleorg.jgroups.util.ResponseCollector<org.jgroups.protocols.raft.election.VoteResponse> getVotes()protected voidhandleMessage(org.jgroups.Message msg, RaftHeader hdr) protected voidhandleView(org.jgroups.View v) voidinit()protected final booleanprotected booleanbooleanraft()org.jgroups.protocols.raft.election.BaseElectionvoidbooleanprotected voidsendLeaderElectedMessage(org.jgroups.Address leader, long term) protected voidsendVoteRequest(long new_term) protected voidsendVoteResponse(org.jgroups.Address dest, long term, long last_log_term, long last_log_index) voidstart()CompletionStage<org.jgroups.Address> startForcedElection(org.jgroups.Address exclude) Starts a forced leader election, excluding the given address from candidacy.org.jgroups.protocols.raft.election.BaseElectionvoidstop()org.jgroups.protocols.raft.election.BaseElectionlongup(org.jgroups.Event evt) up(org.jgroups.Message msg) voidup(org.jgroups.util.MessageBatch batch) longorg.jgroups.protocols.raft.election.BaseElectionvoteTimeout(long timeoutMs) Defines the default timeout in milliseconds to utilize during any election operation.Methods inherited from class org.jgroups.stack.Protocol
accept, addPolicy, addr, addr, afterCreationHook, destroy, down, down, enableStats, getAddress, getComponents, getDownProtocol, getDownServices, getId, getIdsAbove, getLevel, getLog, getName, getPolicies, getProtocolStack, getSocketFactory, getThreadFactory, getTransport, getUpProtocol, getUpServices, getValue, isErgonomics, level, parse, policies, previewWarning, previewWarning, providedDownServices, providedUpServices, removePolicy, requiredDownServices, requiredUpServices, resetStatistics, setAddress, setDownProtocol, setErgonomics, setId, setLevel, setPolicies, setProtocolStack, setSocketFactory, setUpProtocol, setValue, statsEnabled, toString
-
Field Details
-
ELECTION_ID
public static final short ELECTION_ID- See Also:
-
VOTE_REQ
public static final short VOTE_REQ- See Also:
-
VOTE_RSP
public static final short VOTE_RSP- See Also:
-
LEADER_ELECTED
public static final short LEADER_ELECTED- See Also:
-
PRE_VOTE_REQ
public static final short PRE_VOTE_REQ- See Also:
-
PRE_VOTE_RSP
public static final short PRE_VOTE_RSP- See Also:
-
raft
-
vote_timeout
protected long vote_timeout -
num_voting_rounds
protected int num_voting_rounds -
view
protected volatile org.jgroups.View view
-
-
Constructor Details
-
ELECTION
public ELECTION()
-
-
Method Details
-
handleView
protected void handleView(org.jgroups.View v) -
voteTimeout
public long voteTimeout() -
voteTimeout
public org.jgroups.protocols.raft.election.BaseElection voteTimeout(long timeoutMs) Defines the default timeout in milliseconds to utilize during any election operation.- Parameters:
timeoutMs- Timeout value in milliseconds.- Returns:
- This election instance.
- Throws:
IllegalArgumentException- In case timeout is less than or equal to 0.
-
raft
-
raft
-
getVotes
public org.jgroups.util.ResponseCollector<org.jgroups.protocols.raft.election.VoteResponse> getVotes() -
isVotingThreadRunning
public boolean isVotingThreadRunning() -
runVotingThread
public boolean runVotingThread() -
electionStart
-
electionEnd
-
timeSinceLastElection
public long timeSinceLastElection() -
electionMeanLatency
public double electionMeanLatency() -
resetStats
public void resetStats()- Overrides:
resetStatsin classorg.jgroups.stack.Protocol
-
init
-
start
-
stop
public void stop()- Specified by:
stopin interfaceorg.jgroups.Lifecycle- Overrides:
stopin classorg.jgroups.stack.Protocol
-
down
- Overrides:
downin classorg.jgroups.stack.Protocol
-
up
- Overrides:
upin classorg.jgroups.stack.Protocol
-
up
- Overrides:
upin classorg.jgroups.stack.Protocol
-
up
public void up(org.jgroups.util.MessageBatch batch) - Overrides:
upin classorg.jgroups.stack.Protocol
-
electionLatency
- Returns:
- election latency metrics, or disabled metrics if stats are off.
-
handleMessage
-
isViewCoordinator
protected boolean isViewCoordinator() -
determineLeader
protected org.jgroups.Address determineLeader(org.jgroups.Address exclude) -
isMajorityAvailable
protected final boolean isMajorityAvailable() -
startForcedElection
Starts a forced leader election, excluding the given address from candidacy.Must be called on the view coordinator. The excluded address still participates in quorum (votes are collected from it), but it is skipped when determining the leader.
- Parameters:
exclude- the address to exclude from leader candidacy, ornullfor no exclusion.- Returns:
- a stage that completes with the newly elected leader's address.
-
startVotingThread
public org.jgroups.protocols.raft.election.BaseElection startVotingThread() -
sendVoteRequest
protected void sendVoteRequest(long new_term) -
sendLeaderElectedMessage
protected void sendLeaderElectedMessage(org.jgroups.Address leader, long term) -
sendVoteResponse
protected void sendVoteResponse(org.jgroups.Address dest, long term, long last_log_term, long last_log_index) -
stopVotingThread
public org.jgroups.protocols.raft.election.BaseElection stopVotingThread()
-