*/
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 javax.annotation.Nonnull;
-import javax.annotation.Nullable;
+import java.util.function.Consumer;
import org.opendaylight.controller.cluster.raft.persisted.DeleteEntries;
import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
final List<ReplicatedLogEntry> 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) {
@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), NoopProcedure.instance());
+ context.getPersistenceProvider().persist(new DeleteEntries(logEntryIndex), NoopProcedure.instance());
return true;
}
@Override
public boolean shouldCaptureSnapshot(final long logIndex) {
final ConfigParams config = context.getConfigParams();
- final long journalSize = logIndex + 1;
- final long dataThreshold = context.getTotalMemory() * config.getSnapshotDataThresholdPercentage() / 100;
+ if ((logIndex + 1) % config.getSnapshotBatchCount() == 0) {
+ return true;
+ }
- return journalSize % config.getSnapshotBatchCount() == 0 || getDataSizeForSnapshotCheck() > dataThreshold;
+ final long absoluteThreshold = config.getSnapshotDataThreshold();
+ final long dataThreshold = absoluteThreshold != 0 ? absoluteThreshold * ConfigParams.MEGABYTE
+ : context.getTotalMemory() * config.getSnapshotDataThresholdPercentage() / 100;
+ return getDataSizeForSnapshotCheck() > dataThreshold;
}
@Override
}
@Override
- public boolean appendAndPersist(@Nonnull final ReplicatedLogEntry replicatedLogEntry,
- @Nullable final Procedure<ReplicatedLogEntry> callback, final boolean doAsync) {
+ public boolean appendAndPersist(final ReplicatedLogEntry replicatedLogEntry,
+ final Consumer<ReplicatedLogEntry> callback, final boolean doAsync) {
context.getLogger().debug("{}: Append log entry and persist {} ", context.getId(), replicatedLogEntry);
return false;
}
- Procedure<ReplicatedLogEntry> persistCallback = persistedLogEntry -> {
- context.getLogger().debug("{}: persist complete {}", context.getId(), persistedLogEntry);
-
- dataSizeSinceLastSnapshot += persistedLogEntry.size();
-
- if (callback != null) {
- callback.apply(persistedLogEntry);
- }
- };
-
if (doAsync) {
- context.getPersistenceProvider().persistAsync(replicatedLogEntry, persistCallback);
+ context.getPersistenceProvider().persistAsync(replicatedLogEntry,
+ entry -> persistCallback(entry, callback));
} else {
- context.getPersistenceProvider().persist(replicatedLogEntry, persistCallback);
+ context.getPersistenceProvider().persist(replicatedLogEntry, entry -> syncPersistCallback(entry, callback));
}
return true;
}
+
+ private void persistCallback(final ReplicatedLogEntry persistedLogEntry,
+ final Consumer<ReplicatedLogEntry> callback) {
+ context.getExecutor().execute(() -> syncPersistCallback(persistedLogEntry, callback));
+ }
+
+ private void syncPersistCallback(final ReplicatedLogEntry persistedLogEntry,
+ final Consumer<ReplicatedLogEntry> callback) {
+ context.getLogger().debug("{}: persist complete {}", context.getId(), persistedLogEntry);
+
+ dataSizeSinceLastSnapshot += persistedLogEntry.size();
+
+ if (callback != null) {
+ callback.accept(persistedLogEntry);
+ }
+ }
}