Bug 6587: Retain state when transitioning between Leader and IsolatedLeader
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / Leader.java
index c76798f3b276c602c3a0190f8749f3e1aca4de89..827364c29faeefd03149b5b65777195ca3afa5d6 100644 (file)
@@ -19,8 +19,7 @@ import org.opendaylight.controller.cluster.raft.FollowerLogInformation;
 import org.opendaylight.controller.cluster.raft.RaftActorContext;
 import org.opendaylight.controller.cluster.raft.RaftActorLeadershipTransferCohort;
 import org.opendaylight.controller.cluster.raft.RaftState;
-import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
-import org.opendaylight.controller.cluster.raft.base.messages.IsolatedLeaderCheck;
+import org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow;
 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
 
 /**
@@ -46,28 +45,39 @@ import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
  * set commitIndex = N (§5.3, §5.4).
  */
 public class Leader extends AbstractLeader {
-    private static final IsolatedLeaderCheck ISOLATED_LEADER_CHECK = new IsolatedLeaderCheck();
-    private final Stopwatch isolatedLeaderCheck;
+    /**
+     * Internal message sent to periodically check if this leader has become isolated and should transition
+     * to {@link IsolatedLeader}.
+     */
+    @VisibleForTesting
+    static final Object ISOLATED_LEADER_CHECK = new Object();
+
+    private final Stopwatch isolatedLeaderCheck = Stopwatch.createStarted();
     private @Nullable LeadershipTransferContext leadershipTransferContext;
 
+    Leader(RaftActorContext context, @Nullable AbstractLeader initializeFromLeader) {
+        super(context, RaftState.Leader, initializeFromLeader);
+    }
+
     public Leader(RaftActorContext context) {
-        super(context);
-        isolatedLeaderCheck = Stopwatch.createStarted();
+        this(context, null);
     }
 
-    @Override public RaftActorBehavior handleMessage(ActorRef sender, Object originalMessage) {
+    @Override
+    public RaftActorBehavior handleMessage(ActorRef sender, Object originalMessage) {
         Preconditions.checkNotNull(sender, "sender should not be null");
 
-        if (originalMessage instanceof IsolatedLeaderCheck) {
+        if (ISOLATED_LEADER_CHECK.equals(originalMessage)) {
             if (isLeaderIsolated()) {
                 LOG.warn("{}: At least {} followers need to be active, Switching {} from Leader to IsolatedLeader",
-                        context.getId(), getMinIsolatedLeaderPeerCount(), leaderId);
-
-                return internalSwitchBehavior(RaftState.IsolatedLeader);
+                    context.getId(), getMinIsolatedLeaderPeerCount(), getLeaderId());
+                return internalSwitchBehavior(new IsolatedLeader(context, this));
+            } else {
+                return this;
             }
+        } else {
+            return super.handleMessage(sender, originalMessage);
         }
-
-        return super.handleMessage(sender, originalMessage);
     }
 
     @Override
@@ -141,9 +151,9 @@ public class Leader extends AbstractLeader {
             // additional AppendEntries with the latest commit index.
             sendAppendEntries(0, false);
 
-            // Now send an ElectionTimeout to the matching follower to immediately start an election.
+            // Now send a TimeoutNow message to the matching follower to immediately start an election.
             ActorSelection followerActor = context.getPeerActorSelection(followerId);
-            followerActor.tell(new ElectionTimeout(), context.getActor());
+            followerActor.tell(TimeoutNow.INSTANCE, context.getActor());
 
             LOG.debug("{}: Leader transfer complete", logName());
 
@@ -153,9 +163,11 @@ public class Leader extends AbstractLeader {
     }
 
     @Override
-    public void close() throws Exception {
+    public void close() {
         if(leadershipTransferContext != null) {
-            leadershipTransferContext.transferCohort.abortTransfer();
+            LeadershipTransferContext localLeadershipTransferContext = leadershipTransferContext;
+            leadershipTransferContext = null;
+            localLeadershipTransferContext.transferCohort.abortTransfer();
         }
 
         super.close();