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