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 / Follower.java
index 321b2dd86410d6cbac8fccbe0bde0b12b606c4ef..5952dc087f8889e8607edf75dc2be77ee98b4424 100644 (file)
@@ -45,12 +45,17 @@ public class Follower extends AbstractRaftActorBehavior {
     private static final int SYNC_THRESHOLD = 10;
 
     public Follower(RaftActorContext context) {
+        this(context, null);
+    }
+
+    public Follower(RaftActorContext context, String initialLeaderId) {
         super(context, RaftState.Follower);
+        leaderId = initialLeaderId;
 
         initialSyncStatusTracker = new SyncStatusTracker(context.getActor(), getId(), SYNC_THRESHOLD);
 
-        if(context.getRaftPolicy().automaticElectionsEnabled()) {
-            if (context.getPeerIds().isEmpty()) {
+        if(canStartElection()) {
+            if (context.getPeerIds().isEmpty() && getLeaderId() == null) {
                 actor().tell(ELECTION_TIMEOUT, actor());
             } else {
                 scheduleElection(electionDuration());
@@ -325,8 +330,12 @@ public class Follower extends AbstractRaftActorBehavior {
         }
 
         if (message instanceof ElectionTimeout) {
-            LOG.debug("{}: Received ElectionTimeout - switching to Candidate", logName());
-            return internalSwitchBehavior(RaftState.Candidate);
+            if(canStartElection()) {
+                LOG.debug("{}: Received ElectionTimeout - switching to Candidate", logName());
+                return internalSwitchBehavior(RaftState.Candidate);
+            } else {
+                return this;
+            }
 
         } else if (message instanceof InstallSnapshot) {
             InstallSnapshot installSnapshot = (InstallSnapshot) message;
@@ -346,6 +355,8 @@ public class Follower extends AbstractRaftActorBehavior {
                     logName(), installSnapshot.getLeaderId(), installSnapshot.getData().size(),
                     installSnapshot.getChunkIndex(), installSnapshot.getTotalChunks());
 
+        leaderId = installSnapshot.getLeaderId();
+
         if(snapshotTracker == null){
             snapshotTracker = new SnapshotTracker(LOG, installSnapshot.getTotalChunks());
         }
@@ -365,7 +376,8 @@ public class Follower extends AbstractRaftActorBehavior {
                         installSnapshot.getLastIncludedIndex(),
                         installSnapshot.getLastIncludedTerm(),
                         context.getTermInformation().getCurrentTerm(),
-                        context.getTermInformation().getVotedFor());
+                        context.getTermInformation().getVotedFor(),
+                        context.getPeerServerInfo());
 
                 ApplySnapshot.Callback applySnapshotCallback = new ApplySnapshot.Callback() {
                     @Override