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