diff --git a/hudi-client/src/test/java/org/apache/hudi/metrics/TestHoodieJmxMetrics.java b/hudi-client/src/test/java/org/apache/hudi/metrics/TestHoodieJmxMetrics.java index 063f64ecc..7b63a300f 100644 --- a/hudi-client/src/test/java/org/apache/hudi/metrics/TestHoodieJmxMetrics.java +++ b/hudi-client/src/test/java/org/apache/hudi/metrics/TestHoodieJmxMetrics.java @@ -18,6 +18,7 @@ package org.apache.hudi.metrics; +import org.apache.hudi.common.testutils.NetworkTestUtils; import org.apache.hudi.config.HoodieWriteConfig; import org.junit.jupiter.api.Test; @@ -39,7 +40,7 @@ public class TestHoodieJmxMetrics { when(config.isMetricsOn()).thenReturn(true); when(config.getMetricsReporterType()).thenReturn(MetricsReporterType.JMX); when(config.getJmxHost()).thenReturn("localhost"); - when(config.getJmxPort()).thenReturn("9889"); + when(config.getJmxPort()).thenReturn(String.valueOf(NetworkTestUtils.nextFreePort())); new HoodieMetrics(config, "raw_table"); registerGauge("jmx_metric1", 123L); assertEquals("123", Metrics.getInstance().getRegistry().getGauges() @@ -51,7 +52,7 @@ public class TestHoodieJmxMetrics { when(config.isMetricsOn()).thenReturn(true); when(config.getMetricsReporterType()).thenReturn(MetricsReporterType.JMX); when(config.getJmxHost()).thenReturn("localhost"); - when(config.getJmxPort()).thenReturn("1000-5000"); + when(config.getJmxPort()).thenReturn(String.valueOf(NetworkTestUtils.nextFreePort())); new HoodieMetrics(config, "raw_table"); registerGauge("jmx_metric2", 123L); assertEquals("123", Metrics.getInstance().getRegistry().getGauges() diff --git a/hudi-common/src/test/java/org/apache/hudi/common/minicluster/HdfsTestService.java b/hudi-common/src/test/java/org/apache/hudi/common/minicluster/HdfsTestService.java index d331a17aa..00e6e3c7b 100644 --- a/hudi-common/src/test/java/org/apache/hudi/common/minicluster/HdfsTestService.java +++ b/hudi-common/src/test/java/org/apache/hudi/common/minicluster/HdfsTestService.java @@ -19,8 +19,8 @@ package org.apache.hudi.common.minicluster; import org.apache.hudi.common.model.HoodieTestUtils; +import org.apache.hudi.common.testutils.NetworkTestUtils; import org.apache.hudi.common.util.FileIOUtils; -import org.apache.hudi.exception.HoodieIOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; @@ -31,7 +31,6 @@ import org.apache.log4j.Logger; import java.io.File; import java.io.IOException; -import java.net.ServerSocket; import java.nio.file.Files; import java.util.Objects; @@ -61,14 +60,6 @@ public class HdfsTestService { return hadoopConf; } - private static int nextFreePort() { - try (ServerSocket socket = new ServerSocket(0)) { - return socket.getLocalPort(); - } catch (IOException e) { - throw new HoodieIOException("Unable to find next free port", e); - } - } - public MiniDFSCluster start(boolean format) throws IOException { Objects.requireNonNull(workDir, "The work dir must be set before starting cluster."); hadoopConf = HoodieTestUtils.getDefaultHadoopConf(); @@ -81,10 +72,10 @@ public class HdfsTestService { FileIOUtils.deleteDirectory(file); } - int namenodeRpcPort = nextFreePort(); - int datanodePort = nextFreePort(); - int datanodeIpcPort = nextFreePort(); - int datanodeHttpPort = nextFreePort(); + int namenodeRpcPort = NetworkTestUtils.nextFreePort(); + int datanodePort = NetworkTestUtils.nextFreePort(); + int datanodeIpcPort = NetworkTestUtils.nextFreePort(); + int datanodeHttpPort = NetworkTestUtils.nextFreePort(); // Configure and start the HDFS cluster // boolean format = shouldFormatDFSCluster(localDFSLocation, clean); diff --git a/hudi-common/src/test/java/org/apache/hudi/common/testutils/NetworkTestUtils.java b/hudi-common/src/test/java/org/apache/hudi/common/testutils/NetworkTestUtils.java new file mode 100644 index 000000000..1f99b0eaf --- /dev/null +++ b/hudi-common/src/test/java/org/apache/hudi/common/testutils/NetworkTestUtils.java @@ -0,0 +1,35 @@ +/* + * 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.common.testutils; + +import org.apache.hudi.exception.HoodieIOException; + +import java.io.IOException; +import java.net.ServerSocket; + +public class NetworkTestUtils { + + public static int nextFreePort() { + try (ServerSocket socket = new ServerSocket(0)) { + return socket.getLocalPort(); + } catch (IOException e) { + throw new HoodieIOException("Unable to find next free port", e); + } + } +}