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%2FSnapshotManager.java;h=7d9a1bbf5bdbbfba4fd8df57bf17a17f8e8b125f;hp=12508aebffedc10195ad6c3bd482a1905c23fa7a;hb=aad11d48fcace37e3365388f387e16fa67257a25;hpb=2faf656bf68dd3843fd59520b27a7ec2abbdcc68 diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/SnapshotManager.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/SnapshotManager.java index 12508aebff..7d9a1bbf5b 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/SnapshotManager.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/SnapshotManager.java @@ -5,19 +5,18 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.controller.cluster.raft; import akka.persistence.SnapshotSelectionCriteria; import com.google.common.annotations.VisibleForTesting; import com.google.common.io.ByteSource; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.List; import java.util.Optional; import java.util.function.Consumer; -import javax.annotation.Nonnull; +import org.eclipse.jdt.annotation.NonNull; +import org.opendaylight.controller.cluster.io.FileBackedOutputStream; import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot; import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot; import org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot; @@ -56,7 +55,7 @@ public class SnapshotManager implements SnapshotState { private CaptureSnapshot captureSnapshot; private long lastSequenceNumber = -1; - private Consumer> createSnapshotProcedure; + private Consumer> createSnapshotProcedure = null; private ApplySnapshot applySnapshot; private RaftActorSnapshotCohort snapshotCohort = NoopRaftActorSnapshotCohort.INSTANCE; @@ -67,7 +66,7 @@ public class SnapshotManager implements SnapshotState { * @param context the RaftActorContext * @param logger the Logger */ - public SnapshotManager(RaftActorContext context, Logger logger) { + public SnapshotManager(final RaftActorContext context, final Logger logger) { this.context = context; this.log = logger; } @@ -82,17 +81,18 @@ public class SnapshotManager implements SnapshotState { } @Override - public boolean captureToInstall(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex, String targetFollower) { + public boolean captureToInstall(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex, + final String targetFollower) { return currentState.captureToInstall(lastLogEntry, replicatedToAllIndex, targetFollower); } @Override - public boolean capture(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex) { + public boolean capture(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex) { return currentState.capture(lastLogEntry, replicatedToAllIndex); } @Override - public void apply(ApplySnapshot snapshot) { + public void apply(final ApplySnapshot snapshot) { currentState.apply(snapshot); } @@ -103,7 +103,7 @@ public class SnapshotManager implements SnapshotState { } @Override - public void commit(final long sequenceNumber, long timeStamp) { + public void commit(final long sequenceNumber, final long timeStamp) { currentState.commit(sequenceNumber, timeStamp); } @@ -117,7 +117,8 @@ public class SnapshotManager implements SnapshotState { return currentState.trimLog(desiredTrimIndex); } - void setCreateSnapshotConsumer(Consumer> createSnapshotProcedure) { + @SuppressWarnings("checkstyle:hiddenField") + void setCreateSnapshotConsumer(final Consumer> createSnapshotProcedure) { this.createSnapshotProcedure = createSnapshotProcedure; } @@ -125,8 +126,7 @@ public class SnapshotManager implements SnapshotState { this.snapshotCohort = snapshotCohort; } - @Nonnull - public Snapshot.State convertSnapshot(ByteSource snapshotBytes) throws IOException { + public Snapshot.@NonNull State convertSnapshot(final ByteSource snapshotBytes) throws IOException { return snapshotCohort.deserializeSnapshot(snapshotBytes); } @@ -154,7 +154,7 @@ public class SnapshotManager implements SnapshotState { * @param replicatedToAllIndex the index of the last entry replicated to all followers. * @return a new CaptureSnapshot instance. */ - public CaptureSnapshot newCaptureSnapshot(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex) { + public CaptureSnapshot newCaptureSnapshot(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex) { TermInformationReader lastAppliedTermInfoReader = lastAppliedTermInformationReader.init(context.getReplicatedLog(), context.getLastApplied(), lastLogEntry, hasFollowers()); @@ -170,14 +170,19 @@ public class SnapshotManager implements SnapshotState { List unAppliedEntries = context.getReplicatedLog().getFrom(lastAppliedIndex + 1); - long lastLogEntryIndex = lastAppliedIndex; - long lastLogEntryTerm = lastAppliedTerm; - if (lastLogEntry != null) { + final long lastLogEntryIndex; + final long lastLogEntryTerm; + if (lastLogEntry == null) { + // When we don't have journal present, for example two captureSnapshots executed right after another with no + // new journal we still want to preserve the index and term in the snapshot. + lastAppliedIndex = lastLogEntryIndex = context.getReplicatedLog().getSnapshotIndex(); + lastAppliedTerm = lastLogEntryTerm = context.getReplicatedLog().getSnapshotTerm(); + + log.debug("{}: Capturing Snapshot : lastLogEntry is null. Using snapshot values lastAppliedIndex {} and " + + "lastAppliedTerm {} instead.", persistenceId(), lastAppliedIndex, lastAppliedTerm); + } else { lastLogEntryIndex = lastLogEntry.getIndex(); lastLogEntryTerm = lastLogEntry.getTerm(); - } else { - log.debug("{}: Capturing Snapshot : lastLogEntry is null. Using lastAppliedIndex {} and " - + "lastAppliedTerm {} instead.", persistenceId(), lastAppliedIndex, lastAppliedTerm); } return new CaptureSnapshot(lastLogEntryIndex, lastLogEntryTerm, lastAppliedIndex, lastAppliedTerm, @@ -192,20 +197,20 @@ public class SnapshotManager implements SnapshotState { } @Override - public boolean capture(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex) { + public boolean capture(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex) { log.debug("capture should not be called in state {}", this); return false; } @Override - public boolean captureToInstall(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex, - String targetFollower) { + public boolean captureToInstall(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex, + final String targetFollower) { log.debug("captureToInstall should not be called in state {}", this); return false; } @Override - public void apply(ApplySnapshot snapshot) { + public void apply(final ApplySnapshot snapshot) { log.debug("apply should not be called in state {}", this); } @@ -216,7 +221,7 @@ public class SnapshotManager implements SnapshotState { } @Override - public void commit(final long sequenceNumber, long timeStamp) { + public void commit(final long sequenceNumber, final long timeStamp) { log.debug("commit should not be called in state {}", this); } @@ -273,12 +278,13 @@ public class SnapshotManager implements SnapshotState { } @SuppressWarnings("checkstyle:IllegalCatch") - private boolean capture(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex, String targetFollower) { + private boolean capture(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex, + final String targetFollower) { captureSnapshot = newCaptureSnapshot(lastLogEntry, replicatedToAllIndex); OutputStream installSnapshotStream = null; if (targetFollower != null) { - installSnapshotStream = new ByteArrayOutputStream(); + installSnapshotStream = context.getFileBackedOutputStreamFactory().newInstance(); log.info("{}: Initiating snapshot capture {} to install on {}", persistenceId(), captureSnapshot, targetFollower); } else { @@ -303,18 +309,18 @@ public class SnapshotManager implements SnapshotState { } @Override - public boolean capture(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex) { + public boolean capture(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex) { return capture(lastLogEntry, replicatedToAllIndex, null); } @Override - public boolean captureToInstall(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex, - String targetFollower) { + public boolean captureToInstall(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex, + final String targetFollower) { return capture(lastLogEntry, replicatedToAllIndex, targetFollower); } @Override - public void apply(ApplySnapshot toApply) { + public void apply(final ApplySnapshot toApply) { SnapshotManager.this.applySnapshot = toApply; lastSequenceNumber = context.getPersistenceProvider().getLastSequenceNumber(); @@ -410,18 +416,17 @@ public class SnapshotManager implements SnapshotState { context.getReplicatedLog().getSnapshotTerm()); if (installSnapshotStream.isPresent()) { - try { - installSnapshotStream.get().close(); - } catch (IOException e) { - log.warn("Error closing install snapshot OutputStream", e); - } - if (context.getId().equals(currentBehavior.getLeaderId())) { - ByteSource snapshotBytes = ByteSource.wrap(((ByteArrayOutputStream)installSnapshotStream.get()) - .toByteArray()); - - // this would be call straight to the leader and won't initiate in serialization - currentBehavior.handleMessage(context.getActor(), new SendInstallSnapshot(snapshot, snapshotBytes)); + try { + ByteSource snapshotBytes = ((FileBackedOutputStream)installSnapshotStream.get()).asByteSource(); + currentBehavior.handleMessage(context.getActor(), + new SendInstallSnapshot(snapshot, snapshotBytes)); + } catch (IOException e) { + log.error("{}: Snapshot install failed due to an unrecoverable streaming error", + context.getId(), e); + } + } else { + ((FileBackedOutputStream)installSnapshotStream.get()).cleanup(); } } @@ -440,7 +445,7 @@ public class SnapshotManager implements SnapshotState { @Override @SuppressWarnings("checkstyle:IllegalCatch") - public void commit(final long sequenceNumber, long timeStamp) { + public void commit(final long sequenceNumber, final long timeStamp) { log.debug("{}: Snapshot success - sequence number: {}", persistenceId(), sequenceNumber); if (applySnapshot != null) { @@ -469,7 +474,7 @@ public class SnapshotManager implements SnapshotState { context.getReplicatedLog().snapshotCommit(); } - context.getPersistenceProvider().deleteSnapshots(new SnapshotSelectionCriteria(sequenceNumber, + context.getPersistenceProvider().deleteSnapshots(new SnapshotSelectionCriteria(scala.Long.MaxValue(), timeStamp - 1, 0L, 0L)); context.getPersistenceProvider().deleteMessages(lastSequenceNumber); @@ -520,8 +525,8 @@ public class SnapshotManager implements SnapshotState { private long index; private long term; - LastAppliedTermInformationReader init(ReplicatedLog log, long originalIndex, ReplicatedLogEntry lastLogEntry, - boolean hasFollowers) { + LastAppliedTermInformationReader init(final ReplicatedLog log, final long originalIndex, + final ReplicatedLogEntry lastLogEntry, final boolean hasFollowers) { ReplicatedLogEntry entry = log.get(originalIndex); this.index = -1L; this.term = -1L; @@ -557,7 +562,7 @@ public class SnapshotManager implements SnapshotState { private long index; private long term; - ReplicatedToAllTermInformationReader init(ReplicatedLog log, long originalIndex) { + ReplicatedToAllTermInformationReader init(final ReplicatedLog log, final long originalIndex) { ReplicatedLogEntry entry = log.get(originalIndex); this.index = -1L; this.term = -1L;