aloyszhang commented on code in PR #9226: URL: https://github.com/apache/inlong/pull/9226#discussion_r1405984248
########## inlong-audit/audit-proxy/src/main/java/org/apache/inlong/audit/sink/KafkaSink.java: ########## @@ -282,6 +310,43 @@ private void initTopicProducer(String topic) { logger.info(getName() + " success create producer"); } + /** + * create topic if need + */ + private void createTopic() { + + try (AdminClient adminClient = AdminClient.create(properties)) { + ListTopicsResult topicList = adminClient.listTopics(); + KafkaFuture<Set<String>> kafkaFuture = topicList.names(); + Set<String> topicSet = kafkaFuture.get(); + + if (topicSet.contains(topic)) { + // not need + logger.info("The audit topic:{} already exists.", topic); + return; + } + + DescribeClusterResult describeClusterResult = adminClient.describeCluster(); + Collection<Node> nodes = describeClusterResult.nodes().get(); + if (nodes.isEmpty()) { + throw new IllegalArgumentException("kafka server not find"); + } + + int partition = Math.min(topicPartitions, nodes.size()); + int factor = nodes.size() > 1 ? Math.min(topicReplications, nodes.size()) : 1; Review Comment: `int factor = Math.min(topicReplications, nodes.size());` is enough? ########## inlong-audit/audit-proxy/src/main/java/org/apache/inlong/audit/sink/KafkaSink.java: ########## @@ -282,6 +310,43 @@ private void initTopicProducer(String topic) { logger.info(getName() + " success create producer"); } + /** + * create topic if need + */ + private void createTopic() { + + try (AdminClient adminClient = AdminClient.create(properties)) { + ListTopicsResult topicList = adminClient.listTopics(); + KafkaFuture<Set<String>> kafkaFuture = topicList.names(); + Set<String> topicSet = kafkaFuture.get(); + + if (topicSet.contains(topic)) { + // not need + logger.info("The audit topic:{} already exists.", topic); + return; + } + + DescribeClusterResult describeClusterResult = adminClient.describeCluster(); + Collection<Node> nodes = describeClusterResult.nodes().get(); + if (nodes.isEmpty()) { + throw new IllegalArgumentException("kafka server not find"); + } + + int partition = Math.min(topicPartitions, nodes.size()); + int factor = nodes.size() > 1 ? Math.min(topicReplications, nodes.size()) : 1; Review Comment: `int factor = Math.min(topicReplications, nodes.size());` is enough? -- 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: commits-unsubscr...@inlong.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org