Refactor ReplicatedLogImpl to separate class
[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      */
55     void removeFrom(long index);
56
57
58     /**
59      * To be called when we need to remove entries from the in-memory log and we
60      * need that information persisted to disk. This method will remove all
61      * entries >= index.
62      * <p>
63      * The persisted information would then be used during recovery to properly
64      * reconstruct the state of the in-memory replicated log
65      *
66      * @param index the index of the log entry
67      */
68     void removeFromAndPersist(long index);
69
70     /**
71      * Append an entry to the log
72      * @param replicatedLogEntry
73      */
74     void append(ReplicatedLogEntry replicatedLogEntry);
75
76     /**
77      * Optimization method to increase the capacity of the journal log prior to appending entries.
78      *
79      * @param amount the amount to increase by
80      */
81     void increaseJournalLogCapacity(int amount);
82
83     /**
84      *
85      * @param replicatedLogEntry
86      */
87     void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry);
88
89     void appendAndPersist(ReplicatedLogEntry replicatedLogEntry, Procedure<ReplicatedLogEntry> callback);
90
91     /**
92      *
93      * @param index the index of the log entry
94      */
95     List<ReplicatedLogEntry> getFrom(long index);
96
97     /**
98      *
99      * @param index the index of the log entry
100      */
101     List<ReplicatedLogEntry> getFrom(long index, int max);
102
103     /**
104      *
105      * @return
106      */
107     long size();
108
109     /**
110      * Checks if the entry at the specified index is present or not
111      *
112      * @param index the index of the log entry
113      * @return true if the entry is present in the in-memory journal
114      */
115     boolean isPresent(long index);
116
117     /**
118      * Checks if the entry is present in a snapshot
119      *
120      * @param index the index of the log entry
121      * @return true if the entry is in the snapshot. false if the entry is not
122      * in the snapshot even if the entry may be present in the replicated log
123      */
124     boolean isInSnapshot(long index);
125
126     /**
127      * Get the index of the snapshot
128      *
129      * @return the index from which the snapshot was created. -1 otherwise.
130      */
131     long getSnapshotIndex();
132
133     /**
134      * Get the term of the snapshot
135      *
136      * @return the term of the index from which the snapshot was created. -1
137      * otherwise
138      */
139     long getSnapshotTerm();
140
141     /**
142      * sets the snapshot index in the replicated log
143      * @param snapshotIndex
144      */
145     void setSnapshotIndex(long snapshotIndex);
146
147     /**
148      * sets snapshot term
149      * @param snapshotTerm
150      */
151     void setSnapshotTerm(long snapshotTerm);
152
153     /**
154      * Clears the journal entries with startIndex(inclusive) and endIndex (exclusive)
155      * @param startIndex
156      * @param endIndex
157      */
158     void clear(int startIndex, int endIndex);
159
160     /**
161      * Handles all the bookkeeping in order to perform a rollback in the
162      * event of SaveSnapshotFailure
163      * @param snapshotCapturedIndex
164      * @param snapshotCapturedTerm
165      */
166     void snapshotPreCommit(long snapshotCapturedIndex, long snapshotCapturedTerm);
167
168     /**
169      * Sets the Replicated log to state after snapshot success.
170      */
171     void snapshotCommit();
172
173     /**
174      * Restores the replicated log to a state in the event of a save snapshot failure
175      */
176     void snapshotRollback();
177
178     /**
179      * Size of the data in the log (in bytes)
180      */
181     int dataSize();
182
183 }