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
8 package org.opendaylight.controller.cluster.raft;
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSelection;
12 import akka.actor.ActorSystem;
13 import akka.actor.Props;
14 import akka.cluster.Cluster;
15 import com.google.common.annotations.VisibleForTesting;
16 import java.util.Collection;
17 import java.util.Optional;
18 import java.util.function.Consumer;
19 import java.util.function.LongSupplier;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.controller.cluster.DataPersistenceProvider;
23 import org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory;
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.
79 @NonNull ElectionTerm getTermInformation();
82 * Returns the index of highest log entry known to be committed.
84 * @return index of highest log entry known to be committed.
86 long getCommitIndex();
90 * Sets the index of highest log entry known to be committed.
92 * @param commitIndex new commit index
94 void setCommitIndex(long commitIndex);
97 * Returns index of highest log entry applied to state machine.
99 * @return index of highest log entry applied to state machine.
101 long getLastApplied();
104 * Sets index of highest log entry applied to state machine.
106 * @param lastApplied the new applied index.
108 void setLastApplied(long lastApplied);
111 * Sets the ReplicatedLog instance.
113 * @param replicatedLog the ReplicatedLog instance.
115 void setReplicatedLog(@NonNull ReplicatedLog replicatedLog);
118 * Returns the ReplicatedLog instance.
120 * @return the ReplicatedLog instance.
122 @NonNull ReplicatedLog getReplicatedLog();
125 * Returns the The ActorSystem associated with this context.
127 * @return the ActorSystem.
129 @NonNull ActorSystem getActorSystem();
132 * Returns the logger to be used for logging messages.
134 * @return the logger.
136 @NonNull Logger getLogger();
139 * Gets the address of a peer as a String. This is the same format in which a consumer would provide the address.
141 * @param peerId the id of the peer.
142 * @return the address of the peer or null if the address has not yet been resolved.
144 @Nullable String getPeerAddress(String peerId);
147 * Updates the peers and information to match the given ServerConfigurationPayload.
149 * @param serverCfgPayload the ServerConfigurationPayload.
151 void updatePeerIds(ServerConfigurationPayload serverCfgPayload);
154 * Returns the PeerInfo instances for each peer.
156 * @return list of PeerInfo
158 @NonNull Collection<PeerInfo> getPeers();
161 * Returns the id's for each peer.
163 * @return the list of peer id's.
165 @NonNull Collection<String> getPeerIds();
168 * Returns the PeerInfo for the given peer.
170 * @param peerId the id of the peer
171 * @return the PeerInfo or null if not found
173 @Nullable PeerInfo getPeerInfo(String peerId);
178 * @param id the id of the new peer.
179 * @param address the address of the new peer.
180 * @param votingState the VotingState of the new peer.
182 void addToPeers(String id, String address, VotingState votingState);
187 * @param id the id of the peer to remove.
189 void removePeer(String id);
192 * Returns an ActorSelection for a peer.
194 * @param peerId the id of the peer.
195 * @return the actorSelection corresponding to the peer or null if the address has not yet been resolved.
197 @Nullable ActorSelection getPeerActorSelection(String peerId);
200 * Sets the address of a peer.
202 * @param peerId the id of the peer.
203 * @param peerAddress the address of the peer.
205 void setPeerAddress(String peerId, String peerAddress);
208 * Returns the ConfigParams instance.
210 * @return the ConfigParams instance.
212 @NonNull ConfigParams getConfigParams();
215 * Returns the SnapshotManager instance.
217 * @return the SnapshotManager instance.
219 @NonNull SnapshotManager getSnapshotManager();
222 * Returns the DataPersistenceProvider instance.
224 * @return the DataPersistenceProvider instance.
226 @NonNull DataPersistenceProvider getPersistenceProvider();
229 * Determines if there are any peer followers.
231 * @return true if there are followers otherwise false.
233 boolean hasFollowers();
236 * Returns the total available memory for use in calculations. Normally this returns JVM's max memory but can be
237 * overridden for unit tests.
239 * @return the total memory.
241 long getTotalMemory();
244 * Sets the retriever of the total memory metric.
246 * @param retriever a supplier of the total memory metric.
249 void setTotalMemoryRetriever(LongSupplier retriever);
252 * Returns the payload version to be used when replicating data.
254 * @return the payload version.
256 short getPayloadVersion();
259 * Returns the RaftPolicy used to determine certain Raft behaviors.
261 * @return the RaftPolicy instance.
263 @NonNull RaftPolicy getRaftPolicy();
266 * Determines if there have been any dynamic server configuration changes applied.
268 * @return true if dynamic server configuration changes have been applied, false otherwise, meaning that static
269 * peer configuration is still in use.
271 boolean isDynamicServerConfigurationInUse();
274 * Sets that dynamic server configuration changes have been applied.
276 void setDynamicServerConfigurationInUse();
279 * Returns the peer information as a ServerConfigurationPayload if dynamic server configurations have been applied.
281 * @param includeSelf include this peer's info.
282 * @return the peer information as a ServerConfigurationPayload or null if no dynamic server configurations have
285 @Nullable ServerConfigurationPayload getPeerServerInfo(boolean includeSelf);
288 * Determines if this peer is a voting member of the cluster.
290 * @return true if this peer is a voting member, false otherwise.
292 boolean isVotingMember();
295 * Determines if there are any voting peers.
297 * @return true if there are any voting peers, false otherwise.
299 boolean anyVotingPeers();
302 * Returns the current behavior attached to the RaftActor.
304 * @return current behavior.
306 RaftActorBehavior getCurrentBehavior();
309 * Returns the consumer of ApplyState operations. This is invoked by a behavior when a log entry needs to be
310 * applied to the state.
312 * @return the Consumer
314 Consumer<ApplyState> getApplyStateConsumer();
317 * Returns the {@link FileBackedOutputStreamFactory} instance with a common configuration.
319 * @return the {@link FileBackedOutputStreamFactory};
321 @NonNull FileBackedOutputStreamFactory getFileBackedOutputStreamFactory();
324 * Returns the RaftActorLeadershipTransferCohort if leadership transfer is in progress.
326 * @return the RaftActorLeadershipTransferCohort if leadership transfer is in progress, null otherwise
328 @Nullable RaftActorLeadershipTransferCohort getRaftActorLeadershipTransferCohort();
331 * Sets the RaftActorLeadershipTransferCohort for transferring leadership.
333 * @param leadershipTransferCohort the RaftActorLeadershipTransferCohort or null to clear the existing one
335 void setRaftActorLeadershipTransferCohort(@Nullable RaftActorLeadershipTransferCohort leadershipTransferCohort);