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=8e0d2f820b722a1b91742e67b5a438467af35ec3;hp=9a916625c9331413685d6263bfe053930b6795bf;hb=3fda1a923defdbf18849c6080c3aa19f1ebf2c5f;hpb=cac7138ddd81d1cc801d4b6cd9bc4372e0a8ddd3 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 9a916625c9..8e0d2f820b 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 @@ -11,8 +11,8 @@ package org.opendaylight.controller.cluster.raft; import akka.japi.Procedure; import akka.persistence.SnapshotSelectionCriteria; import com.google.common.annotations.VisibleForTesting; -import com.google.protobuf.ByteString; import java.util.List; +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.behaviors.RaftActorBehavior; @@ -38,11 +38,18 @@ public class SnapshotManager implements SnapshotState { private Procedure createSnapshotProcedure; + private ApplySnapshot applySnapshot; + private Procedure applySnapshotProcedure; + public SnapshotManager(RaftActorContext context, Logger logger) { this.context = context; this.LOG = logger; } + public boolean isApplying() { + return applySnapshot != null; + } + @Override public boolean isCapturing() { return currentState.isCapturing(); @@ -58,14 +65,19 @@ public class SnapshotManager implements SnapshotState { return currentState.capture(lastLogEntry, replicatedToAllIndex); } + @Override + public void apply(ApplySnapshot snapshot) { + currentState.apply(snapshot); + } + @Override public void persist(byte[] snapshotBytes, RaftActorBehavior currentBehavior, long totalMemory) { currentState.persist(snapshotBytes, currentBehavior, totalMemory); } @Override - public void commit(long sequenceNumber) { - currentState.commit(sequenceNumber); + public void commit(long sequenceNumber, RaftActorBehavior currentBehavior) { + currentState.commit(sequenceNumber, currentBehavior); } @Override @@ -82,6 +94,14 @@ public class SnapshotManager implements SnapshotState { this.createSnapshotProcedure = createSnapshotProcedure; } + public void setApplySnapshotProcedure(Procedure applySnapshotProcedure) { + this.applySnapshotProcedure = applySnapshotProcedure; + } + + public long getLastSequenceNumber() { + return lastSequenceNumber; + } + @VisibleForTesting public CaptureSnapshot getCaptureSnapshot() { return captureSnapshot; @@ -99,7 +119,7 @@ public class SnapshotManager implements SnapshotState { @Override public boolean isCapturing() { - return false; + return true; } @Override @@ -114,13 +134,18 @@ public class SnapshotManager implements SnapshotState { return false; } + @Override + public void apply(ApplySnapshot snapshot) { + LOG.debug("apply should not be called in state {}", this); + } + @Override public void persist(byte[] snapshotBytes, RaftActorBehavior currentBehavior, long totalMemory) { LOG.debug("persist should not be called in state {}", this); } @Override - public void commit(long sequenceNumber) { + public void commit(long sequenceNumber, RaftActorBehavior currentBehavior) { LOG.debug("commit should not be called in state {}", this); } @@ -168,6 +193,11 @@ public class SnapshotManager implements SnapshotState { private class Idle extends AbstractSnapshotState { + @Override + public boolean isCapturing() { + return false; + } + private boolean capture(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex, String targetFollower) { TermInformationReader lastAppliedTermInfoReader = lastAppliedTermInformationReader.init(context.getReplicatedLog(), context.getLastApplied(), @@ -186,8 +216,18 @@ public class SnapshotManager implements SnapshotState { List unAppliedEntries = context.getReplicatedLog().getFrom(lastAppliedIndex + 1); - captureSnapshot = new CaptureSnapshot(lastLogEntry.getIndex(), - lastLogEntry.getTerm(), lastAppliedIndex, lastAppliedTerm, + long lastLogEntryIndex = lastAppliedIndex; + long lastLogEntryTerm = lastAppliedTerm; + if(lastLogEntry != null) { + lastLogEntryIndex = lastLogEntry.getIndex(); + lastLogEntryTerm = lastLogEntry.getTerm(); + } else { + LOG.warn("Capturing Snapshot : lastLogEntry is null. Using lastAppliedIndex {} and lastAppliedTerm {} instead.", + lastAppliedIndex, lastAppliedTerm); + } + + captureSnapshot = new CaptureSnapshot(lastLogEntryIndex, + lastLogEntryTerm, lastAppliedIndex, lastAppliedTerm, newReplicatedToAllIndex, newReplicatedToAllTerm, unAppliedEntries, targetFollower != null); if(captureSnapshot.isInstallSnapshotInitiated()) { @@ -224,6 +264,19 @@ public class SnapshotManager implements SnapshotState { return capture(lastLogEntry, replicatedToAllIndex, targetFollower); } + @Override + public void apply(ApplySnapshot applySnapshot) { + SnapshotManager.this.applySnapshot = applySnapshot; + + lastSequenceNumber = context.getPersistenceProvider().getLastSequenceNumber(); + + LOG.debug("lastSequenceNumber prior to persisting applied snapshot: {}", lastSequenceNumber); + + context.getPersistenceProvider().saveSnapshot(applySnapshot.getSnapshot()); + + SnapshotManager.this.currentState = PERSISTING; + } + @Override public String toString() { return "Idle"; @@ -237,38 +290,46 @@ public class SnapshotManager implements SnapshotState { private class Creating extends AbstractSnapshotState { - @Override - public boolean isCapturing() { - return true; - } - @Override public void persist(byte[] snapshotBytes, RaftActorBehavior currentBehavior, long totalMemory) { // create a snapshot object from the state provided and save it // when snapshot is saved async, SaveSnapshotSuccess is raised. - Snapshot sn = Snapshot.create(snapshotBytes, + Snapshot snapshot = Snapshot.create(snapshotBytes, captureSnapshot.getUnAppliedEntries(), captureSnapshot.getLastIndex(), captureSnapshot.getLastTerm(), - captureSnapshot.getLastAppliedIndex(), captureSnapshot.getLastAppliedTerm()); + captureSnapshot.getLastAppliedIndex(), captureSnapshot.getLastAppliedTerm(), + context.getTermInformation().getCurrentTerm(), + context.getTermInformation().getVotedFor()); - context.getPersistenceProvider().saveSnapshot(sn); + context.getPersistenceProvider().saveSnapshot(snapshot); - LOG.info("{}: Persisting of snapshot done:{}", persistenceId(), sn.getLogMessage()); + LOG.info("{}: Persisting of snapshot done: {}", persistenceId(), snapshot); long dataThreshold = totalMemory * context.getConfigParams().getSnapshotDataThresholdPercentage() / 100; - if (context.getReplicatedLog().dataSize() > dataThreshold) { + boolean dataSizeThresholdExceeded = context.getReplicatedLog().dataSize() > dataThreshold; + + boolean logSizeExceededSnapshotBatchCount = + context.getReplicatedLog().size() >= context.getConfigParams().getSnapshotBatchCount(); + if (dataSizeThresholdExceeded || logSizeExceededSnapshotBatchCount) { if(LOG.isDebugEnabled()) { - LOG.debug("{}: dataSize {} exceeds dataThreshold {} - doing snapshotPreCommit with index {}", - persistenceId(), context.getReplicatedLog().dataSize(), dataThreshold, - captureSnapshot.getLastAppliedIndex()); + 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()); + } } - // if memory is less, clear the log based on lastApplied. - // this could/should only happen if one of the followers is down - // as normally we keep removing from the log when its replicated to all. + // We either exceeded the memory threshold or the log size exceeded the snapshot batch + // count so, to keep the log memory footprint in check, clear the log based on lastApplied. + // This could/should only happen if one of the followers is down as normally we keep + // removing from the log as entries are replicated to all. context.getReplicatedLog().snapshotPreCommit(captureSnapshot.getLastAppliedIndex(), captureSnapshot.getLastAppliedTerm()); @@ -293,15 +354,14 @@ public class SnapshotManager implements SnapshotState { context.getReplicatedLog().getSnapshotTerm()); } - LOG.info("{}: Removed in-memory snapshotted entries, adjusted snaphsotIndex:{} " + - "and term:{}", persistenceId(), captureSnapshot.getLastAppliedIndex(), - captureSnapshot.getLastAppliedTerm()); + 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( - ByteString.copyFrom(snapshotBytes))); + currentBehavior.handleMessage(context.getActor(), new SendInstallSnapshot(snapshot)); } captureSnapshot = null; @@ -318,27 +378,54 @@ public class SnapshotManager implements SnapshotState { private class Persisting extends AbstractSnapshotState { @Override - public void commit(long sequenceNumber) { - context.getReplicatedLog().snapshotCommit(); + public void commit(long sequenceNumber, RaftActorBehavior currentBehavior) { + LOG.debug("Snapshot success sequence number: {}", sequenceNumber); + + if(applySnapshot != null) { + try { + Snapshot snapshot = applySnapshot.getSnapshot(); + applySnapshotProcedure.apply(snapshot.getState()); + + //clears the followers log, sets the snapshot index to ensure adjusted-index works + context.setReplicatedLog(ReplicatedLogImpl.newInstance(snapshot, context, currentBehavior)); + context.setLastApplied(snapshot.getLastAppliedIndex()); + context.setCommitIndex(snapshot.getLastAppliedIndex()); + + applySnapshot.getCallback().onSuccess(); + } catch (Exception e) { + LOG.error("Error applying snapshot", e); + } + } else { + context.getReplicatedLog().snapshotCommit(); + } + context.getPersistenceProvider().deleteSnapshots(new SnapshotSelectionCriteria( sequenceNumber - context.getConfigParams().getSnapshotBatchCount(), 43200000)); context.getPersistenceProvider().deleteMessages(lastSequenceNumber); lastSequenceNumber = -1; + applySnapshot = null; SnapshotManager.this.currentState = IDLE; } @Override public void rollback() { - context.getReplicatedLog().snapshotRollback(); - - 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()); + // Nothing to rollback if we're applying a snapshot from the leader. + 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(), + context.getReplicatedLog().getSnapshotIndex(), + context.getReplicatedLog().getSnapshotTerm(), + context.getReplicatedLog().size()); + } else { + applySnapshot.getCallback().onFailure(); + } + lastSequenceNumber = -1; + applySnapshot = null; SnapshotManager.this.currentState = IDLE; } @@ -365,6 +452,8 @@ public class SnapshotManager implements SnapshotState { this.term = -1L; if (!hasFollowers) { 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(); term = lastLogEntry.getTerm(); }