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=71ef8ffcea04360b6db573de04d5344d44b3cf7c;hb=aad11d48fcace37e3365388f387e16fa67257a25;hpb=d796a8de8b208ca24bb57aebfc689f8be8bc2c7b 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 71ef8ffcea..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,7 +5,6 @@ * 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; @@ -16,7 +15,7 @@ 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; @@ -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 = context.newFileBackedOutputStream(); + 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(); @@ -439,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) { @@ -468,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); @@ -519,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; @@ -556,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;