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=5e4e6571a10d3d1f998796849a9be7486d86de75;hb=refs%2Fchanges%2F14%2F82314%2F25;hpb=97542f208267cb5392fc8c8d9baf6c1d3ee4ae32 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 5e4e6571a1..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,16 +10,16 @@ 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.IOException; -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; @@ -47,7 +47,7 @@ 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); @@ -80,7 +80,7 @@ class RaftActorRecoverySupport { @SuppressWarnings("checkstyle:IllegalCatch") private void possiblyRestoreFromSnapshot() { - byte[] restoreFromSnapshot = cohort.getRestoreFromSnapshot(); + Snapshot restoreFromSnapshot = cohort.getRestoreFromSnapshot(); if (restoreFromSnapshot == null) { return; } @@ -91,15 +91,9 @@ class RaftActorRecoverySupport { return; } - try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(restoreFromSnapshot))) { - Snapshot snapshot = (Snapshot) ois.readObject(); + log.debug("{}: Restore snapshot: {}", context.getId(), restoreFromSnapshot); - log.debug("{}: Deserialized restore snapshot: {}", context.getId(), snapshot); - - context.getSnapshotManager().apply(new ApplySnapshot(snapshot)); - } catch (RuntimeException | ClassNotFoundException | IOException e) { - log.error("{}: Error deserializing snapshot restore", context.getId(), e); - } + context.getSnapshotManager().apply(new ApplySnapshot(restoreFromSnapshot)); } private ReplicatedLog replicatedLog() { @@ -112,8 +106,8 @@ class RaftActorRecoverySupport { } } - private void onRecoveredSnapshot(SnapshotOffer offer) { - log.debug("{}: SnapshotOffer called..", context.getId()); + private void onRecoveredSnapshot(final SnapshotOffer offer) { + log.debug("{}: SnapshotOffer called.", context.getId()); initRecoveryTimer(); @@ -129,7 +123,8 @@ class RaftActorRecoverySupport { // 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()); } @@ -142,26 +137,28 @@ 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) { + private void onRecoveredJournalLogEntry(final ReplicatedLogEntry logEntry) { if (log.isDebugEnabled()) { log.debug("{}: Received ReplicatedLogEntry for recovery: index: {}, size: {}", context.getId(), logEntry.getIndex(), logEntry.size()); @@ -182,7 +179,7 @@ class RaftActorRecoverySupport { } } - private void onRecoveredApplyLogEntries(long toIndex) { + private void onRecoveredApplyLogEntries(final long toIndex) { if (!context.getPersistenceProvider().isRecoveryApplicable()) { dataRecoveredWithPersistenceDisabled = true; return; @@ -214,7 +211,7 @@ class RaftActorRecoverySupport { context.setCommitIndex(lastApplied); } - private void onDeleteEntries(DeleteEntries deleteEntries) { + private void onDeleteEntries(final DeleteEntries deleteEntries) { if (context.getPersistenceProvider().isRecoveryApplicable()) { replicatedLog().removeFrom(deleteEntries.getFromIndex()); } else { @@ -222,7 +219,7 @@ class RaftActorRecoverySupport { } } - private void batchRecoveredLogEntry(ReplicatedLogEntry logEntry) { + private void batchRecoveredLogEntry(final ReplicatedLogEntry logEntry) { initRecoveryTimer(); int batchSize = context.getConfigParams().getJournalRecoveryLogBatchSize(); @@ -244,7 +241,7 @@ class RaftActorRecoverySupport { currentRecoveryBatchCount = 0; } - private void onRecoveryCompletedMessage(PersistentDataProvider persistentProvider) { + private void onRecoveryCompletedMessage(final PersistentDataProvider persistentProvider) { if (currentRecoveryBatchCount > 0) { endCurrentLogRecoveryBatch(); } @@ -256,10 +253,10 @@ class RaftActorRecoverySupport { 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()) { @@ -274,7 +271,8 @@ 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(), + Snapshot snapshot = Snapshot.create( + EmptyState.INSTANCE, Collections.emptyList(), -1, -1, -1, -1, context.getTermInformation().getCurrentTerm(), context.getTermInformation().getVotedFor(), context.getPeerServerInfo(true)); @@ -291,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(); } }