Mmuzaf commented on code in PR #4263:
URL: https://github.com/apache/cassandra/pull/4263#discussion_r2219964702
##########
src/java/org/apache/cassandra/tools/nodetool/GuardrailsConfigCommand.java:
##########
@@ -59,51 +61,107 @@ public static class GetGuardrailsConfig extends
GuardrailsConfigCommand
allowedValues = { "values", "thresholds", "flags", "others" })
private String guardrailCategory;
- @Arguments(description = "Specific names or guardrails to get
configuration of.")
+ @Option(name = { "--verbose", "-v" })
+ private boolean verbose = false;
+
+ @Arguments(description = "Specific name of a guardrail to get
configuration of.")
private List<String> args = new ArrayList<>();
@Override
public void execute(NodeProbe probe)
{
GuardrailCategory categoryEnum =
GuardrailCategory.parseCategory(guardrailCategory, probe.output().out);
- if (!args.isEmpty() && categoryEnum != null)
+ if (args.size() > 1)
+ throw new IllegalStateException("Specify only one guardrail
name to get the configuration of or no name to get the configuration of all of
them.");
+
+ String guardrailName = !args.isEmpty() ? args.get(0) : null;
+
+ if (guardrailName != null && categoryEnum != null)
throw new IllegalStateException("Do not specify additional
arguments when --category/-c is set.");
- List<Method> allGetters =
stream(probe.getGuardrailsMBean().getClass().getDeclaredMethods())
- .filter(method ->
method.getName().startsWith("get")
- &&
!method.getName().endsWith("CSV"))
- .filter(method -> args.isEmpty() ||
args.contains(toSnakeCase(method.getName().substring(3))))
- .sorted(comparing(Method::getName))
- .collect(toList());
+ Map<String, List<Method>> allGetters =
stream(probe.getGuardrailsMBean().getClass().getDeclaredMethods())
+ .filter(method ->
method.getName().startsWith("get")
+ &&
!method.getName().endsWith("CSV")
+ &&
!(method.getName().endsWith("WarnThreshold") ||
method.getName().endsWith("FailThreshold")))
+ .filter(method ->
guardrailName == null ||
guardrailName.equals(toSnakeCase(method.getName().substring(3))))
+
.collect(Collectors.groupingBy(method ->
toSnakeCase(method.getName().substring(3))));
+
+ Map<String, List<Method>> thresholds =
stream(probe.getGuardrailsMBean().getClass().getDeclaredMethods())
+ .filter(method ->
method.getName().startsWith("get")
+ &&
!method.getName().endsWith("CSV")
+ &&
(method.getName().endsWith("WarnThreshold") ||
method.getName().endsWith("FailThreshold")))
+ .filter(method -> {
+ if (guardrailName ==
null)
+ return true;
+
+ String snakeCase =
toSnakeCase(method.getName().substring(3));
+ String
snakeCaseSuccinct = snakeCase.replace("_warn_", "_")
+
.replace("_fail_", "_");
+
+ return
guardrailName.equals(snakeCase) || guardrailName.equals(snakeCaseSuccinct);
+ })
+
.sorted(comparing(Method::getName))
+
.collect(Collectors.groupingBy(method -> {
+ String methodName =
method.getName().substring(3);
+ String snakeCase =
toSnakeCase(methodName);
+ if
(snakeCase.endsWith("warn_threshold"))
+ return
snakeCase.replaceAll("_warn_", "_");
+ else
+ return
snakeCase.replaceAll("_fail_", "_");
+ }));
+
+ allGetters.putAll(thresholds);
+
+ allGetters = allGetters.entrySet()
+ .stream()
+ .sorted(Map.Entry.comparingByKey())
+ .collect(Collectors.toMap(
+ Map.Entry::getKey,
+ Map.Entry::getValue,
+ (e1, e2) -> e1,
+ LinkedHashMap::new
+ ));
+
+ if (allGetters.isEmpty())
+ {
+ assert guardrailName != null;
+ throw new IllegalStateException(String.format("Guardrail %s
not found.", guardrailName));
+ }
- display(probe, allGetters, categoryEnum);
+ display(probe, allGetters, categoryEnum, verbose);
}
@Override
- public void addRow(List<InternalRow> bucket, GuardrailsMBean mBean,
Method method, String guardrailName) throws Throwable
+ public void addRow(List<InternalRow> bucket, GuardrailsMBean mBean,
List<Method> methods, String guardrailName) throws Throwable
{
- Class<?> returnType = method.getReturnType();
- Object value = method.invoke(mBean);
-
- if (returnType.equals(int.class) ||
returnType.equals(Integer.class)
- || returnType.equals(long.class) ||
returnType.equals(Long.class)
- || returnType.equals(boolean.class) ||
returnType.equals(Boolean.class)
- || returnType.equals(Set.class))
- {
- constructRow(bucket, guardrailName, value.toString());
- }
- else if (returnType.equals(String.class))
+ List<String> values = new ArrayList<>();
+ for (Method method : methods)
{
- if (value == null || value.toString().isEmpty())
- constructRow(bucket, guardrailName, "null");
+ Class<?> returnType = method.getReturnType();
+ Object value = method.invoke(mBean);
+
+ if (returnType.equals(int.class) ||
returnType.equals(Integer.class)
+ || returnType.equals(long.class) ||
returnType.equals(Long.class)
+ || returnType.equals(boolean.class) ||
returnType.equals(Boolean.class)
+ || returnType.equals(Set.class))
+ {
+ values.add(value.toString());
+ }
+ else if (returnType.equals(String.class))
+ {
+ if (value == null || value.toString().isEmpty())
Review Comment:
Should we return an empty string `""`? `null` can be confusing when the
return value is truly empty.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]