ddanielr commented on code in PR #4806:
URL: https://github.com/apache/accumulo/pull/4806#discussion_r1717135225
##########
server/base/src/main/java/org/apache/accumulo/server/util/Admin.java:
##########
@@ -1113,4 +1206,129 @@ private static void getAllFateIds(TabletMetadata
tabletMetadata,
fateIdConsumer.accept(tabletMetadata.getOperationId().getFateId());
}
}
+
+ @VisibleForTesting
+ public static void executeCheckCommand(ServerContext context, CheckCommand
cmd) {
+ List<CheckCommand.Check> checks;
+
+ validateAndTransformCheckCommand(cmd);
+
+ if (cmd.list) {
+ listChecks();
+ } else if (cmd.run) {
+ checks =
cmd.checks.stream().map(CheckCommand.Check::valueOf).collect(Collectors.toList());
+ executeRunCheckCommand(checks);
+ }
+ }
+
+ private static void validateAndTransformCheckCommand(CheckCommand cmd) {
+ Preconditions.checkArgument(cmd.list != cmd.run, "Must use either 'list'
or 'run'");
+ if (cmd.list) {
+ Preconditions.checkArgument(cmd.checks == null,
+ "'list' does not expect any further arguments");
+ } else if (cmd.useRegex) {
+ // run with regex provided
+ Preconditions.checkArgument(cmd.checks != null, "Expected a regex
pattern to be provided");
+ Preconditions.checkArgument(cmd.checks.size() == 1,
+ "Expected one argument (the regex pattern)");
+ String regex = cmd.checks.get(0).toUpperCase();
+ List<String> matchingChecks = new ArrayList<>();
+ var pattern = Pattern.compile(regex);
+ for (CheckCommand.Check check : CheckCommand.Check.values()) {
+ if (pattern.matcher(check.name()).matches()) {
+ matchingChecks.add(check.name());
+ }
+ }
+ Preconditions.checkArgument(!matchingChecks.isEmpty(),
+ "No checks matched the given pattern: " + regex);
+ cmd.checks = matchingChecks;
+ } else {
+ // run without regex provided
+ if (cmd.checks == null) {
+ cmd.checks =
EnumSet.allOf(CheckCommand.Check.class).stream().map(Enum::name)
+ .collect(Collectors.toList());
+ }
+ }
+ }
+
+ private static void listChecks() {
+ System.out.println();
+ System.out.printf("%-20s | %-80s | %-20s%n", "Check Name", "Description",
"Depends on");
+ System.out.println("-".repeat(120));
+ for (CheckCommand.Check check : CheckCommand.Check.values()) {
+ System.out.printf("%-20s | %-80s | %-20s%n", check.name(),
+ CheckCommand.Check.CHECK_DESCRIPTION.get(check),
check.getDependencies().stream()
+ .map(CheckCommand.Check::name).collect(Collectors.joining(",
")));
+ }
+ System.out.println("-".repeat(120));
+ System.out.println();
+ }
+
+ private static void executeRunCheckCommand(List<CheckCommand.Check> checks) {
+ final List<CheckCommand.Check> allChecksToRun = new ArrayList<>();
+ final TreeMap<CheckCommand.Check,CheckCommand.CheckStatus> checkStatus =
new TreeMap<>();
+
+ // From the given list of checks to run, we need to compute all the checks
that need to be
+ // run (the given checks and all the checks the given checks depend on)
+ computeAllChecksToRun(checks, allChecksToRun);
+ Preconditions.checkState(!allChecksToRun.isEmpty());
+
+ // Sort the checks in the order they are declared in the enum
+ Collections.sort(allChecksToRun);
+ CheckCommand.Check firstCheck = allChecksToRun.remove(0);
+ Preconditions.checkState(firstCheck.ordinal() == 0,
+ "Expected first check to be " + CheckCommand.Check.values()[0] + " but
was " + firstCheck);
+ checkStatus.put(firstCheck, firstCheck.getCheckRunner().runCheck());
+ // Run each of the checks only if the dependencies of that check were
successful
+ // Otherwise, skip the check
+ for (CheckCommand.Check check : allChecksToRun) {
+ boolean runCheck = true;
+ for (CheckCommand.Check dep : check.getDependencies()) {
+ Preconditions.checkState(checkStatus.get(dep) != null,
+ "Expected dependency " + dep + " to have a status set before
attempting to run " + check
+ + ", but this was not the case.");
+ if (checkStatus.get(dep) ==
CheckCommand.CheckStatus.SKIPPED_DEPENDENCY_FAILED
+ || checkStatus.get(dep) == CheckCommand.CheckStatus.FAILED) {
+ runCheck = false;
+ break;
+ }
+ }
+ if (runCheck) {
+ checkStatus.put(check, check.getCheckRunner().runCheck());
+ } else {
+ checkStatus.put(check,
CheckCommand.CheckStatus.SKIPPED_DEPENDENCY_FAILED);
+ }
+ }
+
+ printChecksResults(checkStatus);
+ }
+
+ private static void computeAllChecksToRun(final List<CheckCommand.Check>
givenChecks,
+ final List<CheckCommand.Check> allChecksToRun) {
+ // Avoids unnecessary computation
+ if ((new
HashSet<>(givenChecks)).equals(Set.of(CheckCommand.Check.values()))) {
+ allChecksToRun.clear();
+ allChecksToRun.addAll(givenChecks);
+ return;
+ }
+
+ for (CheckCommand.Check check : givenChecks) {
+ if (!allChecksToRun.contains(check)) {
+ allChecksToRun.add(check);
+ computeAllChecksToRun(check.getDependencies(), allChecksToRun);
+ }
+ }
+ }
+
+ private static void
+ printChecksResults(TreeMap<CheckCommand.Check,CheckCommand.CheckStatus>
checkStatus) {
+ System.out.println();
Review Comment:
Same logger question here
--
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]