Class AbstractLeader
- java.lang.Object
-
- org.opendaylight.controller.cluster.raft.behaviors.AbstractRaftActorBehavior
-
- org.opendaylight.controller.cluster.raft.behaviors.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).
-
-
Field Summary
-
Fields inherited from class org.opendaylight.controller.cluster.raft.behaviors.AbstractRaftActorBehavior
context, log
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractLeader(RaftActorContext context, RaftState state)protectedAbstractLeader(RaftActorContext context, RaftState state, @Nullable AbstractLeader initializeFromLeader)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddFollower(String followerId)protected voidbeforeSendHeartbeat()voidclose()intfollowerLogSize()FollowerLogInformationgetFollower(String followerId)Collection<String>getFollowerIds()Return an immutable collection of follower identifiers.StringgetLeaderId()Returns the id of the leader.shortgetLeaderPayloadVersion()Returns the leader's payload data version.protected intgetMinIsolatedLeaderPeerCount()protected RaftActorBehaviorhandleAppendEntries(ActorRef sender, AppendEntries appendEntries)Derived classes should not directly handle AppendEntries messages it should let the base class handle it first.protected RaftActorBehaviorhandleAppendEntriesReply(ActorRef sender, AppendEntriesReply appendEntriesReply)Derived classes should not directly handle AppendEntriesReply messages it should let the base class handle it first.RaftActorBehaviorhandleMessage(ActorRef sender, Object message)Handle a message.protected RaftActorBehaviorhandleRequestVoteReply(ActorRef sender, RequestVoteReply requestVoteReply)Derived classes should not directly handle RequestVoteReply messages it should let the base class handle it first.booleaninitiateCaptureSnapshot(String followerId)Initiates a snapshot capture to install on a follower.protected booleanisLeaderIsolated()StringprintFollowerStates()voidremoveFollower(String followerId)protected voidsendAppendEntries(long timeSinceLastActivityIntervalNanos, boolean isHeartbeat)voidupdateMinReplicaCount()-
Methods inherited from class org.opendaylight.controller.cluster.raft.behaviors.AbstractRaftActorBehavior
actor, appendEntries, applyLogToStateMachine, canGrantVote, canStartElection, createBehavior, currentTerm, electionDuration, getId, getLogEntryIndex, getLogEntryOrSnapshotTerm, getLogEntryTerm, getMajorityVoteCount, getReplicatedToAllIndex, internalSwitchBehavior, internalSwitchBehavior, lastIndex, lastTerm, logName, performSnapshotWithoutCapture, requestVote, scheduleElection, setReplicatedToAllIndex, shouldUpdateTerm, state, stopElection, switchBehavior, votedFor
-
-
-
-
Constructor Detail
-
AbstractLeader
protected AbstractLeader(RaftActorContext context, RaftState state, @Nullable AbstractLeader initializeFromLeader)
-
AbstractLeader
protected AbstractLeader(RaftActorContext context, RaftState state)
-
-
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:AbstractRaftActorBehaviorDerived 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:
handleAppendEntriesin classAbstractRaftActorBehavior- Parameters:
sender- The actor that sent this messageappendEntries- 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:AbstractRaftActorBehaviorDerived 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:
handleAppendEntriesReplyin classAbstractRaftActorBehavior- Parameters:
sender- The actor that sent this messageappendEntriesReply- 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:AbstractRaftActorBehaviorDerived 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:
handleRequestVoteReplyin classAbstractRaftActorBehavior- Parameters:
sender- The actor that sent this messagerequestVoteReply- 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: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- Overrides:
handleMessagein classAbstractRaftActorBehavior- 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.
-
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:RaftActorBehaviorReturns 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:RaftActorBehaviorReturns the leader's payload data version.- Returns:
- a short representing the version
-
isLeaderIsolated
protected boolean isLeaderIsolated()
-
printFollowerStates
public String printFollowerStates()
-
getFollower
public FollowerLogInformation getFollower(String followerId)
-
followerLogSize
public int followerLogSize()
-
-