BUG-5626: refactor BehaviorStateHolder
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / Leader.java
index 924c30f657b927528a81ac9997564418c8ec2fe5..6d55316586cf85c725bb8e39953ee64b4f08217c 100644 (file)
@@ -20,7 +20,6 @@ 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.messages.AppendEntriesReply;
 
 /**
@@ -46,25 +45,29 @@ 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;
 
     public Leader(RaftActorContext context) {
-        super(context);
-        isolatedLeaderCheck = Stopwatch.createStarted();
+        super(context, RaftState.Leader);
     }
 
-    @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 (isLeaderIsolated()) {
-                LOG.warn("{}: At least {} followers need to be active, Switching {} from Leader to IsolatedLeader",
-                        context.getId(), getMinIsolatedLeaderPeerCount(), leaderId);
+        if (ISOLATED_LEADER_CHECK.equals(originalMessage) && isLeaderIsolated()) {
+            LOG.warn("{}: At least {} followers need to be active, Switching {} from Leader to IsolatedLeader",
+                context.getId(), getMinIsolatedLeaderPeerCount(), leaderId);
 
-                return internalSwitchBehavior(RaftState.IsolatedLeader);
-            }
+            return internalSwitchBehavior(RaftState.IsolatedLeader);
         }
 
         return super.handleMessage(sender, originalMessage);
@@ -153,7 +156,7 @@ public class Leader extends AbstractLeader {
     }
 
     @Override
-    public void close() throws Exception {
+    public void close() {
         if(leadershipTransferContext != null) {
             leadershipTransferContext.transferCohort.abortTransfer();
         }