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