From 34e6767b0a482fddaa84839df697467d58b64538 Mon Sep 17 00:00:00 2001 From: Jonathan Shook Date: Fri, 15 Dec 2023 23:10:44 -0600 Subject: [PATCH] implement cmd stop directly --- .../engine/cli/TestNBCLIOptions.java | 20 +++---- .../engine/cmdstream/BasicScriptBuffer.java | 2 +- .../io/nosqlbench/engine/cmdstream/Cmd.java | 58 ++----------------- .../nosqlbench/engine/cmdstream/CmdType.java | 2 +- .../core/lifecycle/commands/CMD_stop.java | 57 ++++++++++++++++++ .../core/lifecycle/commands/INFO_stop.java | 29 ++++++++++ .../session/NBScriptCommandResolver.java | 2 +- 7 files changed, 103 insertions(+), 67 deletions(-) create mode 100644 engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/commands/CMD_stop.java create mode 100644 engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/commands/INFO_stop.java diff --git a/engine-cli/src/test/java/io/nosqlbench/engine/cli/TestNBCLIOptions.java b/engine-cli/src/test/java/io/nosqlbench/engine/cli/TestNBCLIOptions.java index 6e0322015..0aa51f465 100644 --- a/engine-cli/src/test/java/io/nosqlbench/engine/cli/TestNBCLIOptions.java +++ b/engine-cli/src/test/java/io/nosqlbench/engine/cli/TestNBCLIOptions.java @@ -155,16 +155,16 @@ public class TestNBCLIOptions { } - @Test - public void shouldRecognizeStopActivityCmd() { - NBCLIOptions opts = new NBCLIOptions(new String[]{ "stop", "activity=woah" }); - List cmds = opts.getCommands(); - assertThat(cmds).hasSize(1); - assertThat(cmds.get(0).getCmdType()).isEqualTo(CmdType.stop); - assertThat(cmds.get(0).getArgValue("activity")).isEqualTo("woah"); - - } - +// @Test +// public void shouldRecognizeStopActivityCmd() { +// NBCLIOptions opts = new NBCLIOptions(new String[]{ "stop", "activity=woah" }); +// List cmds = opts.getCommands(); +// assertThat(cmds).hasSize(1); +// assertThat(cmds.get(0).getCmdType()).isEqualTo(CmdType.stop); +// assertThat(cmds.get(0).getArgValue("activity")).isEqualTo("woah"); +// +// } +// @Disabled("semantic parsing is deferred until later") @Test public void shouldThrowErrorForInvalidStopActivity() { diff --git a/engine-core/src/main/java/io/nosqlbench/engine/cmdstream/BasicScriptBuffer.java b/engine-core/src/main/java/io/nosqlbench/engine/cmdstream/BasicScriptBuffer.java index 98e6b2c22..1e7c26fa4 100644 --- a/engine-core/src/main/java/io/nosqlbench/engine/cmdstream/BasicScriptBuffer.java +++ b/engine-core/src/main/java/io/nosqlbench/engine/cmdstream/BasicScriptBuffer.java @@ -77,7 +77,7 @@ public class BasicScriptBuffer implements ScriptBuffer { case start: // start activity case run: // run activity case await: // await activity - case stop: // stop activity +// case stop: // stop activity case forceStop: // force stopping activity case waitMillis: diff --git a/engine-core/src/main/java/io/nosqlbench/engine/cmdstream/Cmd.java b/engine-core/src/main/java/io/nosqlbench/engine/cmdstream/Cmd.java index 89df1b96d..8cd551e22 100644 --- a/engine-core/src/main/java/io/nosqlbench/engine/cmdstream/Cmd.java +++ b/engine-core/src/main/java/io/nosqlbench/engine/cmdstream/Cmd.java @@ -153,60 +153,6 @@ public class Cmd { return sb.toString(); } - -// public static Cmd parseArg(LinkedList arglist, PathCanonicalizer fixer) { -// -// String cmdName = arglist.removeFirst(); -// CmdType cmdType = CmdType.valueOfAnyCaseOrIndirect(cmdName); -// -// Map params = new LinkedHashMap<>(); -// -// for (CmdParam cmdParam : cmdType.getPositionalArgs()) { -// -// String nextarg = arglist.peekFirst(); -// -// if (nextarg == null) { -// throw new InvalidParameterException( -// "command '" + cmdName + " requires a value for " + cmdParam.name -// + ", but there were no remaining arguments after it."); -// } else if (cmdParam.freeform) { -// logger.debug(() -> "freeform parameter:" + nextarg); -// } else if (nextarg.contains("=")) { -// throw new InvalidParameterException( -// "command '" + cmdName + "' requires a value for " + cmdParam.name + -// ", but a named parameter was found instead: " + nextarg); -// } else if (CmdType.anyMatches(nextarg)) { -// throw new InvalidParameterException( -// "command '" + cmdName + "' requires a value for " + cmdParam.name -// + ", but a reserved word was found instead: " + nextarg); -// } -// -// logger.debug(() -> "cmd name:" + cmdName + ", positional " + cmdParam.name + ": " + nextarg); -// params.put(cmdParam.name, cmdParam.converter.apply(arglist.removeFirst()).toString()); -// } -// -// while (arglist.size() > 0 && -// !CmdType.anyMatches(arglist.peekFirst()) -// && arglist.peekFirst().contains("=")) { -// String arg = arglist.removeFirst(); -// String[] assigned = arg.split("=", 2); -// String pname = assigned[0]; -// String pval = assigned[1]; -// -// -// if (pname.equals("yaml") || pname.equals("workload")) { -// pval = fixer.canonicalizePath(pval); -// } -// if (params.containsKey(pname)) { -// throw new InvalidParameterException("parameter '" + pname + "' is already set for '" + cmdType + "' command. For each command," + -// " a named parameter may only be set once. Multiple occurrences are disallowed to avoid errors or ambiguity."); -// } -// params.put(pname, pval); -// } -// -// return new Cmd(cmdType, params); -// } - public static String toJSONBlock(Map map, boolean oneline) { int klen = map.keySet().stream().mapToInt(String::length).max().orElse(1); @@ -247,6 +193,10 @@ public class Cmd { return argmap; } + public String getStepName() { + return this.stepName; + } + // public static List parseCmds(String... arglist) { // LinkedList ll = new LinkedList<>(Arrays.asList(arglist)); // List cmds = new ArrayList<>(); diff --git a/engine-core/src/main/java/io/nosqlbench/engine/cmdstream/CmdType.java b/engine-core/src/main/java/io/nosqlbench/engine/cmdstream/CmdType.java index 044fa406e..5cf98c180 100644 --- a/engine-core/src/main/java/io/nosqlbench/engine/cmdstream/CmdType.java +++ b/engine-core/src/main/java/io/nosqlbench/engine/cmdstream/CmdType.java @@ -25,7 +25,7 @@ package io.nosqlbench.engine.cmdstream; public enum CmdType { run(), start(), - stop(CmdParam.of("activity")), +// stop(CmdParam.of("activity")), forceStop(CmdParam.of("activity")), script(CmdParam.of("path", s -> s)), java(CmdParam.of("class", s -> s)), diff --git a/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/commands/CMD_stop.java b/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/commands/CMD_stop.java new file mode 100644 index 000000000..20eabe01a --- /dev/null +++ b/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/commands/CMD_stop.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 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.commands; + +import io.nosqlbench.engine.api.activityapi.core.Activity; +import io.nosqlbench.engine.core.lifecycle.scenario.container.ContainerActivitiesController; +import io.nosqlbench.engine.core.lifecycle.scenario.container.NBBufferedContainer; +import io.nosqlbench.engine.core.lifecycle.scenario.container.NBCommandParams; +import io.nosqlbench.engine.core.lifecycle.scenario.execution.NBBaseCommand; +import io.nosqlbench.nb.annotations.Service; +import io.nosqlbench.nb.api.errors.BasicError; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.PrintWriter; +import java.io.Reader; +import java.util.Optional; + +@Service(value = NBBaseCommand.class,selector = "stop") +public class CMD_stop extends NBBaseCommand { + public final static Logger logger = LogManager.getLogger("stop"); + + public CMD_stop(NBBufferedContainer parentComponent, String stepName, String targetScenario) { + super(parentComponent, stepName, targetScenario); + } + + @Override + public Object invoke(NBCommandParams params, PrintWriter stdout, PrintWriter stderr, Reader stdin, ContainerActivitiesController controller) { + String activityName + = params.maybeGet("activity").orElseThrow( + () -> new RuntimeException("The stop command requires an 'activity' parameter") + ); + Optional activity = controller.getActivity(activityName); + if (activity.isEmpty()) { + BasicError error = new BasicError("Activity '" + activityName + "' was not found for stop command."); + logger.warn(error); + throw error; + } + + controller.stop(activityName); + return null; + } +} diff --git a/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/commands/INFO_stop.java b/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/commands/INFO_stop.java new file mode 100644 index 000000000..96b48f384 --- /dev/null +++ b/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/commands/INFO_stop.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 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.commands; + +import io.nosqlbench.engine.core.lifecycle.scenario.execution.NBCommandInfo; +import io.nosqlbench.engine.core.lifecycle.scenario.execution.NBInvokableCommand; +import io.nosqlbench.nb.annotations.Service; + +@Service(value = NBCommandInfo.class,selector = "stop") +public class INFO_stop extends NBCommandInfo { + @Override + public Class getType() { + return CMD_stop.class; + } +} diff --git a/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/session/NBScriptCommandResolver.java b/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/session/NBScriptCommandResolver.java index c178e18fb..842ac09fe 100644 --- a/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/session/NBScriptCommandResolver.java +++ b/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/session/NBScriptCommandResolver.java @@ -27,7 +27,7 @@ public class NBScriptCommandResolver implements NBInvokableResolver { @Override public NBInvokableCommand resolve(Cmd cmd, NBBufferedContainer parent, String phaseName) { return switch (cmd.getCmdType()) { - case run, await, forceStop, stop, start, waitMillis, fragment, script-> + case run, await, forceStop, start, waitMillis, fragment, script-> new NBScriptedCommand(parent, phaseName, cmd.getTargetContext()).add(cmd); // case fragment -> // new NBScriptedCommand(parent, phaseName, cmd.getTargetContext()).addScriptText(cmd.getArgValue("fragment"));