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 | */ |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.statistic.impl; |
| 17 | |
| 18 | import static org.onlab.onos.store.statistic.impl.StatisticStoreMessageSubjects.*; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 22 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.apache.felix.scr.annotations.Service; |
| 29 | import org.onlab.onos.cluster.ClusterService; |
| 30 | import org.onlab.onos.net.ConnectPoint; |
| 31 | import org.onlab.onos.net.PortNumber; |
| 32 | import org.onlab.onos.net.flow.FlowEntry; |
| 33 | import org.onlab.onos.net.flow.FlowRule; |
| 34 | import org.onlab.onos.net.flow.instructions.Instruction; |
| 35 | import org.onlab.onos.net.flow.instructions.Instructions; |
| 36 | import org.onlab.onos.net.statistic.StatisticStore; |
| 37 | import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService; |
| 38 | import org.onlab.onos.store.cluster.messaging.ClusterMessage; |
| 39 | import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 40 | import org.onlab.onos.store.flow.ReplicaInfo; |
| 41 | import org.onlab.onos.store.flow.ReplicaInfoService; |
| 42 | import org.onlab.onos.store.serializers.KryoNamespaces; |
| 43 | import org.onlab.onos.store.serializers.KryoSerializer; |
Yuta HIGUCHI | 4cf23ce | 2014-10-22 20:37:13 -0700 | [diff] [blame] | 44 | import org.onlab.util.KryoNamespace; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 45 | import org.slf4j.Logger; |
| 46 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 47 | import java.io.IOException; |
| 48 | import java.util.HashSet; |
| 49 | import java.util.Map; |
| 50 | import java.util.Set; |
| 51 | import java.util.concurrent.ConcurrentHashMap; |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 52 | import java.util.concurrent.ExecutionException; |
| 53 | import java.util.concurrent.Future; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 54 | import java.util.concurrent.TimeUnit; |
| 55 | import java.util.concurrent.TimeoutException; |
| 56 | import java.util.concurrent.atomic.AtomicInteger; |
| 57 | |
| 58 | |
| 59 | /** |
| 60 | * Maintains statistics using RPC calls to collect stats from remote instances |
| 61 | * on demand. |
| 62 | */ |
| 63 | @Component(immediate = true) |
| 64 | @Service |
| 65 | public class DistributedStatisticStore implements StatisticStore { |
| 66 | |
| 67 | private final Logger log = getLogger(getClass()); |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | private ReplicaInfoService replicaInfoManager; |
| 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | private ClusterCommunicationService clusterCommunicator; |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | private ClusterService clusterService; |
| 77 | |
| 78 | private Map<ConnectPoint, InternalStatisticRepresentation> representations = |
| 79 | new ConcurrentHashMap<>(); |
| 80 | |
| 81 | private Map<ConnectPoint, Set<FlowEntry>> previous = |
| 82 | new ConcurrentHashMap<>(); |
| 83 | |
| 84 | private Map<ConnectPoint, Set<FlowEntry>> current = |
| 85 | new ConcurrentHashMap<>(); |
| 86 | |
| 87 | protected static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 88 | @Override |
| 89 | protected void setupKryoPool() { |
Yuta HIGUCHI | 4cf23ce | 2014-10-22 20:37:13 -0700 | [diff] [blame] | 90 | serializerPool = KryoNamespace.newBuilder() |
| 91 | .register(KryoNamespaces.API) |
| 92 | // register this store specific classes here |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 93 | .build() |
| 94 | .populate(1); |
| 95 | } |
| 96 | };; |
| 97 | |
| 98 | private static final long STATISTIC_STORE_TIMEOUT_MILLIS = 3000; |
| 99 | |
| 100 | @Activate |
| 101 | public void activate() { |
| 102 | clusterCommunicator.addSubscriber(GET_CURRENT, new ClusterMessageHandler() { |
| 103 | |
| 104 | @Override |
| 105 | public void handle(ClusterMessage message) { |
| 106 | ConnectPoint cp = SERIALIZER.decode(message.payload()); |
| 107 | try { |
| 108 | message.respond(SERIALIZER.encode(getCurrentStatisticInternal(cp))); |
| 109 | } catch (IOException e) { |
| 110 | log.error("Failed to respond back", e); |
| 111 | } |
| 112 | } |
| 113 | }); |
| 114 | |
| 115 | clusterCommunicator.addSubscriber(GET_PREVIOUS, new ClusterMessageHandler() { |
| 116 | |
| 117 | @Override |
| 118 | public void handle(ClusterMessage message) { |
| 119 | ConnectPoint cp = SERIALIZER.decode(message.payload()); |
| 120 | try { |
| 121 | message.respond(SERIALIZER.encode(getPreviousStatisticInternal(cp))); |
| 122 | } catch (IOException e) { |
| 123 | log.error("Failed to respond back", e); |
| 124 | } |
| 125 | } |
| 126 | }); |
| 127 | log.info("Started"); |
| 128 | } |
| 129 | |
| 130 | @Deactivate |
| 131 | public void deactivate() { |
| 132 | log.info("Stopped"); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void prepareForStatistics(FlowRule rule) { |
| 137 | ConnectPoint cp = buildConnectPoint(rule); |
| 138 | if (cp == null) { |
| 139 | return; |
| 140 | } |
| 141 | InternalStatisticRepresentation rep; |
| 142 | synchronized (representations) { |
| 143 | rep = getOrCreateRepresentation(cp); |
| 144 | } |
| 145 | rep.prepare(); |
| 146 | } |
| 147 | |
| 148 | @Override |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 149 | public synchronized void removeFromStatistics(FlowRule rule) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 150 | ConnectPoint cp = buildConnectPoint(rule); |
| 151 | if (cp == null) { |
| 152 | return; |
| 153 | } |
| 154 | InternalStatisticRepresentation rep = representations.get(cp); |
| 155 | if (rep != null) { |
| 156 | rep.remove(rule); |
| 157 | } |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 158 | Set<FlowEntry> values = current.get(cp); |
| 159 | if (values != null) { |
| 160 | values.remove(rule); |
| 161 | } |
| 162 | values = previous.get(cp); |
| 163 | if (values != null) { |
| 164 | values.remove(rule); |
| 165 | } |
| 166 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void addOrUpdateStatistic(FlowEntry rule) { |
| 171 | ConnectPoint cp = buildConnectPoint(rule); |
| 172 | if (cp == null) { |
| 173 | return; |
| 174 | } |
| 175 | InternalStatisticRepresentation rep = representations.get(cp); |
| 176 | if (rep != null && rep.submit(rule)) { |
| 177 | updatePublishedStats(cp, rep.get()); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | private synchronized void updatePublishedStats(ConnectPoint cp, |
| 182 | Set<FlowEntry> flowEntries) { |
| 183 | Set<FlowEntry> curr = current.get(cp); |
| 184 | if (curr == null) { |
| 185 | curr = new HashSet<>(); |
| 186 | } |
| 187 | previous.put(cp, curr); |
| 188 | current.put(cp, flowEntries); |
| 189 | |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public Set<FlowEntry> getCurrentStatistic(ConnectPoint connectPoint) { |
| 194 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(connectPoint.deviceId()); |
| 195 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
| 196 | return getCurrentStatisticInternal(connectPoint); |
| 197 | } else { |
| 198 | ClusterMessage message = new ClusterMessage( |
| 199 | clusterService.getLocalNode().id(), |
| 200 | GET_CURRENT, |
| 201 | SERIALIZER.encode(connectPoint)); |
| 202 | |
| 203 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 204 | Future<byte[]> response = |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 205 | clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 206 | return SERIALIZER.decode(response.get(STATISTIC_STORE_TIMEOUT_MILLIS, |
| 207 | TimeUnit.MILLISECONDS)); |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 208 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 209 | // FIXME: throw a StatsStoreException |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 210 | throw new RuntimeException(e); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | } |
| 215 | |
| 216 | private synchronized Set<FlowEntry> getCurrentStatisticInternal(ConnectPoint connectPoint) { |
| 217 | return current.get(connectPoint); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public Set<FlowEntry> getPreviousStatistic(ConnectPoint connectPoint) { |
| 222 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(connectPoint.deviceId()); |
| 223 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
| 224 | return getPreviousStatisticInternal(connectPoint); |
| 225 | } else { |
| 226 | ClusterMessage message = new ClusterMessage( |
| 227 | clusterService.getLocalNode().id(), |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 228 | GET_PREVIOUS, |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 229 | SERIALIZER.encode(connectPoint)); |
| 230 | |
| 231 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 232 | Future<byte[]> response = |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 233 | clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 234 | return SERIALIZER.decode(response.get(STATISTIC_STORE_TIMEOUT_MILLIS, |
| 235 | TimeUnit.MILLISECONDS)); |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 236 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 237 | // FIXME: throw a StatsStoreException |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 238 | throw new RuntimeException(e); |
| 239 | } |
| 240 | } |
| 241 | |
| 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); |
| 262 | if (port == null) { |
| 263 | log.warn("Rule {} has no output.", rule); |
| 264 | return null; |
| 265 | } |
| 266 | ConnectPoint cp = new ConnectPoint(rule.deviceId(), port); |
| 267 | return cp; |
| 268 | } |
| 269 | |
| 270 | private PortNumber getOutput(FlowRule rule) { |
| 271 | for (Instruction i : rule.treatment().instructions()) { |
| 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 | |
| 292 | public synchronized void remove(FlowRule rule) { |
| 293 | rules.remove(rule); |
| 294 | counter.decrementAndGet(); |
| 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 | } |