package org.opendaylight.controller.cluster.raft;
-import akka.japi.Procedure;
import akka.persistence.SnapshotSelectionCriteria;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.io.ByteSource;
+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.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;
import org.opendaylight.controller.cluster.raft.base.messages.SnapshotComplete;
import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
+import org.opendaylight.controller.cluster.raft.persisted.EmptyState;
+import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
import org.slf4j.Logger;
+/**
+ * Manages the capturing of snapshots for a RaftActor.
+ *
+ * @author Moiz Raja
+ * @author Thomas Pantelis
+ */
public class SnapshotManager implements SnapshotState {
+ @SuppressWarnings("checkstyle:MemberName")
private final SnapshotState IDLE = new Idle();
+
+ @SuppressWarnings({"checkstyle:MemberName", "checkstyle:AbbreviationAsWordInName"})
private final SnapshotState PERSISTING = new Persisting();
+
+ @SuppressWarnings({"checkstyle:MemberName", "checkstyle:AbbreviationAsWordInName"})
private final SnapshotState CREATING = new Creating();
- private final Logger LOG;
+ private final Logger log;
private final RaftActorContext context;
private final LastAppliedTermInformationReader lastAppliedTermInformationReader =
new LastAppliedTermInformationReader();
private CaptureSnapshot captureSnapshot;
private long lastSequenceNumber = -1;
- private Procedure<Void> createSnapshotProcedure;
+ private Consumer<Optional<OutputStream>> createSnapshotProcedure;
private ApplySnapshot applySnapshot;
- private Procedure<byte[]> applySnapshotProcedure;
-
+ private RaftActorSnapshotCohort snapshotCohort = NoopRaftActorSnapshotCohort.INSTANCE;
+
+ /**
+ * Constructs an instance.
+ *
+ * @param context the RaftActorContext
+ * @param logger the Logger
+ */
public SnapshotManager(RaftActorContext context, Logger logger) {
this.context = context;
- this.LOG = logger;
+ this.log = logger;
}
public boolean isApplying() {
}
@Override
- public void persist(final byte[] snapshotBytes, final long totalMemory) {
- currentState.persist(snapshotBytes, totalMemory);
+ public void persist(final Snapshot.State state, final Optional<OutputStream> installSnapshotStream,
+ final long totalMemory) {
+ currentState.persist(state, installSnapshotStream, totalMemory);
}
@Override
- public void commit(final long sequenceNumber) {
- currentState.commit(sequenceNumber);
+ public void commit(final long sequenceNumber, long timeStamp) {
+ currentState.commit(sequenceNumber, timeStamp);
}
@Override
return currentState.trimLog(desiredTrimIndex);
}
- public void setCreateSnapshotCallable(Procedure<Void> createSnapshotProcedure) {
+ void setCreateSnapshotConsumer(Consumer<Optional<OutputStream>> createSnapshotProcedure) {
this.createSnapshotProcedure = createSnapshotProcedure;
}
- public void setApplySnapshotProcedure(Procedure<byte[]> applySnapshotProcedure) {
- this.applySnapshotProcedure = applySnapshotProcedure;
+ void setSnapshotCohort(final RaftActorSnapshotCohort snapshotCohort) {
+ this.snapshotCohort = snapshotCohort;
+ }
+
+ @Nonnull
+ public Snapshot.State convertSnapshot(ByteSource snapshotBytes) throws IOException {
+ return snapshotCohort.deserializeSnapshot(snapshotBytes);
}
public long getLastSequenceNumber() {
return captureSnapshot;
}
- private boolean hasFollowers(){
+ private boolean hasFollowers() {
return context.hasFollowers();
}
- private String persistenceId(){
+ private String persistenceId() {
return context.getId();
}
- public CaptureSnapshot newCaptureSnapshot(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex,
- boolean installSnapshotInitiated) {
+ /**
+ * Constructs a CaptureSnapshot instance.
+ *
+ * @param lastLogEntry the last log entry for the snapshot.
+ * @param replicatedToAllIndex the index of the last entry replicated to all followers.
+ * @return a new CaptureSnapshot instance.
+ */
+ public CaptureSnapshot newCaptureSnapshot(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex) {
TermInformationReader lastAppliedTermInfoReader =
lastAppliedTermInformationReader.init(context.getReplicatedLog(), context.getLastApplied(),
lastLogEntry, hasFollowers());
long lastLogEntryIndex = lastAppliedIndex;
long lastLogEntryTerm = lastAppliedTerm;
- if(lastLogEntry != null) {
+ if (lastLogEntry != null) {
lastLogEntryIndex = lastLogEntry.getIndex();
lastLogEntryTerm = lastLogEntry.getTerm();
} else {
- LOG.debug("{}: Capturing Snapshot : lastLogEntry is null. Using lastAppliedIndex {} and lastAppliedTerm {} instead.",
- persistenceId(), lastAppliedIndex, lastAppliedTerm);
+ 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, installSnapshotInitiated);
+ newReplicatedToAllIndex, newReplicatedToAllTerm, unAppliedEntries);
}
private class AbstractSnapshotState implements SnapshotState {
@Override
public boolean capture(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex) {
- LOG.debug("capture should not be called in state {}", this);
+ log.debug("capture should not be called in state {}", this);
return false;
}
@Override
- public boolean captureToInstall(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex, String targetFollower) {
- LOG.debug("captureToInstall should not be called in state {}", this);
+ public boolean captureToInstall(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex,
+ String targetFollower) {
+ log.debug("captureToInstall should not be called in state {}", this);
return false;
}
@Override
public void apply(ApplySnapshot snapshot) {
- LOG.debug("apply should not be called in state {}", this);
+ log.debug("apply should not be called in state {}", this);
}
@Override
- public void persist(final byte[] snapshotBytes, final long totalMemory) {
- LOG.debug("persist should not be called in state {}", this);
+ public void persist(final Snapshot.State state, final Optional<OutputStream> installSnapshotStream,
+ final long totalMemory) {
+ log.debug("persist should not be called in state {}", this);
}
@Override
- public void commit(final long sequenceNumber) {
- LOG.debug("commit should not be called in state {}", this);
+ public void commit(final long sequenceNumber, long timeStamp) {
+ log.debug("commit should not be called in state {}", this);
}
@Override
public void rollback() {
- LOG.debug("rollback should not be called in state {}", this);
+ log.debug("rollback should not be called in state {}", this);
}
@Override
public long trimLog(final long desiredTrimIndex) {
- LOG.debug("trimLog should not be called in state {}", this);
+ log.debug("trimLog should not be called in state {}", this);
return -1;
}
protected long doTrimLog(final long desiredTrimIndex) {
// we would want to keep the lastApplied as its used while capturing snapshots
long lastApplied = context.getLastApplied();
- long tempMin = Math.min(desiredTrimIndex, (lastApplied > -1 ? lastApplied - 1 : -1));
+ long tempMin = Math.min(desiredTrimIndex, lastApplied > -1 ? lastApplied - 1 : -1);
- if(LOG.isTraceEnabled()) {
- LOG.trace("{}: performSnapshotWithoutCapture: desiredTrimIndex: {}, lastApplied: {}, tempMin: {}",
+ if (log.isTraceEnabled()) {
+ log.trace("{}: performSnapshotWithoutCapture: desiredTrimIndex: {}, lastApplied: {}, tempMin: {}",
persistenceId(), desiredTrimIndex, lastApplied, tempMin);
}
if (tempMin > -1 && context.getReplicatedLog().isPresent(tempMin)) {
- LOG.debug("{}: fakeSnapshot purging log to {} for term {}", persistenceId(), tempMin,
+ log.debug("{}: fakeSnapshot purging log to {} for term {}", persistenceId(), tempMin,
context.getTermInformation().getCurrentTerm());
//use the term of the temp-min, since we check for isPresent, entry will not be null
}
final RaftActorBehavior currentBehavior = context.getCurrentBehavior();
- if(tempMin > currentBehavior.getReplicatedToAllIndex()) {
+ if (tempMin > currentBehavior.getReplicatedToAllIndex()) {
// It's possible a follower was lagging and an install snapshot advanced its match index past
// the current replicatedToAllIndex. Since the follower is now caught up we should advance the
// replicatedToAllIndex (to tempMin). The fact that tempMin wasn't found in the log is likely
return false;
}
+ @SuppressWarnings("checkstyle:IllegalCatch")
private boolean capture(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex, String targetFollower) {
- captureSnapshot = newCaptureSnapshot(lastLogEntry, replicatedToAllIndex, targetFollower != null);
+ captureSnapshot = newCaptureSnapshot(lastLogEntry, replicatedToAllIndex);
- if(captureSnapshot.isInstallSnapshotInitiated()) {
- LOG.info("{}: Initiating snapshot capture {} to install on {}",
+ OutputStream installSnapshotStream = null;
+ if (targetFollower != null) {
+ installSnapshotStream = context.getFileBackedOutputStreamFactory().newInstance();
+ log.info("{}: Initiating snapshot capture {} to install on {}",
persistenceId(), captureSnapshot, targetFollower);
} else {
- LOG.info("{}: Initiating snapshot capture {}", persistenceId(), captureSnapshot);
+ log.info("{}: Initiating snapshot capture {}", persistenceId(), captureSnapshot);
}
lastSequenceNumber = context.getPersistenceProvider().getLastSequenceNumber();
- LOG.debug("{}: lastSequenceNumber prior to capture: {}", persistenceId(), lastSequenceNumber);
+ log.debug("{}: lastSequenceNumber prior to capture: {}", persistenceId(), lastSequenceNumber);
SnapshotManager.this.currentState = CREATING;
try {
- createSnapshotProcedure.apply(null);
+ createSnapshotProcedure.accept(Optional.ofNullable(installSnapshotStream));
} catch (Exception e) {
SnapshotManager.this.currentState = IDLE;
- LOG.error("Error creating snapshot", e);
+ log.error("Error creating snapshot", e);
return false;
}
}
@Override
- public boolean captureToInstall(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex, String targetFollower) {
+ public boolean captureToInstall(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex,
+ String targetFollower) {
return capture(lastLogEntry, replicatedToAllIndex, targetFollower);
}
@Override
- public void apply(ApplySnapshot applySnapshot) {
- SnapshotManager.this.applySnapshot = applySnapshot;
+ public void apply(ApplySnapshot toApply) {
+ SnapshotManager.this.applySnapshot = toApply;
lastSequenceNumber = context.getPersistenceProvider().getLastSequenceNumber();
- LOG.debug("lastSequenceNumber prior to persisting applied snapshot: {}", lastSequenceNumber);
+ log.debug("lastSequenceNumber prior to persisting applied snapshot: {}", lastSequenceNumber);
- context.getPersistenceProvider().saveSnapshot(applySnapshot.getSnapshot());
+ context.getPersistenceProvider().saveSnapshot(toApply.getSnapshot());
SnapshotManager.this.currentState = PERSISTING;
}
private class Creating extends AbstractSnapshotState {
@Override
- public void persist(final byte[] snapshotBytes, final long totalMemory) {
+ public void persist(final Snapshot.State snapshotState, final Optional<OutputStream> installSnapshotStream,
+ final long totalMemory) {
// create a snapshot object from the state provided and save it
// when snapshot is saved async, SaveSnapshotSuccess is raised.
- Snapshot snapshot = Snapshot.create(snapshotBytes,
+ Snapshot snapshot = Snapshot.create(snapshotState,
captureSnapshot.getUnAppliedEntries(),
captureSnapshot.getLastIndex(), captureSnapshot.getLastTerm(),
captureSnapshot.getLastAppliedIndex(), captureSnapshot.getLastAppliedTerm(),
context.getPersistenceProvider().saveSnapshot(snapshot);
- LOG.info("{}: Persisting of snapshot done: {}", persistenceId(), snapshot);
+ log.info("{}: Persisting of snapshot done: {}", persistenceId(), snapshot);
- long dataThreshold = totalMemory *
- context.getConfigParams().getSnapshotDataThresholdPercentage() / 100;
+ long dataThreshold = totalMemory * context.getConfigParams().getSnapshotDataThresholdPercentage() / 100;
boolean dataSizeThresholdExceeded = context.getReplicatedLog().dataSize() > dataThreshold;
boolean logSizeExceededSnapshotBatchCount =
final RaftActorBehavior currentBehavior = context.getCurrentBehavior();
if (dataSizeThresholdExceeded || logSizeExceededSnapshotBatchCount) {
- 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());
+ 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 {
- LOG.debug("{}: log size {} exceeds the snapshot batch count {} - doing snapshotPreCommit with index {}",
- context.getId(), context.getReplicatedLog().size(),
- context.getConfigParams().getSnapshotBatchCount(), captureSnapshot.getLastAppliedIndex());
+ log.debug("{}: log size {} exceeds the snapshot batch count {} - doing snapshotPreCommit with "
+ + "index {}", context.getId(), context.getReplicatedLog().size(),
+ context.getConfigParams().getSnapshotBatchCount(),
+ captureSnapshot.getLastAppliedIndex());
}
}
// Don't reset replicatedToAllIndex to -1 as this may prevent us from trimming the log after an
// install snapshot to a follower.
- if(captureSnapshot.getReplicatedToAllIndex() >= 0) {
+ if (captureSnapshot.getReplicatedToAllIndex() >= 0) {
currentBehavior.setReplicatedToAllIndex(captureSnapshot.getReplicatedToAllIndex());
}
- } else if(captureSnapshot.getReplicatedToAllIndex() != -1){
+ } else if (captureSnapshot.getReplicatedToAllIndex() != -1) {
// clear the log based on replicatedToAllIndex
context.getReplicatedLog().snapshotPreCommit(captureSnapshot.getReplicatedToAllIndex(),
captureSnapshot.getReplicatedToAllTerm());
context.getReplicatedLog().getSnapshotTerm());
}
- LOG.info("{}: Removed in-memory snapshotted entries, adjusted snaphsotIndex: {} " +
- "and term: {}", context.getId(), context.getReplicatedLog().getSnapshotIndex(),
+ log.info("{}: Removed in-memory snapshotted entries, adjusted snaphsotIndex: {} and term: {}",
+ context.getId(), context.getReplicatedLog().getSnapshotIndex(),
context.getReplicatedLog().getSnapshotTerm());
- if (context.getId().equals(currentBehavior.getLeaderId())
- && captureSnapshot.isInstallSnapshotInitiated()) {
- // this would be call straight to the leader and won't initiate in serialization
- currentBehavior.handleMessage(context.getActor(), new SendInstallSnapshot(snapshot));
+ if (installSnapshotStream.isPresent()) {
+ if (context.getId().equals(currentBehavior.getLeaderId())) {
+ 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();
+ }
}
captureSnapshot = null;
private class Persisting extends AbstractSnapshotState {
@Override
- public void commit(final long sequenceNumber) {
- LOG.debug("{}: Snapshot success - sequence number: {}", persistenceId(), sequenceNumber);
+ @SuppressWarnings("checkstyle:IllegalCatch")
+ public void commit(final long sequenceNumber, long timeStamp) {
+ log.debug("{}: Snapshot success - sequence number: {}", persistenceId(), sequenceNumber);
- if(applySnapshot != null) {
+ if (applySnapshot != null) {
try {
Snapshot snapshot = applySnapshot.getSnapshot();
context.setCommitIndex(snapshot.getLastAppliedIndex());
context.getTermInformation().update(snapshot.getElectionTerm(), snapshot.getElectionVotedFor());
- if(snapshot.getState().length > 0 ) {
- applySnapshotProcedure.apply(snapshot.getState());
+ if (snapshot.getServerConfiguration() != null) {
+ context.updatePeerIds(snapshot.getServerConfiguration());
+ }
+
+ if (!(snapshot.getState() instanceof EmptyState)) {
+ snapshotCohort.applySnapshot(snapshot.getState());
}
applySnapshot.getCallback().onSuccess();
} catch (Exception e) {
- LOG.error("{}: Error applying snapshot", context.getId(), e);
+ log.error("{}: Error applying snapshot", context.getId(), e);
}
} else {
context.getReplicatedLog().snapshotCommit();
}
- context.getPersistenceProvider().deleteSnapshots(new SnapshotSelectionCriteria(
- sequenceNumber - context.getConfigParams().getSnapshotBatchCount(), Long.MAX_VALUE, 0L, 0L));
+ context.getPersistenceProvider().deleteSnapshots(new SnapshotSelectionCriteria(sequenceNumber,
+ timeStamp - 1, 0L, 0L));
context.getPersistenceProvider().deleteMessages(lastSequenceNumber);
@Override
public void rollback() {
// Nothing to rollback if we're applying a snapshot from the leader.
- if(applySnapshot == null) {
+ if (applySnapshot == null) {
context.getReplicatedLog().snapshotRollback();
- LOG.info("{}: Replicated Log rolled back. Snapshot will be attempted in the next cycle." +
- "snapshotIndex:{}, snapshotTerm:{}, log-size:{}", persistenceId(),
+ log.info("{}: Replicated Log rolled back. Snapshot will be attempted in the next cycle."
+ + "snapshotIndex:{}, snapshotTerm:{}, log-size:{}", persistenceId(),
context.getReplicatedLog().getSnapshotIndex(),
context.getReplicatedLog().getSnapshotTerm(),
context.getReplicatedLog().size());
}
- private static interface TermInformationReader {
+ private interface TermInformationReader {
long getIndex();
+
long getTerm();
}
- static class LastAppliedTermInformationReader implements TermInformationReader{
+ static class LastAppliedTermInformationReader implements TermInformationReader {
private long index;
private long term;
- public LastAppliedTermInformationReader init(ReplicatedLog log, long originalIndex,
- ReplicatedLogEntry lastLogEntry, boolean hasFollowers){
+ LastAppliedTermInformationReader init(ReplicatedLog log, long originalIndex, ReplicatedLogEntry lastLogEntry,
+ boolean hasFollowers) {
ReplicatedLogEntry entry = log.get(originalIndex);
this.index = -1L;
this.term = -1L;
if (!hasFollowers) {
- if(lastLogEntry != null) {
+ if (lastLogEntry != null) {
// since we have persisted the last-log-entry to persistent journal before the capture,
// we would want to snapshot from this entry.
index = lastLogEntry.getIndex();
} else if (entry != null) {
index = entry.getIndex();
term = entry.getTerm();
- } else if(log.getSnapshotIndex() > -1){
+ } else if (log.getSnapshotIndex() > -1) {
index = log.getSnapshotIndex();
term = log.getSnapshotTerm();
}
}
@Override
- public long getIndex(){
+ public long getIndex() {
return this.index;
}
@Override
- public long getTerm(){
+ public long getTerm() {
return this.term;
}
}
- private static class ReplicatedToAllTermInformationReader implements TermInformationReader{
+ private static class ReplicatedToAllTermInformationReader implements TermInformationReader {
private long index;
private long term;
- ReplicatedToAllTermInformationReader init(ReplicatedLog log, long originalIndex){
+ ReplicatedToAllTermInformationReader init(ReplicatedLog log, long originalIndex) {
ReplicatedLogEntry entry = log.get(originalIndex);
this.index = -1L;
this.term = -1L;
}
@Override
- public long getIndex(){
+ public long getIndex() {
return this.index;
}
@Override
- public long getTerm(){
+ public long getTerm() {
return this.term;
}
}