Class AbstractLeader

  • All Implemented Interfaces:
    AutoCloseable, RaftActorBehavior
    Direct Known Subclasses:
    IsolatedLeader, Leader, PreLeader

    public abstract class AbstractLeader
    extends AbstractRaftActorBehavior
    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

      • getFollowerIds

        public final Collection<String> getFollowerIds()
        Return an immutable collection of follower identifiers.
        Returns:
        Collection of follower IDs
      • addFollower

        public void addFollower​(String followerId)
      • removeFollower

        public void removeFollower​(String followerId)
      • updateMinReplicaCount

        public void updateMinReplicaCount()
      • getMinIsolatedLeaderPeerCount

        protected int getMinIsolatedLeaderPeerCount()
      • handleAppendEntries

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

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

        protected void beforeSendHeartbeat()
      • handleMessage

        public RaftActorBehavior handleMessage​(ActorRef sender,
                                               Object message)
        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 AbstractRaftActorBehavior
        Parameters:
        sender - The sender of the message
        message - A message that needs to be processed
        Returns:
        The new behavior or current behavior, or null if the message was not handled.
      • sendAppendEntries

        protected void sendAppendEntries​(long timeSinceLastActivityIntervalNanos,
                                         boolean isHeartbeat)
      • initiateCaptureSnapshot

        public boolean initiateCaptureSnapshot​(String followerId)
        Initiates a snapshot capture to install on a follower.

        Install Snapshot works as follows 1. Leader initiates the capture snapshot by calling createSnapshot on the RaftActor. 2. On receipt of the CaptureSnapshotReply message, the RaftActor persists the snapshot and makes a call to the Leader's handleMessage with a SendInstallSnapshot message. 3. The Leader obtains and stores the Snapshot from the SendInstallSnapshot message and sends it in chunks to the Follower via InstallSnapshot messages. 4. For each chunk, the Follower sends back an InstallSnapshotReply. 5. On receipt of the InstallSnapshotReply for the last chunk, the Leader marks the install complete for that follower. 6. If another follower requires a snapshot and a snapshot has been collected (via SendInstallSnapshot) then send the existing snapshot in chunks to the follower.

        Parameters:
        followerId - the id of the follower.
        Returns:
        true if capture was initiated, false otherwise.
      • close

        public void close()
      • getLeaderId

        public final String getLeaderId()
        Description copied from interface: RaftActorBehavior
        Returns the id of the leader.
        Returns:
        the id of the leader or null if not known
      • getLeaderPayloadVersion

        public final short getLeaderPayloadVersion()
        Description copied from interface: RaftActorBehavior
        Returns the leader's payload data version.
        Returns:
        a short representing the version
      • isLeaderIsolated

        protected boolean isLeaderIsolated()
      • printFollowerStates

        public String printFollowerStates()
      • followerLogSize

        public int followerLogSize()