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