Move null check from getTotalMemory()
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / RaftActorContextImpl.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.raft;
10
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.Preconditions;
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;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.function.LongSupplier;
26 import org.opendaylight.controller.cluster.DataPersistenceProvider;
27 import org.opendaylight.controller.cluster.raft.ServerConfigurationPayload.ServerInfo;
28 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
29 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
30 import org.slf4j.Logger;
31
32 public class RaftActorContextImpl implements RaftActorContext {
33     private static final LongSupplier JVM_MEMORY_RETRIEVER = () -> Runtime.getRuntime().totalMemory();
34
35     private final ActorRef actor;
36
37     private final ActorContext context;
38
39     private final String id;
40
41     private final ElectionTerm termInformation;
42
43     private long commitIndex;
44
45     private long lastApplied;
46
47     private ReplicatedLog replicatedLog;
48
49     private final Map<String, PeerInfo> peerInfoMap = new HashMap<>();
50
51     private final Logger LOG;
52
53     private ConfigParams configParams;
54
55     private boolean dynamicServerConfiguration = false;
56
57     @VisibleForTesting
58     private LongSupplier totalMemoryRetriever = JVM_MEMORY_RETRIEVER;
59
60     // Snapshot manager will need to be created on demand as it needs raft actor context which cannot
61     // be passed to it in the constructor
62     private SnapshotManager snapshotManager;
63
64     private final DataPersistenceProvider persistenceProvider;
65
66     private short payloadVersion;
67
68     private boolean votingMember = true;
69
70     private RaftActorBehavior currentBehavior;
71
72     public RaftActorContextImpl(ActorRef actor, ActorContext context, String id,
73             ElectionTerm termInformation, long commitIndex, long lastApplied, Map<String, String> peerAddresses,
74             ConfigParams configParams, DataPersistenceProvider persistenceProvider, Logger logger) {
75         this.actor = actor;
76         this.context = context;
77         this.id = id;
78         this.termInformation = termInformation;
79         this.commitIndex = commitIndex;
80         this.lastApplied = lastApplied;
81         this.configParams = configParams;
82         this.persistenceProvider = persistenceProvider;
83         this.LOG = logger;
84
85         for(Map.Entry<String, String> e: peerAddresses.entrySet()) {
86             peerInfoMap.put(e.getKey(), new PeerInfo(e.getKey(), e.getValue(), VotingState.VOTING));
87         }
88     }
89
90     @VisibleForTesting
91     public void setPayloadVersion(short payloadVersion) {
92         this.payloadVersion = payloadVersion;
93     }
94
95     @Override
96     public short getPayloadVersion() {
97         return payloadVersion;
98     }
99
100     public void setConfigParams(ConfigParams configParams) {
101         this.configParams = configParams;
102     }
103
104     @Override
105     public ActorRef actorOf(Props props){
106         return context.actorOf(props);
107     }
108
109     @Override
110     public ActorSelection actorSelection(String path){
111         return context.actorSelection(path);
112     }
113
114     @Override
115     public String getId() {
116         return id;
117     }
118
119     @Override
120     public ActorRef getActor() {
121         return actor;
122     }
123
124     @Override
125     public ElectionTerm getTermInformation() {
126         return termInformation;
127     }
128
129     @Override
130     public long getCommitIndex() {
131         return commitIndex;
132     }
133
134     @Override public void setCommitIndex(long commitIndex) {
135         this.commitIndex = commitIndex;
136     }
137
138     @Override
139     public long getLastApplied() {
140         return lastApplied;
141     }
142
143     @Override
144     public void setLastApplied(long lastApplied) {
145         this.lastApplied = lastApplied;
146     }
147
148     @Override
149     public void setReplicatedLog(ReplicatedLog replicatedLog) {
150         this.replicatedLog = replicatedLog;
151     }
152
153     @Override
154     public ReplicatedLog getReplicatedLog() {
155         return replicatedLog;
156     }
157
158     @Override public ActorSystem getActorSystem() {
159         return context.system();
160     }
161
162     @Override public Logger getLogger() {
163         return this.LOG;
164     }
165
166     @Override
167     public Collection<String> getPeerIds() {
168         return peerInfoMap.keySet();
169     }
170
171     @Override
172     public Collection<PeerInfo> getPeers() {
173         return peerInfoMap.values();
174     }
175
176     @Override
177     public PeerInfo getPeerInfo(String peerId) {
178         return peerInfoMap.get(peerId);
179     }
180
181     @Override
182     public String getPeerAddress(String peerId) {
183         String peerAddress = null;
184         PeerInfo peerInfo = peerInfoMap.get(peerId);
185         if(peerInfo != null) {
186             peerAddress = peerInfo.getAddress();
187             if(peerAddress == null) {
188                 peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
189                 peerInfo.setAddress(peerAddress);
190             }
191         } else {
192             peerAddress = configParams.getPeerAddressResolver().resolve(peerId);
193         }
194
195         return peerAddress;
196     }
197
198     @Override
199     public void updatePeerIds(ServerConfigurationPayload serverConfig){
200         votingMember = true;
201         boolean foundSelf = false;
202         Set<String> currentPeers = new HashSet<>(this.getPeerIds());
203         for(ServerInfo server: serverConfig.getServerConfig()) {
204             if(getId().equals(server.getId())) {
205                 foundSelf = true;
206                 if(!server.isVoting()) {
207                     votingMember = false;
208                 }
209             } else {
210                 VotingState votingState = server.isVoting() ? VotingState.VOTING: VotingState.NON_VOTING;
211                 if(!currentPeers.contains(server.getId())) {
212                     this.addToPeers(server.getId(), null, votingState);
213                 } else {
214                     this.getPeerInfo(server.getId()).setVotingState(votingState);
215                     currentPeers.remove(server.getId());
216                 }
217             }
218         }
219
220         for(String peerIdToRemove: currentPeers) {
221             this.removePeer(peerIdToRemove);
222         }
223
224         if(!foundSelf) {
225             votingMember = false;
226         }
227
228         setDynamicServerConfigurationInUse();
229     }
230
231     @Override public ConfigParams getConfigParams() {
232         return configParams;
233     }
234
235     @Override
236     public void addToPeers(String id, String address, VotingState votingState) {
237         peerInfoMap.put(id, new PeerInfo(id, address, votingState));
238     }
239
240     @Override
241     public void removePeer(String name) {
242         if(getId().equals(name)) {
243             votingMember = false;
244         } else {
245             peerInfoMap.remove(name);
246         }
247     }
248
249     @Override public ActorSelection getPeerActorSelection(String peerId) {
250         String peerAddress = getPeerAddress(peerId);
251         if(peerAddress != null){
252             return actorSelection(peerAddress);
253         }
254         return null;
255     }
256
257     @Override
258     public void setPeerAddress(String peerId, String peerAddress) {
259         PeerInfo peerInfo = peerInfoMap.get(peerId);
260         if(peerInfo != null) {
261             LOG.info("Peer address for peer {} set to {}", peerId, peerAddress);
262             peerInfo.setAddress(peerAddress);
263         }
264     }
265
266     @Override
267     public SnapshotManager getSnapshotManager() {
268         if(snapshotManager == null){
269             snapshotManager = new SnapshotManager(this, LOG);
270         }
271         return snapshotManager;
272     }
273
274     @Override
275     public long getTotalMemory() {
276         return totalMemoryRetriever.getAsLong();
277     }
278
279     @Override
280     public void setTotalMemoryRetriever(LongSupplier retriever) {
281         totalMemoryRetriever = retriever == null ? JVM_MEMORY_RETRIEVER : retriever;
282     }
283
284     @Override
285     public boolean hasFollowers() {
286         return getPeerIds().size() > 0;
287     }
288
289     @Override
290     public DataPersistenceProvider getPersistenceProvider() {
291         return persistenceProvider;
292     }
293
294
295     @Override
296     public RaftPolicy getRaftPolicy() {
297         return configParams.getRaftPolicy();
298     }
299
300     @Override
301     public boolean isDynamicServerConfigurationInUse() {
302         return dynamicServerConfiguration;
303     }
304
305     @Override
306     public void setDynamicServerConfigurationInUse() {
307         this.dynamicServerConfiguration = true;
308     }
309
310     @Override
311     public ServerConfigurationPayload getPeerServerInfo(boolean includeSelf) {
312         if (!isDynamicServerConfigurationInUse()) {
313             return null;
314         }
315         Collection<PeerInfo> peers = getPeers();
316         List<ServerInfo> newConfig = new ArrayList<>(peers.size() + 1);
317         for(PeerInfo peer: peers) {
318             newConfig.add(new ServerInfo(peer.getId(), peer.isVoting()));
319         }
320
321         if(includeSelf) {
322             newConfig.add(new ServerInfo(getId(), votingMember));
323         }
324
325         return (new ServerConfigurationPayload(newConfig));
326     }
327
328     @Override
329     public boolean isVotingMember() {
330         return votingMember;
331     }
332
333     @Override
334     public RaftActorBehavior getCurrentBehavior() {
335         return currentBehavior;
336     }
337
338     void setCurrentBehavior(final RaftActorBehavior behavior) {
339         this.currentBehavior = Preconditions.checkNotNull(behavior);
340     }
341
342     void close() {
343         if (currentBehavior != null) {
344             try {
345                 currentBehavior.close();
346             } catch (Exception e) {
347                 LOG.debug("{}: Error closing behavior {}", getId(), currentBehavior.state());
348             }
349         }
350     }
351 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.