Fix remaining CS errors in sal-akka-raft and enable enforcement
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / ReplicatedLog.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.raft;
10
11 import akka.japi.Procedure;
12 import java.util.List;
13 import javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
15
16 /**
17  * Represents the ReplicatedLog that needs to be kept in sync by the RaftActor.
18  */
19 public interface ReplicatedLog {
20     long NO_MAX_SIZE = -1;
21
22     /**
23      * Return the replicated log entry at the specified index.
24      *
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
28      */
29     @Nullable
30     ReplicatedLogEntry get(long index);
31
32     /**
33      * Return the last replicated log entry in the log or null of not found.
34      *
35      * @return the last replicated log entry in the log or null of not found.
36      */
37     @Nullable
38     ReplicatedLogEntry last();
39
40     /**
41      * Return the index of the last entry in the log or -1 if the log is empty.
42      *
43      * @return the index of the last entry in the log or -1 if the log is empty.
44      */
45     long lastIndex();
46
47     /**
48      * Return the term of the last entry in the log or -1 if the log is empty.
49      *
50      * @return the term of the last entry in the log or -1 if the log is empty.
51      */
52     long lastTerm();
53
54     /**
55      * Removes entries from the in-memory log starting at the given index.
56      *
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.
59      */
60     long removeFrom(long index);
61
62     /**
63      * Removes entries from the in-memory log and the persisted log starting at the given index.
64      * <p/>
65      * The persisted information would then be used during recovery to properly
66      * reconstruct the state of the in-memory replicated log
67      *
68      * @param index the index of the first log entry to remove
69      * @return true if entries were removed, false otherwise
70      */
71     boolean removeFromAndPersist(long index);
72
73     /**
74      * Appends an entry to the log.
75      *
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.
79      */
80     boolean append(ReplicatedLogEntry replicatedLogEntry);
81
82     /**
83      * Optimization method to increase the capacity of the journal log prior to appending entries.
84      *
85      * @param amount the amount to increase by
86      */
87     void increaseJournalLogCapacity(int amount);
88
89     /**
90      * Appends an entry to the in-memory log and persists it as well.
91      *
92      * @param replicatedLogEntry the entry to append
93      */
94     void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry);
95
96     /**
97      * Appends an entry to the in-memory log and persists it as well.
98      *
99      * @param replicatedLogEntry the entry to append
100      * @param callback the Procedure to be notified when persistence is complete.
101      */
102     void appendAndPersist(ReplicatedLogEntry replicatedLogEntry, Procedure<ReplicatedLogEntry> callback);
103
104     /**
105      * Returns a list of log entries starting from the given index to the end of the log.
106      *
107      * @param index the index of the first log entry to get.
108      * @return the List of entries
109      */
110     @Nonnull List<ReplicatedLogEntry> getFrom(long index);
111
112     /**
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.
115      *
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.
120      */
121     @Nonnull List<ReplicatedLogEntry> getFrom(long index, int maxEntries, long maxDataSize);
122
123     /**
124      * Returns the number of entries in the journal.
125      *
126      * @return the number of entries
127      */
128     long size();
129
130     /**
131      * Checks if the entry at the specified index is present or not.
132      *
133      * @param index the index of the log entry
134      * @return true if the entry is present in the in-memory journal
135      */
136     boolean isPresent(long index);
137
138     /**
139      * Checks if the entry is present in a snapshot.
140      *
141      * @param index the index of the log entry
142      * @return true if the entry is in the snapshot. false if the entry is not in the snapshot even if the entry may
143      *         be present in the replicated log
144      */
145     boolean isInSnapshot(long index);
146
147     /**
148      * Returns the index of the snapshot.
149      *
150      * @return the index from which the snapshot was created. -1 otherwise.
151      */
152     long getSnapshotIndex();
153
154     /**
155      * Returns the term of the snapshot.
156      *
157      * @return the term of the index from which the snapshot was created. -1 otherwise
158      */
159     long getSnapshotTerm();
160
161     /**
162      * Sets the snapshot index in the replicated log.
163      *
164      * @param snapshotIndex the index to set
165      */
166     void setSnapshotIndex(long snapshotIndex);
167
168     /**
169      * Sets snapshot term.
170      *
171      * @param snapshotTerm the term to set
172      */
173     void setSnapshotTerm(long snapshotTerm);
174
175     /**
176      * Clears the journal entries with startIndex (inclusive) and endIndex (exclusive).
177      *
178      * @param startIndex the start index (inclusive)
179      * @param endIndex the end index (exclusive)
180      */
181     void clear(int startIndex, int endIndex);
182
183     /**
184      * Handles all the bookkeeping in order to perform a rollback in the event of SaveSnapshotFailure.
185      *
186      * @param snapshotCapturedIndex the new snapshot index
187      * @param snapshotCapturedTerm the new snapshot term
188      */
189     void snapshotPreCommit(long snapshotCapturedIndex, long snapshotCapturedTerm);
190
191     /**
192      * Sets the Replicated log to state after snapshot success.
193      */
194     void snapshotCommit();
195
196     /**
197      * Restores the replicated log to a state in the event of a save snapshot failure.
198      */
199     void snapshotRollback();
200
201     /**
202      * Returns the size of the data in the log (in bytes).
203      *
204      * @return the size of the data in the log (in bytes)
205      */
206     int dataSize();
207
208     /**
209      * Determines if a snapshot need to be captured based on the count/memory consumed.
210      *
211      * @param replicatedLogEntry the last log entry.
212      */
213     void captureSnapshotIfReady(ReplicatedLogEntry replicatedLogEntry);
214 }