BUG 4212 : Follower should not reschedule election timeout in certain cases.
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / RaftActor.java
index f8bbf638a07f37c29c3aef64a4d51a963fe3b7f0..ebc157bc1729aff08199b5d6fe4a17b04afdc552 100644 (file)
@@ -209,6 +209,16 @@ public abstract class RaftActor extends AbstractUntypedPersistentActor {
             applyState(applyState.getClientActor(), applyState.getIdentifier(),
                 applyState.getReplicatedLogEntry().getData());
 
+            if (!hasFollowers()) {
+                // for single node, the capture should happen after the apply state
+                // as we delete messages from the persistent journal which have made it to the snapshot
+                // capturing the snapshot before applying makes the persistent journal and snapshot out of sync
+                // and recovery shows data missing
+                context.getReplicatedLog().captureSnapshotIfReady(applyState.getReplicatedLogEntry());
+
+                context.getSnapshotManager().trimLog(context.getLastApplied(), currentBehavior);
+            }
+
         } else if (message instanceof ApplyJournalEntries){
             ApplyJournalEntries applyEntries = (ApplyJournalEntries) message;
             if(LOG.isDebugEnabled()) {
@@ -293,9 +303,11 @@ public abstract class RaftActor extends AbstractUntypedPersistentActor {
 
         // it can happen that the state has not changed but the leader has changed.
         Optional<ActorRef> roleChangeNotifier = getRoleChangeNotifier();
-        if(!Objects.equal(oldBehaviorLeaderId, currentBehavior.getLeaderId())) {
+        if(!Objects.equal(oldBehaviorLeaderId, currentBehavior.getLeaderId()) ||
+           oldBehaviorState.getLeaderPayloadVersion() != currentBehavior.getLeaderPayloadVersion()) {
             if(roleChangeNotifier.isPresent()) {
-                roleChangeNotifier.get().tell(newLeaderStateChanged(getId(), currentBehavior.getLeaderId()), getSelf());
+                roleChangeNotifier.get().tell(newLeaderStateChanged(getId(), currentBehavior.getLeaderId(),
+                        currentBehavior.getLeaderPayloadVersion()), getSelf());
             }
 
             onLeaderChanged(oldBehaviorLeaderId, currentBehavior.getLeaderId());
@@ -308,8 +320,18 @@ public abstract class RaftActor extends AbstractUntypedPersistentActor {
         }
     }
 
-    protected LeaderStateChanged newLeaderStateChanged(String memberId, String leaderId) {
-        return new LeaderStateChanged(memberId, leaderId);
+    protected LeaderStateChanged newLeaderStateChanged(String memberId, String leaderId, short leaderPayloadVersion) {
+        return new LeaderStateChanged(memberId, leaderId, leaderPayloadVersion);
+    }
+
+    @Override
+    public long snapshotSequenceNr() {
+        // When we do a snapshot capture, we also capture and save the sequence-number of the persistent journal,
+        // so that we can delete the persistent journal based on the saved sequence-number
+        // However , when akka replays the journal during recovery, it replays it from the sequence number when the snapshot
+        // was saved and not the number we saved.
+        // We would want to override it , by asking akka to use the last-sequence number known to us.
+        return context.getSnapshotManager().getLastSequenceNumber();
     }
 
     /**
@@ -336,21 +358,21 @@ public abstract class RaftActor extends AbstractUntypedPersistentActor {
         replicatedLog().appendAndPersist(replicatedLogEntry, new Procedure<ReplicatedLogEntry>() {
             @Override
             public void apply(ReplicatedLogEntry replicatedLogEntry) throws Exception {
-                if(!hasFollowers()){
+                if (!hasFollowers()){
                     // Increment the Commit Index and the Last Applied values
                     raftContext.setCommitIndex(replicatedLogEntry.getIndex());
                     raftContext.setLastApplied(replicatedLogEntry.getIndex());
 
-                    // Apply the state immediately
+                    // Apply the state immediately.
                     self().tell(new ApplyState(clientActor, identifier, replicatedLogEntry), self());
 
                     // Send a ApplyJournalEntries message so that we write the fact that we applied
                     // the state to durable storage
                     self().tell(new ApplyJournalEntries(replicatedLogEntry.getIndex()), self());
 
-                    context.getSnapshotManager().trimLog(context.getLastApplied(), currentBehavior);
-
                 } else if (clientActor != null) {
+                    context.getReplicatedLog().captureSnapshotIfReady(replicatedLogEntry);
+
                     // Send message for replication
                     currentBehavior.handleMessage(getSelf(),
                             new Replicate(clientActor, identifier, replicatedLogEntry));
@@ -613,10 +635,12 @@ public abstract class RaftActor extends AbstractUntypedPersistentActor {
     private static class BehaviorStateHolder {
         private RaftActorBehavior behavior;
         private String leaderId;
+        private short leaderPayloadVersion;
 
         void init(RaftActorBehavior behavior) {
             this.behavior = behavior;
             this.leaderId = behavior != null ? behavior.getLeaderId() : null;
+            this.leaderPayloadVersion = behavior != null ? behavior.getLeaderPayloadVersion() : -1;
         }
 
         RaftActorBehavior getBehavior() {
@@ -626,5 +650,9 @@ public abstract class RaftActor extends AbstractUntypedPersistentActor {
         String getLeaderId() {
             return leaderId;
         }
+
+        short getLeaderPayloadVersion() {
+            return leaderPayloadVersion;
+        }
     }
 }