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