All Implemented Interfaces:
AutoCloseable

public non-sealed class Leader extends AbstractLeader
The behavior of a RaftActor when it is in the Leader state.

Leaders:

  • Upon election: send initial empty AppendEntries RPCs (heartbeat) to each server; repeat during idle periods to prevent election timeouts (§5.2)
  • If command received from client: append entry to local log, respond after entry applied to state machine (§5.3)
  • If last log index ≥ nextIndex for a follower: send AppendEntries RPC with log entries starting at nextIndex
  • If successful: update nextIndex and matchIndex for follower (§5.3)
  • If AppendEntries fails because of log inconsistency: decrement nextIndex and retry (§5.3)
  • If there exists an N such that N > commitIndex, a majority of matchIndex[i] ≥ N, and log[N].term == currentTerm: set commitIndex = N (§5.3, §5.4).
  • Constructor Details

  • Method Details

    • handleMessage

      public RaftActorBehavior handleMessage(org.apache.pekko.actor.ActorRef sender, Object originalMessage)
      Description copied from class: RaftActorBehavior
      Handle a message. If the processing of the message warrants a state change then a new behavior should be returned otherwise this method should return the current behavior.
      Overrides:
      handleMessage in class AbstractLeader
      Parameters:
      sender - The sender of the message
      originalMessage - A message that needs to be processed
      Returns:
      The new behavior or current behavior, or null if the message was not handled.
    • transferLeadership

      public void transferLeadership(@NonNull RaftActorLeadershipTransferCohort leadershipTransferCohort)
      Attempts to transfer leadership to a follower as per the raft paper (§3.10) as follows:
      • Start a timer (Stopwatch).
      • Send an initial AppendEntries heartbeat to all followers.
      • On AppendEntriesReply, check if the follower's new match Index matches the leader's last index
      • If it matches,
      • Otherwise if the election time out period elapses, notify RaftActorLeadershipTransferCohort.abortTransfer().
      Parameters:
      leadershipTransferCohort - the cohort participating in the leadership transfer
    • close

      public final void close()
      Specified by:
      close in interface AutoCloseable
      Overrides:
      close in class AbstractLeader