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 | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableMap; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.core.IdGenerator; |
| 28 | import org.onosproject.event.AbstractListenerRegistry; |
| 29 | import org.onosproject.event.EventDeliveryService; |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 30 | import org.onosproject.net.flow.FlowRule; |
| 31 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.FlowRuleOperations; |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.FlowRuleService; |
| 36 | import org.onosproject.net.intent.Intent; |
| 37 | import org.onosproject.net.intent.IntentBatchDelegate; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.intent.IntentCompiler; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 39 | import org.onosproject.net.intent.IntentData; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.intent.IntentEvent; |
| 41 | import org.onosproject.net.intent.IntentException; |
| 42 | import org.onosproject.net.intent.IntentExtensionService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.net.intent.IntentInstaller; |
| 44 | import org.onosproject.net.intent.IntentListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import org.onosproject.net.intent.IntentService; |
| 46 | import org.onosproject.net.intent.IntentState; |
| 47 | import org.onosproject.net.intent.IntentStore; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 48 | import org.onosproject.net.intent.IntentStoreDelegate; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 49 | import org.onosproject.net.intent.Key; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 50 | import org.slf4j.Logger; |
| 51 | |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 52 | import java.util.ArrayList; |
| 53 | import java.util.Collection; |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 54 | import java.util.Collections; |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 55 | import java.util.EnumSet; |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 56 | import java.util.Iterator; |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 57 | import java.util.List; |
| 58 | import java.util.Map; |
| 59 | import java.util.Optional; |
| 60 | import java.util.concurrent.Callable; |
| 61 | import java.util.concurrent.ConcurrentHashMap; |
| 62 | import java.util.concurrent.ConcurrentMap; |
| 63 | import java.util.concurrent.ExecutionException; |
| 64 | import java.util.concurrent.ExecutorService; |
| 65 | import java.util.concurrent.Future; |
| 66 | import java.util.stream.Collectors; |
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; |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 69 | import static com.google.common.base.Preconditions.checkState; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 70 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 71 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 72 | import static org.onlab.util.Tools.isNullOrEmpty; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 73 | import static org.onlab.util.Tools.namedThreads; |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -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.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"; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 87 | public static final String INTENT_ID_NULL = "Intent key cannot be null"; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 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 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 94 | // Collections for compiler, installer, and listener are ONOS instance local |
| 95 | private final ConcurrentMap<Class<? extends Intent>, |
| 96 | IntentCompiler<? extends Intent>> compilers = new ConcurrentHashMap<>(); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 97 | private final ConcurrentMap<Class<? extends Intent>, |
| 98 | IntentInstaller<? extends Intent>> installers = new ConcurrentHashMap<>(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 99 | |
| 100 | private final AbstractListenerRegistry<IntentEvent, IntentListener> |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 101 | listenerRegistry = new AbstractListenerRegistry<>(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 102 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected CoreService coreService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected IntentStore store; |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 110 | protected ObjectiveTrackerService trackerService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 111 | |
| 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 113 | protected EventDeliveryService eventDispatcher; |
| 114 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 116 | protected FlowRuleService flowRuleService; |
| 117 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 118 | |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 119 | private ExecutorService batchExecutor; |
| 120 | private ExecutorService workerExecutor; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 121 | |
| 122 | private final IntentStoreDelegate delegate = new InternalStoreDelegate(); |
| 123 | private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate(); |
| 124 | private final IntentBatchDelegate batchDelegate = new InternalBatchDelegate(); |
| 125 | private IdGenerator idGenerator; |
| 126 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 127 | private final IntentAccumulator accumulator = new IntentAccumulator(batchDelegate); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 128 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 129 | @Activate |
| 130 | public void activate() { |
| 131 | store.setDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 132 | trackerService.setDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 133 | eventDispatcher.addSink(IntentEvent.class, listenerRegistry); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 134 | batchExecutor = newSingleThreadExecutor(namedThreads("onos-intent-batch")); |
| 135 | workerExecutor = newFixedThreadPool(NUM_THREADS, namedThreads("onos-intent-worker-%d")); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 136 | idGenerator = coreService.getIdGenerator("intent-ids"); |
| 137 | Intent.bindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 138 | log.info("Started"); |
| 139 | } |
| 140 | |
| 141 | @Deactivate |
| 142 | public void deactivate() { |
| 143 | store.unsetDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 144 | trackerService.unsetDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 145 | eventDispatcher.removeSink(IntentEvent.class); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 146 | batchExecutor.shutdown(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 147 | Intent.unbindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 148 | log.info("Stopped"); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void submit(Intent intent) { |
| 153 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 154 | IntentData data = new IntentData(intent, IntentState.INSTALL_REQ, null); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 155 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void withdraw(Intent intent) { |
| 160 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 161 | IntentData data = new IntentData(intent, IntentState.WITHDRAW_REQ, null); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 162 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 165 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 166 | public Intent getIntent(Key key) { |
| 167 | return store.getIntent(key); |
| 168 | } |
| 169 | |
| 170 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 171 | public Iterable<Intent> getIntents() { |
| 172 | return store.getIntents(); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public long getIntentCount() { |
| 177 | return store.getIntentCount(); |
| 178 | } |
| 179 | |
| 180 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 181 | public IntentState getIntentState(Key intentKey) { |
| 182 | checkNotNull(intentKey, INTENT_ID_NULL); |
| 183 | return store.getIntentState(intentKey); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 187 | public List<Intent> getInstallableIntents(Key intentKey) { |
| 188 | checkNotNull(intentKey, INTENT_ID_NULL); |
| 189 | return store.getInstallableIntents(intentKey); |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 193 | public void addListener(IntentListener listener) { |
| 194 | listenerRegistry.addListener(listener); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void removeListener(IntentListener listener) { |
| 199 | listenerRegistry.removeListener(listener); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) { |
| 204 | compilers.put(cls, compiler); |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
| 209 | compilers.remove(cls); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { |
| 214 | return ImmutableMap.copyOf(compilers); |
| 215 | } |
| 216 | |
| 217 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 218 | 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] | 219 | installers.put(cls, installer); |
| 220 | } |
| 221 | |
| 222 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 223 | public <T extends Intent> void unregisterInstaller(Class<T> cls) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 224 | installers.remove(cls); |
| 225 | } |
| 226 | |
| 227 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 228 | public Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> getInstallers() { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 229 | return ImmutableMap.copyOf(installers); |
| 230 | } |
| 231 | |
| 232 | /** |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 233 | * Returns the corresponding intent compiler to the specified intent. |
| 234 | * |
| 235 | * @param intent intent |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 236 | * @param <T> the type of intent |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 237 | * @return intent compiler corresponding to the specified intent |
| 238 | */ |
| 239 | private <T extends Intent> IntentCompiler<T> getCompiler(T intent) { |
| 240 | @SuppressWarnings("unchecked") |
| 241 | IntentCompiler<T> compiler = (IntentCompiler<T>) compilers.get(intent.getClass()); |
| 242 | if (compiler == null) { |
| 243 | throw new IntentException("no compiler for class " + intent.getClass()); |
| 244 | } |
| 245 | return compiler; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Returns the corresponding intent installer to the specified installable intent. |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 250 | * |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 251 | * @param intent intent |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 252 | * @param <T> the type of installable intent |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 253 | * @return intent installer corresponding to the specified installable intent |
| 254 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 255 | private <T extends Intent> IntentInstaller<T> getInstaller(T intent) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 256 | @SuppressWarnings("unchecked") |
| 257 | IntentInstaller<T> installer = (IntentInstaller<T>) installers.get(intent.getClass()); |
| 258 | if (installer == null) { |
| 259 | throw new IntentException("no installer for class " + intent.getClass()); |
| 260 | } |
| 261 | return installer; |
| 262 | } |
| 263 | |
| 264 | /** |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 265 | * Compiles an intent recursively. |
| 266 | * |
| 267 | * @param intent intent |
Jonathan Hart | d0ba217 | 2015-02-11 13:54:33 -0800 | [diff] [blame] | 268 | * @param previousInstallables previous intent installables |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 269 | * @return result of compilation |
| 270 | */ |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 271 | List<Intent> compileIntent(Intent intent, List<Intent> previousInstallables) { |
Thomas Vachuska | 4926c1b | 2014-10-21 00:44:10 -0700 | [diff] [blame] | 272 | if (intent.isInstallable()) { |
| 273 | return ImmutableList.of(intent); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 274 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 275 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 276 | registerSubclassCompilerIfNeeded(intent); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 277 | // FIXME: get previous resources |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 278 | List<Intent> installable = new ArrayList<>(); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 279 | for (Intent compiled : getCompiler(intent).compile(intent, previousInstallables, null)) { |
| 280 | installable.addAll(compileIntent(compiled, previousInstallables)); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 281 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 282 | return installable; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 285 | //TODO javadoc |
| 286 | //FIXME |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 287 | FlowRuleOperations coordinate(IntentData current, IntentData pending) { |
| 288 | List<Intent> oldInstallables = (current != null) ? current.installables() : null; |
| 289 | List<Intent> newInstallables = pending.installables(); |
| 290 | |
| 291 | checkState(isNullOrEmpty(oldInstallables) || |
| 292 | oldInstallables.size() == newInstallables.size(), |
| 293 | "Old and New Intent must have equivalent installable intents."); |
| 294 | |
| 295 | List<List<FlowRuleBatchOperation>> plans = new ArrayList<>(); |
| 296 | for (int i = 0; i < newInstallables.size(); i++) { |
| 297 | Intent newInstallable = newInstallables.get(i); |
| 298 | registerSubclassInstallerIfNeeded(newInstallable); |
Brian O'Connor | ea2ba3e | 2015-02-12 11:27:55 -0800 | [diff] [blame] | 299 | //TODO consider migrating installers to FlowRuleOperations |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 300 | /* FIXME |
| 301 | - we need to do another pass on this method about that doesn't |
| 302 | require the length of installables to be equal, and also doesn't |
| 303 | depend on ordering |
| 304 | - we should also reconsider when to start/stop tracking resources |
| 305 | */ |
| 306 | if (isNullOrEmpty(oldInstallables)) { |
| 307 | plans.add(getInstaller(newInstallable).install(newInstallable)); |
| 308 | } else { |
| 309 | Intent oldInstallable = oldInstallables.get(i); |
| 310 | checkState(oldInstallable.getClass().equals(newInstallable.getClass()), |
| 311 | "Installable Intent type mismatch."); |
| 312 | trackerService.removeTrackedResources(pending.key(), oldInstallable.resources()); |
| 313 | plans.add(getInstaller(newInstallable).replace(oldInstallable, newInstallable)); |
| 314 | } |
| 315 | trackerService.addTrackedResources(pending.key(), newInstallable.resources()); |
| 316 | // } catch (IntentException e) { |
| 317 | // log.warn("Unable to update intent {} due to:", oldIntent.id(), e); |
| 318 | // //FIXME... we failed. need to uninstall (if same) or revert (if different) |
| 319 | // trackerService.removeTrackedResources(newIntent.id(), newInstallable.resources()); |
| 320 | // exception = e; |
| 321 | // batches = uninstallIntent(oldIntent, oldInstallables); |
| 322 | // } |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 323 | } |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 324 | |
Brian O'Connor | ea2ba3e | 2015-02-12 11:27:55 -0800 | [diff] [blame] | 325 | return merge(plans).build(new FlowRuleOperationsContext() { // TODO move this out |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 326 | @Override |
| 327 | public void onSuccess(FlowRuleOperations ops) { |
Brian O'Connor | ab8ef82 | 2015-02-17 18:08:54 -0800 | [diff] [blame^] | 328 | log.debug("Completed installing: {}", pending.key()); |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 329 | pending.setState(INSTALLED); |
| 330 | store.write(pending); |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public void onError(FlowRuleOperations ops) { |
Brian O'Connor | ba1abbe | 2015-02-06 15:21:48 -0800 | [diff] [blame] | 335 | log.warn("Failed installation: {} {} on {}", pending.key(), |
| 336 | pending.intent(), ops); |
Brian O'Connor | ea2ba3e | 2015-02-12 11:27:55 -0800 | [diff] [blame] | 337 | //TODO store.write(pending.setState(BROKEN)); |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 338 | pending.setState(FAILED); |
| 339 | store.write(pending); |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 340 | } |
| 341 | }); |
| 342 | } |
| 343 | |
Sho SHIMIZU | 37a24a8 | 2015-02-09 09:12:21 -0800 | [diff] [blame] | 344 | /** |
| 345 | * Generate a {@link FlowRuleOperations} instance from the specified intent data. |
| 346 | * |
| 347 | * @param current intent data stored in the store |
| 348 | * @return flow rule operations |
| 349 | */ |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 350 | FlowRuleOperations uninstallCoordinate(IntentData current, IntentData pending) { |
Sho SHIMIZU | 37a24a8 | 2015-02-09 09:12:21 -0800 | [diff] [blame] | 351 | List<Intent> installables = current.installables(); |
| 352 | List<List<FlowRuleBatchOperation>> plans = new ArrayList<>(); |
| 353 | for (Intent installable : installables) { |
Sho SHIMIZU | b5cd581 | 2015-02-09 14:25:13 -0800 | [diff] [blame] | 354 | plans.add(getInstaller(installable).uninstall(installable)); |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 355 | trackerService.removeTrackedResources(pending.key(), installable.resources()); |
Sho SHIMIZU | 37a24a8 | 2015-02-09 09:12:21 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | return merge(plans).build(new FlowRuleOperationsContext() { |
| 359 | @Override |
| 360 | public void onSuccess(FlowRuleOperations ops) { |
Brian O'Connor | ab8ef82 | 2015-02-17 18:08:54 -0800 | [diff] [blame^] | 361 | log.debug("Completed withdrawing: {}", pending.key()); |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 362 | pending.setState(WITHDRAWN); |
| 363 | pending.setInstallables(Collections.emptyList()); |
| 364 | store.write(pending); |
Sho SHIMIZU | 37a24a8 | 2015-02-09 09:12:21 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | @Override |
| 368 | public void onError(FlowRuleOperations ops) { |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 369 | log.warn("Failed withdraw: {}", pending.key()); |
| 370 | pending.setState(FAILED); |
| 371 | store.write(pending); |
Sho SHIMIZU | 37a24a8 | 2015-02-09 09:12:21 -0800 | [diff] [blame] | 372 | } |
| 373 | }); |
| 374 | } |
| 375 | |
| 376 | |
Brian O'Connor | ea2ba3e | 2015-02-12 11:27:55 -0800 | [diff] [blame] | 377 | // TODO needs tests... or maybe it's just perfect |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 378 | private FlowRuleOperations.Builder merge(List<List<FlowRuleBatchOperation>> plans) { |
| 379 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
| 380 | // Build a batch one stage at a time |
| 381 | for (int stageNumber = 0;; stageNumber++) { |
| 382 | // Get the sub-stage from each plan (List<FlowRuleBatchOperation>) |
| 383 | for (Iterator<List<FlowRuleBatchOperation>> itr = plans.iterator(); itr.hasNext();) { |
| 384 | List<FlowRuleBatchOperation> plan = itr.next(); |
| 385 | if (plan.size() <= stageNumber) { |
| 386 | // we have consumed all stages from this plan, so remove it |
| 387 | itr.remove(); |
| 388 | continue; |
| 389 | } |
| 390 | // write operations from this sub-stage into the builder |
| 391 | FlowRuleBatchOperation stage = plan.get(stageNumber); |
| 392 | for (FlowRuleBatchEntry entry : stage.getOperations()) { |
| 393 | FlowRule rule = entry.target(); |
| 394 | switch (entry.operator()) { |
| 395 | case ADD: |
| 396 | builder.add(rule); |
| 397 | break; |
| 398 | case REMOVE: |
| 399 | builder.remove(rule); |
| 400 | break; |
| 401 | case MODIFY: |
| 402 | builder.modify(rule); |
| 403 | break; |
| 404 | default: |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | // we are done with the stage, start the next one... |
| 410 | if (plans.isEmpty()) { |
| 411 | break; // we don't need to start a new stage, we are done. |
| 412 | } |
| 413 | builder.newStage(); |
| 414 | } |
| 415 | return builder; |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 418 | /** |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 419 | * Registers an intent compiler of the specified intent if an intent compiler |
| 420 | * for the intent is not registered. This method traverses the class hierarchy of |
| 421 | * the intent. Once an intent compiler for a parent type is found, this method |
| 422 | * registers the found intent compiler. |
| 423 | * |
| 424 | * @param intent intent |
| 425 | */ |
| 426 | private void registerSubclassCompilerIfNeeded(Intent intent) { |
| 427 | if (!compilers.containsKey(intent.getClass())) { |
| 428 | Class<?> cls = intent.getClass(); |
| 429 | while (cls != Object.class) { |
| 430 | // As long as we're within the Intent class descendants |
| 431 | if (Intent.class.isAssignableFrom(cls)) { |
| 432 | IntentCompiler<?> compiler = compilers.get(cls); |
| 433 | if (compiler != null) { |
| 434 | compilers.put(intent.getClass(), compiler); |
| 435 | return; |
| 436 | } |
| 437 | } |
| 438 | cls = cls.getSuperclass(); |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Registers an intent installer of the specified intent if an intent installer |
| 445 | * for the intent is not registered. This method traverses the class hierarchy of |
| 446 | * the intent. Once an intent installer for a parent type is found, this method |
| 447 | * registers the found intent installer. |
| 448 | * |
| 449 | * @param intent intent |
| 450 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 451 | private void registerSubclassInstallerIfNeeded(Intent intent) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 452 | if (!installers.containsKey(intent.getClass())) { |
| 453 | Class<?> cls = intent.getClass(); |
| 454 | while (cls != Object.class) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 455 | // As long as we're within the Intent class descendants |
| 456 | if (Intent.class.isAssignableFrom(cls)) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 457 | IntentInstaller<?> installer = installers.get(cls); |
| 458 | if (installer != null) { |
| 459 | installers.put(intent.getClass(), installer); |
| 460 | return; |
| 461 | } |
| 462 | } |
| 463 | cls = cls.getSuperclass(); |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 468 | // Store delegate to re-post events emitted from the store. |
| 469 | private class InternalStoreDelegate implements IntentStoreDelegate { |
| 470 | @Override |
| 471 | public void notify(IntentEvent event) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 472 | eventDispatcher.post(event); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 473 | } |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 474 | |
| 475 | @Override |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 476 | public void process(IntentData data) { |
| 477 | accumulator.add(data); |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 478 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 481 | private void buildAndSubmitBatches(Iterable<Key> intentKeys, |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 482 | boolean compileAllFailed) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 483 | // Attempt recompilation of the specified intents first. |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 484 | for (Key key : intentKeys) { |
| 485 | Intent intent = store.getIntent(key); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 486 | if (intent == null) { |
| 487 | continue; |
| 488 | } |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 489 | submit(intent); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | if (compileAllFailed) { |
| 493 | // If required, compile all currently failed intents. |
| 494 | for (Intent intent : getIntents()) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 495 | IntentState state = getIntentState(intent.key()); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 496 | if (RECOMPILE.contains(state)) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 497 | if (state == WITHDRAW_REQ) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 498 | withdraw(intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 499 | } else { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 500 | submit(intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 501 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 506 | //FIXME |
| 507 | // for (ApplicationId appId : batches.keySet()) { |
| 508 | // if (batchService.isLocalLeader(appId)) { |
| 509 | // execute(batches.get(appId).build()); |
| 510 | // } |
| 511 | // } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 512 | } |
| 513 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 514 | // Topology change delegate |
| 515 | private class InternalTopoChangeDelegate implements TopologyChangeDelegate { |
| 516 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 517 | public void triggerCompile(Iterable<Key> intentKeys, |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 518 | boolean compileAllFailed) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 519 | buildAndSubmitBatches(intentKeys, compileAllFailed); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 520 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 521 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 522 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 523 | private IntentUpdate createIntentUpdate(IntentData intentData) { |
Sho SHIMIZU | 95a7baf | 2015-02-12 09:15:57 -0800 | [diff] [blame] | 524 | IntentData current = store.getIntentData(intentData.key()); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 525 | switch (intentData.state()) { |
| 526 | case INSTALL_REQ: |
Sho SHIMIZU | 95a7baf | 2015-02-12 09:15:57 -0800 | [diff] [blame] | 527 | return new InstallRequest(this, intentData, Optional.ofNullable(current)); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 528 | case WITHDRAW_REQ: |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 529 | if (current == null || isNullOrEmpty(current.installables())) { |
Brian O'Connor | ab8ef82 | 2015-02-17 18:08:54 -0800 | [diff] [blame^] | 530 | return new Withdrawn(intentData, WITHDRAWN); |
Sho SHIMIZU | 95a7baf | 2015-02-12 09:15:57 -0800 | [diff] [blame] | 531 | } else { |
| 532 | return new WithdrawRequest(this, intentData, current); |
| 533 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 534 | default: |
| 535 | // illegal state |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 536 | return new CompilingFailed(intentData); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 537 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 540 | private Future<CompletedIntentUpdate> submitIntentData(IntentData data) { |
| 541 | return workerExecutor.submit(new IntentWorker(data)); |
Sho SHIMIZU | 8d9d136 | 2015-02-04 12:28:15 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 544 | private class IntentBatchPreprocess implements Runnable { |
| 545 | |
| 546 | // TODO make this configurable |
| 547 | private static final int TIMEOUT_PER_OP = 500; // ms |
| 548 | protected static final int MAX_ATTEMPTS = 3; |
| 549 | |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 550 | protected final Collection<IntentData> data; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 551 | |
| 552 | // future holding current FlowRuleBatch installation result |
| 553 | protected final long startTime = System.currentTimeMillis(); |
| 554 | protected final long endTime; |
| 555 | |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 556 | private IntentBatchPreprocess(Collection<IntentData> data, long endTime) { |
| 557 | this.data = checkNotNull(data); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 558 | this.endTime = endTime; |
| 559 | } |
| 560 | |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 561 | public IntentBatchPreprocess(Collection<IntentData> data) { |
| 562 | this(data, System.currentTimeMillis() + data.size() * TIMEOUT_PER_OP); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | // FIXME compute reasonable timeouts |
| 566 | protected long calculateTimeoutLimit() { |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 567 | return System.currentTimeMillis() + data.size() * TIMEOUT_PER_OP; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | @Override |
| 571 | public void run() { |
| 572 | try { |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 573 | /* |
| 574 | 1. wrap each intentdata in a runnable and submit |
| 575 | 2. wait for completion of all the work |
| 576 | 3. accumulate results and submit batch write of IntentData to store |
| 577 | (we can also try to update these individually) |
| 578 | */ |
| 579 | submitUpdates(waitForFutures(createIntentUpdates())); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 580 | } catch (Exception e) { |
| 581 | log.error("Error submitting batches:", e); |
| 582 | // FIXME incomplete Intents should be cleaned up |
| 583 | // (transition to FAILED, etc.) |
| 584 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 585 | // the batch has failed |
| 586 | // TODO: maybe we should do more? |
| 587 | log.error("Walk the plank, matey..."); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 588 | //FIXME |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 589 | // batchService.removeIntentOperations(data); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 593 | private List<Future<CompletedIntentUpdate>> createIntentUpdates() { |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 594 | return data.stream() |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 595 | .map(IntentManager.this::submitIntentData) |
| 596 | .collect(Collectors.toList()); |
| 597 | } |
| 598 | |
| 599 | private List<CompletedIntentUpdate> waitForFutures(List<Future<CompletedIntentUpdate>> futures) { |
| 600 | ImmutableList.Builder<CompletedIntentUpdate> updateBuilder = ImmutableList.builder(); |
| 601 | for (Future<CompletedIntentUpdate> future : futures) { |
| 602 | try { |
| 603 | updateBuilder.add(future.get()); |
| 604 | } catch (InterruptedException | ExecutionException e) { |
| 605 | //FIXME |
| 606 | log.warn("Future failed: {}", e); |
| 607 | } |
| 608 | } |
| 609 | return updateBuilder.build(); |
| 610 | } |
| 611 | |
| 612 | private void submitUpdates(List<CompletedIntentUpdate> updates) { |
| 613 | store.batchWrite(updates.stream() |
| 614 | .map(CompletedIntentUpdate::data) |
| 615 | .collect(Collectors.toList())); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 616 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 617 | } |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 618 | |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 619 | private final class IntentWorker implements Callable<CompletedIntentUpdate> { |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 620 | |
| 621 | private final IntentData data; |
| 622 | |
| 623 | private IntentWorker(IntentData data) { |
| 624 | this.data = data; |
| 625 | } |
| 626 | |
| 627 | @Override |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 628 | public CompletedIntentUpdate call() throws Exception { |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 629 | IntentUpdate update = createIntentUpdate(data); |
| 630 | Optional<IntentUpdate> currentPhase = Optional.of(update); |
| 631 | IntentUpdate previousPhase = update; |
| 632 | |
| 633 | while (currentPhase.isPresent()) { |
| 634 | previousPhase = currentPhase.get(); |
| 635 | currentPhase = previousPhase.execute(); |
| 636 | } |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 637 | return (CompletedIntentUpdate) previousPhase; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 638 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 639 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 640 | |
| 641 | private class InternalBatchDelegate implements IntentBatchDelegate { |
| 642 | @Override |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 643 | public void execute(Collection<IntentData> operations) { |
Brian O'Connor | ab8ef82 | 2015-02-17 18:08:54 -0800 | [diff] [blame^] | 644 | log.debug("Execute {} operation(s).", operations.size()); |
| 645 | log.trace("Execute operations: {}", operations); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 646 | batchExecutor.execute(new IntentBatchPreprocess(operations)); |
| 647 | // 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] | 648 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 649 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 650 | } |