Fix warnings and javadocs in sal-akka-raft
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / ReplicatedLogImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.raft;
9
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.persisted.DeleteEntries;
15
16 /**
17  * Implementation of ReplicatedLog used by the RaftActor.
18  */
19 class ReplicatedLogImpl extends AbstractReplicatedLogImpl {
20     private static final int DATA_SIZE_DIVIDER = 5;
21
22     private final Procedure<DeleteEntries> deleteProcedure = NoopProcedure.instance();
23
24     private final RaftActorContext context;
25     private long dataSizeSinceLastSnapshot = 0L;
26
27     private ReplicatedLogImpl(final long snapshotIndex, final long snapshotTerm, final List<ReplicatedLogEntry> unAppliedEntries,
28             final RaftActorContext context) {
29         super(snapshotIndex, snapshotTerm, unAppliedEntries, context.getId());
30         this.context = Preconditions.checkNotNull(context);
31     }
32
33     static ReplicatedLog newInstance(final Snapshot snapshot, final RaftActorContext context) {
34         return new ReplicatedLogImpl(snapshot.getLastAppliedIndex(), snapshot.getLastAppliedTerm(),
35                 snapshot.getUnAppliedEntries(), context);
36     }
37
38     static ReplicatedLog newInstance(final RaftActorContext context) {
39         return new ReplicatedLogImpl(-1L, -1L, Collections.<ReplicatedLogEntry>emptyList(), context);
40     }
41
42     @Override
43     public boolean removeFromAndPersist(final long logEntryIndex) {
44         // FIXME: Maybe this should be done after the command is saved
45         long adjustedIndex = removeFrom(logEntryIndex);
46         if(adjustedIndex >= 0) {
47             context.getPersistenceProvider().persist(new DeleteEntries(adjustedIndex), deleteProcedure);
48             return true;
49         }
50
51         return false;
52     }
53
54     @Override
55     public void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry) {
56         appendAndPersist(replicatedLogEntry, null);
57     }
58
59     @Override
60     public void captureSnapshotIfReady(final ReplicatedLogEntry replicatedLogEntry) {
61         final ConfigParams config = context.getConfigParams();
62         final long journalSize = replicatedLogEntry.getIndex() + 1;
63         final long dataThreshold = context.getTotalMemory() * config.getSnapshotDataThresholdPercentage() / 100;
64
65         if (journalSize % config.getSnapshotBatchCount() == 0 || getDataSizeForSnapshotCheck() > dataThreshold) {
66             boolean started = context.getSnapshotManager().capture(replicatedLogEntry,
67                     context.getCurrentBehavior().getReplicatedToAllIndex());
68             if (started && !context.hasFollowers()) {
69                 dataSizeSinceLastSnapshot = 0;
70             }
71         }
72     }
73
74     private long getDataSizeForSnapshotCheck() {
75         if (!context.hasFollowers()) {
76             // When we do not have followers we do not maintain an in-memory log
77             // due to this the journalSize will never become anything close to the
78             // snapshot batch count. In fact will mostly be 1.
79             // Similarly since the journal's dataSize depends on the entries in the
80             // journal the journal's dataSize will never reach a value close to the
81             // memory threshold.
82             // By maintaining the dataSize outside the journal we are tracking essentially
83             // what we have written to the disk however since we no longer are in
84             // need of doing a snapshot just for the sake of freeing up memory we adjust
85             // the real size of data by the DATA_SIZE_DIVIDER so that we do not snapshot as often
86             // as if we were maintaining a real snapshot
87             return dataSizeSinceLastSnapshot / DATA_SIZE_DIVIDER;
88         } else {
89             return dataSize();
90         }
91     }
92
93     @Override
94     public void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry,
95             final Procedure<ReplicatedLogEntry> callback)  {
96
97         context.getLogger().debug("{}: Append log entry and persist {} ", context.getId(), replicatedLogEntry);
98
99         // FIXME : By adding the replicated log entry to the in-memory journal we are not truly ensuring durability of the logs
100         if(!append(replicatedLogEntry)) {
101             return;
102         }
103
104         // When persisting events with persist it is guaranteed that the
105         // persistent actor will not receive further commands between the
106         // persist call and the execution(s) of the associated event
107         // handler. This also holds for multiple persist calls in context
108         // of a single command.
109         context.getPersistenceProvider().persist(replicatedLogEntry,
110             param -> {
111                 context.getLogger().debug("{}: persist complete {}", context.getId(), param);
112
113                 int logEntrySize = param.size();
114                 dataSizeSinceLastSnapshot += logEntrySize;
115
116                 if (callback != null) {
117                     callback.apply(param);
118                 }
119             }
120         );
121     }
122 }