This is an automated email from the ASF dual-hosted git repository.
danny0405 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hudi.git
The following commit(s) were added to refs/heads/master by this push:
new b0ea242c8a65 fix(kafka-connect): close AdminClient after fetching
topic partitions (#20303)
b0ea242c8a65 is described below
commit b0ea242c8a65cb841c874cd79aa2a6eed3404789
Author: fhan <[email protected]>
AuthorDate: Thu Oct 15 11:05:12 2026 +0800
fix(kafka-connect): close AdminClient after fetching topic partitions
(#20303)
Co-authored-by: fhan <[email protected]>
---
.../hudi/connect/utils/KafkaConnectUtils.java | 3 +-
.../hudi/connect/utils/TestKafkaConnectUtils.java | 81 ++++++++++++++++++++++
2 files changed, 82 insertions(+), 2 deletions(-)
diff --git
a/hudi-kafka-connect/src/main/java/org/apache/hudi/connect/utils/KafkaConnectUtils.java
b/hudi-kafka-connect/src/main/java/org/apache/hudi/connect/utils/KafkaConnectUtils.java
index d77873065b9b..6b7ab192ce0d 100644
---
a/hudi-kafka-connect/src/main/java/org/apache/hudi/connect/utils/KafkaConnectUtils.java
+++
b/hudi-kafka-connect/src/main/java/org/apache/hudi/connect/utils/KafkaConnectUtils.java
@@ -119,8 +119,7 @@ public class KafkaConnectUtils {
public static int getLatestNumPartitions(String bootstrapServers, String
topicName) {
Properties props = new Properties();
props.put("bootstrap.servers", bootstrapServers);
- try {
- AdminClient client = AdminClient.create(props);
+ try (AdminClient client = AdminClient.create(props)) {
DescribeTopicsResult result =
client.describeTopics(Collections.singletonList(topicName));
Map<String, KafkaFuture<TopicDescription>> values = result.values();
KafkaFuture<TopicDescription> topicDescription = values.get(topicName);
diff --git
a/hudi-kafka-connect/src/test/java/org/apache/hudi/connect/utils/TestKafkaConnectUtils.java
b/hudi-kafka-connect/src/test/java/org/apache/hudi/connect/utils/TestKafkaConnectUtils.java
new file mode 100644
index 000000000000..d5d5ab324e2d
--- /dev/null
+++
b/hudi-kafka-connect/src/test/java/org/apache/hudi/connect/utils/TestKafkaConnectUtils.java
@@ -0,0 +1,81 @@
+/*
+ * 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.hudi.connect.utils;
+
+import org.apache.hudi.exception.HoodieException;
+
+import org.apache.kafka.clients.admin.AdminClient;
+import org.apache.kafka.clients.admin.DescribeTopicsResult;
+import org.apache.kafka.clients.admin.TopicDescription;
+import org.apache.kafka.common.KafkaFuture;
+import org.apache.kafka.common.TopicPartitionInfo;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+
+import java.util.Collections;
+import java.util.Properties;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class TestKafkaConnectUtils {
+
+ private static final String BOOTSTRAP_SERVERS = "localhost:9092";
+ private static final String TOPIC_NAME = "test-topic";
+
+ @Test
+ void closesAdminClientAfterFetchingLatestNumberOfPartitions() {
+ AdminClient adminClient = mock(AdminClient.class);
+ DescribeTopicsResult describeTopicsResult =
mock(DescribeTopicsResult.class);
+ TopicDescription topicDescription = mock(TopicDescription.class);
+ TopicPartitionInfo partitionInfo = mock(TopicPartitionInfo.class);
+
+ try (MockedStatic<AdminClient> mockedAdminClient =
mockStatic(AdminClient.class)) {
+ mockedAdminClient.when(() ->
AdminClient.create(any(Properties.class))).thenReturn(adminClient);
+
when(adminClient.describeTopics(Collections.singletonList(TOPIC_NAME))).thenReturn(describeTopicsResult);
+ when(describeTopicsResult.values())
+ .thenReturn(Collections.singletonMap(TOPIC_NAME,
KafkaFuture.completedFuture(topicDescription)));
+ when(topicDescription.partitions()).thenReturn(Collections.nCopies(3,
partitionInfo));
+
+ assertEquals(3,
KafkaConnectUtils.getLatestNumPartitions(BOOTSTRAP_SERVERS, TOPIC_NAME));
+ verify(adminClient).close();
+ }
+ }
+
+ @Test
+ void closesAdminClientWhenFetchingLatestNumberOfPartitionsFails() {
+ AdminClient adminClient = mock(AdminClient.class);
+
+ try (MockedStatic<AdminClient> mockedAdminClient =
mockStatic(AdminClient.class)) {
+ mockedAdminClient.when(() ->
AdminClient.create(any(Properties.class))).thenReturn(adminClient);
+ when(adminClient.describeTopics(Collections.singletonList(TOPIC_NAME)))
+ .thenThrow(new IllegalStateException("Failed to describe topic"));
+
+ assertThrows(
+ HoodieException.class,
+ () -> KafkaConnectUtils.getLatestNumPartitions(BOOTSTRAP_SERVERS,
TOPIC_NAME));
+ verify(adminClient).close();
+ }
+ }
+}