Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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; |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Modified; |
| 24 | import org.apache.felix.scr.annotations.Property; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 28 | import org.onlab.util.Tools; |
sangyun-han | 9f0af2d | 2016-08-04 13:04:59 +0900 | [diff] [blame] | 29 | import org.onosproject.cfg.ComponentConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.cluster.ClusterService; |
Madan Jampani | c156dd0 | 2015-08-12 15:57:46 -0700 | [diff] [blame] | 31 | import org.onosproject.cluster.NodeId; |
| 32 | import org.onosproject.mastership.MastershipService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 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.StatisticStore; |
| 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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -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; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | |
alshabib | 9c57bdd | 2014-11-28 19:14:06 -0500 | [diff] [blame] | 48 | import java.util.Collections; |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 49 | import java.util.Dictionary; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 50 | import java.util.HashSet; |
| 51 | import java.util.Map; |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 52 | import java.util.Properties; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 53 | import java.util.Set; |
| 54 | import java.util.concurrent.ConcurrentHashMap; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 55 | import java.util.concurrent.ExecutorService; |
| 56 | import java.util.concurrent.Executors; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 57 | import java.util.concurrent.TimeUnit; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 58 | import java.util.concurrent.atomic.AtomicInteger; |
| 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 | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 64 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 65 | import static org.slf4j.LoggerFactory.getLogger; |
| 66 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Maintains statistics using RPC calls to collect stats from remote instances |
| 70 | * on demand. |
| 71 | */ |
| 72 | @Component(immediate = true) |
| 73 | @Service |
| 74 | public class DistributedStatisticStore implements StatisticStore { |
| 75 | |
| 76 | private final Logger log = getLogger(getClass()); |
| 77 | |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 78 | private static final String FORMAT = "Setting: messageHandlerThreadPoolSize={}"; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 79 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Madan Jampani | c156dd0 | 2015-08-12 15:57:46 -0700 | [diff] [blame] | 81 | protected MastershipService mastershipService; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 84 | protected ClusterCommunicationService clusterCommunicator; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 87 | protected ClusterService clusterService; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 88 | |
sangyun-han | 9f0af2d | 2016-08-04 13:04:59 +0900 | [diff] [blame] | 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected ComponentConfigService cfgService; |
| 91 | |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 92 | public static final MessageSubject GET_CURRENT = new MessageSubject("peer-return-current"); |
| 93 | public static final MessageSubject GET_PREVIOUS = new MessageSubject("peer-return-previous"); |
| 94 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 95 | private Map<ConnectPoint, InternalStatisticRepresentation> representations = |
| 96 | new ConcurrentHashMap<>(); |
| 97 | |
| 98 | private Map<ConnectPoint, Set<FlowEntry>> previous = |
| 99 | new ConcurrentHashMap<>(); |
| 100 | |
| 101 | private Map<ConnectPoint, Set<FlowEntry>> current = |
| 102 | new ConcurrentHashMap<>(); |
| 103 | |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 104 | protected static final StoreSerializer SERIALIZER = StoreSerializer.using(KryoNamespaces.API); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 105 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 106 | private ExecutorService messageHandlingExecutor; |
| 107 | |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 108 | private static final int DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE = 4; |
| 109 | @Property(name = "messageHandlerThreadPoolSize", intValue = DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE, |
| 110 | label = "Size of thread pool to assign message handler") |
| 111 | private static int messageHandlerThreadPoolSize = DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE; |
| 112 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 113 | private static final long STATISTIC_STORE_TIMEOUT_MILLIS = 3000; |
| 114 | |
| 115 | @Activate |
sangyun-han | 9f0af2d | 2016-08-04 13:04:59 +0900 | [diff] [blame] | 116 | public void activate(ComponentContext context) { |
| 117 | cfgService.registerProperties(getClass()); |
| 118 | |
| 119 | modified(context); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 120 | |
| 121 | messageHandlingExecutor = Executors.newFixedThreadPool( |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 122 | messageHandlerThreadPoolSize, |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 123 | groupedThreads("onos/store/statistic", "message-handlers", log)); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 124 | |
Madan Jampani | 1151b55 | 2015-08-12 16:11:27 -0700 | [diff] [blame] | 125 | clusterCommunicator.<ConnectPoint, Set<FlowEntry>>addSubscriber(GET_CURRENT, |
| 126 | SERIALIZER::decode, |
| 127 | this::getCurrentStatisticInternal, |
| 128 | SERIALIZER::encode, |
| 129 | messageHandlingExecutor); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 130 | |
Madan Jampani | 1151b55 | 2015-08-12 16:11:27 -0700 | [diff] [blame] | 131 | clusterCommunicator.<ConnectPoint, Set<FlowEntry>>addSubscriber(GET_PREVIOUS, |
| 132 | SERIALIZER::decode, |
| 133 | this::getPreviousStatisticInternal, |
| 134 | SERIALIZER::encode, |
| 135 | messageHandlingExecutor); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 136 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 137 | log.info("Started"); |
| 138 | } |
| 139 | |
| 140 | @Deactivate |
| 141 | public void deactivate() { |
sangyun-han | 9f0af2d | 2016-08-04 13:04:59 +0900 | [diff] [blame] | 142 | cfgService.unregisterProperties(getClass(), false); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 143 | clusterCommunicator.removeSubscriber(GET_PREVIOUS); |
| 144 | clusterCommunicator.removeSubscriber(GET_CURRENT); |
| 145 | messageHandlingExecutor.shutdown(); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 146 | log.info("Stopped"); |
| 147 | } |
| 148 | |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 149 | @Modified |
| 150 | public void modified(ComponentContext context) { |
| 151 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 152 | |
| 153 | int newMessageHandlerThreadPoolSize; |
| 154 | |
| 155 | try { |
| 156 | String s = get(properties, "messageHandlerThreadPoolSize"); |
| 157 | |
| 158 | newMessageHandlerThreadPoolSize = |
| 159 | isNullOrEmpty(s) ? messageHandlerThreadPoolSize : Integer.parseInt(s.trim()); |
| 160 | |
| 161 | } catch (NumberFormatException e) { |
| 162 | log.warn(e.getMessage()); |
| 163 | newMessageHandlerThreadPoolSize = messageHandlerThreadPoolSize; |
| 164 | } |
| 165 | |
| 166 | // Any change in the following parameters implies thread pool restart |
| 167 | if (newMessageHandlerThreadPoolSize != messageHandlerThreadPoolSize) { |
| 168 | setMessageHandlerThreadPoolSize(newMessageHandlerThreadPoolSize); |
| 169 | restartMessageHandlerThreadPool(); |
| 170 | } |
| 171 | |
| 172 | log.info(FORMAT, messageHandlerThreadPoolSize); |
| 173 | } |
| 174 | |
| 175 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 176 | @Override |
| 177 | public void prepareForStatistics(FlowRule rule) { |
| 178 | ConnectPoint cp = buildConnectPoint(rule); |
| 179 | if (cp == null) { |
| 180 | return; |
| 181 | } |
| 182 | InternalStatisticRepresentation rep; |
| 183 | synchronized (representations) { |
| 184 | rep = getOrCreateRepresentation(cp); |
| 185 | } |
| 186 | rep.prepare(); |
| 187 | } |
| 188 | |
| 189 | @Override |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 190 | public synchronized void removeFromStatistics(FlowRule rule) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 191 | ConnectPoint cp = buildConnectPoint(rule); |
| 192 | if (cp == null) { |
| 193 | return; |
| 194 | } |
| 195 | InternalStatisticRepresentation rep = representations.get(cp); |
alshabib | 9c57bdd | 2014-11-28 19:14:06 -0500 | [diff] [blame] | 196 | if (rep != null && rep.remove(rule)) { |
| 197 | updatePublishedStats(cp, Collections.emptySet()); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 198 | } |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 199 | Set<FlowEntry> values = current.get(cp); |
| 200 | if (values != null) { |
| 201 | values.remove(rule); |
| 202 | } |
| 203 | values = previous.get(cp); |
| 204 | if (values != null) { |
| 205 | values.remove(rule); |
| 206 | } |
| 207 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public void addOrUpdateStatistic(FlowEntry rule) { |
| 212 | ConnectPoint cp = buildConnectPoint(rule); |
| 213 | if (cp == null) { |
| 214 | return; |
| 215 | } |
| 216 | InternalStatisticRepresentation rep = representations.get(cp); |
| 217 | if (rep != null && rep.submit(rule)) { |
| 218 | updatePublishedStats(cp, rep.get()); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | private synchronized void updatePublishedStats(ConnectPoint cp, |
| 223 | Set<FlowEntry> flowEntries) { |
| 224 | Set<FlowEntry> curr = current.get(cp); |
| 225 | if (curr == null) { |
| 226 | curr = new HashSet<>(); |
| 227 | } |
| 228 | previous.put(cp, curr); |
| 229 | current.put(cp, flowEntries); |
| 230 | |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public Set<FlowEntry> getCurrentStatistic(ConnectPoint connectPoint) { |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 235 | final DeviceId deviceId = connectPoint.deviceId(); |
Madan Jampani | c156dd0 | 2015-08-12 15:57:46 -0700 | [diff] [blame] | 236 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 237 | if (master == null) { |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 238 | log.warn("No master for {}", deviceId); |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 239 | return Collections.emptySet(); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 240 | } |
Madan Jampani | c156dd0 | 2015-08-12 15:57:46 -0700 | [diff] [blame] | 241 | if (master.equals(clusterService.getLocalNode().id())) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 242 | return getCurrentStatisticInternal(connectPoint); |
| 243 | } else { |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 244 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive( |
| 245 | connectPoint, |
| 246 | GET_CURRENT, |
| 247 | SERIALIZER::encode, |
| 248 | SERIALIZER::decode, |
Madan Jampani | c156dd0 | 2015-08-12 15:57:46 -0700 | [diff] [blame] | 249 | master), |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 250 | STATISTIC_STORE_TIMEOUT_MILLIS, |
| 251 | TimeUnit.MILLISECONDS, |
| 252 | Collections.emptySet()); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | } |
| 256 | |
| 257 | private synchronized Set<FlowEntry> getCurrentStatisticInternal(ConnectPoint connectPoint) { |
| 258 | return current.get(connectPoint); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public Set<FlowEntry> getPreviousStatistic(ConnectPoint connectPoint) { |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 263 | final DeviceId deviceId = connectPoint.deviceId(); |
Madan Jampani | c156dd0 | 2015-08-12 15:57:46 -0700 | [diff] [blame] | 264 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 265 | if (master == null) { |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 266 | log.warn("No master for {}", deviceId); |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 267 | return Collections.emptySet(); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 268 | } |
Madan Jampani | c156dd0 | 2015-08-12 15:57:46 -0700 | [diff] [blame] | 269 | if (master.equals(clusterService.getLocalNode().id())) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 270 | return getPreviousStatisticInternal(connectPoint); |
| 271 | } else { |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 272 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive( |
| 273 | connectPoint, |
| 274 | GET_PREVIOUS, |
| 275 | SERIALIZER::encode, |
| 276 | SERIALIZER::decode, |
Madan Jampani | c156dd0 | 2015-08-12 15:57:46 -0700 | [diff] [blame] | 277 | master), |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 278 | STATISTIC_STORE_TIMEOUT_MILLIS, |
| 279 | TimeUnit.MILLISECONDS, |
| 280 | Collections.emptySet()); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 281 | } |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | private synchronized Set<FlowEntry> getPreviousStatisticInternal(ConnectPoint connectPoint) { |
| 285 | return previous.get(connectPoint); |
| 286 | } |
| 287 | |
| 288 | private InternalStatisticRepresentation getOrCreateRepresentation(ConnectPoint cp) { |
| 289 | |
| 290 | if (representations.containsKey(cp)) { |
| 291 | return representations.get(cp); |
| 292 | } else { |
| 293 | InternalStatisticRepresentation rep = new InternalStatisticRepresentation(); |
| 294 | representations.put(cp, rep); |
| 295 | return rep; |
| 296 | } |
| 297 | |
| 298 | } |
| 299 | |
| 300 | private ConnectPoint buildConnectPoint(FlowRule rule) { |
| 301 | PortNumber port = getOutput(rule); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 302 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 303 | if (port == null) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 304 | return null; |
| 305 | } |
| 306 | ConnectPoint cp = new ConnectPoint(rule.deviceId(), port); |
| 307 | return cp; |
| 308 | } |
| 309 | |
| 310 | private PortNumber getOutput(FlowRule rule) { |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 311 | for (Instruction i : rule.treatment().allInstructions()) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 312 | if (i.type() == Instruction.Type.OUTPUT) { |
| 313 | Instructions.OutputInstruction out = (Instructions.OutputInstruction) i; |
| 314 | return out.port(); |
| 315 | } |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 316 | } |
| 317 | return null; |
| 318 | } |
| 319 | |
| 320 | private class InternalStatisticRepresentation { |
| 321 | |
| 322 | private final AtomicInteger counter = new AtomicInteger(0); |
| 323 | private final Set<FlowEntry> rules = new HashSet<>(); |
| 324 | |
| 325 | public void prepare() { |
| 326 | counter.incrementAndGet(); |
| 327 | } |
| 328 | |
alshabib | 9c57bdd | 2014-11-28 19:14:06 -0500 | [diff] [blame] | 329 | public synchronized boolean remove(FlowRule rule) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 330 | rules.remove(rule); |
alshabib | 9c57bdd | 2014-11-28 19:14:06 -0500 | [diff] [blame] | 331 | return counter.decrementAndGet() == 0; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | public synchronized boolean submit(FlowEntry rule) { |
| 335 | if (rules.contains(rule)) { |
| 336 | rules.remove(rule); |
| 337 | } |
| 338 | rules.add(rule); |
| 339 | if (counter.get() == 0) { |
| 340 | return true; |
| 341 | } else { |
| 342 | return counter.decrementAndGet() == 0; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | public synchronized Set<FlowEntry> get() { |
| 347 | counter.set(rules.size()); |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 348 | return Sets.newHashSet(rules); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | |
| 352 | } |
| 353 | |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 354 | /** |
| 355 | * Sets thread pool size of message handler. |
| 356 | * |
| 357 | * @param poolSize |
| 358 | */ |
| 359 | private void setMessageHandlerThreadPoolSize(int poolSize) { |
| 360 | checkArgument(poolSize >= 0, "Message handler pool size must be 0 or more"); |
| 361 | messageHandlerThreadPoolSize = poolSize; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Restarts thread pool of message handler. |
| 366 | */ |
| 367 | private void restartMessageHandlerThreadPool() { |
| 368 | ExecutorService prevExecutor = messageHandlingExecutor; |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 369 | messageHandlingExecutor = newFixedThreadPool(getMessageHandlerThreadPoolSize(), |
| 370 | groupedThreads("DistStatsStore", "messageHandling-%d", log)); |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 371 | prevExecutor.shutdown(); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Gets current thread pool size of message handler. |
| 376 | * |
| 377 | * @return messageHandlerThreadPoolSize |
| 378 | */ |
| 379 | private int getMessageHandlerThreadPoolSize() { |
| 380 | return messageHandlerThreadPoolSize; |
| 381 | } |
| 382 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 383 | } |