-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-14575: Move ClusterTool to tools module #13080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
74 changes: 0 additions & 74 deletions
74
core/src/test/scala/unit/kafka/tools/ClusterToolTest.scala
This file was deleted.
Oops, something went wrong.
134 changes: 134 additions & 0 deletions
134
tools/src/main/java/org/apache/kafka/tools/ClusterTool.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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. | ||
| */ | ||
| package org.apache.kafka.tools; | ||
|
|
||
| import net.sourceforge.argparse4j.ArgumentParsers; | ||
| import net.sourceforge.argparse4j.inf.ArgumentParser; | ||
| import net.sourceforge.argparse4j.inf.Namespace; | ||
| import net.sourceforge.argparse4j.inf.Subparser; | ||
| import net.sourceforge.argparse4j.inf.Subparsers; | ||
| import org.apache.kafka.clients.admin.Admin; | ||
| import org.apache.kafka.common.errors.UnsupportedVersionException; | ||
| import org.apache.kafka.common.utils.Exit; | ||
| import org.apache.kafka.common.utils.Utils; | ||
|
|
||
| import java.io.PrintStream; | ||
| import java.util.Arrays; | ||
| import java.util.Properties; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| import static net.sourceforge.argparse4j.impl.Arguments.store; | ||
|
|
||
| public class ClusterTool { | ||
|
|
||
| public static void main(String... args) { | ||
| Exit.exit(mainNoExit(args)); | ||
| } | ||
|
|
||
| static int mainNoExit(String... args) { | ||
| try { | ||
| execute(args); | ||
| return 0; | ||
| } catch (TerseException e) { | ||
| System.err.println(e.getMessage()); | ||
| return 1; | ||
| } catch (Throwable e) { | ||
| System.err.println(e.getMessage()); | ||
| System.err.println(Utils.stackTrace(e)); | ||
| return 1; | ||
| } | ||
| } | ||
|
|
||
| static void execute(String... args) throws Exception { | ||
| ArgumentParser parser = ArgumentParsers | ||
| .newArgumentParser("kafka-cluster") | ||
| .defaultHelp(true) | ||
| .description("The Kafka cluster tool."); | ||
| Subparsers subparsers = parser.addSubparsers().dest("command"); | ||
|
|
||
| Subparser clusterIdParser = subparsers.addParser("cluster-id") | ||
| .help("Get information about the ID of a cluster."); | ||
| Subparser unregisterParser = subparsers.addParser("unregister") | ||
| .help("Unregister a broker."); | ||
| for (Subparser subpparser : Arrays.asList(clusterIdParser, unregisterParser)) { | ||
| subpparser.addArgument("--bootstrap-server", "-b") | ||
| .action(store()) | ||
| .help("A list of host/port pairs to use for establishing the connection to the Kafka cluster."); | ||
| subpparser.addArgument("--config", "-c") | ||
| .action(store()) | ||
| .help("A property file containing configurations for the Admin client."); | ||
| } | ||
| unregisterParser.addArgument("--id", "-i") | ||
| .type(Integer.class) | ||
| .action(store()) | ||
| .required(true) | ||
| .help("The ID of the broker to unregister."); | ||
|
|
||
| Namespace namespace = parser.parseArgsOrFail(args); | ||
| String command = namespace.getString("command"); | ||
| String configPath = namespace.getString("config"); | ||
| Properties properties = (configPath == null) ? new Properties() : Utils.loadProps(configPath); | ||
|
|
||
| String bootstrapServer = namespace.getString("bootstrap_server"); | ||
| if (bootstrapServer != null) { | ||
| properties.setProperty("bootstrap.servers", bootstrapServer); | ||
| } | ||
| if (properties.getProperty("bootstrap.servers") == null) { | ||
| throw new TerseException("Please specify --bootstrap-server."); | ||
| } | ||
|
|
||
| switch (command) { | ||
| case "cluster-id": { | ||
| try (Admin adminClient = Admin.create(properties)) { | ||
| clusterIdCommand(System.out, adminClient); | ||
| } | ||
| break; | ||
| } | ||
| case "unregister": { | ||
| try (Admin adminClient = Admin.create(properties)) { | ||
| unregisterCommand(System.out, adminClient, namespace.getInt("id")); | ||
| } | ||
| break; | ||
| } | ||
| default: | ||
| throw new RuntimeException("Unknown command " + command); | ||
| } | ||
| } | ||
|
|
||
| static void clusterIdCommand(PrintStream stream, Admin adminClient) throws Exception { | ||
| String clusterId = adminClient.describeCluster().clusterId().get(); | ||
| if (clusterId != null) { | ||
| stream.println("Cluster ID: " + clusterId); | ||
| } else { | ||
| stream.println("No cluster ID found. The Kafka version is probably too old."); | ||
| } | ||
| } | ||
|
|
||
| static void unregisterCommand(PrintStream stream, Admin adminClient, int id) throws Exception { | ||
| try { | ||
| adminClient.unregisterBroker(id).all().get(); | ||
| stream.println("Broker " + id + " is no longer registered."); | ||
| } catch (ExecutionException ee) { | ||
| Throwable cause = ee.getCause(); | ||
| if (cause instanceof UnsupportedVersionException) { | ||
| stream.println("The target cluster does not support the broker unregistration API."); | ||
| } else { | ||
| throw ee; | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not simply embed this logic inside the main method to have one less stack frame?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea was to make it like
MetadataQuorumCommand. In the future we will be able to put this method in a Command interface or utils class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of the command interface. In that case, we should add the suffix
*Commandto all implementing classes (e.g.ClusterToolCommand).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I was thinking of doing that after the Scala to Java conversion.