Add more debug output in AbstractLeader and Follower
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / Leader.java
index 6bbb70ce6b71ebb2ed7077b4962235fbf13f5cdf..c76798f3b276c602c3a0190f8749f3e1aca4de89 100644 (file)
@@ -91,12 +91,25 @@ public class Leader extends AbstractLeader {
         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) {
-        if(!context.hasFollowers()) {
-            leadershipTransferCohort.transferComplete();
-            return;
-        }
-
         LOG.debug("{}: Attempting to transfer leadership", logName());
 
         leadershipTransferContext = new LeadershipTransferContext(leadershipTransferCohort);
@@ -116,15 +129,16 @@ public class Leader extends AbstractLeader {
         }
 
         long lastIndex = context.getReplicatedLog().lastIndex();
+        boolean isVoting = context.getPeerInfo(followerId).isVoting();
 
-        LOG.debug("{}: tryToCompleteLeadershipTransfer: followerId: {}, matchIndex: {}, lastIndex: {}",
-                logName(), followerId, followerInfo.getMatchIndex(), lastIndex);
+        LOG.debug("{}: tryToCompleteLeadershipTransfer: followerId: {}, matchIndex: {}, lastIndex: {}, isVoting: {}",
+                logName(), followerId, followerInfo.getMatchIndex(), lastIndex, isVoting);
 
-        if(followerInfo.getMatchIndex() == lastIndex) {
+        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.
+            // additional AppendEntries with the latest commit index.
             sendAppendEntries(0, false);
 
             // Now send an ElectionTimeout to the matching follower to immediately start an election.