Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent.impl; |
| 17 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.Collections; |
| 20 | import java.util.List; |
| 21 | import java.util.Map; |
| 22 | import java.util.concurrent.ConcurrentHashMap; |
| 23 | import java.util.concurrent.ConcurrentMap; |
| 24 | import java.util.concurrent.ExecutionException; |
| 25 | import java.util.concurrent.ExecutorService; |
| 26 | import java.util.concurrent.Future; |
| 27 | import java.util.concurrent.TimeUnit; |
| 28 | import java.util.concurrent.TimeoutException; |
| 29 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 30 | import org.apache.felix.scr.annotations.Activate; |
| 31 | import org.apache.felix.scr.annotations.Component; |
| 32 | import org.apache.felix.scr.annotations.Deactivate; |
| 33 | import org.apache.felix.scr.annotations.Reference; |
| 34 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 35 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 36 | import org.onlab.onos.core.ApplicationId; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 37 | import org.onlab.onos.core.CoreService; |
| 38 | import org.onlab.onos.core.IdGenerator; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 39 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 40 | import org.onlab.onos.event.EventDeliveryService; |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 41 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
| 43 | import org.onlab.onos.net.flow.FlowRuleService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 44 | import org.onlab.onos.net.intent.Intent; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 45 | import org.onlab.onos.net.intent.IntentBatchDelegate; |
| 46 | import org.onlab.onos.net.intent.IntentBatchService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 47 | import org.onlab.onos.net.intent.IntentCompiler; |
| 48 | import org.onlab.onos.net.intent.IntentEvent; |
| 49 | import org.onlab.onos.net.intent.IntentException; |
| 50 | import org.onlab.onos.net.intent.IntentExtensionService; |
| 51 | import org.onlab.onos.net.intent.IntentId; |
| 52 | import org.onlab.onos.net.intent.IntentInstaller; |
| 53 | import org.onlab.onos.net.intent.IntentListener; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 54 | import org.onlab.onos.net.intent.IntentOperation; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 55 | import org.onlab.onos.net.intent.IntentOperations; |
| 56 | import org.onlab.onos.net.intent.IntentService; |
| 57 | import org.onlab.onos.net.intent.IntentState; |
| 58 | import org.onlab.onos.net.intent.IntentStore; |
| 59 | import org.onlab.onos.net.intent.IntentStoreDelegate; |
| 60 | import org.slf4j.Logger; |
| 61 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 62 | import com.google.common.collect.ImmutableList; |
| 63 | import com.google.common.collect.ImmutableMap; |
| 64 | import com.google.common.collect.Lists; |
| 65 | import com.google.common.collect.Maps; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 66 | |
| 67 | import static com.google.common.base.Preconditions.checkArgument; |
| 68 | import static com.google.common.base.Preconditions.checkNotNull; |
| 69 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 70 | import static org.onlab.onos.net.intent.IntentState.COMPILING; |
| 71 | import static org.onlab.onos.net.intent.IntentState.FAILED; |
| 72 | import static org.onlab.onos.net.intent.IntentState.INSTALLED; |
| 73 | import static org.onlab.onos.net.intent.IntentState.INSTALLING; |
| 74 | import static org.onlab.onos.net.intent.IntentState.WITHDRAWING; |
| 75 | import static org.onlab.onos.net.intent.IntentState.WITHDRAWN; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 76 | import static org.onlab.util.Tools.namedThreads; |
| 77 | import static org.slf4j.LoggerFactory.getLogger; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * An implementation of Intent Manager. |
| 81 | */ |
| 82 | @Component(immediate = true) |
| 83 | @Service |
| 84 | public class IntentManager |
| 85 | implements IntentService, IntentExtensionService { |
Sho SHIMIZU | 8b5051d | 2014-11-05 11:24:13 -0800 | [diff] [blame] | 86 | private static final Logger log = getLogger(IntentManager.class); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 87 | |
| 88 | public static final String INTENT_NULL = "Intent cannot be null"; |
| 89 | public static final String INTENT_ID_NULL = "Intent ID cannot be null"; |
| 90 | |
| 91 | // Collections for compiler, installer, and listener are ONOS instance local |
| 92 | private final ConcurrentMap<Class<? extends Intent>, |
| 93 | IntentCompiler<? extends Intent>> compilers = new ConcurrentHashMap<>(); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 94 | private final ConcurrentMap<Class<? extends Intent>, |
| 95 | IntentInstaller<? extends Intent>> installers = new ConcurrentHashMap<>(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 96 | |
| 97 | private final AbstractListenerRegistry<IntentEvent, IntentListener> |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 98 | listenerRegistry = new AbstractListenerRegistry<>(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 99 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 101 | protected CoreService coreService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected IntentStore store; |
| 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 107 | protected IntentBatchService batchService; |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 110 | protected ObjectiveTrackerService trackerService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 111 | |
| 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 113 | protected EventDeliveryService eventDispatcher; |
| 114 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 116 | protected FlowRuleService flowRuleService; |
| 117 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 118 | |
| 119 | private ExecutorService executor; |
| 120 | private ExecutorService monitorExecutor; |
| 121 | |
| 122 | private final IntentStoreDelegate delegate = new InternalStoreDelegate(); |
| 123 | private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate(); |
| 124 | private final IntentBatchDelegate batchDelegate = new InternalBatchDelegate(); |
| 125 | private IdGenerator idGenerator; |
| 126 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 127 | @Activate |
| 128 | public void activate() { |
| 129 | store.setDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 130 | trackerService.setDelegate(topoDelegate); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 131 | batchService.setDelegate(batchDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 132 | eventDispatcher.addSink(IntentEvent.class, listenerRegistry); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 133 | executor = newSingleThreadExecutor(namedThreads("onos-intents")); |
| 134 | monitorExecutor = newSingleThreadExecutor(namedThreads("onos-intent-monitor")); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 135 | idGenerator = coreService.getIdGenerator("intent-ids"); |
| 136 | Intent.bindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 137 | log.info("Started"); |
| 138 | } |
| 139 | |
| 140 | @Deactivate |
| 141 | public void deactivate() { |
| 142 | store.unsetDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 143 | trackerService.unsetDelegate(topoDelegate); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 144 | batchService.unsetDelegate(batchDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 145 | eventDispatcher.removeSink(IntentEvent.class); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 146 | executor.shutdown(); |
| 147 | monitorExecutor.shutdown(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 148 | Intent.unbindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 149 | log.info("Stopped"); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void submit(Intent intent) { |
| 154 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 155 | execute(IntentOperations.builder(intent.appId()) |
| 156 | .addSubmitOperation(intent).build()); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void withdraw(Intent intent) { |
| 161 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 162 | execute(IntentOperations.builder(intent.appId()) |
| 163 | .addWithdrawOperation(intent.id()).build()); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 166 | @Override |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 167 | public void replace(IntentId oldIntentId, Intent newIntent) { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 168 | checkNotNull(oldIntentId, INTENT_ID_NULL); |
| 169 | checkNotNull(newIntent, INTENT_NULL); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 170 | execute(IntentOperations.builder(newIntent.appId()) |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 171 | .addReplaceOperation(oldIntentId, newIntent) |
| 172 | .build()); |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 175 | @Override |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 176 | public void execute(IntentOperations operations) { |
Brian O'Connor | e2ff25a | 2014-11-18 19:25:43 -0800 | [diff] [blame] | 177 | if (operations.operations().isEmpty()) { |
| 178 | return; |
| 179 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 180 | batchService.addIntentOperations(operations); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public Iterable<Intent> getIntents() { |
| 185 | return store.getIntents(); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public long getIntentCount() { |
| 190 | return store.getIntentCount(); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public Intent getIntent(IntentId id) { |
| 195 | checkNotNull(id, INTENT_ID_NULL); |
| 196 | return store.getIntent(id); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public IntentState getIntentState(IntentId id) { |
| 201 | checkNotNull(id, INTENT_ID_NULL); |
| 202 | return store.getIntentState(id); |
| 203 | } |
| 204 | |
| 205 | @Override |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 206 | public List<Intent> getInstallableIntents(IntentId intentId) { |
| 207 | checkNotNull(intentId, INTENT_ID_NULL); |
| 208 | return store.getInstallableIntents(intentId); |
| 209 | } |
| 210 | |
| 211 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 212 | public void addListener(IntentListener listener) { |
| 213 | listenerRegistry.addListener(listener); |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public void removeListener(IntentListener listener) { |
| 218 | listenerRegistry.removeListener(listener); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) { |
| 223 | compilers.put(cls, compiler); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
| 228 | compilers.remove(cls); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { |
| 233 | return ImmutableMap.copyOf(compilers); |
| 234 | } |
| 235 | |
| 236 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 237 | public <T extends Intent> void registerInstaller(Class<T> cls, IntentInstaller<T> installer) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 238 | installers.put(cls, installer); |
| 239 | } |
| 240 | |
| 241 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 242 | public <T extends Intent> void unregisterInstaller(Class<T> cls) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 243 | installers.remove(cls); |
| 244 | } |
| 245 | |
| 246 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 247 | public Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> getInstallers() { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 248 | return ImmutableMap.copyOf(installers); |
| 249 | } |
| 250 | |
| 251 | /** |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 252 | * Returns the corresponding intent compiler to the specified intent. |
| 253 | * |
| 254 | * @param intent intent |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 255 | * @param <T> the type of intent |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 256 | * @return intent compiler corresponding to the specified intent |
| 257 | */ |
| 258 | private <T extends Intent> IntentCompiler<T> getCompiler(T intent) { |
| 259 | @SuppressWarnings("unchecked") |
| 260 | IntentCompiler<T> compiler = (IntentCompiler<T>) compilers.get(intent.getClass()); |
| 261 | if (compiler == null) { |
| 262 | throw new IntentException("no compiler for class " + intent.getClass()); |
| 263 | } |
| 264 | return compiler; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Returns the corresponding intent installer to the specified installable intent. |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 269 | * |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 270 | * @param intent intent |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 271 | * @param <T> the type of installable intent |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 272 | * @return intent installer corresponding to the specified installable intent |
| 273 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 274 | private <T extends Intent> IntentInstaller<T> getInstaller(T intent) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 275 | @SuppressWarnings("unchecked") |
| 276 | IntentInstaller<T> installer = (IntentInstaller<T>) installers.get(intent.getClass()); |
| 277 | if (installer == null) { |
| 278 | throw new IntentException("no installer for class " + intent.getClass()); |
| 279 | } |
| 280 | return installer; |
| 281 | } |
| 282 | |
| 283 | /** |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 284 | * Compiles the specified intent. |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 285 | * |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 286 | * @param update intent update |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 287 | */ |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 288 | private void executeCompilingPhase(IntentUpdate update) { |
| 289 | Intent intent = update.newIntent(); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 290 | // Indicate that the intent is entering the compiling phase. |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 291 | update.setState(intent, COMPILING); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 292 | |
| 293 | try { |
| 294 | // Compile the intent into installable derivatives. |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 295 | List<Intent> installables = compileIntent(intent, update); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 296 | |
| 297 | // If all went well, associate the resulting list of installable |
| 298 | // intents with the top-level intent and proceed to install. |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 299 | update.setInstallables(installables); |
| 300 | } catch (IntentException e) { |
Jonathan Hart | 1109640 | 2014-10-20 17:31:49 -0700 | [diff] [blame] | 301 | log.warn("Unable to compile intent {} due to:", intent.id(), e); |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 302 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 303 | // If compilation failed, mark the intent as failed. |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 304 | update.setState(intent, FAILED); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 308 | /** |
| 309 | * Compiles an intent recursively. |
| 310 | * |
| 311 | * @param intent intent |
| 312 | * @return result of compilation |
| 313 | */ |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 314 | private List<Intent> compileIntent(Intent intent, IntentUpdate update) { |
Thomas Vachuska | 4926c1b | 2014-10-21 00:44:10 -0700 | [diff] [blame] | 315 | if (intent.isInstallable()) { |
| 316 | return ImmutableList.of(intent); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 317 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 318 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 319 | registerSubclassCompilerIfNeeded(intent); |
| 320 | List<Intent> previous = update.oldInstallables(); |
| 321 | // FIXME: get previous resources |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 322 | List<Intent> installable = new ArrayList<>(); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 323 | for (Intent compiled : getCompiler(intent).compile(intent, previous, null)) { |
| 324 | installable.addAll(compileIntent(compiled, update)); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 325 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 326 | return installable; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /** |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 330 | * Installs all installable intents associated with the specified top-level |
| 331 | * intent. |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 332 | * |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 333 | * @param update intent update |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 334 | */ |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 335 | private void executeInstallingPhase(IntentUpdate update) { |
| 336 | if (update.newInstallables() == null) { |
| 337 | //no failed intents allowed past this point... |
| 338 | return; |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 339 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 340 | // Indicate that the intent is entering the installing phase. |
| 341 | update.setState(update.newIntent(), INSTALLING); |
| 342 | |
| 343 | List<FlowRuleBatchOperation> batches = Lists.newArrayList(); |
| 344 | for (Intent installable : update.newInstallables()) { |
| 345 | registerSubclassInstallerIfNeeded(installable); |
| 346 | trackerService.addTrackedResources(update.newIntent().id(), |
| 347 | installable.resources()); |
| 348 | try { |
| 349 | batches.addAll(getInstaller(installable).install(installable)); |
| 350 | } catch (IntentException e) { |
| 351 | log.warn("Unable to install intent {} due to:", update.newIntent().id(), e); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 352 | trackerService.removeTrackedResources(update.newIntent().id(), |
| 353 | installable.resources()); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 354 | //FIXME we failed... intent should be recompiled |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 357 | update.addBatches(batches); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Uninstalls the specified intent by uninstalling all of its associated |
| 362 | * installable derivatives. |
| 363 | * |
| 364 | * @param update intent update |
| 365 | */ |
| 366 | private void executeWithdrawingPhase(IntentUpdate update) { |
| 367 | if (!update.oldIntent().equals(update.newIntent())) { |
| 368 | update.setState(update.oldIntent(), WITHDRAWING); |
| 369 | } // else newIntent is FAILED |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 370 | update.addBatches(uninstallIntent(update.oldIntent(), update.oldInstallables())); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Uninstalls all installable intents associated with the given intent. |
| 375 | * |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 376 | * @param intent intent |
| 377 | * @param installables installable intents |
| 378 | * @return list of batches to uninstall intent |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 379 | */ |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 380 | private List<FlowRuleBatchOperation> uninstallIntent(Intent intent, List<Intent> installables) { |
| 381 | if (installables == null) { |
| 382 | return Collections.emptyList(); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 383 | } |
| 384 | List<FlowRuleBatchOperation> batches = Lists.newArrayList(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 385 | for (Intent installable : installables) { |
| 386 | trackerService.removeTrackedResources(intent.id(), |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 387 | installable.resources()); |
| 388 | try { |
| 389 | batches.addAll(getInstaller(installable).uninstall(installable)); |
| 390 | } catch (IntentException e) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 391 | log.warn("Unable to uninstall intent {} due to:", intent.id(), e); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 392 | // TODO: this should never happen. but what if it does? |
| 393 | } |
| 394 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 395 | return batches; |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /** |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 399 | * Withdraws the old intent and installs the new intent as one operation. |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 400 | * |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 401 | * @param update intent update |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 402 | */ |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 403 | private void executeReplacementPhase(IntentUpdate update) { |
| 404 | checkArgument(update.oldInstallables().size() == update.newInstallables().size(), |
| 405 | "Old and New Intent must have equivalent installable intents."); |
| 406 | if (!update.oldIntent().equals(update.newIntent())) { |
| 407 | // only set the old intent's state if it is different |
| 408 | update.setState(update.oldIntent(), WITHDRAWING); |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 409 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 410 | update.setState(update.newIntent(), INSTALLING); |
| 411 | |
| 412 | List<FlowRuleBatchOperation> batches = Lists.newArrayList(); |
| 413 | for (int i = 0; i < update.oldInstallables().size(); i++) { |
| 414 | Intent oldInstallable = update.oldInstallables().get(i); |
| 415 | Intent newInstallable = update.newInstallables().get(i); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 416 | //FIXME revisit this |
| 417 | // if (oldInstallable.equals(newInstallable)) { |
| 418 | // continue; |
| 419 | // } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 420 | checkArgument(oldInstallable.getClass().equals(newInstallable.getClass()), |
| 421 | "Installable Intent type mismatch."); |
| 422 | trackerService.removeTrackedResources(update.oldIntent().id(), oldInstallable.resources()); |
| 423 | trackerService.addTrackedResources(update.newIntent().id(), newInstallable.resources()); |
| 424 | try { |
| 425 | batches.addAll(getInstaller(newInstallable).replace(oldInstallable, newInstallable)); |
| 426 | } catch (IntentException e) { |
| 427 | log.warn("Unable to update intent {} due to:", update.oldIntent().id(), e); |
| 428 | //FIXME... we failed. need to uninstall (if same) or revert (if different) |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 429 | trackerService.removeTrackedResources(update.newIntent().id(), newInstallable.resources()); |
| 430 | update.setState(update.newIntent(), FAILED); |
| 431 | batches = uninstallIntent(update.oldIntent(), update.oldInstallables()); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 434 | update.addBatches(batches); |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /** |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 438 | * Registers an intent compiler of the specified intent if an intent compiler |
| 439 | * for the intent is not registered. This method traverses the class hierarchy of |
| 440 | * the intent. Once an intent compiler for a parent type is found, this method |
| 441 | * registers the found intent compiler. |
| 442 | * |
| 443 | * @param intent intent |
| 444 | */ |
| 445 | private void registerSubclassCompilerIfNeeded(Intent intent) { |
| 446 | if (!compilers.containsKey(intent.getClass())) { |
| 447 | Class<?> cls = intent.getClass(); |
| 448 | while (cls != Object.class) { |
| 449 | // As long as we're within the Intent class descendants |
| 450 | if (Intent.class.isAssignableFrom(cls)) { |
| 451 | IntentCompiler<?> compiler = compilers.get(cls); |
| 452 | if (compiler != null) { |
| 453 | compilers.put(intent.getClass(), compiler); |
| 454 | return; |
| 455 | } |
| 456 | } |
| 457 | cls = cls.getSuperclass(); |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Registers an intent installer of the specified intent if an intent installer |
| 464 | * for the intent is not registered. This method traverses the class hierarchy of |
| 465 | * the intent. Once an intent installer for a parent type is found, this method |
| 466 | * registers the found intent installer. |
| 467 | * |
| 468 | * @param intent intent |
| 469 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 470 | private void registerSubclassInstallerIfNeeded(Intent intent) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 471 | if (!installers.containsKey(intent.getClass())) { |
| 472 | Class<?> cls = intent.getClass(); |
| 473 | while (cls != Object.class) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 474 | // As long as we're within the Intent class descendants |
| 475 | if (Intent.class.isAssignableFrom(cls)) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 476 | IntentInstaller<?> installer = installers.get(cls); |
| 477 | if (installer != null) { |
| 478 | installers.put(intent.getClass(), installer); |
| 479 | return; |
| 480 | } |
| 481 | } |
| 482 | cls = cls.getSuperclass(); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 487 | // Store delegate to re-post events emitted from the store. |
| 488 | private class InternalStoreDelegate implements IntentStoreDelegate { |
| 489 | @Override |
| 490 | public void notify(IntentEvent event) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 491 | eventDispatcher.post(event); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 495 | private void buildAndSubmitBatches(Iterable<IntentId> intentIds, |
| 496 | boolean compileAllFailed) { |
| 497 | Map<ApplicationId, IntentOperations.Builder> batches = Maps.newHashMap(); |
| 498 | // Attempt recompilation of the specified intents first. |
| 499 | for (IntentId id : intentIds) { |
| 500 | Intent intent = store.getIntent(id); |
| 501 | if (intent == null) { |
| 502 | continue; |
| 503 | } |
| 504 | IntentOperations.Builder builder = batches.get(intent.appId()); |
| 505 | if (builder == null) { |
| 506 | builder = IntentOperations.builder(intent.appId()); |
| 507 | batches.put(intent.appId(), builder); |
| 508 | } |
| 509 | builder.addUpdateOperation(id); |
| 510 | } |
| 511 | |
| 512 | if (compileAllFailed) { |
| 513 | // If required, compile all currently failed intents. |
| 514 | for (Intent intent : getIntents()) { |
| 515 | if (getIntentState(intent.id()) == FAILED) { |
| 516 | IntentOperations.Builder builder = batches.get(intent.appId()); |
| 517 | if (builder == null) { |
| 518 | builder = IntentOperations.builder(intent.appId()); |
| 519 | batches.put(intent.appId(), builder); |
| 520 | } |
| 521 | builder.addUpdateOperation(intent.id()); |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | for (ApplicationId appId : batches.keySet()) { |
| 527 | if (batchService.isLocalLeader(appId)) { |
| 528 | execute(batches.get(appId).build()); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 533 | // Topology change delegate |
| 534 | private class InternalTopoChangeDelegate implements TopologyChangeDelegate { |
| 535 | @Override |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 536 | public void triggerCompile(Iterable<IntentId> intentIds, |
| 537 | boolean compileAllFailed) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 538 | buildAndSubmitBatches(intentIds, compileAllFailed); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 539 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 540 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 541 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 542 | /** |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 543 | * TODO. rename this... |
| 544 | * @param update intent update |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 545 | */ |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 546 | private void processIntentUpdate(IntentUpdate update) { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 547 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 548 | // check to see if the intent needs to be compiled or recompiled |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 549 | if (update.newIntent() != null) { |
| 550 | executeCompilingPhase(update); |
| 551 | } |
| 552 | |
| 553 | if (update.oldInstallables() != null && update.newInstallables() != null) { |
| 554 | executeReplacementPhase(update); |
| 555 | } else if (update.newInstallables() != null) { |
| 556 | executeInstallingPhase(update); |
| 557 | } else if (update.oldInstallables() != null) { |
| 558 | executeWithdrawingPhase(update); |
| 559 | } else { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 560 | if (update.oldIntent() != null && |
| 561 | !update.oldIntent().equals(update.newIntent())) { |
| 562 | // removing failed intent |
| 563 | update.setState(update.oldIntent(), WITHDRAWING); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 564 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 565 | // if (update.newIntent() != null) { |
| 566 | // // TODO assert that next state is failed |
| 567 | // } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 568 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | // TODO comments... |
| 572 | private class IntentUpdate { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 573 | private final Intent oldIntent; |
| 574 | private final Intent newIntent; |
| 575 | private final Map<Intent, IntentState> stateMap = Maps.newHashMap(); |
| 576 | |
| 577 | private final List<Intent> oldInstallables; |
| 578 | private List<Intent> newInstallables; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 579 | private final List<FlowRuleBatchOperation> batches = Lists.newLinkedList(); |
| 580 | private int currentBatch = 0; // TODO: maybe replace with an iterator |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 581 | |
| 582 | IntentUpdate(IntentOperation op) { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 583 | switch (op.type()) { |
| 584 | case SUBMIT: |
| 585 | newIntent = op.intent(); |
| 586 | oldIntent = null; |
| 587 | break; |
| 588 | case WITHDRAW: |
| 589 | newIntent = null; |
| 590 | oldIntent = store.getIntent(op.intentId()); |
| 591 | break; |
| 592 | case REPLACE: |
| 593 | newIntent = op.intent(); |
| 594 | oldIntent = store.getIntent(op.intentId()); |
| 595 | break; |
| 596 | case UPDATE: |
| 597 | oldIntent = store.getIntent(op.intentId()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 598 | newIntent = oldIntent; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 599 | break; |
| 600 | default: |
| 601 | oldIntent = null; |
| 602 | newIntent = null; |
| 603 | break; |
| 604 | } |
| 605 | // add new intent to store (if required) |
| 606 | if (newIntent != null) { |
| 607 | IntentEvent event = store.createIntent(newIntent); |
| 608 | if (event != null) { |
| 609 | eventDispatcher.post(event); |
| 610 | } |
| 611 | } |
| 612 | // fetch the old intent's installables from the store |
| 613 | if (oldIntent != null) { |
| 614 | oldInstallables = store.getInstallableIntents(oldIntent.id()); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 615 | } else { |
| 616 | oldInstallables = null; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | Intent oldIntent() { |
| 621 | return oldIntent; |
| 622 | } |
| 623 | |
| 624 | Intent newIntent() { |
| 625 | return newIntent; |
| 626 | } |
| 627 | |
| 628 | List<Intent> oldInstallables() { |
| 629 | return oldInstallables; |
| 630 | } |
| 631 | |
| 632 | List<Intent> newInstallables() { |
| 633 | return newInstallables; |
| 634 | } |
| 635 | |
| 636 | void setInstallables(List<Intent> installables) { |
| 637 | newInstallables = installables; |
| 638 | store.setInstallableIntents(newIntent.id(), installables); |
| 639 | } |
| 640 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 641 | boolean isComplete() { |
| 642 | return currentBatch >= batches.size(); |
| 643 | } |
| 644 | |
| 645 | FlowRuleBatchOperation currentBatch() { |
| 646 | return !isComplete() ? batches.get(currentBatch) : null; |
| 647 | } |
| 648 | |
| 649 | void incrementBatch(boolean success) { |
| 650 | if (success) { // actually increment |
| 651 | if (++currentBatch == batches.size()) { |
| 652 | finalizeStates(); |
| 653 | } |
| 654 | } else { // the current batch has failed, so recompile |
| 655 | // remove the current batch and all remaining |
| 656 | for (int i = currentBatch; i < batches.size(); i++) { |
| 657 | batches.remove(i); |
| 658 | } |
| 659 | if (oldIntent != null) { |
| 660 | executeWithdrawingPhase(this); // remove the old intent |
| 661 | } |
| 662 | if (newIntent != null) { |
| 663 | setState(newIntent, FAILED); |
| 664 | batches.addAll(uninstallIntent(newIntent, newInstallables())); |
| 665 | } |
| 666 | |
| 667 | // FIXME: should we try to recompile? |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | // FIXME make sure this is called!!! |
| 672 | private void finalizeStates() { |
| 673 | for (Intent intent : stateMap.keySet()) { |
| 674 | switch (getState(intent)) { |
| 675 | case INSTALLING: |
| 676 | setState(intent, INSTALLED); |
| 677 | break; |
| 678 | case WITHDRAWING: |
| 679 | setState(intent, WITHDRAWN); |
| 680 | store.removeInstalledIntents(intent.id()); |
| 681 | //store.removeIntent(intent.id()); // FIXME we die a horrible death here |
| 682 | break; |
| 683 | case FAILED: |
| 684 | store.removeInstalledIntents(intent.id()); |
| 685 | break; |
| 686 | |
| 687 | // FALLTHROUGH to default from here |
| 688 | case SUBMITTED: |
| 689 | case COMPILING: |
| 690 | case RECOMPILING: |
| 691 | case WITHDRAWN: |
| 692 | case INSTALLED: |
| 693 | default: |
| 694 | //FIXME clean this up (we shouldn't ever get here) |
| 695 | log.warn("Bad state: {} for {}", getState(intent), intent); |
| 696 | break; |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 701 | List<FlowRuleBatchOperation> batches() { |
| 702 | return batches; |
| 703 | } |
| 704 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 705 | void addBatches(List<FlowRuleBatchOperation> batches) { |
| 706 | this.batches.addAll(batches); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | IntentState getState(Intent intent) { |
| 710 | return stateMap.get(intent); |
| 711 | } |
| 712 | |
| 713 | void setState(Intent intent, IntentState newState) { |
| 714 | // TODO: clean this up, or set to debug |
| 715 | IntentState oldState = stateMap.get(intent); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 716 | log.debug("intent id: {}, old state: {}, new state: {}", |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 717 | intent.id(), oldState, newState); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 718 | |
| 719 | stateMap.put(intent, newState); |
| 720 | IntentEvent event = store.setState(intent, newState); |
| 721 | if (event != null) { |
| 722 | eventDispatcher.post(event); |
| 723 | } |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 724 | |
| 725 | if (newState == WITHDRAWN) { |
| 726 | store.removeIntent(intent.id()); |
| 727 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | Map<Intent, IntentState> stateMap() { |
| 731 | return stateMap; |
| 732 | } |
| 733 | } |
| 734 | |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 735 | private class IntentInstallMonitor implements Runnable { |
| 736 | |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 737 | // TODO make this configurable |
| 738 | private static final int TIMEOUT_PER_OP = 500; // ms |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 739 | private static final int MAX_ATTEMPTS = 3; |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 740 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 741 | private final IntentOperations ops; |
| 742 | private final List<IntentUpdate> intentUpdates = Lists.newArrayList(); |
| 743 | |
| 744 | private Future<CompletedBatchOperation> future; |
| 745 | private long startTime = System.currentTimeMillis(); |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 746 | private long endTime; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 747 | private int installAttempt; |
| 748 | |
| 749 | public IntentInstallMonitor(IntentOperations ops) { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 750 | this.ops = ops; |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 751 | resetTimeoutLimit(); |
| 752 | } |
| 753 | |
| 754 | private void resetTimeoutLimit() { |
| 755 | // FIXME compute reasonable timeouts |
| 756 | this.endTime = System.currentTimeMillis() |
| 757 | + ops.operations().size() * TIMEOUT_PER_OP; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | private void buildIntentUpdates() { |
| 761 | for (IntentOperation op : ops.operations()) { |
| 762 | IntentUpdate update = new IntentUpdate(op); |
| 763 | intentUpdates.add(update); |
| 764 | processIntentUpdate(update); |
| 765 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 766 | future = applyNextBatch(); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 769 | /** |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 770 | * Builds and applies the next batch, and returns the future. |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 771 | * |
| 772 | * @return Future for next batch |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 773 | */ |
| 774 | private Future<CompletedBatchOperation> applyNextBatch() { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 775 | //TODO test this. (also, maybe save this batch) |
| 776 | FlowRuleBatchOperation batch = new FlowRuleBatchOperation(Collections.emptyList()); |
| 777 | for (IntentUpdate update : intentUpdates) { |
| 778 | if (!update.isComplete()) { |
| 779 | batch.addAll(update.currentBatch()); |
| 780 | } |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 781 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 782 | return (batch.size() > 0) ? flowRuleService.applyBatch(batch) : null; |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 785 | private void updateBatches(CompletedBatchOperation completed) { |
| 786 | if (completed.isSuccess()) { |
| 787 | for (IntentUpdate update : intentUpdates) { |
| 788 | update.incrementBatch(true); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 789 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 790 | } else { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 791 | // entire batch has been reverted... |
| 792 | log.warn("Failed items: {}", completed.failedItems()); |
| 793 | |
| 794 | for (Long id : completed.failedIds()) { |
| 795 | IntentId targetId = IntentId.valueOf(id); |
| 796 | for (IntentUpdate update : intentUpdates) { |
| 797 | List<Intent> installables = Lists.newArrayList(update.newInstallables()); |
| 798 | installables.addAll(update.oldInstallables()); |
| 799 | for (Intent intent : installables) { |
| 800 | if (intent.id().equals(targetId)) { |
| 801 | update.incrementBatch(false); |
| 802 | break; |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | // don't increment the non-failed items, as they have been reverted. |
| 807 | } |
| 808 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /** |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 812 | * Iterate through the pending futures, and remove them when they have completed. |
| 813 | */ |
| 814 | private void processFutures() { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 815 | if (future == null) { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 816 | log.warn("I have no Future."); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 817 | return; //FIXME look at this |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 818 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 819 | try { |
| 820 | CompletedBatchOperation completed = future.get(100, TimeUnit.NANOSECONDS); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 821 | updateBatches(completed); |
| 822 | future = applyNextBatch(); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 823 | } catch (TimeoutException | InterruptedException | ExecutionException te) { |
| 824 | //TODO look into error message |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 825 | log.debug("Installation of intents are still pending: {}", ops); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 826 | } |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 829 | private void retry() { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 830 | log.debug("Execution timed out, retrying."); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 831 | if (future.cancel(true)) { // cancel success; batch is reverted |
| 832 | // reset the timer |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 833 | resetTimeoutLimit(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 834 | if (installAttempt++ >= MAX_ATTEMPTS) { |
| 835 | log.warn("Install request timed out: {}", ops); |
| 836 | for (IntentUpdate update : intentUpdates) { |
| 837 | update.incrementBatch(false); |
| 838 | } |
| 839 | } // else just resubmit the work |
| 840 | future = applyNextBatch(); |
| 841 | monitorExecutor.submit(this); |
| 842 | } else { |
| 843 | // FIXME |
| 844 | // cancel failed... batch is broken; shouldn't happen! |
| 845 | // we could manually reverse everything |
| 846 | // ... or just core dump and send email to Ali |
| 847 | batchService.removeIntentOperations(ops); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | boolean isComplete() { |
| 852 | // TODO: actually check with the intent update? |
| 853 | return future == null; |
| 854 | } |
| 855 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 856 | @Override |
| 857 | public void run() { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 858 | try { |
| 859 | if (intentUpdates.isEmpty()) { |
| 860 | // this should only be called on the first iteration |
| 861 | // note: this a "expensive", so it is not done in the constructor |
| 862 | buildIntentUpdates(); |
| 863 | } |
| 864 | processFutures(); |
| 865 | if (isComplete()) { |
| 866 | // there are no outstanding batches; we are done |
| 867 | batchService.removeIntentOperations(ops); |
| 868 | } else if (endTime < System.currentTimeMillis()) { |
| 869 | retry(); |
| 870 | } else { |
| 871 | // we are not done yet, yield the thread by resubmitting ourselves |
| 872 | monitorExecutor.submit(this); |
| 873 | } |
| 874 | } catch (Exception e) { |
| 875 | log.error("Error submitting batches:", e); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 879 | |
| 880 | private class InternalBatchDelegate implements IntentBatchDelegate { |
| 881 | @Override |
| 882 | public void execute(IntentOperations operations) { |
Yuta HIGUCHI | fe4367a | 2014-11-24 19:19:09 -0800 | [diff] [blame] | 883 | log.info("Execute {} operation(s).", operations.operations().size()); |
| 884 | log.debug("Execute operations: {}", operations.operations()); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 885 | //FIXME: perhaps we want to track this task so that we can cancel it. |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 886 | monitorExecutor.execute(new IntentInstallMonitor(operations)); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | @Override |
| 890 | public void cancel(IntentOperations operations) { |
| 891 | //FIXME: implement this |
| 892 | log.warn("NOT IMPLEMENTED -- Cancel operations: {}", operations); |
| 893 | } |
| 894 | } |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 895 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 896 | } |