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