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