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 java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
25 import java.util.Optional;
27 import java.util.concurrent.Executor;
28 import java.util.function.Consumer;
29 import java.util.function.LongSupplier;
30 import org.eclipse.jdt.annotation.NonNull;
31 import org.opendaylight.controller.cluster.DataPersistenceProvider;
32 import org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory;
33 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
34 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
35 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
36 import org.opendaylight.controller.cluster.raft.persisted.ServerInfo;
37 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
38 import org.slf4j.Logger;
41 * Implementation of the RaftActorContext interface.
44 * @author Thomas Pantelis
46 public class RaftActorContextImpl implements RaftActorContext {
47 private static final LongSupplier JVM_MEMORY_RETRIEVER = () -> Runtime.getRuntime().maxMemory();
49 private final ActorRef actor;
51 private final ActorContext context;
53 private final @NonNull Executor executor;
55 private final String id;
57 private final ElectionTerm termInformation;
59 private long commitIndex;
61 private long lastApplied;
63 private ReplicatedLog replicatedLog;
65 private final Map<String, PeerInfo> peerInfoMap = new HashMap<>();
67 private final Logger log;
69 private ConfigParams configParams;
71 private boolean dynamicServerConfiguration = false;
74 private LongSupplier totalMemoryRetriever = JVM_MEMORY_RETRIEVER;
76 // Snapshot manager will need to be created on demand as it needs raft actor context which cannot
77 // be passed to it in the constructor
78 private SnapshotManager snapshotManager;
80 private final DataPersistenceProvider persistenceProvider;
82 private short payloadVersion;
84 private boolean votingMember = true;
86 private RaftActorBehavior currentBehavior;
88 private int numVotingPeers = -1;
90 private Optional<Cluster> cluster;
92 private final Consumer<ApplyState> applyStateConsumer;
94 private final FileBackedOutputStreamFactory fileBackedOutputStreamFactory;
96 private RaftActorLeadershipTransferCohort leadershipTransferCohort;
98 public RaftActorContextImpl(final ActorRef actor, final ActorContext context, final String id,
99 final @NonNull ElectionTerm termInformation, final long commitIndex, final long lastApplied,
100 final @NonNull Map<String, String> peerAddresses,
101 final @NonNull ConfigParams configParams, final @NonNull DataPersistenceProvider persistenceProvider,
102 final @NonNull Consumer<ApplyState> applyStateConsumer, final @NonNull Logger logger,
103 final @NonNull Executor executor) {
105 this.context = context;
107 this.termInformation = requireNonNull(termInformation);
108 this.executor = requireNonNull(executor);
109 this.commitIndex = commitIndex;
110 this.lastApplied = lastApplied;
111 this.configParams = requireNonNull(configParams);
112 this.persistenceProvider = requireNonNull(persistenceProvider);
113 this.log = requireNonNull(logger);
114 this.applyStateConsumer = requireNonNull(applyStateConsumer);
116 fileBackedOutputStreamFactory = new FileBackedOutputStreamFactory(
117 configParams.getFileBackedStreamingThreshold(), configParams.getTempFileDirectory());
119 for (Map.Entry<String, String> e : requireNonNull(peerAddresses).entrySet()) {
120 peerInfoMap.put(e.getKey(), new PeerInfo(e.getKey(), e.getValue(), VotingState.VOTING));
125 public void setPayloadVersion(final short payloadVersion) {
126 this.payloadVersion = payloadVersion;
130 public short getPayloadVersion() {
131 return payloadVersion;
134 public void setConfigParams(final ConfigParams configParams) {
135 this.configParams = configParams;
139 public ActorRef actorOf(final Props props) {
140 return context.actorOf(props);
144 public ActorSelection actorSelection(final String path) {
145 return context.actorSelection(path);
149 public String getId() {
154 public ActorRef getActor() {
159 public final Executor getExecutor() {
164 @SuppressWarnings("checkstyle:IllegalCatch")
165 public Optional<Cluster> getCluster() {
166 if (cluster == null) {
168 cluster = Optional.of(Cluster.get(getActorSystem()));
169 } catch (Exception e) {
170 // An exception means there's no cluster configured. This will only happen in unit tests.
171 log.debug("{}: Could not obtain Cluster", getId(), e);
172 cluster = Optional.empty();
180 public ElectionTerm getTermInformation() {
181 return termInformation;
185 public long getCommitIndex() {
189 @Override public void setCommitIndex(final long commitIndex) {
190 this.commitIndex = commitIndex;
194 public long getLastApplied() {
199 public void setLastApplied(final long lastApplied) {
200 final Throwable stackTrace = log.isTraceEnabled() ? new Throwable() : null;
201 log.debug("{}: Moving last applied index from {} to {}", id, this.lastApplied, lastApplied, stackTrace);
202 this.lastApplied = lastApplied;
206 public void setReplicatedLog(final ReplicatedLog replicatedLog) {
207 this.replicatedLog = replicatedLog;
211 public ReplicatedLog getReplicatedLog() {
212 return replicatedLog;
216 public ActorSystem getActorSystem() {
217 return context.system();
221 public Logger getLogger() {
226 public Collection<String> getPeerIds() {
227 return peerInfoMap.keySet();
231 public Collection<PeerInfo> getPeers() {
232 return peerInfoMap.values();
236 public PeerInfo getPeerInfo(final String peerId) {
237 return peerInfoMap.get(peerId);
241 public String getPeerAddress(final String peerId) {
243 PeerInfo peerInfo = peerInfoMap.get(peerId);
244 if (peerInfo != null) {
245 peerAddress = peerInfo.getAddress();
246 if (peerAddress == null) {
247 peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
248 peerInfo.setAddress(peerAddress);
251 peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
258 public void updatePeerIds(final ServerConfigurationPayload serverConfig) {
260 boolean foundSelf = false;
261 Set<String> currentPeers = new HashSet<>(this.getPeerIds());
262 for (ServerInfo server : serverConfig.getServerConfig()) {
263 if (getId().equals(server.getId())) {
265 if (!server.isVoting()) {
266 votingMember = false;
269 VotingState votingState = server.isVoting() ? VotingState.VOTING : VotingState.NON_VOTING;
270 if (!currentPeers.contains(server.getId())) {
271 this.addToPeers(server.getId(), null, votingState);
273 this.getPeerInfo(server.getId()).setVotingState(votingState);
274 currentPeers.remove(server.getId());
279 for (String peerIdToRemove : currentPeers) {
280 this.removePeer(peerIdToRemove);
284 votingMember = false;
287 log.debug("{}: Updated server config: isVoting: {}, peers: {}", id, votingMember, peerInfoMap.values());
289 setDynamicServerConfigurationInUse();
292 @Override public ConfigParams getConfigParams() {
297 public void addToPeers(final String peerId, final String address, final VotingState votingState) {
298 peerInfoMap.put(peerId, new PeerInfo(peerId, address, votingState));
303 public void removePeer(final String name) {
304 if (getId().equals(name)) {
305 votingMember = false;
307 peerInfoMap.remove(name);
312 @Override public ActorSelection getPeerActorSelection(final String peerId) {
313 String peerAddress = getPeerAddress(peerId);
314 if (peerAddress != null) {
315 return actorSelection(peerAddress);
321 public void setPeerAddress(final String peerId, final String peerAddress) {
322 PeerInfo peerInfo = peerInfoMap.get(peerId);
323 if (peerInfo != null) {
324 log.info("Peer address for peer {} set to {}", peerId, peerAddress);
325 peerInfo.setAddress(peerAddress);
330 public SnapshotManager getSnapshotManager() {
331 if (snapshotManager == null) {
332 snapshotManager = new SnapshotManager(this, log);
334 return snapshotManager;
338 public long getTotalMemory() {
339 return totalMemoryRetriever.getAsLong();
343 public void setTotalMemoryRetriever(final LongSupplier retriever) {
344 totalMemoryRetriever = retriever == null ? JVM_MEMORY_RETRIEVER : retriever;
348 public boolean hasFollowers() {
349 return !getPeerIds().isEmpty();
353 public DataPersistenceProvider getPersistenceProvider() {
354 return persistenceProvider;
359 public RaftPolicy getRaftPolicy() {
360 return configParams.getRaftPolicy();
364 public boolean isDynamicServerConfigurationInUse() {
365 return dynamicServerConfiguration;
369 public void setDynamicServerConfigurationInUse() {
370 this.dynamicServerConfiguration = true;
374 public ServerConfigurationPayload getPeerServerInfo(final boolean includeSelf) {
375 if (!isDynamicServerConfigurationInUse()) {
378 Collection<PeerInfo> peers = getPeers();
379 List<ServerInfo> newConfig = new ArrayList<>(peers.size() + 1);
380 for (PeerInfo peer: peers) {
381 newConfig.add(new ServerInfo(peer.getId(), peer.isVoting()));
385 newConfig.add(new ServerInfo(getId(), votingMember));
388 return new ServerConfigurationPayload(newConfig);
392 public boolean isVotingMember() {
397 public boolean anyVotingPeers() {
398 if (numVotingPeers < 0) {
400 for (PeerInfo info: getPeers()) {
401 if (info.isVoting()) {
407 return numVotingPeers > 0;
411 public RaftActorBehavior getCurrentBehavior() {
412 return currentBehavior;
415 void setCurrentBehavior(final RaftActorBehavior behavior) {
416 this.currentBehavior = requireNonNull(behavior);
420 public Consumer<ApplyState> getApplyStateConsumer() {
421 return applyStateConsumer;
425 public FileBackedOutputStreamFactory getFileBackedOutputStreamFactory() {
426 return fileBackedOutputStreamFactory;
429 @SuppressWarnings("checkstyle:IllegalCatch")
431 if (currentBehavior != null) {
433 currentBehavior.close();
434 } catch (Exception e) {
435 log.debug("{}: Error closing behavior {}", getId(), currentBehavior.state(), e);
441 public RaftActorLeadershipTransferCohort getRaftActorLeadershipTransferCohort() {
442 return leadershipTransferCohort;
446 @SuppressWarnings("checkstyle:hiddenField")
447 public void setRaftActorLeadershipTransferCohort(final RaftActorLeadershipTransferCohort leadershipTransferCohort) {
448 this.leadershipTransferCohort = leadershipTransferCohort;