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.ActorRef;
12 import akka.actor.ActorSelection;
13 import akka.actor.ActorSystem;
14 import akka.actor.Props;
15 import akka.actor.UntypedActorContext;
17 import com.google.common.annotations.VisibleForTesting;
18 import com.google.common.base.Supplier;
19 import com.google.common.collect.Maps;
21 import java.util.Collection;
22 import java.util.HashMap;
25 import org.opendaylight.controller.cluster.DataPersistenceProvider;
26 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
27 import org.slf4j.Logger;
29 public class RaftActorContextImpl implements RaftActorContext {
31 private final ActorRef actor;
33 private final UntypedActorContext context;
35 private final String id;
37 private final ElectionTerm termInformation;
39 private long commitIndex;
41 private long lastApplied;
43 private ReplicatedLog replicatedLog;
45 private final Map<String, String> peerAddresses;
47 private final Logger LOG;
49 private ConfigParams configParams;
52 private Supplier<Long> totalMemoryRetriever;
54 // Snapshot manager will need to be created on demand as it needs raft actor context which cannot
55 // be passed to it in the constructor
56 private SnapshotManager snapshotManager;
58 private final DataPersistenceProvider persistenceProvider;
60 private short payloadVersion;
62 public RaftActorContextImpl(ActorRef actor, UntypedActorContext context, String id,
63 ElectionTerm termInformation, long commitIndex, long lastApplied, Map<String, String> peerAddresses,
64 ConfigParams configParams, DataPersistenceProvider persistenceProvider, Logger logger) {
66 this.context = context;
68 this.termInformation = termInformation;
69 this.commitIndex = commitIndex;
70 this.lastApplied = lastApplied;
71 this.peerAddresses = Maps.newHashMap(peerAddresses);
72 this.configParams = configParams;
73 this.persistenceProvider = persistenceProvider;
77 void setPayloadVersion(short payloadVersion) {
78 this.payloadVersion = payloadVersion;
82 public short getPayloadVersion() {
83 return payloadVersion;
86 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 Map<String, String> getPeerAddresses() {
154 return new HashMap<String, String>(peerAddresses);
158 public Collection<String> getPeerIds() {
159 return peerAddresses.keySet();
162 @Override public String getPeerAddress(String peerId) {
163 String peerAddress = peerAddresses.get(peerId);
164 if(peerAddress == null) {
165 peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
166 peerAddresses.put(peerId, peerAddress);
172 @Override public ConfigParams getConfigParams() {
176 @Override public void addToPeers(String name, String address) {
177 peerAddresses.put(name, address);
180 @Override public void removePeer(String name) {
181 peerAddresses.remove(name);
184 @Override public ActorSelection getPeerActorSelection(String peerId) {
185 String peerAddress = getPeerAddress(peerId);
186 if(peerAddress != null){
187 return actorSelection(peerAddress);
193 public void setPeerAddress(String peerId, String peerAddress) {
194 if(peerAddresses.containsKey(peerId)) {
195 LOG.info("Peer address for peer {} set to {}", peerId, peerAddress);
196 peerAddresses.put(peerId, peerAddress);
201 public SnapshotManager getSnapshotManager() {
202 if(snapshotManager == null){
203 snapshotManager = new SnapshotManager(this, LOG);
205 return snapshotManager;
209 public long getTotalMemory() {
210 return totalMemoryRetriever != null ? totalMemoryRetriever.get() : Runtime.getRuntime().totalMemory();
214 public void setTotalMemoryRetriever(Supplier<Long> retriever) {
215 totalMemoryRetriever = retriever;
219 public boolean hasFollowers() {
220 return getPeerIds().size() > 0;
224 public DataPersistenceProvider getPersistenceProvider() {
225 return persistenceProvider;
230 public RaftPolicy getRaftPolicy() {
231 return configParams.getRaftPolicy();