Class AbstractRaftActorBehavior

java.lang.Object
org.opendaylight.controller.cluster.raft.behaviors.AbstractRaftActorBehavior
All Implemented Interfaces:
AutoCloseable, RaftActorBehavior
Direct Known Subclasses:
AbstractLeader, Candidate, Follower

public abstract class AbstractRaftActorBehavior extends Object implements RaftActorBehavior
Abstract class that provides common code for a RaftActor behavior.
  • Field Details

    • context

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

      protected final org.slf4j.Logger log
      Used for message logging.
  • Method Details

    • createBehavior

      public static RaftActorBehavior createBehavior(RaftActorContext context, RaftState state)
    • state

      public final RaftState state()
      Description copied from interface: RaftActorBehavior
      Returns the state associated with this behavior.
      Specified by:
      state in interface RaftActorBehavior
      Returns:
      the RaftState
    • 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(akka.actor.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(akka.actor.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(akka.actor.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(akka.actor.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(akka.actor.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 final akka.actor.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(akka.actor.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.
    • switchBehavior

      public RaftActorBehavior switchBehavior(RaftActorBehavior behavior)
      Description copied from interface: RaftActorBehavior
      Closes the current behavior and switches to the specified behavior, if possible.
      Specified by:
      switchBehavior in interface RaftActorBehavior
      Parameters:
      behavior - the new behavior to switch to
      Returns:
      the new behavior
    • internalSwitchBehavior

      protected RaftActorBehavior internalSwitchBehavior(RaftState newState)
    • internalSwitchBehavior

      protected RaftActorBehavior internalSwitchBehavior(RaftActorBehavior newBehavior)
    • 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 final String getId()
    • shouldUpdateTerm

      protected boolean shouldUpdateTerm(RaftRPC rpc)