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