2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.raft;
10 import akka.japi.Procedure;
11 import com.google.common.base.Preconditions;
12 import java.util.Collections;
13 import java.util.List;
14 import org.opendaylight.controller.cluster.raft.base.messages.DeleteEntries;
17 * Implementation of ReplicatedLog used by the RaftActor.
19 class ReplicatedLogImpl extends AbstractReplicatedLogImpl {
20 private static final int DATA_SIZE_DIVIDER = 5;
22 private long dataSizeSinceLastSnapshot = 0L;
23 private final RaftActorContext context;
25 private final Procedure<DeleteEntries> deleteProcedure = new Procedure<DeleteEntries>() {
27 public void apply(final DeleteEntries notUsed) {
31 static ReplicatedLog newInstance(final Snapshot snapshot, final RaftActorContext context) {
32 return new ReplicatedLogImpl(snapshot.getLastAppliedIndex(), snapshot.getLastAppliedTerm(),
33 snapshot.getUnAppliedEntries(), context);
36 static ReplicatedLog newInstance(final RaftActorContext context) {
37 return new ReplicatedLogImpl(-1L, -1L, Collections.<ReplicatedLogEntry>emptyList(), context);
40 private ReplicatedLogImpl(final long snapshotIndex, final long snapshotTerm, final List<ReplicatedLogEntry> unAppliedEntries,
41 final RaftActorContext context) {
42 super(snapshotIndex, snapshotTerm, unAppliedEntries);
43 this.context = Preconditions.checkNotNull(context);
47 public void removeFromAndPersist(final long logEntryIndex) {
48 // FIXME: Maybe this should be done after the command is saved
49 long adjustedIndex = removeFrom(logEntryIndex);
50 if(adjustedIndex >= 0) {
51 context.getPersistenceProvider().persist(new DeleteEntries(adjustedIndex), deleteProcedure);
56 public void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry) {
57 appendAndPersist(replicatedLogEntry, null);
61 public void captureSnapshotIfReady(final ReplicatedLogEntry replicatedLogEntry) {
62 long journalSize = replicatedLogEntry.getIndex() + 1;
63 long dataThreshold = context.getTotalMemory() *
64 context.getConfigParams().getSnapshotDataThresholdPercentage() / 100;
66 if ((journalSize % context.getConfigParams().getSnapshotBatchCount() == 0
67 || getDataSizeForSnapshotCheck() > dataThreshold)) {
69 boolean started = context.getSnapshotManager().capture(replicatedLogEntry,
70 context.getCurrentBehavior().getReplicatedToAllIndex());
72 if (!context.hasFollowers()) {
73 dataSizeSinceLastSnapshot = 0;
79 private long getDataSizeForSnapshotCheck() {
80 long dataSizeForCheck = dataSize();
81 if (!context.hasFollowers()) {
82 // When we do not have followers we do not maintain an in-memory log
83 // due to this the journalSize will never become anything close to the
84 // snapshot batch count. In fact will mostly be 1.
85 // Similarly since the journal's dataSize depends on the entries in the
86 // journal the journal's dataSize will never reach a value close to the
88 // By maintaining the dataSize outside the journal we are tracking essentially
89 // what we have written to the disk however since we no longer are in
90 // need of doing a snapshot just for the sake of freeing up memory we adjust
91 // the real size of data by the DATA_SIZE_DIVIDER so that we do not snapshot as often
92 // as if we were maintaining a real snapshot
93 dataSizeForCheck = dataSizeSinceLastSnapshot / DATA_SIZE_DIVIDER;
95 return dataSizeForCheck;
99 public void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry,
100 final Procedure<ReplicatedLogEntry> callback) {
102 if (context.getLogger().isDebugEnabled()) {
103 context.getLogger().debug("{}: Append log entry and persist {} ", context.getId(), replicatedLogEntry);
106 // FIXME : By adding the replicated log entry to the in-memory journal we are not truly ensuring durability of the logs
107 append(replicatedLogEntry);
109 // When persisting events with persist it is guaranteed that the
110 // persistent actor will not receive further commands between the
111 // persist call and the execution(s) of the associated event
112 // handler. This also holds for multiple persist calls in context
113 // of a single command.
114 context.getPersistenceProvider().persist(replicatedLogEntry,
115 new Procedure<ReplicatedLogEntry>() {
117 public void apply(final ReplicatedLogEntry param) throws Exception {
118 context.getLogger().debug("{}: persist complete {}", context.getId(), param);
120 int logEntrySize = param.size();
121 dataSizeSinceLastSnapshot += logEntrySize;
123 if (callback != null) {
124 callback.apply(param);