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));
89 public void setPayloadVersion(short payloadVersion) {
90 this.payloadVersion = payloadVersion;
94 public short getPayloadVersion() {
95 return payloadVersion;
98 public void setConfigParams(ConfigParams configParams) {
99 this.configParams = configParams;
103 public ActorRef actorOf(Props props){
104 return context.actorOf(props);
108 public ActorSelection actorSelection(String path){
109 return context.actorSelection(path);
113 public String getId() {
118 public ActorRef getActor() {
123 public ElectionTerm getTermInformation() {
124 return termInformation;
128 public long getCommitIndex() {
132 @Override public void setCommitIndex(long commitIndex) {
133 this.commitIndex = commitIndex;
137 public long getLastApplied() {
142 public void setLastApplied(long lastApplied) {
143 this.lastApplied = lastApplied;
147 public void setReplicatedLog(ReplicatedLog replicatedLog) {
148 this.replicatedLog = replicatedLog;
152 public ReplicatedLog getReplicatedLog() {
153 return replicatedLog;
156 @Override public ActorSystem getActorSystem() {
157 return context.system();
160 @Override public Logger getLogger() {
165 public Collection<String> getPeerIds() {
166 return peerInfoMap.keySet();
170 public Collection<PeerInfo> getPeers() {
171 return peerInfoMap.values();
175 public PeerInfo getPeerInfo(String peerId) {
176 return peerInfoMap.get(peerId);
180 public String getPeerAddress(String peerId) {
181 String peerAddress = null;
182 PeerInfo peerInfo = peerInfoMap.get(peerId);
183 if(peerInfo != null) {
184 peerAddress = peerInfo.getAddress();
185 if(peerAddress == null) {
186 peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
187 peerInfo.setAddress(peerAddress);
190 peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
197 public void updatePeerIds(ServerConfigurationPayload serverConfig){
199 boolean foundSelf = false;
200 Set<String> currentPeers = new HashSet<>(this.getPeerIds());
201 for(ServerInfo server: serverConfig.getServerConfig()) {
202 if(getId().equals(server.getId())) {
204 if(!server.isVoting()) {
205 votingMember = false;
208 VotingState votingState = server.isVoting() ? VotingState.VOTING: VotingState.NON_VOTING;
209 if(!currentPeers.contains(server.getId())) {
210 this.addToPeers(server.getId(), null, votingState);
212 this.getPeerInfo(server.getId()).setVotingState(votingState);
213 currentPeers.remove(server.getId());
218 for(String peerIdToRemove: currentPeers) {
219 this.removePeer(peerIdToRemove);
223 votingMember = false;
226 setDynamicServerConfigurationInUse();
229 @Override public ConfigParams getConfigParams() {
234 public void addToPeers(String id, String address, VotingState votingState) {
235 peerInfoMap.put(id, new PeerInfo(id, address, votingState));
239 public void removePeer(String name) {
240 if(getId().equals(name)) {
241 votingMember = false;
243 peerInfoMap.remove(name);
247 @Override public ActorSelection getPeerActorSelection(String peerId) {
248 String peerAddress = getPeerAddress(peerId);
249 if(peerAddress != null){
250 return actorSelection(peerAddress);
256 public void setPeerAddress(String peerId, String peerAddress) {
257 PeerInfo peerInfo = peerInfoMap.get(peerId);
258 if(peerInfo != null) {
259 LOG.info("Peer address for peer {} set to {}", peerId, peerAddress);
260 peerInfo.setAddress(peerAddress);
265 public SnapshotManager getSnapshotManager() {
266 if(snapshotManager == null){
267 snapshotManager = new SnapshotManager(this, LOG);
269 return snapshotManager;
273 public long getTotalMemory() {
274 return totalMemoryRetriever != null ? totalMemoryRetriever.get() : Runtime.getRuntime().totalMemory();
278 public void setTotalMemoryRetriever(Supplier<Long> retriever) {
279 totalMemoryRetriever = retriever;
283 public boolean hasFollowers() {
284 return getPeerIds().size() > 0;
288 public DataPersistenceProvider getPersistenceProvider() {
289 return persistenceProvider;
294 public RaftPolicy getRaftPolicy() {
295 return configParams.getRaftPolicy();
299 public boolean isDynamicServerConfigurationInUse() {
300 return dynamicServerConfiguration;
304 public void setDynamicServerConfigurationInUse() {
305 this.dynamicServerConfiguration = true;
309 public ServerConfigurationPayload getPeerServerInfo(boolean includeSelf) {
310 if (!isDynamicServerConfigurationInUse()) {
313 Collection<PeerInfo> peers = getPeers();
314 List<ServerInfo> newConfig = new ArrayList<>(peers.size() + 1);
315 for(PeerInfo peer: peers) {
316 newConfig.add(new ServerInfo(peer.getId(), peer.isVoting()));
320 newConfig.add(new ServerInfo(getId(), votingMember));
323 return (new ServerConfigurationPayload(newConfig));
327 public boolean isVotingMember() {
332 public RaftActorBehavior getCurrentBehavior() {
333 return currentBehavior;
336 void setCurrentBehavior(final RaftActorBehavior behavior) {
337 this.currentBehavior = Preconditions.checkNotNull(behavior);
341 if (currentBehavior != null) {
343 currentBehavior.close();
344 } catch (Exception e) {
345 LOG.debug("{}: Error closing behavior {}", getId(), currentBehavior.state());