Class AbstractRaftActorBehavior

    • Field Detail

      • context

        protected final RaftActorContext context
        Information about the RaftActor whose behavior this class represents.
      • log

        protected final Logger log
        Used for message logging.
    • Method Detail

      • logName

        protected final String logName()
      • setReplicatedToAllIndex

        public void setReplicatedToAllIndex​(long replicatedToAllIndex)
        Description copied from interface: RaftActorBehavior
        Sets the index of the last log entry that has been replicated to all peers.
        Specified by:
        setReplicatedToAllIndex in interface RaftActorBehavior
        Parameters:
        replicatedToAllIndex - the index
      • getReplicatedToAllIndex

        public long getReplicatedToAllIndex()
        Description copied from interface: RaftActorBehavior
        Returns the index of the last log entry that has been replicated to all peers.
        Specified by:
        getReplicatedToAllIndex in interface RaftActorBehavior
        Returns:
        the index or -1 if not known
      • handleAppendEntries

        protected abstract RaftActorBehavior handleAppendEntries​(ActorRef sender,
                                                                 AppendEntries appendEntries)
        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
        Parameters:
        sender - The actor that sent this message
        appendEntries - The AppendEntries message
        Returns:
        a new behavior if it was changed or the current behavior
      • appendEntries

        protected RaftActorBehavior appendEntries​(ActorRef sender,
                                                  AppendEntries appendEntries)
        Handles the common logic for the AppendEntries message and delegates handling to the derived class.
        Parameters:
        sender - the ActorRef that sent the message
        appendEntries - the message
        Returns:
        a new behavior if it was changed or the current behavior
      • handleAppendEntriesReply

        protected abstract RaftActorBehavior handleAppendEntriesReply​(ActorRef sender,
                                                                      AppendEntriesReply appendEntriesReply)
        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
        Parameters:
        sender - The actor that sent this message
        appendEntriesReply - The AppendEntriesReply message
        Returns:
        a new behavior if it was changed or the current behavior
      • requestVote

        protected RaftActorBehavior requestVote​(ActorRef sender,
                                                RequestVote requestVote)
        Handles the logic for the RequestVote message that is common for all behaviors.
        Parameters:
        sender - the ActorRef that sent the message
        requestVote - the message
        Returns:
        a new behavior if it was changed or the current behavior
      • canGrantVote

        protected boolean canGrantVote​(RequestVote requestVote)
      • handleRequestVoteReply

        protected abstract RaftActorBehavior handleRequestVoteReply​(ActorRef sender,
                                                                    RequestVoteReply requestVoteReply)
        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
        Parameters:
        sender - The actor that sent this message
        requestVoteReply - The RequestVoteReply message
        Returns:
        a new behavior if it was changed or the current behavior
      • electionDuration

        protected scala.concurrent.duration.FiniteDuration electionDuration()
        Returns a duration for election with an additional variance for randomness.
        Returns:
        a random election duration
      • stopElection

        protected void stopElection()
        Stops the currently scheduled election.
      • canStartElection

        protected boolean canStartElection()
      • scheduleElection

        protected void scheduleElection​(scala.concurrent.duration.FiniteDuration interval)
        Schedule a new election.
        Parameters:
        interval - the duration after which we should trigger a new election
      • currentTerm

        protected long currentTerm()
        Returns the current election term.
        Returns:
        the current term
      • votedFor

        protected String votedFor()
        Returns the id of the candidate that this server voted for in current term.
        Returns:
        the candidate for whom we voted in the current term
      • actor

        protected ActorRef actor()
        Returns the actor associated with this behavior.
        Returns:
        the actor
      • lastTerm

        protected long lastTerm()
        Returns the term of the last entry in the log.
        Returns:
        the term
      • lastIndex

        protected long lastIndex()
        Returns the index of the last entry in the log.
        Returns:
        the index
      • getLogEntryIndex

        protected long getLogEntryIndex​(long index)
        Returns the actual index of the entry in replicated log for the given index or -1 if not found.
        Returns:
        the log entry index or -1 if not found
      • getLogEntryTerm

        protected long getLogEntryTerm​(long index)
        Returns the actual term of the entry in the replicated log for the given index or -1 if not found.
        Returns:
        the log entry term or -1 if not found
      • getLogEntryOrSnapshotTerm

        protected long getLogEntryOrSnapshotTerm​(long index)
        Returns the actual term of the entry in the replicated log for the given index or, if not present, returns the snapshot term if the given index is in the snapshot or -1 otherwise.
        Returns:
        the term or -1 otherwise
      • applyLogToStateMachine

        protected void applyLogToStateMachine​(long index)
        Applies the log entries up to the specified index that is known to be committed to the state machine.
        Parameters:
        index - the log index
      • 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
        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.
      • getMajorityVoteCount

        protected int getMajorityVoteCount​(int numPeers)
      • performSnapshotWithoutCapture

        protected void performSnapshotWithoutCapture​(long snapshotCapturedIndex)
        Performs a snapshot with no capture on the replicated log. It clears the log from the supplied index or lastApplied-1 which ever is minimum.
        Parameters:
        snapshotCapturedIndex - the index from which to clear
      • getId

        protected String getId()
      • shouldUpdateTerm

        protected boolean shouldUpdateTerm​(RaftRPC rpc)