Class AbstractRaftActorBehavior
java.lang.Object
org.opendaylight.controller.cluster.raft.behaviors.AbstractRaftActorBehavior
- All Implemented Interfaces:
AutoCloseable,RaftActorBehavior
- Direct Known Subclasses:
AbstractLeader,Candidate,Follower
Abstract class that provides common code for a RaftActor behavior.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final RaftActorContextInformation about the RaftActor whose behavior this class represents.protected final org.slf4j.LoggerUsed for message logging. -
Method Summary
Modifier and TypeMethodDescriptionprotected final akka.actor.ActorRefactor()Returns the actor associated with this behavior.protected RaftActorBehaviorappendEntries(akka.actor.ActorRef sender, AppendEntries appendEntries) Handles the common logic for the AppendEntries message and delegates handling to the derived class.protected voidapplyLogToStateMachine(long index) Applies the log entries up to the specified index that is known to be committed to the state machine.protected booleancanGrantVote(RequestVote requestVote) protected booleanstatic RaftActorBehaviorcreateBehavior(RaftActorContext context, RaftState state) protected longReturns the current election term.protected scala.concurrent.duration.FiniteDurationReturns a duration for election with an additional variance for randomness.protected final StringgetId()protected longgetLogEntryIndex(long index) Returns the actual index of the entry in replicated log for the given index or -1 if not found.protected longgetLogEntryOrSnapshotTerm(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.protected longgetLogEntryTerm(long index) Returns the actual term of the entry in the replicated log for the given index or -1 if not found.protected intgetMajorityVoteCount(int numPeers) longReturns the index of the last log entry that has been replicated to all peers.protected abstract RaftActorBehaviorhandleAppendEntries(akka.actor.ActorRef sender, AppendEntries appendEntries) Derived classes should not directly handle AppendEntries messages it should let the base class handle it first.protected abstract RaftActorBehaviorhandleAppendEntriesReply(akka.actor.ActorRef sender, AppendEntriesReply appendEntriesReply) Derived classes should not directly handle AppendEntriesReply messages it should let the base class handle it first.handleMessage(akka.actor.ActorRef sender, Object message) Handle a message.protected abstract RaftActorBehaviorhandleRequestVoteReply(akka.actor.ActorRef sender, RequestVoteReply requestVoteReply) Derived classes should not directly handle RequestVoteReply messages it should let the base class handle it first.protected RaftActorBehaviorinternalSwitchBehavior(RaftActorBehavior newBehavior) protected RaftActorBehaviorinternalSwitchBehavior(RaftState newState) protected longReturns the index of the last entry in the log.protected longlastTerm()Returns the term of the last entry in the log.protected final StringlogName()protected voidperformSnapshotWithoutCapture(long snapshotCapturedIndex) Performs a snapshot with no capture on the replicated log.protected RaftActorBehaviorrequestVote(akka.actor.ActorRef sender, RequestVote requestVote) Handles the logic for the RequestVote message that is common for all behaviors.protected voidscheduleElection(scala.concurrent.duration.FiniteDuration interval) Schedule a new election.voidsetReplicatedToAllIndex(long replicatedToAllIndex) Sets the index of the last log entry that has been replicated to all peers.protected booleanshouldUpdateTerm(RaftRPC rpc) final RaftStatestate()Returns the state associated with this behavior.protected voidStops the currently scheduled election.switchBehavior(RaftActorBehavior behavior) Closes the current behavior and switches to the specified behavior, if possible.protected StringvotedFor()Returns the id of the candidate that this server voted for in current term.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior
close, getLeaderId, getLeaderPayloadVersion
-
Field Details
-
context
Information about the RaftActor whose behavior this class represents. -
log
protected final org.slf4j.Logger logUsed for message logging.
-
-
Method Details
-
createBehavior
-
state
Description copied from interface:RaftActorBehaviorReturns the state associated with this behavior.- Specified by:
statein interfaceRaftActorBehavior- Returns:
- the RaftState
-
logName
-
setReplicatedToAllIndex
public void setReplicatedToAllIndex(long replicatedToAllIndex) Description copied from interface:RaftActorBehaviorSets the index of the last log entry that has been replicated to all peers.- Specified by:
setReplicatedToAllIndexin interfaceRaftActorBehavior- Parameters:
replicatedToAllIndex- the index
-
getReplicatedToAllIndex
public long getReplicatedToAllIndex()Description copied from interface:RaftActorBehaviorReturns the index of the last log entry that has been replicated to all peers.- Specified by:
getReplicatedToAllIndexin interfaceRaftActorBehavior- 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 messageappendEntries- The AppendEntries message- Returns:
- a new behavior if it was changed or the current behavior
-
appendEntries
Handles the common logic for the AppendEntries message and delegates handling to the derived class.- Parameters:
sender- the ActorRef that sent the messageappendEntries- 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 messageappendEntriesReply- The AppendEntriesReply message- Returns:
- a new behavior if it was changed or the current behavior
-
requestVote
Handles the logic for the RequestVote message that is common for all behaviors.- Parameters:
sender- the ActorRef that sent the messagerequestVote- the message- Returns:
- a new behavior if it was changed or the current behavior
-
canGrantVote
-
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 messagerequestVoteReply- 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
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
Description copied from interface:RaftActorBehaviorHandle 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:
handleMessagein interfaceRaftActorBehavior- Parameters:
sender- The sender of the messagemessage- A message that needs to be processed- Returns:
- The new behavior or current behavior, or null if the message was not handled.
-
switchBehavior
Description copied from interface:RaftActorBehaviorCloses the current behavior and switches to the specified behavior, if possible.- Specified by:
switchBehaviorin interfaceRaftActorBehavior- Parameters:
behavior- the new behavior to switch to- Returns:
- the new behavior
-
internalSwitchBehavior
-
internalSwitchBehavior
-
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
-
shouldUpdateTerm
-