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 | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.core.IdGenerator; |
| 27 | import org.onosproject.event.AbstractListenerRegistry; |
| 28 | import org.onosproject.event.EventDeliveryService; |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 29 | import org.onosproject.net.flow.FlowRuleOperations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.flow.FlowRuleService; |
| 31 | import org.onosproject.net.intent.Intent; |
| 32 | import org.onosproject.net.intent.IntentBatchDelegate; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.intent.IntentCompiler; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 34 | import org.onosproject.net.intent.IntentData; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.intent.IntentEvent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.IntentExtensionService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.intent.IntentInstaller; |
| 38 | import org.onosproject.net.intent.IntentListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.intent.IntentService; |
| 40 | import org.onosproject.net.intent.IntentState; |
| 41 | import org.onosproject.net.intent.IntentStore; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.intent.IntentStoreDelegate; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 43 | import org.onosproject.net.intent.Key; |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 44 | import org.onosproject.net.intent.impl.phase.CompilingFailed; |
| 45 | import org.onosproject.net.intent.impl.phase.FinalIntentProcessPhase; |
| 46 | import org.onosproject.net.intent.impl.phase.InstallRequest; |
| 47 | import org.onosproject.net.intent.impl.phase.IntentProcessPhase; |
| 48 | import org.onosproject.net.intent.impl.phase.WithdrawRequest; |
| 49 | import org.onosproject.net.intent.impl.phase.Withdrawn; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 50 | import org.slf4j.Logger; |
| 51 | |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 52 | import java.util.Collection; |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 53 | import java.util.EnumSet; |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 54 | import java.util.List; |
| 55 | import java.util.Map; |
| 56 | import java.util.Optional; |
| 57 | import java.util.concurrent.Callable; |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 58 | import java.util.concurrent.ExecutionException; |
| 59 | import java.util.concurrent.ExecutorService; |
| 60 | import java.util.concurrent.Future; |
| 61 | import java.util.stream.Collectors; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 62 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 63 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 64 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 65 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Brian O'Connor | bdc7f00 | 2015-02-18 20:49:41 -0800 | [diff] [blame] | 66 | import static org.onlab.util.Tools.groupedThreads; |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 67 | import static org.onlab.util.Tools.isNullOrEmpty; |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 68 | import static org.onosproject.net.intent.IntentState.FAILED; |
| 69 | import static org.onosproject.net.intent.IntentState.INSTALL_REQ; |
| 70 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
| 71 | import static org.onosproject.net.intent.IntentState.WITHDRAW_REQ; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 72 | import static org.slf4j.LoggerFactory.getLogger; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * An implementation of Intent Manager. |
| 76 | */ |
| 77 | @Component(immediate = true) |
| 78 | @Service |
| 79 | public class IntentManager |
| 80 | implements IntentService, IntentExtensionService { |
Sho SHIMIZU | 8b5051d | 2014-11-05 11:24:13 -0800 | [diff] [blame] | 81 | private static final Logger log = getLogger(IntentManager.class); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 82 | |
| 83 | public static final String INTENT_NULL = "Intent cannot be null"; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 84 | 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] | 85 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 86 | private static final int NUM_THREADS = 12; |
| 87 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 88 | private static final EnumSet<IntentState> RECOMPILE |
| 89 | = EnumSet.of(INSTALL_REQ, FAILED, WITHDRAW_REQ); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 90 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 91 | private final AbstractListenerRegistry<IntentEvent, IntentListener> |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 92 | listenerRegistry = new AbstractListenerRegistry<>(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 93 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 95 | protected CoreService coreService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 96 | |
| 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 98 | protected IntentStore store; |
| 99 | |
| 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 101 | protected ObjectiveTrackerService trackerService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 104 | protected EventDeliveryService eventDispatcher; |
| 105 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 106 | // TODO: make this protected due to short term hack for ONOS-1051 |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 108 | public FlowRuleService flowRuleService; |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 109 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 110 | |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 111 | private ExecutorService batchExecutor; |
| 112 | private ExecutorService workerExecutor; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 113 | |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 114 | private final CompilerRegistry compilerRegistry = new CompilerRegistry(); |
| 115 | private final InstallerRegistry installerRegistry = new InstallerRegistry(); |
| 116 | private final InternalIntentProcessor processor = new InternalIntentProcessor(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 117 | private final IntentStoreDelegate delegate = new InternalStoreDelegate(); |
| 118 | private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate(); |
| 119 | private final IntentBatchDelegate batchDelegate = new InternalBatchDelegate(); |
| 120 | private IdGenerator idGenerator; |
| 121 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 122 | private final IntentAccumulator accumulator = new IntentAccumulator(batchDelegate); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 123 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 124 | @Activate |
| 125 | public void activate() { |
| 126 | store.setDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 127 | trackerService.setDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 128 | eventDispatcher.addSink(IntentEvent.class, listenerRegistry); |
Brian O'Connor | bdc7f00 | 2015-02-18 20:49:41 -0800 | [diff] [blame] | 129 | batchExecutor = newSingleThreadExecutor(groupedThreads("onos/intent", "batch")); |
| 130 | workerExecutor = newFixedThreadPool(NUM_THREADS, groupedThreads("onos/intent", "worker-%d")); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 131 | idGenerator = coreService.getIdGenerator("intent-ids"); |
| 132 | Intent.bindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 133 | log.info("Started"); |
| 134 | } |
| 135 | |
| 136 | @Deactivate |
| 137 | public void deactivate() { |
| 138 | store.unsetDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 139 | trackerService.unsetDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 140 | eventDispatcher.removeSink(IntentEvent.class); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 141 | batchExecutor.shutdown(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 142 | Intent.unbindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 143 | log.info("Stopped"); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void submit(Intent intent) { |
| 148 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 149 | IntentData data = new IntentData(intent, IntentState.INSTALL_REQ, null); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 150 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void withdraw(Intent intent) { |
| 155 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 156 | IntentData data = new IntentData(intent, IntentState.WITHDRAW_REQ, null); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 157 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 160 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 161 | public Intent getIntent(Key key) { |
| 162 | return store.getIntent(key); |
| 163 | } |
| 164 | |
| 165 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 166 | public Iterable<Intent> getIntents() { |
| 167 | return store.getIntents(); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public long getIntentCount() { |
| 172 | return store.getIntentCount(); |
| 173 | } |
| 174 | |
| 175 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 176 | public IntentState getIntentState(Key intentKey) { |
| 177 | checkNotNull(intentKey, INTENT_ID_NULL); |
| 178 | return store.getIntentState(intentKey); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 182 | public List<Intent> getInstallableIntents(Key intentKey) { |
| 183 | checkNotNull(intentKey, INTENT_ID_NULL); |
| 184 | return store.getInstallableIntents(intentKey); |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | @Override |
Brian O'Connor | be28a87 | 2015-02-19 21:44:37 -0800 | [diff] [blame] | 188 | public boolean isLocal(Key intentKey) { |
| 189 | return store.isMaster(intentKey); |
| 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) { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 204 | compilerRegistry.registerCompiler(cls, compiler); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 209 | compilerRegistry.unregisterCompiler(cls); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 214 | return compilerRegistry.getCompilers(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 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) { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 219 | installerRegistry.registerInstaller(cls, installer); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 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) { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 224 | installerRegistry.unregisterInstaller(cls); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 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() { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 229 | return installerRegistry.getInstallers(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 232 | // Store delegate to re-post events emitted from the store. |
| 233 | private class InternalStoreDelegate implements IntentStoreDelegate { |
| 234 | @Override |
| 235 | public void notify(IntentEvent event) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 236 | eventDispatcher.post(event); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 237 | } |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 238 | |
| 239 | @Override |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 240 | public void process(IntentData data) { |
| 241 | accumulator.add(data); |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 242 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 245 | private void buildAndSubmitBatches(Iterable<Key> intentKeys, |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 246 | boolean compileAllFailed) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 247 | // Attempt recompilation of the specified intents first. |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 248 | for (Key key : intentKeys) { |
| 249 | Intent intent = store.getIntent(key); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 250 | if (intent == null) { |
| 251 | continue; |
| 252 | } |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 253 | submit(intent); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | if (compileAllFailed) { |
| 257 | // If required, compile all currently failed intents. |
| 258 | for (Intent intent : getIntents()) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 259 | IntentState state = getIntentState(intent.key()); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 260 | if (RECOMPILE.contains(state)) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 261 | if (state == WITHDRAW_REQ) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 262 | withdraw(intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 263 | } else { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 264 | submit(intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 265 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 270 | //FIXME |
| 271 | // for (ApplicationId appId : batches.keySet()) { |
| 272 | // if (batchService.isLocalLeader(appId)) { |
| 273 | // execute(batches.get(appId).build()); |
| 274 | // } |
| 275 | // } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 276 | } |
| 277 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 278 | // Topology change delegate |
| 279 | private class InternalTopoChangeDelegate implements TopologyChangeDelegate { |
| 280 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 281 | public void triggerCompile(Iterable<Key> intentKeys, |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 282 | boolean compileAllFailed) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 283 | buildAndSubmitBatches(intentKeys, compileAllFailed); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 284 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 285 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 286 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 287 | private IntentProcessPhase createIntentUpdate(IntentData intentData) { |
Sho SHIMIZU | 95a7baf | 2015-02-12 09:15:57 -0800 | [diff] [blame] | 288 | IntentData current = store.getIntentData(intentData.key()); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 289 | switch (intentData.state()) { |
| 290 | case INSTALL_REQ: |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 291 | return new InstallRequest(processor, intentData, Optional.ofNullable(current)); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 292 | case WITHDRAW_REQ: |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 293 | if (current == null || isNullOrEmpty(current.installables())) { |
Brian O'Connor | ab8ef82 | 2015-02-17 18:08:54 -0800 | [diff] [blame] | 294 | return new Withdrawn(intentData, WITHDRAWN); |
Sho SHIMIZU | 95a7baf | 2015-02-12 09:15:57 -0800 | [diff] [blame] | 295 | } else { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 296 | return new WithdrawRequest(processor, intentData, current); |
Sho SHIMIZU | 95a7baf | 2015-02-12 09:15:57 -0800 | [diff] [blame] | 297 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 298 | default: |
| 299 | // illegal state |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 300 | return new CompilingFailed(intentData); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 301 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 304 | private Future<FinalIntentProcessPhase> submitIntentData(IntentData data) { |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 305 | return workerExecutor.submit(new IntentWorker(data)); |
Sho SHIMIZU | 8d9d136 | 2015-02-04 12:28:15 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 308 | private class IntentBatchPreprocess implements Runnable { |
| 309 | |
| 310 | // TODO make this configurable |
| 311 | private static final int TIMEOUT_PER_OP = 500; // ms |
| 312 | protected static final int MAX_ATTEMPTS = 3; |
| 313 | |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 314 | protected final Collection<IntentData> data; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 315 | |
| 316 | // future holding current FlowRuleBatch installation result |
| 317 | protected final long startTime = System.currentTimeMillis(); |
| 318 | protected final long endTime; |
| 319 | |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 320 | private IntentBatchPreprocess(Collection<IntentData> data, long endTime) { |
| 321 | this.data = checkNotNull(data); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 322 | this.endTime = endTime; |
| 323 | } |
| 324 | |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 325 | public IntentBatchPreprocess(Collection<IntentData> data) { |
| 326 | this(data, System.currentTimeMillis() + data.size() * TIMEOUT_PER_OP); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // FIXME compute reasonable timeouts |
| 330 | protected long calculateTimeoutLimit() { |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 331 | return System.currentTimeMillis() + data.size() * TIMEOUT_PER_OP; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public void run() { |
| 336 | try { |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 337 | /* |
| 338 | 1. wrap each intentdata in a runnable and submit |
| 339 | 2. wait for completion of all the work |
| 340 | 3. accumulate results and submit batch write of IntentData to store |
| 341 | (we can also try to update these individually) |
| 342 | */ |
| 343 | submitUpdates(waitForFutures(createIntentUpdates())); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 344 | } catch (Exception e) { |
| 345 | log.error("Error submitting batches:", e); |
| 346 | // FIXME incomplete Intents should be cleaned up |
| 347 | // (transition to FAILED, etc.) |
| 348 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 349 | // the batch has failed |
| 350 | // TODO: maybe we should do more? |
| 351 | log.error("Walk the plank, matey..."); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 352 | //FIXME |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 353 | // batchService.removeIntentOperations(data); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 357 | private List<Future<FinalIntentProcessPhase>> createIntentUpdates() { |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 358 | return data.stream() |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 359 | .map(IntentManager.this::submitIntentData) |
| 360 | .collect(Collectors.toList()); |
| 361 | } |
| 362 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 363 | private List<FinalIntentProcessPhase> waitForFutures(List<Future<FinalIntentProcessPhase>> futures) { |
| 364 | ImmutableList.Builder<FinalIntentProcessPhase> updateBuilder = ImmutableList.builder(); |
| 365 | for (Future<FinalIntentProcessPhase> future : futures) { |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 366 | try { |
| 367 | updateBuilder.add(future.get()); |
| 368 | } catch (InterruptedException | ExecutionException e) { |
| 369 | //FIXME |
| 370 | log.warn("Future failed: {}", e); |
| 371 | } |
| 372 | } |
| 373 | return updateBuilder.build(); |
| 374 | } |
| 375 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 376 | private void submitUpdates(List<FinalIntentProcessPhase> updates) { |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 377 | store.batchWrite(updates.stream() |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 378 | .map(FinalIntentProcessPhase::data) |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 379 | .collect(Collectors.toList())); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 380 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 381 | } |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 382 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 383 | private final class IntentWorker implements Callable<FinalIntentProcessPhase> { |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 384 | |
| 385 | private final IntentData data; |
| 386 | |
| 387 | private IntentWorker(IntentData data) { |
| 388 | this.data = data; |
| 389 | } |
| 390 | |
| 391 | @Override |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 392 | public FinalIntentProcessPhase call() throws Exception { |
| 393 | IntentProcessPhase update = createIntentUpdate(data); |
| 394 | Optional<IntentProcessPhase> currentPhase = Optional.of(update); |
| 395 | IntentProcessPhase previousPhase = update; |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 396 | |
| 397 | while (currentPhase.isPresent()) { |
| 398 | previousPhase = currentPhase.get(); |
| 399 | currentPhase = previousPhase.execute(); |
| 400 | } |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 401 | return (FinalIntentProcessPhase) previousPhase; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 402 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 403 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 404 | |
| 405 | private class InternalBatchDelegate implements IntentBatchDelegate { |
| 406 | @Override |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 407 | public void execute(Collection<IntentData> operations) { |
Brian O'Connor | ab8ef82 | 2015-02-17 18:08:54 -0800 | [diff] [blame] | 408 | log.debug("Execute {} operation(s).", operations.size()); |
| 409 | log.trace("Execute operations: {}", operations); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 410 | batchExecutor.execute(new IntentBatchPreprocess(operations)); |
| 411 | // 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] | 412 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 413 | } |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 414 | |
| 415 | private class InternalIntentProcessor implements IntentProcessor { |
| 416 | @Override |
| 417 | public List<Intent> compile(Intent intent, List<Intent> previousInstallables) { |
| 418 | return compilerRegistry.compile(intent, previousInstallables); |
| 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public FlowRuleOperations coordinate(IntentData current, IntentData pending) { |
| 423 | return installerRegistry.coordinate(current, pending, store, trackerService); |
| 424 | } |
| 425 | |
| 426 | @Override |
| 427 | public FlowRuleOperations uninstallCoordinate(IntentData current, IntentData pending) { |
| 428 | return installerRegistry.uninstallCoordinate(current, pending, store, trackerService); |
| 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public void applyFlowRules(FlowRuleOperations flowRules) { |
| 433 | flowRuleService.apply(flowRules); |
| 434 | } |
| 435 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 436 | } |