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 | |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.Collection; |
| 20 | import java.util.Collections; |
| 21 | import java.util.EnumSet; |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.Optional; |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame^] | 25 | import java.util.concurrent.Callable; |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 26 | import java.util.concurrent.ConcurrentHashMap; |
| 27 | import java.util.concurrent.ConcurrentMap; |
| 28 | import java.util.concurrent.ExecutionException; |
| 29 | import java.util.concurrent.ExecutorService; |
| 30 | import java.util.concurrent.Future; |
| 31 | import java.util.concurrent.TimeUnit; |
| 32 | import java.util.concurrent.TimeoutException; |
| 33 | import java.util.stream.Collectors; |
| 34 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 35 | import org.apache.felix.scr.annotations.Activate; |
| 36 | import org.apache.felix.scr.annotations.Component; |
| 37 | import org.apache.felix.scr.annotations.Deactivate; |
| 38 | import org.apache.felix.scr.annotations.Reference; |
| 39 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 40 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.core.CoreService; |
| 42 | import org.onosproject.core.IdGenerator; |
| 43 | import org.onosproject.event.AbstractListenerRegistry; |
| 44 | import org.onosproject.event.EventDeliveryService; |
| 45 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 46 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 47 | import org.onosproject.net.flow.FlowRuleService; |
| 48 | import org.onosproject.net.intent.Intent; |
| 49 | import org.onosproject.net.intent.IntentBatchDelegate; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import org.onosproject.net.intent.IntentCompiler; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 51 | import org.onosproject.net.intent.IntentData; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 52 | import org.onosproject.net.intent.IntentEvent; |
| 53 | import org.onosproject.net.intent.IntentException; |
| 54 | import org.onosproject.net.intent.IntentExtensionService; |
| 55 | import org.onosproject.net.intent.IntentId; |
| 56 | import org.onosproject.net.intent.IntentInstaller; |
| 57 | import org.onosproject.net.intent.IntentListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 58 | import org.onosproject.net.intent.IntentService; |
| 59 | import org.onosproject.net.intent.IntentState; |
| 60 | import org.onosproject.net.intent.IntentStore; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 61 | import org.onosproject.net.intent.IntentStoreDelegate; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 62 | import org.slf4j.Logger; |
| 63 | |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 64 | import com.google.common.collect.ImmutableList; |
| 65 | import com.google.common.collect.ImmutableMap; |
| 66 | import com.google.common.collect.Lists; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 67 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 68 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 69 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame^] | 70 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 71 | import static org.onlab.util.Tools.namedThreads; |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 72 | import static org.onosproject.net.intent.IntentState.FAILED; |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 73 | import static org.onosproject.net.intent.IntentState.INSTALL_REQ; |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 74 | import static org.onosproject.net.intent.IntentState.WITHDRAW_REQ; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 75 | import static org.slf4j.LoggerFactory.getLogger; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * An implementation of Intent Manager. |
| 79 | */ |
| 80 | @Component(immediate = true) |
| 81 | @Service |
| 82 | public class IntentManager |
| 83 | implements IntentService, IntentExtensionService { |
Sho SHIMIZU | 8b5051d | 2014-11-05 11:24:13 -0800 | [diff] [blame] | 84 | private static final Logger log = getLogger(IntentManager.class); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 85 | |
| 86 | public static final String INTENT_NULL = "Intent cannot be null"; |
| 87 | public static final String INTENT_ID_NULL = "Intent ID cannot be null"; |
| 88 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 89 | private static final int NUM_THREADS = 12; |
| 90 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 91 | private static final EnumSet<IntentState> RECOMPILE |
| 92 | = EnumSet.of(INSTALL_REQ, FAILED, WITHDRAW_REQ); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 93 | |
| 94 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 95 | // Collections for compiler, installer, and listener are ONOS instance local |
| 96 | private final ConcurrentMap<Class<? extends Intent>, |
| 97 | IntentCompiler<? extends Intent>> compilers = new ConcurrentHashMap<>(); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 98 | private final ConcurrentMap<Class<? extends Intent>, |
| 99 | IntentInstaller<? extends Intent>> installers = new ConcurrentHashMap<>(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 100 | |
| 101 | private final AbstractListenerRegistry<IntentEvent, IntentListener> |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 102 | listenerRegistry = new AbstractListenerRegistry<>(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 103 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 105 | protected CoreService coreService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 106 | |
| 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 108 | protected IntentStore store; |
| 109 | |
| 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 111 | protected ObjectiveTrackerService trackerService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 112 | |
| 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 114 | protected EventDeliveryService eventDispatcher; |
| 115 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 117 | protected FlowRuleService flowRuleService; |
| 118 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 119 | |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame^] | 120 | private ExecutorService batchExecutor; |
| 121 | private ExecutorService workerExecutor; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 122 | |
| 123 | private final IntentStoreDelegate delegate = new InternalStoreDelegate(); |
| 124 | private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate(); |
| 125 | private final IntentBatchDelegate batchDelegate = new InternalBatchDelegate(); |
| 126 | private IdGenerator idGenerator; |
| 127 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 128 | private final IntentAccumulator accumulator = new IntentAccumulator(batchDelegate); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 129 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 130 | @Activate |
| 131 | public void activate() { |
| 132 | store.setDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 133 | trackerService.setDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 134 | eventDispatcher.addSink(IntentEvent.class, listenerRegistry); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame^] | 135 | batchExecutor = newSingleThreadExecutor(namedThreads("onos-intent-batch")); |
| 136 | workerExecutor = newFixedThreadPool(NUM_THREADS, namedThreads("onos-intent-worker-%d")); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 137 | idGenerator = coreService.getIdGenerator("intent-ids"); |
| 138 | Intent.bindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 139 | log.info("Started"); |
| 140 | } |
| 141 | |
| 142 | @Deactivate |
| 143 | public void deactivate() { |
| 144 | store.unsetDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 145 | trackerService.unsetDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 146 | eventDispatcher.removeSink(IntentEvent.class); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame^] | 147 | batchExecutor.shutdown(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 148 | Intent.unbindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 149 | log.info("Stopped"); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void submit(Intent intent) { |
| 154 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 155 | IntentData data = new IntentData(intent, IntentState.INSTALL_REQ, null); |
| 156 | //FIXME timestamp? |
| 157 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void withdraw(Intent intent) { |
| 162 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 163 | IntentData data = new IntentData(intent, IntentState.WITHDRAW_REQ, null); |
| 164 | //FIXME timestamp? |
| 165 | store.addPending(data); |
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 |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 169 | public Iterable<Intent> getIntents() { |
| 170 | return store.getIntents(); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public long getIntentCount() { |
| 175 | return store.getIntentCount(); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public Intent getIntent(IntentId id) { |
| 180 | checkNotNull(id, INTENT_ID_NULL); |
| 181 | return store.getIntent(id); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public IntentState getIntentState(IntentId id) { |
| 186 | checkNotNull(id, INTENT_ID_NULL); |
| 187 | return store.getIntentState(id); |
| 188 | } |
| 189 | |
| 190 | @Override |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 191 | public List<Intent> getInstallableIntents(IntentId intentId) { |
| 192 | checkNotNull(intentId, INTENT_ID_NULL); |
| 193 | return store.getInstallableIntents(intentId); |
| 194 | } |
| 195 | |
| 196 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 197 | public void addListener(IntentListener listener) { |
| 198 | listenerRegistry.addListener(listener); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public void removeListener(IntentListener listener) { |
| 203 | listenerRegistry.removeListener(listener); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) { |
| 208 | compilers.put(cls, compiler); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
| 213 | compilers.remove(cls); |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { |
| 218 | return ImmutableMap.copyOf(compilers); |
| 219 | } |
| 220 | |
| 221 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 222 | 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] | 223 | installers.put(cls, installer); |
| 224 | } |
| 225 | |
| 226 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 227 | public <T extends Intent> void unregisterInstaller(Class<T> cls) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 228 | installers.remove(cls); |
| 229 | } |
| 230 | |
| 231 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 232 | public Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> getInstallers() { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 233 | return ImmutableMap.copyOf(installers); |
| 234 | } |
| 235 | |
| 236 | /** |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 237 | * Returns the corresponding intent compiler to the specified intent. |
| 238 | * |
| 239 | * @param intent intent |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 240 | * @param <T> the type of intent |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 241 | * @return intent compiler corresponding to the specified intent |
| 242 | */ |
| 243 | private <T extends Intent> IntentCompiler<T> getCompiler(T intent) { |
| 244 | @SuppressWarnings("unchecked") |
| 245 | IntentCompiler<T> compiler = (IntentCompiler<T>) compilers.get(intent.getClass()); |
| 246 | if (compiler == null) { |
| 247 | throw new IntentException("no compiler for class " + intent.getClass()); |
| 248 | } |
| 249 | return compiler; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Returns the corresponding intent installer to the specified installable intent. |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 254 | * |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 255 | * @param intent intent |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 256 | * @param <T> the type of installable intent |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 257 | * @return intent installer corresponding to the specified installable intent |
| 258 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 259 | private <T extends Intent> IntentInstaller<T> getInstaller(T intent) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 260 | @SuppressWarnings("unchecked") |
| 261 | IntentInstaller<T> installer = (IntentInstaller<T>) installers.get(intent.getClass()); |
| 262 | if (installer == null) { |
| 263 | throw new IntentException("no installer for class " + intent.getClass()); |
| 264 | } |
| 265 | return installer; |
| 266 | } |
| 267 | |
| 268 | /** |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 269 | * Compiles an intent recursively. |
| 270 | * |
| 271 | * @param intent intent |
| 272 | * @return result of compilation |
| 273 | */ |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 274 | List<Intent> compileIntent(Intent intent, List<Intent> previousInstallables) { |
Thomas Vachuska | 4926c1b | 2014-10-21 00:44:10 -0700 | [diff] [blame] | 275 | if (intent.isInstallable()) { |
| 276 | return ImmutableList.of(intent); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 277 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 278 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 279 | registerSubclassCompilerIfNeeded(intent); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 280 | // FIXME: get previous resources |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 281 | List<Intent> installable = new ArrayList<>(); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 282 | for (Intent compiled : getCompiler(intent).compile(intent, previousInstallables, null)) { |
| 283 | installable.addAll(compileIntent(compiled, previousInstallables)); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 284 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 285 | return installable; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 289 | * Uninstalls all installable intents associated with the given intent. |
| 290 | * |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 291 | * @param intent intent |
| 292 | * @param installables installable intents |
| 293 | * @return list of batches to uninstall intent |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 294 | */ |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 295 | List<FlowRuleBatchOperation> uninstallIntent(Intent intent, List<Intent> installables) { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 296 | List<FlowRuleBatchOperation> batches = Lists.newArrayList(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 297 | for (Intent installable : installables) { |
| 298 | trackerService.removeTrackedResources(intent.id(), |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 299 | installable.resources()); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 300 | try { |
| 301 | batches.addAll(getInstaller(installable).uninstall(installable)); |
| 302 | } catch (IntentException e) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 303 | log.warn("Unable to uninstall intent {} due to:", intent.id(), e); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 304 | // TODO: this should never happen. but what if it does? |
| 305 | } |
| 306 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 307 | return batches; |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /** |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 311 | * Registers an intent compiler of the specified intent if an intent compiler |
| 312 | * for the intent is not registered. This method traverses the class hierarchy of |
| 313 | * the intent. Once an intent compiler for a parent type is found, this method |
| 314 | * registers the found intent compiler. |
| 315 | * |
| 316 | * @param intent intent |
| 317 | */ |
| 318 | private void registerSubclassCompilerIfNeeded(Intent intent) { |
| 319 | if (!compilers.containsKey(intent.getClass())) { |
| 320 | Class<?> cls = intent.getClass(); |
| 321 | while (cls != Object.class) { |
| 322 | // As long as we're within the Intent class descendants |
| 323 | if (Intent.class.isAssignableFrom(cls)) { |
| 324 | IntentCompiler<?> compiler = compilers.get(cls); |
| 325 | if (compiler != null) { |
| 326 | compilers.put(intent.getClass(), compiler); |
| 327 | return; |
| 328 | } |
| 329 | } |
| 330 | cls = cls.getSuperclass(); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Registers an intent installer of the specified intent if an intent installer |
| 337 | * for the intent is not registered. This method traverses the class hierarchy of |
| 338 | * the intent. Once an intent installer for a parent type is found, this method |
| 339 | * registers the found intent installer. |
| 340 | * |
| 341 | * @param intent intent |
| 342 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 343 | private void registerSubclassInstallerIfNeeded(Intent intent) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 344 | if (!installers.containsKey(intent.getClass())) { |
| 345 | Class<?> cls = intent.getClass(); |
| 346 | while (cls != Object.class) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 347 | // As long as we're within the Intent class descendants |
| 348 | if (Intent.class.isAssignableFrom(cls)) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 349 | IntentInstaller<?> installer = installers.get(cls); |
| 350 | if (installer != null) { |
| 351 | installers.put(intent.getClass(), installer); |
| 352 | return; |
| 353 | } |
| 354 | } |
| 355 | cls = cls.getSuperclass(); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 360 | // Store delegate to re-post events emitted from the store. |
| 361 | private class InternalStoreDelegate implements IntentStoreDelegate { |
| 362 | @Override |
| 363 | public void notify(IntentEvent event) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 364 | eventDispatcher.post(event); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 365 | } |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 366 | |
| 367 | @Override |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 368 | public void process(IntentData data) { |
| 369 | accumulator.add(data); |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 370 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 373 | private void buildAndSubmitBatches(Iterable<IntentId> intentIds, |
| 374 | boolean compileAllFailed) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 375 | // Attempt recompilation of the specified intents first. |
| 376 | for (IntentId id : intentIds) { |
| 377 | Intent intent = store.getIntent(id); |
| 378 | if (intent == null) { |
| 379 | continue; |
| 380 | } |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 381 | submit(intent); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | if (compileAllFailed) { |
| 385 | // If required, compile all currently failed intents. |
| 386 | for (Intent intent : getIntents()) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 387 | IntentState state = getIntentState(intent.id()); |
| 388 | if (RECOMPILE.contains(state)) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 389 | if (state == WITHDRAW_REQ) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 390 | withdraw(intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 391 | } else { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 392 | submit(intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 393 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 398 | //FIXME |
| 399 | // for (ApplicationId appId : batches.keySet()) { |
| 400 | // if (batchService.isLocalLeader(appId)) { |
| 401 | // execute(batches.get(appId).build()); |
| 402 | // } |
| 403 | // } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 404 | } |
| 405 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 406 | // Topology change delegate |
| 407 | private class InternalTopoChangeDelegate implements TopologyChangeDelegate { |
| 408 | @Override |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 409 | public void triggerCompile(Iterable<IntentId> intentIds, |
| 410 | boolean compileAllFailed) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 411 | buildAndSubmitBatches(intentIds, compileAllFailed); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 412 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 413 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 414 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 415 | // TODO: simplify the branching statements |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 416 | private IntentUpdate createIntentUpdate(IntentData intentData) { |
| 417 | IntentData currentState = store.getIntentData(intentData.key()); |
| 418 | switch (intentData.state()) { |
| 419 | case INSTALL_REQ: |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 420 | return new InstallRequest(this, intentData.intent(), currentState); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 421 | case WITHDRAW_REQ: |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 422 | return new WithdrawRequest(this, intentData.intent(), currentState); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 423 | // fallthrough |
| 424 | case COMPILING: |
| 425 | case INSTALLING: |
| 426 | case INSTALLED: |
| 427 | case RECOMPILING: |
| 428 | case WITHDRAWING: |
| 429 | case WITHDRAWN: |
| 430 | case FAILED: |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 431 | default: |
| 432 | // illegal state |
| 433 | return new DoNothing(); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 434 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 437 | List<FlowRuleBatchOperation> convert(List<Intent> installables) { |
Sho SHIMIZU | 8d9d136 | 2015-02-04 12:28:15 -0800 | [diff] [blame] | 438 | List<FlowRuleBatchOperation> batches = new ArrayList<>(installables.size()); |
| 439 | for (Intent installable : installables) { |
| 440 | try { |
| 441 | registerSubclassInstallerIfNeeded(installable); |
| 442 | batches.addAll(getInstaller(installable).install(installable)); |
| 443 | } catch (Exception e) { // TODO this should be IntentException |
Brian O'Connor | 098da07 | 2015-02-04 14:07:59 -0800 | [diff] [blame] | 444 | throw new FlowRuleBatchOperationConversionException(batches, e); |
Sho SHIMIZU | 8d9d136 | 2015-02-04 12:28:15 -0800 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | return batches; |
| 448 | } |
| 449 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 450 | private class IntentBatchPreprocess implements Runnable { |
| 451 | |
| 452 | // TODO make this configurable |
| 453 | private static final int TIMEOUT_PER_OP = 500; // ms |
| 454 | protected static final int MAX_ATTEMPTS = 3; |
| 455 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 456 | protected final Collection<IntentData> ops; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 457 | |
| 458 | // future holding current FlowRuleBatch installation result |
| 459 | protected final long startTime = System.currentTimeMillis(); |
| 460 | protected final long endTime; |
| 461 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 462 | private IntentBatchPreprocess(Collection<IntentData> ops, long endTime) { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 463 | this.ops = checkNotNull(ops); |
| 464 | this.endTime = endTime; |
| 465 | } |
| 466 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 467 | public IntentBatchPreprocess(Collection<IntentData> ops) { |
| 468 | this(ops, System.currentTimeMillis() + ops.size() * TIMEOUT_PER_OP); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | // FIXME compute reasonable timeouts |
| 472 | protected long calculateTimeoutLimit() { |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 473 | return System.currentTimeMillis() + ops.size() * TIMEOUT_PER_OP; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public void run() { |
| 478 | try { |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame^] | 479 | // 1. wrap each intentdata in a runnable and submit |
| 480 | List<Future<IntentUpdate>> updates = createIntentUpdates(); |
| 481 | // TODO |
| 482 | // 2. wait for completion of all the work |
| 483 | // 3. accumulate results and submit batch write of IntentData to store |
| 484 | // (we can also try to update these individually) |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 485 | |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame^] | 486 | |
| 487 | //new IntentBatchApplyFirst(ops, processIntentUpdates(updates), endTime, 0, null).run(); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 488 | } catch (Exception e) { |
| 489 | log.error("Error submitting batches:", e); |
| 490 | // FIXME incomplete Intents should be cleaned up |
| 491 | // (transition to FAILED, etc.) |
| 492 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 493 | // the batch has failed |
| 494 | // TODO: maybe we should do more? |
| 495 | log.error("Walk the plank, matey..."); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 496 | //FIXME |
| 497 | // batchService.removeIntentOperations(ops); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame^] | 501 | private List<Future<IntentUpdate>> createIntentUpdates() { |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 502 | return ops.stream() |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame^] | 503 | .map(IntentManager.this::submitIntentData) |
| 504 | .collect(Collectors.toList()); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 505 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 506 | } |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 507 | |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame^] | 508 | private Future<IntentUpdate> submitIntentData(IntentData data) { |
| 509 | return workerExecutor.submit(new IntentWorker(data)); |
| 510 | } |
| 511 | |
| 512 | private class IntentWorker implements Callable<IntentUpdate> { |
| 513 | |
| 514 | private final IntentData data; |
| 515 | |
| 516 | private IntentWorker(IntentData data) { |
| 517 | this.data = data; |
| 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public IntentUpdate call() throws Exception { |
| 522 | IntentUpdate update = createIntentUpdate(data); |
| 523 | Optional<IntentUpdate> currentPhase = Optional.of(update); |
| 524 | IntentUpdate previousPhase = update; |
| 525 | |
| 526 | while (currentPhase.isPresent()) { |
| 527 | previousPhase = currentPhase.get(); |
| 528 | currentPhase = previousPhase.execute(); |
| 529 | } |
| 530 | return previousPhase; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 535 | // TODO: better naming |
| 536 | private class IntentBatchApplyFirst extends IntentBatchPreprocess { |
| 537 | |
| 538 | protected final List<CompletedIntentUpdate> intentUpdates; |
| 539 | protected final int installAttempt; |
| 540 | protected Future<CompletedBatchOperation> future; |
| 541 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 542 | IntentBatchApplyFirst(Collection<IntentData> operations, List<CompletedIntentUpdate> intentUpdates, |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 543 | long endTime, int installAttempt, Future<CompletedBatchOperation> future) { |
| 544 | super(operations, endTime); |
| 545 | this.intentUpdates = ImmutableList.copyOf(intentUpdates); |
| 546 | this.future = future; |
| 547 | this.installAttempt = installAttempt; |
| 548 | } |
| 549 | |
| 550 | @Override |
| 551 | public void run() { |
| 552 | Future<CompletedBatchOperation> future = applyNextBatch(intentUpdates); |
| 553 | new IntentBatchProcessFutures(ops, intentUpdates, endTime, installAttempt, future).run(); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 556 | /** |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 557 | * Builds and applies the next batch, and returns the future. |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 558 | * |
| 559 | * @return Future for next batch |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 560 | */ |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 561 | protected Future<CompletedBatchOperation> applyNextBatch(List<CompletedIntentUpdate> updates) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 562 | //TODO test this. (also, maybe save this batch) |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 563 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 564 | FlowRuleBatchOperation batch = createFlowRuleBatchOperation(updates); |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 565 | if (batch.size() > 0) { |
| 566 | //FIXME apply batch might throw an exception |
| 567 | return flowRuleService.applyBatch(batch); |
| 568 | } else { |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 569 | return null; |
| 570 | } |
| 571 | } |
| 572 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 573 | private FlowRuleBatchOperation createFlowRuleBatchOperation(List<CompletedIntentUpdate> intentUpdates) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 574 | FlowRuleBatchOperation batch = new FlowRuleBatchOperation(Collections.emptyList(), null, 0); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 575 | for (CompletedIntentUpdate update : intentUpdates) { |
| 576 | FlowRuleBatchOperation currentBatch = update.currentBatch(); |
| 577 | if (currentBatch != null) { |
| 578 | batch.addAll(currentBatch); |
| 579 | } |
| 580 | } |
| 581 | return batch; |
| 582 | } |
| 583 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 584 | protected void abandonShip() { |
| 585 | // the batch has failed |
| 586 | // TODO: maybe we should do more? |
| 587 | log.error("Walk the plank, matey..."); |
| 588 | future = null; |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 589 | //FIXME |
| 590 | // batchService.removeIntentOperations(ops); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
| 594 | // TODO: better naming |
| 595 | private class IntentBatchProcessFutures extends IntentBatchApplyFirst { |
| 596 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 597 | IntentBatchProcessFutures(Collection<IntentData> operations, List<CompletedIntentUpdate> intentUpdates, |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 598 | long endTime, int installAttempt, Future<CompletedBatchOperation> future) { |
| 599 | super(operations, intentUpdates, endTime, installAttempt, future); |
| 600 | } |
| 601 | |
| 602 | @Override |
| 603 | public void run() { |
| 604 | try { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 605 | Future<CompletedBatchOperation> future = processFutures(); |
| 606 | if (future == null) { |
| 607 | // there are no outstanding batches; we are done |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 608 | //FIXME |
| 609 | return; //? |
| 610 | // batchService.removeIntentOperations(ops); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 611 | } else if (System.currentTimeMillis() > endTime) { |
| 612 | // - cancel current FlowRuleBatch and resubmit again |
| 613 | retry(); |
| 614 | } else { |
| 615 | // we are not done yet, yield the thread by resubmitting ourselves |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame^] | 616 | batchExecutor.submit(new IntentBatchProcessFutures(ops, intentUpdates, endTime, installAttempt, future)); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 617 | } |
| 618 | } catch (Exception e) { |
| 619 | log.error("Error submitting batches:", e); |
| 620 | // FIXME incomplete Intents should be cleaned up |
| 621 | // (transition to FAILED, etc.) |
| 622 | abandonShip(); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * Iterate through the pending futures, and remove them when they have completed. |
| 628 | */ |
| 629 | private Future<CompletedBatchOperation> processFutures() { |
| 630 | try { |
| 631 | CompletedBatchOperation completed = future.get(100, TimeUnit.NANOSECONDS); |
| 632 | updateBatches(completed); |
| 633 | return applyNextBatch(intentUpdates); |
| 634 | } catch (TimeoutException | InterruptedException te) { |
| 635 | log.trace("Installation of intents are still pending: {}", ops); |
| 636 | return future; |
| 637 | } catch (ExecutionException e) { |
| 638 | log.warn("Execution of batch failed: {}", ops, e); |
| 639 | abandonShip(); |
| 640 | return future; |
| 641 | } |
| 642 | } |
| 643 | |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 644 | private void updateBatches(CompletedBatchOperation completed) { |
| 645 | if (completed.isSuccess()) { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 646 | for (CompletedIntentUpdate update : intentUpdates) { |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 647 | update.batchSuccess(); |
| 648 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 649 | } else { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 650 | // entire batch has been reverted... |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 651 | log.debug("Failed items: {}", completed.failedItems()); |
| 652 | log.debug("Failed ids: {}", completed.failedIds()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 653 | |
| 654 | for (Long id : completed.failedIds()) { |
| 655 | IntentId targetId = IntentId.valueOf(id); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 656 | for (CompletedIntentUpdate update : intentUpdates) { |
| 657 | for (Intent intent : update.allInstallables()) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 658 | if (intent.id().equals(targetId)) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 659 | update.batchFailed(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 660 | break; |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | // don't increment the non-failed items, as they have been reverted. |
| 665 | } |
| 666 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 669 | private void retry() { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 670 | log.debug("Execution timed out, retrying."); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 671 | if (future.cancel(true)) { // cancel success; batch is reverted |
| 672 | // reset the timer |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 673 | long timeLimit = calculateTimeoutLimit(); |
| 674 | int attempts = installAttempt + 1; |
| 675 | if (attempts == MAX_ATTEMPTS) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 676 | log.warn("Install request timed out: {}", ops); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 677 | for (CompletedIntentUpdate update : intentUpdates) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 678 | update.batchFailed(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 679 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 680 | } else if (attempts > MAX_ATTEMPTS) { |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 681 | abandonShip(); |
| 682 | return; |
Sho SHIMIZU | 55a5518 | 2015-02-03 22:32:56 -0800 | [diff] [blame] | 683 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 684 | Future<CompletedBatchOperation> future = applyNextBatch(intentUpdates); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame^] | 685 | batchExecutor.submit(new IntentBatchProcessFutures(ops, intentUpdates, timeLimit, attempts, future)); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 686 | } else { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 687 | log.error("Cancelling FlowRuleBatch failed."); |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 688 | abandonShip(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 689 | } |
| 690 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 691 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 692 | |
| 693 | private class InternalBatchDelegate implements IntentBatchDelegate { |
| 694 | @Override |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 695 | public void execute(Collection<IntentData> operations) { |
| 696 | log.info("Execute {} operation(s).", operations.size()); |
| 697 | log.debug("Execute operations: {}", operations); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame^] | 698 | batchExecutor.execute(new IntentBatchPreprocess(operations)); |
| 699 | // TODO ensure that only one batch is in flight at a time |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 700 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 701 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 702 | } |