Move ServerConfigurationPayload to cluster.raft.persisted
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / RaftActorRecoverySupport.java
index caa853da2390af2a30802a90c22a63b26e68df31..9e449085713bf53cdda43f506d744a5d376ce582 100644 (file)
@@ -11,13 +11,15 @@ import akka.persistence.RecoveryCompleted;
 import akka.persistence.SnapshotOffer;
 import akka.persistence.SnapshotSelectionCriteria;
 import com.google.common.base.Stopwatch;
+import java.io.ByteArrayInputStream;
+import java.io.ObjectInputStream;
 import org.opendaylight.controller.cluster.DataPersistenceProvider;
 import org.opendaylight.controller.cluster.PersistentDataProvider;
 import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries;
-import org.opendaylight.controller.cluster.raft.base.messages.ApplyLogEntries;
+import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot;
 import org.opendaylight.controller.cluster.raft.base.messages.DeleteEntries;
 import org.opendaylight.controller.cluster.raft.base.messages.UpdateElectionTerm;
-import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
+import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
 import org.slf4j.Logger;
 
 /**
@@ -27,25 +29,25 @@ import org.slf4j.Logger;
  */
 class RaftActorRecoverySupport {
     private final RaftActorContext context;
-    private final RaftActorBehavior currentBehavior;
     private final RaftActorRecoveryCohort cohort;
 
     private int currentRecoveryBatchCount;
     private boolean dataRecoveredWithPersistenceDisabled;
+    private boolean anyDataRecovered;
 
     private Stopwatch recoveryTimer;
     private final Logger log;
 
-    RaftActorRecoverySupport(RaftActorContext context, RaftActorBehavior currentBehavior,
-            RaftActorRecoveryCohort cohort) {
+    RaftActorRecoverySupport(final RaftActorContext context, final RaftActorRecoveryCohort cohort) {
         this.context = context;
-        this.currentBehavior = currentBehavior;
         this.cohort = cohort;
         this.log = context.getLogger();
     }
 
     boolean handleRecoveryMessage(Object message, PersistentDataProvider persistentProvider) {
-        log.trace("handleRecoveryMessage: {}", message);
+        log.trace("{}: handleRecoveryMessage: {}", context.getId(), message);
+
+        anyDataRecovered = anyDataRecovered || !(message instanceof RecoveryCompleted);
 
         boolean recoveryComplete = false;
         DataPersistenceProvider persistence = context.getPersistenceProvider();
@@ -62,9 +64,6 @@ class RaftActorRecoverySupport {
                 onRecoveredSnapshot((SnapshotOffer) message);
             } else if (message instanceof ReplicatedLogEntry) {
                 onRecoveredJournalLogEntry((ReplicatedLogEntry) message);
-            } else if (message instanceof ApplyLogEntries) {
-                // Handle this message for backwards compatibility with pre-Lithium versions.
-                onRecoveredApplyLogEntries(((ApplyLogEntries) message).getToIndex());
             } else if (message instanceof ApplyJournalEntries) {
                 onRecoveredApplyLogEntries(((ApplyJournalEntries) message).getToIndex());
             } else if (message instanceof DeleteEntries) {
@@ -74,6 +73,7 @@ class RaftActorRecoverySupport {
                 replicatedLog().removeFrom(((org.opendaylight.controller.cluster.raft.RaftActor.DeleteEntries) message).getFromIndex());
             } else if (message instanceof RecoveryCompleted) {
                 onRecoveryCompletedMessage();
+                possiblyRestoreFromSnapshot();
                 recoveryComplete = true;
             }
         } else if (message instanceof RecoveryCompleted) {
@@ -88,19 +88,56 @@ class RaftActorRecoverySupport {
 
                 // Delete all the akka snapshots as they will not be needed
                 persistentProvider.deleteSnapshots(new SnapshotSelectionCriteria(scala.Long.MaxValue(),
-                        scala.Long.MaxValue()));
+                        scala.Long.MaxValue(), 0L, 0L));
 
                 // Since we cleaned out the journal, we need to re-write the current election info.
                 context.getTermInformation().updateAndPersist(context.getTermInformation().getCurrentTerm(),
                         context.getTermInformation().getVotedFor());
             }
+
+            onRecoveryCompletedMessage();
+            possiblyRestoreFromSnapshot();
         } else {
-            dataRecoveredWithPersistenceDisabled = true;
+            boolean isServerConfigPayload = false;
+            if(message instanceof ReplicatedLogEntry){
+                ReplicatedLogEntry repLogEntry = (ReplicatedLogEntry)message;
+                if(isServerConfigurationPayload(repLogEntry)){
+                    isServerConfigPayload = true;
+                    context.updatePeerIds((ServerConfigurationPayload)repLogEntry.getData());
+                }
+            }
+
+            if(!isServerConfigPayload){
+                dataRecoveredWithPersistenceDisabled = true;
+            }
         }
 
         return recoveryComplete;
     }
 
