Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.statistic.impl; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 17 | |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 19 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.cluster.ClusterService; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.PortNumber; |
| 30 | import org.onosproject.net.flow.FlowEntry; |
| 31 | import org.onosproject.net.flow.FlowRule; |
| 32 | import org.onosproject.net.flow.instructions.Instruction; |
| 33 | import org.onosproject.net.flow.instructions.Instructions; |
| 34 | import org.onosproject.net.statistic.StatisticStore; |
| 35 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 36 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 37 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 38 | import org.onosproject.store.flow.ReplicaInfo; |
| 39 | import org.onosproject.store.flow.ReplicaInfoService; |
| 40 | import org.onosproject.store.serializers.KryoNamespaces; |
| 41 | import org.onosproject.store.serializers.KryoSerializer; |
Yuta HIGUCHI | 4cf23ce | 2014-10-22 20:37:13 -0700 | [diff] [blame] | 42 | import org.onlab.util.KryoNamespace; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 43 | import org.slf4j.Logger; |
| 44 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 45 | import java.io.IOException; |
alshabib | 9c57bdd | 2014-11-28 19:14:06 -0500 | [diff] [blame] | 46 | import java.util.Collections; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 47 | import java.util.HashSet; |
| 48 | import java.util.Map; |
| 49 | import java.util.Set; |
| 50 | import java.util.concurrent.ConcurrentHashMap; |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 51 | import java.util.concurrent.ExecutionException; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 52 | import java.util.concurrent.ExecutorService; |
| 53 | import java.util.concurrent.Executors; |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 54 | import java.util.concurrent.Future; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 55 | import java.util.concurrent.TimeUnit; |
| 56 | import java.util.concurrent.TimeoutException; |
| 57 | import java.util.concurrent.atomic.AtomicInteger; |
| 58 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 59 | import static org.onlab.util.Tools.groupedThreads; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 60 | import static org.onosproject.store.statistic.impl.StatisticStoreMessageSubjects.GET_CURRENT; |
| 61 | import static org.onosproject.store.statistic.impl.StatisticStoreMessageSubjects.GET_PREVIOUS; |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 62 | import static org.slf4j.LoggerFactory.getLogger; |
| 63 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Maintains statistics using RPC calls to collect stats from remote instances |
| 67 | * on demand. |
| 68 | */ |
| 69 | @Component(immediate = true) |
| 70 | @Service |
| 71 | public class DistributedStatisticStore implements StatisticStore { |
| 72 | |
| 73 | private final Logger log = getLogger(getClass()); |
| 74 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 75 | // TODO: Make configurable. |
| 76 | private static final int MESSAGE_HANDLER_THREAD_POOL_SIZE = 4; |
| 77 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 79 | protected ReplicaInfoService replicaInfoManager; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 82 | protected ClusterCommunicationService clusterCommunicator; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 85 | protected ClusterService clusterService; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 86 | |
| 87 | private Map<ConnectPoint, InternalStatisticRepresentation> representations = |
| 88 | new ConcurrentHashMap<>(); |
| 89 | |
| 90 | private Map<ConnectPoint, Set<FlowEntry>> previous = |
| 91 | new ConcurrentHashMap<>(); |
| 92 | |
| 93 | private Map<ConnectPoint, Set<FlowEntry>> current = |
| 94 | new ConcurrentHashMap<>(); |
| 95 | |
| 96 | protected static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 97 | @Override |
| 98 | protected void setupKryoPool() { |
Yuta HIGUCHI | 4cf23ce | 2014-10-22 20:37:13 -0700 | [diff] [blame] | 99 | serializerPool = KryoNamespace.newBuilder() |
| 100 | .register(KryoNamespaces.API) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 101 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
Yuta HIGUCHI | 4cf23ce | 2014-10-22 20:37:13 -0700 | [diff] [blame] | 102 | // register this store specific classes here |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 103 | .build(); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 104 | } |
| 105 | };; |
| 106 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 107 | private ExecutorService messageHandlingExecutor; |
| 108 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 109 | private static final long STATISTIC_STORE_TIMEOUT_MILLIS = 3000; |
| 110 | |
| 111 | @Activate |
| 112 | public void activate() { |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 113 | |
| 114 | messageHandlingExecutor = Executors.newFixedThreadPool( |
| 115 | MESSAGE_HANDLER_THREAD_POOL_SIZE, |
| 116 | groupedThreads("onos/store/statistic", "message-handlers")); |
| 117 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 118 | clusterCommunicator.addSubscriber(GET_CURRENT, new ClusterMessageHandler() { |
| 119 | |
| 120 | @Override |
| 121 | public void handle(ClusterMessage message) { |
| 122 | ConnectPoint cp = SERIALIZER.decode(message.payload()); |
| 123 | try { |
| 124 | message.respond(SERIALIZER.encode(getCurrentStatisticInternal(cp))); |
| 125 | } catch (IOException e) { |
| 126 | log.error("Failed to respond back", e); |
| 127 | } |
| 128 | } |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 129 | }, messageHandlingExecutor); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 130 | |
| 131 | clusterCommunicator.addSubscriber(GET_PREVIOUS, new ClusterMessageHandler() { |
| 132 | |
| 133 | @Override |
| 134 | public void handle(ClusterMessage message) { |
| 135 | ConnectPoint cp = SERIALIZER.decode(message.payload()); |
| 136 | try { |
| 137 | message.respond(SERIALIZER.encode(getPreviousStatisticInternal(cp))); |
| 138 | } catch (IOException e) { |
| 139 | log.error("Failed to respond back", e); |
| 140 | } |
| 141 | } |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 142 | }, messageHandlingExecutor); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 143 | log.info("Started"); |
| 144 | } |
| 145 | |
| 146 | @Deactivate |
| 147 | public void deactivate() { |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 148 | clusterCommunicator.removeSubscriber(GET_PREVIOUS); |
| 149 | clusterCommunicator.removeSubscriber(GET_CURRENT); |
| 150 | messageHandlingExecutor.shutdown(); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 151 | log.info("Stopped"); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void prepareForStatistics(FlowRule rule) { |
| 156 | ConnectPoint cp = buildConnectPoint(rule); |
| 157 | if (cp == null) { |
| 158 | return; |
| 159 | } |
| 160 | InternalStatisticRepresentation rep; |
| 161 | synchronized (representations) { |
| 162 | rep = getOrCreateRepresentation(cp); |
| 163 | } |
| 164 | rep.prepare(); |
| 165 | } |
| 166 | |
| 167 | @Override |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 168 | public synchronized void removeFromStatistics(FlowRule rule) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 169 | ConnectPoint cp = buildConnectPoint(rule); |
| 170 | if (cp == null) { |
| 171 | return; |
| 172 | } |
| 173 | InternalStatisticRepresentation rep = representations.get(cp); |
alshabib | 9c57bdd | 2014-11-28 19:14:06 -0500 | [diff] [blame] | 174 | if (rep != null && rep.remove(rule)) { |
| 175 | updatePublishedStats(cp, Collections.emptySet()); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 176 | } |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 177 | Set<FlowEntry> values = current.get(cp); |
| 178 | if (values != null) { |
| 179 | values.remove(rule); |
| 180 | } |
| 181 | values = previous.get(cp); |
| 182 | if (values != null) { |
| 183 | values.remove(rule); |
| 184 | } |
| 185 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void addOrUpdateStatistic(FlowEntry rule) { |
| 190 | ConnectPoint cp = buildConnectPoint(rule); |
| 191 | if (cp == null) { |
| 192 | return; |
| 193 | } |
| 194 | InternalStatisticRepresentation rep = representations.get(cp); |
| 195 | if (rep != null && rep.submit(rule)) { |
| 196 | updatePublishedStats(cp, rep.get()); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | private synchronized void updatePublishedStats(ConnectPoint cp, |
| 201 | Set<FlowEntry> flowEntries) { |
| 202 | Set<FlowEntry> curr = current.get(cp); |
| 203 | if (curr == null) { |
| 204 | curr = new HashSet<>(); |
| 205 | } |
| 206 | previous.put(cp, curr); |
| 207 | current.put(cp, flowEntries); |
| 208 | |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public Set<FlowEntry> getCurrentStatistic(ConnectPoint connectPoint) { |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 213 | final DeviceId deviceId = connectPoint.deviceId(); |
| 214 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 215 | if (!replicaInfo.master().isPresent()) { |
| 216 | log.warn("No master for {}", deviceId); |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 217 | return Collections.emptySet(); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 218 | } |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 219 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
| 220 | return getCurrentStatisticInternal(connectPoint); |
| 221 | } else { |
| 222 | ClusterMessage message = new ClusterMessage( |
| 223 | clusterService.getLocalNode().id(), |
| 224 | GET_CURRENT, |
| 225 | SERIALIZER.encode(connectPoint)); |
| 226 | |
| 227 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 228 | Future<byte[]> response = |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 229 | clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 230 | return SERIALIZER.decode(response.get(STATISTIC_STORE_TIMEOUT_MILLIS, |
| 231 | TimeUnit.MILLISECONDS)); |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 232 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 233 | log.warn("Unable to communicate with peer {}", replicaInfo.master().get()); |
| 234 | return Collections.emptySet(); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | } |
| 239 | |
| 240 | private synchronized Set<FlowEntry> getCurrentStatisticInternal(ConnectPoint connectPoint) { |
| 241 | return current.get(connectPoint); |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public Set<FlowEntry> getPreviousStatistic(ConnectPoint connectPoint) { |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 246 | final DeviceId deviceId = connectPoint.deviceId(); |
| 247 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 248 | if (!replicaInfo.master().isPresent()) { |
| 249 | log.warn("No master for {}", deviceId); |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 250 | return Collections.emptySet(); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 251 | } |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 252 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
| 253 | return getPreviousStatisticInternal(connectPoint); |
| 254 | } else { |
| 255 | ClusterMessage message = new ClusterMessage( |
| 256 | clusterService.getLocalNode().id(), |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 257 | GET_PREVIOUS, |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 258 | SERIALIZER.encode(connectPoint)); |
| 259 | |
| 260 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 261 | Future<byte[]> response = |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 262 | clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 263 | return SERIALIZER.decode(response.get(STATISTIC_STORE_TIMEOUT_MILLIS, |
| 264 | TimeUnit.MILLISECONDS)); |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 265 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 266 | log.warn("Unable to communicate with peer {}", replicaInfo.master().get()); |
| 267 | return Collections.emptySet(); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | } |
| 272 | |
| 273 | private synchronized Set<FlowEntry> getPreviousStatisticInternal(ConnectPoint connectPoint) { |
| 274 | return previous.get(connectPoint); |
| 275 | } |
| 276 | |
| 277 | private InternalStatisticRepresentation getOrCreateRepresentation(ConnectPoint cp) { |
| 278 | |
| 279 | if (representations.containsKey(cp)) { |
| 280 | return representations.get(cp); |
| 281 | } else { |
| 282 | InternalStatisticRepresentation rep = new InternalStatisticRepresentation(); |
| 283 | representations.put(cp, rep); |
| 284 | return rep; |
| 285 | } |
| 286 | |
| 287 | } |
| 288 | |
| 289 | private ConnectPoint buildConnectPoint(FlowRule rule) { |
| 290 | PortNumber port = getOutput(rule); |
| 291 | if (port == null) { |
Brian O'Connor | faaedf4 | 2014-11-17 14:48:48 -0800 | [diff] [blame] | 292 | log.debug("Rule {} has no output.", rule); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 293 | return null; |
| 294 | } |
| 295 | ConnectPoint cp = new ConnectPoint(rule.deviceId(), port); |
| 296 | return cp; |
| 297 | } |
| 298 | |
| 299 | private PortNumber getOutput(FlowRule rule) { |
| 300 | for (Instruction i : rule.treatment().instructions()) { |
| 301 | if (i.type() == Instruction.Type.OUTPUT) { |
| 302 | Instructions.OutputInstruction out = (Instructions.OutputInstruction) i; |
| 303 | return out.port(); |
| 304 | } |
| 305 | if (i.type() == Instruction.Type.DROP) { |
| 306 | return PortNumber.P0; |
| 307 | } |
| 308 | } |
| 309 | return null; |
| 310 | } |
| 311 | |
| 312 | private class InternalStatisticRepresentation { |
| 313 | |
| 314 | private final AtomicInteger counter = new AtomicInteger(0); |
| 315 | private final Set<FlowEntry> rules = new HashSet<>(); |
| 316 | |
| 317 | public void prepare() { |
| 318 | counter.incrementAndGet(); |
| 319 | } |
| 320 | |
alshabib | 9c57bdd | 2014-11-28 19:14:06 -0500 | [diff] [blame] | 321 | public synchronized boolean remove(FlowRule rule) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 322 | rules.remove(rule); |
alshabib | 9c57bdd | 2014-11-28 19:14:06 -0500 | [diff] [blame] | 323 | return counter.decrementAndGet() == 0; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | public synchronized boolean submit(FlowEntry rule) { |
| 327 | if (rules.contains(rule)) { |
| 328 | rules.remove(rule); |
| 329 | } |
| 330 | rules.add(rule); |
| 331 | if (counter.get() == 0) { |
| 332 | return true; |
| 333 | } else { |
| 334 | return counter.decrementAndGet() == 0; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | public synchronized Set<FlowEntry> get() { |
| 339 | counter.set(rules.size()); |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 340 | return Sets.newHashSet(rules); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | |
| 344 | } |
| 345 | |
| 346 | } |