jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | */ |
| 16 | package org.onosproject.workflow.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 19 | import org.onosproject.cluster.ClusterService; |
| 20 | import org.onosproject.cluster.LeadershipService; |
| 21 | import org.onosproject.cluster.NodeId; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.core.CoreService; |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 24 | import org.onosproject.store.service.StorageException; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 25 | import org.onosproject.workflow.api.DefaultWorkplace; |
| 26 | import org.onosproject.workflow.api.EventHintSupplier; |
| 27 | import org.onosproject.workflow.api.EventTask; |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 28 | import org.onosproject.workflow.api.JsonDataModelInjector; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 29 | import org.onosproject.workflow.api.JsonDataModelTree; |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 30 | import org.onosproject.workflow.api.ProgramCounter; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 31 | import org.onosproject.workflow.api.SystemWorkflowContext; |
| 32 | import org.onosproject.workflow.api.EventTimeoutTask; |
| 33 | import org.onosproject.workflow.api.TimeoutTask; |
| 34 | import org.onosproject.workflow.api.TimerChain; |
nitinanand | c8b7025 | 2019-04-17 15:35:43 +0530 | [diff] [blame] | 35 | import org.onosproject.workflow.api.TriggerWorklet; |
| 36 | import org.onosproject.workflow.api.WorkflowEventMetaData; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 37 | import org.onosproject.workflow.api.Worklet; |
| 38 | import org.onosproject.workflow.api.Workflow; |
| 39 | import org.onosproject.workflow.api.WorkflowContext; |
| 40 | import org.onosproject.workflow.api.WorkflowData; |
| 41 | import org.onosproject.workflow.api.ContextEventMapStore; |
| 42 | import org.onosproject.workflow.api.WorkflowState; |
| 43 | import org.onosproject.workflow.api.WorkflowStore; |
| 44 | import org.onosproject.workflow.api.WorkflowBatchDelegate; |
| 45 | import org.onosproject.workflow.api.WorkflowDataEvent; |
| 46 | import org.onosproject.workflow.api.WorkflowDataListener; |
| 47 | import org.onosproject.workflow.api.WorkflowException; |
| 48 | import org.onosproject.workflow.api.HandlerTask; |
| 49 | import org.onosproject.workflow.api.HandlerTaskBatchDelegate; |
| 50 | import org.onosproject.workflow.api.Workplace; |
| 51 | import org.onosproject.workflow.api.WorkplaceStore; |
| 52 | import org.onosproject.workflow.api.WorkplaceStoreDelegate; |
| 53 | import org.onosproject.workflow.api.WorkflowExecutionService; |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 54 | import org.onosproject.workflow.api.WorkletDescription; |
| 55 | import org.onosproject.workflow.api.StaticDataModelInjector; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 56 | import org.onosproject.event.AbstractListenerManager; |
| 57 | import org.onosproject.event.Event; |
| 58 | import org.onosproject.net.intent.WorkPartitionService; |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 59 | import org.osgi.service.component.annotations.Activate; |
| 60 | import org.osgi.service.component.annotations.Component; |
| 61 | import org.osgi.service.component.annotations.Deactivate; |
| 62 | import org.osgi.service.component.annotations.Reference; |
| 63 | import org.osgi.service.component.annotations.ReferenceCardinality; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 64 | import org.slf4j.Logger; |
| 65 | |
| 66 | import java.util.Collection; |
| 67 | import java.util.List; |
| 68 | import java.util.Map; |
| 69 | import java.util.Objects; |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 70 | import java.util.Set; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 71 | import java.util.UUID; |
| 72 | import java.util.concurrent.CompletableFuture; |
| 73 | import java.util.concurrent.ExecutorService; |
| 74 | import java.util.concurrent.ScheduledExecutorService; |
| 75 | import java.util.stream.Collectors; |
| 76 | |
| 77 | import static java.util.concurrent.Executors.newFixedThreadPool; |
| 78 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 79 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 80 | import static org.onlab.util.Tools.groupedThreads; |
jaegonkim | 566991c | 2020-03-08 08:52:23 +0900 | [diff] [blame^] | 81 | import static org.onosproject.workflow.api.CheckCondition.check; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 82 | import static org.onosproject.workflow.api.WorkflowAttribute.REMOVE_AFTER_COMPLETE; |
| 83 | import static org.slf4j.LoggerFactory.getLogger; |
| 84 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 85 | @Component(immediate = true, service = WorkflowExecutionService.class) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 86 | public class WorkFlowEngine extends AbstractListenerManager<WorkflowDataEvent, WorkflowDataListener> |
| 87 | implements WorkflowExecutionService { |
| 88 | |
| 89 | protected static final Logger log = getLogger(WorkFlowEngine.class); |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 91 | protected CoreService coreService; |
| 92 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 94 | protected ClusterService clusterService; |
| 95 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 97 | protected LeadershipService leadershipService; |
| 98 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 100 | protected WorkPartitionService partitionService; |
| 101 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 103 | protected WorkplaceStore workplaceStore; |
| 104 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 106 | protected WorkflowStore workflowStore; |
| 107 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 109 | protected ContextEventMapStore eventMapStore; |
| 110 | |
| 111 | private final WorkplaceStoreDelegate workplaceStoreDelegate = this::post; |
| 112 | |
| 113 | private final WorkflowBatchDelegate workflowBatchDelegate = new InternalWorkflowBatchDelegate(); |
| 114 | private final WorkflowAccumulator workflowAccumulator = new WorkflowAccumulator(workflowBatchDelegate); |
| 115 | |
| 116 | private final HandlerTaskBatchDelegate eventtaskBatchDelegate = new InternalHandlerTaskBatchDelegate(); |
| 117 | private final HandlerTaskAccumulator eventtaskAccumulator = new HandlerTaskAccumulator(eventtaskBatchDelegate); |
| 118 | |
| 119 | private ExecutorService workflowBatchExecutor; |
| 120 | private ExecutorService workflowExecutor; |
| 121 | |
| 122 | private ExecutorService handlerTaskBatchExecutor; |
| 123 | private ExecutorService handlerTaskExecutor; |
| 124 | |
| 125 | private static final int DEFAULT_WORKFLOW_THREADS = 12; |
| 126 | private static final int DEFAULT_EVENTTASK_THREADS = 12; |
| 127 | private static final int MAX_REGISTER_EVENTMAP_WAITS = 10; |
| 128 | |
| 129 | private ScheduledExecutorService eventMapTriggerExecutor; |
| 130 | |
| 131 | private TimerChain timerChain = new TimerChain(); |
| 132 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 133 | private JsonDataModelInjector dataModelInjector = new JsonDataModelInjector(); |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 134 | private StaticDataModelInjector staticDataModelInjector = new StaticDataModelInjector(); |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 135 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 136 | public static final String APPID = "org.onosproject.workflow"; |
| 137 | private ApplicationId appId; |
| 138 | private NodeId localNodeId; |
| 139 | |
| 140 | @Activate |
| 141 | public void activate() { |
| 142 | appId = coreService.registerApplication(APPID); |
| 143 | workplaceStore.setDelegate(workplaceStoreDelegate); |
| 144 | localNodeId = clusterService.getLocalNode().id(); |
| 145 | leadershipService.runForLeadership(appId.name()); |
| 146 | |
| 147 | workflowBatchExecutor = newSingleThreadExecutor( |
| 148 | groupedThreads("onos/workflow", "workflow-batch", log)); |
| 149 | workflowExecutor = newFixedThreadPool(DEFAULT_WORKFLOW_THREADS, |
| 150 | groupedThreads("onos/workflow-exec", "worker-%d", log)); |
| 151 | handlerTaskBatchExecutor = newSingleThreadExecutor( |
| 152 | groupedThreads("onos/workflow", "handlertask-batch", log)); |
| 153 | handlerTaskExecutor = newFixedThreadPool(DEFAULT_EVENTTASK_THREADS, |
| 154 | groupedThreads("onos/handlertask-exec", "worker-%d", log)); |
| 155 | eventMapTriggerExecutor = newSingleThreadScheduledExecutor( |
| 156 | groupedThreads("onos/workflow-engine", "eventmap-trigger-executor")); |
| 157 | |
| 158 | (new WorkplaceWorkflow(this, workflowStore)).registerWorkflows(); |
| 159 | JsonDataModelTree data = new JsonDataModelTree(JsonNodeFactory.instance.objectNode()); |
| 160 | workplaceStore.registerWorkplace(Workplace.SYSTEM_WORKPLACE, |
| 161 | new DefaultWorkplace(Workplace.SYSTEM_WORKPLACE, data)); |
| 162 | |
| 163 | log.info("Started"); |
| 164 | } |
| 165 | |
| 166 | @Deactivate |
| 167 | public void deactivate() { |
| 168 | leadershipService.withdraw(appId.name()); |
| 169 | workplaceStore.unsetDelegate(workplaceStoreDelegate); |
| 170 | workflowBatchExecutor.shutdown(); |
| 171 | workflowExecutor.shutdown(); |
| 172 | handlerTaskBatchExecutor.shutdown(); |
| 173 | handlerTaskExecutor.shutdown(); |
| 174 | eventMapTriggerExecutor.shutdown(); |
| 175 | log.info("Stopped"); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void execInitWorklet(WorkflowContext context) { |
| 180 | |
| 181 | Workflow workflow = workflowStore.get(context.workflowId()); |
| 182 | if (workflow == null) { |
| 183 | log.error("Invalid workflow {}", context.workflowId()); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | initWorkletExecution(context); |
| 188 | try { |
| 189 | Worklet initWorklet = workflow.init(context); |
| 190 | if (initWorklet != null) { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 191 | |
| 192 | log.info("{} worklet.process:{}", context.name(), initWorklet.tag()); |
| 193 | log.trace("{} context: {}", context.name(), context); |
| 194 | |
| 195 | dataModelInjector.inject(initWorklet, context); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 196 | initWorklet.process(context); |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 197 | dataModelInjector.inhale(initWorklet, context); |
| 198 | |
| 199 | log.info("{} worklet.process(done): {}", context.name(), initWorklet.tag()); |
| 200 | log.trace("{} context: {}", context.name(), context); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 201 | } |
| 202 | |
jaegonkim | 566991c | 2020-03-08 08:52:23 +0900 | [diff] [blame^] | 203 | check(workplaceStore.getContext(context.name()) == null, |
| 204 | "Duplicated workflow context(" + context.name() + ") assignment."); |
| 205 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 206 | } catch (WorkflowException e) { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 207 | log.error("Exception: ", e); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 208 | context.setCause(e.getMessage()); |
| 209 | context.setState(WorkflowState.EXCEPTION); |
| 210 | workplaceStore.commitContext(context.name(), context, false); |
| 211 | return; |
| 212 | } |
| 213 | // trigger the execution of next worklet. |
| 214 | workplaceStore.registerContext(context.name(), context); |
| 215 | } |
| 216 | |
| 217 | @Override |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 218 | public void eval(String contextName) { |
| 219 | |
| 220 | final WorkflowContext latestContext = workplaceStore.getContext(contextName); |
| 221 | if (latestContext == null) { |
| 222 | log.error("Invalid workflow context {}", contextName); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | initWorkletExecution(latestContext); |
| 227 | |
| 228 | workplaceStore.commitContext(latestContext.name(), latestContext, true); |
nitinanand | c8b7025 | 2019-04-17 15:35:43 +0530 | [diff] [blame] | 229 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Override |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 233 | public void eventMapTrigger(Event event, EventHintSupplier supplier) { |
| 234 | |
| 235 | if (event.subject() instanceof SystemWorkflowContext) { |
| 236 | return; |
| 237 | } |
| 238 | |
nitinanand | c8b7025 | 2019-04-17 15:35:43 +0530 | [diff] [blame] | 239 | Map<String, WorkflowEventMetaData> eventMap; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 240 | |
| 241 | String eventHint; |
| 242 | try { |
| 243 | eventHint = supplier.apply(event); |
| 244 | } catch (Throwable e) { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 245 | log.error("Exception: ", e); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 246 | return; |
| 247 | } |
nitinanand | c8b7025 | 2019-04-17 15:35:43 +0530 | [diff] [blame] | 248 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 249 | if (eventHint == null) { |
| 250 | // do nothing |
| 251 | log.error("Invalid eventHint, event: {}", event); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | try { |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 256 | eventMap = eventMapStore.getEventMapByHint(event.getClass().getName(), eventHint); |
nitinanand | c8b7025 | 2019-04-17 15:35:43 +0530 | [diff] [blame] | 257 | if (Objects.isNull(eventMap) || eventMap.isEmpty()) { |
| 258 | // do nothing; |
| 259 | log.debug("Invalid eventMap, event: {}", event); |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 260 | return; |
| 261 | } |
| 262 | |
nitinanand | c8b7025 | 2019-04-17 15:35:43 +0530 | [diff] [blame] | 263 | for (Map.Entry<String, WorkflowEventMetaData> entry : eventMap.entrySet()) { |
| 264 | String contextName = entry.getKey(); |
| 265 | ProgramCounter pc = ProgramCounter.valueOf("INVALID_WORKLET", 0); |
| 266 | WorkflowContext context = null; |
| 267 | |
| 268 | context = workplaceStore.getContext(contextName); |
| 269 | |
| 270 | if (Objects.isNull(context)) { |
| 271 | log.info("Invalid context: {}, event: {}", contextName, event); |
| 272 | continue; |
| 273 | } |
| 274 | |
| 275 | EventTask eventtask = null; |
| 276 | if (eventMapStore.isTriggerSet(event.getClass().getName(), eventHint, contextName)) { |
| 277 | try { |
| 278 | eventtask = EventTask.builder() |
| 279 | .event(event) |
| 280 | .eventHint(eventHint) |
| 281 | .context(context) |
| 282 | .programCounter(pc) |
| 283 | .build(); |
| 284 | } catch (WorkflowException e) { |
| 285 | log.error("Exception: ", e); |
| 286 | } |
| 287 | |
| 288 | log.debug("eventtaskAccumulator.add: task: {}", eventtask); |
| 289 | if (!Objects.isNull(eventtask)) { |
| 290 | eventtaskAccumulator.add(eventtask); |
| 291 | } |
| 292 | } |
| 293 | /*Both type of event is being scheduled here if applicable. |
| 294 | If worfklow trigger event is set but may not be a valid one for current event type, |
| 295 | then normal worklet event should be processed if applicable. But validity of workflow |
| 296 | trigger event would be checked later, so as of now both kind of event would be scheduled. |
| 297 | later while trigger event processing, its validity is found to be true, then worklet events |
| 298 | would be unregistered and eventually these events wont be processed.*/ |
| 299 | if (eventMapStore.isEventMapPresent(contextName)) { |
| 300 | try { |
| 301 | pc = entry.getValue().getProgramCounter(); |
| 302 | } catch (IllegalArgumentException e) { |
| 303 | log.error("Exception: ", e); |
| 304 | continue; |
| 305 | } |
| 306 | try { |
| 307 | eventtask = EventTask.builder() |
| 308 | .event(event) |
| 309 | .eventHint(eventHint) |
| 310 | .context(context) |
| 311 | .programCounter(pc) |
| 312 | .build(); |
| 313 | } catch (WorkflowException e) { |
| 314 | log.error("Exception: ", e); |
| 315 | } |
| 316 | |
| 317 | log.debug("eventtaskAccumulator.add: task: {}", eventtask); |
| 318 | if (!Objects.isNull(eventtask)) { |
| 319 | eventtaskAccumulator.add(eventtask); |
| 320 | } |
| 321 | |
| 322 | } |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 323 | } |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 324 | |
nitinanand | c8b7025 | 2019-04-17 15:35:43 +0530 | [diff] [blame] | 325 | } catch (WorkflowException we) { |
| 326 | log.error("Exception {} occured in fetching contexts for trigger event {}", we, event); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | @Override |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 331 | public void registerEventMap(Class<? extends Event> eventType, Set<String> eventHintSet, |
nitinanand | c8b7025 | 2019-04-17 15:35:43 +0530 | [diff] [blame] | 332 | String contextName, ProgramCounter programCounter) throws WorkflowException { |
| 333 | eventMapStore.registerEventMap(eventType.getName(), eventHintSet, contextName, programCounter); |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 334 | for (String eventHint : eventHintSet) { |
| 335 | for (int i = 0; i < MAX_REGISTER_EVENTMAP_WAITS; i++) { |
nitinanand | c8b7025 | 2019-04-17 15:35:43 +0530 | [diff] [blame] | 336 | Map<String, WorkflowEventMetaData> eventMap = |
| 337 | eventMapStore.getEventMapByHint(eventType.getName(), eventHint); |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 338 | if (eventMap != null && eventMap.containsKey(contextName)) { |
| 339 | break; |
| 340 | } |
| 341 | try { |
| 342 | log.info("sleep {}", i); |
| 343 | Thread.sleep(10L * (i + 1)); |
| 344 | } catch (InterruptedException e) { |
| 345 | log.error("Exception: ", e); |
| 346 | Thread.currentThread().interrupt(); |
| 347 | } |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 348 | } |
| 349 | } |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 350 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | @Override |
| 354 | protected void post(WorkflowDataEvent event) { |
| 355 | |
| 356 | if (event.subject() == null || !isRelevant(event.subject())) { |
| 357 | log.debug("ignore event {}", event); |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | // trigger next worklet selection |
| 362 | WorkflowData dataModelContainer = event.subject(); |
| 363 | switch (event.type()) { |
| 364 | case INSERT: |
| 365 | case UPDATE: |
| 366 | if (dataModelContainer.triggerNext()) { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 367 | log.debug("workflowAccumulator.add: {}", dataModelContainer); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 368 | workflowAccumulator.add(dataModelContainer); |
| 369 | } else { |
| 370 | log.debug("pass-workflowAccumulator.add: {}", dataModelContainer); |
| 371 | } |
| 372 | break; |
| 373 | case REMOVE: |
| 374 | break; |
| 375 | default: |
| 376 | } |
| 377 | |
| 378 | // trigger EventTask for WorkflowDataEvent |
| 379 | eventMapTriggerExecutor.submit( |
| 380 | () -> eventMapTrigger( |
| 381 | event, |
| 382 | // event hint supplier |
| 383 | (ev) -> { |
| 384 | if (ev == null || ev.subject() == null) { |
| 385 | return null; |
| 386 | } |
| 387 | |
| 388 | if (ev.subject() instanceof WorkflowData) { |
| 389 | return ((WorkflowData) ev.subject()).name(); |
| 390 | } else { |
| 391 | return null; |
| 392 | } |
| 393 | } |
| 394 | ) |
| 395 | ); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Checks whether this workflow data job is relevant to this ONOS node. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 400 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 401 | * @param job workflow data |
| 402 | * @return checking result |
| 403 | */ |
| 404 | private boolean isRelevant(WorkflowData job) { |
| 405 | // distributes event processing with work-partition |
| 406 | return partitionService.isMine(job.distributor(), this::stringHash); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Gets hash of the string. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 411 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 412 | * @param str string to get a hash |
| 413 | * @return hash value |
| 414 | */ |
| 415 | public Long stringHash(String str) { |
| 416 | return UUID.nameUUIDFromBytes(str.getBytes()).getMostSignificantBits(); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Class for handler task batch delegation. |
| 421 | */ |
| 422 | private class InternalHandlerTaskBatchDelegate implements HandlerTaskBatchDelegate { |
| 423 | @Override |
| 424 | public void execute(Collection<Collection<HandlerTask>> operations) { |
| 425 | log.debug("Execute {} operation(s).", operations.size()); |
| 426 | |
| 427 | CompletableFuture.runAsync(() -> { |
| 428 | List<CompletableFuture<Collection<HandlerTask>>> futures = operations.stream() |
| 429 | .map( |
| 430 | x -> CompletableFuture.completedFuture(x) |
| 431 | .thenApplyAsync(WorkFlowEngine.this::processHandlerTask, handlerTaskExecutor) |
| 432 | .exceptionally(e -> null) |
| 433 | ) |
| 434 | .collect(Collectors.toList()); |
| 435 | |
| 436 | // waiting the completion of futures |
| 437 | futures.parallelStream().forEach(x -> x.join()); |
| 438 | |
| 439 | }, handlerTaskBatchExecutor).exceptionally(e -> { |
| 440 | log.error("Error submitting batches:", e); |
| 441 | return null; |
| 442 | }).thenRun(eventtaskAccumulator::ready); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Initializes worklet execution. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 448 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 449 | * @param context workflow context |
| 450 | */ |
| 451 | private void initWorkletExecution(WorkflowContext context) { |
| 452 | context.setState(WorkflowState.RUNNING); |
| 453 | context.setCause(""); |
| 454 | context.setWorkflowExecutionService(this); |
| 455 | context.setWorkflowStore(workflowStore); |
| 456 | context.setWorkplaceStore(workplaceStore); |
nitinanand | c8b7025 | 2019-04-17 15:35:43 +0530 | [diff] [blame] | 457 | context.setEventMapStore(eventMapStore); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 458 | context.waitCompletion(null, null, null, 0L); |
| 459 | context.setTriggerNext(false); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Processes handler tasks. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 464 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 465 | * @param tasks handler tasks |
| 466 | * @return handler tasks processed |
| 467 | */ |
| 468 | private Collection<HandlerTask> processHandlerTask(Collection<HandlerTask> tasks) { |
| 469 | |
| 470 | for (HandlerTask task : tasks) { |
| 471 | if (task instanceof EventTimeoutTask) { |
| 472 | execEventTimeoutTask((EventTimeoutTask) task); |
| 473 | } else if (task instanceof TimeoutTask) { |
| 474 | execTimeoutTask((TimeoutTask) task); |
| 475 | } else if (task instanceof EventTask) { |
| 476 | execEventTask((EventTask) task); |
| 477 | } else { |
| 478 | log.error("Unsupported handler task {}", task); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | return null; |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Executes event task. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 487 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 488 | * @param task event task |
| 489 | * @return event task |
| 490 | */ |
| 491 | private EventTask execEventTask(EventTask task) { |
| 492 | |
nitinanand | c8b7025 | 2019-04-17 15:35:43 +0530 | [diff] [blame] | 493 | WorkflowContext context = (WorkflowContext) (task.context()); |
| 494 | String cxtName = context.name(); |
| 495 | try { |
| 496 | if (eventMapStore.isTriggerSet(task.event().getClass().getName(), task.eventHint(), cxtName)) { |
| 497 | WorkflowContext workflowContext = workplaceStore.getContext(cxtName); |
| 498 | Workflow workflow = workflowStore.get(workflowContext.workflowId()); |
| 499 | String triggerWorkletName = workflow.getTriggerWorkletClassName().get(); |
| 500 | Worklet worklet = workflow.getTriggerWorkletInstance(triggerWorkletName); |
| 501 | if (worklet instanceof TriggerWorklet) { |
| 502 | if (((TriggerWorklet) worklet).isTriggerValid(workflowContext, task.event())) { |
| 503 | if (Objects.nonNull(workflowContext.completionEventType())) { |
| 504 | eventMapStore.unregisterEventMap(workflowContext.completionEventType().getName(), |
| 505 | workflowContext.name()); |
| 506 | } |
| 507 | initWorkletExecution(workflowContext); |
| 508 | workflowContext.setCurrent(ProgramCounter.INIT_PC); |
| 509 | workplaceStore.commitContext(cxtName, workflowContext, true); |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | } catch (WorkflowException we) { |
| 515 | log.error("Error Occurred in validating trigger for eventType {} eventHint {} context name {}", |
| 516 | task.eventType(), task.eventHint(), cxtName); |
| 517 | } |
| 518 | |
| 519 | if (!eventMapStore.isEventMapPresent(cxtName)) { |
| 520 | log.trace("EventMap doesnt exist for taskcontext:{}", cxtName); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 521 | return task; |
| 522 | } |
| 523 | |
| 524 | log.debug("execEventTask- task: {}, hash: {}", task, stringHash(task.context().distributor())); |
| 525 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 526 | Workflow workflow = workflowStore.get(context.workflowId()); |
| 527 | if (workflow == null) { |
| 528 | log.error("Invalid workflow {}", context.workflowId()); |
| 529 | return task; |
| 530 | } |
| 531 | |
| 532 | WorkflowContext latestContext = workplaceStore.getContext(context.name()); |
| 533 | if (latestContext == null) { |
| 534 | log.error("Invalid workflow context {}", context.name()); |
| 535 | return task; |
| 536 | } |
| 537 | |
| 538 | try { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 539 | if (!Objects.equals(latestContext.current(), task.programCounter())) { |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 540 | log.error("Current worklet({}) is not mismatched with task work({}). Ignored.", |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 541 | latestContext.current(), task.programCounter()); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 542 | return task; |
| 543 | } |
| 544 | |
jaegonkim | f85ee3c | 2019-04-21 11:10:25 +0900 | [diff] [blame] | 545 | Worklet worklet = workflow.getWorkletInstance(task.programCounter()); |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 546 | if (Worklet.Common.COMPLETED.equals(worklet) || Worklet.Common.INIT.equals(worklet)) { |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 547 | log.error("Current worklet is {}, Ignored", worklet); |
| 548 | return task; |
| 549 | } |
| 550 | |
| 551 | initWorkletExecution(latestContext); |
| 552 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 553 | log.info("{} worklet.isCompleted:{}", latestContext.name(), worklet.tag()); |
| 554 | log.trace("{} task:{}, context: {}", latestContext.name(), task, latestContext); |
| 555 | |
| 556 | dataModelInjector.inject(worklet, latestContext); |
| 557 | boolean completed = worklet.isCompleted(latestContext, task.event()); |
| 558 | dataModelInjector.inhale(worklet, latestContext); |
| 559 | |
| 560 | if (completed) { |
| 561 | |
| 562 | log.info("{} worklet.isCompleted(true):{}", latestContext.name(), worklet.tag()); |
| 563 | log.trace("{} task:{}, context: {}", latestContext.name(), task, latestContext); |
| 564 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 565 | eventMapStore.unregisterEventMap( |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 566 | task.eventType(), latestContext.name()); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 567 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 568 | //completed case |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 569 | //increase program counter |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 570 | ProgramCounter pc = latestContext.current(); |
| 571 | latestContext.setCurrent(workflow.increased(pc)); |
| 572 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 573 | workplaceStore.commitContext(latestContext.name(), latestContext, true); |
| 574 | return null; |
| 575 | } else { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 576 | |
| 577 | log.info("{} worklet.isCompleted(false):{}", latestContext.name(), worklet.tag()); |
| 578 | log.trace("{} task:{}, context: {}", latestContext.name(), task, latestContext); |
| 579 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 580 | workplaceStore.commitContext(latestContext.name(), latestContext, false); |
| 581 | } |
| 582 | |
| 583 | } catch (WorkflowException e) { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 584 | log.error("Exception: ", e); |
| 585 | latestContext.setCause(e.getMessage()); |
| 586 | latestContext.setState(WorkflowState.EXCEPTION); |
| 587 | workplaceStore.commitContext(latestContext.name(), latestContext, false); |
| 588 | } catch (StorageException e) { |
| 589 | log.error("Exception: ", e); |
| 590 | // StorageException does not commit context. |
| 591 | } catch (Exception e) { |
| 592 | log.error("Exception: ", e); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 593 | latestContext.setCause(e.getMessage()); |
| 594 | latestContext.setState(WorkflowState.EXCEPTION); |
| 595 | workplaceStore.commitContext(latestContext.name(), latestContext, false); |
| 596 | } |
| 597 | |
| 598 | return task; |
| 599 | } |
| 600 | |
| 601 | /** |
| 602 | * Executes event timeout task. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 603 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 604 | * @param task event timeout task |
| 605 | * @return handler task |
| 606 | */ |
| 607 | private HandlerTask execEventTimeoutTask(EventTimeoutTask task) { |
| 608 | |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 609 | if (!eventMapStore.isEventMapPresent(task.context().name())) { |
| 610 | log.trace("EventMap doesnt exist for taskcontext:{}", task.context().name()); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 611 | return task; |
| 612 | } |
| 613 | |
| 614 | log.debug("execEventTimeoutTask- task: {}, hash: {}", task, stringHash(task.context().distributor())); |
| 615 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 616 | WorkflowContext context = (WorkflowContext) (task.context()); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 617 | Workflow workflow = workflowStore.get(context.workflowId()); |
| 618 | if (workflow == null) { |
| 619 | log.error("execEventTimeoutTask: Invalid workflow {}", context.workflowId()); |
| 620 | return task; |
| 621 | } |
| 622 | |
| 623 | WorkflowContext latestContext = workplaceStore.getContext(context.name()); |
| 624 | if (latestContext == null) { |
| 625 | log.error("execEventTimeoutTask: Invalid workflow context {}", context.name()); |
| 626 | return task; |
| 627 | } |
| 628 | |
| 629 | try { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 630 | if (!Objects.equals(latestContext.current(), task.programCounter())) { |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 631 | log.error("execEventTimeoutTask: Current worklet({}) is not mismatched with task work({}). Ignored.", |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 632 | latestContext.current(), task.programCounter()); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 633 | return task; |
| 634 | } |
| 635 | |
jaegonkim | f85ee3c | 2019-04-21 11:10:25 +0900 | [diff] [blame] | 636 | Worklet worklet = workflow.getWorkletInstance(task.programCounter()); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 637 | if (worklet == Worklet.Common.COMPLETED || worklet == Worklet.Common.INIT) { |
| 638 | log.error("execEventTimeoutTask: Current worklet is {}, Ignored", worklet); |
| 639 | return task; |
| 640 | } |
| 641 | |
| 642 | initWorkletExecution(latestContext); |
| 643 | |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 644 | eventMapStore.unregisterEventMap(task.eventType(), latestContext.name()); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 645 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 646 | log.info("{} worklet.timeout(for event):{}", latestContext.name(), worklet.tag()); |
| 647 | log.trace("{} task:{}, context: {}", latestContext.name(), task, latestContext); |
| 648 | |
| 649 | dataModelInjector.inject(worklet, latestContext); |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 650 | |
| 651 | WorkletDescription workletDesc = workflow.getWorkletDesc(task.programCounter()); |
| 652 | if (Objects.nonNull(workletDesc)) { |
| 653 | if (!(workletDesc.tag().equals("INIT") || workletDesc.tag().equals("COMPLETED"))) { |
| 654 | staticDataModelInjector.inject(worklet, workletDesc); |
| 655 | } |
| 656 | } |
| 657 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 658 | worklet.timeout(latestContext); |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 659 | dataModelInjector.inhale(worklet, latestContext); |
| 660 | |
| 661 | log.info("{} worklet.timeout(for event)(done):{}", latestContext.name(), worklet.tag()); |
| 662 | log.trace("{} task:{}, context: {}", latestContext.name(), task, latestContext); |
| 663 | |
| 664 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 665 | workplaceStore.commitContext(latestContext.name(), latestContext, latestContext.triggerNext()); |
| 666 | |
| 667 | } catch (WorkflowException e) { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 668 | log.error("Exception: ", e); |
| 669 | latestContext.setCause(e.getMessage()); |
| 670 | latestContext.setState(WorkflowState.EXCEPTION); |
| 671 | workplaceStore.commitContext(latestContext.name(), latestContext, false); |
| 672 | } catch (StorageException e) { |
| 673 | log.error("Exception: ", e); |
| 674 | // StorageException does not commit context. |
| 675 | } catch (Exception e) { |
| 676 | log.error("Exception: ", e); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 677 | latestContext.setCause(e.getMessage()); |
| 678 | latestContext.setState(WorkflowState.EXCEPTION); |
| 679 | workplaceStore.commitContext(latestContext.name(), latestContext, false); |
| 680 | } |
| 681 | |
| 682 | return task; |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * Executes timeout task. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 687 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 688 | * @param task time out task |
| 689 | * @return handler task |
| 690 | */ |
| 691 | private HandlerTask execTimeoutTask(TimeoutTask task) { |
| 692 | |
| 693 | log.debug("execTimeoutTask- task: {}, hash: {}", task, stringHash(task.context().distributor())); |
| 694 | |
| 695 | WorkflowContext context = (WorkflowContext) (task.context()); |
| 696 | Workflow workflow = workflowStore.get(context.workflowId()); |
| 697 | if (workflow == null) { |
| 698 | log.error("execTimeoutTask: Invalid workflow {}", context.workflowId()); |
| 699 | return task; |
| 700 | } |
| 701 | |
| 702 | WorkflowContext latestContext = workplaceStore.getContext(context.name()); |
| 703 | if (latestContext == null) { |
| 704 | log.error("execTimeoutTask: Invalid workflow context {}", context.name()); |
| 705 | return task; |
| 706 | } |
| 707 | |
| 708 | try { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 709 | if (!Objects.equals(latestContext.current(), task.programCounter())) { |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 710 | log.error("execTimeoutTask: Current worklet({}) is not mismatched with task work({}). Ignored.", |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 711 | latestContext.current(), task.programCounter()); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 712 | return task; |
| 713 | } |
| 714 | |
jaegonkim | f85ee3c | 2019-04-21 11:10:25 +0900 | [diff] [blame] | 715 | Worklet worklet = workflow.getWorkletInstance(task.programCounter()); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 716 | if (worklet == Worklet.Common.COMPLETED || worklet == Worklet.Common.INIT) { |
| 717 | log.error("execTimeoutTask: Current worklet is {}, Ignored", worklet); |
| 718 | return task; |
| 719 | } |
| 720 | |
| 721 | initWorkletExecution(latestContext); |
| 722 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 723 | log.info("{} worklet.timeout:{}", latestContext.name(), worklet.tag()); |
| 724 | log.trace("{} context: {}", latestContext.name(), latestContext); |
| 725 | |
| 726 | dataModelInjector.inject(worklet, latestContext); |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 727 | |
| 728 | WorkletDescription workletDesc = workflow.getWorkletDesc(task.programCounter()); |
| 729 | if (Objects.nonNull(workletDesc)) { |
| 730 | if (!(workletDesc.tag().equals("INIT") || workletDesc.tag().equals("COMPLETED"))) { |
| 731 | staticDataModelInjector.inject(worklet, workletDesc); |
| 732 | } |
| 733 | } |
| 734 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 735 | worklet.timeout(latestContext); |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 736 | dataModelInjector.inhale(worklet, latestContext); |
| 737 | |
| 738 | log.info("{} worklet.timeout(done):{}", latestContext.name(), worklet.tag()); |
| 739 | log.trace("{} context: {}", latestContext.name(), latestContext); |
| 740 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 741 | workplaceStore.commitContext(latestContext.name(), latestContext, latestContext.triggerNext()); |
| 742 | |
| 743 | } catch (WorkflowException e) { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 744 | log.error("Exception: ", e); |
| 745 | latestContext.setCause(e.getMessage()); |
| 746 | latestContext.setState(WorkflowState.EXCEPTION); |
| 747 | workplaceStore.commitContext(latestContext.name(), latestContext, false); |
| 748 | } catch (StorageException e) { |
| 749 | log.error("Exception: ", e); |
| 750 | // StorageException does not commit context. |
| 751 | } catch (Exception e) { |
| 752 | log.error("Exception: ", e); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 753 | latestContext.setCause(e.getMessage()); |
| 754 | latestContext.setState(WorkflowState.EXCEPTION); |
| 755 | workplaceStore.commitContext(latestContext.name(), latestContext, false); |
| 756 | } |
| 757 | |
| 758 | return task; |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * Class for delegation of workflow batch execution. |
| 763 | */ |
| 764 | private class InternalWorkflowBatchDelegate implements WorkflowBatchDelegate { |
| 765 | @Override |
| 766 | public void execute(Collection<WorkflowData> operations) { |
| 767 | log.debug("Execute {} operation(s).", operations.size()); |
| 768 | |
| 769 | CompletableFuture.runAsync(() -> { |
| 770 | List<CompletableFuture<WorkflowData>> futures = operations.stream() |
| 771 | .map( |
| 772 | x -> CompletableFuture.completedFuture(x) |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 773 | .thenApplyAsync(WorkFlowEngine.this::execWorkflow, workflowExecutor) |
| 774 | .exceptionally(e -> null) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 775 | ) |
| 776 | .collect(Collectors.toList()); |
| 777 | |
| 778 | // waiting the completion of futures |
| 779 | futures.parallelStream().forEach(x -> x.join()); |
| 780 | |
| 781 | }, workflowBatchExecutor).exceptionally(e -> { |
| 782 | log.error("Error submitting batches:", e); |
| 783 | return null; |
| 784 | }).thenRun(workflowAccumulator::ready); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * Executes workflow. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 790 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 791 | * @param dataModelContainer workflow data model container(workflow or workplace) |
| 792 | * @return |
| 793 | */ |
| 794 | private WorkflowData execWorkflow(WorkflowData dataModelContainer) { |
| 795 | if (dataModelContainer instanceof WorkflowContext) { |
| 796 | return execWorkflowContext((WorkflowContext) dataModelContainer); |
| 797 | } else if (dataModelContainer instanceof Workplace) { |
| 798 | return execWorkplace((Workplace) dataModelContainer); |
| 799 | } else { |
| 800 | log.error("Invalid context {}", dataModelContainer); |
| 801 | return null; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * Executes workflow context. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 807 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 808 | * @param context workflow context |
| 809 | * @return workflow context |
| 810 | */ |
| 811 | private WorkflowContext execWorkflowContext(WorkflowContext context) { |
| 812 | |
| 813 | Workflow workflow = workflowStore.get(context.workflowId()); |
| 814 | if (workflow == null) { |
| 815 | log.error("Invalid workflow {}", context.workflowId()); |
| 816 | return null; |
| 817 | } |
| 818 | |
| 819 | final WorkflowContext latestContext = workplaceStore.getContext(context.name()); |
| 820 | if (latestContext == null) { |
| 821 | log.error("Invalid workflow context {}", context.name()); |
| 822 | return null; |
| 823 | } |
| 824 | |
| 825 | initWorkletExecution(latestContext); |
| 826 | |
| 827 | try { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 828 | final ProgramCounter pc = workflow.next(latestContext); |
jaegonkim | f85ee3c | 2019-04-21 11:10:25 +0900 | [diff] [blame] | 829 | final Worklet worklet = workflow.getWorkletInstance(pc); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 830 | |
| 831 | if (worklet == Worklet.Common.INIT) { |
| 832 | log.error("workflow.next gave INIT. It cannot be executed (context: {})", context.name()); |
| 833 | return latestContext; |
| 834 | } |
| 835 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 836 | latestContext.setCurrent(pc); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 837 | if (worklet == Worklet.Common.COMPLETED) { |
| 838 | |
| 839 | if (workflow.attributes().contains(REMOVE_AFTER_COMPLETE)) { |
| 840 | workplaceStore.removeContext(latestContext.name()); |
| 841 | return null; |
| 842 | } else { |
| 843 | latestContext.setState(WorkflowState.IDLE); |
| 844 | workplaceStore.commitContext(latestContext.name(), latestContext, false); |
| 845 | return latestContext; |
| 846 | } |
| 847 | } |
| 848 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 849 | log.info("{} worklet.process:{}", latestContext.name(), worklet.tag()); |
| 850 | log.trace("{} context: {}", latestContext.name(), latestContext); |
| 851 | |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 852 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 853 | dataModelInjector.inject(worklet, latestContext); |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 854 | |
| 855 | WorkletDescription workletDesc = workflow.getWorkletDesc(pc); |
| 856 | if (Objects.nonNull(workletDesc)) { |
| 857 | if (!(workletDesc.tag().equals("INIT") || workletDesc.tag().equals("COMPLETED"))) { |
| 858 | staticDataModelInjector.inject(worklet, workletDesc); |
| 859 | } |
| 860 | } |
| 861 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 862 | worklet.process(latestContext); |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 863 | dataModelInjector.inhale(worklet, latestContext); |
| 864 | |
| 865 | log.info("{} worklet.process(done): {}", latestContext.name(), worklet.tag()); |
| 866 | log.trace("{} context: {}", latestContext.name(), latestContext); |
| 867 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 868 | if (latestContext.completionEventType() != null) { |
| 869 | if (latestContext.completionEventGenerator() == null) { |
| 870 | String msg = String.format("Invalid exepecting event(%s), generator(%s)", |
| 871 | latestContext.completionEventType(), |
| 872 | latestContext.completionEventGenerator()); |
| 873 | throw new WorkflowException(msg); |
| 874 | } |
| 875 | |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 876 | registerEventMap(latestContext.completionEventType(), latestContext.completionEventHints(), |
nitinanand | c8b7025 | 2019-04-17 15:35:43 +0530 | [diff] [blame] | 877 | latestContext.name(), pc); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 878 | latestContext.completionEventGenerator().apply(); |
| 879 | |
| 880 | if (latestContext.completionEventTimeout() != 0L) { |
| 881 | final EventTimeoutTask eventTimeoutTask = EventTimeoutTask.builder() |
| 882 | .context(latestContext) |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 883 | .programCounter(pc) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 884 | .eventType(latestContext.completionEventType().getName()) |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 885 | .eventHintSet(latestContext.completionEventHints()) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 886 | .build(); |
| 887 | timerChain.schedule(latestContext.completionEventTimeout(), |
| 888 | () -> { |
| 889 | eventtaskAccumulator.add(eventTimeoutTask); |
| 890 | }); |
| 891 | } |
| 892 | } else { |
| 893 | if (latestContext.completionEventTimeout() != 0L) { |
| 894 | final TimeoutTask timeoutTask = TimeoutTask.builder() |
| 895 | .context(latestContext) |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 896 | .programCounter(pc) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 897 | .build(); |
| 898 | |
| 899 | timerChain.schedule(latestContext.completionEventTimeout(), |
| 900 | () -> { |
| 901 | eventtaskAccumulator.add(timeoutTask); |
| 902 | }); |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 903 | } else { |
| 904 | //completed case |
| 905 | // increase program counter |
| 906 | latestContext.setCurrent(workflow.increased(pc)); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 907 | } |
| 908 | } |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 909 | workplaceStore.commitContext(latestContext.name(), latestContext, latestContext.triggerNext()); |
| 910 | |
| 911 | } catch (WorkflowException e) { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 912 | log.error("Exception: ", e); |
| 913 | latestContext.setCause(e.getMessage()); |
| 914 | latestContext.setState(WorkflowState.EXCEPTION); |
| 915 | workplaceStore.commitContext(latestContext.name(), latestContext, false); |
| 916 | } catch (StorageException e) { |
| 917 | log.error("Exception: ", e); |
| 918 | // StorageException does not commit context. |
| 919 | } catch (Exception e) { |
| 920 | log.error("Exception: ", e); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 921 | latestContext.setCause(e.getMessage()); |
| 922 | latestContext.setState(WorkflowState.EXCEPTION); |
| 923 | workplaceStore.commitContext(latestContext.name(), latestContext, false); |
| 924 | } |
| 925 | |
| 926 | return latestContext; |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * Execute workplace. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 931 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 932 | * @param workplace workplace |
| 933 | * @return workplace |
| 934 | */ |
| 935 | private Workplace execWorkplace(Workplace workplace) { |
| 936 | |
| 937 | return null; |
| 938 | } |
| 939 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 940 | } |