Simplify isolated leader check
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / RaftActorContextImpl.java
index 4677ea946e65cf352779d027744e1c7e232bc572..8b28d5213fbbe45c51bb1a260f5fe324a9a9c40b 100644 (file)
@@ -14,6 +14,7 @@ import akka.actor.ActorSelection;
 import akka.actor.ActorSystem;
 import akka.actor.Props;
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
 import com.google.common.base.Supplier;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -24,6 +25,7 @@ import java.util.Map;
 import java.util.Set;
 import org.opendaylight.controller.cluster.DataPersistenceProvider;
 import org.opendaylight.controller.cluster.raft.ServerConfigurationPayload.ServerInfo;
+import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
 import org.slf4j.Logger;
 
@@ -64,6 +66,8 @@ public class RaftActorContextImpl implements RaftActorContext {
 
     private boolean votingMember = true;
 
+    private RaftActorBehavior currentBehavior;
+
     public RaftActorContextImpl(ActorRef actor, ActorContext context, String id,
             ElectionTerm termInformation, long commitIndex, long lastApplied, Map<String, String> peerAddresses,
             ConfigParams configParams, DataPersistenceProvider persistenceProvider, Logger logger) {
@@ -192,9 +196,11 @@ public class RaftActorContextImpl implements RaftActorContext {
     @Override
     public void updatePeerIds(ServerConfigurationPayload serverConfig){
         votingMember = true;
+        boolean foundSelf = false;
         Set<String> currentPeers = new HashSet<>(this.getPeerIds());
         for(ServerInfo server: serverConfig.getServerConfig()) {
             if(getId().equals(server.getId())) {
+                foundSelf = true;
                 if(!server.isVoting()) {
                     votingMember = false;
                 }
@@ -212,6 +218,11 @@ public class RaftActorContextImpl implements RaftActorContext {
         for(String peerIdToRemove: currentPeers) {
             this.removePeer(peerIdToRemove);
         }
+
+        if(!foundSelf) {
+            votingMember = false;
+        }
+
         setDynamicServerConfigurationInUse();
     }
 
@@ -316,4 +327,23 @@ public class RaftActorContextImpl implements RaftActorContext {
     public boolean isVotingMember() {
         return votingMember;
     }
+
+    @Override
+    public RaftActorBehavior getCurrentBehavior() {
+        return currentBehavior;
+    }
+
+    void setCurrentBehavior(final RaftActorBehavior behavior) {
+        this.currentBehavior = Preconditions.checkNotNull(behavior);
+    }
+
+    void close() {
+        if (currentBehavior != null) {
+            try {
+                currentBehavior.close();
+            } catch (Exception e) {
+                LOG.debug("{}: Error closing behavior {}", getId(), currentBehavior.state());
+            }
+        }
+    }
 }