implement cmd stop directly

This commit is contained in:
Jonathan Shook
2023-12-15 23:10:44 -06:00
parent 06ef405b63
commit 34e6767b0a
7 changed files with 103 additions and 67 deletions

View File

@@ -155,16 +155,16 @@ public class TestNBCLIOptions {
} }
@Test // @Test
public void shouldRecognizeStopActivityCmd() { // public void shouldRecognizeStopActivityCmd() {
NBCLIOptions opts = new NBCLIOptions(new String[]{ "stop", "activity=woah" }); // NBCLIOptions opts = new NBCLIOptions(new String[]{ "stop", "activity=woah" });
List<Cmd> cmds = opts.getCommands(); // List<Cmd> cmds = opts.getCommands();
assertThat(cmds).hasSize(1); // assertThat(cmds).hasSize(1);
assertThat(cmds.get(0).getCmdType()).isEqualTo(CmdType.stop); // assertThat(cmds.get(0).getCmdType()).isEqualTo(CmdType.stop);
assertThat(cmds.get(0).getArgValue("activity")).isEqualTo("woah"); // assertThat(cmds.get(0).getArgValue("activity")).isEqualTo("woah");
//
} // }
//
@Disabled("semantic parsing is deferred until later") @Disabled("semantic parsing is deferred until later")
@Test @Test
public void shouldThrowErrorForInvalidStopActivity() { public void shouldThrowErrorForInvalidStopActivity() {

View File

@@ -77,7 +77,7 @@ public class BasicScriptBuffer implements ScriptBuffer {
case start: // start activity case start: // start activity
case run: // run activity case run: // run activity
case await: // await activity case await: // await activity
case stop: // stop activity // case stop: // stop activity
case forceStop: // force stopping activity case forceStop: // force stopping activity
case waitMillis: case waitMillis:

View File

@@ -153,60 +153,6 @@ public class Cmd {
return sb.toString(); return sb.toString();
} }
// public static Cmd parseArg(LinkedList<String> arglist, PathCanonicalizer fixer) {
//
// String cmdName = arglist.removeFirst();
// CmdType cmdType = CmdType.valueOfAnyCaseOrIndirect(cmdName);
//
// Map<String, String> 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<String, String> map, boolean oneline) { public static String toJSONBlock(Map<String, String> map, boolean oneline) {
int klen = map.keySet().stream().mapToInt(String::length).max().orElse(1); int klen = map.keySet().stream().mapToInt(String::length).max().orElse(1);
@@ -247,6 +193,10 @@ public class Cmd {
return argmap; return argmap;
} }
public String getStepName() {
return this.stepName;
}
// public static List<Cmd> parseCmds(String... arglist) { // public static List<Cmd> parseCmds(String... arglist) {
// LinkedList<String> ll = new LinkedList<>(Arrays.asList(arglist)); // LinkedList<String> ll = new LinkedList<>(Arrays.asList(arglist));
// List<Cmd> cmds = new ArrayList<>(); // List<Cmd> cmds = new ArrayList<>();

View File

@@ -25,7 +25,7 @@ package io.nosqlbench.engine.cmdstream;
public enum CmdType { public enum CmdType {
run(), run(),
start(), start(),
stop(CmdParam.of("activity")), // stop(CmdParam.of("activity")),
forceStop(CmdParam.of("activity")), forceStop(CmdParam.of("activity")),
script(CmdParam.of("path", s -> s)), script(CmdParam.of("path", s -> s)),
java(CmdParam.of("class", s -> s)), java(CmdParam.of("class", s -> s)),

View File

@@ -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> 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;
}
}

View File

@@ -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<? extends NBInvokableCommand> getType() {
return CMD_stop.class;
}
}

View File

@@ -27,7 +27,7 @@ public class NBScriptCommandResolver implements NBInvokableResolver {
@Override @Override
public NBInvokableCommand resolve(Cmd cmd, NBBufferedContainer parent, String phaseName) { public NBInvokableCommand resolve(Cmd cmd, NBBufferedContainer parent, String phaseName) {
return switch (cmd.getCmdType()) { 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); new NBScriptedCommand(parent, phaseName, cmd.getTargetContext()).add(cmd);
// case fragment -> // case fragment ->
// new NBScriptedCommand(parent, phaseName, cmd.getTargetContext()).addScriptText(cmd.getArgValue("fragment")); // new NBScriptedCommand(parent, phaseName, cmd.getTargetContext()).addScriptText(cmd.getArgValue("fragment"));