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.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;
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.
33 public interface RaftActorContext {
35 * Creates a new local actor.
37 * @param props the Props used to create the actor.
38 * @return a reference to the newly created actor.
40 ActorRef actorOf(Props props);
43 * Creates an actor selection.
45 * @param path the path.
46 * @return an actor selection for the given actor path.
48 ActorSelection actorSelection(String path);
51 * Returns the identifier for the RaftActor. This identifier represents the
52 * name of the actor whose common state is being shared.
54 * @return the identifier
59 * Returns the reference to the RaftActor.
61 * @return the reference to the RaftActor itself. This can be used to send messages to the RaftActor
66 * The akka Cluster singleton for the actor system if one is configured.
68 * @return an Optional containing the Cluster instance is present.
70 Optional<Cluster> getCluster();
73 * Returns the current ElectionTerm information.
75 * @return the ElectionTerm.
78 ElectionTerm getTermInformation();
81 * Returns the index of highest log entry known to be committed.
83 * @return index of highest log entry known to be committed.
85 long getCommitIndex();
89 * Sets the index of highest log entry known to be committed.
91 * @param commitIndex new commit index
93 void setCommitIndex(long commitIndex);
96 * Returns index of highest log entry applied to state machine.
98 * @return index of highest log entry applied to state machine.
100 long getLastApplied();
103 * Sets index of highest log entry applied to state machine.
105 * @param lastApplied the new applied index.
107 void setLastApplied(long lastApplied);
110 * Sets the ReplicatedLog instance.
112 * @param replicatedLog the ReplicatedLog instance.
114 void setReplicatedLog(@Nonnull ReplicatedLog replicatedLog);
117 * Returns the ReplicatedLog instance.
119 * @return the ReplicatedLog instance.
122 ReplicatedLog getReplicatedLog();
125 * Returns the The ActorSystem associated with this context.
127 * @return the ActorSystem.
130 ActorSystem getActorSystem();
133 * Returns the logger to be used for logging messages.
135 * @return the logger.
141 * Gets the address of a peer as a String. This is the same format in which a consumer would provide the address.
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.
147 String getPeerAddress(String peerId);
150 * Updates the peers and information to match the given ServerConfigurationPayload.
152 * @param serverCfgPayload the ServerConfigurationPayload.
154 void updatePeerIds(ServerConfigurationPayload serverCfgPayload);
157 * Returns the PeerInfo instances for each peer.
159 * @return list of PeerInfo
162 Collection<PeerInfo> getPeers();
165 * Returns the id's for each peer.
167 * @return the list of peer id's.
170 Collection<String> getPeerIds();
173 * Returns the PeerInfo for the given peer.
175 * @param peerId the id of the peer
176 * @return the PeerInfo or null if not found
179 PeerInfo getPeerInfo(String peerId);
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.
188 void addToPeers(String id, String address, VotingState votingState);
193 * @param id the id of the peer to remove.
195 void removePeer(String id);
198 * Returns an ActorSelection for a peer.
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.
204 ActorSelection getPeerActorSelection(String peerId);
207 * Sets the address of a peer.
209 * @param peerId the id of the peer.
210 * @param peerAddress the address of the peer.
212 void setPeerAddress(String peerId, String peerAddress);
215 * Returns the ConfigParams instance.
217 * @return the ConfigParams instance.
220 ConfigParams getConfigParams();
223 * Returns the SnapshotManager instance.
225 * @return the SnapshotManager instance.
228 SnapshotManager getSnapshotManager();
231 * Returns the DataPersistenceProvider instance.
233 * @return the DataPersistenceProvider instance.
236 DataPersistenceProvider getPersistenceProvider();
239 * Determines if there are any peer followers.
241 * @return true if there are followers otherwise false.
243 boolean hasFollowers();
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.
249 * @return the total memory.
251 long getTotalMemory();
254 * Sets the retriever of the total memory metric.
256 * @param retriever a supplier of the total memory metric.
259 void setTotalMemoryRetriever(LongSupplier retriever);
262 * Returns the payload version to be used when replicating data.
264 * @return the payload version.
266 short getPayloadVersion();
269 * Returns the RaftPolicy used to determine certain Raft behaviors.
271 * @return the RaftPolicy instance.
274 RaftPolicy getRaftPolicy();
277 * Determines if there have been any dynamic server configuration changes applied.
279 * @return true if dynamic server configuration changes have been applied, false otherwise, meaning that static
280 * peer configuration is still in use.
282 boolean isDynamicServerConfigurationInUse();
285 * Sets that dynamic server configuration changes have been applied.
287 void setDynamicServerConfigurationInUse();
290 * Returns the peer information as a ServerConfigurationPayload if dynamic server configurations have been applied.
292 * @param includeSelf include this peer's info.
293 * @return the peer information as a ServerConfigurationPayload or null if no dynamic server configurations have
297 ServerConfigurationPayload getPeerServerInfo(boolean includeSelf);
300 * Determines if this peer is a voting member of the cluster.
302 * @return true if this peer is a voting member, false otherwise.
304 boolean isVotingMember();
307 * Determines if there are any voting peers.
309 * @return true if there are any voting peers, false otherwise.
311 boolean anyVotingPeers();
314 * Returns the current behavior attached to the RaftActor.
316 * @return current behavior.
318 RaftActorBehavior getCurrentBehavior();