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