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