mingyen066 commented on code in PR #13459: URL: https://github.com/apache/kafka/pull/13459#discussion_r2485829451
########## tools/src/main/java/org/apache/kafka/tools/FeatureCommand.java: ########## @@ -0,0 +1,324 @@ +/* + * 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 java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Properties; +import java.util.TreeMap; +import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; +import net.sourceforge.argparse4j.ArgumentParsers; +import net.sourceforge.argparse4j.impl.Arguments; +import net.sourceforge.argparse4j.inf.ArgumentParser; +import net.sourceforge.argparse4j.inf.ArgumentParserException; +import net.sourceforge.argparse4j.inf.Namespace; +import net.sourceforge.argparse4j.inf.Subparser; +import net.sourceforge.argparse4j.inf.Subparsers; +import net.sourceforge.argparse4j.internal.HelpScreenException; +import org.apache.kafka.clients.admin.Admin; +import org.apache.kafka.clients.admin.FeatureMetadata; +import org.apache.kafka.clients.admin.FeatureUpdate; +import org.apache.kafka.clients.admin.SupportedVersionRange; +import org.apache.kafka.clients.admin.UpdateFeaturesOptions; +import org.apache.kafka.clients.admin.UpdateFeaturesResult; +import org.apache.kafka.common.utils.Exit; +import org.apache.kafka.common.utils.Utils; +import org.apache.kafka.server.common.MetadataVersion; + +import static net.sourceforge.argparse4j.impl.Arguments.append; +import static net.sourceforge.argparse4j.impl.Arguments.store; +import static net.sourceforge.argparse4j.impl.Arguments.storeTrue; + +public class FeatureCommand { + public static void main(String... args) { + Exit.exit(mainNoExit(args)); + } + + static int mainNoExit(String... args) { + try { + execute(args); + return 0; + } catch (HelpScreenException e) { + return 0; + } catch (ArgumentParserException e) { + System.err.printf("Command line error: " + e.getMessage() + ". Type --help for help."); + return 1; + } catch (Throwable e) { + System.err.println(e.getMessage()); + return 1; + } + } + + static void execute(String... args) throws Exception { + ArgumentParser parser = ArgumentParsers + .newArgumentParser("kafka-features") + .defaultHelp(true) + .description("This tool manages feature flags in Kafka."); + parser + .addArgument("--bootstrap-server") + .help("A comma-separated list of host:port pairs to use for establishing the connection to the Kafka cluster.") + .required(true); + + parser + .addArgument("--command-config") + .type(Arguments.fileType()) + .help("Property file containing configs to be passed to Admin Client."); + Subparsers subparsers = parser.addSubparsers().dest("command"); + addDescribeParser(subparsers); + addUpgradeParser(subparsers); + addDowngradeParser(subparsers); + addDisableParser(subparsers); + + Namespace namespace = parser.parseArgsOrFail(args); + String command = namespace.getString("command"); + String configPath = namespace.getString("command_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."); + } + + try (Admin adminClient = Admin.create(properties)) { + switch (command) { + case "describe": + handleDescribe(adminClient); + break; + case "upgrade": + handleUpgrade(namespace, adminClient); + break; + case "downgrade": + handleDowngrade(namespace, adminClient); + break; + case "disable": + handleDisable(namespace, adminClient); + break; + default: + throw new TerseException("Unknown command " + command); + } + } + } + + private static void addDescribeParser(Subparsers subparsers) { + subparsers.addParser("describe") + .help("Describes the current active feature flags."); + } + + private static void addUpgradeParser(Subparsers subparsers) { + Subparser upgradeParser = subparsers.addParser("upgrade") + .help("Upgrade one or more feature flags."); + upgradeParser.addArgument("--metadata") + .help("The level to which we should upgrade the metadata. For example, 3.3-IV3.") + .action(store()); + upgradeParser.addArgument("--feature") + .help("A feature upgrade we should perform, in feature=level format. For example: `metadata.version=5`.") + .action(append()); + upgradeParser.addArgument("--dry-run") + .help("Validate this upgrade, but do not perform it.") + .action(storeTrue()); + + } + + private static void addDowngradeParser(Subparsers subparsers) { + Subparser downgradeParser = subparsers.addParser("downgrade") + .help("Upgrade one or more feature flags."); Review Comment: This should be "Downgrade". I’ve opened #20813 to fix it. -- 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]
