blob: 51552f0bbb3ed78c59abd36b7385d34be104ae56 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
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 -070019
alshabib3d643ec2014-10-22 18:33:00 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
sangyun-hanad84e0c2016-02-19 18:30:03 +090023import org.apache.felix.scr.annotations.Modified;
24import org.apache.felix.scr.annotations.Property;
alshabib3d643ec2014-10-22 18:33:00 -070025import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
27import org.apache.felix.scr.annotations.Service;
Madan Jampani2bfa94c2015-04-11 05:03:49 -070028import org.onlab.util.Tools;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.cluster.ClusterService;
Madan Jampanic156dd02015-08-12 15:57:46 -070030import org.onosproject.cluster.NodeId;
31import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.DeviceId;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.flow.FlowEntry;
36import org.onosproject.net.flow.FlowRule;
37import org.onosproject.net.flow.instructions.Instruction;
38import org.onosproject.net.flow.instructions.Instructions;
39import org.onosproject.net.statistic.StatisticStore;
40import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
Madan Jampani78be2492016-06-03 23:27:07 -070041import org.onosproject.store.cluster.messaging.MessageSubject;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.store.serializers.KryoNamespaces;
HIGUCHI Yutae7290652016-05-18 11:29:01 -070043import org.onosproject.store.serializers.StoreSerializer;
sangyun-hanad84e0c2016-02-19 18:30:03 +090044import org.osgi.service.component.ComponentContext;
alshabib3d643ec2014-10-22 18:33:00 -070045import org.slf4j.Logger;
46
alshabib9c57bdd2014-11-28 19:14:06 -050047import java.util.Collections;
sangyun-hanad84e0c2016-02-19 18:30:03 +090048import java.util.Dictionary;
alshabib3d643ec2014-10-22 18:33:00 -070049import java.util.HashSet;
50import java.util.Map;
sangyun-hanad84e0c2016-02-19 18:30:03 +090051import java.util.Properties;
alshabib3d643ec2014-10-22 18:33:00 -070052import java.util.Set;
53import java.util.concurrent.ConcurrentHashMap;
Madan Jampani2af244a2015-02-22 13:12:01 -080054import java.util.concurrent.ExecutorService;
55import java.util.concurrent.Executors;
alshabib3d643ec2014-10-22 18:33:00 -070056import java.util.concurrent.TimeUnit;
alshabib3d643ec2014-10-22 18:33:00 -070057import java.util.concurrent.atomic.AtomicInteger;
58
sangyun-hanad84e0c2016-02-19 18:30:03 +090059import static com.google.common.base.Preconditions.checkArgument;
60import static com.google.common.base.Strings.isNullOrEmpty;
61import static org.onlab.util.Tools.get;
Madan Jampani2af244a2015-02-22 13:12:01 -080062import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska82041f52014-11-30 22:14:02 -080063import static org.slf4j.LoggerFactory.getLogger;
64
alshabib3d643ec2014-10-22 18:33:00 -070065
66/**
67 * Maintains statistics using RPC calls to collect stats from remote instances
68 * on demand.
69 */
70@Component(immediate = true)
71@Service
72public class DistributedStatisticStore implements StatisticStore {
73
74 private final Logger log = getLogger(getClass());
75
sangyun-hanad84e0c2016-02-19 18:30:03 +090076 private static final String FORMAT = "Setting: messageHandlerThreadPoolSize={}";
Madan Jampani2af244a2015-02-22 13:12:01 -080077
alshabib3d643ec2014-10-22 18:33:00 -070078 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Madan Jampanic156dd02015-08-12 15:57:46 -070079 protected MastershipService mastershipService;
alshabib3d643ec2014-10-22 18:33:00 -070080
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuska82041f52014-11-30 22:14:02 -080082 protected ClusterCommunicationService clusterCommunicator;
alshabib3d643ec2014-10-22 18:33:00 -070083
84 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuska82041f52014-11-30 22:14:02 -080085 protected ClusterService clusterService;
alshabib3d643ec2014-10-22 18:33:00 -070086
Madan Jampani78be2492016-06-03 23:27:07 -070087 public static final MessageSubject GET_CURRENT = new MessageSubject("peer-return-current");
88 public static final MessageSubject GET_PREVIOUS = new MessageSubject("peer-return-previous");
89
alshabib3d643ec2014-10-22 18:33:00 -070090 private Map<ConnectPoint, InternalStatisticRepresentation> representations =
91 new ConcurrentHashMap<>();
92
93 private Map<ConnectPoint, Set<FlowEntry>> previous =
94 new ConcurrentHashMap<>();
95
96 private Map<ConnectPoint, Set<FlowEntry>> current =
97 new ConcurrentHashMap<>();
98
HIGUCHI Yutae7290652016-05-18 11:29:01 -070099 protected static final StoreSerializer SERIALIZER = StoreSerializer.using(KryoNamespaces.API);
alshabib3d643ec2014-10-22 18:33:00 -0700100
Madan Jampani2af244a2015-02-22 13:12:01 -0800101 private ExecutorService messageHandlingExecutor;
102
sangyun-hanad84e0c2016-02-19 18:30:03 +0900103 private static final int DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE = 4;
104 @Property(name = "messageHandlerThreadPoolSize", intValue = DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE,
105 label = "Size of thread pool to assign message handler")
106 private static int messageHandlerThreadPoolSize = DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE;
107
alshabib3d643ec2014-10-22 18:33:00 -0700108 private static final long STATISTIC_STORE_TIMEOUT_MILLIS = 3000;
109
110 @Activate
111 public void activate() {
Madan Jampani2af244a2015-02-22 13:12:01 -0800112
113 messageHandlingExecutor = Executors.newFixedThreadPool(
sangyun-hanad84e0c2016-02-19 18:30:03 +0900114 messageHandlerThreadPoolSize,
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700115 groupedThreads("onos/store/statistic", "message-handlers", log));
Madan Jampani2af244a2015-02-22 13:12:01 -0800116
Madan Jampani1151b552015-08-12 16:11:27 -0700117 clusterCommunicator.<ConnectPoint, Set<FlowEntry>>addSubscriber(GET_CURRENT,
118 SERIALIZER::decode,
119 this::getCurrentStatisticInternal,
120 SERIALIZER::encode,
121 messageHandlingExecutor);
alshabib3d643ec2014-10-22 18:33:00 -0700122
Madan Jampani1151b552015-08-12 16:11:27 -0700123 clusterCommunicator.<ConnectPoint, Set<FlowEntry>>addSubscriber(GET_PREVIOUS,
124 SERIALIZER::decode,
125 this::getPreviousStatisticInternal,
126 SERIALIZER::encode,
127 messageHandlingExecutor);
alshabib3d643ec2014-10-22 18:33:00 -0700128
alshabib3d643ec2014-10-22 18:33:00 -0700129 log.info("Started");
130 }
131
132 @Deactivate
133 public void deactivate() {
Madan Jampani2af244a2015-02-22 13:12:01 -0800134 clusterCommunicator.removeSubscriber(GET_PREVIOUS);
135 clusterCommunicator.removeSubscriber(GET_CURRENT);
136 messageHandlingExecutor.shutdown();
alshabib3d643ec2014-10-22 18:33:00 -0700137 log.info("Stopped");
138 }
139
sangyun-hanad84e0c2016-02-19 18:30:03 +0900140 @Modified
141 public void modified(ComponentContext context) {
142 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
143
144 int newMessageHandlerThreadPoolSize;
145
146 try {
147 String s = get(properties, "messageHandlerThreadPoolSize");
148
149 newMessageHandlerThreadPoolSize =
150 isNullOrEmpty(s) ? messageHandlerThreadPoolSize : Integer.parseInt(s.trim());
151
152 } catch (NumberFormatException e) {
153 log.warn(e.getMessage());
154 newMessageHandlerThreadPoolSize = messageHandlerThreadPoolSize;
155 }
156
157 // Any change in the following parameters implies thread pool restart
158 if (newMessageHandlerThreadPoolSize != messageHandlerThreadPoolSize) {
159 setMessageHandlerThreadPoolSize(newMessageHandlerThreadPoolSize);
160 restartMessageHandlerThreadPool();
161 }
162
163 log.info(FORMAT, messageHandlerThreadPoolSize);
164 }
165
166
alshabib3d643ec2014-10-22 18:33:00 -0700167 @Override
168 public void prepareForStatistics(FlowRule rule) {
169 ConnectPoint cp = buildConnectPoint(rule);
170 if (cp == null) {
171 return;
172 }
173 InternalStatisticRepresentation rep;
174 synchronized (representations) {
175 rep = getOrCreateRepresentation(cp);
176 }
177 rep.prepare();
178 }
179
180 @Override
alshabibf6c2ede2014-10-22 23:31:50 -0700181 public synchronized void removeFromStatistics(FlowRule rule) {
alshabib3d643ec2014-10-22 18:33:00 -0700182 ConnectPoint cp = buildConnectPoint(rule);
183 if (cp == null) {
184 return;
185 }
186 InternalStatisticRepresentation rep = representations.get(cp);
alshabib9c57bdd2014-11-28 19:14:06 -0500187 if (rep != null && rep.remove(rule)) {
188 updatePublishedStats(cp, Collections.emptySet());
alshabib3d643ec2014-10-22 18:33:00 -0700189 }
alshabibf6c2ede2014-10-22 23:31:50 -0700190 Set<FlowEntry> values = current.get(cp);
191 if (values != null) {
192 values.remove(rule);
193 }
194 values = previous.get(cp);
195 if (values != null) {
196 values.remove(rule);
197 }
198
alshabib3d643ec2014-10-22 18:33:00 -0700199 }
200
201 @Override
202 public void addOrUpdateStatistic(FlowEntry rule) {
203 ConnectPoint cp = buildConnectPoint(rule);
204 if (cp == null) {
205 return;
206 }
207 InternalStatisticRepresentation rep = representations.get(cp);
208 if (rep != null && rep.submit(rule)) {
209 updatePublishedStats(cp, rep.get());
210 }
211 }
212
213 private synchronized void updatePublishedStats(ConnectPoint cp,
214 Set<FlowEntry> flowEntries) {
215 Set<FlowEntry> curr = current.get(cp);
216 if (curr == null) {
217 curr = new HashSet<>();
218 }
219 previous.put(cp, curr);
220 current.put(cp, flowEntries);
221
222 }
223
224 @Override
225 public Set<FlowEntry> getCurrentStatistic(ConnectPoint connectPoint) {
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700226 final DeviceId deviceId = connectPoint.deviceId();
Madan Jampanic156dd02015-08-12 15:57:46 -0700227 NodeId master = mastershipService.getMasterFor(deviceId);
228 if (master == null) {
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700229 log.warn("No master for {}", deviceId);
Thomas Vachuska82041f52014-11-30 22:14:02 -0800230 return Collections.emptySet();
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700231 }
Madan Jampanic156dd02015-08-12 15:57:46 -0700232 if (master.equals(clusterService.getLocalNode().id())) {
alshabib3d643ec2014-10-22 18:33:00 -0700233 return getCurrentStatisticInternal(connectPoint);
234 } else {
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700235 return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(
236 connectPoint,
237 GET_CURRENT,
238 SERIALIZER::encode,
239 SERIALIZER::decode,
Madan Jampanic156dd02015-08-12 15:57:46 -0700240 master),
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700241 STATISTIC_STORE_TIMEOUT_MILLIS,
242 TimeUnit.MILLISECONDS,
243 Collections.emptySet());
alshabib3d643ec2014-10-22 18:33:00 -0700244 }
245
246 }
247
248 private synchronized Set<FlowEntry> getCurrentStatisticInternal(ConnectPoint connectPoint) {
249 return current.get(connectPoint);
250 }
251
252 @Override
253 public Set<FlowEntry> getPreviousStatistic(ConnectPoint connectPoint) {
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700254 final DeviceId deviceId = connectPoint.deviceId();
Madan Jampanic156dd02015-08-12 15:57:46 -0700255 NodeId master = mastershipService.getMasterFor(deviceId);
256 if (master == null) {
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700257 log.warn("No master for {}", deviceId);
Thomas Vachuska82041f52014-11-30 22:14:02 -0800258 return Collections.emptySet();
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700259 }
Madan Jampanic156dd02015-08-12 15:57:46 -0700260 if (master.equals(clusterService.getLocalNode().id())) {
alshabib3d643ec2014-10-22 18:33:00 -0700261 return getPreviousStatisticInternal(connectPoint);
262 } else {
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700263 return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(
264 connectPoint,
265 GET_PREVIOUS,
266 SERIALIZER::encode,
267 SERIALIZER::decode,
Madan Jampanic156dd02015-08-12 15:57:46 -0700268 master),
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700269 STATISTIC_STORE_TIMEOUT_MILLIS,
270 TimeUnit.MILLISECONDS,
271 Collections.emptySet());
alshabib3d643ec2014-10-22 18:33:00 -0700272 }
alshabib3d643ec2014-10-22 18:33:00 -0700273 }
274
275 private synchronized Set<FlowEntry> getPreviousStatisticInternal(ConnectPoint connectPoint) {
276 return previous.get(connectPoint);
277 }
278
279 private InternalStatisticRepresentation getOrCreateRepresentation(ConnectPoint cp) {
280
281 if (representations.containsKey(cp)) {
282 return representations.get(cp);
283 } else {
284 InternalStatisticRepresentation rep = new InternalStatisticRepresentation();
285 representations.put(cp, rep);
286 return rep;
287 }
288
289 }
290
291 private ConnectPoint buildConnectPoint(FlowRule rule) {
292 PortNumber port = getOutput(rule);
Jonathan Hart7baba072015-02-23 14:27:59 -0800293
alshabib3d643ec2014-10-22 18:33:00 -0700294 if (port == null) {
alshabib3d643ec2014-10-22 18:33:00 -0700295 return null;
296 }
297 ConnectPoint cp = new ConnectPoint(rule.deviceId(), port);
298 return cp;
299 }
300
301 private PortNumber getOutput(FlowRule rule) {
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700302 for (Instruction i : rule.treatment().allInstructions()) {
alshabib3d643ec2014-10-22 18:33:00 -0700303 if (i.type() == Instruction.Type.OUTPUT) {
304 Instructions.OutputInstruction out = (Instructions.OutputInstruction) i;
305 return out.port();
306 }
alshabib3d643ec2014-10-22 18:33:00 -0700307 }
308 return null;
309 }
310
311 private class InternalStatisticRepresentation {
312
313 private final AtomicInteger counter = new AtomicInteger(0);
314 private final Set<FlowEntry> rules = new HashSet<>();
315
316 public void prepare() {
317 counter.incrementAndGet();
318 }
319
alshabib9c57bdd2014-11-28 19:14:06 -0500320 public synchronized boolean remove(FlowRule rule) {
alshabib3d643ec2014-10-22 18:33:00 -0700321 rules.remove(rule);
alshabib9c57bdd2014-11-28 19:14:06 -0500322 return counter.decrementAndGet() == 0;
alshabib3d643ec2014-10-22 18:33:00 -0700323 }
324
325 public synchronized boolean submit(FlowEntry rule) {
326 if (rules.contains(rule)) {
327 rules.remove(rule);
328 }
329 rules.add(rule);
330 if (counter.get() == 0) {
331 return true;
332 } else {
333 return counter.decrementAndGet() == 0;
334 }
335 }
336
337 public synchronized Set<FlowEntry> get() {
338 counter.set(rules.size());
alshabibf6c2ede2014-10-22 23:31:50 -0700339 return Sets.newHashSet(rules);
alshabib3d643ec2014-10-22 18:33:00 -0700340 }
341
342
343 }
344
sangyun-hanad84e0c2016-02-19 18:30:03 +0900345 /**
346 * Sets thread pool size of message handler.
347 *
348 * @param poolSize
349 */
350 private void setMessageHandlerThreadPoolSize(int poolSize) {
351 checkArgument(poolSize >= 0, "Message handler pool size must be 0 or more");
352 messageHandlerThreadPoolSize = poolSize;
353 }
354
355 /**
356 * Restarts thread pool of message handler.
357 */
358 private void restartMessageHandlerThreadPool() {
359 ExecutorService prevExecutor = messageHandlingExecutor;
360 messageHandlingExecutor = Executors.newFixedThreadPool(getMessageHandlerThreadPoolSize());
361 prevExecutor.shutdown();
362 }
363
364 /**
365 * Gets current thread pool size of message handler.
366 *
367 * @return messageHandlerThreadPoolSize
368 */
369 private int getMessageHandlerThreadPoolSize() {
370 return messageHandlerThreadPoolSize;
371 }
372
alshabib3d643ec2014-10-22 18:33:00 -0700373}