Simplify isolated leader check
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / RaftActorContextImpl.java
index d9cfbcdd11a24db86a11fccf79fb023101a7becc..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();
     }
 
@@ -224,8 +235,13 @@ public class RaftActorContextImpl implements RaftActorContext {
         peerInfoMap.put(id, new PeerInfo(id, address, votingState));
     }
 
-    @Override public void removePeer(String name) {
-        peerInfoMap.remove(name);
+    @Override
+    public void removePeer(String name) {
+        if(getId().equals(name)) {
+            votingMember = false;
+        } else {
+            peerInfoMap.remove(name);
+        }
     }
 
     @Override public ActorSelection getPeerActorSelection(String peerId) {
@@ -290,7 +306,7 @@ public class RaftActorContextImpl implements RaftActorContext {
     }
 
     @Override
-    public ServerConfigurationPayload getPeerServerInfo() {
+    public ServerConfigurationPayload getPeerServerInfo(boolean includeSelf) {
         if (!isDynamicServerConfigurationInUse()) {
             return null;
         }
@@ -300,7 +316,10 @@ public class RaftActorContextImpl implements RaftActorContext {
             newConfig.add(new ServerInfo(peer.getId(), peer.isVoting()));
         }
 
-        newConfig.add(new ServerInfo(getId(), true));
+        if(includeSelf) {
+            newConfig.add(new ServerInfo(getId(), votingMember));
+        }
+
         return (new ServerConfigurationPayload(newConfig));
     }
 
@@ -308,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());
+            }
+        }
+    }
 }