Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -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 | */ |
| 16 | package org.onosproject.intentperf; |
| 17 | |
| 18 | import com.google.common.collect.ArrayListMultimap; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Multimap; |
| 22 | import com.google.common.collect.Sets; |
| 23 | import org.apache.commons.lang.math.RandomUtils; |
| 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Modified; |
| 28 | import org.apache.felix.scr.annotations.Property; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 31 | import org.apache.felix.scr.annotations.Service; |
| 32 | import org.onlab.packet.MacAddress; |
| 33 | import org.onlab.util.Counter; |
| 34 | import org.onosproject.cfg.ComponentConfigService; |
| 35 | import org.onosproject.cluster.ClusterService; |
| 36 | import org.onosproject.cluster.ControllerNode; |
| 37 | import org.onosproject.cluster.NodeId; |
| 38 | import org.onosproject.core.ApplicationId; |
| 39 | import org.onosproject.core.CoreService; |
| 40 | import org.onosproject.mastership.MastershipService; |
| 41 | import org.onosproject.net.ConnectPoint; |
| 42 | import org.onosproject.net.Device; |
| 43 | import org.onosproject.net.PortNumber; |
| 44 | import org.onosproject.net.device.DeviceService; |
| 45 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 46 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 47 | import org.onosproject.net.flow.TrafficSelector; |
| 48 | import org.onosproject.net.flow.TrafficTreatment; |
| 49 | import org.onosproject.net.intent.Intent; |
| 50 | import org.onosproject.net.intent.IntentEvent; |
| 51 | import org.onosproject.net.intent.IntentListener; |
| 52 | import org.onosproject.net.intent.IntentService; |
| 53 | import org.onosproject.net.intent.Key; |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 54 | import org.onosproject.net.intent.WorkPartitionService; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 55 | import org.onosproject.net.intent.PointToPointIntent; |
| 56 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 57 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 58 | import org.osgi.service.component.ComponentContext; |
| 59 | import org.slf4j.Logger; |
| 60 | |
| 61 | import java.util.ArrayList; |
| 62 | import java.util.Collections; |
| 63 | import java.util.Dictionary; |
| 64 | import java.util.List; |
| 65 | import java.util.Map; |
| 66 | import java.util.Set; |
| 67 | import java.util.Timer; |
| 68 | import java.util.TimerTask; |
| 69 | import java.util.concurrent.ExecutorService; |
| 70 | import java.util.concurrent.Executors; |
| 71 | import java.util.concurrent.TimeUnit; |
Sho SHIMIZU | c032c83 | 2016-01-13 13:02:05 -0800 | [diff] [blame] | 72 | import java.util.function.Consumer; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 73 | import java.util.stream.Collectors; |
| 74 | |
| 75 | import static com.google.common.base.Preconditions.checkState; |
| 76 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 77 | import static java.lang.String.format; |
| 78 | import static java.lang.System.currentTimeMillis; |
| 79 | import static org.apache.felix.scr.annotations.ReferenceCardinality.MANDATORY_UNARY; |
| 80 | import static org.onlab.util.Tools.*; |
| 81 | import static org.onosproject.net.intent.IntentEvent.Type.*; |
| 82 | import static org.slf4j.LoggerFactory.getLogger; |
| 83 | |
| 84 | /** |
| 85 | * Application to test sustained intent throughput. |
| 86 | */ |
| 87 | @Component(immediate = true) |
| 88 | @Service(value = IntentPerfInstaller.class) |
| 89 | public class IntentPerfInstaller { |
| 90 | |
| 91 | private final Logger log = getLogger(getClass()); |
| 92 | |
| 93 | private static final int DEFAULT_NUM_WORKERS = 1; |
| 94 | |
| 95 | private static final int DEFAULT_NUM_KEYS = 40000; |
| 96 | private static final int DEFAULT_GOAL_CYCLE_PERIOD = 1000; //ms |
| 97 | |
| 98 | private static final int DEFAULT_NUM_NEIGHBORS = 0; |
| 99 | |
| 100 | private static final int START_DELAY = 5_000; // ms |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 101 | private static final int REPORT_PERIOD = 1_000; //ms |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 102 | |
| 103 | private static final String START = "start"; |
| 104 | private static final String STOP = "stop"; |
| 105 | private static final MessageSubject CONTROL = new MessageSubject("intent-perf-ctl"); |
| 106 | |
| 107 | //FIXME add path length |
| 108 | |
| 109 | @Property(name = "numKeys", intValue = DEFAULT_NUM_KEYS, |
| 110 | label = "Number of keys (i.e. unique intents) to generate per instance") |
| 111 | private int numKeys = DEFAULT_NUM_KEYS; |
| 112 | |
| 113 | //TODO implement numWorkers property |
| 114 | // @Property(name = "numThreads", intValue = DEFAULT_NUM_WORKERS, |
| 115 | // label = "Number of installer threads per instance") |
| 116 | // private int numWokers = DEFAULT_NUM_WORKERS; |
| 117 | |
| 118 | @Property(name = "cyclePeriod", intValue = DEFAULT_GOAL_CYCLE_PERIOD, |
| 119 | label = "Goal for cycle period (in ms)") |
| 120 | private int cyclePeriod = DEFAULT_GOAL_CYCLE_PERIOD; |
| 121 | |
| 122 | @Property(name = "numNeighbors", intValue = DEFAULT_NUM_NEIGHBORS, |
| 123 | label = "Number of neighbors to generate intents for") |
| 124 | private int numNeighbors = DEFAULT_NUM_NEIGHBORS; |
| 125 | |
| 126 | @Reference(cardinality = MANDATORY_UNARY) |
| 127 | protected CoreService coreService; |
| 128 | |
| 129 | @Reference(cardinality = MANDATORY_UNARY) |
| 130 | protected IntentService intentService; |
| 131 | |
| 132 | @Reference(cardinality = MANDATORY_UNARY) |
| 133 | protected ClusterService clusterService; |
| 134 | |
| 135 | @Reference(cardinality = MANDATORY_UNARY) |
| 136 | protected DeviceService deviceService; |
| 137 | |
| 138 | @Reference(cardinality = MANDATORY_UNARY) |
| 139 | protected MastershipService mastershipService; |
| 140 | |
| 141 | @Reference(cardinality = MANDATORY_UNARY) |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 142 | protected WorkPartitionService partitionService; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 143 | |
| 144 | @Reference(cardinality = MANDATORY_UNARY) |
| 145 | protected ComponentConfigService configService; |
| 146 | |
| 147 | @Reference(cardinality = MANDATORY_UNARY) |
| 148 | protected IntentPerfCollector sampleCollector; |
| 149 | |
| 150 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 151 | protected ClusterCommunicationService communicationService; |
| 152 | |
| 153 | private ExecutorService messageHandlingExecutor; |
| 154 | |
| 155 | private ExecutorService workers; |
| 156 | private ApplicationId appId; |
| 157 | private Listener listener; |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 158 | private boolean stopped = true; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 159 | |
| 160 | private Timer reportTimer; |
| 161 | |
| 162 | // FIXME this variable isn't shared properly between multiple worker threads |
| 163 | private int lastKey = 0; |
| 164 | |
| 165 | private IntentPerfUi perfUi; |
| 166 | private NodeId nodeId; |
| 167 | private TimerTask reporterTask; |
| 168 | |
| 169 | @Activate |
| 170 | public void activate(ComponentContext context) { |
| 171 | configService.registerProperties(getClass()); |
| 172 | |
| 173 | nodeId = clusterService.getLocalNode().id(); |
| 174 | appId = coreService.registerApplication("org.onosproject.intentperf." + nodeId.toString()); |
| 175 | |
| 176 | // TODO: replace with shared timer |
| 177 | reportTimer = new Timer("onos-intent-perf-reporter"); |
| 178 | workers = Executors.newFixedThreadPool(DEFAULT_NUM_WORKERS, groupedThreads("onos/intent-perf", "worker-%d")); |
| 179 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 180 | // TODO: replace with shared executor |
| 181 | messageHandlingExecutor = Executors.newSingleThreadExecutor( |
| 182 | groupedThreads("onos/perf", "command-handler")); |
| 183 | |
Sho SHIMIZU | c032c83 | 2016-01-13 13:02:05 -0800 | [diff] [blame] | 184 | communicationService.addSubscriber(CONTROL, String::new, new InternalControl(), |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 185 | messageHandlingExecutor); |
| 186 | |
| 187 | listener = new Listener(); |
| 188 | intentService.addListener(listener); |
| 189 | |
| 190 | // TODO: investigate why this seems to be necessary for configs to get picked up on initial activation |
| 191 | modify(context); |
| 192 | } |
| 193 | |
| 194 | @Deactivate |
| 195 | public void deactivate() { |
| 196 | stopTestRun(); |
| 197 | |
| 198 | configService.unregisterProperties(getClass(), false); |
| 199 | messageHandlingExecutor.shutdown(); |
| 200 | communicationService.removeSubscriber(CONTROL); |
| 201 | |
| 202 | if (listener != null) { |
| 203 | reportTimer.cancel(); |
| 204 | intentService.removeListener(listener); |
| 205 | listener = null; |
| 206 | reportTimer = null; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | @Modified |
| 211 | public void modify(ComponentContext context) { |
| 212 | if (context == null) { |
| 213 | logConfig("Reconfigured"); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | Dictionary<?, ?> properties = context.getProperties(); |
| 218 | int newNumKeys, newCyclePeriod, newNumNeighbors; |
| 219 | try { |
| 220 | String s = get(properties, "numKeys"); |
| 221 | newNumKeys = isNullOrEmpty(s) ? numKeys : Integer.parseInt(s.trim()); |
| 222 | |
| 223 | s = get(properties, "cyclePeriod"); |
| 224 | newCyclePeriod = isNullOrEmpty(s) ? cyclePeriod : Integer.parseInt(s.trim()); |
| 225 | |
| 226 | s = get(properties, "numNeighbors"); |
| 227 | newNumNeighbors = isNullOrEmpty(s) ? numNeighbors : Integer.parseInt(s.trim()); |
| 228 | |
| 229 | } catch (NumberFormatException | ClassCastException e) { |
| 230 | log.warn("Malformed configuration detected; using defaults", e); |
| 231 | newNumKeys = DEFAULT_NUM_KEYS; |
| 232 | newCyclePeriod = DEFAULT_GOAL_CYCLE_PERIOD; |
| 233 | newNumNeighbors = DEFAULT_NUM_NEIGHBORS; |
| 234 | } |
| 235 | |
| 236 | if (newNumKeys != numKeys || newCyclePeriod != cyclePeriod || newNumNeighbors != numNeighbors) { |
| 237 | numKeys = newNumKeys; |
| 238 | cyclePeriod = newCyclePeriod; |
| 239 | numNeighbors = newNumNeighbors; |
| 240 | logConfig("Reconfigured"); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | public void start() { |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 245 | if (stopped) { |
| 246 | stopped = false; |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 247 | communicationService.broadcast(START, CONTROL, str -> str.getBytes()); |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 248 | startTestRun(); |
| 249 | } |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | public void stop() { |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 253 | if (!stopped) { |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 254 | communicationService.broadcast(STOP, CONTROL, str -> str.getBytes()); |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 255 | stopTestRun(); |
| 256 | } |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | private void logConfig(String prefix) { |
| 260 | log.info("{} with appId {}; numKeys = {}; cyclePeriod = {} ms; numNeighbors={}", |
| 261 | prefix, appId.id(), numKeys, cyclePeriod, numNeighbors); |
| 262 | } |
| 263 | |
| 264 | private void startTestRun() { |
| 265 | sampleCollector.clearSamples(); |
| 266 | |
| 267 | // adjust numNeighbors and generate list of neighbors |
| 268 | numNeighbors = Math.min(clusterService.getNodes().size() - 1, numNeighbors); |
| 269 | |
| 270 | // Schedule reporter task on report period boundary |
| 271 | reporterTask = new ReporterTask(); |
| 272 | reportTimer.scheduleAtFixedRate(reporterTask, |
| 273 | REPORT_PERIOD - currentTimeMillis() % REPORT_PERIOD, |
| 274 | REPORT_PERIOD); |
| 275 | |
| 276 | // Submit workers |
| 277 | stopped = false; |
| 278 | for (int i = 0; i < DEFAULT_NUM_WORKERS; i++) { |
| 279 | workers.submit(new Submitter(createIntents(numKeys, /*FIXME*/ 2, lastKey))); |
| 280 | } |
| 281 | log.info("Started test run"); |
| 282 | } |
| 283 | |
| 284 | private void stopTestRun() { |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 285 | if (reporterTask != null) { |
| 286 | reporterTask.cancel(); |
| 287 | reporterTask = null; |
| 288 | } |
| 289 | |
| 290 | try { |
| 291 | workers.awaitTermination(5 * cyclePeriod, TimeUnit.MILLISECONDS); |
| 292 | } catch (InterruptedException e) { |
| 293 | log.warn("Failed to stop worker", e); |
| 294 | } |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 295 | |
| 296 | sampleCollector.recordSample(0, 0); |
| 297 | sampleCollector.recordSample(0, 0); |
| 298 | stopped = true; |
| 299 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 300 | log.info("Stopped test run"); |
| 301 | } |
| 302 | |
| 303 | private List<NodeId> getNeighbors() { |
| 304 | List<NodeId> nodes = clusterService.getNodes().stream() |
| 305 | .map(ControllerNode::id) |
| 306 | .collect(Collectors.toCollection(ArrayList::new)); |
| 307 | // sort neighbors by id |
| 308 | Collections.sort(nodes, (node1, node2) -> |
| 309 | node1.toString().compareTo(node2.toString())); |
| 310 | // rotate the local node to index 0 |
| 311 | Collections.rotate(nodes, -1 * nodes.indexOf(clusterService.getLocalNode().id())); |
| 312 | log.debug("neighbors (raw): {}", nodes); //TODO remove |
| 313 | // generate the sub-list that will contain local node and selected neighbors |
| 314 | nodes = nodes.subList(0, numNeighbors + 1); |
| 315 | log.debug("neighbors: {}", nodes); //TODO remove |
| 316 | return nodes; |
| 317 | } |
| 318 | |
| 319 | private Intent createIntent(Key key, long mac, NodeId node, Multimap<NodeId, Device> devices) { |
| 320 | // choose a random device for which this node is master |
| 321 | List<Device> deviceList = devices.get(node).stream().collect(Collectors.toList()); |
| 322 | Device device = deviceList.get(RandomUtils.nextInt(deviceList.size())); |
| 323 | |
| 324 | //FIXME we currently ignore the path length and always use the same device |
| 325 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 326 | .matchEthDst(MacAddress.valueOf(mac)).build(); |
| 327 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 328 | ConnectPoint ingress = new ConnectPoint(device.id(), PortNumber.portNumber(1)); |
| 329 | ConnectPoint egress = new ConnectPoint(device.id(), PortNumber.portNumber(2)); |
| 330 | |
| 331 | return PointToPointIntent.builder() |
| 332 | .appId(appId) |
| 333 | .key(key) |
| 334 | .selector(selector) |
| 335 | .treatment(treatment) |
| 336 | .ingressPoint(ingress) |
| 337 | .egressPoint(egress) |
| 338 | .build(); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Creates a specified number of intents for testing purposes. |
| 343 | * |
| 344 | * @param numberOfKeys number of intents |
| 345 | * @param pathLength path depth |
| 346 | * @param firstKey first key to attempt |
| 347 | * @return set of intents |
| 348 | */ |
| 349 | private Set<Intent> createIntents(int numberOfKeys, int pathLength, int firstKey) { |
| 350 | List<NodeId> neighbors = getNeighbors(); |
| 351 | |
| 352 | Multimap<NodeId, Device> devices = ArrayListMultimap.create(); |
| 353 | deviceService.getAvailableDevices() |
| 354 | .forEach(device -> devices.put(mastershipService.getMasterFor(device.id()), device)); |
| 355 | |
| 356 | // ensure that we have at least one device per neighbor |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 357 | neighbors.forEach(node -> checkState(!devices.get(node).isEmpty(), |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 358 | "There are no devices for {}", node)); |
| 359 | |
| 360 | // TODO pull this outside so that createIntent can use it |
| 361 | // prefix based on node id for keys generated on this instance |
| 362 | long keyPrefix = ((long) clusterService.getLocalNode().ip().getIp4Address().toInt()) << 32; |
| 363 | |
| 364 | int maxKeysPerNode = (int) Math.ceil((double) numberOfKeys / neighbors.size()); |
| 365 | Multimap<NodeId, Intent> intents = ArrayListMultimap.create(); |
| 366 | |
| 367 | for (int count = 0, k = firstKey; count < numberOfKeys; k++) { |
| 368 | Key key = Key.of(keyPrefix + k, appId); |
| 369 | |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 370 | NodeId leader = partitionService.getLeader(key, Key::hash); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 371 | if (!neighbors.contains(leader) || intents.get(leader).size() >= maxKeysPerNode) { |
| 372 | // Bail if we are not sending to this node or we have enough for this node |
| 373 | continue; |
| 374 | } |
| 375 | intents.put(leader, createIntent(key, keyPrefix + k, leader, devices)); |
| 376 | |
| 377 | // Bump up the counter and remember this as the last key used. |
| 378 | count++; |
| 379 | lastKey = k; |
| 380 | if (count % 1000 == 0) { |
| 381 | log.info("Building intents... {} (attempt: {})", count, lastKey); |
| 382 | } |
| 383 | } |
| 384 | checkState(intents.values().size() == numberOfKeys, |
| 385 | "Generated wrong number of intents"); |
| 386 | log.info("Created {} intents", numberOfKeys); |
| 387 | intents.keySet().forEach(node -> log.info("\t{}\t{}", node, intents.get(node).size())); |
| 388 | |
| 389 | return Sets.newHashSet(intents.values()); |
| 390 | } |
| 391 | |
Yi Tseng | 356d125 | 2017-05-25 16:01:58 -0700 | [diff] [blame] | 392 | final Set<Intent> submitted = Sets.newConcurrentHashSet(); |
| 393 | final Set<Intent> withdrawn = Sets.newConcurrentHashSet(); |
| 394 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 395 | // Submits intent operations. |
| 396 | final class Submitter implements Runnable { |
| 397 | |
| 398 | private long lastDuration; |
| 399 | private int lastCount; |
| 400 | |
| 401 | private Set<Intent> intents = Sets.newHashSet(); |
Yi Tseng | 356d125 | 2017-05-25 16:01:58 -0700 | [diff] [blame] | 402 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 403 | |
| 404 | private Submitter(Set<Intent> intents) { |
| 405 | this.intents = intents; |
| 406 | lastCount = numKeys / 4; |
| 407 | lastDuration = 1_000; // 1 second |
| 408 | } |
| 409 | |
| 410 | @Override |
| 411 | public void run() { |
| 412 | prime(); |
| 413 | while (!stopped) { |
| 414 | try { |
| 415 | cycle(); |
| 416 | } catch (Exception e) { |
| 417 | log.warn("Exception during cycle", e); |
| 418 | } |
| 419 | } |
| 420 | clear(); |
| 421 | } |
| 422 | |
| 423 | private Iterable<Intent> subset(Set<Intent> intents) { |
| 424 | List<Intent> subset = Lists.newArrayList(intents); |
| 425 | Collections.shuffle(subset); |
Yi Tseng | 356d125 | 2017-05-25 16:01:58 -0700 | [diff] [blame] | 426 | return subset.subList(0, Math.min(subset.size(), lastCount)); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | // Submits the specified intent. |
| 430 | private void submit(Intent intent) { |
| 431 | intentService.submit(intent); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 432 | withdrawn.remove(intent); //TODO could check result here... |
| 433 | } |
| 434 | |
| 435 | // Withdraws the specified intent. |
| 436 | private void withdraw(Intent intent) { |
| 437 | intentService.withdraw(intent); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 438 | submitted.remove(intent); //TODO could check result here... |
| 439 | } |
| 440 | |
| 441 | // Primes the cycle. |
| 442 | private void prime() { |
| 443 | int i = 0; |
| 444 | withdrawn.addAll(intents); |
| 445 | for (Intent intent : intents) { |
| 446 | submit(intent); |
| 447 | // only submit half of the intents to start |
| 448 | if (i++ >= intents.size() / 2) { |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | private void clear() { |
| 455 | submitted.forEach(this::withdraw); |
| 456 | } |
| 457 | |
| 458 | // Runs a single operation cycle. |
| 459 | private void cycle() { |
| 460 | //TODO consider running without rate adjustment |
| 461 | adjustRates(); |
| 462 | |
| 463 | long start = currentTimeMillis(); |
| 464 | subset(submitted).forEach(this::withdraw); |
| 465 | subset(withdrawn).forEach(this::submit); |
| 466 | long delta = currentTimeMillis() - start; |
| 467 | |
| 468 | if (delta > cyclePeriod * 3 || delta < 0) { |
| 469 | log.warn("Cycle took {} ms", delta); |
| 470 | } |
| 471 | |
| 472 | int difference = cyclePeriod - (int) delta; |
| 473 | if (difference > 0) { |
| 474 | delay(difference); |
| 475 | } |
| 476 | |
| 477 | lastDuration = delta; |
| 478 | } |
| 479 | |
| 480 | int cycleCount = 0; |
| 481 | |
| 482 | private void adjustRates() { |
| 483 | |
| 484 | int addDelta = Math.max(1000 - cycleCount, 10); |
| 485 | double multRatio = Math.min(0.8 + cycleCount * 0.0002, 0.995); |
| 486 | |
| 487 | //FIXME need to iron out the rate adjustment |
| 488 | //FIXME we should taper the adjustments over time |
| 489 | //FIXME don't just use the lastDuration, take an average |
| 490 | if (++cycleCount % 5 == 0) { //TODO: maybe use a timer (we should do this every 5-10 sec) |
| 491 | if (listener.requestThroughput() - listener.processedThroughput() <= 2000 && //was 500 |
| 492 | lastDuration <= cyclePeriod) { |
| 493 | lastCount = Math.min(lastCount + addDelta, intents.size() / 2); |
| 494 | } else { |
| 495 | lastCount *= multRatio; |
| 496 | } |
| 497 | log.info("last count: {}, last duration: {} ms (sub: {} vs inst: {})", |
| 498 | lastCount, lastDuration, listener.requestThroughput(), listener.processedThroughput()); |
| 499 | } |
| 500 | |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | // Event listener to monitor throughput. |
| 505 | final class Listener implements IntentListener { |
| 506 | |
| 507 | private final Counter runningTotal = new Counter(); |
| 508 | private volatile Map<IntentEvent.Type, Counter> counters; |
| 509 | |
| 510 | private volatile double processedThroughput = 0; |
| 511 | private volatile double requestThroughput = 0; |
| 512 | |
| 513 | public Listener() { |
| 514 | counters = initCounters(); |
| 515 | } |
| 516 | |
| 517 | private Map<IntentEvent.Type, Counter> initCounters() { |
| 518 | Map<IntentEvent.Type, Counter> map = Maps.newHashMap(); |
| 519 | for (IntentEvent.Type type : IntentEvent.Type.values()) { |
| 520 | map.put(type, new Counter()); |
| 521 | } |
| 522 | return map; |
| 523 | } |
| 524 | |
| 525 | public double processedThroughput() { |
| 526 | return processedThroughput; |
| 527 | } |
| 528 | |
| 529 | public double requestThroughput() { |
| 530 | return requestThroughput; |
| 531 | } |
| 532 | |
| 533 | @Override |
| 534 | public void event(IntentEvent event) { |
| 535 | if (event.subject().appId().equals(appId)) { |
Yi Tseng | 356d125 | 2017-05-25 16:01:58 -0700 | [diff] [blame] | 536 | if (event.type() == INSTALLED) { |
| 537 | submitted.add(event.subject()); |
| 538 | } |
| 539 | if (event.type() == WITHDRAWN) { |
| 540 | withdrawn.add(event.subject()); |
| 541 | } |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 542 | counters.get(event.type()).add(1); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | public void report() { |
| 547 | Map<IntentEvent.Type, Counter> reportCounters = counters; |
| 548 | counters = initCounters(); |
| 549 | |
| 550 | // update running total and latest throughput |
| 551 | Counter installed = reportCounters.get(INSTALLED); |
| 552 | Counter withdrawn = reportCounters.get(WITHDRAWN); |
| 553 | processedThroughput = installed.throughput() + withdrawn.throughput(); |
| 554 | runningTotal.add(installed.total() + withdrawn.total()); |
| 555 | |
| 556 | Counter installReq = reportCounters.get(INSTALL_REQ); |
| 557 | Counter withdrawReq = reportCounters.get(WITHDRAW_REQ); |
| 558 | requestThroughput = installReq.throughput() + withdrawReq.throughput(); |
| 559 | |
| 560 | // build the string to report |
| 561 | StringBuilder stringBuilder = new StringBuilder(); |
| 562 | for (IntentEvent.Type type : IntentEvent.Type.values()) { |
| 563 | Counter counter = reportCounters.get(type); |
| 564 | stringBuilder.append(format("%s=%.2f;", type, counter.throughput())); |
| 565 | } |
| 566 | log.info("Throughput: OVERALL={}; CURRENT={}; {}", |
| 567 | format("%.2f", runningTotal.throughput()), |
| 568 | format("%.2f", processedThroughput), |
| 569 | stringBuilder); |
| 570 | |
| 571 | sampleCollector.recordSample(runningTotal.throughput(), |
| 572 | processedThroughput); |
| 573 | } |
| 574 | } |
| 575 | |
Sho SHIMIZU | c032c83 | 2016-01-13 13:02:05 -0800 | [diff] [blame] | 576 | private class InternalControl implements Consumer<String> { |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 577 | @Override |
Sho SHIMIZU | c032c83 | 2016-01-13 13:02:05 -0800 | [diff] [blame] | 578 | public void accept(String cmd) { |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 579 | log.info("Received command {}", cmd); |
| 580 | if (cmd.equals(START)) { |
| 581 | startTestRun(); |
| 582 | } else { |
| 583 | stopTestRun(); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | private class ReporterTask extends TimerTask { |
| 589 | @Override |
| 590 | public void run() { |
| 591 | //adjustRates(); // FIXME we currently adjust rates in the cycle thread |
| 592 | listener.report(); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | } |