Class Leader
- java.lang.Object
-
- org.opendaylight.controller.cluster.raft.behaviors.AbstractRaftActorBehavior
-
- org.opendaylight.controller.cluster.raft.behaviors.AbstractLeader
-
- org.opendaylight.controller.cluster.raft.behaviors.Leader
-
- All Implemented Interfaces:
AutoCloseable
,RaftActorBehavior
public class Leader extends AbstractLeader
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 Constructor Description Leader(RaftActorContext context)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
beforeSendHeartbeat()
void
close()
protected 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 originalMessage)
Handle a message.void
transferLeadership(@NonNull RaftActorLeadershipTransferCohort leadershipTransferCohort)
Attempts to transfer leadership to a follower as per the raft paper (§3.10) as follows: Start a timer (Stopwatch). Send an initial AppendEntries heartbeat to all followers. On AppendEntriesReply, check if the follower's new match Index matches the leader's last index If it matches, Send an additional AppendEntries to ensure the follower has applied all its log entries to its state. Send an ElectionTimeout to the follower to immediately start an election. NotifyRaftActorLeadershipTransferCohort.transferComplete()
. Otherwise if the election time out period elapses, notifyRaftActorLeadershipTransferCohort.abortTransfer()
.-
Methods inherited from class org.opendaylight.controller.cluster.raft.behaviors.AbstractLeader
addFollower, followerLogSize, getFollower, getFollowerIds, getLeaderId, getLeaderPayloadVersion, getMinIsolatedLeaderPeerCount, handleAppendEntries, handleRequestVoteReply, initiateCaptureSnapshot, isLeaderIsolated, printFollowerStates, removeFollower, sendAppendEntries, updateMinReplicaCount
-
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
-
Leader
public Leader(RaftActorContext context)
-
-
Method Detail
-
handleMessage
public RaftActorBehavior handleMessage(ActorRef sender, Object originalMessage)
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
- Overrides:
handleMessage
in classAbstractLeader
- Parameters:
sender
- The sender of the messageoriginalMessage
- A message that needs to be processed- Returns:
- The new behavior or current behavior, or null if the message was not handled.
-
beforeSendHeartbeat
protected void beforeSendHeartbeat()
- Overrides:
beforeSendHeartbeat
in classAbstractLeader
-
handleAppendEntriesReply
protected RaftActorBehavior handleAppendEntriesReply(ActorRef sender, AppendEntriesReply appendEntriesReply)
Description copied from class:AbstractRaftActorBehavior
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- Overrides:
handleAppendEntriesReply
in classAbstractLeader
- Parameters:
sender
- The actor that sent this messageappendEntriesReply
- The AppendEntriesReply message- Returns:
- a new behavior if it was changed or the current behavior
-
transferLeadership
public void transferLeadership(@NonNull RaftActorLeadershipTransferCohort leadershipTransferCohort)
Attempts to transfer leadership to a follower as per the raft paper (§3.10) as follows:- Start a timer (Stopwatch).
- Send an initial AppendEntries heartbeat to all followers.
- On AppendEntriesReply, check if the follower's new match Index matches the leader's last index
- If it matches,
- Send an additional AppendEntries to ensure the follower has applied all its log entries to its state.
- Send an ElectionTimeout to the follower to immediately start an election.
- Notify
RaftActorLeadershipTransferCohort.transferComplete()
.
- Otherwise if the election time out period elapses, notify
RaftActorLeadershipTransferCohort.abortTransfer()
.
- Parameters:
leadershipTransferCohort
- the cohort participating in the leadership transfer
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceRaftActorBehavior
- Overrides:
close
in classAbstractLeader
-
-