Skip to content

Commit

Permalink
MINOR: Small cleanup in ToolsUtils (apache#17388)
Browse files Browse the repository at this point in the history
Reviewers: Chia-Ping Tsai <[email protected]>
  • Loading branch information
mimaison authored Oct 8, 2024
1 parent 8db86c6 commit e124e06
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 30 deletions.
18 changes: 1 addition & 17 deletions tools/src/main/java/org/apache/kafka/tools/ToolsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.apache.kafka.common.Metric;
import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.utils.Utils;
import org.apache.kafka.server.util.CommandLineUtils;

import java.io.PrintStream;
import java.util.Arrays;
Expand All @@ -30,8 +29,6 @@
import java.util.TreeMap;
import java.util.stream.Collectors;

import joptsimple.OptionParser;

public class ToolsUtils {
/**
* print out the metrics in alphabetical order
Expand All @@ -44,7 +41,7 @@ public static void printMetrics(Map<MetricName, ? extends Metric> metrics) {
for (Metric metric : metrics.values()) {
MetricName mName = metric.metricName();
String mergedName = mName.group() + ":" + mName.name() + ":" + mName.tags();
maxLengthOfDisplayName = maxLengthOfDisplayName < mergedName.length() ? mergedName.length() : maxLengthOfDisplayName;
maxLengthOfDisplayName = Math.max(maxLengthOfDisplayName, mergedName.length());
sortedMetrics.put(mergedName, metric.metricValue());
}
String doubleOutputFormat = "%-" + maxLengthOfDisplayName + "s : %.3f";
Expand Down Expand Up @@ -157,17 +154,4 @@ public static <T> Set<T> minus(Set<T> set, T...toRemove) {
res.remove(t);
return res;
}

/**
* This is a simple wrapper around `CommandLineUtils.printUsageAndExit`.
* It is needed for tools migration (KAFKA-14525), as there is no Java equivalent for return type `Nothing`.
* Can be removed once [[kafka.tools.ConsoleConsumer]] are migrated.
*
* @param parser Command line options parser.
* @param message Error message.
*/
public static void printUsageAndExit(OptionParser parser, String message) {
CommandLineUtils.printUsageAndExit(parser, message);
throw new AssertionError("printUsageAndExit should not return, but it did.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.apache.kafka.common.requests.ListOffsetsResponse;
import org.apache.kafka.common.utils.Utils;
import org.apache.kafka.server.util.CommandLineUtils;
import org.apache.kafka.tools.ToolsUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectReader;
Expand Down Expand Up @@ -435,7 +434,7 @@ private void printStates(Map<String, GroupState> states) {
String format = "\n%" + -coordinatorColLen + "s %-25s %-20s %-15s %s";

System.out.printf(format, "GROUP", "COORDINATOR (ID)", "ASSIGNMENT-STRATEGY", "STATE", "#MEMBERS");
System.out.printf(format, state.group, coordinator, state.assignmentStrategy, state.state.toString(), state.numMembers);
System.out.printf(format, state.group, coordinator, state.assignmentStrategy, state.state, state.numMembers);
System.out.println();
}
});
Expand Down Expand Up @@ -1002,8 +1001,7 @@ private Map<TopicPartition, OffsetAndMetadata> prepareOffsetsToReset(String grou
LogOffsetResult logOffsetResult = logStartOffsets.get(topicPartition);

if (!(logOffsetResult instanceof LogOffset)) {
ToolsUtils.printUsageAndExit(opts.parser, "Error getting starting offset of topic partition: " + topicPartition);
return null;
CommandLineUtils.printUsageAndExit(opts.parser, "Error getting starting offset of topic partition: " + topicPartition);
}

return new OffsetAndMetadata(((LogOffset) logOffsetResult).value);
Expand All @@ -1014,8 +1012,7 @@ private Map<TopicPartition, OffsetAndMetadata> prepareOffsetsToReset(String grou
LogOffsetResult logOffsetResult = logEndOffsets.get(topicPartition);

if (!(logOffsetResult instanceof LogOffset)) {
ToolsUtils.printUsageAndExit(opts.parser, "Error getting ending offset of topic partition: " + topicPartition);
return null;
CommandLineUtils.printUsageAndExit(opts.parser, "Error getting ending offset of topic partition: " + topicPartition);
}

return new OffsetAndMetadata(((LogOffset) logOffsetResult).value);
Expand All @@ -1042,8 +1039,7 @@ private Map<TopicPartition, OffsetAndMetadata> prepareOffsetsToReset(String grou
LogOffsetResult logTimestampOffset = logTimestampOffsets.get(topicPartition);

if (!(logTimestampOffset instanceof LogOffset)) {
ToolsUtils.printUsageAndExit(opts.parser, "Error getting offset by timestamp of topic partition: " + topicPartition);
return null;
CommandLineUtils.printUsageAndExit(opts.parser, "Error getting offset by timestamp of topic partition: " + topicPartition);
}

return new OffsetAndMetadata(((LogOffset) logTimestampOffset).value);
Expand All @@ -1062,8 +1058,7 @@ private Map<TopicPartition, OffsetAndMetadata> prepareOffsetsToReset(String grou
LogOffsetResult logTimestampOffset = logTimestampOffsets.get(topicPartition);

if (!(logTimestampOffset instanceof LogOffset)) {
ToolsUtils.printUsageAndExit(opts.parser, "Error getting offset by timestamp of topic partition: " + topicPartition);
return null;
CommandLineUtils.printUsageAndExit(opts.parser, "Error getting offset by timestamp of topic partition: " + topicPartition);
}

return new OffsetAndMetadata(((LogOffset) logTimestampOffset).value);
Expand Down Expand Up @@ -1110,8 +1105,7 @@ private Map<TopicPartition, OffsetAndMetadata> prepareOffsetsToReset(String grou
Map<TopicPartition, OffsetAndMetadata> preparedOffsetsForPartitionsWithoutCommittedOffset = getLogEndOffsets(partitionsToResetWithoutCommittedOffset)
.entrySet().stream().collect(Collectors.toMap(Entry::getKey, e -> {
if (!(e.getValue() instanceof LogOffset)) {
ToolsUtils.printUsageAndExit(opts.parser, "Error getting ending offset of topic partition: " + e.getKey());
return null;
CommandLineUtils.printUsageAndExit(opts.parser, "Error getting ending offset of topic partition: " + e.getKey());
}

return new OffsetAndMetadata(((LogOffset) e.getValue()).value);
Expand All @@ -1122,7 +1116,7 @@ private Map<TopicPartition, OffsetAndMetadata> prepareOffsetsToReset(String grou
return preparedOffsetsForPartitionsWithCommittedOffset;
}

ToolsUtils.printUsageAndExit(opts.parser, String.format("Option '%s' requires one of the following scenarios: %s", opts.resetOffsetsOpt, opts.allResetOffsetScenarioOpts));
CommandLineUtils.printUsageAndExit(opts.parser, String.format("Option '%s' requires one of the following scenarios: %s", opts.resetOffsetsOpt, opts.allResetOffsetScenarioOpts));
return null;
}

Expand Down

0 comments on commit e124e06

Please sign in to comment.