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.ActorContext;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSelection;
14 import akka.actor.ActorSystem;
15 import akka.actor.Props;
16 import com.google.common.annotations.VisibleForTesting;
17 import com.google.common.base.Preconditions;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.List;
25 import java.util.function.Supplier;
26 import org.opendaylight.controller.cluster.DataPersistenceProvider;
27 import org.opendaylight.controller.cluster.raft.ServerConfigurationPayload.ServerInfo;
28 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
29 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
30 import org.slf4j.Logger;
32 public class RaftActorContextImpl implements RaftActorContext {
34 private final ActorRef actor;
36 private final ActorContext context;
38 private final String id;
40 private final ElectionTerm termInformation;
42 private long commitIndex;
44 private long lastApplied;
46 private ReplicatedLog replicatedLog;
48 private final Map<String, PeerInfo> peerInfoMap = new HashMap<>();
50 private final Logger LOG;
52 private ConfigParams configParams;
54 private boolean dynamicServerConfiguration = false;
57 private Supplier<Long> totalMemoryRetriever;
59 // Snapshot manager will need to be created on demand as it needs raft actor context which cannot
60 // be passed to it in the constructor
61 private SnapshotManager snapshotManager;
63 private final DataPersistenceProvider persistenceProvider;
65 private short payloadVersion;
67 private boolean votingMember = true;
69 private RaftActorBehavior currentBehavior;
71 public RaftActorContextImpl(ActorRef actor, ActorContext context, String id,
72 ElectionTerm termInformation, long commitIndex, long lastApplied, Map<String, String> peerAddresses,
73 ConfigParams configParams, DataPersistenceProvider persistenceProvider, Logger logger) {
75 this.context = context;
77 this.termInformation = termInformation;
78 this.commitIndex = commitIndex;
79 this.lastApplied = lastApplied;
80 this.configParams = configParams;
81 this.persistenceProvider = persistenceProvider;
84 for(Map.Entry<String, String> e: peerAddresses.entrySet()) {
85 peerInfoMap.put(e.getKey(), new PeerInfo(e.getKey(), e.getValue(), VotingState.VOTING));
90 public void setPayloadVersion(short payloadVersion) {
91 this.payloadVersion = payloadVersion;
95 public short getPayloadVersion() {
96 return payloadVersion;
99 public void setConfigParams(ConfigParams configParams) {
100 this.configParams = configParams;
104 public ActorRef actorOf(Props props){
105 return context.actorOf(props);
109 public ActorSelection actorSelection(String path){
110 return context.actorSelection(path);
114 public String getId() {
119 public ActorRef getActor() {
124 public ElectionTerm getTermInformation() {
125 return termInformation;
129 public long getCommitIndex() {
133 @Override public void setCommitIndex(long commitIndex) {
134 this.commitIndex = commitIndex;
138 public long getLastApplied() {
143 public void setLastApplied(long lastApplied) {
144 this.lastApplied = lastApplied;
148 public void setReplicatedLog(ReplicatedLog replicatedLog) {
149 this.replicatedLog = replicatedLog;
153 public ReplicatedLog getReplicatedLog() {
154 return replicatedLog;
157 @Override public ActorSystem getActorSystem() {
158 return context.system();
161 @Override public Logger getLogger() {
166 public Collection<String> getPeerIds() {
167 return peerInfoMap.keySet();
171 public Collection<PeerInfo> getPeers() {
172 return peerInfoMap.values();
176 public PeerInfo getPeerInfo(String peerId) {
177 return peerInfoMap.get(peerId);
181 public String getPeerAddress(String peerId) {
182 String peerAddress = null;
183 PeerInfo peerInfo = peerInfoMap.get(peerId);
184 if(peerInfo != null) {
185 peerAddress = peerInfo.getAddress();
186 if(peerAddress == null) {
187 peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
188 peerInfo.setAddress(peerAddress);
191 peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
198 public void updatePeerIds(ServerConfigurationPayload serverConfig){
200 boolean foundSelf = false;
201 Set<String> currentPeers = new HashSet<>(this.getPeerIds());
202 for(ServerInfo server: serverConfig.getServerConfig()) {
203 if(getId().equals(server.getId())) {
205 if(!server.isVoting()) {
206 votingMember = false;
209 VotingState votingState = server.isVoting() ? VotingState.VOTING: VotingState.NON_VOTING;
210 if(!currentPeers.contains(server.getId())) {
211 this.addToPeers(server.getId(), null, votingState);
213 this.getPeerInfo(server.getId()).setVotingState(votingState);
214 currentPeers.remove(server.getId());
219 for(String peerIdToRemove: currentPeers) {
220 this.removePeer(peerIdToRemove);
224 votingMember = false;
227 setDynamicServerConfigurationInUse();
230 @Override public ConfigParams getConfigParams() {
235 public void addToPeers(String id, String address, VotingState votingState) {
236 peerInfoMap.put(id, new PeerInfo(id, address, votingState));
240 public void removePeer(String name) {
241 if(getId().equals(name)) {
242 votingMember = false;
244 peerInfoMap.remove(name);
248 @Override public ActorSelection getPeerActorSelection(String peerId) {
249 String peerAddress = getPeerAddress(peerId);
250 if(peerAddress != null){
251 return actorSelection(peerAddress);
257 public void setPeerAddress(String peerId, String peerAddress) {
258 PeerInfo peerInfo = peerInfoMap.get(peerId);
259 if(peerInfo != null) {
260 LOG.info("Peer address for peer {} set to {}", peerId, peerAddress);
261 peerInfo.setAddress(peerAddress);
266 public SnapshotManager getSnapshotManager() {
267 if(snapshotManager == null){
268 snapshotManager = new SnapshotManager(this, LOG);
270 return snapshotManager;
274 public long getTotalMemory() {
275 return totalMemoryRetriever != null ? totalMemoryRetriever.get() : Runtime.getRuntime().totalMemory();
279 public void setTotalMemoryRetriever(Supplier<Long> retriever) {
280 totalMemoryRetriever = retriever;
284 public boolean hasFollowers() {
285 return getPeerIds().size() > 0;
289 public DataPersistenceProvider getPersistenceProvider() {
290 return persistenceProvider;
295 public RaftPolicy getRaftPolicy() {
296 return configParams.getRaftPolicy();
300 public boolean isDynamicServerConfigurationInUse() {
301 return dynamicServerConfiguration;
305 public void setDynamicServerConfigurationInUse() {
306 this.dynamicServerConfiguration = true;
310 public ServerConfigurationPayload getPeerServerInfo(boolean includeSelf) {
311 if (!isDynamicServerConfigurationInUse()) {
314 Collection<PeerInfo> peers = getPeers();
315 List<ServerInfo> newConfig = new ArrayList<>(peers.size() + 1);
316 for(PeerInfo peer: peers) {
317 newConfig.add(new ServerInfo(peer.getId(), peer.isVoting()));
321 newConfig.add(new ServerInfo(getId(), votingMember));
324 return (new ServerConfigurationPayload(newConfig));
328 public boolean isVotingMember() {
333 public RaftActorBehavior getCurrentBehavior() {
334 return currentBehavior;
337 void setCurrentBehavior(final RaftActorBehavior behavior) {
338 this.currentBehavior = Preconditions.checkNotNull(behavior);
342 if (currentBehavior != null) {
344 currentBehavior.close();
345 } catch (Exception e) {
346 LOG.debug("{}: Error closing behavior {}", getId(), currentBehavior.state());