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.io.FileBackedOutputStream;
25 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
26 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
27 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
28 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
29 import org.slf4j.Logger;
32 * The RaftActorContext contains that portion of the RaftActors state that
33 * needs to be shared with it's behaviors. A RaftActorContext should NEVER be
34 * used in any actor context outside the RaftActor that constructed it.
36 public interface RaftActorContext {
38 * Creates a new local actor.
40 * @param props the Props used to create the actor.
41 * @return a reference to the newly created actor.
43 ActorRef actorOf(Props props);
46 * Creates an actor selection.
48 * @param path the path.
49 * @return an actor selection for the given actor path.
51 ActorSelection actorSelection(String path);
54 * Returns the identifier for the RaftActor. This identifier represents the
55 * name of the actor whose common state is being shared.
57 * @return the identifier
62 * Returns the reference to the RaftActor.
64 * @return the reference to the RaftActor itself. This can be used to send messages to the RaftActor
69 * The akka Cluster singleton for the actor system if one is configured.
71 * @return an Optional containing the Cluster instance is present.
73 Optional<Cluster> getCluster();
76 * Returns the current ElectionTerm information.
78 * @return the ElectionTerm.
81 ElectionTerm getTermInformation();
84 * Returns the index of highest log entry known to be committed.
86 * @return index of highest log entry known to be committed.
88 long getCommitIndex();
92 * Sets the index of highest log entry known to be committed.
94 * @param commitIndex new commit index
96 void setCommitIndex(long commitIndex);
99 * Returns index of highest log entry applied to state machine.
101 * @return index of highest log entry applied to state machine.
103 long getLastApplied();
106 * Sets index of highest log entry applied to state machine.
108 * @param lastApplied the new applied index.
110 void setLastApplied(long lastApplied);
113 * Sets the ReplicatedLog instance.
115 * @param replicatedLog the ReplicatedLog instance.
117 void setReplicatedLog(@Nonnull ReplicatedLog replicatedLog);
120 * Returns the ReplicatedLog instance.
122 * @return the ReplicatedLog instance.
125 ReplicatedLog getReplicatedLog();
128 * Returns the The ActorSystem associated with this context.
130 * @return the ActorSystem.
133 ActorSystem getActorSystem();
136 * Returns the logger to be used for logging messages.
138 * @return the logger.
144 * Gets the address of a peer as a String. This is the same format in which a consumer would provide the address.
146 * @param peerId the id of the peer.
147 * @return the address of the peer or null if the address has not yet been resolved.
150 String getPeerAddress(String peerId);
153 * Updates the peers and information to match the given ServerConfigurationPayload.
155 * @param serverCfgPayload the ServerConfigurationPayload.
157 void updatePeerIds(ServerConfigurationPayload serverCfgPayload);
160 * Returns the PeerInfo instances for each peer.
162 * @return list of PeerInfo
165 Collection<PeerInfo> getPeers();
168 * Returns the id's for each peer.
170 * @return the list of peer id's.
173 Collection<String> getPeerIds();
176 * Returns the PeerInfo for the given peer.
178 * @param peerId the id of the peer
179 * @return the PeerInfo or null if not found
182 PeerInfo getPeerInfo(String peerId);
187 * @param id the id of the new peer.
188 * @param address the address of the new peer.
189 * @param votingState the VotingState of the new peer.
191 void addToPeers(String id, String address, VotingState votingState);
196 * @param id the id of the peer to remove.
198 void removePeer(String id);
201 * Returns an ActorSelection for a peer.
203 * @param peerId the id of the peer.
204 * @return the actorSelection corresponding to the peer or null if the address has not yet been resolved.
207 ActorSelection getPeerActorSelection(String peerId);
210 * Sets the address of a peer.
212 * @param peerId the id of the peer.
213 * @param peerAddress the address of the peer.
215 void setPeerAddress(String peerId, String peerAddress);
218 * Returns the ConfigParams instance.
220 * @return the ConfigParams instance.
223 ConfigParams getConfigParams();
226 * Returns the SnapshotManager instance.
228 * @return the SnapshotManager instance.
231 SnapshotManager getSnapshotManager();
234 * Returns the DataPersistenceProvider instance.
236 * @return the DataPersistenceProvider instance.
239 DataPersistenceProvider getPersistenceProvider();
242 * Determines if there are any peer followers.
244 * @return true if there are followers otherwise false.
246 boolean hasFollowers();
249 * Returns the total available memory for use in calculations. Normally this returns JVM's max memory but can be
250 * overridden for unit tests.
252 * @return the total memory.
254 long getTotalMemory();
257 * Sets the retriever of the total memory metric.
259 * @param retriever a supplier of the total memory metric.
262 void setTotalMemoryRetriever(LongSupplier retriever);
265 * Returns the payload version to be used when replicating data.
267 * @return the payload version.
269 short getPayloadVersion();
272 * Returns the RaftPolicy used to determine certain Raft behaviors.
274 * @return the RaftPolicy instance.
277 RaftPolicy getRaftPolicy();
280 * Determines if there have been any dynamic server configuration changes applied.
282 * @return true if dynamic server configuration changes have been applied, false otherwise, meaning that static
283 * peer configuration is still in use.
285 boolean isDynamicServerConfigurationInUse();
288 * Sets that dynamic server configuration changes have been applied.
290 void setDynamicServerConfigurationInUse();
293 * Returns the peer information as a ServerConfigurationPayload if dynamic server configurations have been applied.
295 * @param includeSelf include this peer's info.
296 * @return the peer information as a ServerConfigurationPayload or null if no dynamic server configurations have
300 ServerConfigurationPayload getPeerServerInfo(boolean includeSelf);
303 * Determines if this peer is a voting member of the cluster.
305 * @return true if this peer is a voting member, false otherwise.
307 boolean isVotingMember();
310 * Determines if there are any voting peers.
312 * @return true if there are any voting peers, false otherwise.
314 boolean anyVotingPeers();
317 * Returns the current behavior attached to the RaftActor.
319 * @return current behavior.
321 RaftActorBehavior getCurrentBehavior();
324 * Returns the consumer of ApplyState operations. This is invoked by a behavior when a log entry needs to be
325 * applied to the state.
327 * @return the Consumer
329 Consumer<ApplyState> getApplyStateConsumer();
332 * Creates a FileBackedOutputStream with a common configuration.
334 * @return a FileBackedOutputStream instance
337 FileBackedOutputStream newFileBackedOutputStream();