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