Bug 7362: Notify applyState synchronously
[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 akka.cluster.Cluster;
16 import com.google.common.annotations.VisibleForTesting;
17 import java.util.Collection;
18 import java.util.Optional;
19 import java.util.function.Consumer;
20 import java.util.function.LongSupplier;
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23 import org.opendaylight.controller.cluster.DataPersistenceProvider;
24 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
25 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
26 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
27 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
28 import org.slf4j.Logger;
29
30 /**
31  * The RaftActorContext contains that portion of the RaftActors state that
32  * needs to be shared with it's behaviors. A RaftActorContext should NEVER be
33  * used in any actor context outside the RaftActor that constructed it.
34  */
35 public interface RaftActorContext {
36     /**
37      * Creates a new local actor.
38      *
39      * @param props the Props used to create the actor.
40      * @return a reference to the newly created actor.
41      */
42     ActorRef actorOf(Props props);
43
44     /**
45      * Creates an actor selection.
46      *
47      * @param path the path.
48      * @return an actor selection for the given actor path.
49      */
50     ActorSelection actorSelection(String path);
51
52     /**
53      * Returns the identifier for the RaftActor. This identifier represents the
54      * name of the actor whose common state is being shared.
55      *
56      * @return the identifier
57      */
58     String getId();
59
60     /**
61      * Returns the reference to the RaftActor.
62      *
63      * @return the reference to the RaftActor itself. This can be used to send messages to the RaftActor
64      */
65     ActorRef getActor();
66
67     /**
68      * The akka Cluster singleton for the actor system if one is configured.
69      *
70      * @return an Optional containing the Cluster instance is present.
71      */
72     Optional<Cluster> getCluster();
73
74     /**
75      * Returns the current ElectionTerm information.
76      *
77      * @return the ElectionTerm.
78      */
79     @Nonnull
80     ElectionTerm getTermInformation();
81
82     /**
83      * Returns the index of highest log entry known to be committed.
84      *
85      * @return index of highest log entry known to be committed.
86      */
87     long getCommitIndex();
88
89
90     /**
91      * Sets the index of highest log entry known to be committed.
92      *
93      * @param commitIndex new commit index
94      */
95     void setCommitIndex(long commitIndex);
96
97     /**
98      * Returns index of highest log entry applied to state machine.
99      *
100      * @return index of highest log entry applied to state machine.
101      */
102     long getLastApplied();
103
104     /**
105      * Sets index of highest log entry applied to state machine.
106      *
107      * @param lastApplied the new applied index.
108      */
109     void setLastApplied(long lastApplied);
110
111     /**
112      * Sets the ReplicatedLog instance.
113      *
114      * @param replicatedLog the ReplicatedLog instance.
115      */
116     void setReplicatedLog(@Nonnull ReplicatedLog replicatedLog);
117
118     /**
119      * Returns the ReplicatedLog instance.
120      *
121      * @return the ReplicatedLog instance.
122      */
123     @Nonnull
124     ReplicatedLog getReplicatedLog();
125
126     /**
127      * Returns the The ActorSystem associated with this context.
128      *
129      * @return the ActorSystem.
130      */
131     @Nonnull
132     ActorSystem getActorSystem();
133
134     /**
135      * Returns the logger to be used for logging messages.
136      *
137      * @return the logger.
138      */
139     @Nonnull
140     Logger getLogger();
141
142     /**
143      * Gets the address of a peer as a String. This is the same format in which a consumer would provide the address.
144      *
145      * @param peerId the id of the peer.
146      * @return the address of the peer or null if the address has not yet been resolved.
147      */
148     @Nullable
149     String getPeerAddress(String peerId);
150
151     /**
152      * Updates the peers and information to match the given ServerConfigurationPayload.
153      *
154      * @param serverCfgPayload the ServerConfigurationPayload.
155      */
156     void updatePeerIds(ServerConfigurationPayload serverCfgPayload);
157
158     /**
159      * Returns the PeerInfo instances for each peer.
160      *
161      * @return list of PeerInfo
162      */
163     @Nonnull
164     Collection<PeerInfo> getPeers();
165
166     /**
167      * Returns the id's for each peer.
168      *
169      * @return the list of peer id's.
170      */
171     @Nonnull
172     Collection<String> getPeerIds();
173
174     /**
175      * Returns the PeerInfo for the given peer.
176      *
177      * @param peerId the id of the peer
178      * @return the PeerInfo or null if not found
179      */
180     @Nullable
181     PeerInfo getPeerInfo(String peerId);
182
183     /**
184      * Adds a new peer.
185      *
186      * @param id the id of the new peer.
187      * @param address the address of the new peer.
188      * @param votingState the VotingState of the new peer.
189      */
190     void addToPeers(String id, String address, VotingState votingState);
191
192     /**
193      * Removes a peer.
194      *
195      * @param id the id of the peer to remove.
196      */
197     void removePeer(String id);
198
199     /**
200      * Returns an ActorSelection for a peer.
201      *
202      * @param peerId the id of the peer.
203      * @return the actorSelection corresponding to the peer or null if the address has not yet been resolved.
204      */
205     @Nullable
206     ActorSelection getPeerActorSelection(String peerId);
207
208     /**
209      * Sets the address of a peer.
210      *
211      * @param peerId the id of the peer.
212      * @param peerAddress the address of the peer.
213      */
214     void setPeerAddress(String peerId, String peerAddress);
215
216     /**
217      * Returns the ConfigParams instance.
218      *
219      * @return the ConfigParams instance.
220      */
221     @Nonnull
222     ConfigParams getConfigParams();
223
224     /**
225      * Returns the SnapshotManager instance.
226      *
227      * @return the SnapshotManager instance.
228      */
229     @Nonnull
230     SnapshotManager getSnapshotManager();
231
232     /**
233      * Returns the DataPersistenceProvider instance.
234      *
235      * @return the DataPersistenceProvider instance.
236      */
237     @Nonnull
238     DataPersistenceProvider getPersistenceProvider();
239
240     /**
241      * Determines if there are any peer followers.
242      *
243      * @return true if there are followers otherwise false.
244      */
245     boolean hasFollowers();
246
247     /**
248      * Returns the total available memory for use in calculations. Normally this returns JVM's max memory but can be
249      * overridden for unit tests.
250      *
251      * @return the total memory.
252      */
253     long getTotalMemory();
254
255     /**
256      * Sets the retriever of the total memory metric.
257      *
258      * @param retriever a supplier of the total memory metric.
259      */
260     @VisibleForTesting
261     void setTotalMemoryRetriever(LongSupplier retriever);
262
263     /**
264      * Returns the payload version to be used when replicating data.
265      *
266      * @return the payload version.
267      */
268     short getPayloadVersion();
269
270     /**
271      * Returns the RaftPolicy used to determine certain Raft behaviors.
272      *
273      * @return the RaftPolicy instance.
274      */
275     @Nonnull
276     RaftPolicy getRaftPolicy();
277
278     /**
279      * Determines if there have been any dynamic server configuration changes applied.
280      *
281      * @return true if dynamic server configuration changes have been applied, false otherwise, meaning that static
282      *         peer configuration is still in use.
283      */
284     boolean isDynamicServerConfigurationInUse();
285
286     /**
287      * Sets that dynamic server configuration changes have been applied.
288      */
289     void setDynamicServerConfigurationInUse();
290
291     /**
292      * Returns the peer information as a ServerConfigurationPayload if dynamic server configurations have been applied.
293      *
294      * @param includeSelf include this peer's info.
295      * @return the peer information as a ServerConfigurationPayload or null if no dynamic server configurations have
296      *         been applied.
297      */
298     @Nullable
299     ServerConfigurationPayload getPeerServerInfo(boolean includeSelf);
300
301     /**
302      * Determines if this peer is a voting member of the cluster.
303      *
304      * @return true if this peer is a voting member, false otherwise.
305      */
306     boolean isVotingMember();
307
308     /**
309      * Determines if there are any voting peers.
310      *
311      * @return true if there are any voting peers, false otherwise.
312      */
313     boolean anyVotingPeers();
314
315     /**
316      * Returns the current behavior attached to the RaftActor.
317      *
318      * @return current behavior.
319      */
320     RaftActorBehavior getCurrentBehavior();
321
322     /**
323      * Returns the consumer of ApplyState operations. This is invoked by a behavior when a log entry needs to be
324      * applied to the state.
325      *
326      * @return the Consumer
327      */
328     Consumer<ApplyState> getApplyStateConsumer();
329 }