From 284e86e64faef0a0ef210a765df78970679451bd Mon Sep 17 00:00:00 2001 From: Jonathan Shook Date: Thu, 22 Jul 2021 17:40:06 -0500 Subject: [PATCH] make config model construction more uniform --- .../functions/to_tuple/CustomFunc955.java | 31 +++++++------ .../annotator/GrafanaMetricsAnnotator.java | 43 +++++++++---------- .../engine/core/metrics/LoggingAnnotator.java | 13 +++--- 3 files changed, 42 insertions(+), 45 deletions(-) diff --git a/driver-cql-shaded/src/main/java/io/nosqlbench/activitytype/cql/datamappers/functions/to_tuple/CustomFunc955.java b/driver-cql-shaded/src/main/java/io/nosqlbench/activitytype/cql/datamappers/functions/to_tuple/CustomFunc955.java index d7e6db213..034379a28 100644 --- a/driver-cql-shaded/src/main/java/io/nosqlbench/activitytype/cql/datamappers/functions/to_tuple/CustomFunc955.java +++ b/driver-cql-shaded/src/main/java/io/nosqlbench/activitytype/cql/datamappers/functions/to_tuple/CustomFunc955.java @@ -5,11 +5,14 @@ import com.datastax.driver.core.DataType; import com.datastax.driver.core.TupleType; import com.datastax.driver.core.TupleValue; import io.nosqlbench.nb.api.config.standard.ConfigModel; -import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper; -import io.nosqlbench.nb.api.config.standard.NBMapConfigurable; import io.nosqlbench.nb.api.config.standard.NBConfigModel; +import io.nosqlbench.nb.api.config.standard.NBMapConfigurable; +import io.nosqlbench.nb.api.config.standard.Param; +import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper; -import java.util.*; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; import java.util.function.LongFunction; import java.util.function.LongToIntFunction; import java.util.function.LongUnaryOperator; @@ -21,14 +24,14 @@ import java.util.function.LongUnaryOperator; * * Functions are required for: *
  • - *
  • map size {@code (LongToIntFunction)}
  • - *
  • key {@code (LongFunction)} - *
  • tuple field 1 {@code (LongToIntFunction)}
  • - *
  • tuple field 2 {@code {LongToIntFunction)}
  • + *
  • map size {@code (LongToIntFunction)}
  • + *
  • key {@code (LongFunction)} + *
  • tuple field 1 {@code (LongToIntFunction)}
  • + *
  • tuple field 2 {@code {LongToIntFunction)}
  • * */ @ThreadSafeMapper -public class CustomFunc955 implements LongFunction>, NBMapConfigurable { +public class CustomFunc955 implements LongFunction>, NBMapConfigurable { private final LongToIntFunction sizefunc; private final LongFunction keyfunc; @@ -56,16 +59,16 @@ public class CustomFunc955 implements LongFunction>, NBMapConfigurable } @Override - public Map apply(long value) { + public Map apply(long value) { int size = sizefunc.applyAsInt(value); HashMap map = new HashMap<>(size); for (int i = 0; i < size; i++) { - String key = keyfunc.apply(value+i).toString(); - int tuple1 = field1func.applyAsInt(value+i); - long tuple2 = field2func.applyAsLong(value+i); + String key = keyfunc.apply(value + i).toString(); + int tuple1 = field1func.applyAsInt(value + i); + long tuple2 = field2func.applyAsLong(value + i); TupleValue tupleValue = tupleType.newValue(tuple1, tuple2); - map.put(key,tupleValue); + map.put(key, tupleValue); } return map; } @@ -81,7 +84,7 @@ public class CustomFunc955 implements LongFunction>, NBMapConfigurable @Override public NBConfigModel getConfigModel() { return ConfigModel.of(this.getClass()) - .optional("", Cluster.class) + .add(Param.optional("", Cluster.class)) .asReadOnly(); } } diff --git a/engine-clients/src/main/java/io/nosqlbench/engine/clients/grafana/annotator/GrafanaMetricsAnnotator.java b/engine-clients/src/main/java/io/nosqlbench/engine/clients/grafana/annotator/GrafanaMetricsAnnotator.java index 51c498c0c..38e909801 100644 --- a/engine-clients/src/main/java/io/nosqlbench/engine/clients/grafana/annotator/GrafanaMetricsAnnotator.java +++ b/engine-clients/src/main/java/io/nosqlbench/engine/clients/grafana/annotator/GrafanaMetricsAnnotator.java @@ -9,10 +9,7 @@ import io.nosqlbench.nb.api.SystemId; import io.nosqlbench.nb.api.annotations.Annotation; import io.nosqlbench.nb.api.annotations.Annotator; import io.nosqlbench.nb.api.config.params.ParamsParser; -import io.nosqlbench.nb.api.config.standard.ConfigModel; -import io.nosqlbench.nb.api.config.standard.NBConfigModel; -import io.nosqlbench.nb.api.config.standard.NBConfigurable; -import io.nosqlbench.nb.api.config.standard.NBConfiguration; +import io.nosqlbench.nb.api.config.standard.*; import io.nosqlbench.nb.api.errors.BasicError; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -111,7 +108,7 @@ public class GrafanaMetricsAnnotator implements Annotator, NBConfigurable { public void applyConfig(NBConfiguration cfg) { GrafanaClientConfig gc = new GrafanaClientConfig(); - gc.setBaseUri(cfg.param("baseurl", String.class)); + gc.setBaseUri(cfg.get("baseurl")); cfg.getOptional("tags") .map(t -> ParamsParser.parse(t, false)) @@ -133,7 +130,7 @@ public class GrafanaMetricsAnnotator implements Annotator, NBConfigurable { Optional optionalApikey = cfg.getOptional("apikey"); if (optionalApikeyfile.isPresent()) { - keyfilePath=optionalApikeyfile.map(Path::of).orElseThrow(); + keyfilePath = optionalApikeyfile.map(Path::of).orElseThrow(); } else if (optionalApikey.isPresent()) { gc.addHeaderSource(() -> Map.of("Authorization", "Bearer " + optionalApikey.get())); } else { @@ -151,7 +148,7 @@ public class GrafanaMetricsAnnotator implements Annotator, NBConfigurable { } private void setOnError(OnError onError) { - this.onError=onError; + this.onError = onError; } private void setTags(Map tags) { @@ -161,22 +158,22 @@ public class GrafanaMetricsAnnotator implements Annotator, NBConfigurable { @Override public NBConfigModel getConfigModel() { return ConfigModel.of(this.getClass()) - .required("baseurl", String.class, - "The base url of the grafana node, like http://localhost:3000/") - .defaults("apikeyfile", "$NBSTATEDIR/grafana/grafana_apikey", - "The file that contains the api key, supersedes apikey") - .optional("apikey", String.class, - "The api key to use, supersedes basic username and password") - .optional("username", String.class, - "The username to use for basic auth") - .optional("password", String.class, - "The password to use for basic auth") - .defaults("tags", "source:nosqlbench", - "The tags that identify the annotations, in k:v,... form") - .defaults("onerror", "warn", - "What to do when an error occurs while posting an annotation") - .defaults("timeoutms", 5000, - "connect and transport timeout for the HTTP client") + .add(Param.required("baseurl", String.class) + .setDescription("The base url of the grafana node, like http://localhost:3000/")) + .add(Param.defaultTo("apikeyfile", "$NBSTATEDIR/grafana/grafana_apikey") + .setDescription("The file that contains the api key, supersedes apikey")) + .add(Param.optional("apikey", String.class) + .setDescription("The api key to use, supersedes basic username and password")) + .add(Param.optional("username", String.class) + .setDescription("The username to use for basic auth")) + .add(Param.optional("password", String.class) + .setDescription("The password to use for basic auth")) + .add(Param.defaultTo("tags", "source:nosqlbench") + .setDescription("The tags that identify the annotations, in k:v,... form")) + .add(Param.defaultTo("onerror", "warn") + .setDescription("What to do when an error occurs while posting an annotation")) + .add(Param.defaultTo("timeoutms", 5000) + .setDescription("connect and transport timeout for the HTTP client")) .asReadOnly(); } diff --git a/engine-core/src/main/java/io/nosqlbench/engine/core/metrics/LoggingAnnotator.java b/engine-core/src/main/java/io/nosqlbench/engine/core/metrics/LoggingAnnotator.java index b11495ec4..723d3cc17 100644 --- a/engine-core/src/main/java/io/nosqlbench/engine/core/metrics/LoggingAnnotator.java +++ b/engine-core/src/main/java/io/nosqlbench/engine/core/metrics/LoggingAnnotator.java @@ -3,10 +3,7 @@ package io.nosqlbench.engine.core.metrics; import io.nosqlbench.nb.annotations.Service; import io.nosqlbench.nb.api.annotations.Annotation; import io.nosqlbench.nb.api.annotations.Annotator; -import io.nosqlbench.nb.api.config.standard.NBMapConfigurable; -import io.nosqlbench.nb.api.config.standard.ConfigModel; -import io.nosqlbench.nb.api.config.standard.NBConfigModel; -import io.nosqlbench.nb.api.config.standard.NBConfiguration; +import io.nosqlbench.nb.api.config.standard.*; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -35,16 +32,16 @@ public class LoggingAnnotator implements Annotator, NBMapConfigurable { public void applyConfig(Map providedConfig) { NBConfigModel configModel = getConfigModel(); NBConfiguration cfg = configModel.apply(providedConfig); - String levelName = cfg.param("level", String.class); + String levelName = cfg.get("level"); this.level = Level.valueOf(levelName); } @Override public NBConfigModel getConfigModel() { return ConfigModel.of(this.getClass()) - .defaults("level", "INFO", - "The logging level to use for this annotator") - .asReadOnly(); + .add(Param.defaultTo("level", "INFO") + .setDescription("The logging level to use for this annotator")) + .asReadOnly(); } }