Class Leader

  • All Implemented Interfaces:
    AutoCloseable, RaftActorBehavior

    public 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).
    • Method Detail

      • handleMessage

        public RaftActorBehavior handleMessage​(ActorRef sender,
                                               Object originalMessage)
        Description copied from interface: 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.
        Specified by:
        handleMessage in interface RaftActorBehavior
        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.
      • handleAppendEntriesReply

        protected RaftActorBehavior handleAppendEntriesReply​(ActorRef sender,
                                                             AppendEntriesReply appendEntriesReply)
        Description copied from class: AbstractRaftActorBehavior
        Derived classes should not directly handle AppendEntriesReply messages it should let the base class handle it first. Once the base class handles the AppendEntriesReply message and does the common actions that are applicable in all RaftState's it will delegate the handling of the AppendEntriesReply message to the derived class to do more state specific handling by calling this method
        Overrides:
        handleAppendEntriesReply in class AbstractLeader
        Parameters:
        sender - The actor that sent this message
        appendEntriesReply - The AppendEntriesReply message
        Returns:
        a new behavior if it was changed or the current behavior
      • 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