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 appendAndPersist(final ReplicatedLogEntry replicatedLogEntry,
66 final Procedure<ReplicatedLogEntry> callback) {
68 if(context.getLogger().isDebugEnabled()) {
69 context.getLogger().debug("{}: Append log entry and persist {} ", context.getId(), replicatedLogEntry);
72 // FIXME : By adding the replicated log entry to the in-memory journal we are not truly ensuring durability of the logs
73 append(replicatedLogEntry);
75 // When persisting events with persist it is guaranteed that the
76 // persistent actor will not receive further commands between the
77 // persist call and the execution(s) of the associated event
78 // handler. This also holds for multiple persist calls in context
79 // of a single command.
80 context.getPersistenceProvider().persist(replicatedLogEntry,
81 new Procedure<ReplicatedLogEntry>() {
83 public void apply(ReplicatedLogEntry evt) throws Exception {
84 context.getLogger().debug("{}: persist complete {}", context.getId(), replicatedLogEntry);
86 int logEntrySize = replicatedLogEntry.size();
88 long dataSizeForCheck = dataSize();
90 dataSizeSinceLastSnapshot += logEntrySize;
92 if (!context.hasFollowers()) {
93 // When we do not have followers we do not maintain an in-memory log
94 // due to this the journalSize will never become anything close to the
95 // snapshot batch count. In fact will mostly be 1.
96 // Similarly since the journal's dataSize depends on the entries in the
97 // journal the journal's dataSize will never reach a value close to the
99 // By maintaining the dataSize outside the journal we are tracking essentially
100 // what we have written to the disk however since we no longer are in
101 // need of doing a snapshot just for the sake of freeing up memory we adjust
102 // the real size of data by the DATA_SIZE_DIVIDER so that we do not snapshot as often
103 // as if we were maintaining a real snapshot
104 dataSizeForCheck = dataSizeSinceLastSnapshot / DATA_SIZE_DIVIDER;
106 long journalSize = replicatedLogEntry.getIndex() + 1;
107 long dataThreshold = context.getTotalMemory() *
108 context.getConfigParams().getSnapshotDataThresholdPercentage() / 100;
110 if ((journalSize % context.getConfigParams().getSnapshotBatchCount() == 0
111 || dataSizeForCheck > dataThreshold)) {
113 boolean started = context.getSnapshotManager().capture(replicatedLogEntry,
114 currentBehavior.getReplicatedToAllIndex());
117 dataSizeSinceLastSnapshot = 0;
121 if (callback != null){
122 callback.apply(replicatedLogEntry);