X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FSnapshotManager.java;h=26d8c0af084a5233437896539df7d5db6f439ad8;hb=5aa58404a8ee1ad053742780439823309360a3a1;hp=77875a567ed45e2552f56b23e5bac2a70a35d47d;hpb=ed7166c5ba3a378f46640bb479464580c2b167ba;p=controller.git 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 77875a567e..26d8c0af08 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 @@ -12,6 +12,7 @@ import akka.japi.Procedure; import akka.persistence.SnapshotSelectionCriteria; import com.google.common.annotations.VisibleForTesting; 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; @@ -37,7 +38,7 @@ public class SnapshotManager implements SnapshotState { private Procedure createSnapshotProcedure; - private Snapshot applySnapshot; + private ApplySnapshot applySnapshot; private Procedure applySnapshotProcedure; public SnapshotManager(RaftActorContext context, Logger logger) { @@ -45,6 +46,10 @@ public class SnapshotManager implements SnapshotState { this.LOG = logger; } + public boolean isApplying() { + return applySnapshot != null; + } + @Override public boolean isCapturing() { return currentState.isCapturing(); @@ -61,7 +66,7 @@ public class SnapshotManager implements SnapshotState { } @Override - public void apply(Snapshot snapshot) { + public void apply(ApplySnapshot snapshot) { currentState.apply(snapshot); } @@ -103,7 +108,7 @@ public class SnapshotManager implements SnapshotState { } private boolean hasFollowers(){ - return context.getPeerAddresses().keySet().size() > 0; + return context.hasFollowers(); } private String persistenceId(){ @@ -114,7 +119,7 @@ public class SnapshotManager implements SnapshotState { @Override public boolean isCapturing() { - return false; + return true; } @Override @@ -130,7 +135,7 @@ public class SnapshotManager implements SnapshotState { } @Override - public void apply(Snapshot snapshot) { + public void apply(ApplySnapshot snapshot) { LOG.debug("apply should not be called in state {}", this); } @@ -188,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(), @@ -206,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()) { @@ -245,14 +265,14 @@ public class SnapshotManager implements SnapshotState { } @Override - public void apply(Snapshot snapshot) { - applySnapshot = snapshot; + 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(snapshot); + context.getPersistenceProvider().saveSnapshot(applySnapshot.getSnapshot()); SnapshotManager.this.currentState = PERSISTING; } @@ -270,11 +290,6 @@ 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 @@ -283,25 +298,38 @@ public class SnapshotManager implements SnapshotState { 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(snapshot); - LOG.info("{}: Persisting of snapshot done:{}", persistenceId(), snapshot.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()); @@ -326,9 +354,9 @@ 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()) { @@ -351,16 +379,19 @@ public class SnapshotManager implements SnapshotState { @Override public void commit(long sequenceNumber, RaftActorBehavior currentBehavior) { - LOG.debug("Snapshot success sequence number:", sequenceNumber); + LOG.debug("Snapshot success sequence number: {}", sequenceNumber); if(applySnapshot != null) { try { - applySnapshotProcedure.apply(applySnapshot.getState()); + 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(applySnapshot, context, currentBehavior)); - context.setLastApplied(applySnapshot.getLastAppliedIndex()); - context.setCommitIndex(applySnapshot.getLastAppliedIndex()); + 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); } @@ -389,6 +420,8 @@ public class SnapshotManager implements SnapshotState { context.getReplicatedLog().getSnapshotIndex(), context.getReplicatedLog().getSnapshotTerm(), context.getReplicatedLog().size()); + } else { + applySnapshot.getCallback().onFailure(); } lastSequenceNumber = -1;