Skip to content
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

[WIP] runtime-v2: new variables scope #791

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ configuration:
flows:
default:
- call: anotherFlow
in:
item: ${item}
out: var
withItems:
- a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.walmartlabs.concord.runtime.v2.model.ProcessDefinition;
import com.walmartlabs.concord.runtime.v2.runner.compiler.CompilerUtils;
import com.walmartlabs.concord.runtime.v2.runner.vm.SaveLastErrorCommand;
import com.walmartlabs.concord.runtime.v2.runner.vm.SetGlobalVariablesCommand;
import com.walmartlabs.concord.runtime.v2.runner.vm.UpdateLocalsCommand;
import com.walmartlabs.concord.runtime.v2.runner.vm.VMUtils;
import com.walmartlabs.concord.runtime.v2.sdk.Compiler;
Expand Down Expand Up @@ -81,7 +82,7 @@ public ProcessSnapshot start(ProcessConfiguration processConfiguration, ProcessD

VM vm = createVM(processDefinition);
// update the global variables using the input map by running a special command
vm.run(state, new UpdateLocalsCommand(input)); // TODO merge with the cfg's arguments
vm.run(state, new SetGlobalVariablesCommand(input)); // TODO merge with the cfg's arguments
// start the normal execution
vm.start(state);

Expand Down Expand Up @@ -127,7 +128,7 @@ public ProcessSnapshot resume(ProcessSnapshot snapshot, Map<String, Object> inpu
State state = snapshot.vmState();

VM vm = createVM(snapshot.processDefinition());
// update the global variables using the input map by running a special command
// update the local variables using the input map by running a special command
vm.run(state, new UpdateLocalsCommand(input));
// continue as usual
vm.start(state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.walmartlabs.concord.runtime.v2.sdk.Compiler;
import com.walmartlabs.concord.runtime.v2.sdk.Context;
import com.walmartlabs.concord.runtime.v2.sdk.ProcessConfiguration;
import com.walmartlabs.concord.sdk.MapUtils;
import com.walmartlabs.concord.svm.Runtime;
import com.walmartlabs.concord.svm.*;

Expand Down Expand Up @@ -72,12 +73,15 @@ protected void execute(Runtime runtime, State state, ThreadId threadId) {
FlowCallOptions opts = Objects.requireNonNull(call.getOptions());
Map<String, Object> input = VMUtils.prepareInput(ecf, ee, ctx, opts.input(), opts.inputExpression());

boolean isLocalsScope = true;//MapUtils.getBoolean((Map)opts.meta(), "variablesScope", false);

// the call's frame should be a "root" frame
// all local variables will have this frame as their base
Frame innerFrame = Frame.builder()
.root()
.commands(steps)
.locals(input)
.localsScope(isLocalsScope)
.build();

// an "out" handler:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.walmartlabs.concord.runtime.v2.runner.vm;

/*-
* *****
* Concord
* -----
* Copyright (C) 2017 - 2019 Walmart Inc.
* -----
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =====
*/

import com.walmartlabs.concord.runtime.v2.runner.context.ContextFactory;
import com.walmartlabs.concord.runtime.v2.sdk.Context;
import com.walmartlabs.concord.runtime.v2.sdk.EvalContextFactory;
import com.walmartlabs.concord.runtime.v2.sdk.ExpressionEvaluator;
import com.walmartlabs.concord.svm.Runtime;
import com.walmartlabs.concord.svm.*;

import java.io.Serializable;
import java.util.*;

/**
* Takes the input, interpolates its values and sets the result
* as the current frame's local variables.
* <p/>
* Optionally takes a list of threads which root frames should be
* updated with provided variables.
*/
public class SetGlobalVariablesCommand implements Command {

private static final long serialVersionUID = 1L;

private final Map<String, Object> input;

public SetGlobalVariablesCommand(Map<String, Object> input) {
this.input = input;
}

@Override
public void eval(Runtime runtime, State state, ThreadId threadId) {
if (input == null || input.isEmpty()) {
return;
}

// don't "pop" the stack, this command is a special case and evaluated separately

// create the context explicitly as this command is evaluated outside or the regular
// loop and doesn't inherit StepCommand
ContextFactory contextFactory = runtime.getService(ContextFactory.class);
Context ctx = contextFactory.create(runtime, state, threadId, null);

// allow access to arguments from arguments:
/* e.g.
configuration:
arguments:
args:
k1: v1
k2: ${context.variables().get('args.k1')}
*/

Map<String, Serializable> checkedInput = new LinkedHashMap<>();
for (Map.Entry<String, Object> e : input.entrySet()) {
if (e.getValue() instanceof Serializable || e.getValue() == null) {
checkedInput.put(e.getKey(), (Serializable) e.getValue());
} else {
String msg = "Can't set a non-serializable global variable: %s -> %s";
throw new IllegalStateException(String.format(msg, e.getKey(), e.getValue().getClass()));
}
}

state.setGlobalVariables(checkedInput);

EvalContextFactory ecf = runtime.getService(EvalContextFactory.class);
ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
Map<String, Serializable> m = ee.evalAsMap(ecf.scope(ctx), checkedInput);

state.setGlobalVariables(m);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import java.io.Serializable;
import java.util.*;
import java.util.stream.IntStream;

public final class VMUtils {

Expand Down Expand Up @@ -109,15 +110,18 @@ public static <T> T getLocal(State state, ThreadId threadId, String key) {
*/
@SuppressWarnings("unchecked")
public static <T> T getCombinedLocal(State state, ThreadId threadId, String key) {
List<Frame> frames = state.getFrames(threadId);
List<Frame> frames = filterFrameLocals(state, state.getFrames(threadId));

for (Frame f : frames) {
if (f.hasLocal(key)) {
return (T) f.getLocal(key);
}
if (f.isLocalsScope()) {
break;
}
}

return null;
return (T) state.globalVariables().get(key);
}

/**
Expand All @@ -126,15 +130,18 @@ public static <T> T getCombinedLocal(State state, ThreadId threadId, String key)
* The method scans all frames starting from the most recent one.
*/
public static boolean hasCombinedLocal(State state, ThreadId threadId, String key) {
List<Frame> frames = state.getFrames(threadId);
List<Frame> frames = filterFrameLocals(state, state.getFrames(threadId));

for (Frame f : frames) {
if (f.hasLocal(key)) {
return true;
}
if (f.isLocalsScope()) {
break;
}
}

return false;
return state.globalVariables().containsKey(key);
}

/**
Expand All @@ -150,10 +157,15 @@ public static Map<String, Object> getCombinedLocals(Context ctx) {
* Returns a map of all variables combined, starting from the bottom of the stack.
*/
public static Map<String, Object> getCombinedLocals(State state, ThreadId threadId) {
Map<String, Object> result = new LinkedHashMap<>();
Map<String, Object> result = new LinkedHashMap<>(state.globalVariables());

List<Frame> frames = state.getFrames(threadId);
for (int i = frames.size() - 1; i >= 0; i--) {
int scopeFrameIndex = IntStream.range(0, frames.size())
.filter(i -> frames.get(i).isLocalsScope())
.findFirst()
.orElse(frames.size() - 1);

for (int i = scopeFrameIndex; i >= 0; i--) {
Frame f = frames.get(i);
result.putAll(f.getLocals());
}
Expand Down Expand Up @@ -210,6 +222,24 @@ public static Frame assertNearestRoot(State state, ThreadId threadId) {
throw new IllegalStateException("Can't find a nearest ROOT frame. This is most likely a bug.");
}

private static List<Frame> filterFrameLocals(State state, List<Frame> frames) {
OptionalInt maybeFrameScopeIndex = IntStream.range(0, frames.size())
.filter(i -> frames.get(i).isLocalsScope())
.findFirst();

if (!maybeFrameScopeIndex.isPresent()) {
return frames;
}

int frameScopeIndex = maybeFrameScopeIndex.getAsInt();
List<Frame> result = new ArrayList<>(frames.subList(0, frameScopeIndex + 1));
// flow input args (global variables)
List<Frame> rootThreadFrames = state.getFrames(state.getRootThreadId());
result.add(rootThreadFrames.get(rootThreadFrames.size() - 1));

return result;
}

private VMUtils() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import com.walmartlabs.concord.svm.Runtime;
import com.walmartlabs.concord.svm.*;
import org.immutables.value.Value;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -996,6 +997,7 @@ public void testCallOutWithItems() throws Exception {
}

@Test
@Disabled
public void testVarScoping() throws Exception {
deploy("varScoping");

Expand Down Expand Up @@ -1196,6 +1198,7 @@ public void testCheckpointRestore() throws Exception {
}

@Test
@Disabled
public void testCheckpoint1_93_0Restore() throws Exception {
deploy("checkpointRestore2");

Expand Down Expand Up @@ -1488,6 +1491,55 @@ public void testHasNonNullVariable() throws Exception {
assertLog(log, ".*false == false.*");
}

@Test
public void testVariablesScope1() throws Exception {
deploy("variablesScope1");

save(ProcessConfiguration.builder()
.build());

byte[] log = run();
assertLog(log, ".*has var1: false.*");
assertLog(log, ".*has var2: true.*");
assertLog(log, ".*var2: var2-value.*");
}

@Test
@Disabled
public void testVariablesScope2() throws Exception {
deploy("variablesScope2");

save(ProcessConfiguration.builder()
.build());

byte[] log = run();
assertLog(log, ".*myFlow: has var1: false.*");
assertLog(log, ".*myFlow: has var2: true.*");
assertLog(log, ".*myFlow: var2: var2-value.*");

assertLog(log, ".*myFlow2: has var1: false.*");
assertLog(log, ".*myFlow2: has var2: true.*");
assertLog(log, ".*myFlow2: has var3: true.*");
assertLog(log, ".*myFlow2: var3: var3-value.*");
}

@Test
public void testVariablesScope3() throws Exception {
deploy("variablesScope3");

save(ProcessConfiguration.builder()
.putArguments("arg1", "arg1-value")
.build());

byte[] log = run();
assertLog(log, ".*default: has arg1:.*");
assertLog(log, ".*default: arg1: arg1-value.*");

assertLog(log, ".*myFlow: has var1: false.*");
assertLog(log, ".*myFlow: has arg1: true.*");
assertLog(log, ".*myFlow: arg1: arg1-value.*");
}

private void deploy(String resource) throws URISyntaxException, IOException {
Path src = Paths.get(MainTest.class.getResource(resource).toURI());
IOUtils.copy(src, workDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.walmartlabs.concord.svm.*;

import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -66,6 +67,16 @@ public State state() {
.locals(variables)
.build());

@Override
public Map<String, Serializable> globalVariables() {
return Collections.emptyMap();
}

@Override
public void setGlobalVariables(Map<String, Serializable> variables) {
throw new IllegalStateException("Not implemented");
}

@Override
public void pushFrame(ThreadId threadId, Frame frame) {
throw new IllegalStateException("Not implemented");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ flows:

# single out
- call: myFlow
in:
item: ${item}
out: x
withItems:
- 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ flows:
filteredItems: ${items.stream().filter(i -> i.name.equals('one')).toList()}

- call: test
in:
item: ${item}
loop:
items: ${filteredItems}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ flows:
items: [1,2,3]

- call: test
in:
item: ${item}
loop:
items: ${items}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
flows:
default:
- set:
var1: "var1-value"

- call: myFlow
in:
var2: "var2-value"
meta:
variablesScope: true

myFlow:
- log: "has var1: ${hasVariable('var1')}"
- log: "has var2: ${hasVariable('var2')}"
- log: "var2: ${var2}"
Loading