BUG-5626: handle ElectionTimeout message first
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / Candidate.java
index a5def02b1f5ad3301edfdf7bca2669fc8ab2256e..4d51922bc22f7c2bc63886989e354cfb0e912288 100644 (file)
@@ -123,9 +123,25 @@ public class Candidate extends AbstractRaftActorBehavior {
 
     @Override
     public RaftActorBehavior handleMessage(ActorRef sender, Object originalMessage) {
+        if (originalMessage instanceof ElectionTimeout) {
+            LOG.debug("{}: Received ElectionTimeout", logName());
+
+            if (votesRequired == 0) {
+                // If there are no peers then we should be a Leader
+                // We wait for the election timeout to occur before declare
+                // ourselves the leader. This gives enough time for a leader
+                // who we do not know about (as a peer)
+                // to send a message to the candidate
 
-        Object message = fromSerializableMessage(originalMessage);
+                return internalSwitchBehavior(RaftState.Leader);
+            }
+
+            startNewTerm();
+            scheduleElection(electionDuration());
+            return this;
+        }
 
+        final Object message = fromSerializableMessage(originalMessage);
         if (message instanceof RaftRPC) {
 
             RaftRPC rpc = (RaftRPC) message;
@@ -145,23 +161,6 @@ public class Candidate extends AbstractRaftActorBehavior {
             }
         }
 
-        if (message instanceof ElectionTimeout) {
-            LOG.debug("{}: Received ElectionTimeout", logName());
-
-            if (votesRequired == 0) {
-                // If there are no peers then we should be a Leader
-                // We wait for the election timeout to occur before declare
-                // ourselves the leader. This gives enough time for a leader
-                // who we do not know about (as a peer)
-                // to send a message to the candidate
-
-                return internalSwitchBehavior(RaftState.Leader);
-            }
-            startNewTerm();
-            scheduleElection(electionDuration());
-            return this;
-        }
-
         return super.handleMessage(sender, message);
     }