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 static com.google.common.base.Preconditions.checkState;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSelection;
14 import akka.actor.ActorSystem;
15 import akka.actor.Props;
16 import akka.actor.UntypedActorContext;
17 import com.google.common.annotations.VisibleForTesting;
18 import com.google.common.base.Supplier;
20 import org.slf4j.Logger;
22 public class RaftActorContextImpl implements RaftActorContext {
24 private final ActorRef actor;
26 private final UntypedActorContext context;
28 private final String id;
30 private final ElectionTerm termInformation;
32 private long commitIndex;
34 private long lastApplied;
36 private ReplicatedLog replicatedLog;
38 private final Map<String, String> peerAddresses;
40 private final Logger LOG;
42 private ConfigParams configParams;
45 private Supplier<Long> totalMemoryRetriever;
47 // Snapshot manager will need to be created on demand as it needs raft actor context which cannot
48 // be passed to it in the constructor
49 private SnapshotManager snapshotManager;
51 public RaftActorContextImpl(ActorRef actor, UntypedActorContext context, String id,
52 ElectionTerm termInformation, long commitIndex, long lastApplied, Map<String, String> peerAddresses,
53 ConfigParams configParams, Logger logger) {
55 this.context = context;
57 this.termInformation = termInformation;
58 this.commitIndex = commitIndex;
59 this.lastApplied = lastApplied;
60 this.peerAddresses = peerAddresses;
61 this.configParams = configParams;
65 void setConfigParams(ConfigParams configParams) {
66 this.configParams = configParams;
70 public ActorRef actorOf(Props props){
71 return context.actorOf(props);
75 public ActorSelection actorSelection(String path){
76 return context.actorSelection(path);
80 public String getId() {
85 public ActorRef getActor() {
90 public ElectionTerm getTermInformation() {
91 return termInformation;
95 public long getCommitIndex() {
99 @Override public void setCommitIndex(long commitIndex) {
100 this.commitIndex = commitIndex;
104 public long getLastApplied() {
108 @Override public void setLastApplied(long lastApplied) {
109 this.lastApplied = lastApplied;
112 @Override public void setReplicatedLog(ReplicatedLog replicatedLog) {
113 this.replicatedLog = replicatedLog;
116 @Override public ReplicatedLog getReplicatedLog() {
117 return replicatedLog;
120 @Override public ActorSystem getActorSystem() {
121 return context.system();
124 @Override public Logger getLogger() {
128 @Override public Map<String, String> getPeerAddresses() {
129 return peerAddresses;
132 @Override public String getPeerAddress(String peerId) {
133 return peerAddresses.get(peerId);
136 @Override public ConfigParams getConfigParams() {
140 @Override public void addToPeers(String name, String address) {
141 peerAddresses.put(name, address);
144 @Override public void removePeer(String name) {
145 peerAddresses.remove(name);
148 @Override public ActorSelection getPeerActorSelection(String peerId) {
149 String peerAddress = getPeerAddress(peerId);
150 if(peerAddress != null){
151 return actorSelection(peerAddress);
156 @Override public void setPeerAddress(String peerId, String peerAddress) {
157 LOG.info("Peer address for peer {} set to {}", peerId, peerAddress);
158 checkState(peerAddresses.containsKey(peerId), peerId + " is unknown");
160 peerAddresses.put(peerId, peerAddress);
164 public SnapshotManager getSnapshotManager() {
165 if(snapshotManager == null){
166 snapshotManager = new SnapshotManager(this, LOG);
168 return snapshotManager;
172 public long getTotalMemory() {
173 return totalMemoryRetriever != null ? totalMemoryRetriever.get() : Runtime.getRuntime().totalMemory();
177 public void setTotalMemoryRetriever(Supplier<Long> retriever) {
178 totalMemoryRetriever = retriever;
182 public boolean hasFollowers() {
183 return getPeerAddresses().keySet().size() > 0;