blob: d9902fe989b257bb7d43871a724cd2f48b9ad142 [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;
Ray Milkeyb5646e62018-10-16 11:42:18 -070062import static org.onosproject.store.OsgiPropertyConstants.DSS_MESSAGE_HANDLER_THREAD_POOL_SIZE;
63import static org.onosproject.store.OsgiPropertyConstants.DSS_MESSAGE_HANDLER_THREAD_POOL_SIZE_DEFAULT;
Thomas Vachuska82041f52014-11-30 22:14:02 -080064import static org.slf4j.LoggerFactory.getLogger;
65
alshabib3d643ec2014-10-22 18:33:00 -070066
67/**
68 * Maintains statistics using RPC calls to collect stats from remote instances
69 * on demand.
70 */
Ray Milkeyb5646e62018-10-16 11:42:18 -070071@Component(
72 immediate = true,
73 service = StatisticStore.class,
74 property = {
75 DSS_MESSAGE_HANDLER_THREAD_POOL_SIZE + "=" + DSS_MESSAGE_HANDLER_THREAD_POOL_SIZE_DEFAULT
76 }
77)
alshabib3d643ec2014-10-22 18:33:00 -070078public class DistributedStatisticStore implements StatisticStore {
79
80 private final Logger log = getLogger(getClass());
81
sangyun-hanad84e0c2016-02-19 18:30:03 +090082 private static final String FORMAT = "Setting: messageHandlerThreadPoolSize={}";
Madan Jampani2af244a2015-02-22 13:12:01 -080083
Ray Milkeyd84f89b2018-08-17 14:54:17 -070084 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Madan Jampanic156dd02015-08-12 15:57:46 -070085 protected MastershipService mastershipService;
alshabib3d643ec2014-10-22 18:33:00 -070086
Ray Milkeyd84f89b2018-08-17 14:54:17 -070087 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska82041f52014-11-30 22:14:02 -080088 protected ClusterCommunicationService clusterCommunicator;
alshabib3d643ec2014-10-22 18:33:00 -070089
Ray Milkeyd84f89b2018-08-17 14:54:17 -070090 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska82041f52014-11-30 22:14:02 -080091 protected ClusterService clusterService;
alshabib3d643ec2014-10-22 18:33:00 -070092
Ray Milkeyd84f89b2018-08-17 14:54:17 -070093 @Reference(cardinality = ReferenceCardinality.MANDATORY)
sangyun-han9f0af2d2016-08-04 13:04:59 +090094 protected ComponentConfigService cfgService;
95
Madan Jampani78be2492016-06-03 23:27:07 -070096 public static final MessageSubject GET_CURRENT = new MessageSubject("peer-return-current");
97 public static final MessageSubject GET_PREVIOUS = new MessageSubject("peer-return-previous");
98
alshabib3d643ec2014-10-22 18:33:00 -070099 private Map<ConnectPoint, InternalStatisticRepresentation> representations =
100 new ConcurrentHashMap<>();
101
102 private Map<ConnectPoint, Set<FlowEntry>> previous =
103 new ConcurrentHashMap<>();
104
105 private Map<ConnectPoint, Set<FlowEntry>> current =
106 new ConcurrentHashMap<>();
107
Jordan Halterman2c83a102017-08-20 17:11:41 -0700108 protected static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API);
alshabib3d643ec2014-10-22 18:33:00 -0700109
Madan Jampani2af244a2015-02-22 13:12:01 -0800110 private ExecutorService messageHandlingExecutor;
111
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700112 //@Property(name = "messageHandlerThreadPoolSize", intValue = DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE,
113 // label = "Size of thread pool to assign message handler")
Ray Milkeyb5646e62018-10-16 11:42:18 -0700114 private static int messageHandlerThreadPoolSize = DSS_MESSAGE_HANDLER_THREAD_POOL_SIZE_DEFAULT;
sangyun-hanad84e0c2016-02-19 18:30:03 +0900115
alshabib3d643ec2014-10-22 18:33:00 -0700116 private static final long STATISTIC_STORE_TIMEOUT_MILLIS = 3000;
117
118 @Activate
sangyun-han9f0af2d2016-08-04 13:04:59 +0900119 public void activate(ComponentContext context) {
120 cfgService.registerProperties(getClass());
121
122 modified(context);
Madan Jampani2af244a2015-02-22 13:12:01 -0800123
124 messageHandlingExecutor = Executors.newFixedThreadPool(
sangyun-hanad84e0c2016-02-19 18:30:03 +0900125 messageHandlerThreadPoolSize,
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700126 groupedThreads("onos/store/statistic", "message-handlers", log));
Madan Jampani2af244a2015-02-22 13:12:01 -0800127
Madan Jampani1151b552015-08-12 16:11:27 -0700128 clusterCommunicator.<ConnectPoint, Set<FlowEntry>>addSubscriber(GET_CURRENT,
129 SERIALIZER::decode,
130 this::getCurrentStatisticInternal,
131 SERIALIZER::encode,
132 messageHandlingExecutor);
alshabib3d643ec2014-10-22 18:33:00 -0700133
Madan Jampani1151b552015-08-12 16:11:27 -0700134 clusterCommunicator.<ConnectPoint, Set<FlowEntry>>addSubscriber(GET_PREVIOUS,
135 SERIALIZER::decode,
136 this::getPreviousStatisticInternal,
137 SERIALIZER::encode,
138 messageHandlingExecutor);
alshabib3d643ec2014-10-22 18:33:00 -0700139
alshabib3d643ec2014-10-22 18:33:00 -0700140 log.info("Started");
141 }
142
143 @Deactivate
144 public void deactivate() {
sangyun-han9f0af2d2016-08-04 13:04:59 +0900145 cfgService.unregisterProperties(getClass(), false);
Madan Jampani2af244a2015-02-22 13:12:01 -0800146 clusterCommunicator.removeSubscriber(GET_PREVIOUS);
147 clusterCommunicator.removeSubscriber(GET_CURRENT);
148 messageHandlingExecutor.shutdown();
alshabib3d643ec2014-10-22 18:33:00 -0700149 log.info("Stopped");
150 }
151
sangyun-hanad84e0c2016-02-19 18:30:03 +0900152 @Modified
153 public void modified(ComponentContext context) {
154 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
155
156 int newMessageHandlerThreadPoolSize;
157
158 try {
159 String s = get(properties, "messageHandlerThreadPoolSize");
160
161 newMessageHandlerThreadPoolSize =
162 isNullOrEmpty(s) ? messageHandlerThreadPoolSize : Integer.parseInt(s.trim());
163
164 } catch (NumberFormatException e) {
165 log.warn(e.getMessage());
166 newMessageHandlerThreadPoolSize = messageHandlerThreadPoolSize;
167 }
168
169 // Any change in the following parameters implies thread pool restart
170 if (newMessageHandlerThreadPoolSize != messageHandlerThreadPoolSize) {
171 setMessageHandlerThreadPoolSize(newMessageHandlerThreadPoolSize);
172 restartMessageHandlerThreadPool();
173 }
174
175 log.info(FORMAT, messageHandlerThreadPoolSize);
176 }
177
178
alshabib3d643ec2014-10-22 18:33:00 -0700179 @Override
180 public void prepareForStatistics(FlowRule rule) {
181 ConnectPoint cp = buildConnectPoint(rule);
182 if (cp == null) {
183 return;
184 }
185 InternalStatisticRepresentation rep;
186 synchronized (representations) {
187 rep = getOrCreateRepresentation(cp);
188 }
189 rep.prepare();
190 }
191
192 @Override
alshabibf6c2ede2014-10-22 23:31:50 -0700193 public synchronized void removeFromStatistics(FlowRule rule) {
alshabib3d643ec2014-10-22 18:33:00 -0700194 ConnectPoint cp = buildConnectPoint(rule);
195 if (cp == null) {
196 return;
197 }
198 InternalStatisticRepresentation rep = representations.get(cp);
alshabib9c57bdd2014-11-28 19:14:06 -0500199 if (rep != null && rep.remove(rule)) {
200 updatePublishedStats(cp, Collections.emptySet());
alshabib3d643ec2014-10-22 18:33:00 -0700201 }
alshabibf6c2ede2014-10-22 23:31:50 -0700202 Set<FlowEntry> values = current.get(cp);
203 if (values != null) {
204 values.remove(rule);
205 }
206 values = previous.get(cp);
207 if (values != null) {
208 values.remove(rule);
209 }
210
alshabib3d643ec2014-10-22 18:33:00 -0700211 }
212
213 @Override
214 public void addOrUpdateStatistic(FlowEntry rule) {
215 ConnectPoint cp = buildConnectPoint(rule);
216 if (cp == null) {
217 return;
218 }
219 InternalStatisticRepresentation rep = representations.get(cp);
220 if (rep != null && rep.submit(rule)) {
221 updatePublishedStats(cp, rep.get());
222 }
223 }
224
225 private synchronized void updatePublishedStats(ConnectPoint cp,
226 Set<FlowEntry> flowEntries) {
227 Set<FlowEntry> curr = current.get(cp);
228 if (curr == null) {
229 curr = new HashSet<>();
230 }
231 previous.put(cp, curr);
232 current.put(cp, flowEntries);
233
234 }
235
236 @Override
237 public Set<FlowEntry> getCurrentStatistic(ConnectPoint connectPoint) {
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700238 final DeviceId deviceId = connectPoint.deviceId();
Madan Jampanic156dd02015-08-12 15:57:46 -0700239 NodeId master = mastershipService.getMasterFor(deviceId);
240 if (master == null) {
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700241 log.warn("No master for {}", deviceId);
Thomas Vachuska82041f52014-11-30 22:14:02 -0800242 return Collections.emptySet();
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700243 }
Madan Jampanic156dd02015-08-12 15:57:46 -0700244 if (master.equals(clusterService.getLocalNode().id())) {
alshabib3d643ec2014-10-22 18:33:00 -0700245 return getCurrentStatisticInternal(connectPoint);
246 } else {
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700247 return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(
248 connectPoint,
249 GET_CURRENT,
250 SERIALIZER::encode,
251 SERIALIZER::decode,
Madan Jampanic156dd02015-08-12 15:57:46 -0700252 master),
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700253 STATISTIC_STORE_TIMEOUT_MILLIS,
254 TimeUnit.MILLISECONDS,
255 Collections.emptySet());
alshabib3d643ec2014-10-22 18:33:00 -0700256 }
257
258 }
259
260 private synchronized Set<FlowEntry> getCurrentStatisticInternal(ConnectPoint connectPoint) {
261 return current.get(connectPoint);
262 }
263
264 @Override
265 public Set<FlowEntry> getPreviousStatistic(ConnectPoint connectPoint) {
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700266 final DeviceId deviceId = connectPoint.deviceId();
Madan Jampanic156dd02015-08-12 15:57:46 -0700267 NodeId master = mastershipService.getMasterFor(deviceId);
268 if (master == null) {
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700269 log.warn("No master for {}", deviceId);
Thomas Vachuska82041f52014-11-30 22:14:02 -0800270 return Collections.emptySet();
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700271 }
Madan Jampanic156dd02015-08-12 15:57:46 -0700272 if (master.equals(clusterService.getLocalNode().id())) {
alshabib3d643ec2014-10-22 18:33:00 -0700273 return getPreviousStatisticInternal(connectPoint);
274 } else {
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700275 return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(
276 connectPoint,
277 GET_PREVIOUS,
278 SERIALIZER::encode,
279 SERIALIZER::decode,
Madan Jampanic156dd02015-08-12 15:57:46 -0700280 master),
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700281 STATISTIC_STORE_TIMEOUT_MILLIS,
282 TimeUnit.MILLISECONDS,
283 Collections.emptySet());
alshabib3d643ec2014-10-22 18:33:00 -0700284 }
alshabib3d643ec2014-10-22 18:33:00 -0700285 }
286
287 private synchronized Set<FlowEntry> getPreviousStatisticInternal(ConnectPoint connectPoint) {
288 return previous.get(connectPoint);
289 }
290
291 private InternalStatisticRepresentation getOrCreateRepresentation(ConnectPoint cp) {
292
293 if (representations.containsKey(cp)) {
294 return representations.get(cp);
295 } else {
296 InternalStatisticRepresentation rep = new InternalStatisticRepresentation();
297 representations.put(cp, rep);
298 return rep;
299 }
300
301 }
302
303 private ConnectPoint buildConnectPoint(FlowRule rule) {
304 PortNumber port = getOutput(rule);
Jonathan Hart7baba072015-02-23 14:27:59 -0800305
alshabib3d643ec2014-10-22 18:33:00 -0700306 if (port == null) {
alshabib3d643ec2014-10-22 18:33:00 -0700307 return null;
308 }
309 ConnectPoint cp = new ConnectPoint(rule.deviceId(), port);
310 return cp;
311 }
312
313 private PortNumber getOutput(FlowRule rule) {
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700314 for (Instruction i : rule.treatment().allInstructions()) {
alshabib3d643ec2014-10-22 18:33:00 -0700315 if (i.type() == Instruction.Type.OUTPUT) {
316 Instructions.OutputInstruction out = (Instructions.OutputInstruction) i;
317 return out.port();
318 }
alshabib3d643ec2014-10-22 18:33:00 -0700319 }
320 return null;
321 }
322
323 private class InternalStatisticRepresentation {
324
325 private final AtomicInteger counter = new AtomicInteger(0);
326 private final Set<FlowEntry> rules = new HashSet<>();
327
328 public void prepare() {
329 counter.incrementAndGet();
330 }
331
alshabib9c57bdd2014-11-28 19:14:06 -0500332 public synchronized boolean remove(FlowRule rule) {
alshabib3d643ec2014-10-22 18:33:00 -0700333 rules.remove(rule);
alshabib9c57bdd2014-11-28 19:14:06 -0500334 return counter.decrementAndGet() == 0;
alshabib3d643ec2014-10-22 18:33:00 -0700335 }
336
337 public synchronized boolean submit(FlowEntry rule) {
338 if (rules.contains(rule)) {
339 rules.remove(rule);
340 }
341 rules.add(rule);
342 if (counter.get() == 0) {
343 return true;
344 } else {
345 return counter.decrementAndGet() == 0;
346 }
347 }
348
349 public synchronized Set<FlowEntry> get() {
350 counter.set(rules.size());
alshabibf6c2ede2014-10-22 23:31:50 -0700351 return Sets.newHashSet(rules);
alshabib3d643ec2014-10-22 18:33:00 -0700352 }
353
354
355 }
356
sangyun-hanad84e0c2016-02-19 18:30:03 +0900357 /**
358 * Sets thread pool size of message handler.
359 *
360 * @param poolSize
361 */
362 private void setMessageHandlerThreadPoolSize(int poolSize) {
363 checkArgument(poolSize >= 0, "Message handler pool size must be 0 or more");
364 messageHandlerThreadPoolSize = poolSize;
365 }
366
367 /**
368 * Restarts thread pool of message handler.
369 */
370 private void restartMessageHandlerThreadPool() {
371 ExecutorService prevExecutor = messageHandlingExecutor;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700372 messageHandlingExecutor = newFixedThreadPool(getMessageHandlerThreadPoolSize(),
373 groupedThreads("DistStatsStore", "messageHandling-%d", log));
sangyun-hanad84e0c2016-02-19 18:30:03 +0900374 prevExecutor.shutdown();
375 }
376
377 /**
378 * Gets current thread pool size of message handler.
379 *
380 * @return messageHandlerThreadPoolSize
381 */
382 private int getMessageHandlerThreadPoolSize() {
383 return messageHandlerThreadPoolSize;
384 }
385
alshabib3d643ec2014-10-22 18:33:00 -0700386}