+ if(leadershipTransferContext != null && leadershipTransferContext.isExpired(
+ context.getConfigParams().getElectionTimeOutInterval().toMillis())) {
+ LOG.debug("{}: Leadership transfer expired", logName());
+ leadershipTransferContext = null;
+ }
+ }
+
+ @Override
+ protected RaftActorBehavior handleAppendEntriesReply(ActorRef sender, AppendEntriesReply appendEntriesReply) {
+ RaftActorBehavior returnBehavior = super.handleAppendEntriesReply(sender, appendEntriesReply);
+ tryToCompleteLeadershipTransfer(appendEntriesReply.getFollowerId());
+ return returnBehavior;
+ }
+
+ /**
+ * Attempts to transfer leadership to a follower as per the raft paper (ยง3.10) as follows:
+ * <ul>
+ * <li>Start a timer (Stopwatch).</li>
+ * <li>Send an initial AppendEntries heartbeat to all followers.</li>
+ * <li>On AppendEntriesReply, check if the follower's new match Index matches the leader's last index</li>
+ * <li>If it matches, </li>
+ * <ul>
+ * <li>Send an additional AppendEntries to ensure the follower has applied all its log entries to its state.</li>
+ * <li>Send an ElectionTimeout to the follower to immediately start an election.</li>
+ * <li>Notify {@link RaftActorLeadershipTransferCohort#transferComplete}.</li>
+ * </ul>
+ * <li>Otherwise if the election time out period elapses, notify
+ * {@link RaftActorLeadershipTransferCohort#abortTtransfer}.</li>
+ * </ul>
+ *
+ * @param leadershipTransferCohort
+ */
+ public void transferLeadership(@Nonnull RaftActorLeadershipTransferCohort leadershipTransferCohort) {
+ LOG.debug("{}: Attempting to transfer leadership", logName());
+
+ leadershipTransferContext = new LeadershipTransferContext(leadershipTransferCohort);
+
+ // Send an immediate heart beat to the followers.
+ sendAppendEntries(0, false);
+ }
+
+ private void tryToCompleteLeadershipTransfer(String followerId) {
+ if(leadershipTransferContext == null) {
+ return;
+ }
+
+ FollowerLogInformation followerInfo = getFollower(followerId);
+ if(followerInfo == null) {
+ return;
+ }
+
+ long lastIndex = context.getReplicatedLog().lastIndex();
+ boolean isVoting = context.getPeerInfo(followerId).isVoting();
+
+ LOG.debug("{}: tryToCompleteLeadershipTransfer: followerId: {}, matchIndex: {}, lastIndex: {}, isVoting: {}",
+ logName(), followerId, followerInfo.getMatchIndex(), lastIndex, isVoting);
+
+ if(isVoting && followerInfo.getMatchIndex() == lastIndex) {
+ LOG.debug("{}: Follower's log matches - sending ElectionTimeout", logName());
+
+ // We can't be sure if the follower has applied all its log entries to its state so send an
+ // additional AppendEntries with the latest commit index.
+ sendAppendEntries(0, false);
+
+ // Now send an ElectionTimeout to the matching follower to immediately start an election.
+ ActorSelection followerActor = context.getPeerActorSelection(followerId);
+ followerActor.tell(ElectionTimeout.INSTANCE, context.getActor());
+
+ LOG.debug("{}: Leader transfer complete", logName());
+
+ leadershipTransferContext.transferCohort.transferComplete();
+ leadershipTransferContext = null;
+ }