2 * Copyright (c) 2014 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
9 package org.opendaylight.controller.cluster.raft;
11 import akka.japi.Procedure;
12 import java.util.List;
13 import javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
17 * Represents the ReplicatedLog that needs to be kept in sync by the RaftActor
19 public interface ReplicatedLog {
20 long NO_MAX_SIZE = -1;
23 * Return the replicated log entry at the specified index.
25 * @param index the index of the log entry
26 * @return the ReplicatedLogEntry if found, otherwise null if the adjusted index less than 0 or
27 * greater than the size of the in-memory journal.
30 ReplicatedLogEntry get(long index);
33 * Return the last replicated log entry in the log or null of not found.
35 * @return the last replicated log entry in the log or null of not found.
38 ReplicatedLogEntry last();
41 * Return the index of the last entry in the log or -1 if the log is empty.
43 * @return the index of the last entry in the log or -1 if the log is empty.
48 * Return the term of the last entry in the log or -1 if the log is empty.
50 * @return the term of the last entry in the log or -1 if the log is empty.
55 * Removes entries from the in-memory log starting at the given index.
57 * @param index the index of the first log entry to remove
58 * @return the adjusted index of the first log entry removed or -1 if the log entry is not found.
60 long removeFrom(long index);
63 * Removes entries from the in-memory log a nd the persisted log starting at the given index.
65 * The persisted information would then be used during recovery to properly
66 * reconstruct the state of the in-memory replicated log
68 * @param index the index of the first log entry to remove
71 boolean removeFromAndPersist(long index);
74 * Appends an entry to the log.
76 * @param replicatedLogEntry the entry to append
77 * @return true if the entry was successfully appended, false otherwise. An entry can fail to append if
78 * the index is already included in the log.
80 boolean append(ReplicatedLogEntry replicatedLogEntry);
83 * Optimization method to increase the capacity of the journal log prior to appending entries.
85 * @param amount the amount to increase by
87 void increaseJournalLogCapacity(int amount);
90 * Appends an entry to the in-memory log and persists it as well.
92 * @param replicatedLogEntry the entry to append
94 void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry);
97 * Appends an entry to the in-memory log and persists it as well.
99 * @param replicatedLogEntry the entry to append
100 * @param callback the Procedure to be notified when persistence is complete.
102 void appendAndPersist(ReplicatedLogEntry replicatedLogEntry, Procedure<ReplicatedLogEntry> callback);
105 * Returns a list of log entries starting from the given index to the end of the log.
107 * @param index the index of the first log entry to get.
108 * @return the List of entries
110 @Nonnull List<ReplicatedLogEntry> getFrom(long index);
113 * Returns a list of log entries starting from the given index up to the given maximum of entries or
114 * the given maximum accumulated size, whichever comes first.
116 * @param index the index of the first log entry to get
117 * @param maxEntries the maximum number of entries to get
118 * @param maxDataSize the maximum accumulated size of the log entries to get
119 * @return the List of entries meeting the criteria.
121 @Nonnull List<ReplicatedLogEntry> getFrom(long index, int maxEntries, long maxDataSize);
125 * @return the number of entries in the journal
130 * Checks if the entry at the specified index is present or not
132 * @param index the index of the log entry
133 * @return true if the entry is present in the in-memory journal
135 boolean isPresent(long index);
138 * Checks if the entry is present in a snapshot
140 * @param index the index of the log entry
141 * @return true if the entry is in the snapshot. false if the entry is not
142 * in the snapshot even if the entry may be present in the replicated log
144 boolean isInSnapshot(long index);
147 * Get the index of the snapshot
149 * @return the index from which the snapshot was created. -1 otherwise.
151 long getSnapshotIndex();
154 * Get the term of the snapshot
156 * @return the term of the index from which the snapshot was created. -1
159 long getSnapshotTerm();
162 * sets the snapshot index in the replicated log
163 * @param snapshotIndex
165 void setSnapshotIndex(long snapshotIndex);
169 * @param snapshotTerm
171 void setSnapshotTerm(long snapshotTerm);
174 * Clears the journal entries with startIndex(inclusive) and endIndex (exclusive)
178 void clear(int startIndex, int endIndex);
181 * Handles all the bookkeeping in order to perform a rollback in the
182 * event of SaveSnapshotFailure
183 * @param snapshotCapturedIndex
184 * @param snapshotCapturedTerm
186 void snapshotPreCommit(long snapshotCapturedIndex, long snapshotCapturedTerm);
189 * Sets the Replicated log to state after snapshot success.
191 void snapshotCommit();
194 * Restores the replicated log to a state in the event of a save snapshot failure
196 void snapshotRollback();
199 * Returns the size of the data in the log (in bytes)
201 * @return the size of the data in the log (in bytes).
206 * Determines if a snapshot need to be captured based on the count/memory consumed.
208 * @param replicatedLogEntry the last log entry.
210 void captureSnapshotIfReady(ReplicatedLogEntry replicatedLogEntry);