Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | |
| 17 | package org.onosproject.store.statistic.impl; |
| 18 | |
| 19 | import com.google.common.base.Objects; |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 20 | |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Modified; |
| 25 | import org.apache.felix.scr.annotations.Property; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 29 | import org.onlab.util.Tools; |
sangyun-han | 9f0af2d | 2016-08-04 13:04:59 +0900 | [diff] [blame] | 30 | import org.onosproject.cfg.ComponentConfigService; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 31 | import org.onosproject.cluster.ClusterService; |
| 32 | import org.onosproject.cluster.NodeId; |
| 33 | import org.onosproject.mastership.MastershipService; |
| 34 | import org.onosproject.net.ConnectPoint; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.PortNumber; |
| 37 | import org.onosproject.net.flow.FlowEntry; |
| 38 | import org.onosproject.net.flow.FlowRule; |
| 39 | import org.onosproject.net.flow.instructions.Instruction; |
| 40 | import org.onosproject.net.flow.instructions.Instructions; |
| 41 | import org.onosproject.net.statistic.FlowStatisticStore; |
| 42 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 43 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 44 | import org.onosproject.store.serializers.KryoNamespaces; |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 45 | import org.onosproject.store.serializers.StoreSerializer; |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 46 | import org.osgi.service.component.ComponentContext; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | |
| 49 | import java.util.Collections; |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 50 | import java.util.Dictionary; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 51 | import java.util.HashSet; |
| 52 | import java.util.Map; |
| 53 | import java.util.Optional; |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 54 | import java.util.Properties; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 55 | import java.util.Set; |
| 56 | import java.util.concurrent.ConcurrentHashMap; |
| 57 | import java.util.concurrent.ExecutorService; |
| 58 | import java.util.concurrent.Executors; |
| 59 | import java.util.concurrent.TimeUnit; |
| 60 | |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 61 | import static com.google.common.base.Preconditions.checkArgument; |
| 62 | import static com.google.common.base.Strings.isNullOrEmpty; |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 63 | import static java.util.concurrent.Executors.newFixedThreadPool; |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 64 | import static org.onlab.util.Tools.get; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 65 | import static org.onlab.util.Tools.groupedThreads; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 66 | import static org.slf4j.LoggerFactory.getLogger; |
| 67 | |
| 68 | /** |
| 69 | * Maintains flow statistics using RPC calls to collect stats from remote instances |
| 70 | * on demand. |
| 71 | */ |
| 72 | @Component(immediate = true) |
| 73 | @Service |
| 74 | public class DistributedFlowStatisticStore implements FlowStatisticStore { |
| 75 | private final Logger log = getLogger(getClass()); |
| 76 | |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 77 | private static final String FORMAT = "Setting: messageHandlerThreadPoolSize={}"; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected MastershipService mastershipService; |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected ClusterCommunicationService clusterCommunicator; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected ClusterService clusterService; |
| 87 | |
sangyun-han | 9f0af2d | 2016-08-04 13:04:59 +0900 | [diff] [blame] | 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected ComponentConfigService cfgService; |
| 90 | |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 91 | private Map<ConnectPoint, Set<FlowEntry>> previous = |
| 92 | new ConcurrentHashMap<>(); |
| 93 | |
| 94 | private Map<ConnectPoint, Set<FlowEntry>> current = |
| 95 | new ConcurrentHashMap<>(); |
| 96 | |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 97 | public static final MessageSubject GET_CURRENT = new MessageSubject("peer-return-current"); |
| 98 | public static final MessageSubject GET_PREVIOUS = new MessageSubject("peer-return-previous"); |
| 99 | |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 100 | protected static final StoreSerializer SERIALIZER = StoreSerializer.using(KryoNamespaces.API); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 101 | |
| 102 | private NodeId local; |
| 103 | private ExecutorService messageHandlingExecutor; |
| 104 | |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 105 | private static final int DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE = 4; |
| 106 | @Property(name = "messageHandlerThreadPoolSize", intValue = DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE, |
| 107 | label = "Size of thread pool to assign message handler") |
| 108 | private static int messageHandlerThreadPoolSize = DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE; |
| 109 | |
| 110 | |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 111 | private static final long STATISTIC_STORE_TIMEOUT_MILLIS = 3000; |
| 112 | |
| 113 | @Activate |
sangyun-han | 9f0af2d | 2016-08-04 13:04:59 +0900 | [diff] [blame] | 114 | public void activate(ComponentContext context) { |
| 115 | cfgService.registerProperties(getClass()); |
| 116 | |
| 117 | modified(context); |
| 118 | |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 119 | local = clusterService.getLocalNode().id(); |
| 120 | |
| 121 | messageHandlingExecutor = Executors.newFixedThreadPool( |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 122 | messageHandlerThreadPoolSize, |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 123 | groupedThreads("onos/store/statistic", "message-handlers", log)); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 124 | |
| 125 | clusterCommunicator.addSubscriber( |
| 126 | GET_CURRENT, SERIALIZER::decode, this::getCurrentStatisticInternal, SERIALIZER::encode, |
| 127 | messageHandlingExecutor); |
| 128 | |
| 129 | clusterCommunicator.addSubscriber( |
| 130 | GET_CURRENT, SERIALIZER::decode, this::getPreviousStatisticInternal, SERIALIZER::encode, |
| 131 | messageHandlingExecutor); |
| 132 | |
| 133 | log.info("Started"); |
| 134 | } |
| 135 | |
| 136 | @Deactivate |
| 137 | public void deactivate() { |
sangyun-han | 9f0af2d | 2016-08-04 13:04:59 +0900 | [diff] [blame] | 138 | cfgService.unregisterProperties(getClass(), false); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 139 | clusterCommunicator.removeSubscriber(GET_PREVIOUS); |
| 140 | clusterCommunicator.removeSubscriber(GET_CURRENT); |
| 141 | messageHandlingExecutor.shutdown(); |
| 142 | log.info("Stopped"); |
| 143 | } |
| 144 | |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 145 | @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 | |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 171 | @Override |
| 172 | public synchronized void removeFlowStatistic(FlowRule rule) { |
| 173 | ConnectPoint cp = buildConnectPoint(rule); |
| 174 | if (cp == null) { |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | // remove this rule if present from current map |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 179 | current.computeIfPresent(cp, (c, e) -> { |
| 180 | e.remove(rule); |
| 181 | return e; |
| 182 | }); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 183 | |
| 184 | // remove this on if present from previous map |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 185 | previous.computeIfPresent(cp, (c, e) -> { |
| 186 | e.remove(rule); |
| 187 | return e; |
| 188 | }); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public synchronized void addFlowStatistic(FlowEntry rule) { |
| 193 | ConnectPoint cp = buildConnectPoint(rule); |
| 194 | if (cp == null) { |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | // create one if absent and add this rule |
| 199 | current.putIfAbsent(cp, new HashSet<>()); |
| 200 | current.computeIfPresent(cp, (c, e) -> { e.add(rule); return e; }); |
| 201 | |
| 202 | // remove previous one if present |
| 203 | previous.computeIfPresent(cp, (c, e) -> { e.remove(rule); return e; }); |
| 204 | } |
| 205 | |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 206 | @Override |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 207 | public synchronized void updateFlowStatistic(FlowEntry rule) { |
| 208 | ConnectPoint cp = buildConnectPoint(rule); |
| 209 | if (cp == null) { |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | Set<FlowEntry> curr = current.get(cp); |
| 214 | if (curr == null) { |
| 215 | addFlowStatistic(rule); |
| 216 | } else { |
| 217 | Optional<FlowEntry> f = curr.stream().filter(c -> rule.equals(c)). |
| 218 | findAny(); |
| 219 | if (f.isPresent() && rule.bytes() < f.get().bytes()) { |
| 220 | log.debug("DistributedFlowStatisticStore:updateFlowStatistic():" + |
| 221 | " Invalid Flow Update! Will be removed!!" + |
| 222 | " curr flowId=" + Long.toHexString(rule.id().value()) + |
| 223 | ", prev flowId=" + Long.toHexString(f.get().id().value()) + |
| 224 | ", curr bytes=" + rule.bytes() + ", prev bytes=" + f.get().bytes() + |
| 225 | ", curr life=" + rule.life() + ", prev life=" + f.get().life() + |
| 226 | ", curr lastSeen=" + rule.lastSeen() + ", prev lastSeen=" + f.get().lastSeen()); |
| 227 | // something is wrong! invalid flow entry, so delete it |
| 228 | removeFlowStatistic(rule); |
| 229 | return; |
| 230 | } |
| 231 | Set<FlowEntry> prev = previous.get(cp); |
| 232 | if (prev == null) { |
| 233 | prev = new HashSet<>(); |
| 234 | previous.put(cp, prev); |
| 235 | } |
| 236 | |
| 237 | // previous one is exist |
| 238 | if (f.isPresent()) { |
| 239 | // remove old one and add new one |
| 240 | prev.remove(rule); |
| 241 | if (!prev.add(f.get())) { |
| 242 | log.debug("DistributedFlowStatisticStore:updateFlowStatistic():" + |
| 243 | " flowId={}, add failed into previous.", |
| 244 | Long.toHexString(rule.id().value())); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // remove old one and add new one |
| 249 | curr.remove(rule); |
| 250 | if (!curr.add(rule)) { |
| 251 | log.debug("DistributedFlowStatisticStore:updateFlowStatistic():" + |
| 252 | " flowId={}, add failed into current.", |
| 253 | Long.toHexString(rule.id().value())); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public Set<FlowEntry> getCurrentFlowStatistic(ConnectPoint connectPoint) { |
| 260 | final DeviceId deviceId = connectPoint.deviceId(); |
| 261 | |
| 262 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 263 | if (master == null) { |
| 264 | log.warn("No master for {}", deviceId); |
| 265 | return Collections.emptySet(); |
| 266 | } |
| 267 | |
| 268 | if (Objects.equal(local, master)) { |
| 269 | return getCurrentStatisticInternal(connectPoint); |
| 270 | } else { |
| 271 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive( |
| 272 | connectPoint, |
| 273 | GET_CURRENT, |
| 274 | SERIALIZER::encode, |
| 275 | SERIALIZER::decode, |
| 276 | master), |
| 277 | STATISTIC_STORE_TIMEOUT_MILLIS, |
| 278 | TimeUnit.MILLISECONDS, |
| 279 | Collections.emptySet()); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | private synchronized Set<FlowEntry> getCurrentStatisticInternal(ConnectPoint connectPoint) { |
| 284 | return current.get(connectPoint); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public Set<FlowEntry> getPreviousFlowStatistic(ConnectPoint connectPoint) { |
| 289 | final DeviceId deviceId = connectPoint.deviceId(); |
| 290 | |
| 291 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 292 | if (master == null) { |
| 293 | log.warn("No master for {}", deviceId); |
| 294 | return Collections.emptySet(); |
| 295 | } |
| 296 | |
| 297 | if (Objects.equal(local, master)) { |
| 298 | return getPreviousStatisticInternal(connectPoint); |
| 299 | } else { |
| 300 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive( |
| 301 | connectPoint, |
| 302 | GET_PREVIOUS, |
| 303 | SERIALIZER::encode, |
| 304 | SERIALIZER::decode, |
| 305 | master), |
| 306 | STATISTIC_STORE_TIMEOUT_MILLIS, |
| 307 | TimeUnit.MILLISECONDS, |
| 308 | Collections.emptySet()); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | private synchronized Set<FlowEntry> getPreviousStatisticInternal(ConnectPoint connectPoint) { |
| 313 | return previous.get(connectPoint); |
| 314 | } |
| 315 | |
| 316 | private ConnectPoint buildConnectPoint(FlowRule rule) { |
| 317 | PortNumber port = getOutput(rule); |
| 318 | |
| 319 | if (port == null) { |
| 320 | return null; |
| 321 | } |
| 322 | ConnectPoint cp = new ConnectPoint(rule.deviceId(), port); |
| 323 | return cp; |
| 324 | } |
| 325 | |
| 326 | private PortNumber getOutput(FlowRule rule) { |
| 327 | for (Instruction i : rule.treatment().allInstructions()) { |
| 328 | if (i.type() == Instruction.Type.OUTPUT) { |
| 329 | Instructions.OutputInstruction out = (Instructions.OutputInstruction) i; |
| 330 | return out.port(); |
| 331 | } |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 332 | } |
| 333 | return null; |
| 334 | } |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 335 | |
| 336 | /** |
| 337 | * Sets thread pool size of message handler. |
| 338 | * |
| 339 | * @param poolSize |
| 340 | */ |
| 341 | private void setMessageHandlerThreadPoolSize(int poolSize) { |
| 342 | checkArgument(poolSize >= 0, "Message handler pool size must be 0 or more"); |
| 343 | messageHandlerThreadPoolSize = poolSize; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Restarts thread pool of message handler. |
| 348 | */ |
| 349 | private void restartMessageHandlerThreadPool() { |
| 350 | ExecutorService prevExecutor = messageHandlingExecutor; |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 351 | messageHandlingExecutor = newFixedThreadPool(getMessageHandlerThreadPoolSize(), |
| 352 | groupedThreads("DistFlowStats", "messageHandling-%d", log)); |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 353 | prevExecutor.shutdown(); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Gets current thread pool size of message handler. |
| 358 | * |
| 359 | * @return messageHandlerThreadPoolSize |
| 360 | */ |
| 361 | private int getMessageHandlerThreadPoolSize() { |
| 362 | return messageHandlerThreadPoolSize; |
| 363 | } |
Sho SHIMIZU | 57f2efd | 2016-02-24 12:20:05 -0800 | [diff] [blame] | 364 | } |