Search in sources :

Example 1 with ParallelBlockOptions

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);
    });
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) ParallelBlock(com.walmartlabs.concord.runtime.v2.model.ParallelBlock) java.util(java.util) ParallelBlockOptions(com.walmartlabs.concord.runtime.v2.model.ParallelBlockOptions) Runtime(com.walmartlabs.concord.svm.Runtime) com.walmartlabs.concord.svm(com.walmartlabs.concord.svm) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EvalContextFactory(com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory) Collectors(java.util.stream.Collectors) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator) Serializable(java.io.Serializable) ParallelBlockOptions(com.walmartlabs.concord.runtime.v2.model.ParallelBlockOptions) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

ParallelBlock (com.walmartlabs.concord.runtime.v2.model.ParallelBlock)1 ParallelBlockOptions (com.walmartlabs.concord.runtime.v2.model.ParallelBlockOptions)1 EvalContextFactory (com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory)1 ExpressionEvaluator (com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)1 Context (com.walmartlabs.concord.runtime.v2.sdk.Context)1 com.walmartlabs.concord.svm (com.walmartlabs.concord.svm)1 Runtime (com.walmartlabs.concord.svm.Runtime)1 Serializable (java.io.Serializable)1 java.util (java.util)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Collectors (java.util.stream.Collectors)1