Merge "Add missing copyright text"
[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
14 /**
15  * Represents the ReplicatedLog that needs to be kept in sync by the RaftActor
16  */
17 public interface ReplicatedLog {
18     /**
19      * Get a replicated log entry at the specified index
20      *
21      * @param index the index of the log entry
22      * @return the ReplicatedLogEntry at index. null if index less than 0 or
23      * greater than the size of the in-memory journal.
24      */
25     ReplicatedLogEntry get(long index);
26
27
28     /**
29      * Get the last replicated log entry
30      *
31      * @return
32      */
33     ReplicatedLogEntry last();
34
35     /**
36      *
37      * @return
38      */
39     long lastIndex();
40
41     /**
42      *
43      * @return
44      */
45     long lastTerm();
46
47     /**
48      * To be called when we need to remove entries from the in-memory log.
49      * This method will remove all entries >= index. This method should be used
50      * during recovery to appropriately trim the log based on persisted
51      * information
52      *
53      * @param index the index of the log entry
54      * @return the adjusted index of the first log entry removed or -1 if log entry not found.
55      */
56     long removeFrom(long index);
57
58
59     /**
60      * To be called when we need to remove entries from the in-memory log and we
61      * need that information persisted to disk. This method will remove all
62      * entries >= index.
63      * <p>
64      * The persisted information would then be used during recovery to properly
65      * reconstruct the state of the in-memory replicated log
66      *
67      * @param index the index of the log entry
68      */
69     void removeFromAndPersist(long index);
70
71     /**
72      * Append an entry to the log
73      * @param replicatedLogEntry
74      */
75     void append(ReplicatedLogEntry replicatedLogEntry);
76
77     /**
78      * Optimization method to increase the capacity of the journal log prior to appending entries.
79      *
80      * @param amount the amount to increase by
81      */
82     void increaseJournalLogCapacity(int amount);
83
84     /**
85      *
86      * @param replicatedLogEntry
87      */
88     void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry);
89
90     void appendAndPersist(ReplicatedLogEntry replicatedLogEntry, Procedure<ReplicatedLogEntry> callback);
91
92     /**
93      *
94      * @param index the index of the log entry
95      */
96     List<ReplicatedLogEntry> getFrom(long index);
97
98     /**
99      *
100      * @param index the index of the log entry
101      */
102     List<ReplicatedLogEntry> getFrom(long index, int max);
103
104     /**
105      *
106      * @return
107      */
108     long size();
109
110     /**
111      * Checks if the entry at the specified index is present or not
112      *
113      * @param index the index of the log entry
114      * @return true if the entry is present in the in-memory journal
115      */
116     boolean isPresent(long index);
117
118     /**
119      * Checks if the entry is present in a snapshot
120      *
121      * @param index the index of the log entry
122      * @return true if the entry is in the snapshot. false if the entry is not
123      * in the snapshot even if the entry may be present in the replicated log
124      */
125     boolean isInSnapshot(long index);
126
127     /**
128      * Get the index of the snapshot
129      *
130      * @return the index from which the snapshot was created. -1 otherwise.
131      */
132     long getSnapshotIndex();
133
134     /**
135      * Get the term of the snapshot
136      *
137      * @return the term of the index from which the snapshot was created. -1
138      * otherwise
139      */
140     long getSnapshotTerm();
141
142     /**
143      * sets the snapshot index in the replicated log
144      * @param snapshotIndex
145      */
146     void setSnapshotIndex(long snapshotIndex);
147
148     /**
149      * sets snapshot term
150      * @param snapshotTerm
151      */
152     void setSnapshotTerm(long snapshotTerm);
153
154     /**
155      * Clears the journal entries with startIndex(inclusive) and endIndex (exclusive)
156      * @param startIndex
157      * @param endIndex
158      */
159     void clear(int startIndex, int endIndex);
160
161     /**
162      * Handles all the bookkeeping in order to perform a rollback in the
163      * event of SaveSnapshotFailure
164      * @param snapshotCapturedIndex
165      * @param snapshotCapturedTerm
166      */
167     void snapshotPreCommit(long snapshotCapturedIndex, long snapshotCapturedTerm);
168
169     /**
170      * Sets the Replicated log to state after snapshot success.
171      */
172     void snapshotCommit();
173
174     /**
175      * Restores the replicated log to a state in the event of a save snapshot failure
176      */
177     void snapshotRollback();
178
179     /**
180      * Size of the data in the log (in bytes)
181      */
182     int dataSize();
183
184 }