Move null check from getTotalMemory()
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / RaftActorRecoverySupport.java
index 57603a505833af58b99d2916eaa5ed2a09a065c0..ae7fdeadbc12a657c6e6c33189a1b7e36c16d687 100644 (file)
@@ -9,13 +9,17 @@ package org.opendaylight.controller.cluster.raft;
 
 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.raft.RaftActor.UpdateElectionTerm;
+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.behaviors.RaftActorBehavior;
+import org.opendaylight.controller.cluster.raft.base.messages.UpdateElectionTerm;
 import org.slf4j.Logger;
 
 /**
@@ -24,28 +28,38 @@ import org.slf4j.Logger;
  * @author Thomas Pantelis
  */
 class RaftActorRecoverySupport {
-    private final DataPersistenceProvider persistence;
     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(DataPersistenceProvider persistence, RaftActorContext context,
-            RaftActorBehavior currentBehavior, RaftActorRecoveryCohort cohort) {
-        this.persistence = persistence;
+    RaftActorRecoverySupport(final RaftActorContext context, final RaftActorRecoveryCohort cohort) {
         this.context = context;
-        this.currentBehavior = currentBehavior;
         this.cohort = cohort;
         this.log = context.getLogger();
     }
 
-    boolean handleRecoveryMessage(Object message) {
+    boolean handleRecoveryMessage(Object message, PersistentDataProvider persistentProvider) {
+        log.trace("{}: handleRecoveryMessage: {}", context.getId(), message);
+
+        anyDataRecovered = anyDataRecovered || !(message instanceof RecoveryCompleted);
+
         boolean recoveryComplete = false;
-        if(persistence.isRecoveryApplicable()) {
+        DataPersistenceProvider persistence = context.getPersistenceProvider();
+        if (message instanceof org.opendaylight.controller.cluster.raft.RaftActor.UpdateElectionTerm) {
+            // Handle this message for backwards compatibility with pre-Lithium versions.
+            org.opendaylight.controller.cluster.raft.RaftActor.UpdateElectionTerm update =
+                    (org.opendaylight.controller.cluster.raft.RaftActor.UpdateElectionTerm)message;
+            context.getTermInformation().update(update.getCurrentTerm(), update.getVotedFor());
+        } else if (message instanceof UpdateElectionTerm) {
+            context.getTermInformation().update(((UpdateElectionTerm) message).getCurrentTerm(),
+                    ((UpdateElectionTerm) message).getVotedFor());
+        } else if(persistence.isRecoveryApplicable()) {
             if (message instanceof SnapshotOffer) {
                 onRecoveredSnapshot((SnapshotOffer) message);
             } else if (message instanceof ReplicatedLogEntry) {
@@ -60,20 +74,72 @@ class RaftActorRecoverySupport {
             } else if (message instanceof org.opendaylight.controller.cluster.raft.RaftActor.DeleteEntries) {
                 // Handle this message for backwards compatibility with pre-Lithium versions.
                 replicatedLog().removeFrom(((org.opendaylight.controller.cluster.raft.RaftActor.DeleteEntries) message).getFromIndex());
-            } else if (message instanceof UpdateElectionTerm) {
-                context.getTermInformation().update(((UpdateElectionTerm) message).getCurrentTerm(),
-                        ((UpdateElectionTerm) message).getVotedFor());
             } else if (message instanceof RecoveryCompleted) {
                 onRecoveryCompletedMessage();
+                possiblyRestoreFromSnapshot();
                 recoveryComplete = true;
             }
         } else if (message instanceof RecoveryCompleted) {
             recoveryComplete = true;
+
+            if(dataRecoveredWithPersistenceDisabled) {
+                // Data persistence is disabled but we recovered some data entries so we must have just
+                // transitioned to disabled or a persistence backup was restored. Either way, delete all the
+                // messages from the akka journal for efficiency and so that we do not end up with consistency
+                // issues in case persistence is -re-enabled.
+                persistentProvider.deleteMessages(persistentProvider.getLastSequenceNumber());
+
+                // Delete all the akka snapshots as they will not be needed
+                persistentProvider.deleteSnapshots(new SnapshotSelectionCriteria(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());
+            }
+
+            possiblyRestoreFromSnapshot();
+        } else {
+            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();
     }
@@ -97,15 +163,20 @@ 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, persistence, currentBehavior));
+        context.setReplicatedLog(ReplicatedLogImpl.newInstance(snapshot, context));
         context.setLastApplied(snapshot.getLastAppliedIndex());
         context.setCommitIndex(snapshot.getLastAppliedIndex());
+        context.getTermInformation().update(snapshot.getElectionTerm(), snapshot.getElectionVotedFor());
 
         Stopwatch timer = Stopwatch.createStarted();
 
         // 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(),
@@ -118,6 +189,9 @@ class RaftActorRecoverySupport {
                     logEntry.getIndex(), logEntry.size());
         }
 
+        if(isServerConfigurationPayload(logEntry)){
+            context.updatePeerIds((ServerConfigurationPayload)logEntry.getData());
+        }
         replicatedLog().append(logEntry);
     }
 
@@ -125,6 +199,8 @@ class RaftActorRecoverySupport {
         long lastUnappliedIndex = context.getLastApplied() + 1;
 
         if(log.isDebugEnabled()) {
+            // it can happen that lastUnappliedIndex > toIndex, if the AJE is in the persistent journal
+            // but the entry itself has made it to that state and recovered via the snapshot
             log.debug("{}: Received apply journal entries for recovery, applying to state: {} to {}",
                     context.getId(), lastUnappliedIndex, toIndex);
         }
@@ -137,7 +213,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;
             }
         }
@@ -150,14 +226,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();
+            }
         }
     }
 
@@ -184,4 +262,8 @@ class RaftActorRecoverySupport {
                  "journal-size = {}", replicatedLog().lastIndex(), replicatedLog().getSnapshotIndex(),
                  replicatedLog().getSnapshotTerm(), replicatedLog().size());
     }
+
+    private static boolean isServerConfigurationPayload(ReplicatedLogEntry repLogEntry){
+        return (repLogEntry.getData() instanceof ServerConfigurationPayload);
+    }
 }