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.Collection;
20 import java.util.HashMap;
22 import org.opendaylight.controller.cluster.DataPersistenceProvider;
23 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
24 import org.slf4j.Logger;
26 public class RaftActorContextImpl implements RaftActorContext {
28 private final ActorRef actor;
30 private final ActorContext context;
32 private final String id;
34 private final ElectionTerm termInformation;
36 private long commitIndex;
38 private long lastApplied;
40 private ReplicatedLog replicatedLog;
42 private final Map<String, PeerInfo> peerInfoMap = new HashMap<>();
44 private final Logger LOG;
46 private ConfigParams configParams;
49 private Supplier<Long> totalMemoryRetriever;
51 // Snapshot manager will need to be created on demand as it needs raft actor context which cannot
52 // be passed to it in the constructor
53 private SnapshotManager snapshotManager;
55 private final DataPersistenceProvider persistenceProvider;
57 private short payloadVersion;
59 public RaftActorContextImpl(ActorRef actor, ActorContext context, String id,
60 ElectionTerm termInformation, long commitIndex, long lastApplied, Map<String, String> peerAddresses,
61 ConfigParams configParams, DataPersistenceProvider persistenceProvider, Logger logger) {
63 this.context = context;
65 this.termInformation = termInformation;
66 this.commitIndex = commitIndex;
67 this.lastApplied = lastApplied;
68 this.configParams = configParams;
69 this.persistenceProvider = persistenceProvider;
72 for(Map.Entry<String, String> e: peerAddresses.entrySet()) {
73 peerInfoMap.put(e.getKey(), new PeerInfo(e.getKey(), e.getValue(), VotingState.VOTING));
77 public void setPayloadVersion(short payloadVersion) {
78 this.payloadVersion = payloadVersion;
82 public short getPayloadVersion() {
83 return payloadVersion;
86 public void setConfigParams(ConfigParams configParams) {
87 this.configParams = configParams;
91 public ActorRef actorOf(Props props){
92 return context.actorOf(props);
96 public ActorSelection actorSelection(String path){
97 return context.actorSelection(path);
101 public String getId() {
106 public ActorRef getActor() {
111 public ElectionTerm getTermInformation() {
112 return termInformation;
116 public long getCommitIndex() {
120 @Override public void setCommitIndex(long commitIndex) {
121 this.commitIndex = commitIndex;
125 public long getLastApplied() {
130 public void setLastApplied(long lastApplied) {
131 this.lastApplied = lastApplied;
135 public void setReplicatedLog(ReplicatedLog replicatedLog) {
136 this.replicatedLog = replicatedLog;
140 public ReplicatedLog getReplicatedLog() {
141 return replicatedLog;
144 @Override public ActorSystem getActorSystem() {
145 return context.system();
148 @Override public Logger getLogger() {
153 public Collection<String> getPeerIds() {
154 return peerInfoMap.keySet();
158 public Collection<PeerInfo> getPeers() {
159 return peerInfoMap.values();
163 public PeerInfo getPeerInfo(String peerId) {
164 return peerInfoMap.get(peerId);
168 public String getPeerAddress(String peerId) {
169 String peerAddress = null;
170 PeerInfo peerInfo = peerInfoMap.get(peerId);
171 if(peerInfo != null) {
172 peerAddress = peerInfo.getAddress();
173 if(peerAddress == null) {
174 peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
175 peerInfo.setAddress(peerAddress);
182 @Override public ConfigParams getConfigParams() {
187 public void addToPeers(String id, String address, VotingState votingState) {
188 peerInfoMap.put(id, new PeerInfo(id, address, votingState));
191 @Override public void removePeer(String name) {
192 peerInfoMap.remove(name);
195 @Override public ActorSelection getPeerActorSelection(String peerId) {
196 String peerAddress = getPeerAddress(peerId);
197 if(peerAddress != null){
198 return actorSelection(peerAddress);
204 public void setPeerAddress(String peerId, String peerAddress) {
205 PeerInfo peerInfo = peerInfoMap.get(peerId);
206 if(peerInfo != null) {
207 LOG.info("Peer address for peer {} set to {}", peerId, peerAddress);
208 peerInfo.setAddress(peerAddress);
213 public SnapshotManager getSnapshotManager() {
214 if(snapshotManager == null){
215 snapshotManager = new SnapshotManager(this, LOG);
217 return snapshotManager;
221 public long getTotalMemory() {
222 return totalMemoryRetriever != null ? totalMemoryRetriever.get() : Runtime.getRuntime().totalMemory();
226 public void setTotalMemoryRetriever(Supplier<Long> retriever) {
227 totalMemoryRetriever = retriever;
231 public boolean hasFollowers() {
232 return getPeerIds().size() > 0;
236 public DataPersistenceProvider getPersistenceProvider() {
237 return persistenceProvider;
242 public RaftPolicy getRaftPolicy() {
243 return configParams.getRaftPolicy();