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.
29 @Nullable ReplicatedLogEntry get(long index);
32 * Return the last replicated log entry in the log or null of not found.
34 @Nullable ReplicatedLogEntry last();
37 * Return the index of the last entry in the log or -1 if the log is empty.
42 * Return the term of the last entry in the log or -1 if the log is empty.
47 * Removes entries from the in-memory log starting at the given index.
49 * @param index the index of the first log entry to remove
50 * @return the adjusted index of the first log entry removed or -1 if the log entry is not found.
52 long removeFrom(long index);
55 * Removes entries from the in-memory log a nd the persisted log starting at the given index.
57 * The persisted information would then be used during recovery to properly
58 * reconstruct the state of the in-memory replicated log
60 * @param index the index of the first log entry to remove
62 void removeFromAndPersist(long index);
65 * Appends an entry to the log.
67 * @param replicatedLogEntry the entry to append
68 * @return true if the entry was successfully appended, false otherwise. An entry can fail to append if
69 * the index is already included in the log.
71 boolean append(ReplicatedLogEntry replicatedLogEntry);
74 * Optimization method to increase the capacity of the journal log prior to appending entries.
76 * @param amount the amount to increase by
78 void increaseJournalLogCapacity(int amount);
81 * Appends an entry to the in-memory log and persists it as well.
83 * @param replicatedLogEntry the entry to append
85 void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry);
87 void appendAndPersist(ReplicatedLogEntry replicatedLogEntry, Procedure<ReplicatedLogEntry> callback);
90 * Returns a list of log entries starting from the given index to the end of the log.
92 * @param index the index of the first log entry to get.
93 * @return the List of entries
95 @Nonnull List<ReplicatedLogEntry> getFrom(long index);
98 * Returns a list of log entries starting from the given index up to the given maximum of entries or
99 * the given maximum accumulated size, whichever comes first.
101 * @param index the index of the first log entry to get
102 * @param maxEntries the maximum number of entries to get
103 * @param maxDataSize the maximum accumulated size of the log entries to get
104 * @return the List of entries meeting the criteria.
106 @Nonnull List<ReplicatedLogEntry> getFrom(long index, int maxEntries, long maxDataSize);
110 * @return the number of entries in the journal
115 * Checks if the entry at the specified index is present or not
117 * @param index the index of the log entry
118 * @return true if the entry is present in the in-memory journal
120 boolean isPresent(long index);
123 * Checks if the entry is present in a snapshot
125 * @param index the index of the log entry
126 * @return true if the entry is in the snapshot. false if the entry is not
127 * in the snapshot even if the entry may be present in the replicated log
129 boolean isInSnapshot(long index);
132 * Get the index of the snapshot
134 * @return the index from which the snapshot was created. -1 otherwise.
136 long getSnapshotIndex();
139 * Get the term of the snapshot
141 * @return the term of the index from which the snapshot was created. -1
144 long getSnapshotTerm();
147 * sets the snapshot index in the replicated log
148 * @param snapshotIndex
150 void setSnapshotIndex(long snapshotIndex);
154 * @param snapshotTerm
156 void setSnapshotTerm(long snapshotTerm);
159 * Clears the journal entries with startIndex(inclusive) and endIndex (exclusive)
163 void clear(int startIndex, int endIndex);
166 * Handles all the bookkeeping in order to perform a rollback in the
167 * event of SaveSnapshotFailure
168 * @param snapshotCapturedIndex
169 * @param snapshotCapturedTerm
171 void snapshotPreCommit(long snapshotCapturedIndex, long snapshotCapturedTerm);
174 * Sets the Replicated log to state after snapshot success.
176 void snapshotCommit();
179 * Restores the replicated log to a state in the event of a save snapshot failure
181 void snapshotRollback();
184 * Size of the data in the log (in bytes)
189 * We decide if snapshot need to be captured based on the count/memory consumed.
190 * @param replicatedLogEntry
192 void captureSnapshotIfReady(ReplicatedLogEntry replicatedLogEntry);