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.Supplier;
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 org.opendaylight.controller.cluster.DataPersistenceProvider;
26 import org.opendaylight.controller.cluster.raft.ServerConfigurationPayload.ServerInfo;
27 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
28 import org.slf4j.Logger;
30 public class RaftActorContextImpl implements RaftActorContext {
32 private final ActorRef actor;
34 private final ActorContext context;
36 private final String id;
38 private final ElectionTerm termInformation;
40 private long commitIndex;
42 private long lastApplied;
44 private ReplicatedLog replicatedLog;
46 private final Map<String, PeerInfo> peerInfoMap = new HashMap<>();
48 private final Logger LOG;
50 private ConfigParams configParams;
52 private boolean dynamicServerConfiguration = false;
55 private Supplier<Long> totalMemoryRetriever;
57 // Snapshot manager will need to be created on demand as it needs raft actor context which cannot
58 // be passed to it in the constructor
59 private SnapshotManager snapshotManager;
61 private final DataPersistenceProvider persistenceProvider;
63 private short payloadVersion;
65 public RaftActorContextImpl(ActorRef actor, ActorContext context, String id,
66 ElectionTerm termInformation, long commitIndex, long lastApplied, Map<String, String> peerAddresses,
67 ConfigParams configParams, DataPersistenceProvider persistenceProvider, Logger logger) {
69 this.context = context;
71 this.termInformation = termInformation;
72 this.commitIndex = commitIndex;
73 this.lastApplied = lastApplied;
74 this.configParams = configParams;
75 this.persistenceProvider = persistenceProvider;
78 for(Map.Entry<String, String> e: peerAddresses.entrySet()) {
79 peerInfoMap.put(e.getKey(), new PeerInfo(e.getKey(), e.getValue(), VotingState.VOTING));
83 public void setPayloadVersion(short payloadVersion) {
84 this.payloadVersion = payloadVersion;
88 public short getPayloadVersion() {
89 return payloadVersion;
92 public void setConfigParams(ConfigParams configParams) {
93 this.configParams = configParams;
97 public ActorRef actorOf(Props props){
98 return context.actorOf(props);
102 public ActorSelection actorSelection(String path){
103 return context.actorSelection(path);
107 public String getId() {
112 public ActorRef getActor() {
117 public ElectionTerm getTermInformation() {
118 return termInformation;
122 public long getCommitIndex() {
126 @Override public void setCommitIndex(long commitIndex) {
127 this.commitIndex = commitIndex;
131 public long getLastApplied() {
136 public void setLastApplied(long lastApplied) {
137 this.lastApplied = lastApplied;
141 public void setReplicatedLog(ReplicatedLog replicatedLog) {
142 this.replicatedLog = replicatedLog;
146 public ReplicatedLog getReplicatedLog() {
147 return replicatedLog;
150 @Override public ActorSystem getActorSystem() {
151 return context.system();
154 @Override public Logger getLogger() {
159 public Collection<String> getPeerIds() {
160 return peerInfoMap.keySet();
164 public Collection<PeerInfo> getPeers() {
165 return peerInfoMap.values();
169 public PeerInfo getPeerInfo(String peerId) {
170 return peerInfoMap.get(peerId);
174 public String getPeerAddress(String peerId) {
175 String peerAddress = null;
176 PeerInfo peerInfo = peerInfoMap.get(peerId);
177 if(peerInfo != null) {
178 peerAddress = peerInfo.getAddress();
179 if(peerAddress == null) {
180 peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
181 peerInfo.setAddress(peerAddress);
184 peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
191 public void updatePeerIds(ServerConfigurationPayload serverConfig){
193 Set<String> currentPeers = new HashSet<>(this.getPeerIds());
194 for(ServerInfo server: serverConfig.getServerConfig()) {
195 if(!getId().equals(server.getId())) {
196 VotingState votingState = server.isVoting() ? VotingState.VOTING: VotingState.NON_VOTING;
197 if(!currentPeers.contains(server.getId())) {
198 this.addToPeers(server.getId(), null, votingState);
200 this.getPeerInfo(server.getId()).setVotingState(votingState);
201 currentPeers.remove(server.getId());
206 for(String peerIdToRemove: currentPeers) {
207 this.removePeer(peerIdToRemove);
209 setDynamicServerConfigurationInUse();
212 @Override public ConfigParams getConfigParams() {
217 public void addToPeers(String id, String address, VotingState votingState) {
218 peerInfoMap.put(id, new PeerInfo(id, address, votingState));
221 @Override public void removePeer(String name) {
222 peerInfoMap.remove(name);
225 @Override public ActorSelection getPeerActorSelection(String peerId) {
226 String peerAddress = getPeerAddress(peerId);
227 if(peerAddress != null){
228 return actorSelection(peerAddress);
234 public void setPeerAddress(String peerId, String peerAddress) {
235 PeerInfo peerInfo = peerInfoMap.get(peerId);
236 if(peerInfo != null) {
237 LOG.info("Peer address for peer {} set to {}", peerId, peerAddress);
238 peerInfo.setAddress(peerAddress);
243 public SnapshotManager getSnapshotManager() {
244 if(snapshotManager == null){
245 snapshotManager = new SnapshotManager(this, LOG);
247 return snapshotManager;
251 public long getTotalMemory() {
252 return totalMemoryRetriever != null ? totalMemoryRetriever.get() : Runtime.getRuntime().totalMemory();
256 public void setTotalMemoryRetriever(Supplier<Long> retriever) {
257 totalMemoryRetriever = retriever;
261 public boolean hasFollowers() {
262 return getPeerIds().size() > 0;
266 public DataPersistenceProvider getPersistenceProvider() {
267 return persistenceProvider;
272 public RaftPolicy getRaftPolicy() {
273 return configParams.getRaftPolicy();
277 public boolean isDynamicServerConfigurationInUse() {
278 return dynamicServerConfiguration;
282 public void setDynamicServerConfigurationInUse() {
283 this.dynamicServerConfiguration = true;
287 public ServerConfigurationPayload getPeerServerInfo() {
288 if (!isDynamicServerConfigurationInUse()) {
291 Collection<PeerInfo> peers = getPeers();
292 List<ServerInfo> newConfig = new ArrayList<>(peers.size() + 1);
293 for(PeerInfo peer: peers) {
294 newConfig.add(new ServerInfo(peer.getId(), peer.isVoting()));
297 newConfig.add(new ServerInfo(getId(), true));
298 return (new ServerConfigurationPayload(newConfig));