From dffdd70bed3bd16b3ec2fbbb1ead053a6da8e0a9 Mon Sep 17 00:00:00 2001 From: Jonathan Shook Date: Thu, 18 May 2023 14:57:42 -0500 Subject: [PATCH] scaffold labels into components --- .../java/io/nosqlbench/engine/cli/NBCLI.java | 22 +++++++--- .../nosqlbench/engine/cli/NBCLIOptions.java | 41 +++++++++++++++++++ .../core/lifecycle/scenario/Scenario.java | 31 ++++++++------ 3 files changed, 76 insertions(+), 18 deletions(-) diff --git a/engine-cli/src/main/java/io/nosqlbench/engine/cli/NBCLI.java b/engine-cli/src/main/java/io/nosqlbench/engine/cli/NBCLI.java index 7f252dd6b..38c9dc1bf 100644 --- a/engine-cli/src/main/java/io/nosqlbench/engine/cli/NBCLI.java +++ b/engine-cli/src/main/java/io/nosqlbench/engine/cli/NBCLI.java @@ -18,10 +18,11 @@ package io.nosqlbench.engine.cli; import io.nosqlbench.api.annotations.Annotation; import io.nosqlbench.api.annotations.Layer; +import io.nosqlbench.api.config.NBLabeledElement; +import io.nosqlbench.api.config.NBLabels; import io.nosqlbench.api.content.Content; import io.nosqlbench.api.content.NBIO; import io.nosqlbench.api.engine.metrics.ActivityMetrics; -import io.nosqlbench.api.engine.metrics.reporters.PromPushReporter; import io.nosqlbench.api.errors.BasicError; import io.nosqlbench.api.logging.NBLogLevel; import io.nosqlbench.api.metadata.SessionNamer; @@ -66,7 +67,7 @@ import java.util.ServiceLoader.Provider; import java.util.function.Function; import java.util.stream.Collectors; -public class NBCLI implements Function { +public class NBCLI implements Function, NBLabeledElement { private static Logger logger; private static final LoggerConfig loggerConfig; @@ -81,6 +82,9 @@ public class NBCLI implements Function { private final String commandName; + private NBLabels labels; + private String sessionName; + public NBCLI(final String commandName) { this.commandName = commandName; } @@ -95,7 +99,7 @@ public class NBCLI implements Function { */ public static void main(final String[] args) { try { - final NBCLI cli = new NBCLI("nb"); + final NBCLI cli = new NBCLI("nb5"); final int statusCode = cli.apply(args); System.exit(statusCode); } catch (final Exception e) { @@ -115,7 +119,7 @@ public class NBCLI implements Function { @Override public Integer apply(final String[] args) { try { - final NBCLI cli = new NBCLI("nb"); + final NBCLI cli = new NBCLI("nb5"); final int result = cli.applyDirect(args); return result; } catch (final Exception e) { @@ -149,7 +153,8 @@ public class NBCLI implements Function { NBCLI.loggerConfig.setConsoleLevel(NBLogLevel.ERROR); final NBCLIOptions globalOptions = new NBCLIOptions(args, Mode.ParseGlobalsOnly); - final String sessionName = SessionNamer.format(globalOptions.getSessionName()); + this.labels=NBLabels.forKV("command",commandName).and(globalOptions.getLabelMap()); + this.sessionName = SessionNamer.format(globalOptions.getSessionName()); NBCLI.loggerConfig .setSessionName(sessionName) @@ -432,7 +437,8 @@ public class NBCLI implements Function { options.getReportSummaryTo(), String.join("\n", args), options.getLogsDirectory(), - Maturity.Unspecified); + Maturity.Unspecified, + this); final ScriptBuffer buffer = new BasicScriptBuffer() .add(options.getCommands() @@ -504,4 +510,8 @@ public class NBCLI implements Function { return metrics; } + @Override + public NBLabels getLabels() { + return labels; + } } diff --git a/engine-cli/src/main/java/io/nosqlbench/engine/cli/NBCLIOptions.java b/engine-cli/src/main/java/io/nosqlbench/engine/cli/NBCLIOptions.java index 1687a225e..b5a09cd0d 100644 --- a/engine-cli/src/main/java/io/nosqlbench/engine/cli/NBCLIOptions.java +++ b/engine-cli/src/main/java/io/nosqlbench/engine/cli/NBCLIOptions.java @@ -51,6 +51,7 @@ public class NBCLIOptions { private static final String userHome = System.getProperty("user.home"); + private static final Map DEFAULT_LABELS=Map.of("appname","nosqlbench"); private static final String METRICS_PREFIX = "--metrics-prefix"; private static final String ANNOTATE_EVENTS = "--annotate"; private static final String ANNOTATORS_CONFIG = "--annotators"; @@ -82,6 +83,9 @@ public class NBCLIOptions { private static final String EXPERIMENTAL = "--experimental"; private static final String MATURITY = "--maturity"; + private static final String SET_LABELS = "--set-labels"; + private static final String ADD_LABELS = "--add-labels"; + // Execution private static final String EXPORT_CYCLE_LOG = "--export-cycle-log"; private static final String IMPORT_CYCLE_LOG = "--import-cycle-log"; @@ -132,6 +136,7 @@ public class NBCLIOptions { // private static final String DEFAULT_CONSOLE_LOGGING_PATTERN = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"; + private final Map labels = new LinkedHashMap<>(DEFAULT_LABELS); private final List cmdList = new ArrayList<>(); private int logsMax; private boolean wantsVersionShort; @@ -205,6 +210,9 @@ public class NBCLIOptions { return this.annotatorsConfig; } + public Map getLabelMap() { + return Collections.unmodifiableMap(this.labels); + } public String getChartHdrFileName() { return this.hdrForChartFileName; @@ -460,6 +468,16 @@ public class NBCLIOptions { arglist.removeFirst(); final String maturity = this.readWordOrThrow(arglist, "maturity of components to allow"); minMaturity = Maturity.valueOf(maturity.toLowerCase(Locale.ROOT)); + case NBCLIOptions.SET_LABELS: + arglist.removeFirst(); + String setLabelData = arglist.removeFirst(); + setLabels(setLabelData); + break; + case NBCLIOptions.ADD_LABELS: + arglist.removeFirst(); + String addLabeldata = arglist.removeFirst(); + addLabels(addLabeldata); + break; default: nonincludes.addLast(arglist.removeFirst()); } @@ -468,6 +486,29 @@ public class NBCLIOptions { return nonincludes; } + private void setLabels(String labeldata) { + this.labels.clear(); + addLabels(labeldata); + } + + private void addLabels(String labeldata) { + Map newLabels = parseLabels(labeldata); + this.labels.putAll(newLabels); + } + + private Map parseLabels(String labeldata) { + Map setLabelsTo = new LinkedHashMap<>(); + for (String component : labeldata.split("[,; ]")) { + String[] parts = component.split("\\W", 2); + if (parts.length!=2) { + throw new BasicError("Unable to parse labels to set:" + labeldata); + } + setLabelsTo.put(parts[0],parts[1]); + } + return setLabelsTo; + } + + private Path setStatePath() { if (0 < statePathAccesses.size()) throw new BasicError("The state dir must be set before it is used by other\n" + diff --git a/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/scenario/Scenario.java b/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/scenario/Scenario.java index 46c7cb1c4..641ec2b16 100644 --- a/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/scenario/Scenario.java +++ b/engine-core/src/main/java/io/nosqlbench/engine/core/lifecycle/scenario/Scenario.java @@ -74,6 +74,7 @@ public class Scenario implements Callable, NBLabeledElem private ScenarioMetadata scenarioMetadata; private ExecutionMetricsResult result; + private final NBLabeledElement parentComponent; public Optional getResultIfComplete() { return Optional.ofNullable(result); @@ -82,7 +83,7 @@ public class Scenario implements Callable, NBLabeledElem @Override public NBLabels getLabels() { - return NBLabels.forKV("scenario", this.scenarioName); + return this.parentComponent.getLabels().and("scenario", this.scenarioName); } public enum State { @@ -100,10 +101,10 @@ public class Scenario implements Callable, NBLabeledElem private ScenarioContext scriptEnv; private final String scenarioName; private ScriptParams scenarioScriptParams; - private String scriptfile; + private final String scriptfile; private Engine engine = Engine.Graalvm; - private boolean wantsStackTraces; - private boolean wantsCompiledScript; + private final boolean wantsStackTraces; + private final boolean wantsCompiledScript; private long startedAtMillis = -1L; private long endedAtMillis = -1L; @@ -121,7 +122,8 @@ public class Scenario implements Callable, NBLabeledElem final String reportSummaryTo, final String commandLine, final Path logsPath, - final Maturity minMaturity) { + final Maturity minMaturity, + NBLabeledElement parentComponent) { this.scenarioName = scenarioName; this.scriptfile = scriptfile; @@ -133,17 +135,22 @@ public class Scenario implements Callable, NBLabeledElem this.commandLine = commandLine; this.logsPath = logsPath; this.minMaturity = minMaturity; + this.parentComponent = parentComponent; } - public Scenario(final String name, final Engine engine, final String reportSummaryTo, final Maturity minMaturity) { - scenarioName = name; - this.reportSummaryTo = reportSummaryTo; - this.engine = engine; - commandLine = ""; - this.minMaturity = minMaturity; - logsPath = Path.of("logs"); + public static Scenario forTesting(final String name, final Engine engine, final String reportSummaryTo, final Maturity minMaturity) { + return new Scenario(name,null,engine,"console:10s",true,true,reportSummaryTo,"",Path.of("logs"),minMaturity, NBLabeledElement.forKV("test-name","name")); } +// public Scenario(final String name, final Engine engine, final String reportSummaryTo, final Maturity minMaturity) { +// scenarioName = name; +// this.reportSummaryTo = reportSummaryTo; +// this.engine = engine; +// commandLine = ""; +// this.minMaturity = minMaturity; +// logsPath = Path.of("logs"); +// } +// public Scenario setLogger(final Logger logger) { this.logger = logger; return this;