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