Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions tools/src/main/java/org/apache/kafka/tools/FeatureCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static void execute(String... args) throws Exception {
.newArgumentParser("kafka-features")
.defaultHelp(true)
.description("This tool manages feature flags in Kafka.");
MutuallyExclusiveGroup bootstrapGroup = parser.addMutuallyExclusiveGroup().required(true);
MutuallyExclusiveGroup bootstrapGroup = parser.addMutuallyExclusiveGroup();
bootstrapGroup.addArgument("--bootstrap-server")
.help("A comma-separated list of host:port pairs to use for establishing the connection to the Kafka cluster.");
bootstrapGroup.addArgument("--bootstrap-controller")
Expand All @@ -92,14 +92,27 @@ static void execute(String... args) throws Exception {
addVersionMappingParser(subparsers);
addFeatureDependenciesParser(subparsers);

Namespace namespace = parser.parseArgsOrFail(args);
Namespace namespace = parser.parseArgs(args);
String command = namespace.getString("command");
if (command.equals("version-mapping")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach allows version-mapping to be used with both bootstrap-server and bootstrap-controller, which seems a bit odd to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version-mapping and feature-dependencies subcommands don't use bootstrap-server and bootstrap-controller arguments at all. The best practice is to let these arguments be required arguments of other subcommands. But I am not sure whether this change will require an KIP or worth one. Do you have any suggestion?

Or, we can check whether user uses these arguments with version-mapping and feature-dependencies, which makes the code more ugly but can provide user more information.

handleVersionMapping(namespace, Feature.PRODUCTION_FEATURES);
return;
} else if (command.equals("feature-dependencies")) {
handleFeatureDependencies(namespace, Feature.PRODUCTION_FEATURES);
return;
}
String configPath = namespace.getString("command_config");
Properties properties = (configPath == null) ? new Properties() : Utils.loadProps(configPath);

CommandLineUtils.initializeBootstrapProperties(properties,
Optional.ofNullable(namespace.getString("bootstrap_server")),
Optional.ofNullable(namespace.getString("bootstrap_controller")));
try {
CommandLineUtils.initializeBootstrapProperties(properties,
Optional.ofNullable(namespace.getString("bootstrap_server")),
Optional.ofNullable(namespace.getString("bootstrap_controller")));
} catch (Exception e) {
// bootstrap_server and bootstrap_controller are in a mutually exclusive group,
// so the exception happens only when both of them are missing
throw new ArgumentParserException("one of the arguments --bootstrap-server --bootstrap-controller is required", parser);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about new ArgumentParserException(e.getMessage(), parser)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! Yes, it's better to use the existing message rather than write a new one. The code has been updated.

}

try (Admin adminClient = Admin.create(properties)) {
switch (command) {
Expand All @@ -115,12 +128,6 @@ static void execute(String... args) throws Exception {
case "disable":
handleDisable(namespace, adminClient);
break;
case "version-mapping":
handleVersionMapping(namespace, Feature.PRODUCTION_FEATURES);
break;
case "feature-dependencies":
handleFeatureDependencies(namespace, Feature.PRODUCTION_FEATURES);
break;
default:
throw new TerseException("Unknown command " + command);
}
Expand Down
15 changes: 15 additions & 0 deletions tools/src/test/java/org/apache/kafka/tools/FeatureCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,21 @@ public void testHandleVersionMappingWithInvalidReleaseVersion() {
MetadataVersion.MINIMUM_VERSION, MetadataVersion.latestTesting()), exception2.getMessage());
}

@Test
public void testHandleVersionMappingWithoutBootstrap() {
Map.Entry<String, String> output = ToolsTestUtils.grabConsoleOutputAndError(() -> FeatureCommand.mainNoExit("version-mapping"));
assertEquals("", output.getValue());
MetadataVersion metadataVersion = MetadataVersion.latestProduction();
assertTrue(output.getKey().contains("metadata.version=" + metadataVersion.featureLevel() + " (" + metadataVersion.version() + ")"),
"Output did not contain expected Metadata Version: " + output.getKey());
}

@Test
public void testHandleRemoteCommandWithoutBootstrap() {
String errorMessage = ToolsTestUtils.grabConsoleError(() -> FeatureCommand.mainNoExit("upgrade"));
assertTrue(errorMessage.contains("one of the arguments --bootstrap-server --bootstrap-controller is required"));
}

@Test
public void testHandleFeatureDependenciesForFeatureWithDependencies() {
Map<String, Object> namespace = new HashMap<>();
Expand Down
Loading