This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 5c7863c  [improvement](fe-unit-test) Fix port in use when the cluster 
starts in UT. (#7768)
5c7863c is described below

commit 5c7863c6833bcaac3f804f422e7ffa248877b657
Author: Adonis Ling <[email protected]>
AuthorDate: Sun Jan 16 10:42:56 2022 +0800

    [improvement](fe-unit-test) Fix port in use when the cluster starts in UT. 
(#7768)
---
 .../org/apache/doris/utframe/UtFrameUtils.java     | 28 ++++++++++++----------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/utframe/UtFrameUtils.java 
b/fe/fe-core/src/test/java/org/apache/doris/utframe/UtFrameUtils.java
index 518e210..52c2d73 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/utframe/UtFrameUtils.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/utframe/UtFrameUtils.java
@@ -33,7 +33,6 @@ import org.apache.doris.common.util.SqlParserUtils;
 import org.apache.doris.mysql.privilege.PaloAuth;
 import org.apache.doris.planner.Planner;
 import org.apache.doris.qe.ConnectContext;
-import org.apache.doris.qe.Coordinator;
 import org.apache.doris.qe.QueryState;
 import org.apache.doris.qe.StmtExecutor;
 import org.apache.doris.system.Backend;
@@ -55,7 +54,9 @@ import org.apache.commons.io.FileUtils;
 import java.io.File;
 import java.io.IOException;
 import java.io.StringReader;
+import java.net.DatagramSocket;
 import java.net.ServerSocket;
+import java.net.SocketException;
 import java.nio.channels.SocketChannel;
 import java.nio.file.Files;
 import java.util.List;
@@ -226,21 +227,22 @@ public class UtFrameUtils {
     }
 
     public static int findValidPort() {
-        ServerSocket socket = null;
-        try {
-            socket = new ServerSocket(0);
-            socket.setReuseAddress(true);
-            return socket.getLocalPort();
-        } catch (Exception e) {
-            throw new IllegalStateException("Could not find a free TCP/IP port 
to start HTTP Server on");
-        } finally {
-            if (socket != null) {
-                try {
-                    socket.close();
-                } catch (Exception e) {
+        int port = 0;
+        while (true) {
+            try (ServerSocket socket = new ServerSocket(0)) {
+                socket.setReuseAddress(true);
+                port = socket.getLocalPort();
+                try (DatagramSocket datagramSocket = new DatagramSocket(port)) 
{
+                    datagramSocket.setReuseAddress(true);
+                    break;
+                } catch (SocketException e) {
+                    System.out.println("The port " + port  + " is invalid and 
try another port.");
                 }
+            } catch (IOException e) {
+                throw new IllegalStateException("Could not find a free TCP/IP 
port to start HTTP Server on");
             }
         }
+        return port;
     }
 
     public static String getSQLPlanOrErrorMsg(ConnectContext ctx, String 
queryStr) throws Exception {

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to