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