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