2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.raft;
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;
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.
35 public interface RaftActorContext {
37 * Creates a new local actor.
39 * @param props the Props used to create the actor.
40 * @return a reference to the newly created actor.
42 ActorRef actorOf(Props props);
45 * Creates an actor selection.
47 * @param path the path.
48 * @return an actor selection for the given actor path.
50 ActorSelection actorSelection(String path);
53 * Returns the identifier for the RaftActor. This identifier represents the
54 * name of the actor whose common state is being shared.
56 * @return the identifier
61 * Returns the reference to the RaftActor.
63 * @return the reference to the RaftActor itself. This can be used to send messages to the RaftActor
68 * The akka Cluster singleton for the actor system if one is configured.
70 * @return an Optional containing the Cluster instance is present.
72 Optional<Cluster> getCluster();
75 * Returns the current ElectionTerm information.
77 * @return the ElectionTerm.
80 ElectionTerm getTermInformation();
83 * Returns the index of highest log entry known to be committed.
85 * @return index of highest log entry known to be committed.
87 long getCommitIndex();
91 * Sets the index of highest log entry known to be committed.
93 * @param commitIndex new commit index
95 void setCommitIndex(long commitIndex);
98 * Returns index of highest log entry applied to state machine.
100 * @return index of highest log entry applied to state machine.
102 long getLastApplied();
105 * Sets index of highest log entry applied to state machine.
107 * @param lastApplied the new applied index.
109 void setLastApplied(long lastApplied);
112 * Sets the ReplicatedLog instance.
114 * @param replicatedLog the ReplicatedLog instance.
116 void setReplicatedLog(@Nonnull ReplicatedLog replicatedLog);
119 * Returns the ReplicatedLog instance.
121 * @return the ReplicatedLog instance.
124 ReplicatedLog getReplicatedLog();
127 * Returns the The ActorSystem associated with this context.
129 * @return the ActorSystem.
132 ActorSystem getActorSystem();
135 * Returns the logger to be used for logging messages.
137 * @return the logger.
143 * Gets the address of a peer as a String. This is the same format in which a consumer would provide the address.
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.
149 String getPeerAddress(String peerId);
152 * Updates the peers and information to match the given ServerConfigurationPayload.
154 * @param serverCfgPayload the ServerConfigurationPayload.
156 void updatePeerIds(ServerConfigurationPayload serverCfgPayload);
159 * Returns the PeerInfo instances for each peer.
161 * @return list of PeerInfo
164 Collection<PeerInfo> getPeers();
167 * Returns the id's for each peer.
169 * @return the list of peer id's.
172 Collection<String> getPeerIds();
175 * Returns the PeerInfo for the given peer.
177 * @param peerId the id of the peer
178 * @return the PeerInfo or null if not found
181 PeerInfo getPeerInfo(String peerId);
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.
190 void addToPeers(String id, String address, VotingState votingState);
195 * @param id the id of the peer to remove.
197 void removePeer(String id);
200 * Returns an ActorSelection for a peer.
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.
206 ActorSelection getPeerActorSelection(String peerId);
209 * Sets the address of a peer.
211 * @param peerId the id of the peer.
212 * @param peerAddress the address of the peer.
214 void setPeerAddress(String peerId, String peerAddress);
217 * Returns the ConfigParams instance.
219 * @return the ConfigParams instance.
222 ConfigParams getConfigParams();
225 * Returns the SnapshotManager instance.
227 * @return the SnapshotManager instance.
230 SnapshotManager getSnapshotManager();
233 * Returns the DataPersistenceProvider instance.
235 * @return the DataPersistenceProvider instance.
238 DataPersistenceProvider getPersistenceProvider();
241 * Determines if there are any peer followers.
243 * @return true if there are followers otherwise false.
245 boolean hasFollowers();
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.
251 * @return the total memory.
253 long getTotalMemory();
256 * Sets the retriever of the total memory metric.
258 * @param retriever a supplier of the total memory metric.
261 void setTotalMemoryRetriever(LongSupplier retriever);
264 * Returns the payload version to be used when replicating data.
266 * @return the payload version.
268 short getPayloadVersion();
271 * Returns the RaftPolicy used to determine certain Raft behaviors.
273 * @return the RaftPolicy instance.
276 RaftPolicy getRaftPolicy();
279 * Determines if there have been any dynamic server configuration changes applied.
281 * @return true if dynamic server configuration changes have been applied, false otherwise, meaning that static
282 * peer configuration is still in use.
284 boolean isDynamicServerConfigurationInUse();
287 * Sets that dynamic server configuration changes have been applied.
289 void setDynamicServerConfigurationInUse();
292 * Returns the peer information as a ServerConfigurationPayload if dynamic server configurations have been applied.
294 * @param includeSelf include this peer's info.
295 * @return the peer information as a ServerConfigurationPayload or null if no dynamic server configurations have
299 ServerConfigurationPayload getPeerServerInfo(boolean includeSelf);
302 * Determines if this peer is a voting member of the cluster.
304 * @return true if this peer is a voting member, false otherwise.
306 boolean isVotingMember();
309 * Determines if there are any voting peers.
311 * @return true if there are any voting peers, false otherwise.
313 boolean anyVotingPeers();
316 * Returns the current behavior attached to the RaftActor.
318 * @return current behavior.
320 RaftActorBehavior getCurrentBehavior();
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.
326 * @return the Consumer
328 Consumer<ApplyState> getApplyStateConsumer();