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; |
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; |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 28 | import org.onlab.util.KryoNamespace; |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 29 | import org.onlab.util.Tools; |
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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.store.serializers.KryoNamespaces; |
| 43 | import org.onosproject.store.serializers.KryoSerializer; |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 44 | import org.osgi.service.component.ComponentContext; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 45 | import org.slf4j.Logger; |
| 46 | |
alshabib | 9c57bdd | 2014-11-28 19:14:06 -0500 | [diff] [blame] | 47 | import java.util.Collections; |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 48 | import java.util.Dictionary; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 49 | import java.util.HashSet; |
| 50 | import java.util.Map; |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 51 | import java.util.Properties; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 52 | import java.util.Set; |
| 53 | import java.util.concurrent.ConcurrentHashMap; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 54 | import java.util.concurrent.ExecutorService; |
| 55 | import java.util.concurrent.Executors; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 56 | import java.util.concurrent.TimeUnit; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 57 | import java.util.concurrent.atomic.AtomicInteger; |
| 58 | |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 59 | import static com.google.common.base.Preconditions.checkArgument; |
| 60 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 61 | import static org.onlab.util.Tools.get; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 62 | import static org.onlab.util.Tools.groupedThreads; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 63 | import static org.onosproject.store.statistic.impl.StatisticStoreMessageSubjects.GET_CURRENT; |
| 64 | import static org.onosproject.store.statistic.impl.StatisticStoreMessageSubjects.GET_PREVIOUS; |
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 | |
| 89 | private Map<ConnectPoint, InternalStatisticRepresentation> representations = |
| 90 | new ConcurrentHashMap<>(); |
| 91 | |
| 92 | private Map<ConnectPoint, Set<FlowEntry>> previous = |
| 93 | new ConcurrentHashMap<>(); |
| 94 | |
| 95 | private Map<ConnectPoint, Set<FlowEntry>> current = |
| 96 | new ConcurrentHashMap<>(); |
| 97 | |
| 98 | protected static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 99 | @Override |
| 100 | protected void setupKryoPool() { |
Yuta HIGUCHI | 4cf23ce | 2014-10-22 20:37:13 -0700 | [diff] [blame] | 101 | serializerPool = KryoNamespace.newBuilder() |
| 102 | .register(KryoNamespaces.API) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 103 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
Yuta HIGUCHI | 4cf23ce | 2014-10-22 20:37:13 -0700 | [diff] [blame] | 104 | // register this store specific classes here |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 105 | .build(); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 106 | } |
Sho SHIMIZU | 9f614a4 | 2015-09-11 15:32:46 -0700 | [diff] [blame] | 107 | }; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 108 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 109 | private ExecutorService messageHandlingExecutor; |
| 110 | |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 111 | private static final int DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE = 4; |
| 112 | @Property(name = "messageHandlerThreadPoolSize", intValue = DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE, |
| 113 | label = "Size of thread pool to assign message handler") |
| 114 | private static int messageHandlerThreadPoolSize = DEFAULT_MESSAGE_HANDLER_THREAD_POOL_SIZE; |
| 115 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 116 | private static final long STATISTIC_STORE_TIMEOUT_MILLIS = 3000; |
| 117 | |
| 118 | @Activate |
| 119 | public void activate() { |
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, |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 123 | groupedThreads("onos/store/statistic", "message-handlers")); |
| 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() { |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 142 | clusterCommunicator.removeSubscriber(GET_PREVIOUS); |
| 143 | clusterCommunicator.removeSubscriber(GET_CURRENT); |
| 144 | messageHandlingExecutor.shutdown(); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 145 | log.info("Stopped"); |
| 146 | } |
| 147 | |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 148 | @Modified |
| 149 | public void modified(ComponentContext context) { |
| 150 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 151 | |
| 152 | int newMessageHandlerThreadPoolSize; |
| 153 | |
| 154 | try { |
| 155 | String s = get(properties, "messageHandlerThreadPoolSize"); |
| 156 | |
| 157 | newMessageHandlerThreadPoolSize = |
| 158 | isNullOrEmpty(s) ? messageHandlerThreadPoolSize : Integer.parseInt(s.trim()); |
| 159 | |
| 160 | } catch (NumberFormatException e) { |
| 161 | log.warn(e.getMessage()); |
| 162 | newMessageHandlerThreadPoolSize = messageHandlerThreadPoolSize; |
| 163 | } |
| 164 | |
| 165 | // Any change in the following parameters implies thread pool restart |
| 166 | if (newMessageHandlerThreadPoolSize != messageHandlerThreadPoolSize) { |
| 167 | setMessageHandlerThreadPoolSize(newMessageHandlerThreadPoolSize); |
| 168 | restartMessageHandlerThreadPool(); |
| 169 | } |
| 170 | |
| 171 | log.info(FORMAT, messageHandlerThreadPoolSize); |
| 172 | } |
| 173 | |
| 174 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 175 | @Override |
| 176 | public void prepareForStatistics(FlowRule rule) { |
| 177 | ConnectPoint cp = buildConnectPoint(rule); |
| 178 | if (cp == null) { |
| 179 | return; |
| 180 | } |
| 181 | InternalStatisticRepresentation rep; |
| 182 | synchronized (representations) { |
| 183 | rep = getOrCreateRepresentation(cp); |
| 184 | } |
| 185 | rep.prepare(); |
| 186 | } |
| 187 | |
| 188 | @Override |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 189 | public synchronized void removeFromStatistics(FlowRule rule) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 190 | ConnectPoint cp = buildConnectPoint(rule); |
| 191 | if (cp == null) { |
| 192 | return; |
| 193 | } |
| 194 | InternalStatisticRepresentation rep = representations.get(cp); |
alshabib | 9c57bdd | 2014-11-28 19:14:06 -0500 | [diff] [blame] | 195 | if (rep != null && rep.remove(rule)) { |
| 196 | updatePublishedStats(cp, Collections.emptySet()); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 197 | } |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 198 | Set<FlowEntry> values = current.get(cp); |
| 199 | if (values != null) { |
| 200 | values.remove(rule); |
| 201 | } |
| 202 | values = previous.get(cp); |
| 203 | if (values != null) { |
| 204 | values.remove(rule); |
| 205 | } |
| 206 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void addOrUpdateStatistic(FlowEntry rule) { |
| 211 | ConnectPoint cp = buildConnectPoint(rule); |
| 212 | if (cp == null) { |
| 213 | return; |
| 214 | } |
| 215 | InternalStatisticRepresentation rep = representations.get(cp); |
| 216 | if (rep != null && rep.submit(rule)) { |
| 217 | updatePublishedStats(cp, rep.get()); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | private synchronized void updatePublishedStats(ConnectPoint cp, |
| 222 | Set<FlowEntry> flowEntries) { |
| 223 | Set<FlowEntry> curr = current.get(cp); |
| 224 | if (curr == null) { |
| 225 | curr = new HashSet<>(); |
| 226 | } |
| 227 | previous.put(cp, curr); |
| 228 | current.put(cp, flowEntries); |
| 229 | |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public Set<FlowEntry> getCurrentStatistic(ConnectPoint connectPoint) { |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 234 | final DeviceId deviceId = connectPoint.deviceId(); |
Madan Jampani | c156dd0 | 2015-08-12 15:57:46 -0700 | [diff] [blame] | 235 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 236 | if (master == null) { |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 237 | log.warn("No master for {}", deviceId); |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 238 | return Collections.emptySet(); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 239 | } |
Madan Jampani | c156dd0 | 2015-08-12 15:57:46 -0700 | [diff] [blame] | 240 | if (master.equals(clusterService.getLocalNode().id())) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 241 | return getCurrentStatisticInternal(connectPoint); |
| 242 | } else { |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 243 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive( |
| 244 | connectPoint, |
| 245 | GET_CURRENT, |
| 246 | SERIALIZER::encode, |
| 247 | SERIALIZER::decode, |
Madan Jampani | c156dd0 | 2015-08-12 15:57:46 -0700 | [diff] [blame] | 248 | master), |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 249 | STATISTIC_STORE_TIMEOUT_MILLIS, |
| 250 | TimeUnit.MILLISECONDS, |
| 251 | Collections.emptySet()); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | } |
| 255 | |
| 256 | private synchronized Set<FlowEntry> getCurrentStatisticInternal(ConnectPoint connectPoint) { |
| 257 | return current.get(connectPoint); |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public Set<FlowEntry> getPreviousStatistic(ConnectPoint connectPoint) { |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 262 | final DeviceId deviceId = connectPoint.deviceId(); |
Madan Jampani | c156dd0 | 2015-08-12 15:57:46 -0700 | [diff] [blame] | 263 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 264 | if (master == null) { |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 265 | log.warn("No master for {}", deviceId); |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 266 | return Collections.emptySet(); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 267 | } |
Madan Jampani | c156dd0 | 2015-08-12 15:57:46 -0700 | [diff] [blame] | 268 | if (master.equals(clusterService.getLocalNode().id())) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 269 | return getPreviousStatisticInternal(connectPoint); |
| 270 | } else { |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 271 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive( |
| 272 | connectPoint, |
| 273 | GET_PREVIOUS, |
| 274 | SERIALIZER::encode, |
| 275 | SERIALIZER::decode, |
Madan Jampani | c156dd0 | 2015-08-12 15:57:46 -0700 | [diff] [blame] | 276 | master), |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 277 | STATISTIC_STORE_TIMEOUT_MILLIS, |
| 278 | TimeUnit.MILLISECONDS, |
| 279 | Collections.emptySet()); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 280 | } |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | private synchronized Set<FlowEntry> getPreviousStatisticInternal(ConnectPoint connectPoint) { |
| 284 | return previous.get(connectPoint); |
| 285 | } |
| 286 | |
| 287 | private InternalStatisticRepresentation getOrCreateRepresentation(ConnectPoint cp) { |
| 288 | |
| 289 | if (representations.containsKey(cp)) { |
| 290 | return representations.get(cp); |
| 291 | } else { |
| 292 | InternalStatisticRepresentation rep = new InternalStatisticRepresentation(); |
| 293 | representations.put(cp, rep); |
| 294 | return rep; |
| 295 | } |
| 296 | |
| 297 | } |
| 298 | |
| 299 | private ConnectPoint buildConnectPoint(FlowRule rule) { |
| 300 | PortNumber port = getOutput(rule); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 301 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 302 | if (port == null) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 303 | return null; |
| 304 | } |
| 305 | ConnectPoint cp = new ConnectPoint(rule.deviceId(), port); |
| 306 | return cp; |
| 307 | } |
| 308 | |
| 309 | private PortNumber getOutput(FlowRule rule) { |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 310 | for (Instruction i : rule.treatment().allInstructions()) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 311 | if (i.type() == Instruction.Type.OUTPUT) { |
| 312 | Instructions.OutputInstruction out = (Instructions.OutputInstruction) i; |
| 313 | return out.port(); |
| 314 | } |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 315 | } |
| 316 | return null; |
| 317 | } |
| 318 | |
| 319 | private class InternalStatisticRepresentation { |
| 320 | |
| 321 | private final AtomicInteger counter = new AtomicInteger(0); |
| 322 | private final Set<FlowEntry> rules = new HashSet<>(); |
| 323 | |
| 324 | public void prepare() { |
| 325 | counter.incrementAndGet(); |
| 326 | } |
| 327 | |
alshabib | 9c57bdd | 2014-11-28 19:14:06 -0500 | [diff] [blame] | 328 | public synchronized boolean remove(FlowRule rule) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 329 | rules.remove(rule); |
alshabib | 9c57bdd | 2014-11-28 19:14:06 -0500 | [diff] [blame] | 330 | return counter.decrementAndGet() == 0; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | public synchronized boolean submit(FlowEntry rule) { |
| 334 | if (rules.contains(rule)) { |
| 335 | rules.remove(rule); |
| 336 | } |
| 337 | rules.add(rule); |
| 338 | if (counter.get() == 0) { |
| 339 | return true; |
| 340 | } else { |
| 341 | return counter.decrementAndGet() == 0; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | public synchronized Set<FlowEntry> get() { |
| 346 | counter.set(rules.size()); |
alshabib | f6c2ede | 2014-10-22 23:31:50 -0700 | [diff] [blame] | 347 | return Sets.newHashSet(rules); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | |
| 351 | } |
| 352 | |
sangyun-han | ad84e0c | 2016-02-19 18:30:03 +0900 | [diff] [blame] | 353 | /** |
| 354 | * Sets thread pool size of message handler. |
| 355 | * |
| 356 | * @param poolSize |
| 357 | */ |
| 358 | private void setMessageHandlerThreadPoolSize(int poolSize) { |
| 359 | checkArgument(poolSize >= 0, "Message handler pool size must be 0 or more"); |
| 360 | messageHandlerThreadPoolSize = poolSize; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Restarts thread pool of message handler. |
| 365 | */ |
| 366 | private void restartMessageHandlerThreadPool() { |
| 367 | ExecutorService prevExecutor = messageHandlingExecutor; |
| 368 | messageHandlingExecutor = Executors.newFixedThreadPool(getMessageHandlerThreadPoolSize()); |
| 369 | prevExecutor.shutdown(); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Gets current thread pool size of message handler. |
| 374 | * |
| 375 | * @return messageHandlerThreadPoolSize |
| 376 | */ |
| 377 | private int getMessageHandlerThreadPoolSize() { |
| 378 | return messageHandlerThreadPoolSize; |
| 379 | } |
| 380 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 381 | } |