X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FRaftActorRecoverySupport.java;h=283aa8f142087c2421f58d7a116351652c58575f;hp=15d98b5289d1db98050e1f956dbf8a4859830d4e;hb=refs%2Fchanges%2F14%2F82314%2F25;hpb=c42a5e91e0dcfc499b33a321ef45c0d310d366cc diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorRecoverySupport.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorRecoverySupport.java index 15d98b5289..283aa8f142 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorRecoverySupport.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorRecoverySupport.java @@ -10,18 +10,20 @@ package org.opendaylight.controller.cluster.raft; import akka.persistence.RecoveryCompleted; import akka.persistence.SnapshotOffer; import com.google.common.base.Stopwatch; -import java.io.ByteArrayInputStream; -import java.io.ObjectInputStream; import java.util.Collections; import org.opendaylight.controller.cluster.PersistentDataProvider; import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot; import org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries; import org.opendaylight.controller.cluster.raft.persisted.DeleteEntries; +import org.opendaylight.controller.cluster.raft.persisted.EmptyState; import org.opendaylight.controller.cluster.raft.persisted.MigratedSerializable; import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload; +import org.opendaylight.controller.cluster.raft.persisted.Snapshot; +import org.opendaylight.controller.cluster.raft.persisted.Snapshot.State; import org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm; import org.opendaylight.controller.cluster.raft.protobuff.client.messages.PersistentPayload; import org.slf4j.Logger; + /** * Support class that handles persistence recovery for a RaftActor. * @@ -45,12 +47,12 @@ class RaftActorRecoverySupport { this.log = context.getLogger(); } - boolean handleRecoveryMessage(Object message, PersistentDataProvider persistentProvider) { + boolean handleRecoveryMessage(final Object message, final PersistentDataProvider persistentProvider) { log.trace("{}: handleRecoveryMessage: {}", context.getId(), message); anyDataRecovered = anyDataRecovered || !(message instanceof RecoveryCompleted); - if(isMigratedSerializable(message)) { + if (isMigratedSerializable(message)) { hasMigratedDataRecovered = true; } @@ -76,27 +78,22 @@ class RaftActorRecoverySupport { return recoveryComplete; } + @SuppressWarnings("checkstyle:IllegalCatch") private void possiblyRestoreFromSnapshot() { - byte[] restoreFromSnapshot = cohort.getRestoreFromSnapshot(); - if(restoreFromSnapshot == null) { + Snapshot restoreFromSnapshot = cohort.getRestoreFromSnapshot(); + if (restoreFromSnapshot == null) { return; } - if(anyDataRecovered) { + 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); + log.debug("{}: Restore snapshot: {}", context.getId(), restoreFromSnapshot); - context.getSnapshotManager().apply(new ApplySnapshot(snapshot)); - } catch(Exception e) { - log.error("{}: Error deserializing snapshot restore", context.getId(), e); - } + context.getSnapshotManager().apply(new ApplySnapshot(restoreFromSnapshot)); } private ReplicatedLog replicatedLog() { @@ -104,31 +101,30 @@ class RaftActorRecoverySupport { } private void initRecoveryTimer() { - if(recoveryTimer == null) { + if (recoveryTimer == null) { recoveryTimer = Stopwatch.createStarted(); } } - private void onRecoveredSnapshot(SnapshotOffer offer) { - if(log.isDebugEnabled()) { - log.debug("{}: SnapshotOffer called..", context.getId()); - } + private void onRecoveredSnapshot(final SnapshotOffer offer) { + log.debug("{}: SnapshotOffer called.", context.getId()); initRecoveryTimer(); Snapshot snapshot = (Snapshot) offer.snapshot(); - for(ReplicatedLogEntry entry: snapshot.getUnAppliedEntries()) { - if(isMigratedPayload(entry)) { + for (ReplicatedLogEntry entry: snapshot.getUnAppliedEntries()) { + if (isMigratedPayload(entry)) { hasMigratedDataRecovered = true; } } - if(!context.getPersistenceProvider().isRecoveryApplicable()) { + if (!context.getPersistenceProvider().isRecoveryApplicable()) { // We may have just transitioned to disabled and have a snapshot containing state data and/or log // entries - we don't want to preserve these, only the server config and election term info. - snapshot = Snapshot.create(new byte[0], Collections.emptyList(), -1, -1, -1, -1, + snapshot = Snapshot.create( + EmptyState.INSTANCE, Collections.emptyList(), -1, -1, -1, -1, snapshot.getElectionTerm(), snapshot.getElectionVotedFor(), snapshot.getServerConfiguration()); } @@ -141,55 +137,57 @@ class RaftActorRecoverySupport { context.setCommitIndex(snapshot.getLastAppliedIndex()); context.getTermInformation().update(snapshot.getElectionTerm(), snapshot.getElectionVotedFor()); - Stopwatch timer = Stopwatch.createStarted(); + final Stopwatch timer = Stopwatch.createStarted(); // Apply the snapshot to the actors state - cohort.applyRecoverySnapshot(snapshot.getState()); + final State snapshotState = snapshot.getState(); + if (snapshotState.needsMigration()) { + hasMigratedDataRecovered = true; + } + if (!(snapshotState instanceof EmptyState)) { + cohort.applyRecoverySnapshot(snapshotState); + } if (snapshot.getServerConfiguration() != null) { context.updatePeerIds(snapshot.getServerConfiguration()); - - if(isMigratedSerializable(snapshot.getServerConfiguration())) { - hasMigratedDataRecovered = true; - } } timer.stop(); log.info("Recovery snapshot applied for {} in {}: snapshotIndex={}, snapshotTerm={}, journal-size={}", - context.getId(), timer.toString(), replicatedLog().getSnapshotIndex(), - replicatedLog().getSnapshotTerm(), replicatedLog().size()); + context.getId(), timer, replicatedLog().getSnapshotIndex(), replicatedLog().getSnapshotTerm(), + replicatedLog().size()); } - private void onRecoveredJournalLogEntry(ReplicatedLogEntry logEntry) { - if(log.isDebugEnabled()) { + private void onRecoveredJournalLogEntry(final ReplicatedLogEntry logEntry) { + if (log.isDebugEnabled()) { log.debug("{}: Received ReplicatedLogEntry for recovery: index: {}, size: {}", context.getId(), logEntry.getIndex(), logEntry.size()); } - if(isServerConfigurationPayload(logEntry)){ + if (isServerConfigurationPayload(logEntry)) { context.updatePeerIds((ServerConfigurationPayload)logEntry.getData()); } - if(isMigratedPayload(logEntry)) { + if (isMigratedPayload(logEntry)) { hasMigratedDataRecovered = true; } - if(context.getPersistenceProvider().isRecoveryApplicable()) { + if (context.getPersistenceProvider().isRecoveryApplicable()) { replicatedLog().append(logEntry); - } else if(!isPersistentPayload(logEntry)) { + } else if (!isPersistentPayload(logEntry)) { dataRecoveredWithPersistenceDisabled = true; } } - private void onRecoveredApplyLogEntries(long toIndex) { - if(!context.getPersistenceProvider().isRecoveryApplicable()) { + private void onRecoveredApplyLogEntries(final long toIndex) { + if (!context.getPersistenceProvider().isRecoveryApplicable()) { dataRecoveredWithPersistenceDisabled = true; return; } long lastUnappliedIndex = context.getLastApplied() + 1; - if(log.isDebugEnabled()) { + 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 {}", @@ -199,7 +197,7 @@ class RaftActorRecoverySupport { long lastApplied = lastUnappliedIndex - 1; for (long i = lastUnappliedIndex; i <= toIndex; i++) { ReplicatedLogEntry logEntry = replicatedLog().get(i); - if(logEntry != null) { + if (logEntry != null) { lastApplied++; batchRecoveredLogEntry(logEntry); } else { @@ -213,26 +211,26 @@ class RaftActorRecoverySupport { context.setCommitIndex(lastApplied); } - private void onDeleteEntries(DeleteEntries deleteEntries) { - if(context.getPersistenceProvider().isRecoveryApplicable()) { + private void onDeleteEntries(final DeleteEntries deleteEntries) { + if (context.getPersistenceProvider().isRecoveryApplicable()) { replicatedLog().removeFrom(deleteEntries.getFromIndex()); } else { dataRecoveredWithPersistenceDisabled = true; } } - private void batchRecoveredLogEntry(ReplicatedLogEntry logEntry) { + private void batchRecoveredLogEntry(final ReplicatedLogEntry logEntry) { initRecoveryTimer(); int batchSize = context.getConfigParams().getJournalRecoveryLogBatchSize(); - if(!isServerConfigurationPayload(logEntry)){ - if(currentRecoveryBatchCount == 0) { + if (!isServerConfigurationPayload(logEntry)) { + if (currentRecoveryBatchCount == 0) { cohort.startLogRecoveryBatch(batchSize); } cohort.appendRecoveredLogEntry(logEntry.getData()); - if(++currentRecoveryBatchCount >= batchSize) { + if (++currentRecoveryBatchCount >= batchSize) { endCurrentLogRecoveryBatch(); } } @@ -243,27 +241,26 @@ class RaftActorRecoverySupport { currentRecoveryBatchCount = 0; } - private void onRecoveryCompletedMessage(PersistentDataProvider persistentProvider) { - if(currentRecoveryBatchCount > 0) { + private void onRecoveryCompletedMessage(final PersistentDataProvider persistentProvider) { + if (currentRecoveryBatchCount > 0) { endCurrentLogRecoveryBatch(); } String recoveryTime = ""; - if(recoveryTimer != null) { + if (recoveryTimer != null) { recoveryTimer.stop(); recoveryTime = " in " + recoveryTimer.toString(); recoveryTimer = null; } - log.info("Recovery completed" + recoveryTime + " - Switching actor to Follower - " + - "Persistence Id = " + context.getId() + - " Last index in log = {}, snapshotIndex = {}, snapshotTerm = {}, " + - "journal-size = {}", replicatedLog().lastIndex(), replicatedLog().getSnapshotIndex(), - replicatedLog().getSnapshotTerm(), replicatedLog().size()); + log.info("{}: Recovery completed {} - Switching actor to Follower - last log index = {}, last log term = {}, " + + "snapshot index = {}, snapshot term = {}, journal size = {}", context.getId(), recoveryTime, + replicatedLog().lastIndex(), replicatedLog().lastTerm(), replicatedLog().getSnapshotIndex(), + replicatedLog().getSnapshotTerm(), replicatedLog().size()); - if(dataRecoveredWithPersistenceDisabled || - hasMigratedDataRecovered && !context.getPersistenceProvider().isRecoveryApplicable()) { - if(hasMigratedDataRecovered) { + if (dataRecoveredWithPersistenceDisabled + || hasMigratedDataRecovered && !context.getPersistenceProvider().isRecoveryApplicable()) { + if (hasMigratedDataRecovered) { log.info("{}: Saving snapshot after recovery due to migrated messages", context.getId()); } else { log.info("{}: Saving snapshot after recovery due to data persistence disabled", context.getId()); @@ -274,14 +271,16 @@ class RaftActorRecoverySupport { // messages. Either way, we persist a snapshot and delete all the messages from the akka journal // to clean out unwanted messages. - Snapshot snapshot = Snapshot.create(new byte[0], Collections.emptyList(), -1, -1, -1, -1, + Snapshot snapshot = Snapshot.create( + EmptyState.INSTANCE, Collections.emptyList(), + -1, -1, -1, -1, context.getTermInformation().getCurrentTerm(), context.getTermInformation().getVotedFor(), context.getPeerServerInfo(true)); persistentProvider.saveSnapshot(snapshot); persistentProvider.deleteMessages(persistentProvider.getLastSequenceNumber()); - } else if(hasMigratedDataRecovered) { + } else if (hasMigratedDataRecovered) { log.info("{}: Snapshot capture initiated after recovery due to migrated messages", context.getId()); context.getSnapshotManager().capture(replicatedLog().last(), -1); @@ -290,19 +289,19 @@ class RaftActorRecoverySupport { } } - private static boolean isServerConfigurationPayload(ReplicatedLogEntry repLogEntry){ + private static boolean isServerConfigurationPayload(final ReplicatedLogEntry repLogEntry) { return repLogEntry.getData() instanceof ServerConfigurationPayload; } - private static boolean isPersistentPayload(ReplicatedLogEntry repLogEntry){ + private static boolean isPersistentPayload(final ReplicatedLogEntry repLogEntry) { return repLogEntry.getData() instanceof PersistentPayload; } - private static boolean isMigratedPayload(ReplicatedLogEntry repLogEntry){ + private static boolean isMigratedPayload(final ReplicatedLogEntry repLogEntry) { return isMigratedSerializable(repLogEntry.getData()); } - private static boolean isMigratedSerializable(Object message){ + private static boolean isMigratedSerializable(final Object message) { return message instanceof MigratedSerializable && ((MigratedSerializable)message).isMigrated(); } }