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;
17 import com.google.common.annotations.VisibleForTesting;
18 import com.google.common.base.Supplier;
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;
26 import org.opendaylight.controller.cluster.DataPersistenceProvider;
27 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
28 import org.opendaylight.controller.cluster.raft.ServerConfigurationPayload.ServerInfo;
29 import org.slf4j.Logger;
31 public class RaftActorContextImpl implements RaftActorContext {
33 private final ActorRef actor;
35 private final ActorContext context;
37 private final String id;
39 private final ElectionTerm termInformation;
41 private long commitIndex;
43 private long lastApplied;
45 private ReplicatedLog replicatedLog;
47 private final Map<String, PeerInfo> peerInfoMap = new HashMap<>();
49 private final Logger LOG;
51 private ConfigParams configParams;
53 private boolean dynamicServerConfiguration = false;
56 private Supplier<Long> totalMemoryRetriever;
58 // Snapshot manager will need to be created on demand as it needs raft actor context which cannot
59 // be passed to it in the constructor
60 private SnapshotManager snapshotManager;
62 private final DataPersistenceProvider persistenceProvider;
64 private short payloadVersion;
66 public RaftActorContextImpl(ActorRef actor, ActorContext context, String id,
67 ElectionTerm termInformation, long commitIndex, long lastApplied, Map<String, String> peerAddresses,
68 ConfigParams configParams, DataPersistenceProvider persistenceProvider, Logger logger) {
70 this.context = context;
72 this.termInformation = termInformation;
73 this.commitIndex = commitIndex;
74 this.lastApplied = lastApplied;
75 this.configParams = configParams;
76 this.persistenceProvider = persistenceProvider;
79 for(Map.Entry<String, String> e: peerAddresses.entrySet()) {
80 peerInfoMap.put(e.getKey(), new PeerInfo(e.getKey(), e.getValue(), VotingState.VOTING));
84 public void setPayloadVersion(short payloadVersion) {
85 this.payloadVersion = payloadVersion;
89 public short getPayloadVersion() {
90 return payloadVersion;
93 public void setConfigParams(ConfigParams configParams) {
94 this.configParams = configParams;
98 public ActorRef actorOf(Props props){
99 return context.actorOf(props);
103 public ActorSelection actorSelection(String path){
104 return context.actorSelection(path);
108 public String getId() {
113 public ActorRef getActor() {
118 public ElectionTerm getTermInformation() {
119 return termInformation;
123 public long getCommitIndex() {
127 @Override public void setCommitIndex(long commitIndex) {
128 this.commitIndex = commitIndex;
132 public long getLastApplied() {
137 public void setLastApplied(long lastApplied) {
138 this.lastApplied = lastApplied;
142 public void setReplicatedLog(ReplicatedLog replicatedLog) {
143 this.replicatedLog = replicatedLog;
147 public ReplicatedLog getReplicatedLog() {
148 return replicatedLog;
151 @Override public ActorSystem getActorSystem() {
152 return context.system();
155 @Override public Logger getLogger() {
160 public Collection<String> getPeerIds() {
161 return peerInfoMap.keySet();
165 public Collection<PeerInfo> getPeers() {
166 return peerInfoMap.values();
170 public PeerInfo getPeerInfo(String peerId) {
171 return peerInfoMap.get(peerId);
175 public String getPeerAddress(String peerId) {
176 String peerAddress = null;
177 PeerInfo peerInfo = peerInfoMap.get(peerId);
178 if(peerInfo != null) {
179 peerAddress = peerInfo.getAddress();
180 if(peerAddress == null) {
181 peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
182 peerInfo.setAddress(peerAddress);
190 public void updatePeerIds(ServerConfigurationPayload serverConfig){
192 Set<String> currentPeers = new HashSet<>(this.getPeerIds());
193 for(ServerInfo server: serverConfig.getServerConfig()) {
194 if(!getId().equals(server.getId())) {
195 VotingState votingState = server.isVoting() ? VotingState.VOTING: VotingState.NON_VOTING;
196 if(!currentPeers.contains(server.getId())) {
197 this.addToPeers(server.getId(), null, votingState);
199 this.getPeerInfo(server.getId()).setVotingState(votingState);
200 currentPeers.remove(server.getId());
205 for(String peerIdToRemove: currentPeers) {
206 this.removePeer(peerIdToRemove);
208 setDynamicServerConfigurationInUse();
211 @Override public ConfigParams getConfigParams() {
216 public void addToPeers(String id, String address, VotingState votingState) {
217 peerInfoMap.put(id, new PeerInfo(id, address, votingState));
220 @Override public void removePeer(String name) {
221 peerInfoMap.remove(name);
224 @Override public ActorSelection getPeerActorSelection(String peerId) {
225 String peerAddress = getPeerAddress(peerId);
226 if(peerAddress != null){
227 return actorSelection(peerAddress);
233 public void setPeerAddress(String peerId, String peerAddress) {
234 PeerInfo peerInfo = peerInfoMap.get(peerId);
235 if(peerInfo != null) {
236 LOG.info("Peer address for peer {} set to {}", peerId, peerAddress);
237 peerInfo.setAddress(peerAddress);
242 public SnapshotManager getSnapshotManager() {
243 if(snapshotManager == null){
244 snapshotManager = new SnapshotManager(this, LOG);
246 return snapshotManager;
250 public long getTotalMemory() {
251 return totalMemoryRetriever != null ? totalMemoryRetriever.get() : Runtime.getRuntime().totalMemory();
255 public void setTotalMemoryRetriever(Supplier<Long> retriever) {
256 totalMemoryRetriever = retriever;
260 public boolean hasFollowers() {
261 return getPeerIds().size() > 0;
265 public DataPersistenceProvider getPersistenceProvider() {
266 return persistenceProvider;
271 public RaftPolicy getRaftPolicy() {
272 return configParams.getRaftPolicy();
276 public boolean isDynamicServerConfigurationInUse() {
277 return dynamicServerConfiguration;
281 public void setDynamicServerConfigurationInUse() {
282 this.dynamicServerConfiguration = true;
286 public ServerConfigurationPayload getPeerServerInfo() {
287 if (!isDynamicServerConfigurationInUse()) {
290 Collection<PeerInfo> peers = getPeers();
291 List<ServerInfo> newConfig = new ArrayList<>(peers.size() + 1);
292 for(PeerInfo peer: peers) {
293 newConfig.add(new ServerInfo(peer.getId(), peer.isVoting()));
296 newConfig.add(new ServerInfo(getId(), true));
297 return (new ServerConfigurationPayload(newConfig));