* 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 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;
private CaptureSnapshot captureSnapshot;
private long lastSequenceNumber = -1;
- private Consumer<Optional<OutputStream>> createSnapshotProcedure;
+ private Consumer<Optional<OutputStream>> createSnapshotProcedure = null;
private ApplySnapshot applySnapshot;
private RaftActorSnapshotCohort snapshotCohort = NoopRaftActorSnapshotCohort.INSTANCE;
* @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;
}
}
@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 boolean captureWithForcedTrim(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex) {
+ return currentState.captureWithForcedTrim(lastLogEntry, replicatedToAllIndex);
+ }
+
+ @Override
+ public void apply(final ApplySnapshot snapshot) {
currentState.apply(snapshot);
}
}
@Override
- public void commit(final long sequenceNumber, long timeStamp) {
+ public void commit(final long sequenceNumber, final long timeStamp) {
currentState.commit(sequenceNumber, timeStamp);
}
return currentState.trimLog(desiredTrimIndex);
}
- void setCreateSnapshotConsumer(Consumer<Optional<OutputStream>> createSnapshotProcedure) {
+ @SuppressWarnings("checkstyle:hiddenField")
+ void setCreateSnapshotConsumer(final Consumer<Optional<OutputStream>> createSnapshotProcedure) {
this.createSnapshotProcedure = createSnapshotProcedure;
}
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);
}
* @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,
+ final boolean mandatoryTrim) {
TermInformationReader lastAppliedTermInfoReader =
lastAppliedTermInformationReader.init(context.getReplicatedLog(), context.getLastApplied(),
lastLogEntry, hasFollowers());
List<ReplicatedLogEntry> 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,
- newReplicatedToAllIndex, newReplicatedToAllTerm, unAppliedEntries);
+ newReplicatedToAllIndex, newReplicatedToAllTerm, unAppliedEntries, mandatoryTrim);
}
private class AbstractSnapshotState 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 boolean captureWithForcedTrim(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex) {
+ log.debug("captureWithForcedTrim should not be called in state {}", this);
+ return false;
+ }
+
+ @Override
+ public void apply(final ApplySnapshot snapshot) {
log.debug("apply should not be called in state {}", this);
}
}
@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);
}
}
@SuppressWarnings("checkstyle:IllegalCatch")
- private boolean capture(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex, String targetFollower) {
- captureSnapshot = newCaptureSnapshot(lastLogEntry, replicatedToAllIndex);
+ private boolean capture(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex,
+ final String targetFollower, final boolean mandatoryTrim) {
+ captureSnapshot = newCaptureSnapshot(lastLogEntry, replicatedToAllIndex, mandatoryTrim);
OutputStream installSnapshotStream = null;
if (targetFollower != null) {
}
@Override
- public boolean capture(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex) {
- return capture(lastLogEntry, replicatedToAllIndex, null);
+ public boolean capture(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex) {
+ return capture(lastLogEntry, replicatedToAllIndex, null, false);
}
@Override
- public boolean captureToInstall(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex,
- String targetFollower) {
- return capture(lastLogEntry, replicatedToAllIndex, targetFollower);
+ public boolean captureToInstall(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex,
+ final String targetFollower) {
+ return capture(lastLogEntry, replicatedToAllIndex, targetFollower, false);
}
@Override
- public void apply(ApplySnapshot toApply) {
+ public boolean captureWithForcedTrim(final ReplicatedLogEntry lastLogEntry, final long replicatedToAllIndex) {
+ return capture(lastLogEntry, replicatedToAllIndex, null, true);
+ }
+
+ @Override
+ public void apply(final ApplySnapshot toApply) {
SnapshotManager.this.applySnapshot = toApply;
lastSequenceNumber = context.getPersistenceProvider().getLastSequenceNumber();
log.info("{}: Persisting of snapshot done: {}", persistenceId(), snapshot);
- long dataThreshold = totalMemory * context.getConfigParams().getSnapshotDataThresholdPercentage() / 100;
- boolean dataSizeThresholdExceeded = context.getReplicatedLog().dataSize() > dataThreshold;
+ final ConfigParams config = context.getConfigParams();
+ final long absoluteThreshold = config.getSnapshotDataThreshold();
+ final long dataThreshold = absoluteThreshold != 0 ? absoluteThreshold * ConfigParams.MEGABYTE
+ : totalMemory * config.getSnapshotDataThresholdPercentage() / 100;
- boolean logSizeExceededSnapshotBatchCount =
- context.getReplicatedLog().size() >= context.getConfigParams().getSnapshotBatchCount();
+ final boolean dataSizeThresholdExceeded = context.getReplicatedLog().dataSize() > dataThreshold;
+ final boolean logSizeExceededSnapshotBatchCount =
+ context.getReplicatedLog().size() >= config.getSnapshotBatchCount();
final RaftActorBehavior currentBehavior = context.getCurrentBehavior();
- if (dataSizeThresholdExceeded || logSizeExceededSnapshotBatchCount) {
+ if (dataSizeThresholdExceeded || logSizeExceededSnapshotBatchCount || captureSnapshot.isMandatoryTrim()) {
if (log.isDebugEnabled()) {
if (dataSizeThresholdExceeded) {
log.debug("{}: log data size {} exceeds the memory threshold {} - doing snapshotPreCommit "
+ "with index {}", context.getId(), context.getReplicatedLog().dataSize(),
dataThreshold, captureSnapshot.getLastAppliedIndex());
- } else {
+ } else if (logSizeExceededSnapshotBatchCount) {
log.debug("{}: log size {} exceeds the snapshot batch count {} - doing snapshotPreCommit with "
+ "index {}", context.getId(), context.getReplicatedLog().size(),
- context.getConfigParams().getSnapshotBatchCount(),
- captureSnapshot.getLastAppliedIndex());
+ config.getSnapshotBatchCount(), captureSnapshot.getLastAppliedIndex());
+ } else {
+ log.debug("{}: user triggered or root overwrite snapshot encountered, trimming log up to "
+ + "last applied index {}", context.getId(), captureSnapshot.getLastAppliedIndex());
}
}
@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) {
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);
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;
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;