From fe7c9e71eb0aa365306c2d91da82234a96a5c1c6 Mon Sep 17 00:00:00 2001 From: Prashant Wason Date: Mon, 7 Sep 2020 19:30:45 -0700 Subject: [PATCH] [MINOR] Fix BindException when running tests of shared machines. (#2070) When unit tests are run on shared machines (e.g. jenkins cluster), the unit tests sometimes fail due to BindException in starting HDFS Cluster. This is because the port chosen may have been bound by another process using the same machine. The fix is to retry the port selection a few times. --- .../minicluster/HdfsTestService.java | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/hudi-common/src/test/java/org/apache/hudi/common/testutils/minicluster/HdfsTestService.java b/hudi-common/src/test/java/org/apache/hudi/common/testutils/minicluster/HdfsTestService.java index 44819d3ea..44af4ecea 100644 --- a/hudi-common/src/test/java/org/apache/hudi/common/testutils/minicluster/HdfsTestService.java +++ b/hudi-common/src/test/java/org/apache/hudi/common/testutils/minicluster/HdfsTestService.java @@ -31,6 +31,7 @@ import org.apache.log4j.Logger; import java.io.File; import java.io.IOException; +import java.net.BindException; import java.nio.file.Files; import java.util.Objects; @@ -72,20 +73,32 @@ public class HdfsTestService { FileIOUtils.deleteDirectory(file); } - int namenodeRpcPort = NetworkTestUtils.nextFreePort(); - int datanodePort = NetworkTestUtils.nextFreePort(); - int datanodeIpcPort = NetworkTestUtils.nextFreePort(); - int datanodeHttpPort = NetworkTestUtils.nextFreePort(); + int loop = 0; + while (true) { + try { + 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); - String bindIP = "127.0.0.1"; - configureDFSCluster(hadoopConf, localDFSLocation, bindIP, namenodeRpcPort, - datanodePort, datanodeIpcPort, datanodeHttpPort); - miniDfsCluster = new MiniDFSCluster.Builder(hadoopConf).numDataNodes(1).format(format).checkDataNodeAddrConfig(true) - .checkDataNodeHostConfig(true).build(); - LOG.info("HDFS Minicluster service started."); - return miniDfsCluster; + // Configure and start the HDFS cluster + // boolean format = shouldFormatDFSCluster(localDFSLocation, clean); + String bindIP = "127.0.0.1"; + configureDFSCluster(hadoopConf, localDFSLocation, bindIP, namenodeRpcPort, + datanodePort, datanodeIpcPort, datanodeHttpPort); + miniDfsCluster = new MiniDFSCluster.Builder(hadoopConf).numDataNodes(1).format(format).checkDataNodeAddrConfig(true) + .checkDataNodeHostConfig(true).build(); + LOG.info("HDFS Minicluster service started."); + return miniDfsCluster; + } catch (BindException ex) { + ++loop; + if (loop < 5) { + stop(); + } else { + throw ex; + } + } + } } public void stop() {