Bug 2187: Prevent non-voting member from initiating elections
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / AbstractRaftActorBehavior.java
index 787bd74629b0c643400e49deb32a40468c52e50a..d17fba03d4ba58e77168602ffe388431b0a64a1a 100644 (file)
@@ -17,7 +17,6 @@ import org.opendaylight.controller.cluster.raft.RaftActorContext;
 import org.opendaylight.controller.cluster.raft.RaftState;
 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
 import org.opendaylight.controller.cluster.raft.SerializationUtils;
-import org.opendaylight.controller.cluster.raft.ServerConfigurationPayload;
 import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries;
 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
 import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
@@ -253,6 +252,10 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior {
         }
     }
 
+    protected boolean canStartElection() {
+        return context.getRaftPolicy().automaticElectionsEnabled() && context.isVotingMember();
+    }
+
     /**
      * schedule a new election
      *
@@ -261,12 +264,11 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior {
     protected void scheduleElection(FiniteDuration interval) {
         stopElection();
 
-        // Schedule an election. When the scheduler triggers an ElectionTimeout
-        // message is sent to itself
-        electionCancel =
-            context.getActorSystem().scheduler().scheduleOnce(interval,
-                context.getActor(), ELECTION_TIMEOUT,
-                context.getActorSystem().dispatcher(), context.getActor());
+        if(canStartElection()) {
+            // Schedule an election. When the scheduler triggers an ElectionTimeout message is sent to itself
+            electionCancel = context.getActorSystem().scheduler().scheduleOnce(interval, context.getActor(),
+                    ELECTION_TIMEOUT,context.getActorSystem().dispatcher(), context.getActor());
+        }
     }
 
     /**
@@ -490,16 +492,4 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior {
     protected String getId(){
         return context.getId();
     }
-
-    public void applyServerConfiguration(ServerConfigurationPayload serverConfig) {
-        for(String peerId: context.getPeerAddresses().keySet()) {
-            context.removePeer(peerId);
-        }
-
-        for(String peerId: serverConfig.getNewServerConfig()) {
-            if(!getId().equals(peerId)) {
-                context.addToPeers(peerId, null);
-            }
-        }
-    }
 }