90042907a5f79e2d13d590af19d3782a8a269548
[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.base.messages.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 long dataSizeSinceLastSnapshot = 0L;
23     private final RaftActorContext context;
24
25     private final Procedure<DeleteEntries> deleteProcedure = new Procedure<DeleteEntries>() {
26         @Override
27         public void apply(final DeleteEntries notUsed) {
28         }
29     };
30
31     static ReplicatedLog newInstance(final Snapshot snapshot, final RaftActorContext context) {
32         return new ReplicatedLogImpl(snapshot.getLastAppliedIndex(), snapshot.getLastAppliedTerm(),
33                 snapshot.getUnAppliedEntries(), context);
34     }
35
36     static ReplicatedLog newInstance(final RaftActorContext context) {
37         return new ReplicatedLogImpl(-1L, -1L, Collections.<ReplicatedLogEntry>emptyList(), context);
38     }
39
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);
44     }
45
46     @Override
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);
52         }
53     }
54
55     @Override
56     public void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry) {
57         appendAndPersist(replicatedLogEntry, null);
58     }
59
60     @Override
61     public void captureSnapshotIfReady(final ReplicatedLogEntry replicatedLogEntry) {
62         long journalSize = replicatedLogEntry.getIndex() + 1;
63         long dataThreshold = context.getTotalMemory() *
64                 context.getConfigParams().getSnapshotDataThresholdPercentage() / 100;
65
66         if ((journalSize % context.getConfigParams().getSnapshotBatchCount() == 0
67                 || getDataSizeForSnapshotCheck() > dataThreshold)) {
68
69             boolean started = context.getSnapshotManager().capture(replicatedLogEntry,
70                     context.getCurrentBehavior().getReplicatedToAllIndex());
71             if (started) {
72                 if (!context.hasFollowers()) {
73                     dataSizeSinceLastSnapshot = 0;
74                 }
75             }
76         }
77     }
78
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
87             // memory threshold.
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;
94         }
95         return dataSizeForCheck;
96     }
97
98     @Override
99     public void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry,
100             final Procedure<ReplicatedLogEntry> callback)  {
101
102         if (context.getLogger().isDebugEnabled()) {
103             context.getLogger().debug("{}: Append log entry and persist {} ", context.getId(), replicatedLogEntry);
104         }
105
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);
108
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>() {
116                 @Override
117                 public void apply(final ReplicatedLogEntry param) throws Exception {
118                     context.getLogger().debug("{}: persist complete {}", context.getId(), param);
119
120                     int logEntrySize = param.size();
121                     dataSizeSinceLastSnapshot += logEntrySize;
122
123                     if (callback != null) {
124                         callback.apply(param);
125                     }
126                 }
127             }
128         );
129     }
130 }