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 static java.util.Objects.requireNonNull;
12 import akka.actor.ActorContext;
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSelection;
15 import akka.actor.ActorSystem;
16 import akka.actor.Props;
17 import akka.cluster.Cluster;
18 import com.google.common.annotations.VisibleForTesting;
19 import com.google.common.collect.ImmutableList;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.HashSet;
24 import java.util.Optional;
26 import java.util.concurrent.Executor;
27 import java.util.function.Consumer;
28 import java.util.function.LongSupplier;
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.opendaylight.controller.cluster.DataPersistenceProvider;
31 import org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory;
32 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
33 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
34 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
35 import org.opendaylight.controller.cluster.raft.persisted.ServerInfo;
36 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
37 import org.slf4j.Logger;
40 * Implementation of the RaftActorContext interface.
43 * @author Thomas Pantelis
45 public class RaftActorContextImpl implements RaftActorContext {
46 private static final LongSupplier JVM_MEMORY_RETRIEVER = () -> Runtime.getRuntime().maxMemory();
48 private final ActorRef actor;
50 private final ActorContext context;
52 private final @NonNull Executor executor;
54 private final String id;
56 private final ElectionTerm termInformation;
58 private long commitIndex;
60 private long lastApplied;
62 private ReplicatedLog replicatedLog;
64 private final Map<String, PeerInfo> peerInfoMap = new HashMap<>();
66 private final Logger log;
68 private ConfigParams configParams;
70 private boolean dynamicServerConfiguration = false;
73 private LongSupplier totalMemoryRetriever = JVM_MEMORY_RETRIEVER;
75 // Snapshot manager will need to be created on demand as it needs raft actor context which cannot
76 // be passed to it in the constructor
77 private SnapshotManager snapshotManager;
79 private final DataPersistenceProvider persistenceProvider;
81 private short payloadVersion;
83 private boolean votingMember = true;
85 private RaftActorBehavior currentBehavior;
87 private int numVotingPeers = -1;
89 private Optional<Cluster> cluster;
91 private final Consumer<ApplyState> applyStateConsumer;
93 private final FileBackedOutputStreamFactory fileBackedOutputStreamFactory;
95 private RaftActorLeadershipTransferCohort leadershipTransferCohort;
97 public RaftActorContextImpl(final ActorRef actor, final ActorContext context, final String id,
98 final @NonNull ElectionTerm termInformation, final long commitIndex, final long lastApplied,
99 final @NonNull Map<String, String> peerAddresses,
100 final @NonNull ConfigParams configParams, final @NonNull DataPersistenceProvider persistenceProvider,
101 final @NonNull Consumer<ApplyState> applyStateConsumer, final @NonNull Logger logger,
102 final @NonNull Executor executor) {
104 this.context = context;
106 this.termInformation = requireNonNull(termInformation);
107 this.executor = requireNonNull(executor);
108 this.commitIndex = commitIndex;
109 this.lastApplied = lastApplied;
110 this.configParams = requireNonNull(configParams);
111 this.persistenceProvider = requireNonNull(persistenceProvider);
112 log = requireNonNull(logger);
113 this.applyStateConsumer = requireNonNull(applyStateConsumer);
115 fileBackedOutputStreamFactory = new FileBackedOutputStreamFactory(
116 configParams.getFileBackedStreamingThreshold(), configParams.getTempFileDirectory());
118 for (Map.Entry<String, String> e : requireNonNull(peerAddresses).entrySet()) {
119 peerInfoMap.put(e.getKey(), new PeerInfo(e.getKey(), e.getValue(), VotingState.VOTING));
124 public void setPayloadVersion(final short payloadVersion) {
125 this.payloadVersion = payloadVersion;
129 public short getPayloadVersion() {
130 return payloadVersion;
133 public void setConfigParams(final ConfigParams configParams) {
134 this.configParams = configParams;
138 public ActorRef actorOf(final Props props) {
139 return context.actorOf(props);
143 public ActorSelection actorSelection(final String path) {
144 return context.actorSelection(path);
148 public String getId() {
153 public ActorRef getActor() {
158 public final Executor getExecutor() {
163 @SuppressWarnings("checkstyle:IllegalCatch")
164 public Optional<Cluster> getCluster() {
165 if (cluster == null) {
167 cluster = Optional.of(Cluster.get(getActorSystem()));
168 } catch (Exception e) {
169 // An exception means there's no cluster configured. This will only happen in unit tests.
170 log.debug("{}: Could not obtain Cluster", getId(), e);
171 cluster = Optional.empty();
179 public ElectionTerm getTermInformation() {
180 return termInformation;
184 public long getCommitIndex() {
188 @Override public void setCommitIndex(final long commitIndex) {
189 this.commitIndex = commitIndex;
193 public long getLastApplied() {
198 public void setLastApplied(final long lastApplied) {
199 final Throwable stackTrace = log.isTraceEnabled() ? new Throwable() : null;
200 log.debug("{}: Moving last applied index from {} to {}", id, this.lastApplied, lastApplied, stackTrace);
201 this.lastApplied = lastApplied;
205 public void setReplicatedLog(final ReplicatedLog replicatedLog) {
206 this.replicatedLog = replicatedLog;
210 public ReplicatedLog getReplicatedLog() {
211 return replicatedLog;
215 public ActorSystem getActorSystem() {
216 return context.system();
220 public Logger getLogger() {
225 public Collection<String> getPeerIds() {
226 return peerInfoMap.keySet();
230 public Collection<PeerInfo> getPeers() {
231 return peerInfoMap.values();
235 public PeerInfo getPeerInfo(final String peerId) {
236 return peerInfoMap.get(peerId);
240 public String getPeerAddress(final String peerId) {
242 PeerInfo peerInfo = peerInfoMap.get(peerId);
243 if (peerInfo != null) {
244 peerAddress = peerInfo.getAddress();
245 if (peerAddress == null) {
246 peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
247 peerInfo.setAddress(peerAddress);
250 peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
257 public void updatePeerIds(final ServerConfigurationPayload serverConfig) {
259 boolean foundSelf = false;
260 Set<String> currentPeers = new HashSet<>(getPeerIds());
261 for (ServerInfo server : serverConfig.getServerConfig()) {
262 if (getId().equals(server.getId())) {
264 if (!server.isVoting()) {
265 votingMember = false;
268 VotingState votingState = server.isVoting() ? VotingState.VOTING : VotingState.NON_VOTING;
269 if (!currentPeers.contains(server.getId())) {
270 addToPeers(server.getId(), null, votingState);
272 getPeerInfo(server.getId()).setVotingState(votingState);
273 currentPeers.remove(server.getId());
278 for (String peerIdToRemove : currentPeers) {
279 removePeer(peerIdToRemove);
283 votingMember = false;
286 log.debug("{}: Updated server config: isVoting: {}, peers: {}", id, votingMember, peerInfoMap.values());
288 setDynamicServerConfigurationInUse();
291 @Override public ConfigParams getConfigParams() {
296 public void addToPeers(final String peerId, final String address, final VotingState votingState) {
297 peerInfoMap.put(peerId, new PeerInfo(peerId, address, votingState));
302 public void removePeer(final String name) {
303 if (getId().equals(name)) {
304 votingMember = false;
306 peerInfoMap.remove(name);
311 @Override public ActorSelection getPeerActorSelection(final String peerId) {
312 String peerAddress = getPeerAddress(peerId);
313 if (peerAddress != null) {
314 return actorSelection(peerAddress);
320 public void setPeerAddress(final String peerId, final String peerAddress) {
321 PeerInfo peerInfo = peerInfoMap.get(peerId);
322 if (peerInfo != null) {
323 log.info("Peer address for peer {} set to {}", peerId, peerAddress);
324 peerInfo.setAddress(peerAddress);
329 public SnapshotManager getSnapshotManager() {
330 if (snapshotManager == null) {
331 snapshotManager = new SnapshotManager(this, log);
333 return snapshotManager;
337 public long getTotalMemory() {
338 return totalMemoryRetriever.getAsLong();
342 public void setTotalMemoryRetriever(final LongSupplier retriever) {
343 totalMemoryRetriever = retriever == null ? JVM_MEMORY_RETRIEVER : retriever;
347 public boolean hasFollowers() {
348 return !getPeerIds().isEmpty();
352 public DataPersistenceProvider getPersistenceProvider() {
353 return persistenceProvider;
358 public RaftPolicy getRaftPolicy() {
359 return configParams.getRaftPolicy();
363 public boolean isDynamicServerConfigurationInUse() {
364 return dynamicServerConfiguration;
368 public void setDynamicServerConfigurationInUse() {
369 dynamicServerConfiguration = true;
373 public ServerConfigurationPayload getPeerServerInfo(final boolean includeSelf) {
374 if (!isDynamicServerConfigurationInUse()) {
377 final var peers = getPeers();
378 final var newConfig = ImmutableList.<ServerInfo>builderWithExpectedSize(peers.size() + (includeSelf ? 1 : 0));
379 for (PeerInfo peer : peers) {
380 newConfig.add(new ServerInfo(peer.getId(), peer.isVoting()));
384 newConfig.add(new ServerInfo(getId(), votingMember));
387 return new ServerConfigurationPayload(newConfig.build());
391 public boolean isVotingMember() {
396 public boolean anyVotingPeers() {
397 if (numVotingPeers < 0) {
399 for (PeerInfo info: getPeers()) {
400 if (info.isVoting()) {
406 return numVotingPeers > 0;
410 public RaftActorBehavior getCurrentBehavior() {
411 return currentBehavior;
414 void setCurrentBehavior(final RaftActorBehavior behavior) {
415 currentBehavior = requireNonNull(behavior);
419 public Consumer<ApplyState> getApplyStateConsumer() {
420 return applyStateConsumer;
424 public FileBackedOutputStreamFactory getFileBackedOutputStreamFactory() {
425 return fileBackedOutputStreamFactory;
428 @SuppressWarnings("checkstyle:IllegalCatch")
430 if (currentBehavior != null) {
432 currentBehavior.close();
433 } catch (Exception e) {
434 log.debug("{}: Error closing behavior {}", getId(), currentBehavior.state(), e);
440 public RaftActorLeadershipTransferCohort getRaftActorLeadershipTransferCohort() {
441 return leadershipTransferCohort;
445 @SuppressWarnings("checkstyle:hiddenField")
446 public void setRaftActorLeadershipTransferCohort(final RaftActorLeadershipTransferCohort leadershipTransferCohort) {
447 this.leadershipTransferCohort = leadershipTransferCohort;