BUG-5626: handle ElectionTimeout message first 33/37033/7
authorRobert Varga <rovarga@cisco.com>
Sat, 2 Apr 2016 13:19:22 +0000 (15:19 +0200)
committerTony Tkacik <ttkacik@cisco.com>
Mon, 11 Apr 2016 14:15:16 +0000 (14:15 +0000)
ElectionTimeout is a singleton, it is not a RaftRPC, hence its reception leads
to bail from handleMessage(). Move it to the front of the Candidate/Follower methods
so we can simplify the checks performed in those methods.

Change-Id: I3dfef4c42e2997d490de776de1c42e61cfacf217
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Candidate.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Follower.java

index a5def02..4d51922 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);
     }
 
index 2541dfa..2480a0d 100644 (file)
@@ -347,9 +347,16 @@ public class Follower extends AbstractRaftActorBehavior {
 
     @Override
     public RaftActorBehavior handleMessage(ActorRef sender, Object originalMessage) {
+        if (originalMessage instanceof ElectionTimeout) {
+            if (canStartElection()) {
+                LOG.debug("{}: Received ElectionTimeout - switching to Candidate", logName());
+                return internalSwitchBehavior(RaftState.Candidate);
+            } else {
+                return this;
+            }
+        }
 
-        Object message = fromSerializableMessage(originalMessage);
-
+        final Object message = fromSerializableMessage(originalMessage);
         if (message instanceof RaftRPC) {
             RaftRPC rpc = (RaftRPC) message;
             // If RPC request or response contains term T > currentTerm:
@@ -363,20 +370,12 @@ public class Follower extends AbstractRaftActorBehavior {
             }
         }
 
-        if (message instanceof ElectionTimeout) {
-            if(canStartElection()) {
-                LOG.debug("{}: Received ElectionTimeout - switching to Candidate", logName());
-                return internalSwitchBehavior(RaftState.Candidate);
-            } else {
-                return this;
-            }
-
-        } else if (message instanceof InstallSnapshot) {
+        if (message instanceof InstallSnapshot) {
             InstallSnapshot installSnapshot = (InstallSnapshot) message;
             handleInstallSnapshot(sender, installSnapshot);
         }
 
-        if(message instanceof RaftRPC && (!(message instanceof RequestVote) || (canGrantVote((RequestVote) message)))){
+        if (message instanceof RaftRPC && (!(message instanceof RequestVote) || (canGrantVote((RequestVote) message)))){
             scheduleElection(electionDuration());
         }