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