+    private void possiblyRestoreFromSnapshot() {
+        byte[] restoreFromSnapshot = cohort.getRestoreFromSnapshot();
+        if(restoreFromSnapshot == null) {
+            return;
+        }
+
+        if(anyDataRecovered) {
+            log.warn("{}: The provided restore snapshot was not applied because the persistence store is not empty",
+                    context.getId());
+            return;
+        }
+
+        try(ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(restoreFromSnapshot))) {
+            Snapshot snapshot = (Snapshot) ois.readObject();
+
+            log.debug("{}: Deserialized restore snapshot: {}", context.getId(), snapshot);
+
+            context.getSnapshotManager().apply(new ApplySnapshot(snapshot));
+        } catch(Exception e) {
+            log.error("{}: Error deserializing snapshot restore", context.getId(), e);
+        }
+    }
+
     private ReplicatedLog replicatedLog() {
         return context.getReplicatedLog();
     }
@@ -124,7 +161,7 @@ class RaftActorRecoverySupport {
         // The replicated log can be used later on to retrieve this snapshot
         // when we need to install it on a peer
 
-        context.setReplicatedLog(ReplicatedLogImpl.newInstance(snapshot, context, currentBehavior));
+        context.setReplicatedLog(ReplicatedLogImpl.newInstance(snapshot, context));
         context.setLastApplied(snapshot.getLastAppliedIndex());
         context.setCommitIndex(snapshot.getLastAppliedIndex());
         context.getTermInformation().update(snapshot.getElectionTerm(), snapshot.getElectionVotedFor());
@@ -134,6 +171,10 @@ class RaftActorRecoverySupport {
         // Apply the snapshot to the actors state
         cohort.applyRecoverySnapshot(snapshot.getState());
 
+        if (snapshot.getServerConfiguration() != null) {
+            context.updatePeerIds(snapshot.getServerConfiguration());
+        }
+
         timer.stop();
         log.info("Recovery snapshot applied for {} in {}: snapshotIndex={}, snapshotTerm={}, journal-size={}",
                 context.getId(), timer.toString(), replicatedLog().getSnapshotIndex(),
@@ -146,6 +187,9 @@ class RaftActorRecoverySupport {
                     logEntry.getIndex(), logEntry.size());
         }
 
+        if(isServerConfigurationPayload(logEntry)){
+            context.updatePeerIds((ServerConfigurationPayload)logEntry.getData());
+        }
         replicatedLog().append(logEntry);
     }
 
@@ -167,7 +211,7 @@ class RaftActorRecoverySupport {
                 batchRecoveredLogEntry(logEntry);
             } else {
                 // Shouldn't happen but cover it anyway.
-                log.error("Log entry not found for index {}", i);
+                log.error("{}: Log entry not found for index {}", context.getId(), i);
                 break;
             }
         }
@@ -180,14 +224,16 @@ class RaftActorRecoverySupport {
         initRecoveryTimer();
 
         int batchSize = context.getConfigParams().getJournalRecoveryLogBatchSize();
-        if(currentRecoveryBatchCount == 0) {
-            cohort.startLogRecoveryBatch(batchSize);
-        }
+        if(!isServerConfigurationPayload(logEntry)){
+            if(currentRecoveryBatchCount == 0) {
+                cohort.startLogRecoveryBatch(batchSize);
+            }
 
-        cohort.appendRecoveredLogEntry(logEntry.getData());
+            cohort.appendRecoveredLogEntry(logEntry.getData());
 
-        if(++currentRecoveryBatchCount >= batchSize) {
-            endCurrentLogRecoveryBatch();
+            if(++currentRecoveryBatchCount >= batchSize) {
+                endCurrentLogRecoveryBatch();
+            }
         }
     }
 
@@ -214,4 +260,8 @@ class RaftActorRecoverySupport {
                  "journal-size = {}", replicatedLog().lastIndex(), replicatedLog().getSnapshotIndex(),
                  replicatedLog().getSnapshotTerm(), replicatedLog().size());
     }
+
+    private static boolean isServerConfigurationPayload(ReplicatedLogEntry repLogEntry){
+        return (repLogEntry.getData() instanceof ServerConfigurationPayload);
+    }
 }