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 Summary
Fields Modifier and Type Field Description protected RaftActorContext
context
Information about the RaftActor whose behavior this class represents.protected Logger
log
Used for message logging.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected ActorRef
actor()
Returns the actor associated with this behavior.protected RaftActorBehavior
appendEntries(ActorRef sender, AppendEntries appendEntries)
Handles the common logic for the AppendEntries message and delegates handling to the derived class.protected void
applyLogToStateMachine(long index)
Applies the log entries up to the specified index that is known to be committed to the state machine.protected boolean
canGrantVote(RequestVote requestVote)
protected boolean
canStartElection()
static RaftActorBehavior
createBehavior(RaftActorContext context, RaftState state)
protected long
currentTerm()
Returns the current election term.protected scala.concurrent.duration.FiniteDuration
electionDuration()
Returns a duration for election with an additional variance for randomness.protected String
getId()
protected long
getLogEntryIndex(long index)
Returns the actual index of the entry in replicated log for the given index or -1 if not found.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.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.protected int
getMajorityVoteCount(int numPeers)
long
getReplicatedToAllIndex()
Returns the index of the last log entry that has been replicated to all peers.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.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.RaftActorBehavior
handleMessage(ActorRef sender, Object message)
Handle a message.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.protected RaftActorBehavior
internalSwitchBehavior(RaftActorBehavior newBehavior)
protected RaftActorBehavior
internalSwitchBehavior(RaftState newState)
protected long
lastIndex()
Returns the index of the last entry in the log.protected long
lastTerm()
Returns the term of the last entry in the log.protected String
logName()
protected void
performSnapshotWithoutCapture(long snapshotCapturedIndex)
Performs a snapshot with no capture on the replicated log.protected RaftActorBehavior
requestVote(ActorRef sender, RequestVote requestVote)
Handles the logic for the RequestVote message that is common for all behaviors.protected void
scheduleElection(scala.concurrent.duration.FiniteDuration interval)
Schedule a new election.void
setReplicatedToAllIndex(long replicatedToAllIndex)
Sets the index of the last log entry that has been replicated to all peers.protected boolean
shouldUpdateTerm(RaftRPC rpc)
RaftState
state()
Returns the state associated with this behavior.protected void
stopElection()
Stops the currently scheduled election.RaftActorBehavior
switchBehavior(RaftActorBehavior behavior)
Closes the current behavior and switches to the specified behavior, if possible.protected String
votedFor()
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, wait
-
Methods inherited from interface org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior
close, getLeaderId, getLeaderPayloadVersion
-
-
-
-
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
-
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 interfaceRaftActorBehavior
- 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 interfaceRaftActorBehavior
- 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 interfaceRaftActorBehavior
- 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 messageappendEntries
- 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 messageappendEntries
- 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 messageappendEntriesReply
- 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 messagerequestVote
- 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 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
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 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
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 interfaceRaftActorBehavior
- 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 String getId()
-
shouldUpdateTerm
protected boolean shouldUpdateTerm(RaftRPC rpc)
-
-