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=66d7b352a22f3ec94a896104098d3145c4fc30d5;hb=66e956cbf397a86f1a512396feb7cb87ea602f92;hp=9123c14d5d615553a2abf58d19e9a1d89ad63f39;hpb=b0f8283587b5cc8573d29f66219cbe7f70e21e1b;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 9123c14d5d..66d7b352a2 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 @@ -7,20 +7,20 @@ */ package org.opendaylight.controller.cluster.raft; -import akka.japi.Procedure; -import com.google.common.base.Preconditions; +import static java.util.Objects.requireNonNull; + import java.util.Collections; import java.util.List; +import java.util.function.Consumer; import org.opendaylight.controller.cluster.raft.persisted.DeleteEntries; +import org.opendaylight.controller.cluster.raft.persisted.Snapshot; /** * Implementation of ReplicatedLog used by the RaftActor. */ -class ReplicatedLogImpl extends AbstractReplicatedLogImpl { +final class ReplicatedLogImpl extends AbstractReplicatedLogImpl { private static final int DATA_SIZE_DIVIDER = 5; - private final Procedure deleteProcedure = NoopProcedure.instance(); - private final RaftActorContext context; private long dataSizeSinceLastSnapshot = 0L; @@ -28,7 +28,7 @@ class ReplicatedLogImpl extends AbstractReplicatedLogImpl { final List unAppliedEntries, final RaftActorContext context) { super(snapshotIndex, snapshotTerm, unAppliedEntries, context.getId()); - this.context = Preconditions.checkNotNull(context); + this.context = requireNonNull(context); } static ReplicatedLog newInstance(final Snapshot snapshot, final RaftActorContext context) { @@ -42,10 +42,9 @@ class ReplicatedLogImpl extends AbstractReplicatedLogImpl { @Override public boolean removeFromAndPersist(final long logEntryIndex) { - // FIXME: Maybe this should be done after the command is saved long adjustedIndex = removeFrom(logEntryIndex); if (adjustedIndex >= 0) { - context.getPersistenceProvider().persist(new DeleteEntries(adjustedIndex), deleteProcedure); + context.getPersistenceProvider().persist(new DeleteEntries(logEntryIndex), NoopProcedure.instance()); return true; } @@ -53,12 +52,17 @@ class ReplicatedLogImpl extends AbstractReplicatedLogImpl { } @Override - public void captureSnapshotIfReady(final ReplicatedLogEntry replicatedLogEntry) { + public boolean shouldCaptureSnapshot(final long logIndex) { final ConfigParams config = context.getConfigParams(); - final long journalSize = replicatedLogEntry.getIndex() + 1; + final long journalSize = logIndex + 1; final long dataThreshold = context.getTotalMemory() * config.getSnapshotDataThresholdPercentage() / 100; - if (journalSize % config.getSnapshotBatchCount() == 0 || getDataSizeForSnapshotCheck() > dataThreshold) { + return journalSize % config.getSnapshotBatchCount() == 0 || getDataSizeForSnapshotCheck() > dataThreshold; + } + + @Override + public void captureSnapshotIfReady(final ReplicatedLogEntry replicatedLogEntry) { + if (shouldCaptureSnapshot(replicatedLogEntry.getIndex())) { boolean started = context.getSnapshotManager().capture(replicatedLogEntry, context.getCurrentBehavior().getReplicatedToAllIndex()); if (started && !context.hasFollowers()) { @@ -87,38 +91,38 @@ class ReplicatedLogImpl extends AbstractReplicatedLogImpl { } @Override - public void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry) { - appendAndPersist(replicatedLogEntry, null); - } - - @Override - public void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry, - final Procedure callback) { + public boolean appendAndPersist(final ReplicatedLogEntry replicatedLogEntry, + final Consumer callback, final boolean doAsync) { context.getLogger().debug("{}: Append log entry and persist {} ", context.getId(), replicatedLogEntry); - // FIXME : By adding the replicated log entry to the in-memory journal we are not truly ensuring durability - // of the logs if (!append(replicatedLogEntry)) { - return; + return false; } - // When persisting events with persist it is guaranteed that the - // persistent actor will not receive further commands between the - // persist call and the execution(s) of the associated event - // handler. This also holds for multiple persist calls in context - // of a single command. - context.getPersistenceProvider().persist(replicatedLogEntry, - param -> { - context.getLogger().debug("{}: persist complete {}", context.getId(), param); - - int logEntrySize = param.size(); - dataSizeSinceLastSnapshot += logEntrySize; - - if (callback != null) { - callback.apply(param); - } - } - ); + if (doAsync) { + context.getPersistenceProvider().persistAsync(replicatedLogEntry, + entry -> persistCallback(entry, callback)); + } else { + context.getPersistenceProvider().persist(replicatedLogEntry, entry -> syncPersistCallback(entry, callback)); + } + + return true; + } + + private void persistCallback(final ReplicatedLogEntry persistedLogEntry, + final Consumer callback) { + context.getExecutor().execute(() -> syncPersistCallback(persistedLogEntry, callback)); + } + + private void syncPersistCallback(final ReplicatedLogEntry persistedLogEntry, + final Consumer callback) { + context.getLogger().debug("{}: persist complete {}", context.getId(), persistedLogEntry); + + dataSizeSinceLastSnapshot += persistedLogEntry.size(); + + if (callback != null) { + callback.accept(persistedLogEntry); + } } }