Skip to content

Commit 796f013

Browse files
committed
[Fix #484] Removing SortedArrayList
Maybe for larger list that has to be kept sorte that one is useful, but in this case is faster to add everything unsorted (all insertions are really fast) and then sort (there is only one sort of a small amount of items) Signed-off-by: Francisco Javier Tirado Sarti <[email protected]>
1 parent 74fc958 commit 796f013

File tree

5 files changed

+71
-166
lines changed

5 files changed

+71
-166
lines changed

impl/core/src/main/java/io/serverlessworkflow/impl/TaskContext.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,6 @@ public TaskContext(JsonNode input, WorkflowPosition position) {
4141
this(input, null, position, Instant.now(), input, input, input, null, new HashMap<>());
4242
}
4343

44-
public TaskContext<T> copy() {
45-
return new TaskContext<T>(
46-
rawInput,
47-
task,
48-
position.copy(),
49-
startedAt,
50-
input,
51-
output,
52-
rawOutput,
53-
flowDirective,
54-
new HashMap<>(contextVariables));
55-
}
56-
5744
public TaskContext(JsonNode input, TaskContext<?> taskContext, T task) {
5845
this(
5946
input,
@@ -88,6 +75,19 @@ private TaskContext(
8875
this.contextVariables = contextVariables;
8976
}
9077

78+
public TaskContext<T> copy() {
79+
return new TaskContext<T>(
80+
rawInput,
81+
task,
82+
position.copy(),
83+
startedAt,
84+
input,
85+
output,
86+
rawOutput,
87+
flowDirective,
88+
new HashMap<>(contextVariables));
89+
}
90+
9191
public void input(JsonNode input) {
9292
this.input = input;
9393
this.rawOutput = input;

impl/core/src/main/java/io/serverlessworkflow/impl/executors/ForkExecutor.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.serverlessworkflow.impl.executors;
1717

18+
import com.fasterxml.jackson.databind.JsonNode;
1819
import io.serverlessworkflow.api.types.FlowDirectiveEnum;
1920
import io.serverlessworkflow.api.types.ForkTask;
2021
import io.serverlessworkflow.api.types.ForkTaskConfiguration;
@@ -24,16 +25,16 @@
2425
import io.serverlessworkflow.impl.WorkflowDefinition;
2526
import io.serverlessworkflow.impl.WorkflowState;
2627
import io.serverlessworkflow.impl.WorkflowUtils;
27-
import io.serverlessworkflow.impl.generic.SortedArrayList;
2828
import io.serverlessworkflow.impl.json.JsonUtils;
2929
import java.lang.reflect.UndeclaredThrowableException;
30+
import java.util.ArrayList;
3031
import java.util.HashMap;
3132
import java.util.List;
3233
import java.util.Map;
3334
import java.util.concurrent.ExecutionException;
3435
import java.util.concurrent.ExecutorService;
3536
import java.util.concurrent.Future;
36-
import java.util.stream.Collectors;
37+
import java.util.stream.Stream;
3738
import org.slf4j.Logger;
3839
import org.slf4j.LoggerFactory;
3940

@@ -47,8 +48,6 @@ protected ForkExecutor(ForkTask task, WorkflowDefinition definition) {
4748
service = definition.executorService();
4849
}
4950

50-
private record BranchContext(String taskName, TaskContext<?> taskContext) {}
51-
5251
@Override
5352
protected void internalExecute(WorkflowContext workflow, TaskContext<ForkTask> taskContext) {
5453
ForkTaskConfiguration forkConfig = task.getFork();
@@ -62,13 +61,10 @@ protected void internalExecute(WorkflowContext workflow, TaskContext<ForkTask> t
6261
item.getName(),
6362
service.submit(() -> executeBranch(workflow, taskContext.copy(), item, i)));
6463
}
65-
List<BranchContext> results =
66-
new SortedArrayList<>(
67-
(arg1, arg2) ->
68-
arg1.taskContext.completedAt().compareTo(arg2.taskContext.completedAt()));
64+
List<Map.Entry<String, TaskContext<?>>> results = new ArrayList<>();
6965
for (Map.Entry<String, Future<TaskContext<?>>> entry : futures.entrySet()) {
7066
try {
71-
results.add(new BranchContext(entry.getKey(), entry.getValue().get()));
67+
results.add(Map.entry(entry.getKey(), entry.getValue().get()));
7268
} catch (ExecutionException ex) {
7369
Throwable cause = ex.getCause();
7470
if (cause instanceof RuntimeException) {
@@ -77,24 +73,25 @@ protected void internalExecute(WorkflowContext workflow, TaskContext<ForkTask> t
7773
throw new UndeclaredThrowableException(ex);
7874
}
7975
} catch (InterruptedException ex) {
80-
logger.warn(
81-
"Thred executing branch {} was interrupted, this branch will be ignored",
82-
entry.getKey(),
83-
ex);
76+
logger.warn("Branch {} was interrupted, no result will be recorded", entry.getKey(), ex);
8477
}
8578
}
8679
if (!results.isEmpty()) {
80+
Stream<Map.Entry<String, TaskContext<?>>> sortedStream =
81+
results.stream()
82+
.sorted(
83+
(arg1, arg2) ->
84+
arg1.getValue().completedAt().compareTo(arg2.getValue().completedAt()));
8785
taskContext.rawOutput(
8886
forkConfig.isCompete()
89-
? results.get(0).taskContext().output()
90-
: JsonUtils.fromValue(
91-
results.stream()
92-
.map(
93-
e ->
94-
JsonUtils.mapper()
95-
.createObjectNode()
96-
.set(e.taskName(), e.taskContext().output()))
97-
.collect(Collectors.toList())));
87+
? sortedStream.map(e -> e.getValue().output()).findFirst().orElseThrow()
88+
: sortedStream
89+
.<JsonNode>map(
90+
e ->
91+
JsonUtils.mapper()
92+
.createObjectNode()
93+
.set(e.getKey(), e.getValue().output()))
94+
.collect(JsonUtils.arrayNodeCollector()));
9895
}
9996
}
10097
}

impl/core/src/main/java/io/serverlessworkflow/impl/generic/SortedArrayList.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

impl/core/src/main/java/io/serverlessworkflow/impl/json/JsonUtils.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,16 @@
3636
import java.math.BigInteger;
3737
import java.util.ArrayList;
3838
import java.util.Collection;
39+
import java.util.Collections;
40+
import java.util.EnumSet;
3941
import java.util.HashMap;
4042
import java.util.Map;
43+
import java.util.Set;
44+
import java.util.function.BiConsumer;
45+
import java.util.function.BinaryOperator;
4146
import java.util.function.Function;
47+
import java.util.function.Supplier;
48+
import java.util.stream.Collector;
4249

4350
public class JsonUtils {
4451

@@ -48,6 +55,38 @@ public static ObjectMapper mapper() {
4855
return mapper;
4956
}
5057

58+
public static Collector<JsonNode, ArrayNode, ArrayNode> arrayNodeCollector() {
59+
return new Collector<JsonNode, ArrayNode, ArrayNode>() {
60+
@Override
61+
public BiConsumer<ArrayNode, JsonNode> accumulator() {
62+
return (arrayNode, item) -> arrayNode.add(item);
63+
}
64+
65+
@Override
66+
public Set<Characteristics> characteristics() {
67+
return Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH));
68+
}
69+
70+
@Override
71+
public BinaryOperator<ArrayNode> combiner() {
72+
return (r1, r2) -> {
73+
r1.addAll(r2);
74+
return r1;
75+
};
76+
}
77+
78+
@Override
79+
public Function<ArrayNode, ArrayNode> finisher() {
80+
return arrayNode -> arrayNode;
81+
}
82+
83+
@Override
84+
public Supplier<ArrayNode> supplier() {
85+
return () -> mapper.createArrayNode();
86+
}
87+
};
88+
}
89+
5190
/*
5291
* Implementation note:
5392
* Although we can use directly ObjectMapper.convertValue for implementing fromValue and toJavaValue methods,

impl/core/src/test/java/io/serverlessworkflow/impl/SortedListTest.java

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)