deferred resolution of commands

This commit is contained in:
Jonathan Shook 2024-01-18 00:23:20 -06:00
parent 37dd11c344
commit db78ed41da
4 changed files with 126 additions and 18 deletions

View File

@ -27,14 +27,21 @@ import io.nosqlbench.nb.annotations.Service;
@Service(value= NBInvokableResolver.class,selector = "java")
public class NBJavaNativeResolver implements NBInvokableResolver {
@Override
public NBInvokableCommand resolve(Cmd cmd, NBBufferedContainer parent, String phaseName) {
public NBInvokableCommand resolve(Cmd cmd, NBBufferedContainer parent, String stepname) {
return switch (cmd.getCmdType()) {
case CmdType.indirect -> {
String implName = cmd.takeArgValue("_impl");
yield NBJavaCommandLoader.init(implName, parent, phaseName, cmd.getTargetContext());
String implName = cmd.getArgValue("_impl");
NBInvokableCommand invokable = NBJavaCommandLoader.init(implName, parent, stepname, cmd.getTargetContext());
cmd.takeArgValue("_impl");
yield invokable;
}
case CmdType.java -> NBJavaCommandLoader.init(cmd.getArgValue("class"), parent, phaseName, cmd.getTargetContext());
case CmdType.java -> NBJavaCommandLoader.init(cmd.getArgValue("class"), parent, stepname, cmd.getTargetContext());
default -> null;
};
}
@Override
public boolean verify(Cmd cmd) {
return NBJavaCommandLoader.oneExists(cmd.getArgValue("_impl")) != null;
}
}

View File

@ -29,15 +29,35 @@ import java.util.function.Function;
public class NBCommandAssembly {
private static NBCoreInvokableResolver core_resolver = new NBCoreInvokableResolver();
private final static Logger logger = LogManager.getLogger(NBCommandAssembly.class);
public static NBCommandParams paramsFor(Cmd cmd) {
return switch (cmd.getCmdType()) {
case indirect, java, container -> {
Map<String, String> params = cmd.getArgMap();
params.remove("_impl");
yield NBCommandParams.of(params);
}
default -> NBCommandParams.of(Map.of());
};
}
public static record CommandInvocation(NBInvokableCommand command, NBCommandParams params, String containerName) {
}
public static List<CommandInvocation> assemble(List<Cmd> cmds, Function<String, NBBufferedContainer> ctxprovider) {
public static List<Cmd> assemble(List<Cmd> cmds, Function<String, NBBufferedContainer> ctxprovider) {
List<Cmd> mappedCmds = tagCommandsWithContext(cmds);
List<CommandInvocation> invocations = prepareMappedPhases(mappedCmds, ctxprovider);
return invocations;
NBCoreInvokableResolver core_resolver = new NBCoreInvokableResolver();
for (Cmd mappedCmd : mappedCmds) {
core_resolver.verify(mappedCmd);
}
return mappedCmds;
// List<CommandInvocation> invocations = prepareMappedPhases(mappedCmds, ctxprovider);
// return invocations;
}
private static List<Cmd> tagCommandsWithContext(List<Cmd> cmds) {
@ -62,6 +82,40 @@ public class NBCommandAssembly {
return new ArrayList<>(tagged);
}
public static NBInvokableCommand resolve(Cmd cmd, Function<String, NBBufferedContainer> ctxProvider) {
try {
NBCommandParams params = switch (cmd.getCmdType()) {
case indirect, java, container -> NBCommandParams.of(cmd.getArgMap());
default -> NBCommandParams.of(Map.of());
};
String targetContext = cmd.getTargetContext();
NBInvokableCommand command = core_resolver.resolve(cmd, ctxProvider.apply(targetContext), cmd.getStepName());
return command;
} catch (Exception e) {
throw new UnresolvedCommand(cmd, e);
}
}
public static CommandInvocation assembleCommand(Cmd cmd, Function<String, NBBufferedContainer> ctxProvider) {
NBCommandParams params = switch (cmd.getCmdType()) {
case indirect, java, container -> NBCommandParams.of(cmd.getArgMap());
default -> NBCommandParams.of(Map.of());
};
String targetContext = cmd.getTargetContext();
NBInvokableCommand command = core_resolver.resolve(cmd, ctxProvider.apply(targetContext), cmd.getStepName());
if (command == null) {
throw new BasicError("Found zero commands for spec;" + cmd);
}
String containerName = cmd.getTargetContext();
// TODO, make this unnecessary by moving the impl out of the map to a dedicated cmd structure
params.remove("_impl");
return new CommandInvocation(command, params, containerName);
}
private static List<CommandInvocation> prepareMappedPhases(List<Cmd> mappedCmds, Function<String, NBBufferedContainer> ctxProvider) {
List<CommandInvocation> parameterizedInvocations = new ArrayList<>();
NBCoreInvokableResolver core_resolver = new NBCoreInvokableResolver();
@ -73,7 +127,7 @@ public class NBCommandAssembly {
String targetContext = cmd.getTargetContext();
NBInvokableCommand command = core_resolver.resolve(cmd, ctxProvider.apply(targetContext), cmd.getStepName());
if (command==null) {
if (command == null) {
throw new BasicError("Found zero commands for spec;" + cmd);
}
String containerName = cmd.getTargetContext();

View File

@ -16,6 +16,7 @@
package io.nosqlbench.engine.core.lifecycle.session;
import io.nosqlbench.engine.core.lifecycle.scenario.container.NBCommandParams;
import io.nosqlbench.engine.core.lifecycle.scenario.execution.NBInvokableCommand;
import io.nosqlbench.nb.api.components.status.NBHeartbeatComponent;
import io.nosqlbench.nb.api.engine.activityimpl.ActivityDef;
@ -82,23 +83,22 @@ public class NBSession extends NBHeartbeatComponent implements Function<List<Cmd
// TODO: add container closing command
// TODO: inject container closing commands after the last command referencing each container
List<NBCommandAssembly.CommandInvocation> invocationCalls = NBCommandAssembly.assemble(cmds, this::getContext);
List<Cmd> assembledCommands = NBCommandAssembly.assemble(cmds, this::getContext);
ResultCollector collector = new ResultCollector();
// TODO: When a command is not successful, automatically break out of the command loop
try (ResultContext results = new ResultContext(collector).ok()) {
for (NBCommandAssembly.CommandInvocation invocation : invocationCalls) {
String targetContext = invocation.containerName();
String explanation = "in context '" + targetContext + "'";
try (NBInvokableCommand command = invocation.command()) {
explanation += " command '" + command.toString() + "'";
NBBufferedContainer container = getContext(targetContext);
NBCommandResult cmdResult = container.apply(command, invocation.params());
for (Cmd cmd : assembledCommands) {
String explanation = " in context " + cmd.getTargetContext() + ", command '" + cmd.toString() + "'";
try (NBInvokableCommand command = NBCommandAssembly.resolve(cmd,this::getContext)) {
NBCommandParams params = NBCommandAssembly.paramsFor(cmd);
NBBufferedContainer container = getContext(cmd.getTargetContext());
NBCommandResult cmdResult = container.apply(command, params);
results.apply(cmdResult);
if (cmdResult.hasException()) {
throw cmdResult.getException();
}
} catch (Exception e) {
String msg = "While running " + explanation + "', an error occurred: " + e.toString();
String msg = "While running " + explanation + ", an error occurred: " + e.toString();
results.error(e);
onError(e);
logger.error(msg);

View File

@ -0,0 +1,47 @@
/*
* Copyright (c) 2024 nosqlbench
*
* 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.
*/
package io.nosqlbench.engine.core.lifecycle.session;
import io.nosqlbench.engine.cmdstream.Cmd;
import io.nosqlbench.engine.cmdstream.CmdArg;
import io.nosqlbench.nb.api.labels.NBLabeledElement;
import io.nosqlbench.nb.api.labels.NBLabels;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
public class NBSessionTest implements NBLabeledElement {
@Test
public void testThatSessionShortCircuitsCommandErrors() {
NBSession session = new NBSession(this, "session_name", Map.of());
Cmd c1 = new Cmd("test_ok", Map.of("test_ok", CmdArg.of("test_ok","key1","===","value1")));
Cmd c2 = new Cmd("test_error", Map.of());
Cmd c3 = new Cmd("test_ok", Map.of("test_ok", CmdArg.of("test_ok","key2","===","value2")));
List<Cmd> cmdStream = List.of(c1, c2, c3);
session.apply(cmdStream);
}
@Override
public NBLabels getLabels() {
return NBLabels.forKV("testing","session");
}
}