use of com.walmartlabs.concord.runtime.v2.model.ParallelBlockOptions in project concord by walmartlabs.
the class ParallelCommand method execute.
@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
Frame frame = state.peekFrame(threadId);
frame.pop();
// parallel execution consist of "forks" for each command running in separate threads
// and a combined "join" executing in the parent (current) thread
List<Map.Entry<ThreadId, Command>> forks = commands.stream().map(e -> new AbstractMap.SimpleEntry<>(state.nextThreadId(), e)).collect(Collectors.toList());
ParallelBlockOptions opts = Objects.requireNonNull(getStep().getOptions());
Command outVarsCommand;
if (!opts.outExpr().isEmpty()) {
Map<String, Object> accumulator = new ConcurrentHashMap<>();
outVarsCommand = new CollectVariablesCommand(accumulator);
frame.push(new EvalVariablesCommand(runtime.getService(Context.class), accumulator, opts.outExpr(), frame));
} else {
outVarsCommand = new CopyVariablesCommand(opts.out(), State::peekFrame, frame);
}
Collection<ThreadId> forkIds = forks.stream().map(Map.Entry::getKey).collect(Collectors.toSet());
frame.push(new JoinCommand(forkIds));
Collections.reverse(forks);
forks.forEach(f -> {
Command cmd = new ForkCommand(f.getKey(), outVarsCommand, f.getValue());
frame.push(cmd);
});
}
Aggregations