nosqlbench-2149 Label values should not be constrained

This commit is contained in:
Jonathan Shook 2025-01-16 13:54:04 -06:00
parent bc759c0e81
commit 0eeca86d99
5 changed files with 154 additions and 6 deletions

View File

@ -56,6 +56,22 @@ public class NBAdvisorPoint<T> extends NBAdvisorPointOrBuilder<T> {
conditions = newConditions;
}
public Result<T>[] validateAllBut(Collection<T> elements, T... ignored) {
Set<T> ignoreSet = new HashSet(Arrays.asList(ignored));
List<T> toValidate = new ArrayList<>();
for (T element : elements) {
if (!ignoreSet.contains(element)) {
toValidate.add(element);
}
}
return validateAll(toValidate);
}
public Result<T>[] validateOptional(Optional<T> element) {
if (element.isPresent()) {
return validate(element.get());
}
return new Result[0];
}
public Result<T>[] validateAll(Collection<T> elements) {
List<Result<T>> buffer = new ArrayList<>();
for (T element : elements) {

View File

@ -0,0 +1,63 @@
package io.nosqlbench.nb.api.advisor.conditions;
/*
* Copyright (c) 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.
*/
import io.nosqlbench.nb.api.advisor.NBAdvisorCondition;
import org.apache.logging.log4j.Level;
import java.util.function.Function;
import java.util.regex.Pattern;
public class ValidOpenMetricLabel implements NBAdvisorCondition<String> {
private final static Pattern pattern = Pattern.compile("(?<name>[a-zA-Z_][a-zA-Z0-9_]*)");
private final Level level;
public ValidOpenMetricLabel(Level level) {
this.level = level;
}
private static String according_to = " according to the OpenMetrics specification";
@Override
public Function<String, String> okMsg() {
return string -> "String '" + string + "' is a valid label" + according_to;
}
@Override
public Function<String, String> errMsg() {
return string -> "String '" +string + "' is not a valid label" +according_to;
}
@Override
public Level level() {
return level;
}
@Override
public String getName() {
return "valid metric label";
}
@Override
public boolean test(String s) {
return !pattern.matcher(s).matches();
}
}

View File

@ -0,0 +1,64 @@
package io.nosqlbench.nb.api.advisor.conditions;
/*
* Copyright (c) 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.
*/
import io.nosqlbench.nb.api.advisor.NBAdvisorCondition;
import org.apache.logging.log4j.Level;
import java.util.function.Function;
import java.util.regex.Pattern;
public class ValidOpenMetricName implements NBAdvisorCondition<String> {
private final static Pattern pattern =
Pattern.compile("(?<name>[a-zA-Z_:][a-zA-Z0-9_" + ":]*)");
private final Level level;
public ValidOpenMetricName(Level level) {
this.level = level;
}
private static String according_to = " according to the OpenMetrics specification";
@Override
public Function<String, String> okMsg() {
return string -> "String '" + string + "' is a valid metric name" + according_to;
}
@Override
public Function<String, String> errMsg() {
return string -> "String '" + string + "' is not a valid metric name" + according_to;
}
@Override
public Level level() {
return level;
}
@Override
public String getName() {
return "valid metric name";
}
@Override
public boolean test(String s) {
return !pattern.matcher(s).matches();
}
}

View File

@ -64,9 +64,8 @@ public class NBBaseComponent extends NBBaseComponentMetrics implements NBCompone
labelsAdvisor.add(Conditions.NoSpacesWarning);
labelsAdvisor.validateAll(componentSpecificLabelsOnly.asMap().keySet());
labelsAdvisor.validateAll(componentSpecificLabelsOnly.asMap().values());
labelsAdvisor.setName("Labels", "Check label names and values")
labelsAdvisor.setName("Labels", "Check labels")
.logName();
NBAdvisorResults advisorResults = getAdvisorResults();
advisorResults.evaluate();

View File

@ -83,16 +83,22 @@ class ActivityExecutorTest {
// }
@Test
synchronized void testAdvisorError() {
synchronized void testLabelingError() {
// TODO improve contextual labeling assertions
try {
ActivityDef activityDef = ActivityDef.parseActivityDef("driver=diag;alias=test-delayed-start;cycles=1000;initdelay=2000;");
ActivityDef activityDef = ActivityDef.parseActivityDef("driver=diag;"
+ "alias=test-delayed-start;"
+ "cycles=1000;initdelay=2000;"
+ "labels=invalid-name:valid"
+ "-value");
new ActivityTypeLoader().load(activityDef, TestComponent.INSTANCE);
Activity activity = new DelayedInitActivity(activityDef);
fail("Expected an Advisor exception");
} catch (NBAdvisorException e) {
} catch (RuntimeException e) {
assertThat(e.toString().contains("error"));
assertThat(e.getExitCode() == 2);
// assertThat(e.getExitCode() == 2);
}
}