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 A reference to the RaftActor itself. This could be used to send messages
67 * The akka Cluster singleton for the actor system if one is configured.
69 * @return an Optional containing the Cluster instance is present.
71 Optional<Cluster> getCluster();
74 * Returns the current ElectionTerm information.
76 * @return the ElectionTerm.
79 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.
123 ReplicatedLog getReplicatedLog();
126 * Returns the The ActorSystem associated with this context.
128 * @return the ActorSystem.
131 ActorSystem getActorSystem();
134 * Returns the logger to be used for logging messages.
136 * @return the logger.
142 * Gets the address of a peer as a String. This is the same format in which a consumer would provide the address.
144 * @param peerId the id of the peer.
145 * @return the address of the peer or null if the address has not yet been resolved.
148 String getPeerAddress(String peerId);
151 * Updates the peers and information to match the given ServerConfigurationPayload.
153 * @param serverCfgPayload the ServerConfigurationPayload.
155 void updatePeerIds(ServerConfigurationPayload serverCfgPayload);
158 * Returns the PeerInfo instances for each peer.
160 * @return list of PeerInfo
163 Collection<PeerInfo> getPeers();
166 * Returns the id's for each peer.
168 * @return the list of peer id's.
171 Collection<String> getPeerIds();
174 * Returns the PeerInfo for the given peer.
177 * @return the PeerInfo or null if not found.
180 PeerInfo getPeerInfo(String peerId);
185 * @param id the id of the new peer.
186 * @param address the address of the new peer.
187 * @param votingState the VotingState of the new peer.
189 void addToPeers(String id, String address, VotingState votingState);
194 * @param id the id of the peer to remove.
196 void removePeer(String id);
199 * Returns an ActorSelection for a peer.
201 * @param peerId the id of the peer.
202 * @return the actorSelection corresponding to the peer or null if the address has not yet been resolved.
205 ActorSelection getPeerActorSelection(String peerId);
208 * Sets the address of a peer.
210 * @param peerId the id of the peer.
211 * @param peerAddress the address of the peer.
213 void setPeerAddress(String peerId, String peerAddress);
216 * Returns the ConfigParams instance.
218 * @return the ConfigParams instance.
221 ConfigParams getConfigParams();
224 * Returns the SnapshotManager instance.
226 * @return the SnapshotManager instance.
229 SnapshotManager getSnapshotManager();
232 * Returns the DataPersistenceProvider instance.
234 * @return the DataPersistenceProvider instance.
237 DataPersistenceProvider getPersistenceProvider();
240 * Determines if there are any peer followers.
242 * @return true if there are followers otherwise false.
244 boolean hasFollowers();
247 * Returns the total available memory for use in calculations. Normally this returns JVM's max memory but can be
248 * overridden for unit tests.
250 * @return the total memory.
252 long getTotalMemory();
255 * Sets the retriever of the total memory metric.
257 * @param retriever a supplier of the total memory metric.
260 void setTotalMemoryRetriever(LongSupplier retriever);
263 * Returns the payload version to be used when replicating data.
265 * @return the payload version.
267 short getPayloadVersion();
270 * Returns the RaftPolicy used to determine certain Raft behaviors.
272 * @return the RaftPolicy instance.
275 RaftPolicy getRaftPolicy();
278 * Determines if there have been any dynamic server configuration changes applied.
280 * @return true if dynamic server configuration changes have been applied, false otherwise, meaning that static
281 * peer configuration is still in use.
283 boolean isDynamicServerConfigurationInUse();
286 * Sets that dynamic server configuration changes have been applied.
288 void setDynamicServerConfigurationInUse();
291 * Returns the peer information as a ServerConfigurationPayload if dynamic server configurations have been applied.
293 * @param includeSelf include this peer's info.
294 * @return the peer information as a ServerConfigurationPayload or null if no dynamic server configurations have
298 ServerConfigurationPayload getPeerServerInfo(boolean includeSelf);
301 * Determines if this peer is a voting member of the cluster.
303 * @return true if this peer is a voting member, false otherwise.
305 boolean isVotingMember();
308 * Determines if there are any voting peers.
310 * @return true if there are any voting peers, false otherwise.
312 boolean anyVotingPeers();
315 * Returns the current behavior attached to the RaftActor.
317 * @return current behavior.
319 RaftActorBehavior getCurrentBehavior();