Simplify onShutdown checks 73/36873/1
authorRobert Varga <rovarga@cisco.com>
Wed, 30 Mar 2016 09:33:48 +0000 (11:33 +0200)
committerRobert Varga <rovarga@cisco.com>
Wed, 30 Mar 2016 09:36:09 +0000 (11:36 +0200)
We check for RaftState.Leader twice in an if/else chain. Check for non-Leader
first and bail early, leaving a simple if/else to handle the leader case.

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

index 945af8d47d0415200d41462cc15bb7877a113a2a..ff464b0c9ca6629a3c754c017e56eb7cfda6bd2f 100644 (file)
@@ -305,7 +305,13 @@ public abstract class RaftActor extends AbstractUntypedPersistentActor {
         shuttingDown = true;
 
         final RaftActorBehavior currentBehavior = context.getCurrentBehavior();
-        if(currentBehavior.state() == RaftState.Leader && context.hasFollowers()) {
+        if (currentBehavior.state() != RaftState.Leader) {
+            // For non-leaders shutdown is a no-op
+            self().tell(PoisonPill.getInstance(), self());
+            return;
+        }
+
+        if (context.hasFollowers()) {
             initiateLeadershipTransfer(new RaftActorLeadershipTransferCohort.OnComplete() {
                 @Override
                 public void onSuccess(ActorRef raftActorRef, ActorRef replyTo) {
@@ -319,7 +325,7 @@ public abstract class RaftActor extends AbstractUntypedPersistentActor {
                     raftActorRef.tell(PoisonPill.getInstance(), raftActorRef);
                 }
             });
-        } else if(currentBehavior.state() == RaftState.Leader) {
+        } else {
             pauseLeader(new TimedRunnable(context.getConfigParams().getElectionTimeOutInterval(), this) {
                 @Override
                 protected void doRun() {
@@ -331,8 +337,6 @@ public abstract class RaftActor extends AbstractUntypedPersistentActor {
                     self().tell(PoisonPill.getInstance(), self());
                 }
             });
-        } else {
-            self().tell(PoisonPill.getInstance(), self());
         }
     }