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 | |
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 | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 28 | import org.onlab.onos.core.ApplicationId; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 29 | import org.onlab.onos.core.CoreService; |
| 30 | import org.onlab.onos.core.IdGenerator; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 31 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 32 | import org.onlab.onos.event.EventDeliveryService; |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
| 35 | import org.onlab.onos.net.flow.FlowRuleService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.intent.Intent; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.intent.IntentBatchDelegate; |
| 38 | import org.onlab.onos.net.intent.IntentBatchService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.intent.IntentCompiler; |
| 40 | import org.onlab.onos.net.intent.IntentEvent; |
| 41 | import org.onlab.onos.net.intent.IntentException; |
| 42 | import org.onlab.onos.net.intent.IntentExtensionService; |
| 43 | import org.onlab.onos.net.intent.IntentId; |
| 44 | import org.onlab.onos.net.intent.IntentInstaller; |
| 45 | import org.onlab.onos.net.intent.IntentListener; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 46 | import org.onlab.onos.net.intent.IntentOperation; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 47 | import org.onlab.onos.net.intent.IntentOperations; |
| 48 | import org.onlab.onos.net.intent.IntentService; |
| 49 | import org.onlab.onos.net.intent.IntentState; |
| 50 | import org.onlab.onos.net.intent.IntentStore; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 51 | import org.onlab.onos.net.intent.IntentStore.BatchWrite; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 52 | import org.onlab.onos.net.intent.IntentStoreDelegate; |
| 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; |
| 71 | import static org.onlab.onos.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); |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 136 | executor = newFixedThreadPool(NUM_THREADS, namedThreads("onos-intent")); |
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); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 303 | } catch (IntentException e) { |
Jonathan Hart | 1109640 | 2014-10-20 17:31:49 -0700 | [diff] [blame] | 304 | log.warn("Unable to compile intent {} due to:", intent.id(), e); |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 305 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 306 | // If compilation failed, mark the intent as failed. |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 307 | update.setInflightState(intent, FAILED); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 311 | /** |
| 312 | * Compiles an intent recursively. |
| 313 | * |
| 314 | * @param intent intent |
| 315 | * @return result of compilation |
| 316 | */ |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 317 | private List<Intent> compileIntent(Intent intent, IntentUpdate update) { |
Thomas Vachuska | 4926c1b | 2014-10-21 00:44:10 -0700 | [diff] [blame] | 318 | if (intent.isInstallable()) { |
| 319 | return ImmutableList.of(intent); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 320 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 321 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 322 | registerSubclassCompilerIfNeeded(intent); |
| 323 | List<Intent> previous = update.oldInstallables(); |
| 324 | // FIXME: get previous resources |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 325 | List<Intent> installable = new ArrayList<>(); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 326 | for (Intent compiled : getCompiler(intent).compile(intent, previous, null)) { |
| 327 | installable.addAll(compileIntent(compiled, update)); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 328 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 329 | return installable; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 333 | * Installs all installable intents associated with the specified top-level |
| 334 | * intent. |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 335 | * |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 336 | * @param update intent update |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 337 | */ |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 338 | private void executeInstallingPhase(IntentUpdate update) { |
| 339 | if (update.newInstallables() == null) { |
| 340 | //no failed intents allowed past this point... |
| 341 | return; |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 342 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 343 | // Indicate that the intent is entering the installing phase. |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 344 | update.setInflightState(update.newIntent(), INSTALLING); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 345 | |
| 346 | List<FlowRuleBatchOperation> batches = Lists.newArrayList(); |
| 347 | for (Intent installable : update.newInstallables()) { |
| 348 | registerSubclassInstallerIfNeeded(installable); |
| 349 | trackerService.addTrackedResources(update.newIntent().id(), |
| 350 | installable.resources()); |
| 351 | try { |
| 352 | batches.addAll(getInstaller(installable).install(installable)); |
| 353 | } catch (IntentException e) { |
| 354 | 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] | 355 | trackerService.removeTrackedResources(update.newIntent().id(), |
| 356 | installable.resources()); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 357 | //FIXME we failed... intent should be recompiled |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 360 | update.addBatches(batches); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Uninstalls the specified intent by uninstalling all of its associated |
| 365 | * installable derivatives. |
| 366 | * |
| 367 | * @param update intent update |
| 368 | */ |
| 369 | private void executeWithdrawingPhase(IntentUpdate update) { |
| 370 | if (!update.oldIntent().equals(update.newIntent())) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 371 | update.setInflightState(update.oldIntent(), WITHDRAWING); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 372 | } // else newIntent is FAILED |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 373 | update.addBatches(uninstallIntent(update.oldIntent(), update.oldInstallables())); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Uninstalls all installable intents associated with the given intent. |
| 378 | * |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 379 | * @param intent intent |
| 380 | * @param installables installable intents |
| 381 | * @return list of batches to uninstall intent |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 382 | */ |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 383 | private List<FlowRuleBatchOperation> uninstallIntent(Intent intent, List<Intent> installables) { |
| 384 | if (installables == null) { |
| 385 | return Collections.emptyList(); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 386 | } |
| 387 | List<FlowRuleBatchOperation> batches = Lists.newArrayList(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 388 | for (Intent installable : installables) { |
| 389 | trackerService.removeTrackedResources(intent.id(), |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 390 | installable.resources()); |
| 391 | try { |
| 392 | batches.addAll(getInstaller(installable).uninstall(installable)); |
| 393 | } catch (IntentException e) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 394 | log.warn("Unable to uninstall intent {} due to:", intent.id(), e); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 395 | // TODO: this should never happen. but what if it does? |
| 396 | } |
| 397 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 398 | return batches; |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /** |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 402 | * Withdraws the old intent and installs the new intent as one operation. |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 403 | * |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 404 | * @param update intent update |
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 | private void executeReplacementPhase(IntentUpdate update) { |
| 407 | checkArgument(update.oldInstallables().size() == update.newInstallables().size(), |
| 408 | "Old and New Intent must have equivalent installable intents."); |
| 409 | if (!update.oldIntent().equals(update.newIntent())) { |
| 410 | // only set the old intent's state if it is different |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 411 | update.setInflightState(update.oldIntent(), WITHDRAWING); |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 412 | } |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 413 | update.setInflightState(update.newIntent(), INSTALLING); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 414 | |
| 415 | List<FlowRuleBatchOperation> batches = Lists.newArrayList(); |
| 416 | for (int i = 0; i < update.oldInstallables().size(); i++) { |
| 417 | Intent oldInstallable = update.oldInstallables().get(i); |
| 418 | Intent newInstallable = update.newInstallables().get(i); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 419 | //FIXME revisit this |
| 420 | // if (oldInstallable.equals(newInstallable)) { |
| 421 | // continue; |
| 422 | // } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 423 | checkArgument(oldInstallable.getClass().equals(newInstallable.getClass()), |
| 424 | "Installable Intent type mismatch."); |
| 425 | trackerService.removeTrackedResources(update.oldIntent().id(), oldInstallable.resources()); |
| 426 | trackerService.addTrackedResources(update.newIntent().id(), newInstallable.resources()); |
| 427 | try { |
| 428 | batches.addAll(getInstaller(newInstallable).replace(oldInstallable, newInstallable)); |
| 429 | } catch (IntentException e) { |
| 430 | log.warn("Unable to update intent {} due to:", update.oldIntent().id(), e); |
| 431 | //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] | 432 | trackerService.removeTrackedResources(update.newIntent().id(), newInstallable.resources()); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 433 | update.setInflightState(update.newIntent(), FAILED); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 434 | batches = uninstallIntent(update.oldIntent(), update.oldInstallables()); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 437 | update.addBatches(batches); |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /** |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 441 | * Registers an intent compiler of the specified intent if an intent compiler |
| 442 | * for the intent is not registered. This method traverses the class hierarchy of |
| 443 | * the intent. Once an intent compiler for a parent type is found, this method |
| 444 | * registers the found intent compiler. |
| 445 | * |
| 446 | * @param intent intent |
| 447 | */ |
| 448 | private void registerSubclassCompilerIfNeeded(Intent intent) { |
| 449 | if (!compilers.containsKey(intent.getClass())) { |
| 450 | Class<?> cls = intent.getClass(); |
| 451 | while (cls != Object.class) { |
| 452 | // As long as we're within the Intent class descendants |
| 453 | if (Intent.class.isAssignableFrom(cls)) { |
| 454 | IntentCompiler<?> compiler = compilers.get(cls); |
| 455 | if (compiler != null) { |
| 456 | compilers.put(intent.getClass(), compiler); |
| 457 | return; |
| 458 | } |
| 459 | } |
| 460 | cls = cls.getSuperclass(); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Registers an intent installer of the specified intent if an intent installer |
| 467 | * for the intent is not registered. This method traverses the class hierarchy of |
| 468 | * the intent. Once an intent installer for a parent type is found, this method |
| 469 | * registers the found intent installer. |
| 470 | * |
| 471 | * @param intent intent |
| 472 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 473 | private void registerSubclassInstallerIfNeeded(Intent intent) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 474 | if (!installers.containsKey(intent.getClass())) { |
| 475 | Class<?> cls = intent.getClass(); |
| 476 | while (cls != Object.class) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 477 | // As long as we're within the Intent class descendants |
| 478 | if (Intent.class.isAssignableFrom(cls)) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 479 | IntentInstaller<?> installer = installers.get(cls); |
| 480 | if (installer != null) { |
| 481 | installers.put(intent.getClass(), installer); |
| 482 | return; |
| 483 | } |
| 484 | } |
| 485 | cls = cls.getSuperclass(); |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 490 | // Store delegate to re-post events emitted from the store. |
| 491 | private class InternalStoreDelegate implements IntentStoreDelegate { |
| 492 | @Override |
| 493 | public void notify(IntentEvent event) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 494 | eventDispatcher.post(event); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 498 | private void buildAndSubmitBatches(Iterable<IntentId> intentIds, |
| 499 | boolean compileAllFailed) { |
| 500 | Map<ApplicationId, IntentOperations.Builder> batches = Maps.newHashMap(); |
| 501 | // Attempt recompilation of the specified intents first. |
| 502 | for (IntentId id : intentIds) { |
| 503 | Intent intent = store.getIntent(id); |
| 504 | if (intent == null) { |
| 505 | continue; |
| 506 | } |
| 507 | IntentOperations.Builder builder = batches.get(intent.appId()); |
| 508 | if (builder == null) { |
| 509 | builder = IntentOperations.builder(intent.appId()); |
| 510 | batches.put(intent.appId(), builder); |
| 511 | } |
| 512 | builder.addUpdateOperation(id); |
| 513 | } |
| 514 | |
| 515 | if (compileAllFailed) { |
| 516 | // If required, compile all currently failed intents. |
| 517 | for (Intent intent : getIntents()) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame^] | 518 | IntentState state = getIntentState(intent.id()); |
| 519 | if (RECOMPILE.contains(state)) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 520 | IntentOperations.Builder builder = batches.get(intent.appId()); |
| 521 | if (builder == null) { |
| 522 | builder = IntentOperations.builder(intent.appId()); |
| 523 | batches.put(intent.appId(), builder); |
| 524 | } |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame^] | 525 | if (state == WITHDRAW_REQ) { |
| 526 | builder.addWithdrawOperation(intent.id()); |
| 527 | } else { |
| 528 | builder.addUpdateOperation(intent.id()); |
| 529 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | for (ApplicationId appId : batches.keySet()) { |
| 535 | if (batchService.isLocalLeader(appId)) { |
| 536 | execute(batches.get(appId).build()); |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 541 | // Topology change delegate |
| 542 | private class InternalTopoChangeDelegate implements TopologyChangeDelegate { |
| 543 | @Override |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 544 | public void triggerCompile(Iterable<IntentId> intentIds, |
| 545 | boolean compileAllFailed) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 546 | buildAndSubmitBatches(intentIds, compileAllFailed); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 547 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 548 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 549 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 550 | // TODO move this inside IntentUpdate? |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 551 | /** |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 552 | * TODO. rename this... |
| 553 | * @param update intent update |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 554 | */ |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 555 | private void processIntentUpdate(IntentUpdate 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 | // 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] | 558 | if (update.newIntent() != null) { |
| 559 | executeCompilingPhase(update); |
| 560 | } |
| 561 | |
| 562 | if (update.oldInstallables() != null && update.newInstallables() != null) { |
| 563 | executeReplacementPhase(update); |
| 564 | } else if (update.newInstallables() != null) { |
| 565 | executeInstallingPhase(update); |
| 566 | } else if (update.oldInstallables() != null) { |
| 567 | executeWithdrawingPhase(update); |
| 568 | } else { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 569 | if (update.oldIntent() != null && |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 570 | !update.oldIntent().equals(update.newIntent())) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 571 | // removing failed intent |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 572 | update.setInflightState(update.oldIntent(), WITHDRAWING); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 573 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 574 | // if (update.newIntent() != null) { |
| 575 | // // TODO assert that next state is failed |
| 576 | // } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 577 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | // TODO comments... |
| 581 | private class IntentUpdate { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 582 | private final Intent oldIntent; |
| 583 | private final Intent newIntent; |
| 584 | private final Map<Intent, IntentState> stateMap = Maps.newHashMap(); |
| 585 | |
| 586 | private final List<Intent> oldInstallables; |
| 587 | private List<Intent> newInstallables; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 588 | private final List<FlowRuleBatchOperation> batches = Lists.newLinkedList(); |
| 589 | private int currentBatch = 0; // TODO: maybe replace with an iterator |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 590 | |
| 591 | IntentUpdate(IntentOperation op) { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 592 | switch (op.type()) { |
| 593 | case SUBMIT: |
| 594 | newIntent = op.intent(); |
| 595 | oldIntent = null; |
| 596 | break; |
| 597 | case WITHDRAW: |
| 598 | newIntent = null; |
| 599 | oldIntent = store.getIntent(op.intentId()); |
| 600 | break; |
| 601 | case REPLACE: |
| 602 | newIntent = op.intent(); |
| 603 | oldIntent = store.getIntent(op.intentId()); |
| 604 | break; |
| 605 | case UPDATE: |
| 606 | oldIntent = store.getIntent(op.intentId()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 607 | newIntent = oldIntent; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 608 | break; |
| 609 | default: |
| 610 | oldIntent = null; |
| 611 | newIntent = null; |
| 612 | break; |
| 613 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 614 | // fetch the old intent's installables from the store |
| 615 | if (oldIntent != null) { |
| 616 | oldInstallables = store.getInstallableIntents(oldIntent.id()); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 617 | } else { |
| 618 | oldInstallables = null; |
Brian O'Connor | 5d55ed4 | 2014-12-01 18:27:47 -0800 | [diff] [blame] | 619 | if (newIntent == null) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame^] | 620 | log.info("Ignoring {} for missing Intent {}", op.type(), op.intentId()); |
Brian O'Connor | 5d55ed4 | 2014-12-01 18:27:47 -0800 | [diff] [blame] | 621 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 625 | void init(BatchWrite batchWrite) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 626 | if (newIntent != null) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame^] | 627 | // TODO consider only "creating" intent if it does not exist |
| 628 | // Note: We need to set state to INSTALL_REQ regardless. |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 629 | batchWrite.createIntent(newIntent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame^] | 630 | } else if (oldIntent != null && !oldIntent.equals(newIntent)) { |
| 631 | batchWrite.setState(oldIntent, WITHDRAW_REQ); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 635 | Intent oldIntent() { |
| 636 | return oldIntent; |
| 637 | } |
| 638 | |
| 639 | Intent newIntent() { |
| 640 | return newIntent; |
| 641 | } |
| 642 | |
| 643 | List<Intent> oldInstallables() { |
| 644 | return oldInstallables; |
| 645 | } |
| 646 | |
| 647 | List<Intent> newInstallables() { |
| 648 | return newInstallables; |
| 649 | } |
| 650 | |
| 651 | void setInstallables(List<Intent> installables) { |
| 652 | newInstallables = installables; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 655 | boolean isComplete() { |
| 656 | return currentBatch >= batches.size(); |
| 657 | } |
| 658 | |
| 659 | FlowRuleBatchOperation currentBatch() { |
| 660 | return !isComplete() ? batches.get(currentBatch) : null; |
| 661 | } |
| 662 | |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 663 | void batchSuccess(BatchWrite batchWrite) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 664 | // move on to next Batch |
| 665 | if (++currentBatch == batches.size()) { |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 666 | finalizeStates(batchWrite); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 667 | } |
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 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame^] | 687 | // make sure this is called!!! |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 688 | private void finalizeStates(BatchWrite batchWrite) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 689 | // events to be triggered on successful write |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 690 | for (Intent intent : stateMap.keySet()) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 691 | switch (getInflightState(intent)) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 692 | case INSTALLING: |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 693 | batchWrite.setState(intent, INSTALLED); |
| 694 | batchWrite.setInstallableIntents(newIntent.id(), newInstallables); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 695 | break; |
| 696 | case WITHDRAWING: |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 697 | batchWrite.setState(intent, WITHDRAWN); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 698 | batchWrite.removeInstalledIntents(intent.id()); |
| 699 | batchWrite.removeIntent(intent.id()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 700 | break; |
| 701 | case 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() { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 771 | BatchWrite batchWrite = store.newBatchWrite(); |
| 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 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 804 | return (batch.size() > 0) ? flowRuleService.applyBatch(batch) : null; |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 807 | private void updateBatches(CompletedBatchOperation completed) { |
| 808 | if (completed.isSuccess()) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 809 | BatchWrite batchWrite = store.newBatchWrite(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 810 | for (IntentUpdate update : intentUpdates) { |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 811 | update.batchSuccess(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 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 816 | } else { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 817 | // entire batch has been reverted... |
| 818 | log.warn("Failed items: {}", completed.failedItems()); |
| 819 | |
| 820 | for (Long id : completed.failedIds()) { |
| 821 | IntentId targetId = IntentId.valueOf(id); |
| 822 | for (IntentUpdate update : intentUpdates) { |
| 823 | List<Intent> installables = Lists.newArrayList(update.newInstallables()); |
| 824 | installables.addAll(update.oldInstallables()); |
| 825 | for (Intent intent : installables) { |
| 826 | if (intent.id().equals(targetId)) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 827 | update.batchFailed(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 828 | break; |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | // don't increment the non-failed items, as they have been reverted. |
| 833 | } |
| 834 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame^] | 837 | private void abandonShip() { |
| 838 | // the batch has failed |
| 839 | // TODO: maybe we should do more? |
| 840 | future = null; |
| 841 | log.error("Walk the plank, matey..."); |
| 842 | } |
| 843 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 844 | /** |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 845 | * Iterate through the pending futures, and remove them when they have completed. |
| 846 | */ |
| 847 | private void processFutures() { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 848 | if (future == null) { |
Brian O'Connor | 5d55ed4 | 2014-12-01 18:27:47 -0800 | [diff] [blame] | 849 | // we are done if the future is null |
| 850 | return; |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 851 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 852 | try { |
| 853 | CompletedBatchOperation completed = future.get(100, TimeUnit.NANOSECONDS); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 854 | updateBatches(completed); |
| 855 | future = applyNextBatch(); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame^] | 856 | } catch (TimeoutException | InterruptedException te) { |
| 857 | log.trace("Installation of intents are still pending: {}", ops); |
| 858 | } catch (ExecutionException e) { |
| 859 | log.warn("Execution of batch failed: {}", ops, e); |
| 860 | abandonShip(); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 861 | } |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 864 | private void retry() { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 865 | log.debug("Execution timed out, retrying."); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 866 | if (future.cancel(true)) { // cancel success; batch is reverted |
| 867 | // reset the timer |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 868 | resetTimeoutLimit(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 869 | if (installAttempt++ >= MAX_ATTEMPTS) { |
| 870 | log.warn("Install request timed out: {}", ops); |
| 871 | for (IntentUpdate update : intentUpdates) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 872 | update.batchFailed(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 873 | } |
| 874 | } // else just resubmit the work |
| 875 | future = applyNextBatch(); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 876 | executor.submit(this); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 877 | } else { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 878 | log.error("Cancelling FlowRuleBatch failed."); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 879 | // FIXME |
| 880 | // cancel failed... batch is broken; shouldn't happen! |
| 881 | // we could manually reverse everything |
| 882 | // ... or just core dump and send email to Ali |
| 883 | batchService.removeIntentOperations(ops); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | boolean isComplete() { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 888 | return future == null; |
| 889 | } |
| 890 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 891 | @Override |
| 892 | public void run() { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 893 | try { |
| 894 | if (intentUpdates.isEmpty()) { |
| 895 | // this should only be called on the first iteration |
| 896 | // note: this a "expensive", so it is not done in the constructor |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 897 | |
| 898 | // - creates per Intent installation context (IntentUpdate) |
| 899 | // - write Intents to store |
| 900 | // - process (compile, install, etc.) each Intents |
| 901 | // - generate FlowRuleBatch for this phase |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 902 | buildIntentUpdates(); |
| 903 | } |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 904 | |
| 905 | // - peek if current FlowRuleBatch is complete |
| 906 | // -- If complete OK: |
| 907 | // step each IntentUpdate forward |
| 908 | // If phase left: generate next FlowRuleBatch |
| 909 | // If no more phase: write parking states |
| 910 | // -- If complete FAIL: |
| 911 | // Intent which failed: transition Intent to FAILED |
| 912 | // Other Intents: resubmit same FlowRuleBatch for this phase |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 913 | processFutures(); |
| 914 | if (isComplete()) { |
| 915 | // there are no outstanding batches; we are done |
| 916 | batchService.removeIntentOperations(ops); |
| 917 | } else if (endTime < System.currentTimeMillis()) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 918 | // - cancel current FlowRuleBatch and resubmit again |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 919 | retry(); |
| 920 | } else { |
| 921 | // we are not done yet, yield the thread by resubmitting ourselves |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 922 | executor.submit(this); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 923 | } |
| 924 | } catch (Exception e) { |
| 925 | log.error("Error submitting batches:", e); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 926 | // FIXME incomplete Intents should be cleaned up |
| 927 | // (transition to FAILED, etc.) |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 931 | |
| 932 | private class InternalBatchDelegate implements IntentBatchDelegate { |
| 933 | @Override |
| 934 | public void execute(IntentOperations operations) { |
Yuta HIGUCHI | fe4367a | 2014-11-24 19:19:09 -0800 | [diff] [blame] | 935 | log.info("Execute {} operation(s).", operations.operations().size()); |
| 936 | log.debug("Execute operations: {}", operations.operations()); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 937 | //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] | 938 | executor.execute(new IntentInstallMonitor(operations)); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | @Override |
| 942 | public void cancel(IntentOperations operations) { |
| 943 | //FIXME: implement this |
| 944 | log.warn("NOT IMPLEMENTED -- Cancel operations: {}", operations); |
| 945 | } |
| 946 | } |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 947 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 948 | } |