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 private boolean votingMember = true;
67 public RaftActorContextImpl(ActorRef actor, ActorContext context, String id,
68 ElectionTerm termInformation, long commitIndex, long lastApplied, Map<String, String> peerAddresses,
69 ConfigParams configParams, DataPersistenceProvider persistenceProvider, Logger logger) {
71 this.context = context;
73 this.termInformation = termInformation;
74 this.commitIndex = commitIndex;
75 this.lastApplied = lastApplied;
76 this.configParams = configParams;
77 this.persistenceProvider = persistenceProvider;
80 for(Map.Entry<String, String> e: peerAddresses.entrySet()) {
81 peerInfoMap.put(e.getKey(), new PeerInfo(e.getKey(), e.getValue(), VotingState.VOTING));
85 public void setPayloadVersion(short payloadVersion) {
86 this.payloadVersion = payloadVersion;
90 public short getPayloadVersion() {
91 return payloadVersion;
94 public void setConfigParams(ConfigParams configParams) {
95 this.configParams = configParams;
99 public ActorRef actorOf(Props props){
100 return context.actorOf(props);
104 public ActorSelection actorSelection(String path){
105 return context.actorSelection(path);
109 public String getId() {
114 public ActorRef getActor() {
119 public ElectionTerm getTermInformation() {
120 return termInformation;
124 public long getCommitIndex() {
128 @Override public void setCommitIndex(long commitIndex) {
129 this.commitIndex = commitIndex;
133 public long getLastApplied() {
138 public void setLastApplied(long lastApplied) {
139 this.lastApplied = lastApplied;
143 public void setReplicatedLog(ReplicatedLog replicatedLog) {
144 this.replicatedLog = replicatedLog;
148 public ReplicatedLog getReplicatedLog() {
149 return replicatedLog;
152 @Override public ActorSystem getActorSystem() {
153 return context.system();
156 @Override public Logger getLogger() {
161 public Collection<String> getPeerIds() {
162 return peerInfoMap.keySet();
166 public Collection<PeerInfo> getPeers() {
167 return peerInfoMap.values();
171 public PeerInfo getPeerInfo(String peerId) {
172 return peerInfoMap.get(peerId);
176 public String getPeerAddress(String peerId) {
177 String peerAddress = null;
178 PeerInfo peerInfo = peerInfoMap.get(peerId);
179 if(peerInfo != null) {
180 peerAddress = peerInfo.getAddress();
181 if(peerAddress == null) {
182 peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
183 peerInfo.setAddress(peerAddress);
186 peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
193 public void updatePeerIds(ServerConfigurationPayload serverConfig){
195 Set<String> currentPeers = new HashSet<>(this.getPeerIds());
196 for(ServerInfo server: serverConfig.getServerConfig()) {
197 if(getId().equals(server.getId())) {
198 if(!server.isVoting()) {
199 votingMember = false;
202 VotingState votingState = server.isVoting() ? VotingState.VOTING: VotingState.NON_VOTING;
203 if(!currentPeers.contains(server.getId())) {
204 this.addToPeers(server.getId(), null, votingState);
206 this.getPeerInfo(server.getId()).setVotingState(votingState);
207 currentPeers.remove(server.getId());
212 for(String peerIdToRemove: currentPeers) {
213 this.removePeer(peerIdToRemove);
215 setDynamicServerConfigurationInUse();
218 @Override public ConfigParams getConfigParams() {
223 public void addToPeers(String id, String address, VotingState votingState) {
224 peerInfoMap.put(id, new PeerInfo(id, address, votingState));
228 public void removePeer(String name) {
229 if(getId().equals(name)) {
230 votingMember = false;
232 peerInfoMap.remove(name);
236 @Override public ActorSelection getPeerActorSelection(String peerId) {
237 String peerAddress = getPeerAddress(peerId);
238 if(peerAddress != null){
239 return actorSelection(peerAddress);
245 public void setPeerAddress(String peerId, String peerAddress) {
246 PeerInfo peerInfo = peerInfoMap.get(peerId);
247 if(peerInfo != null) {
248 LOG.info("Peer address for peer {} set to {}", peerId, peerAddress);
249 peerInfo.setAddress(peerAddress);
254 public SnapshotManager getSnapshotManager() {
255 if(snapshotManager == null){
256 snapshotManager = new SnapshotManager(this, LOG);
258 return snapshotManager;
262 public long getTotalMemory() {
263 return totalMemoryRetriever != null ? totalMemoryRetriever.get() : Runtime.getRuntime().totalMemory();
267 public void setTotalMemoryRetriever(Supplier<Long> retriever) {
268 totalMemoryRetriever = retriever;
272 public boolean hasFollowers() {
273 return getPeerIds().size() > 0;
277 public DataPersistenceProvider getPersistenceProvider() {
278 return persistenceProvider;
283 public RaftPolicy getRaftPolicy() {
284 return configParams.getRaftPolicy();
288 public boolean isDynamicServerConfigurationInUse() {
289 return dynamicServerConfiguration;
293 public void setDynamicServerConfigurationInUse() {
294 this.dynamicServerConfiguration = true;
298 public ServerConfigurationPayload getPeerServerInfo(boolean includeSelf) {
299 if (!isDynamicServerConfigurationInUse()) {
302 Collection<PeerInfo> peers = getPeers();
303 List<ServerInfo> newConfig = new ArrayList<>(peers.size() + 1);
304 for(PeerInfo peer: peers) {
305 newConfig.add(new ServerInfo(peer.getId(), peer.isVoting()));
309 newConfig.add(new ServerInfo(getId(), votingMember));
312 return (new ServerConfigurationPayload(newConfig));
316 public boolean isVotingMember() {