Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import com.google.common.collect.Maps; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreService; |
| 30 | import org.onosproject.core.IdGenerator; |
| 31 | import org.onosproject.event.AbstractListenerRegistry; |
| 32 | import org.onosproject.event.EventDeliveryService; |
| 33 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 34 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 35 | import org.onosproject.net.flow.FlowRuleService; |
| 36 | import org.onosproject.net.intent.Intent; |
| 37 | import org.onosproject.net.intent.IntentBatchDelegate; |
| 38 | import org.onosproject.net.intent.IntentBatchService; |
| 39 | import org.onosproject.net.intent.IntentCompiler; |
| 40 | import org.onosproject.net.intent.IntentEvent; |
| 41 | import org.onosproject.net.intent.IntentException; |
| 42 | import org.onosproject.net.intent.IntentExtensionService; |
| 43 | import org.onosproject.net.intent.IntentId; |
| 44 | import org.onosproject.net.intent.IntentInstaller; |
| 45 | import org.onosproject.net.intent.IntentListener; |
| 46 | import org.onosproject.net.intent.IntentOperation; |
| 47 | import org.onosproject.net.intent.IntentOperations; |
| 48 | import org.onosproject.net.intent.IntentService; |
| 49 | import org.onosproject.net.intent.IntentState; |
| 50 | import org.onosproject.net.intent.IntentStore; |
Sho SHIMIZU | 64ae11c | 2014-12-03 15:17:47 -0800 | [diff] [blame] | 51 | import org.onosproject.net.intent.BatchWrite; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 52 | import org.onosproject.net.intent.IntentStoreDelegate; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 55 | import java.util.ArrayList; |
| 56 | import java.util.Collections; |
| 57 | import java.util.EnumSet; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 58 | import java.util.LinkedList; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 59 | import java.util.List; |
| 60 | import java.util.Map; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 61 | import java.util.Optional; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 62 | import java.util.concurrent.ConcurrentHashMap; |
| 63 | import java.util.concurrent.ConcurrentMap; |
| 64 | import java.util.concurrent.ExecutionException; |
| 65 | import java.util.concurrent.ExecutorService; |
| 66 | import java.util.concurrent.Future; |
| 67 | import java.util.concurrent.TimeUnit; |
| 68 | import java.util.concurrent.TimeoutException; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 69 | import java.util.stream.Collectors; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 70 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 71 | import static com.google.common.base.Preconditions.checkNotNull; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 72 | import static com.google.common.base.Preconditions.checkState; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 73 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 74 | import static org.onosproject.net.intent.IntentState.*; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 75 | import static org.onlab.util.Tools.namedThreads; |
| 76 | import static org.slf4j.LoggerFactory.getLogger; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * An implementation of Intent Manager. |
| 80 | */ |
| 81 | @Component(immediate = true) |
| 82 | @Service |
| 83 | public class IntentManager |
| 84 | implements IntentService, IntentExtensionService { |
Sho SHIMIZU | 8b5051d | 2014-11-05 11:24:13 -0800 | [diff] [blame] | 85 | private static final Logger log = getLogger(IntentManager.class); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 86 | |
| 87 | public static final String INTENT_NULL = "Intent cannot be null"; |
| 88 | public static final String INTENT_ID_NULL = "Intent ID cannot be null"; |
| 89 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 90 | private static final int NUM_THREADS = 12; |
| 91 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 92 | private static final EnumSet<IntentState> RECOMPILE |
| 93 | = EnumSet.of(INSTALL_REQ, FAILED, WITHDRAW_REQ); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 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()) |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 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()) |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 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 | /** |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 286 | * Compiles an intent recursively. |
| 287 | * |
| 288 | * @param intent intent |
| 289 | * @return result of compilation |
| 290 | */ |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 291 | private List<Intent> compileIntent(Intent intent, List<Intent> previousInstallables) { |
Thomas Vachuska | 4926c1b | 2014-10-21 00:44:10 -0700 | [diff] [blame] | 292 | if (intent.isInstallable()) { |
| 293 | return ImmutableList.of(intent); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 294 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 295 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 296 | registerSubclassCompilerIfNeeded(intent); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 297 | // FIXME: get previous resources |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 298 | List<Intent> installable = new ArrayList<>(); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 299 | for (Intent compiled : getCompiler(intent).compile(intent, previousInstallables, null)) { |
| 300 | installable.addAll(compileIntent(compiled, previousInstallables)); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 301 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 302 | return installable; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /** |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 306 | * Uninstalls all installable intents associated with the given intent. |
| 307 | * |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 308 | * @param intent intent |
| 309 | * @param installables installable intents |
| 310 | * @return list of batches to uninstall intent |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 311 | */ |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 312 | private List<FlowRuleBatchOperation> uninstallIntent(Intent intent, List<Intent> installables) { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 313 | List<FlowRuleBatchOperation> batches = Lists.newArrayList(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 314 | for (Intent installable : installables) { |
| 315 | trackerService.removeTrackedResources(intent.id(), |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 316 | installable.resources()); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 317 | try { |
| 318 | batches.addAll(getInstaller(installable).uninstall(installable)); |
| 319 | } catch (IntentException e) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 320 | log.warn("Unable to uninstall intent {} due to:", intent.id(), e); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 321 | // TODO: this should never happen. but what if it does? |
| 322 | } |
| 323 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 324 | return batches; |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /** |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 328 | * Registers an intent compiler of the specified intent if an intent compiler |
| 329 | * for the intent is not registered. This method traverses the class hierarchy of |
| 330 | * the intent. Once an intent compiler for a parent type is found, this method |
| 331 | * registers the found intent compiler. |
| 332 | * |
| 333 | * @param intent intent |
| 334 | */ |
| 335 | private void registerSubclassCompilerIfNeeded(Intent intent) { |
| 336 | if (!compilers.containsKey(intent.getClass())) { |
| 337 | Class<?> cls = intent.getClass(); |
| 338 | while (cls != Object.class) { |
| 339 | // As long as we're within the Intent class descendants |
| 340 | if (Intent.class.isAssignableFrom(cls)) { |
| 341 | IntentCompiler<?> compiler = compilers.get(cls); |
| 342 | if (compiler != null) { |
| 343 | compilers.put(intent.getClass(), compiler); |
| 344 | return; |
| 345 | } |
| 346 | } |
| 347 | cls = cls.getSuperclass(); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Registers an intent installer of the specified intent if an intent installer |
| 354 | * for the intent is not registered. This method traverses the class hierarchy of |
| 355 | * the intent. Once an intent installer for a parent type is found, this method |
| 356 | * registers the found intent installer. |
| 357 | * |
| 358 | * @param intent intent |
| 359 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 360 | private void registerSubclassInstallerIfNeeded(Intent intent) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 361 | if (!installers.containsKey(intent.getClass())) { |
| 362 | Class<?> cls = intent.getClass(); |
| 363 | while (cls != Object.class) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 364 | // As long as we're within the Intent class descendants |
| 365 | if (Intent.class.isAssignableFrom(cls)) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 366 | IntentInstaller<?> installer = installers.get(cls); |
| 367 | if (installer != null) { |
| 368 | installers.put(intent.getClass(), installer); |
| 369 | return; |
| 370 | } |
| 371 | } |
| 372 | cls = cls.getSuperclass(); |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 377 | // Store delegate to re-post events emitted from the store. |
| 378 | private class InternalStoreDelegate implements IntentStoreDelegate { |
| 379 | @Override |
| 380 | public void notify(IntentEvent event) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 381 | eventDispatcher.post(event); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 382 | } |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 383 | |
| 384 | @Override |
| 385 | public void process(IntentOperation op) { |
| 386 | //FIXME |
| 387 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 390 | private void buildAndSubmitBatches(Iterable<IntentId> intentIds, |
| 391 | boolean compileAllFailed) { |
| 392 | Map<ApplicationId, IntentOperations.Builder> batches = Maps.newHashMap(); |
| 393 | // Attempt recompilation of the specified intents first. |
| 394 | for (IntentId id : intentIds) { |
| 395 | Intent intent = store.getIntent(id); |
| 396 | if (intent == null) { |
| 397 | continue; |
| 398 | } |
| 399 | IntentOperations.Builder builder = batches.get(intent.appId()); |
| 400 | if (builder == null) { |
| 401 | builder = IntentOperations.builder(intent.appId()); |
| 402 | batches.put(intent.appId(), builder); |
| 403 | } |
| 404 | builder.addUpdateOperation(id); |
| 405 | } |
| 406 | |
| 407 | if (compileAllFailed) { |
| 408 | // If required, compile all currently failed intents. |
| 409 | for (Intent intent : getIntents()) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 410 | IntentState state = getIntentState(intent.id()); |
| 411 | if (RECOMPILE.contains(state)) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 412 | IntentOperations.Builder builder = batches.get(intent.appId()); |
| 413 | if (builder == null) { |
| 414 | builder = IntentOperations.builder(intent.appId()); |
| 415 | batches.put(intent.appId(), builder); |
| 416 | } |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 417 | if (state == WITHDRAW_REQ) { |
| 418 | builder.addWithdrawOperation(intent.id()); |
| 419 | } else { |
| 420 | builder.addUpdateOperation(intent.id()); |
| 421 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | for (ApplicationId appId : batches.keySet()) { |
| 427 | if (batchService.isLocalLeader(appId)) { |
| 428 | execute(batches.get(appId).build()); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 433 | // Topology change delegate |
| 434 | private class InternalTopoChangeDelegate implements TopologyChangeDelegate { |
| 435 | @Override |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 436 | public void triggerCompile(Iterable<IntentId> intentIds, |
| 437 | boolean compileAllFailed) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 438 | buildAndSubmitBatches(intentIds, compileAllFailed); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 439 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 440 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 441 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 442 | // TODO: simplify the branching statements |
| 443 | private IntentUpdate createIntentUpdate(IntentOperation operation) { |
| 444 | switch (operation.type()) { |
| 445 | case SUBMIT: |
| 446 | return new InstallRequest(operation.intent()); |
| 447 | case WITHDRAW: { |
| 448 | Intent oldIntent = store.getIntent(operation.intentId()); |
| 449 | if (oldIntent == null) { |
| 450 | return new DoNothing(); |
| 451 | } |
| 452 | List<Intent> installables = store.getInstallableIntents(oldIntent.id()); |
| 453 | if (installables == null) { |
| 454 | return new WithdrawStateChange1(oldIntent); |
| 455 | } |
| 456 | return new WithdrawRequest(oldIntent, installables); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 457 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 458 | case REPLACE: { |
| 459 | Intent newIntent = operation.intent(); |
| 460 | Intent oldIntent = store.getIntent(operation.intentId()); |
| 461 | if (oldIntent == null) { |
| 462 | return new InstallRequest(newIntent); |
| 463 | } |
| 464 | List<Intent> installables = store.getInstallableIntents(oldIntent.id()); |
| 465 | if (installables == null) { |
| 466 | if (newIntent.equals(oldIntent)) { |
| 467 | return new InstallRequest(newIntent); |
| 468 | } else { |
| 469 | return new WithdrawStateChange2(oldIntent); |
| 470 | } |
| 471 | } |
| 472 | return new ReplaceRequest(newIntent, oldIntent, installables); |
| 473 | } |
| 474 | case UPDATE: { |
| 475 | Intent oldIntent = store.getIntent(operation.intentId()); |
| 476 | if (oldIntent == null) { |
| 477 | return new DoNothing(); |
| 478 | } |
| 479 | List<Intent> installables = getInstallableIntents(oldIntent.id()); |
| 480 | if (installables == null) { |
| 481 | return new InstallRequest(oldIntent); |
| 482 | } |
| 483 | return new ReplaceRequest(oldIntent, oldIntent, installables); |
| 484 | } |
| 485 | default: |
| 486 | // illegal state |
| 487 | return new DoNothing(); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 488 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 491 | private class InstallRequest implements IntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 492 | |
| 493 | private final Intent intent; |
| 494 | |
| 495 | InstallRequest(Intent intent) { |
| 496 | this.intent = checkNotNull(intent); |
| 497 | } |
| 498 | |
| 499 | @Override |
| 500 | public void writeBeforeExecution(BatchWrite batchWrite) { |
| 501 | // TODO consider only "creating" intent if it does not exist |
| 502 | // Note: We need to set state to INSTALL_REQ regardless. |
| 503 | batchWrite.createIntent(intent); |
| 504 | } |
| 505 | |
| 506 | @Override |
| 507 | public Optional<IntentUpdate> execute() { |
| 508 | return Optional.of(new Compiling(intent)); |
| 509 | } |
| 510 | } |
| 511 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 512 | private class WithdrawRequest implements IntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 513 | |
| 514 | private final Intent intent; |
| 515 | private final List<Intent> installables; |
| 516 | |
| 517 | WithdrawRequest(Intent intent, List<Intent> installables) { |
| 518 | this.intent = checkNotNull(intent); |
| 519 | this.installables = ImmutableList.copyOf(checkNotNull(installables)); |
| 520 | } |
| 521 | |
| 522 | @Override |
| 523 | public void writeBeforeExecution(BatchWrite batchWrite) { |
| 524 | batchWrite.setState(intent, WITHDRAW_REQ); |
| 525 | } |
| 526 | |
| 527 | @Override |
| 528 | public Optional<IntentUpdate> execute() { |
| 529 | return Optional.of(new Withdrawing(intent, installables)); |
| 530 | } |
| 531 | } |
| 532 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 533 | private class ReplaceRequest implements IntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 534 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 535 | private final Intent newIntent; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 536 | private final Intent oldIntent; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 537 | private final List<Intent> oldInstallables; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 538 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 539 | ReplaceRequest(Intent newIntent, Intent oldIntent, List<Intent> oldInstallables) { |
| 540 | this.newIntent = checkNotNull(newIntent); |
| 541 | this.oldIntent = checkNotNull(oldIntent); |
| 542 | this.oldInstallables = ImmutableList.copyOf(oldInstallables); |
| 543 | } |
| 544 | |
| 545 | @Override |
| 546 | public void writeBeforeExecution(BatchWrite batchWrite) { |
| 547 | // TODO consider only "creating" intent if it does not exist |
| 548 | // Note: We need to set state to INSTALL_REQ regardless. |
| 549 | batchWrite.createIntent(newIntent); |
| 550 | } |
| 551 | |
| 552 | @Override |
| 553 | public Optional<IntentUpdate> execute() { |
| 554 | try { |
| 555 | List<Intent> installables = compileIntent(newIntent, oldInstallables); |
| 556 | return Optional.of(new Replacing(newIntent, oldIntent, installables, oldInstallables)); |
| 557 | } catch (PathNotFoundException e) { |
| 558 | log.debug("Path not found for intent {}", newIntent); |
| 559 | return Optional.of(new Withdrawing(oldIntent, oldInstallables)); |
| 560 | } catch (IntentException e) { |
| 561 | log.warn("Unable to compile intent {} due to:", newIntent.id(), e); |
| 562 | return Optional.of(new Withdrawing(oldIntent, oldInstallables)); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 563 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 567 | // TODO: better naming |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 568 | private class WithdrawStateChange1 implements CompletedIntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 569 | |
| 570 | private final Intent intent; |
| 571 | |
| 572 | WithdrawStateChange1(Intent intent) { |
| 573 | this.intent = checkNotNull(intent); |
| 574 | } |
| 575 | |
| 576 | @Override |
| 577 | public void writeBeforeExecution(BatchWrite batchWrite) { |
| 578 | batchWrite.setState(intent, WITHDRAW_REQ); |
| 579 | } |
| 580 | |
| 581 | @Override |
| 582 | public void writeAfterExecution(BatchWrite batchWrite) { |
| 583 | batchWrite.setState(intent, WITHDRAWN); |
| 584 | batchWrite.removeInstalledIntents(intent.id()); |
| 585 | batchWrite.removeIntent(intent.id()); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | // TODO: better naming |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 590 | private class WithdrawStateChange2 implements CompletedIntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 591 | |
| 592 | private final Intent intent; |
| 593 | |
| 594 | WithdrawStateChange2(Intent intent) { |
| 595 | this.intent = checkNotNull(intent); |
| 596 | } |
| 597 | |
| 598 | @Override |
| 599 | public void writeBeforeExecution(BatchWrite batchWrite) { |
| 600 | // TODO consider only "creating" intent if it does not exist |
| 601 | // Note: We need to set state to INSTALL_REQ regardless. |
| 602 | batchWrite.createIntent(intent); |
| 603 | } |
| 604 | |
| 605 | @Override |
| 606 | public void writeAfterExecution(BatchWrite batchWrite) { |
| 607 | batchWrite.setState(intent, WITHDRAWN); |
| 608 | batchWrite.removeInstalledIntents(intent.id()); |
| 609 | batchWrite.removeIntent(intent.id()); |
| 610 | } |
| 611 | } |
| 612 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 613 | private class Compiling implements IntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 614 | |
| 615 | private final Intent intent; |
| 616 | |
| 617 | Compiling(Intent intent) { |
| 618 | this.intent = checkNotNull(intent); |
| 619 | } |
| 620 | |
| 621 | @Override |
| 622 | public Optional<IntentUpdate> execute() { |
| 623 | try { |
| 624 | // Compile the intent into installable derivatives. |
| 625 | // If all went well, associate the resulting list of installable |
| 626 | // intents with the top-level intent and proceed to install. |
| 627 | return Optional.of(new Installing(intent, compileIntent(intent, null))); |
| 628 | } catch (PathNotFoundException e) { |
Sho SHIMIZU | fd0cd8c | 2015-01-28 08:16:58 -0800 | [diff] [blame] | 629 | log.debug("Path not found for intent {}", intent); |
| 630 | return Optional.of(new CompilingFailed(intent)); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 631 | } catch (IntentException e) { |
Sho SHIMIZU | fd0cd8c | 2015-01-28 08:16:58 -0800 | [diff] [blame] | 632 | log.warn("Unable to compile intent {} due to:", intent.id(), e); |
| 633 | return Optional.of(new CompilingFailed(intent)); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | // TODO: better naming because install() method actually generate FlowRuleBatchOperations |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 639 | private class Installing implements IntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 640 | |
| 641 | private final Intent intent; |
| 642 | private final List<Intent> installables; |
| 643 | |
| 644 | Installing(Intent intent, List<Intent> installables) { |
| 645 | this.intent = checkNotNull(intent); |
| 646 | this.installables = ImmutableList.copyOf(checkNotNull(installables)); |
| 647 | } |
| 648 | |
| 649 | @Override |
| 650 | public Optional<IntentUpdate> execute() { |
| 651 | Exception exception = null; |
| 652 | // Indicate that the intent is entering the installing phase. |
| 653 | |
| 654 | List<FlowRuleBatchOperation> batches = Lists.newArrayList(); |
| 655 | for (Intent installable : installables) { |
| 656 | registerSubclassInstallerIfNeeded(installable); |
| 657 | trackerService.addTrackedResources(intent.id(), installable.resources()); |
| 658 | try { |
| 659 | batches.addAll(getInstaller(installable).install(installable)); |
| 660 | } catch (Exception e) { // TODO this should be IntentException |
| 661 | log.warn("Unable to install intent {} due to:", intent.id(), e); |
| 662 | trackerService.removeTrackedResources(intent.id(), installable.resources()); |
| 663 | //TODO we failed; intent should be recompiled |
| 664 | exception = e; |
Brian O'Connor | 5d55ed4 | 2014-12-01 18:27:47 -0800 | [diff] [blame] | 665 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 666 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 667 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 668 | if (exception != null) { |
| 669 | return Optional.of(new InstallingFailed(intent, installables, batches)); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 670 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 671 | |
| 672 | return Optional.of(new Installed(intent, installables, batches)); |
| 673 | } |
| 674 | } |
| 675 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 676 | private class Withdrawing implements IntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 677 | |
| 678 | private final Intent intent; |
| 679 | private final List<Intent> installables; |
| 680 | |
| 681 | Withdrawing(Intent intent, List<Intent> installables) { |
| 682 | this.intent = checkNotNull(intent); |
| 683 | this.installables = ImmutableList.copyOf(installables); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 686 | @Override |
| 687 | public Optional<IntentUpdate> execute() { |
| 688 | List<FlowRuleBatchOperation> batches = uninstallIntent(intent, installables); |
| 689 | |
| 690 | return Optional.of(new Withdrawn(intent, installables, batches)); |
| 691 | } |
| 692 | } |
| 693 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 694 | private class Replacing implements IntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 695 | |
| 696 | private final Intent newIntent; |
| 697 | private final Intent oldIntent; |
| 698 | private final List<Intent> newInstallables; |
| 699 | private final List<Intent> oldInstallables; |
| 700 | |
| 701 | private Exception exception; |
| 702 | |
| 703 | Replacing(Intent newIntent, Intent oldIntent, |
| 704 | List<Intent> newInstallables, List<Intent> oldInstallables) { |
| 705 | this.newIntent = checkNotNull(newIntent); |
| 706 | this.oldIntent = checkNotNull(oldIntent); |
| 707 | this.newInstallables = ImmutableList.copyOf(checkNotNull(newInstallables)); |
| 708 | this.oldInstallables = ImmutableList.copyOf(checkNotNull(oldInstallables)); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 711 | @Override |
| 712 | public Optional<IntentUpdate> execute() { |
| 713 | List<FlowRuleBatchOperation> batches = replace(); |
| 714 | |
| 715 | if (exception == null) { |
| 716 | return Optional.of( |
| 717 | new Replaced(newIntent, oldIntent, newInstallables, oldInstallables, batches)); |
| 718 | } |
| 719 | |
| 720 | return Optional.of( |
| 721 | new ReplacingFailed(newIntent, oldIntent, newInstallables, oldInstallables, batches)); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 724 | protected List<FlowRuleBatchOperation> replace() { |
| 725 | checkState(oldInstallables.size() == newInstallables.size(), |
| 726 | "Old and New Intent must have equivalent installable intents."); |
| 727 | |
| 728 | List<FlowRuleBatchOperation> batches = Lists.newArrayList(); |
| 729 | for (int i = 0; i < oldInstallables.size(); i++) { |
| 730 | Intent oldInstallable = oldInstallables.get(i); |
| 731 | Intent newInstallable = newInstallables.get(i); |
| 732 | //FIXME revisit this |
| 733 | // if (oldInstallable.equals(newInstallable)) { |
| 734 | // continue; |
| 735 | // } |
| 736 | checkState(oldInstallable.getClass().equals(newInstallable.getClass()), |
| 737 | "Installable Intent type mismatch."); |
| 738 | trackerService.removeTrackedResources(oldIntent.id(), oldInstallable.resources()); |
| 739 | trackerService.addTrackedResources(newIntent.id(), newInstallable.resources()); |
| 740 | try { |
| 741 | batches.addAll(getInstaller(newInstallable).replace(oldInstallable, newInstallable)); |
| 742 | } catch (IntentException e) { |
| 743 | log.warn("Unable to update intent {} due to:", oldIntent.id(), e); |
| 744 | //FIXME... we failed. need to uninstall (if same) or revert (if different) |
| 745 | trackerService.removeTrackedResources(newIntent.id(), newInstallable.resources()); |
| 746 | exception = e; |
| 747 | batches = uninstallIntent(oldIntent, oldInstallables); |
| 748 | } |
| 749 | } |
| 750 | return batches; |
| 751 | } |
| 752 | } |
| 753 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 754 | private class Installed implements CompletedIntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 755 | |
| 756 | private final Intent intent; |
| 757 | private final List<Intent> installables; |
| 758 | private IntentState intentState; |
| 759 | private final List<FlowRuleBatchOperation> batches; |
| 760 | private int currentBatch = 0; |
| 761 | |
| 762 | Installed(Intent intent, List<Intent> installables, List<FlowRuleBatchOperation> batches) { |
| 763 | this.intent = checkNotNull(intent); |
| 764 | this.installables = ImmutableList.copyOf(checkNotNull(installables)); |
| 765 | this.batches = new LinkedList<>(checkNotNull(batches)); |
| 766 | this.intentState = INSTALLING; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 769 | @Override |
| 770 | public void batchSuccess() { |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 771 | currentBatch++; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 772 | } |
| 773 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 774 | @Override |
| 775 | public List<Intent> allInstallables() { |
| 776 | return installables; |
| 777 | } |
| 778 | |
| 779 | @Override |
| 780 | public void writeAfterExecution(BatchWrite batchWrite) { |
| 781 | switch (intentState) { |
| 782 | case INSTALLING: |
| 783 | batchWrite.setState(intent, INSTALLED); |
| 784 | batchWrite.setInstallableIntents(intent.id(), this.installables); |
| 785 | break; |
| 786 | case FAILED: |
| 787 | batchWrite.setState(intent, FAILED); |
| 788 | batchWrite.removeInstalledIntents(intent.id()); |
| 789 | break; |
| 790 | default: |
| 791 | break; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | @Override |
| 796 | public FlowRuleBatchOperation currentBatch() { |
| 797 | return currentBatch < batches.size() ? batches.get(currentBatch) : null; |
| 798 | } |
| 799 | |
| 800 | @Override |
| 801 | public void batchFailed() { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 802 | // the current batch has failed, so recompile |
| 803 | // remove the current batch and all remaining |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 804 | for (int i = batches.size() - 1; i >= currentBatch; i--) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 805 | batches.remove(i); |
| 806 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 807 | intentState = FAILED; |
| 808 | batches.addAll(uninstallIntent(intent, installables)); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 809 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 810 | // TODO we might want to try to recompile the new intent |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 811 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 812 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 813 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 814 | private class Withdrawn implements CompletedIntentUpdate { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 815 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 816 | private final Intent intent; |
| 817 | private final List<Intent> installables; |
| 818 | private final List<FlowRuleBatchOperation> batches; |
| 819 | private int currentBatch; |
| 820 | |
| 821 | Withdrawn(Intent intent, List<Intent> installables, List<FlowRuleBatchOperation> batches) { |
| 822 | this.intent = checkNotNull(intent); |
| 823 | this.installables = ImmutableList.copyOf(installables); |
| 824 | this.batches = new LinkedList<>(batches); |
| 825 | this.currentBatch = 0; |
| 826 | } |
| 827 | |
| 828 | @Override |
| 829 | public List<Intent> allInstallables() { |
| 830 | return installables; |
| 831 | } |
| 832 | |
| 833 | @Override |
| 834 | public void batchSuccess() { |
| 835 | currentBatch++; |
| 836 | } |
| 837 | |
| 838 | @Override |
| 839 | public void writeAfterExecution(BatchWrite batchWrite) { |
| 840 | batchWrite.setState(intent, WITHDRAWN); |
| 841 | batchWrite.removeInstalledIntents(intent.id()); |
| 842 | batchWrite.removeIntent(intent.id()); |
| 843 | } |
| 844 | |
| 845 | @Override |
| 846 | public FlowRuleBatchOperation currentBatch() { |
| 847 | return currentBatch < batches.size() ? batches.get(currentBatch) : null; |
| 848 | } |
| 849 | |
| 850 | @Override |
| 851 | public void batchFailed() { |
| 852 | // the current batch has failed, so recompile |
| 853 | // remove the current batch and all remaining |
| 854 | for (int i = batches.size() - 1; i >= currentBatch; i--) { |
| 855 | batches.remove(i); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 856 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 857 | batches.addAll(uninstallIntent(intent, installables)); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 861 | private class Replaced implements CompletedIntentUpdate { |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 862 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 863 | private final Intent newIntent; |
| 864 | private final Intent oldIntent; |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 865 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 866 | private final List<Intent> newInstallables; |
| 867 | private final List<Intent> oldInstallables; |
| 868 | private final List<FlowRuleBatchOperation> batches; |
| 869 | private int currentBatch; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 870 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 871 | Replaced(Intent newIntent, Intent oldIntent, |
| 872 | List<Intent> newInstallables, List<Intent> oldInstallables, |
| 873 | List<FlowRuleBatchOperation> batches) { |
| 874 | this.newIntent = checkNotNull(newIntent); |
| 875 | this.oldIntent = checkNotNull(oldIntent); |
| 876 | this.newInstallables = ImmutableList.copyOf(checkNotNull(newInstallables)); |
| 877 | this.oldInstallables = ImmutableList.copyOf(checkNotNull(oldInstallables)); |
| 878 | this.batches = new LinkedList<>(batches); |
| 879 | this.currentBatch = 0; |
Sho SHIMIZU | 8cd9fb8 | 2014-12-11 13:19:18 -0800 | [diff] [blame] | 880 | } |
| 881 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 882 | @Override |
| 883 | public List<Intent> allInstallables() { |
| 884 | LinkedList<Intent> allInstallables = new LinkedList<>(); |
| 885 | allInstallables.addAll(newInstallables); |
| 886 | allInstallables.addAll(oldInstallables); |
Sho SHIMIZU | 8cd9fb8 | 2014-12-11 13:19:18 -0800 | [diff] [blame] | 887 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 888 | return allInstallables; |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 889 | } |
| 890 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 891 | @Override |
| 892 | public void batchSuccess() { |
| 893 | currentBatch++; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 894 | } |
| 895 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 896 | @Override |
| 897 | public void writeAfterExecution(BatchWrite batchWrite) { |
| 898 | batchWrite.setState(newIntent, INSTALLED); |
| 899 | batchWrite.setInstallableIntents(newIntent.id(), newInstallables); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 900 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 901 | batchWrite.setState(oldIntent, WITHDRAWN); |
| 902 | batchWrite.removeInstalledIntents(oldIntent.id()); |
| 903 | batchWrite.removeIntent(oldIntent.id()); |
| 904 | } |
| 905 | |
| 906 | @Override |
| 907 | public FlowRuleBatchOperation currentBatch() { |
| 908 | return currentBatch < batches.size() ? batches.get(currentBatch) : null; |
| 909 | } |
| 910 | |
| 911 | @Override |
| 912 | public void batchFailed() { |
| 913 | // the current batch has failed, so recompile |
| 914 | // remove the current batch and all remaining |
| 915 | for (int i = batches.size() - 1; i >= currentBatch; i--) { |
| 916 | batches.remove(i); |
| 917 | } |
| 918 | batches.addAll(uninstallIntent(oldIntent, oldInstallables)); |
| 919 | |
| 920 | batches.addAll(uninstallIntent(newIntent, newInstallables)); |
| 921 | |
| 922 | // TODO we might want to try to recompile the new intent |
| 923 | } |
| 924 | } |
| 925 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 926 | private class InstallingFailed implements CompletedIntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 927 | |
| 928 | private final Intent intent; |
| 929 | private final List<Intent> installables; |
| 930 | private final List<FlowRuleBatchOperation> batches; |
| 931 | private int currentBatch = 0; |
| 932 | |
| 933 | InstallingFailed(Intent intent, List<Intent> installables, List<FlowRuleBatchOperation> batches) { |
| 934 | this.intent = checkNotNull(intent); |
| 935 | this.installables = ImmutableList.copyOf(checkNotNull(installables)); |
| 936 | this.batches = new LinkedList<>(checkNotNull(batches)); |
| 937 | } |
| 938 | |
| 939 | @Override |
| 940 | public List<Intent> allInstallables() { |
| 941 | return installables; |
| 942 | } |
| 943 | |
| 944 | @Override |
| 945 | public void batchSuccess() { |
| 946 | currentBatch++; |
| 947 | } |
| 948 | |
| 949 | @Override |
| 950 | public void writeAfterExecution(BatchWrite batchWrite) { |
| 951 | batchWrite.setState(intent, FAILED); |
| 952 | batchWrite.removeInstalledIntents(intent.id()); |
| 953 | } |
| 954 | |
| 955 | @Override |
| 956 | public FlowRuleBatchOperation currentBatch() { |
| 957 | return currentBatch < batches.size() ? batches.get(currentBatch) : null; |
| 958 | } |
| 959 | |
| 960 | @Override |
| 961 | public void batchFailed() { |
| 962 | // the current batch has failed, so recompile |
| 963 | // remove the current batch and all remaining |
| 964 | for (int i = batches.size() - 1; i >= currentBatch; i--) { |
| 965 | batches.remove(i); |
| 966 | } |
| 967 | batches.addAll(uninstallIntent(intent, installables)); |
| 968 | |
| 969 | // TODO we might want to try to recompile the new intent |
| 970 | } |
| 971 | } |
| 972 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 973 | private class ReplacingFailed implements CompletedIntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 974 | |
| 975 | private final Intent newIntent; |
| 976 | private final Intent oldIntent; |
| 977 | private final List<Intent> newInstallables; |
| 978 | private final List<Intent> oldInstallables; |
| 979 | private final List<FlowRuleBatchOperation> batches; |
| 980 | private int currentBatch; |
| 981 | |
| 982 | ReplacingFailed(Intent newIntent, Intent oldIntent, |
| 983 | List<Intent> newInstallables, List<Intent> oldInstallables, |
| 984 | List<FlowRuleBatchOperation> batches) { |
| 985 | this.newIntent = checkNotNull(newIntent); |
| 986 | this.oldIntent = checkNotNull(oldIntent); |
| 987 | this.newInstallables = ImmutableList.copyOf(checkNotNull(newInstallables)); |
| 988 | this.oldInstallables = ImmutableList.copyOf(checkNotNull(oldInstallables)); |
| 989 | this.batches = new LinkedList<>(batches); |
| 990 | this.currentBatch = 0; |
| 991 | } |
| 992 | |
| 993 | @Override |
| 994 | public List<Intent> allInstallables() { |
| 995 | LinkedList<Intent> allInstallables = new LinkedList<>(); |
| 996 | allInstallables.addAll(newInstallables); |
| 997 | allInstallables.addAll(oldInstallables); |
| 998 | |
| 999 | return allInstallables; |
| 1000 | } |
| 1001 | |
| 1002 | @Override |
| 1003 | public void batchSuccess() { |
| 1004 | currentBatch++; |
| 1005 | } |
| 1006 | |
| 1007 | @Override |
| 1008 | public void writeAfterExecution(BatchWrite batchWrite) { |
| 1009 | batchWrite.setState(newIntent, FAILED); |
| 1010 | batchWrite.removeInstalledIntents(newIntent.id()); |
| 1011 | |
| 1012 | batchWrite.setState(oldIntent, WITHDRAWN); |
| 1013 | batchWrite.removeInstalledIntents(oldIntent.id()); |
| 1014 | batchWrite.removeIntent(oldIntent.id()); |
| 1015 | } |
| 1016 | |
| 1017 | @Override |
| 1018 | public FlowRuleBatchOperation currentBatch() { |
| 1019 | return currentBatch < batches.size() ? batches.get(currentBatch) : null; |
| 1020 | } |
| 1021 | |
| 1022 | @Override |
| 1023 | public void batchFailed() { |
| 1024 | // the current batch has failed, so recompile |
| 1025 | // remove the current batch and all remaining |
| 1026 | for (int i = batches.size() - 1; i >= currentBatch; i--) { |
| 1027 | batches.remove(i); |
| 1028 | } |
| 1029 | batches.addAll(uninstallIntent(oldIntent, oldInstallables)); |
| 1030 | |
| 1031 | batches.addAll(uninstallIntent(newIntent, newInstallables)); |
| 1032 | |
| 1033 | // TODO we might want to try to recompile the new intent |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | private class IntentBatchPreprocess implements Runnable { |
| 1038 | |
| 1039 | // TODO make this configurable |
| 1040 | private static final int TIMEOUT_PER_OP = 500; // ms |
| 1041 | protected static final int MAX_ATTEMPTS = 3; |
| 1042 | |
| 1043 | protected final IntentOperations ops; |
| 1044 | |
| 1045 | // future holding current FlowRuleBatch installation result |
| 1046 | protected final long startTime = System.currentTimeMillis(); |
| 1047 | protected final long endTime; |
| 1048 | |
| 1049 | private IntentBatchPreprocess(IntentOperations ops, long endTime) { |
| 1050 | this.ops = checkNotNull(ops); |
| 1051 | this.endTime = endTime; |
| 1052 | } |
| 1053 | |
| 1054 | public IntentBatchPreprocess(IntentOperations ops) { |
| 1055 | this(ops, System.currentTimeMillis() + ops.operations().size() * TIMEOUT_PER_OP); |
| 1056 | } |
| 1057 | |
| 1058 | // FIXME compute reasonable timeouts |
| 1059 | protected long calculateTimeoutLimit() { |
| 1060 | return System.currentTimeMillis() + ops.operations().size() * TIMEOUT_PER_OP; |
| 1061 | } |
| 1062 | |
| 1063 | @Override |
| 1064 | public void run() { |
| 1065 | try { |
| 1066 | // this should only be called on the first iteration |
| 1067 | // note: this a "expensive", so it is not done in the constructor |
| 1068 | |
| 1069 | // - creates per Intent installation context (IntentUpdate) |
| 1070 | // - write Intents to store |
| 1071 | // - process (compile, install, etc.) each Intents |
| 1072 | // - generate FlowRuleBatch for this phase |
| 1073 | // build IntentUpdates |
| 1074 | List<IntentUpdate> updates = createIntentUpdates(); |
| 1075 | |
| 1076 | // Write batch information |
| 1077 | BatchWrite batchWrite = createBatchWrite(updates); |
Sho SHIMIZU | 9ea3453 | 2015-01-28 12:28:00 -0800 | [diff] [blame] | 1078 | store.batchWrite(batchWrite); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 1079 | |
| 1080 | new IntentBatchApplyFirst(ops, processIntentUpdates(updates), endTime, 0, null).run(); |
| 1081 | } catch (Exception e) { |
| 1082 | log.error("Error submitting batches:", e); |
| 1083 | // FIXME incomplete Intents should be cleaned up |
| 1084 | // (transition to FAILED, etc.) |
| 1085 | |
| 1086 | // TODO: remove duplicate due to inlining |
| 1087 | // the batch has failed |
| 1088 | // TODO: maybe we should do more? |
| 1089 | log.error("Walk the plank, matey..."); |
| 1090 | batchService.removeIntentOperations(ops); |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | private List<IntentUpdate> createIntentUpdates() { |
| 1095 | return ops.operations().stream() |
| 1096 | .map(IntentManager.this::createIntentUpdate) |
| 1097 | .collect(Collectors.toList()); |
| 1098 | } |
| 1099 | |
| 1100 | private BatchWrite createBatchWrite(List<IntentUpdate> updates) { |
| 1101 | BatchWrite batchWrite = BatchWrite.newInstance(); |
| 1102 | updates.forEach(update -> update.writeBeforeExecution(batchWrite)); |
| 1103 | return batchWrite; |
| 1104 | } |
| 1105 | |
| 1106 | private List<CompletedIntentUpdate> processIntentUpdates(List<IntentUpdate> updates) { |
| 1107 | // start processing each Intents |
| 1108 | List<CompletedIntentUpdate> completed = new ArrayList<>(); |
| 1109 | for (IntentUpdate update : updates) { |
| 1110 | Optional<IntentUpdate> phase = Optional.of(update); |
| 1111 | IntentUpdate previous = update; |
| 1112 | while (true) { |
| 1113 | if (!phase.isPresent()) { |
| 1114 | // FIXME: not type safe cast |
| 1115 | completed.add((CompletedIntentUpdate) previous); |
| 1116 | break; |
| 1117 | } |
| 1118 | previous = phase.get(); |
| 1119 | phase = previous.execute(); |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | return completed; |
| 1124 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 1125 | } |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 1126 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 1127 | // TODO: better naming |
| 1128 | private class IntentBatchApplyFirst extends IntentBatchPreprocess { |
| 1129 | |
| 1130 | protected final List<CompletedIntentUpdate> intentUpdates; |
| 1131 | protected final int installAttempt; |
| 1132 | protected Future<CompletedBatchOperation> future; |
| 1133 | |
| 1134 | IntentBatchApplyFirst(IntentOperations operations, List<CompletedIntentUpdate> intentUpdates, |
| 1135 | long endTime, int installAttempt, Future<CompletedBatchOperation> future) { |
| 1136 | super(operations, endTime); |
| 1137 | this.intentUpdates = ImmutableList.copyOf(intentUpdates); |
| 1138 | this.future = future; |
| 1139 | this.installAttempt = installAttempt; |
| 1140 | } |
| 1141 | |
| 1142 | @Override |
| 1143 | public void run() { |
| 1144 | Future<CompletedBatchOperation> future = applyNextBatch(intentUpdates); |
| 1145 | new IntentBatchProcessFutures(ops, intentUpdates, endTime, installAttempt, future).run(); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 1148 | /** |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 1149 | * Builds and applies the next batch, and returns the future. |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 1150 | * |
| 1151 | * @return Future for next batch |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 1152 | */ |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 1153 | protected Future<CompletedBatchOperation> applyNextBatch(List<CompletedIntentUpdate> updates) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 1154 | //TODO test this. (also, maybe save this batch) |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 1155 | FlowRuleBatchOperation batch = createFlowRuleBatchOperation(updates); |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 1156 | if (batch.size() > 0) { |
| 1157 | //FIXME apply batch might throw an exception |
| 1158 | return flowRuleService.applyBatch(batch); |
| 1159 | } else { |
| 1160 | // there are no flow rule batches; finalize the intent update |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 1161 | BatchWrite batchWrite = createFinalizedBatchWrite(updates); |
| 1162 | |
Sho SHIMIZU | 9ea3453 | 2015-01-28 12:28:00 -0800 | [diff] [blame] | 1163 | store.batchWrite(batchWrite); |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 1164 | return null; |
| 1165 | } |
| 1166 | } |
| 1167 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 1168 | private FlowRuleBatchOperation createFlowRuleBatchOperation(List<CompletedIntentUpdate> intentUpdates) { |
| 1169 | FlowRuleBatchOperation batch = new FlowRuleBatchOperation(Collections.emptyList()); |
| 1170 | for (CompletedIntentUpdate update : intentUpdates) { |
| 1171 | FlowRuleBatchOperation currentBatch = update.currentBatch(); |
| 1172 | if (currentBatch != null) { |
| 1173 | batch.addAll(currentBatch); |
| 1174 | } |
| 1175 | } |
| 1176 | return batch; |
| 1177 | } |
| 1178 | |
| 1179 | private BatchWrite createFinalizedBatchWrite(List<CompletedIntentUpdate> intentUpdates) { |
| 1180 | BatchWrite batchWrite = BatchWrite.newInstance(); |
| 1181 | for (CompletedIntentUpdate update : intentUpdates) { |
| 1182 | update.writeAfterExecution(batchWrite); |
| 1183 | } |
| 1184 | return batchWrite; |
| 1185 | } |
| 1186 | |
| 1187 | protected void abandonShip() { |
| 1188 | // the batch has failed |
| 1189 | // TODO: maybe we should do more? |
| 1190 | log.error("Walk the plank, matey..."); |
| 1191 | future = null; |
| 1192 | batchService.removeIntentOperations(ops); |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | // TODO: better naming |
| 1197 | private class IntentBatchProcessFutures extends IntentBatchApplyFirst { |
| 1198 | |
| 1199 | IntentBatchProcessFutures(IntentOperations operations, List<CompletedIntentUpdate> intentUpdates, |
| 1200 | long endTime, int installAttempt, Future<CompletedBatchOperation> future) { |
| 1201 | super(operations, intentUpdates, endTime, installAttempt, future); |
| 1202 | } |
| 1203 | |
| 1204 | @Override |
| 1205 | public void run() { |
| 1206 | try { |
| 1207 | // - peek if current FlowRuleBatch is complete |
| 1208 | // -- If complete OK: |
| 1209 | // step each IntentUpdate forward |
| 1210 | // If phase left: generate next FlowRuleBatch |
| 1211 | // If no more phase: write parking states |
| 1212 | // -- If complete FAIL: |
| 1213 | // Intent which failed: transition Intent to FAILED |
| 1214 | // Other Intents: resubmit same FlowRuleBatch for this phase |
| 1215 | Future<CompletedBatchOperation> future = processFutures(); |
| 1216 | if (future == null) { |
| 1217 | // there are no outstanding batches; we are done |
| 1218 | batchService.removeIntentOperations(ops); |
| 1219 | } else if (System.currentTimeMillis() > endTime) { |
| 1220 | // - cancel current FlowRuleBatch and resubmit again |
| 1221 | retry(); |
| 1222 | } else { |
| 1223 | // we are not done yet, yield the thread by resubmitting ourselves |
| 1224 | executor.submit(new IntentBatchProcessFutures(ops, intentUpdates, endTime, installAttempt, future)); |
| 1225 | } |
| 1226 | } catch (Exception e) { |
| 1227 | log.error("Error submitting batches:", e); |
| 1228 | // FIXME incomplete Intents should be cleaned up |
| 1229 | // (transition to FAILED, etc.) |
| 1230 | abandonShip(); |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | /** |
| 1235 | * Iterate through the pending futures, and remove them when they have completed. |
| 1236 | */ |
| 1237 | private Future<CompletedBatchOperation> processFutures() { |
| 1238 | try { |
| 1239 | CompletedBatchOperation completed = future.get(100, TimeUnit.NANOSECONDS); |
| 1240 | updateBatches(completed); |
| 1241 | return applyNextBatch(intentUpdates); |
| 1242 | } catch (TimeoutException | InterruptedException te) { |
| 1243 | log.trace("Installation of intents are still pending: {}", ops); |
| 1244 | return future; |
| 1245 | } catch (ExecutionException e) { |
| 1246 | log.warn("Execution of batch failed: {}", ops, e); |
| 1247 | abandonShip(); |
| 1248 | return future; |
| 1249 | } |
| 1250 | } |
| 1251 | |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 1252 | private void updateBatches(CompletedBatchOperation completed) { |
| 1253 | if (completed.isSuccess()) { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 1254 | for (CompletedIntentUpdate update : intentUpdates) { |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 1255 | update.batchSuccess(); |
| 1256 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 1257 | } else { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 1258 | // entire batch has been reverted... |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 1259 | log.debug("Failed items: {}", completed.failedItems()); |
| 1260 | log.debug("Failed ids: {}", completed.failedIds()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 1261 | |
| 1262 | for (Long id : completed.failedIds()) { |
| 1263 | IntentId targetId = IntentId.valueOf(id); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 1264 | for (CompletedIntentUpdate update : intentUpdates) { |
| 1265 | for (Intent intent : update.allInstallables()) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 1266 | if (intent.id().equals(targetId)) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 1267 | update.batchFailed(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 1268 | break; |
| 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | // don't increment the non-failed items, as they have been reverted. |
| 1273 | } |
| 1274 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 1277 | private void retry() { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 1278 | log.debug("Execution timed out, retrying."); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 1279 | if (future.cancel(true)) { // cancel success; batch is reverted |
| 1280 | // reset the timer |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 1281 | long timeLimit = calculateTimeoutLimit(); |
| 1282 | int attempts = installAttempt + 1; |
| 1283 | if (attempts == MAX_ATTEMPTS) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 1284 | log.warn("Install request timed out: {}", ops); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 1285 | for (CompletedIntentUpdate update : intentUpdates) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 1286 | update.batchFailed(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 1287 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 1288 | } else if (attempts > MAX_ATTEMPTS) { |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 1289 | abandonShip(); |
| 1290 | return; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 1291 | } // else just resubmit the work |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 1292 | Future<CompletedBatchOperation> future = applyNextBatch(intentUpdates); |
| 1293 | executor.submit(new IntentBatchProcessFutures(ops, intentUpdates, timeLimit, attempts, future)); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 1294 | } else { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 1295 | log.error("Cancelling FlowRuleBatch failed."); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 1296 | // FIXME |
| 1297 | // cancel failed... batch is broken; shouldn't happen! |
| 1298 | // we could manually reverse everything |
| 1299 | // ... or just core dump and send email to Ali |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 1300 | abandonShip(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 1301 | } |
| 1302 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 1303 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 1304 | |
| 1305 | private class InternalBatchDelegate implements IntentBatchDelegate { |
| 1306 | @Override |
| 1307 | public void execute(IntentOperations operations) { |
Yuta HIGUCHI | fe4367a | 2014-11-24 19:19:09 -0800 | [diff] [blame] | 1308 | log.info("Execute {} operation(s).", operations.operations().size()); |
| 1309 | log.debug("Execute operations: {}", operations.operations()); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 1310 | //FIXME: perhaps we want to track this task so that we can cancel it. |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 1311 | executor.execute(new IntentBatchPreprocess(operations)); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | @Override |
| 1315 | public void cancel(IntentOperations operations) { |
| 1316 | //FIXME: implement this |
| 1317 | log.warn("NOT IMPLEMENTED -- Cancel operations: {}", operations); |
| 1318 | } |
| 1319 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 1320 | } |