blob: c4a7f19750ead0b79d0ee70931caa861d5ed3149 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.statistic.impl;
alshabib3d643ec2014-10-22 18:33:00 -070017
alshabibf6c2ede2014-10-22 23:31:50 -070018import com.google.common.collect.Sets;
Madan Jampani2bfa94c2015-04-11 05:03:49 -070019import org.onlab.util.Tools;
sangyun-han9f0af2d2016-08-04 13:04:59 +090020import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.cluster.ClusterService;
Madan Jampanic156dd02015-08-12 15:57:46 -070022import org.onosproject.cluster.NodeId;
23import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.flow.FlowEntry;
28import org.onosproject.net.flow.FlowRule;
29import org.onosproject.net.flow.instructions.Instruction;
30import org.onosproject.net.flow.instructions.Instructions;
31import org.onosproject.net.statistic.StatisticStore;
32import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
Madan Jampani78be2492016-06-03 23:27:07 -070033import org.onosproject.store.cluster.messaging.MessageSubject;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.store.serializers.KryoNamespaces;
Jordan Halterman2c83a102017-08-20 17:11:41 -070035import org.onosproject.store.service.Serializer;
sangyun-hanad84e0c2016-02-19 18:30:03 +090036import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037import org.osgi.service.component.annotations.Activate;
38import org.osgi.service.component.annotations.Component;
39import org.osgi.service.component.annotations.Deactivate;
40import org.osgi.service.component.annotations.Modified;
41import org.osgi.service.component.annotations.Reference;
42import org.osgi.service.component.annotations.ReferenceCardinality;
alshabib3d643ec2014-10-22 18:33:00 -070043import org.slf4j.Logger;
44
alshabib9c57bdd2014-11-28 19:14:06 -050045import java.util.Collections;
sangyun-hanad84e0c2016-02-19 18:30:03 +090046import java.util.Dictionary;
alshabib3d643ec2014-10-22 18:33:00 -070047import java.util.HashSet;
48import java.util.Map;
sangyun-hanad84e0c2016-02-19 18:30:03 +090049import java.util.Properties;
alshabib3d643ec2014-10-22 18:33:00 -070050import java.util.Set;
51import java.util.concurrent.ConcurrentHashMap;
Madan Jampani2af244a2015-02-22 13:12:01 -080052import java.util.concurrent.ExecutorService;
53import java.util.concurrent.Executors;
alshabib3d643ec2014-10-22 18:33:00 -070054import java.util.concurrent.TimeUnit;
alshabib3d643ec2014-10-22 18:33:00 -070055import java.util.concurrent.atomic.AtomicInteger;
56
sangyun-hanad84e0c2016-02-19 18:30:03 +090057import static com.google.common.base.Preconditions.checkArgument;
58import static com.google.common.base.Strings.isNullOrEmpty;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -070059import static java.util.concurrent.Executors.newFixedThreadPool;
sangyun-hanad84e0c2016-02-19 18:30:03 +090060import static org.onlab.util.Tools.get;
Madan Jampani2af244a2015-02-22 13:12:01 -080061import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska82041f52014-11-30 22:14:02 -080062import static org.slf4j.LoggerFactory.getLogger;
63
alshabib3d643ec2014-10-22 18:33:00 -070064
65/**
66 * Maintains statistics using RPC calls to collect stats from remote instances
67 * on demand.
68 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070069@Component(immediate = true, service = StatisticStore.class)
alshabib3d643ec2014-10-22 18:33:00 -070070public class DistributedStatisticStore implements StatisticStore {
71
72 private final Logger log = getLogger(getClass());
73
sangyun-hanad84e0c2016-02-19 18:30:03 +090074 private static final String FORMAT = "Setting: messageHandlerThreadPoolSize={}";
Madan Jampani2af244a2015-02-22 13:12:01 -080075
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Madan Jampanic156dd02015-08-12 15:57:46 -070077 protected MastershipService mastershipService;
alshabib3d643ec2014-10-22 18:33:00 -070078
Ray Milkeyd84f89b2018-08-17 14:54:17 -070079 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska82041f52014-11-30 22:14:02 -080080 protected ClusterCommunicationService clusterCommunicator;
alshabib3d643ec2014-10-22 18:33:00 -070081
Ray Milkeyd84f89b2018-08-17 14:54:17 -070082 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska82041f52014-11-30 22:14:02 -080083 protected ClusterService clusterService;
alshabib3d643ec2014-10-22 18:33:00 -070084
Ray Milkeyd84f89b2018-08-17 14:54:17 -070085 @Reference(cardinality = ReferenceCardinality.MANDATORY)
sangyun-han9f0af2d2016-08-04 13:04:59 +090086 protected ComponentConfigService cfgService;
87
Madan Jampani78be2492016-06-03 23:27:07 -070088 public static final MessageSubject GET_CURRENT = new MessageSubject("peer-return-current");
89 public static final MessageSubject GET_PREVIOUS = new MessageSubject("peer-return-previous");
90
alshabib3d643ec2014-10-22 18:33:00 -070091 private Map<ConnectPoint, InternalStatisticRepresentation> representations =
92 new ConcurrentHashMap<>();
93
94 private Map<ConnectPoint, Set<FlowEntry>> previous =
95 new ConcurrentHashMap<>();
96
97 private Map<ConnectPoint, Set<FlowEntry>> current =
98 new ConcurrentHashMap<>();
99
Jordan Halterman2c83a102017-08-20 17:11:41 -0700100 protected static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API);
alshabib3d643ec2014-10-22 18:33:00 -0700101
Madan Jampani2af244a2015-02-22 13:12:01 -0800102 private ExecutorService messageHandlingExecutor;
103
sangyun-hanad84e0c2016-02-19 18:30:03 +0900104 private static final int DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE = 4;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700105 //@Property(name = "messageHandlerThreadPoolSize", intValue = DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE,
106 // label = "Size of thread pool to assign message handler")
sangyun-hanad84e0c2016-02-19 18:30:03 +0900107 private static int messageHandlerThreadPoolSize = DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE;
108
alshabib3d643ec2014-10-22 18:33:00 -0700109 private static final long STATISTIC_STORE_TIMEOUT_MILLIS = 3000;
110
111 @Activate
sangyun-han9f0af2d2016-08-04 13:04:59 +0900112 public void activate(ComponentContext context) {
113 cfgService.registerProperties(getClass());
114
115 modified(context);
Madan Jampani2af244a2015-02-22 13:12:01 -0800116
117 messageHandlingExecutor = Executors.newFixedThreadPool(
sangyun-hanad84e0c2016-02-19 18:30:03 +0900118 messageHandlerThreadPoolSize,
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700119 groupedThreads("onos/store/statistic", "message-handlers", log));
Madan Jampani2af244a2015-02-22 13:12:01 -0800120
Madan Jampani1151b552015-08-12 16:11:27 -0700121 clusterCommunicator.<ConnectPoint, Set<FlowEntry>>addSubscriber(GET_CURRENT,
122 SERIALIZER::decode,
123 this::getCurrentStatisticInternal,
124 SERIALIZER::encode,
125 messageHandlingExecutor);
alshabib3d643ec2014-10-22 18:33:00 -0700126
Madan Jampani1151b552015-08-12 16:11:27 -0700127 clusterCommunicator.<ConnectPoint, Set<FlowEntry>>addSubscriber(GET_PREVIOUS,
128 SERIALIZER::decode,
129 this::getPreviousStatisticInternal,
130 SERIALIZER::encode,
131 messageHandlingExecutor);
alshabib3d643ec2014-10-22 18:33:00 -0700132
alshabib3d643ec2014-10-22 18:33:00 -0700133 log.info("Started");
134 }
135
136 @Deactivate
137 public void deactivate() {
sangyun-han9f0af2d2016-08-04 13:04:59 +0900138 cfgService.unregisterProperties(getClass(), false);
Madan Jampani2af244a2015-02-22 13:12:01 -0800139 clusterCommunicator.removeSubscriber(GET_PREVIOUS);
140 clusterCommunicator.removeSubscriber(GET_CURRENT);
141 messageHandlingExecutor.shutdown();
alshabib3d643ec2014-10-22 18:33:00 -0700142 log.info("Stopped");
143 }
144
sangyun-hanad84e0c2016-02-19 18:30:03 +0900145 @Modified
146 public void modified(ComponentContext context) {
147 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
148
149 int newMessageHandlerThreadPoolSize;
150
151 try {
152 String s = get(properties, "messageHandlerThreadPoolSize");
153
154 newMessageHandlerThreadPoolSize =
155 isNullOrEmpty(s) ? messageHandlerThreadPoolSize : Integer.parseInt(s.trim());
156
157 } catch (NumberFormatException e) {
158 log.warn(e.getMessage());
159 newMessageHandlerThreadPoolSize = messageHandlerThreadPoolSize;
160 }
161
162 // Any change in the following parameters implies thread pool restart
163 if (newMessageHandlerThreadPoolSize != messageHandlerThreadPoolSize) {
164 setMessageHandlerThreadPoolSize(newMessageHandlerThreadPoolSize);
165 restartMessageHandlerThreadPool();
166 }
167
168 log.info(FORMAT, messageHandlerThreadPoolSize);
169 }
170
171
alshabib3d643ec2014-10-22 18:33:00 -0700172 @Override
173 public void prepareForStatistics(FlowRule rule) {
174 ConnectPoint cp = buildConnectPoint(rule);
175 if (cp == null) {
176 return;
177 }
178 InternalStatisticRepresentation rep;
179 synchronized (representations) {
180 rep = getOrCreateRepresentation(cp);
181 }
182 rep.prepare();
183 }
184
185 @Override
alshabibf6c2ede2014-10-22 23:31:50 -0700186 public synchronized void removeFromStatistics(FlowRule rule) {
alshabib3d643ec2014-10-22 18:33:00 -0700187 ConnectPoint cp = buildConnectPoint(rule);
188 if (cp == null) {
189 return;
190 }
191 InternalStatisticRepresentation rep = representations.get(cp);
alshabib9c57bdd2014-11-28 19:14:06 -0500192 if (rep != null && rep.remove(rule)) {
193 updatePublishedStats(cp, Collections.emptySet());
alshabib3d643ec2014-10-22 18:33:00 -0700194 }
alshabibf6c2ede2014-10-22 23:31:50 -0700195 Set<FlowEntry> values = current.get(cp);
196 if (values != null) {
197 values.remove(rule);
198 }
199 values = previous.get(cp);
200 if (values != null) {
201 values.remove(rule);
202 }
203
alshabib3d643ec2014-10-22 18:33:00 -0700204 }
205
206 @Override
207 public void addOrUpdateStatistic(FlowEntry rule) {
208 ConnectPoint cp = buildConnectPoint(rule);
209 if (cp == null) {
210 return;
211 }
212 InternalStatisticRepresentation rep = representations.get(cp);
213 if (rep != null && rep.submit(rule)) {
214 updatePublishedStats(cp, rep.get());
215 }
216 }
217
218 private synchronized void updatePublishedStats(ConnectPoint cp,
219 Set<FlowEntry> flowEntries) {
220 Set<FlowEntry> curr = current.get(cp);
221 if (curr == null) {
222 curr = new HashSet<>();
223 }
224 previous.put(cp, curr);
225 current.put(cp, flowEntries);
226
227 }
228
229 @Override
230 public Set<FlowEntry> getCurrentStatistic(ConnectPoint connectPoint) {
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700231 final DeviceId deviceId = connectPoint.deviceId();
Madan Jampanic156dd02015-08-12 15:57:46 -0700232 NodeId master = mastershipService.getMasterFor(deviceId);
233 if (master == null) {
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700234 log.warn("No master for {}", deviceId);
Thomas Vachuska82041f52014-11-30 22:14:02 -0800235 return Collections.emptySet();
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700236 }
Madan Jampanic156dd02015-08-12 15:57:46 -0700237 if (master.equals(clusterService.getLocalNode().id())) {
alshabib3d643ec2014-10-22 18:33:00 -0700238 return getCurrentStatisticInternal(connectPoint);
239 } else {
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700240 return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(
241 connectPoint,
242 GET_CURRENT,
243 SERIALIZER::encode,
244 SERIALIZER::decode,
Madan Jampanic156dd02015-08-12 15:57:46 -0700245 master),
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700246 STATISTIC_STORE_TIMEOUT_MILLIS,
247 TimeUnit.MILLISECONDS,
248 Collections.emptySet());
alshabib3d643ec2014-10-22 18:33:00 -0700249 }
250
251 }
252
253 private synchronized Set<FlowEntry> getCurrentStatisticInternal(ConnectPoint connectPoint) {
254 return current.get(connectPoint);
255 }
256
257 @Override
258 public Set<FlowEntry> getPreviousStatistic(ConnectPoint connectPoint) {
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700259 final DeviceId deviceId = connectPoint.deviceId();
Madan Jampanic156dd02015-08-12 15:57:46 -0700260 NodeId master = mastershipService.getMasterFor(deviceId);
261 if (master == null) {
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700262 log.warn("No master for {}", deviceId);
Thomas Vachuska82041f52014-11-30 22:14:02 -0800263 return Collections.emptySet();
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700264 }
Madan Jampanic156dd02015-08-12 15:57:46 -0700265 if (master.equals(clusterService.getLocalNode().id())) {
alshabib3d643ec2014-10-22 18:33:00 -0700266 return getPreviousStatisticInternal(connectPoint);
267 } else {
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700268 return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(
269 connectPoint,
270 GET_PREVIOUS,
271 SERIALIZER::encode,
272 SERIALIZER::decode,
Madan Jampanic156dd02015-08-12 15:57:46 -0700273 master),
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700274 STATISTIC_STORE_TIMEOUT_MILLIS,
275 TimeUnit.MILLISECONDS,
276 Collections.emptySet());
alshabib3d643ec2014-10-22 18:33:00 -0700277 }
alshabib3d643ec2014-10-22 18:33:00 -0700278 }
279
280 private synchronized Set<FlowEntry> getPreviousStatisticInternal(ConnectPoint connectPoint) {
281 return previous.get(connectPoint);
282 }
283
284 private InternalStatisticRepresentation getOrCreateRepresentation(ConnectPoint cp) {
285
286 if (representations.containsKey(cp)) {
287 return representations.get(cp);
288 } else {
289 InternalStatisticRepresentation rep = new InternalStatisticRepresentation();
290 representations.put(cp, rep);
291 return rep;
292 }
293
294 }
295
296 private ConnectPoint buildConnectPoint(FlowRule rule) {
297 PortNumber port = getOutput(rule);
Jonathan Hart7baba072015-02-23 14:27:59 -0800298
alshabib3d643ec2014-10-22 18:33:00 -0700299 if (port == null) {
alshabib3d643ec2014-10-22 18:33:00 -0700300 return null;
301 }
302 ConnectPoint cp = new ConnectPoint(rule.deviceId(), port);
303 return cp;
304 }
305
306 private PortNumber getOutput(FlowRule rule) {
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700307 for (Instruction i : rule.treatment().allInstructions()) {
alshabib3d643ec2014-10-22 18:33:00 -0700308 if (i.type() == Instruction.Type.OUTPUT) {
309 Instructions.OutputInstruction out = (Instructions.OutputInstruction) i;
310 return out.port();
311 }
alshabib3d643ec2014-10-22 18:33:00 -0700312 }
313 return null;
314 }
315
316 private class InternalStatisticRepresentation {
317
318 private final AtomicInteger counter = new AtomicInteger(0);
319 private final Set<FlowEntry> rules = new HashSet<>();
320
321 public void prepare() {
322 counter.incrementAndGet();
323 }
324
alshabib9c57bdd2014-11-28 19:14:06 -0500325 public synchronized boolean remove(FlowRule rule) {
alshabib3d643ec2014-10-22 18:33:00 -0700326 rules.remove(rule);
alshabib9c57bdd2014-11-28 19:14:06 -0500327 return counter.decrementAndGet() == 0;
alshabib3d643ec2014-10-22 18:33:00 -0700328 }
329
330 public synchronized boolean submit(FlowEntry rule) {
331 if (rules.contains(rule)) {
332 rules.remove(rule);
333 }
334 rules.add(rule);
335 if (counter.get() == 0) {
336 return true;
337 } else {
338 return counter.decrementAndGet() == 0;
339 }
340 }
341
342 public synchronized Set<FlowEntry> get() {
343 counter.set(rules.size());
alshabibf6c2ede2014-10-22 23:31:50 -0700344 return Sets.newHashSet(rules);
alshabib3d643ec2014-10-22 18:33:00 -0700345 }
346
347
348 }
349
sangyun-hanad84e0c2016-02-19 18:30:03 +0900350 /**
351 * Sets thread pool size of message handler.
352 *
353 * @param poolSize
354 */
355 private void setMessageHandlerThreadPoolSize(int poolSize) {
356 checkArgument(poolSize >= 0, "Message handler pool size must be 0 or more");
357 messageHandlerThreadPoolSize = poolSize;
358 }
359
360 /**
361 * Restarts thread pool of message handler.
362 */
363 private void restartMessageHandlerThreadPool() {
364 ExecutorService prevExecutor = messageHandlingExecutor;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700365 messageHandlingExecutor = newFixedThreadPool(getMessageHandlerThreadPoolSize(),
366 groupedThreads("DistStatsStore", "messageHandling-%d", log));
sangyun-hanad84e0c2016-02-19 18:30:03 +0900367 prevExecutor.shutdown();
368 }
369
370 /**
371 * Gets current thread pool size of message handler.
372 *
373 * @return messageHandlerThreadPoolSize
374 */
375 private int getMessageHandlerThreadPoolSize() {
376 return messageHandlerThreadPoolSize;
377 }
378
alshabib3d643ec2014-10-22 18:33:00 -0700379}