Merge "Install snapshot and Reply"
[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      *
79      * @param replicatedLogEntry
80      */
81     void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry);
82
83     /**
84      *
85      * @param index the index of the log entry
86      */
87     List<ReplicatedLogEntry> getFrom(long index);
88
89     /**
90      *
91      * @param index the index of the log entry
92      */
93     List<ReplicatedLogEntry> getFrom(long index, int max);
94
95     /**
96      *
97      * @return
98      */
99     long size();
100
101     /**
102      * Checks if the entry at the specified index is present or not
103      *
104      * @param index the index of the log entry
105      * @return true if the entry is present in the in-memory journal
106      */
107     boolean isPresent(long index);
108
109     /**
110      * Checks if the entry is present in a snapshot
111      *
112      * @param index the index of the log entry
113      * @return true if the entry is in the snapshot. false if the entry is not
114      * in the snapshot even if the entry may be present in the replicated log
115      */
116     boolean isInSnapshot(long index);
117
118     /**
119      * Get the snapshot
120      *
121      * @return an object representing the snapshot if it exists. null otherwise
122      */
123     ByteString getSnapshot();
124
125     /**
126      * Get the index of the snapshot
127      *
128      * @return the index from which the snapshot was created. -1 otherwise.
129      */
130     long getSnapshotIndex();
131
132     /**
133      * Get the term of the snapshot
134      *
135      * @return the term of the index from which the snapshot was created. -1
136      * otherwise
137      */
138     long getSnapshotTerm();
139
140     /**
141      * sets the snapshot index in the replicated log
142      * @param snapshotIndex
143      */
144     void setSnapshotIndex(long snapshotIndex);
145
146     /**
147      * sets snapshot term
148      * @param snapshotTerm
149      */
150     public void setSnapshotTerm(long snapshotTerm);
151
152     /**
153      * sets the snapshot in bytes
154      * @param snapshot
155      */
156     public void setSnapshot(ByteString snapshot);
157
158     /**
159      * Clears the journal entries with startIndex(inclusive) and endIndex (exclusive)
160      * @param startIndex
161      * @param endIndex
162      */
163     public void clear(int startIndex, int endIndex);
164
165     /**
166      * Handles all the bookkeeping in order to perform a rollback in the
167      * event of SaveSnapshotFailure
168      * @param snapshot
169      * @param snapshotCapturedIndex
170      * @param snapshotCapturedTerm
171      */
172     public void snapshotPreCommit(ByteString snapshot,
173         long snapshotCapturedIndex, long snapshotCapturedTerm);
174
175     /**
176      * Sets the Replicated log to state after snapshot success.
177      */
178     public void snapshotCommit();
179
180     /**
181      * Restores the replicated log to a state in the event of a save snapshot failure
182      */
183     public void snapshotRollback();
184 }