blob: edbd68d501cb7033e71d255a4c8613e444dd5860 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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;
alshabib346b5b32015-03-06 00:42:16 -080028import org.onlab.util.KryoNamespace;
Madan Jampani2bfa94c2015-04-11 05:03:49 -070029import org.onlab.util.Tools;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.cluster.ClusterService;
Madan Jampanic156dd02015-08-12 15:57:46 -070031import org.onosproject.cluster.NodeId;
32import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.ConnectPoint;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.PortNumber;
36import org.onosproject.net.flow.FlowEntry;
37import org.onosproject.net.flow.FlowRule;
38import org.onosproject.net.flow.instructions.Instruction;
39import org.onosproject.net.flow.instructions.Instructions;
40import org.onosproject.net.statistic.StatisticStore;
41import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.store.serializers.KryoNamespaces;
43import org.onosproject.store.serializers.KryoSerializer;
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080063import static org.onosproject.store.statistic.impl.StatisticStoreMessageSubjects.GET_CURRENT;
64import static org.onosproject.store.statistic.impl.StatisticStoreMessageSubjects.GET_PREVIOUS;
Thomas Vachuska82041f52014-11-30 22:14:02 -080065import static org.slf4j.LoggerFactory.getLogger;
66
alshabib3d643ec2014-10-22 18:33:00 -070067
68/**
69 * Maintains statistics using RPC calls to collect stats from remote instances
70 * on demand.
71 */
72@Component(immediate = true)
73@Service
74public class DistributedStatisticStore implements StatisticStore {
75
76 private final Logger log = getLogger(getClass());
77
sangyun-hanad84e0c2016-02-19 18:30:03 +090078 private static final String FORMAT = "Setting: messageHandlerThreadPoolSize={}";
Madan Jampani2af244a2015-02-22 13:12:01 -080079
alshabib3d643ec2014-10-22 18:33:00 -070080 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Madan Jampanic156dd02015-08-12 15:57:46 -070081 protected MastershipService mastershipService;
alshabib3d643ec2014-10-22 18:33:00 -070082
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuska82041f52014-11-30 22:14:02 -080084 protected ClusterCommunicationService clusterCommunicator;
alshabib3d643ec2014-10-22 18:33:00 -070085
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuska82041f52014-11-30 22:14:02 -080087 protected ClusterService clusterService;
alshabib3d643ec2014-10-22 18:33:00 -070088
89 private Map<ConnectPoint, InternalStatisticRepresentation> representations =
90 new ConcurrentHashMap<>();
91
92 private Map<ConnectPoint, Set<FlowEntry>> previous =
93 new ConcurrentHashMap<>();
94
95 private Map<ConnectPoint, Set<FlowEntry>> current =
96 new ConcurrentHashMap<>();
97
98 protected static final KryoSerializer SERIALIZER = new KryoSerializer() {
99 @Override
100 protected void setupKryoPool() {
Yuta HIGUCHI4cf23ce2014-10-22 20:37:13 -0700101 serializerPool = KryoNamespace.newBuilder()
102 .register(KryoNamespaces.API)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800103 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
Yuta HIGUCHI4cf23ce2014-10-22 20:37:13 -0700104 // register this store specific classes here
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800105 .build();
alshabib3d643ec2014-10-22 18:33:00 -0700106 }
Sho SHIMIZU9f614a42015-09-11 15:32:46 -0700107 };
alshabib3d643ec2014-10-22 18:33:00 -0700108
Madan Jampani2af244a2015-02-22 13:12:01 -0800109 private ExecutorService messageHandlingExecutor;
110
sangyun-hanad84e0c2016-02-19 18:30:03 +0900111 private static final int DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE = 4;
112 @Property(name = "messageHandlerThreadPoolSize", intValue = DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE,
113 label = "Size of thread pool to assign message handler")
114 private static int messageHandlerThreadPoolSize = DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE;
115
alshabib3d643ec2014-10-22 18:33:00 -0700116 private static final long STATISTIC_STORE_TIMEOUT_MILLIS = 3000;
117
118 @Activate
119 public void activate() {
Madan Jampani2af244a2015-02-22 13:12:01 -0800120
121 messageHandlingExecutor = Executors.newFixedThreadPool(
sangyun-hanad84e0c2016-02-19 18:30:03 +0900122 messageHandlerThreadPoolSize,
Madan Jampani2af244a2015-02-22 13:12:01 -0800123 groupedThreads("onos/store/statistic", "message-handlers"));
124
Madan Jampani1151b552015-08-12 16:11:27 -0700125 clusterCommunicator.<ConnectPoint, Set<FlowEntry>>addSubscriber(GET_CURRENT,
126 SERIALIZER::decode,
127 this::getCurrentStatisticInternal,
128 SERIALIZER::encode,
129 messageHandlingExecutor);
alshabib3d643ec2014-10-22 18:33:00 -0700130
Madan Jampani1151b552015-08-12 16:11:27 -0700131 clusterCommunicator.<ConnectPoint, Set<FlowEntry>>addSubscriber(GET_PREVIOUS,
132 SERIALIZER::decode,
133 this::getPreviousStatisticInternal,
134 SERIALIZER::encode,
135 messageHandlingExecutor);
alshabib3d643ec2014-10-22 18:33:00 -0700136
alshabib3d643ec2014-10-22 18:33:00 -0700137 log.info("Started");
138 }
139
140 @Deactivate
141 public void deactivate() {
Madan Jampani2af244a2015-02-22 13:12:01 -0800142 clusterCommunicator.removeSubscriber(GET_PREVIOUS);
143 clusterCommunicator.removeSubscriber(GET_CURRENT);
144 messageHandlingExecutor.shutdown();
alshabib3d643ec2014-10-22 18:33:00 -0700145 log.info("Stopped");
146 }
147
sangyun-hanad84e0c2016-02-19 18:30:03 +0900148 @Modified
149 public void modified(ComponentContext context) {
150 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
151
152 int newMessageHandlerThreadPoolSize;
153
154 try {
155 String s = get(properties, "messageHandlerThreadPoolSize");
156
157 newMessageHandlerThreadPoolSize =
158 isNullOrEmpty(s) ? messageHandlerThreadPoolSize : Integer.parseInt(s.trim());
159
160 } catch (NumberFormatException e) {
161 log.warn(e.getMessage());
162 newMessageHandlerThreadPoolSize = messageHandlerThreadPoolSize;
163 }
164
165 // Any change in the following parameters implies thread pool restart
166 if (newMessageHandlerThreadPoolSize != messageHandlerThreadPoolSize) {
167 setMessageHandlerThreadPoolSize(newMessageHandlerThreadPoolSize);
168 restartMessageHandlerThreadPool();
169 }
170
171 log.info(FORMAT, messageHandlerThreadPoolSize);
172 }
173
174
alshabib3d643ec2014-10-22 18:33:00 -0700175 @Override
176 public void prepareForStatistics(FlowRule rule) {
177 ConnectPoint cp = buildConnectPoint(rule);
178 if (cp == null) {
179 return;
180 }
181 InternalStatisticRepresentation rep;
182 synchronized (representations) {
183 rep = getOrCreateRepresentation(cp);
184 }
185 rep.prepare();
186 }
187
188 @Override
alshabibf6c2ede2014-10-22 23:31:50 -0700189 public synchronized void removeFromStatistics(FlowRule rule) {
alshabib3d643ec2014-10-22 18:33:00 -0700190 ConnectPoint cp = buildConnectPoint(rule);
191 if (cp == null) {
192 return;
193 }
194 InternalStatisticRepresentation rep = representations.get(cp);
alshabib9c57bdd2014-11-28 19:14:06 -0500195 if (rep != null && rep.remove(rule)) {
196 updatePublishedStats(cp, Collections.emptySet());
alshabib3d643ec2014-10-22 18:33:00 -0700197 }
alshabibf6c2ede2014-10-22 23:31:50 -0700198 Set<FlowEntry> values = current.get(cp);
199 if (values != null) {
200 values.remove(rule);
201 }
202 values = previous.get(cp);
203 if (values != null) {
204 values.remove(rule);
205 }
206
alshabib3d643ec2014-10-22 18:33:00 -0700207 }
208
209 @Override
210 public void addOrUpdateStatistic(FlowEntry rule) {
211 ConnectPoint cp = buildConnectPoint(rule);
212 if (cp == null) {
213 return;
214 }
215 InternalStatisticRepresentation rep = representations.get(cp);
216 if (rep != null && rep.submit(rule)) {
217 updatePublishedStats(cp, rep.get());
218 }
219 }
220
221 private synchronized void updatePublishedStats(ConnectPoint cp,
222 Set<FlowEntry> flowEntries) {
223 Set<FlowEntry> curr = current.get(cp);
224 if (curr == null) {
225 curr = new HashSet<>();
226 }
227 previous.put(cp, curr);
228 current.put(cp, flowEntries);
229
230 }
231
232 @Override
233 public Set<FlowEntry> getCurrentStatistic(ConnectPoint connectPoint) {
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700234 final DeviceId deviceId = connectPoint.deviceId();
Madan Jampanic156dd02015-08-12 15:57:46 -0700235 NodeId master = mastershipService.getMasterFor(deviceId);
236 if (master == null) {
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700237 log.warn("No master for {}", deviceId);
Thomas Vachuska82041f52014-11-30 22:14:02 -0800238 return Collections.emptySet();
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700239 }
Madan Jampanic156dd02015-08-12 15:57:46 -0700240 if (master.equals(clusterService.getLocalNode().id())) {
alshabib3d643ec2014-10-22 18:33:00 -0700241 return getCurrentStatisticInternal(connectPoint);
242 } else {
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700243 return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(
244 connectPoint,
245 GET_CURRENT,
246 SERIALIZER::encode,
247 SERIALIZER::decode,
Madan Jampanic156dd02015-08-12 15:57:46 -0700248 master),
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700249 STATISTIC_STORE_TIMEOUT_MILLIS,
250 TimeUnit.MILLISECONDS,
251 Collections.emptySet());
alshabib3d643ec2014-10-22 18:33:00 -0700252 }
253
254 }
255
256 private synchronized Set<FlowEntry> getCurrentStatisticInternal(ConnectPoint connectPoint) {
257 return current.get(connectPoint);
258 }
259
260 @Override
261 public Set<FlowEntry> getPreviousStatistic(ConnectPoint connectPoint) {
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700262 final DeviceId deviceId = connectPoint.deviceId();
Madan Jampanic156dd02015-08-12 15:57:46 -0700263 NodeId master = mastershipService.getMasterFor(deviceId);
264 if (master == null) {
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700265 log.warn("No master for {}", deviceId);
Thomas Vachuska82041f52014-11-30 22:14:02 -0800266 return Collections.emptySet();
Yuta HIGUCHI4b524442014-10-28 22:23:57 -0700267 }
Madan Jampanic156dd02015-08-12 15:57:46 -0700268 if (master.equals(clusterService.getLocalNode().id())) {
alshabib3d643ec2014-10-22 18:33:00 -0700269 return getPreviousStatisticInternal(connectPoint);
270 } else {
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700271 return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(
272 connectPoint,
273 GET_PREVIOUS,
274 SERIALIZER::encode,
275 SERIALIZER::decode,
Madan Jampanic156dd02015-08-12 15:57:46 -0700276 master),
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700277 STATISTIC_STORE_TIMEOUT_MILLIS,
278 TimeUnit.MILLISECONDS,
279 Collections.emptySet());
alshabib3d643ec2014-10-22 18:33:00 -0700280 }
alshabib3d643ec2014-10-22 18:33:00 -0700281 }
282
283 private synchronized Set<FlowEntry> getPreviousStatisticInternal(ConnectPoint connectPoint) {
284 return previous.get(connectPoint);
285 }
286
287 private InternalStatisticRepresentation getOrCreateRepresentation(ConnectPoint cp) {
288
289 if (representations.containsKey(cp)) {
290 return representations.get(cp);
291 } else {
292 InternalStatisticRepresentation rep = new InternalStatisticRepresentation();
293 representations.put(cp, rep);
294 return rep;
295 }
296
297 }
298
299 private ConnectPoint buildConnectPoint(FlowRule rule) {
300 PortNumber port = getOutput(rule);
Jonathan Hart7baba072015-02-23 14:27:59 -0800301
alshabib3d643ec2014-10-22 18:33:00 -0700302 if (port == null) {
alshabib3d643ec2014-10-22 18:33:00 -0700303 return null;
304 }
305 ConnectPoint cp = new ConnectPoint(rule.deviceId(), port);
306 return cp;
307 }
308
309 private PortNumber getOutput(FlowRule rule) {
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700310 for (Instruction i : rule.treatment().allInstructions()) {
alshabib3d643ec2014-10-22 18:33:00 -0700311 if (i.type() == Instruction.Type.OUTPUT) {
312 Instructions.OutputInstruction out = (Instructions.OutputInstruction) i;
313 return out.port();
314 }
alshabib3d643ec2014-10-22 18:33:00 -0700315 }
316 return null;
317 }
318
319 private class InternalStatisticRepresentation {
320
321 private final AtomicInteger counter = new AtomicInteger(0);
322 private final Set<FlowEntry> rules = new HashSet<>();
323
324 public void prepare() {
325 counter.incrementAndGet();
326 }
327
alshabib9c57bdd2014-11-28 19:14:06 -0500328 public synchronized boolean remove(FlowRule rule) {
alshabib3d643ec2014-10-22 18:33:00 -0700329 rules.remove(rule);
alshabib9c57bdd2014-11-28 19:14:06 -0500330 return counter.decrementAndGet() == 0;
alshabib3d643ec2014-10-22 18:33:00 -0700331 }
332
333 public synchronized boolean submit(FlowEntry rule) {
334 if (rules.contains(rule)) {
335 rules.remove(rule);
336 }
337 rules.add(rule);
338 if (counter.get() == 0) {
339 return true;
340 } else {
341 return counter.decrementAndGet() == 0;
342 }
343 }
344
345 public synchronized Set<FlowEntry> get() {
346 counter.set(rules.size());
alshabibf6c2ede2014-10-22 23:31:50 -0700347 return Sets.newHashSet(rules);
alshabib3d643ec2014-10-22 18:33:00 -0700348 }
349
350
351 }
352
sangyun-hanad84e0c2016-02-19 18:30:03 +0900353 /**
354 * Sets thread pool size of message handler.
355 *
356 * @param poolSize
357 */
358 private void setMessageHandlerThreadPoolSize(int poolSize) {
359 checkArgument(poolSize >= 0, "Message handler pool size must be 0 or more");
360 messageHandlerThreadPoolSize = poolSize;
361 }
362
363 /**
364 * Restarts thread pool of message handler.
365 */
366 private void restartMessageHandlerThreadPool() {
367 ExecutorService prevExecutor = messageHandlingExecutor;
368 messageHandlingExecutor = Executors.newFixedThreadPool(getMessageHandlerThreadPoolSize());
369 prevExecutor.shutdown();
370 }
371
372 /**
373 * Gets current thread pool size of message handler.
374 *
375 * @return messageHandlerThreadPoolSize
376 */
377 private int getMessageHandlerThreadPoolSize() {
378 return messageHandlerThreadPoolSize;
379 }
380
alshabib3d643ec2014-10-22 18:33:00 -0700381}