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.DataPersistenceProvider;
14 import org.opendaylight.controller.cluster.raft.base.messages.DeleteEntries;
15 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
18 * Implementation of ReplicatedLog used by the RaftActor.
20 class ReplicatedLogImpl extends AbstractReplicatedLogImpl {
21 private static final int DATA_SIZE_DIVIDER = 5;
23 private long dataSizeSinceLastSnapshot = 0L;
24 private final RaftActorContext context;
25 private final DataPersistenceProvider persistence;
26 private final RaftActorBehavior currentBehavior;
28 private final Procedure<DeleteEntries> deleteProcedure = new Procedure<DeleteEntries>() {
30 public void apply(DeleteEntries notUsed) {
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);
40 static ReplicatedLog newInstance(RaftActorContext context,
41 DataPersistenceProvider persistence, RaftActorBehavior currentBehavior) {
42 return new ReplicatedLogImpl(-1L, -1L, Collections.<ReplicatedLogEntry>emptyList(), context,
43 persistence, currentBehavior);
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;
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);
64 public void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry) {
65 appendAndPersist(replicatedLogEntry, null);
69 public void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry,
70 final Procedure<ReplicatedLogEntry> callback) {
72 if(context.getLogger().isDebugEnabled()) {
73 context.getLogger().debug("{}: Append log entry and persist {} ", context.getId(), replicatedLogEntry);
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);
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>() {
87 public void apply(ReplicatedLogEntry evt) throws Exception {
88 int logEntrySize = replicatedLogEntry.size();
90 long dataSizeForCheck = dataSize();
92 dataSizeSinceLastSnapshot += logEntrySize;
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
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;
108 long journalSize = replicatedLogEntry.getIndex() + 1;
109 long dataThreshold = context.getTotalMemory() *
110 context.getConfigParams().getSnapshotDataThresholdPercentage() / 100;
112 if ((journalSize % context.getConfigParams().getSnapshotBatchCount() == 0
113 || dataSizeForCheck > dataThreshold)) {
115 boolean started = context.getSnapshotManager().capture(replicatedLogEntry,
116 currentBehavior.getReplicatedToAllIndex());
119 dataSizeSinceLastSnapshot = 0;
123 if (callback != null){
124 callback.apply(replicatedLogEntry);