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 process which executes the pre-voting mechanism sends a 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

    Fields
    Modifier and Type
    Field
    Description
    static final short
     
    static final short
     
    protected int
     
    static final short
     
    static final short
     
    protected RAFT
     
    protected org.jgroups.View
     
    static final short
     
    static final short
     
    protected long
     

    Fields 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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected org.jgroups.Address
    determineLeader(org.jgroups.Address exclude)
     
    down(org.jgroups.Event evt)
     
     
     
    double
     
     
    org.jgroups.util.ResponseCollector<org.jgroups.protocols.raft.election.VoteResponse>
     
    protected void
    handleMessage(org.jgroups.Message msg, RaftHeader hdr)
     
    protected void
    handleView(org.jgroups.View v)
     
    void
     
    protected final boolean
     
    boolean
     
    protected boolean
     
    boolean
     
     
    org.jgroups.protocols.raft.election.BaseElection
     
    void
     
    boolean
     
    protected void
    sendLeaderElectedMessage(org.jgroups.Address leader, long term)
     
    protected void
    sendVoteRequest(long new_term)
     
    protected void
    sendVoteResponse(org.jgroups.Address dest, long term, long last_log_term, long last_log_index)
     
    void
     
    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.BaseElection
     
    void
     
    org.jgroups.protocols.raft.election.BaseElection
     
    long
     
    up(org.jgroups.Event evt)
     
    up(org.jgroups.Message msg)
     
    void
    up(org.jgroups.util.MessageBatch batch)
     
    long
     
    org.jgroups.protocols.raft.election.BaseElection
    voteTimeout(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

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • 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

      protected RAFT 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

      protected void handleMessage(org.jgroups.Message msg, RaftHeader hdr)
    • 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

      public RAFT raft()
    • raft

      public org.jgroups.protocols.raft.election.BaseElection raft(RAFT r)
    • getVotes

      public org.jgroups.util.ResponseCollector<org.jgroups.protocols.raft.election.VoteResponse> getVotes()
    • isVotingThreadRunning

      public boolean isVotingThreadRunning()
    • runVotingThread

      public boolean runVotingThread()
    • electionStart

      public Instant electionStart()
    • electionEnd

      public Instant electionEnd()
    • timeSinceLastElection

      public long timeSinceLastElection()
    • electionMeanLatency

      public double electionMeanLatency()
    • resetStats

      public void resetStats()
      Overrides:
      resetStats in class org.jgroups.stack.Protocol
    • init

      public void init() throws Exception
      Specified by:
      init in interface org.jgroups.Lifecycle
      Overrides:
      init in class org.jgroups.stack.Protocol
      Throws:
      Exception
    • start

      public void start() throws Exception
      Specified by:
      start in interface org.jgroups.Lifecycle
      Overrides:
      start in class org.jgroups.stack.Protocol
      Throws:
      Exception
    • stop

      public void stop()
      Specified by:
      stop in interface org.jgroups.Lifecycle
      Overrides:
      stop in class org.jgroups.stack.Protocol
    • down

      public Object down(org.jgroups.Event evt)
      Overrides:
      down in class org.jgroups.stack.Protocol
    • up

      public Object up(org.jgroups.Event evt)
      Overrides:
      up in class org.jgroups.stack.Protocol
    • up

      public Object up(org.jgroups.Message msg)
      Overrides:
      up in class org.jgroups.stack.Protocol
    • up

      public void up(org.jgroups.util.MessageBatch batch)
      Overrides:
      up in class org.jgroups.stack.Protocol
    • electionLatency

      public LatencyMetrics 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

      public CompletionStage<org.jgroups.Address> startForcedElection(org.jgroups.Address exclude)
      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, or null for 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()