Class RaftState

java.lang.Object
org.jgroups.protocols.raft.state.RaftState

@ThreadSafe public class RaftState extends Object
Keep track of part of the algorithm state. The state here is tightly synchronized and needs to be updated atomically.

Throughout the RAFT execution, the node updates the values for term, leader, and the election vote. The values need to be updated atomically, following the term transition. This synchronization is necessary for clients to perceive the same leaders and terms.

Utilizing both Ongaro's dissertation1 and the original RAFT2 article as the base. The relation of all these variables in the state may not be clear. Starting with the assertion that a term only starts through a new election. In other words, when updating the term, the leader is unknown. The term increases monotonically, and some nodes might skip it due to connectivity issues.

All messages carry the sender's current term. On receiving a message, nodes verify and update. Messages with older terms are discarded. A higher term causes the node to advance its term, set both the leader and vote to null, and proceed with the message processing.

The leader value only updates to the elected leader (null -> new leader) or to step down (leader -> null). Updating the leader directly to another throws an exception. The election vote follows the same logic.

In summary, the logic follows that updating to a received higher term sets the leader and vote to null. Later, when the leader is known, the update within the same term is accepted since the current term leader is still null.

Since:
1.0.12
Author:
José Bolina
See Also:
  • Constructor Details

    • RaftState

      public RaftState(RAFT raft, Consumer<org.jgroups.Address> onLeaderUpdate)
  • Method Details

    • leader

      public org.jgroups.Address leader()
    • currentTerm

      public long currentTerm()
    • votedFor

      public org.jgroups.Address votedFor()
    • advanceTermForElection

      public long advanceTermForElection()
      Advances the term for leader election.

      This automatically sets the leader and votedFor to null.

      Returns:
      The new term.
      See Also:
    • tryAdvanceTerm

      public int tryAdvanceTerm(long newTerm)
      Try advancing the term, and if succeeding, the leader is null.
      Parameters:
      newTerm - The term to advance to.
      Returns:
      Has the same result as Long.compare(newTerm, currentTerm());.
      See Also:
    • tryAdvanceTermAndLeader

      public int tryAdvanceTermAndLeader(long newTerm, org.jgroups.Address newLeader)
      Try to advance the current term and set the leader.

      A lower term has no effect. A higher term updates the node's current term to , and the leader to and vote to null. The term and vote are written to disk.

      Parameters:
      newTerm - New term to update to.
      newLeader - The leader to update in case the term advances.
      Returns:
      Has the same result as Long.compare(newTerm, currentTerm());. That is, -1 if the newTerm < currentTerm(), 0 if both are equal, and 1 if currentTerm() is higher.
    • setLeader

      public void setLeader(org.jgroups.Address newLeader)
      Update the leader in the current term.

      A leader can not change within the same term. The update only happens when a new leader is elected (null -> new leader) or when the leader steps down (current leader -> null).

      Parameters:
      newLeader - The new leader to update to.
      Throws:
      IllegalStateException - If trying to update between different leaders in the same term.
    • setVotedFor

      public void setVotedFor(org.jgroups.Address votedFor)
      Set the vote in the current term.

      Follows the same logic as setLeader(Address). The vote is also written to disk.

      Parameters:
      votedFor - The vote in the current term.
      Throws:
      IllegalStateException - If trying to update between different votes within the same term.
    • reload

      public void reload()
      Read the state information from the RAFT's log.
    • toString

      public String toString()
      Overrides:
      toString in class Object