-
Notifications
You must be signed in to change notification settings - Fork 53
[Fix #1329] Adding iterations to TaskContext #1337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,4 +37,8 @@ public interface TaskContextData { | |
| String taskName(); | ||
|
|
||
| Instant completedAt(); | ||
|
|
||
| int iteration(); | ||
|
|
||
| short retryAttempt(); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a leftover from previous PR, retryAttemp make sense at interfaz level |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,7 @@ | |
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Iterator; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
@@ -69,7 +70,7 @@ public class WorkflowApplication implements AutoCloseable { | |
| private final ResourceLoaderFactory resourceLoaderFactory; | ||
| private final SchemaValidatorFactory schemaValidatorFactory; | ||
| private final WorkflowInstanceIdFactory idFactory; | ||
| private final Collection<WorkflowExecutionCompletableListener> listeners; | ||
| private final List<Collection<WorkflowExecutionCompletableListener>> listenersByPriority; | ||
| private final Map<WorkflowDefinitionId, WorkflowDefinition> definitions; | ||
| private final WorkflowPositionFactory positionFactory; | ||
| private final ExecutorServiceFactory executorFactory; | ||
|
|
@@ -99,7 +100,7 @@ private WorkflowApplication(Builder builder) { | |
| this.idFactory = builder.idFactory; | ||
| this.runtimeDescriptorFactory = builder.descriptorFactory; | ||
| this.executorFactory = builder.executorFactory; | ||
| this.listeners = new LinkedHashSet<>(builder.listeners); | ||
| this.listenersByPriority = groupByPriority(new LinkedHashSet<>(builder.listeners)); | ||
| this.definitions = new ConcurrentHashMap<>(); | ||
| this.eventConsumer = builder.eventConsumer; | ||
| this.eventPublishers = builder.eventPublishers; | ||
|
|
@@ -140,7 +141,7 @@ public ResourceLoaderFactory resourceLoaderFactory() { | |
| } | ||
|
|
||
| public Collection<WorkflowExecutionCompletableListener> listeners() { | ||
| return listeners; | ||
| return this.listenersByPriority.stream().flatMap(x -> x.stream()).toList(); | ||
| } | ||
|
|
||
| public Collection<EventPublisher> eventPublishers() { | ||
|
|
@@ -151,6 +152,36 @@ public WorkflowInstanceIdFactory idFactory() { | |
| return idFactory; | ||
| } | ||
|
|
||
| List<Collection<WorkflowExecutionCompletableListener>> listenersByPriority() { | ||
| return listenersByPriority; | ||
|
fjtirado marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private static List<Collection<WorkflowExecutionCompletableListener>> groupByPriority( | ||
| Collection<WorkflowExecutionCompletableListener> listeners) { | ||
| if (listeners.isEmpty()) { | ||
| return List.of(); | ||
|
fjtirado marked this conversation as resolved.
|
||
| } | ||
| List<Collection<WorkflowExecutionCompletableListener>> result = new ArrayList<>(); | ||
| Iterator<WorkflowExecutionCompletableListener> iter = listeners.iterator(); | ||
| List<WorkflowExecutionCompletableListener> currentList = new ArrayList<>(); | ||
| WorkflowExecutionCompletableListener currentListener = iter.next(); | ||
| int currentPriority = currentListener.priority(); | ||
| currentList.add(currentListener); | ||
| while (iter.hasNext()) { | ||
| currentListener = iter.next(); | ||
| if (currentListener.priority() != currentPriority) { | ||
| result.add(currentList); | ||
| currentList = new ArrayList<>(); | ||
| currentPriority = currentListener.priority(); | ||
| } | ||
| currentList.add(currentListener); | ||
| } | ||
| if (!currentList.isEmpty()) { | ||
| result.add(currentList); | ||
| } | ||
| return result; | ||
|
fjtirado marked this conversation as resolved.
Comment on lines
+160
to
+182
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is grouping listeners by priority. |
||
| } | ||
|
|
||
| public static class Builder { | ||
|
|
||
| private static final class EmptySchemaValidatorHolder { | ||
|
|
@@ -423,10 +454,15 @@ public void close() { | |
| } | ||
| definitions.clear(); | ||
|
|
||
| for (WorkflowExecutionCompletableListener listener : listeners) { | ||
| safeClose(listener); | ||
| if (!listenersByPriority.isEmpty()) { | ||
| for (Collection<WorkflowExecutionCompletableListener> listeners : listenersByPriority) { | ||
| for (WorkflowExecutionCompletableListener listener : listeners) { | ||
| safeClose(listener); | ||
| } | ||
| listeners.clear(); | ||
| } | ||
| listenersByPriority.clear(); | ||
| } | ||
| listeners.clear(); | ||
| } | ||
|
|
||
| public WorkflowPositionFactory positionFactory() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,13 @@ public static WorkflowInstance of(WorkflowDefinition definition, PersistenceWork | |
| private WorkflowPersistenceInstance(WorkflowDefinition definition, PersistenceWorkflowInfo info) { | ||
| super(definition, info.id(), info.input()); | ||
| this.info = info; | ||
| info.tasks() | ||
| .forEach( | ||
| (k, v) -> { | ||
| if (v instanceof CompletedTaskInfo task) { | ||
| iterationsMap.put(k, task.iteration()); | ||
| } | ||
| }); | ||
| this.startedAt = info.startedAt(); | ||
| } | ||
|
|
||
|
|
@@ -54,7 +61,13 @@ public CompletableFuture<WorkflowModel> start() { | |
|
|
||
| @Override | ||
| public void restoreContext(WorkflowContext workflow, TaskContext context) { | ||
| if (info.tasks().isEmpty()) { | ||
| return; | ||
| } | ||
|
Comment on lines
+64
to
+66
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an optimization I forgot to add previously, once all persisted task are processed there is not point on performing a remove and two intances of |
||
| PersistenceTaskInfo taskInfo = info.tasks().remove(context.position().jsonPointer()); | ||
| if (taskInfo == null) { | ||
| return; | ||
| } | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the same, there is not point in performing two instanceOf if the task is not in the map |
||
| if (taskInfo instanceof CompletedTaskInfo completedTaskInfo) { | ||
|
fjtirado marked this conversation as resolved.
|
||
| context.output(completedTaskInfo.model()); | ||
|
fjtirado marked this conversation as resolved.
|
||
| context.completedAt(completedTaskInfo.instant()); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized that listeners with different priority should not be executed concurrently (it kills the purpose of priority). listeners with higher priority should be completed before listeners with lower priority starts execution.
Since the list of listeners is not mutable once the application has been created, they are grouped by prioriry at application creation time, so there is not need to group them for every application change.