Merge "BUG-1953: fix SAL compatility layer"
[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 com.google.protobuf.ByteString;
12
13 import java.util.List;
14
15 /**
16  * Represents the ReplicatedLog that needs to be kept in sync by the RaftActor
17  */
18 public interface ReplicatedLog {
19     /**
20      * Get a replicated log entry at the specified index
21      *
22      * @param index the index of the log entry
23      * @return the ReplicatedLogEntry at index. null if index less than 0 or
24      * greater than the size of the in-memory journal.
25      */
26     ReplicatedLogEntry get(long index);
27
28
29     /**
30      * Get the last replicated log entry
31      *
32      * @return
33      */
34     ReplicatedLogEntry last();
35
36     /**
37      *
38      * @return
39      */
40     long lastIndex();
41
42     /**
43      *
44      * @return
45      */
46     long lastTerm();
47
48     /**
49      * To be called when we need to remove entries from the in-memory log.
50      * This method will remove all entries >= index. This method should be used
51      * during recovery to appropriately trim the log based on persisted
52      * information
53      *
54      * @param index the index of the log entry
55      */
56     void 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     /**
91      *
92      * @param index the index of the log entry
93      */
94     List<ReplicatedLogEntry> getFrom(long index);
95
96     /**
97      *
98      * @param index the index of the log entry
99      */
100     List<ReplicatedLogEntry> getFrom(long index, int max);
101
102     /**
103      *
104      * @return
105      */
106     long size();
107
108     /**
109      * Checks if the entry at the specified index is present or not
110      *
111      * @param index the index of the log entry
112      * @return true if the entry is present in the in-memory journal
113      */
114     boolean isPresent(long index);
115
116     /**
117      * Checks if the entry is present in a snapshot
118      *
119      * @param index the index of the log entry
120      * @return true if the entry is in the snapshot. false if the entry is not
121      * in the snapshot even if the entry may be present in the replicated log
122      */
123     boolean isInSnapshot(long index);
124
125     /**
126      * Get the snapshot
127      *
128      * @return an object representing the snapshot if it exists. null otherwise
129      */
130     ByteString getSnapshot();
131
132     /**
133      * Get the index of the snapshot
134      *
135      * @return the index from which the snapshot was created. -1 otherwise.
136      */
137     long getSnapshotIndex();
138
139     /**
140      * Get the term of the snapshot
141      *
142      * @return the term of the index from which the snapshot was created. -1
143      * otherwise
144      */
145     long getSnapshotTerm();
146
147     /**
148      * sets the snapshot index in the replicated log
149      * @param snapshotIndex
150      */
151     void setSnapshotIndex(long snapshotIndex);
152
153     /**
154      * sets snapshot term
155      * @param snapshotTerm
156      */
157     public void setSnapshotTerm(long snapshotTerm);
158
159     /**
160      * sets the snapshot in bytes
161      * @param snapshot
162      */
163     public void setSnapshot(ByteString snapshot);
164
165     /**
166      * Clears the journal entries with startIndex(inclusive) and endIndex (exclusive)
167      * @param startIndex
168      * @param endIndex
169      */
170     public void clear(int startIndex, int endIndex);
171
172     /**
173      * Handles all the bookkeeping in order to perform a rollback in the
174      * event of SaveSnapshotFailure
175      * @param snapshot
176      * @param snapshotCapturedIndex
177      * @param snapshotCapturedTerm
178      */
179     public void snapshotPreCommit(ByteString snapshot,
180         long snapshotCapturedIndex, long snapshotCapturedTerm);
181
182     /**
183      * Sets the Replicated log to state after snapshot success.
184      */
185     public void snapshotCommit();
186
187     /**
188      * Restores the replicated log to a state in the event of a save snapshot failure
189      */
190     public void snapshotRollback();
191 }