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