Bug 3570: Persist snapshot on follower ApplySnapshot
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / RaftActorSnapshotMessageSupport.java
index cab76e90ce69756f084668bd3f695c12a9a71012..2db595d743b727fdde09a66d57f8791c0545ac2f 100644 (file)
@@ -30,11 +30,18 @@ class RaftActorSnapshotMessageSupport {
 
     private final Procedure<Void> createSnapshotProcedure = new Procedure<Void>() {
         @Override
-        public void apply(Void notUsed) throws Exception {
+        public void apply(Void notUsed) {
             cohort.createSnapshot(context.getActor());
         }
     };
 
+    private final Procedure<byte[]> applySnapshotProcedure = new Procedure<byte[]>() {
+        @Override
+        public void apply(byte[] state) {
+            cohort.applySnapshot(state);
+        }
+    };
+
     RaftActorSnapshotMessageSupport(RaftActorContext context, RaftActorBehavior currentBehavior,
             RaftActorSnapshotCohort cohort) {
         this.context = context;
@@ -43,6 +50,7 @@ class RaftActorSnapshotMessageSupport {
         this.log = context.getLogger();
 
         context.getSnapshotManager().setCreateSnapshotCallable(createSnapshotProcedure);
+        context.getSnapshotManager().setApplySnapshotProcedure(applySnapshotProcedure);
     }
 
     boolean handleSnapshotMessage(Object message) {
@@ -59,7 +67,7 @@ class RaftActorSnapshotMessageSupport {
             onCaptureSnapshotReply(((CaptureSnapshotReply) message).getSnapshot());
             return true;
         } else if (message.equals(COMMIT_SNAPSHOT)) {
-            context.getSnapshotManager().commit(-1);
+            context.getSnapshotManager().commit(-1, currentBehavior);
             return true;
         } else {
             return false;
@@ -84,20 +92,13 @@ class RaftActorSnapshotMessageSupport {
 
         long sequenceNumber = success.metadata().sequenceNr();
 
-        context.getSnapshotManager().commit(sequenceNumber);
+        context.getSnapshotManager().commit(sequenceNumber, currentBehavior);
     }
 
     private void onApplySnapshot(Snapshot snapshot) {
-        if(log.isDebugEnabled()) {
-            log.debug("{}: ApplySnapshot called on Follower Actor " +
-                    "snapshotIndex:{}, snapshotTerm:{}", context.getId(), snapshot.getLastAppliedIndex(),
-                snapshot.getLastAppliedTerm());
-        }
-
-        cohort.applySnapshot(snapshot.getState());
+        log.info("{}: Applying snapshot on follower with snapshotIndex: {}, snapshotTerm: {}", context.getId(),
+                snapshot.getLastAppliedIndex(), snapshot.getLastAppliedTerm());
 
-        //clears the followers log, sets the snapshot index to ensure adjusted-index works
-        context.setReplicatedLog(ReplicatedLogImpl.newInstance(snapshot, context, currentBehavior));
-        context.setLastApplied(snapshot.getLastAppliedIndex());
+        context.getSnapshotManager().apply(snapshot);
     }
 }