Class ELECTION2
java.lang.Object
org.jgroups.stack.Protocol
org.jgroups.protocols.raft.ELECTION2
- All Implemented Interfaces:
org.jgroups.Lifecycle
public class ELECTION2
extends org.jgroups.stack.Protocol
A leader election protocol.
This implementation extends ELECTION with a pre-vote mechanism. The pre-vote always runs before starting the
voting thread to define the leader. The pre-vote increases the delay in electing the leader but, in turn, covers more
edge cases. As a rule of thumb, if deploying in an unstable network with frequent partitions, this protocol should
give a more stable mechanism, avoid leader disruptions, and avoid possible liveness issues. Otherwise,
ELECTION is the default choice.
Pre-Voting phase:
This extension includes the pre-voting mechanism proposed in Ongaro's dissertation (§9.6). In the current implementation, a pre-voting phase starts in case the current node is the new view coordinator and:- The view coordinator changes and the computed update is
Utils.Majority.no_change; - The computed view update is
Utils.Majority.reachedorUtils.Majority.leader_lost
PreVoteRequest to all processes in the view. The
recipients reply with a PreVoteResponse identifying the node they see as leader. Once the initiator receives
the reply from all nodes in the view, it can start the voting process, resuming the work the same as ELECTION.- Since:
- 1.0.12
- Author:
- José Bolina
- 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 booleanbooleanprotected 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
-
ELECTION2
public ELECTION2()
-
-
Method Details
-
isPreVoteThreadRunning
public boolean isPreVoteThreadRunning() -
handleView
protected void handleView(org.jgroups.View v) -
handleMessage
-
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.
-
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()
-