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