anton-liauchuk commented on code in PR #20384:
URL: https://github.com/apache/kafka/pull/20384#discussion_r3655560348


##########
tools/src/main/java/org/apache/kafka/tools/ConnectInternalTopics.java:
##########
@@ -0,0 +1,243 @@
+/*
+ * 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 org.apache.kafka.common.config.AbstractConfig;
+import org.apache.kafka.common.config.ConfigDef;
+import org.apache.kafka.common.config.ConfigException;
+import org.apache.kafka.common.config.TopicConfig;
+import org.apache.kafka.common.utils.Exit;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.connect.runtime.distributed.DistributedConfig;
+import org.apache.kafka.connect.util.SharedTopicAdmin;
+import org.apache.kafka.connect.util.TopicAdmin;
+
+import net.sourceforge.argparse4j.ArgumentParsers;
+import net.sourceforge.argparse4j.inf.ArgumentParser;
+import net.sourceforge.argparse4j.inf.ArgumentParserException;
+import net.sourceforge.argparse4j.inf.Namespace;
+
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import static net.sourceforge.argparse4j.impl.Arguments.store;
+
+public class ConnectInternalTopics {
+
+    private static final String CREATE_COMMAND = "create";
+
+    public static void main(String[] args) {
+        Exit.exit(mainNoExit(args, System.out, System.err));
+    }
+
+    static int mainNoExit(String[] args, PrintStream out, PrintStream err) {
+        var parser = parser();
+        try {
+            var namespace = parser.parseArgs(args);
+            var workerProperties = parseWorkerProperties(parser, namespace);
+            out.println("Parsed arguments and loaded worker properties");
+            execute(parser, namespace, workerProperties, out, err);
+            out.println("Command executed successfully");
+            return 0;
+        } catch (ArgumentParserException e) {
+            parser.handleError(e);
+            return 1;
+        } catch (TerseException | ConfigException e) {
+            err.println(e.getMessage());
+            return 2;
+        } catch (Throwable e) {
+            err.println("Unexpected error: " + e.getMessage());
+            err.println(Utils.stackTrace(e));
+            return 3;
+        }
+    }
+
+    private static void execute(ArgumentParser parser, Namespace namespace, 
Map<String, String> workerProperties, PrintStream out, PrintStream err) throws 
ArgumentParserException {
+        var subcommand = namespace.getString("subcommand");
+        out.println("Subcommand: " + subcommand);
+        if (subcommand == null) {
+            throw new ArgumentParserException("No subcommand specified", 
parser);
+        }
+        if (CREATE_COMMAND.equals(subcommand)) {
+            var internalTopicsConfig = new 
InternalTopicsConfig(workerProperties);
+            internalTopicsConfig.validateTopicNames();
+            out.println("Running create command for internal topics");
+            runCommand(internalTopicsConfig, out);
+        } else {
+            throw new ArgumentParserException("Unrecognized subcommand: '" + 
subcommand + "'", parser);
+        }
+    }
+
+    private static void runCommand(InternalTopicsConfig config, PrintStream 
out) {
+        var adminProps = new HashMap<>(config.originals());
+        out.println("Admin properties loaded for topic admin");
+        try (var sharedAdmin = new SharedTopicAdmin(adminProps)) {
+            createInternalTopic(sharedAdmin, buildOffsetTopicSettings(config, 
out), out);
+            createInternalTopic(sharedAdmin, buildConfigTopicSettings(config, 
out), out);
+            createInternalTopic(sharedAdmin, buildStatusTopicSettings(config, 
out), out);
+        }
+    }
+
+    private static void createInternalTopic(SharedTopicAdmin sharedAdmin, 
TopicSettings settings, PrintStream out) {
+        out.println("Creating internal topic: " + settings.topicName);
+        var topicDescription = TopicAdmin.defineTopic(settings.topicName)
+                .config(settings.topicSettings)
+                .compacted()
+                .partitions(settings.partitions)
+                .replicationFactor(settings.replicationFactor)
+                .build();
+        sharedAdmin.topicAdmin().createTopics(topicDescription);
+        out.println("Created internal topic: " + settings.topicName);

Review Comment:
   fixed, thanks!



-- 
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]

Reply via email to