X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FReplicatedLogImpl.java;h=6b4427d5ce39078c772e0ea5f33953cc421712c8;hb=36372c716205959311bb86256872cf9c7b2d350b;hp=c32839c490eb0b53d69192b2c58f4a141fe422f0;hpb=dea515c8870769408b9bea29f555d6b71ff43211;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/ReplicatedLogImpl.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/ReplicatedLogImpl.java index c32839c490..6b4427d5ce 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/ReplicatedLogImpl.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/ReplicatedLogImpl.java @@ -8,6 +8,7 @@ package org.opendaylight.controller.cluster.raft; import akka.japi.Procedure; +import com.google.common.base.Preconditions; import java.util.Collections; import java.util.List; import org.opendaylight.controller.cluster.raft.base.messages.DeleteEntries; @@ -25,30 +26,30 @@ class ReplicatedLogImpl extends AbstractReplicatedLogImpl { private final Procedure deleteProcedure = new Procedure() { @Override - public void apply(DeleteEntries notUsed) { + public void apply(final DeleteEntries notUsed) { } }; - static ReplicatedLog newInstance(Snapshot snapshot, RaftActorContext context, - RaftActorBehavior currentBehavior) { + static ReplicatedLog newInstance(final Snapshot snapshot, final RaftActorContext context, + final RaftActorBehavior currentBehavior) { return new ReplicatedLogImpl(snapshot.getLastAppliedIndex(), snapshot.getLastAppliedTerm(), snapshot.getUnAppliedEntries(), context, currentBehavior); } - static ReplicatedLog newInstance(RaftActorContext context, RaftActorBehavior currentBehavior) { + static ReplicatedLog newInstance(final RaftActorContext context, final RaftActorBehavior currentBehavior) { return new ReplicatedLogImpl(-1L, -1L, Collections.emptyList(), context, currentBehavior); } - private ReplicatedLogImpl(long snapshotIndex, long snapshotTerm, List unAppliedEntries, - RaftActorContext context, RaftActorBehavior currentBehavior) { + private ReplicatedLogImpl(final long snapshotIndex, final long snapshotTerm, final List unAppliedEntries, + final RaftActorContext context, final RaftActorBehavior currentBehavior) { super(snapshotIndex, snapshotTerm, unAppliedEntries); - this.context = context; - this.currentBehavior = currentBehavior; + this.context = Preconditions.checkNotNull(context); + this.currentBehavior = Preconditions.checkNotNull(currentBehavior); } @Override - public void removeFromAndPersist(long logEntryIndex) { + public void removeFromAndPersist(final long logEntryIndex) { // FIXME: Maybe this should be done after the command is saved long adjustedIndex = removeFrom(logEntryIndex); if(adjustedIndex >= 0) { @@ -61,11 +62,49 @@ class ReplicatedLogImpl extends AbstractReplicatedLogImpl { appendAndPersist(replicatedLogEntry, null); } + @Override + public void captureSnapshotIfReady(final ReplicatedLogEntry replicatedLogEntry) { + long journalSize = replicatedLogEntry.getIndex() + 1; + long dataThreshold = context.getTotalMemory() * + context.getConfigParams().getSnapshotDataThresholdPercentage() / 100; + + if ((journalSize % context.getConfigParams().getSnapshotBatchCount() == 0 + || getDataSizeForSnapshotCheck() > dataThreshold)) { + + boolean started = context.getSnapshotManager().capture(replicatedLogEntry, + currentBehavior.getReplicatedToAllIndex()); + if (started) { + if (!context.hasFollowers()) { + dataSizeSinceLastSnapshot = 0; + } + } + } + } + + private long getDataSizeForSnapshotCheck() { + long dataSizeForCheck = dataSize(); + if (!context.hasFollowers()) { + // When we do not have followers we do not maintain an in-memory log + // due to this the journalSize will never become anything close to the + // snapshot batch count. In fact will mostly be 1. + // Similarly since the journal's dataSize depends on the entries in the + // journal the journal's dataSize will never reach a value close to the + // memory threshold. + // By maintaining the dataSize outside the journal we are tracking essentially + // what we have written to the disk however since we no longer are in + // need of doing a snapshot just for the sake of freeing up memory we adjust + // the real size of data by the DATA_SIZE_DIVIDER so that we do not snapshot as often + // as if we were maintaining a real snapshot + dataSizeForCheck = dataSizeSinceLastSnapshot / DATA_SIZE_DIVIDER; + } + return dataSizeForCheck; + } + @Override public void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry, final Procedure callback) { - if(context.getLogger().isDebugEnabled()) { + if (context.getLogger().isDebugEnabled()) { context.getLogger().debug("{}: Append log entry and persist {} ", context.getId(), replicatedLogEntry); } @@ -80,44 +119,14 @@ class ReplicatedLogImpl extends AbstractReplicatedLogImpl { context.getPersistenceProvider().persist(replicatedLogEntry, new Procedure() { @Override - public void apply(ReplicatedLogEntry evt) throws Exception { - int logEntrySize = replicatedLogEntry.size(); - - long dataSizeForCheck = dataSize(); + public void apply(final ReplicatedLogEntry param) throws Exception { + context.getLogger().debug("{}: persist complete {}", context.getId(), param); + int logEntrySize = param.size(); dataSizeSinceLastSnapshot += logEntrySize; - if (!context.hasFollowers()) { - // When we do not have followers we do not maintain an in-memory log - // due to this the journalSize will never become anything close to the - // snapshot batch count. In fact will mostly be 1. - // Similarly since the journal's dataSize depends on the entries in the - // journal the journal's dataSize will never reach a value close to the - // memory threshold. - // By maintaining the dataSize outside the journal we are tracking essentially - // what we have written to the disk however since we no longer are in - // need of doing a snapshot just for the sake of freeing up memory we adjust - // the real size of data by the DATA_SIZE_DIVIDER so that we do not snapshot as often - // as if we were maintaining a real snapshot - dataSizeForCheck = dataSizeSinceLastSnapshot / DATA_SIZE_DIVIDER; - } - long journalSize = replicatedLogEntry.getIndex() + 1; - long dataThreshold = context.getTotalMemory() * - context.getConfigParams().getSnapshotDataThresholdPercentage() / 100; - - if ((journalSize % context.getConfigParams().getSnapshotBatchCount() == 0 - || dataSizeForCheck > dataThreshold)) { - - boolean started = context.getSnapshotManager().capture(replicatedLogEntry, - currentBehavior.getReplicatedToAllIndex()); - - if(started){ - dataSizeSinceLastSnapshot = 0; - } - } - - if (callback != null){ - callback.apply(replicatedLogEntry); + if (callback != null) { + callback.apply(param); } } }