Force install snapshot when follower log is ahead
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / RaftActorContext.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.actor.ActorRef;
12 import akka.actor.ActorSelection;
13 import akka.actor.ActorSystem;
14 import akka.actor.Props;
15 import com.google.common.annotations.VisibleForTesting;
16 import java.util.Collection;
17 import java.util.function.LongSupplier;
18 import javax.annotation.Nullable;
19 import org.opendaylight.controller.cluster.DataPersistenceProvider;
20 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
21 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
22 import org.slf4j.Logger;
23
24 /**
25  * The RaftActorContext contains that portion of the RaftActors state that
26  * needs to be shared with it's behaviors. A RaftActorContext should NEVER be
27  * used in any actor context outside the RaftActor that constructed it.
28  */
29 public interface RaftActorContext {
30     /**
31      * Create a new local actor
32      * @param props
33      * @return a reference to the newly created actor
34      */
35     ActorRef actorOf(Props props);
36
37     /**
38      * Create a actor selection
39      * @param path
40      * @return an actor selection for the given actor path
41      */
42     ActorSelection actorSelection(String path);
43
44     /**
45      * Get the identifier for the RaftActor. This identifier represents the
46      * name of the actor whose common state is being shared. For example the
47      * id could be 'inventory'
48      *
49      * @return the identifier
50      */
51     String getId();
52
53     /**
54      * @return A reference to the RaftActor itself. This could be used to send messages
55      * to the RaftActor
56      */
57     ActorRef getActor();
58
59     /**
60      * @return the ElectionTerm information
61      */
62     ElectionTerm getTermInformation();
63
64     /**
65      * @return index of highest log entry known to be committed (initialized to 0, increases monotonically)
66      */
67     long getCommitIndex();
68
69
70     /**
71      * @param commitIndex new commit index
72      */
73     void setCommitIndex(long commitIndex);
74
75     /**
76      * @return index of highest log entry applied to state machine (initialized to 0, increases monotonically)
77      */
78     long getLastApplied();
79
80
81     /**
82      * @param lastApplied the index of the last log entry that was applied to the state
83      */
84     void setLastApplied(long lastApplied);
85
86     /**
87      *
88      * @param replicatedLog the replicated log of the current RaftActor
89      */
90     void setReplicatedLog(ReplicatedLog replicatedLog);
91
92     /**
93      * @return A representation of the log
94      */
95     ReplicatedLog getReplicatedLog();
96
97     /**
98      * @return The ActorSystem associated with this context
99      */
100     ActorSystem getActorSystem();
101
102     /**
103      * @return the logger to be used for logging messages to a log file
104      */
105     Logger getLogger();
106
107     /**
108      * Get the address of the peer as a String. This is the same format in
109      * which a consumer would provide the address
110      *
111      * @param peerId
112      * @return The address of the peer or null if the address has not yet been
113      *         resolved
114      */
115     String getPeerAddress(String peerId);
116
117     /**
118      * @param serverCfgPayload
119      */
120     void updatePeerIds(ServerConfigurationPayload serverCfgPayload);
121
122     /**
123      * @return list of PeerInfo
124      */
125     Collection<PeerInfo> getPeers();
126
127     /**
128      * @return the list of peer IDs.
129      */
130     Collection<String> getPeerIds();
131
132     /**
133      * Get the PeerInfo for the given peer.
134      *
135      * @param peerId
136      * @return the PeerInfo
137      */
138     PeerInfo getPeerInfo(String peerId);
139
140     /**
141      * Add to actor peers
142      *
143      * @param name
144      * @param address
145      */
146     void addToPeers(String name, String address, VotingState votingState);
147
148     /**
149      *
150      * @param name
151      */
152     void removePeer(String name);
153
154     /**
155      * Given a peerId return the corresponding actor
156      * <p>
157      *
158      *
159      * @param peerId
160      * @return The actorSelection corresponding to the peer or null if the
161      *         address has not yet been resolved
162      */
163     ActorSelection getPeerActorSelection(String peerId);
164
165     /**
166      * Set Peer Address can be called at a later time to change the address of
167      * a known peer.
168      *
169      * <p>
170      * Throws an IllegalStateException if the peer is unknown
171      *
172      * @param peerId
173      * @param peerAddress
174      */
175     void setPeerAddress(String peerId, String peerAddress);
176
177     /**
178      * @return ConfigParams
179      */
180     ConfigParams getConfigParams();
181
182     /**
183      *
184      * @return the SnapshotManager for this RaftActor
185      */
186     SnapshotManager getSnapshotManager();
187
188     /**
189      *
190      * @return the DataPersistenceProvider for this RaftActor
191      */
192     DataPersistenceProvider getPersistenceProvider();
193
194     /**
195      *
196      * @return true if the RaftActor has followers else false
197      */
198     boolean hasFollowers();
199
200     /**
201      *
202      * @return the total memory used by the ReplicatedLog
203      */
204     long getTotalMemory();
205
206     /**
207      *
208      * @param retriever a supplier of the total memory metric
209      */
210     @VisibleForTesting
211     void setTotalMemoryRetriever(LongSupplier retriever);
212
213     /**
214      *
215      * @return the payload version to be used when replicating data
216      */
217     short getPayloadVersion();
218
219     /**
220      * @return an implementation of the RaftPolicy so that the Raft code can be adapted
221      */
222     RaftPolicy getRaftPolicy();
223
224     /**
225      * @return true if there are any dynamic server configuration changes available,
226      *  false if static peer configurations are still in use
227      */
228     boolean isDynamicServerConfigurationInUse();
229
230     /**
231      * Configures the dynamic server configurations are avaialble for the RaftActor
232      */
233     void setDynamicServerConfigurationInUse();
234
235     /**
236      * @return the RaftActor's peer information as a ServerConfigurationPayload if the
237      * dynamic server configurations are available, otherwise returns null
238      */
239     @Nullable ServerConfigurationPayload getPeerServerInfo(boolean includeSelf);
240
241     /**
242      * @return true if this RaftActor is a voting member of the cluster, false otherwise.
243      */
244     boolean isVotingMember();
245
246     /**
247      * @return true if there are any voting peers, false otherwise.
248      */
249     boolean anyVotingPeers();
250
251     /**
252      * @return current behavior attached to the raft actor.
253      */
254     RaftActorBehavior getCurrentBehavior();
255 }