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

yangjie01 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/uniffle.git


The following commit(s) were added to refs/heads/master by this push:
     new b7969b47a introduce a new way to support jetty use random port (#2402)
b7969b47a is described below

commit b7969b47a2cd723414c1a37bd9467ef78045ed49
Author: summaryzb <[email protected]>
AuthorDate: Fri Mar 14 18:01:31 2025 +0800

    introduce a new way to support jetty use random port (#2402)
    
    ### What changes were proposed in this pull request?
    Make it possible to get the actual port when config jetty port to zero
    
    ### Why are the changes needed?
    Support jetty use random port
    Fix: #2401
    
    ### Does this PR introduce _any_ user-facing change?
    No.
    
    ### How was this patch tested?
    UT
---
 .../org/apache/uniffle/common/web/JettyServer.java |  4 ++-
 .../apache/uniffle/common/web/JettyServerTest.java | 36 ++++++++--------------
 .../uniffle/coordinator/CoordinatorServer.java     |  7 ++++-
 .../uniffle/coordinator/CoordinatorServerTest.java |  8 +++--
 .../org/apache/uniffle/server/ShuffleServer.java   |  5 +--
 .../apache/uniffle/server/ShuffleServerTest.java   |  2 +-
 6 files changed, 30 insertions(+), 32 deletions(-)

diff --git 
a/common/src/main/java/org/apache/uniffle/common/web/JettyServer.java 
b/common/src/main/java/org/apache/uniffle/common/web/JettyServer.java
index 763c2ca5b..557ecf95a 100644
--- a/common/src/main/java/org/apache/uniffle/common/web/JettyServer.java
+++ b/common/src/main/java/org/apache/uniffle/common/web/JettyServer.java
@@ -169,13 +169,15 @@ public class JettyServer {
     return this.server;
   }
 
-  public void start() throws Exception {
+  public int start() throws Exception {
     try {
       server.start();
+      httpPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
     } catch (BindException e) {
       ExitUtils.terminate(1, "Fail to start jetty http server, port is " + 
httpPort, e, LOG);
     }
     LOG.info("Jetty http server started, listening on port {}", httpPort);
+    return httpPort;
   }
 
   public void stop() throws Exception {
diff --git 
a/common/src/test/java/org/apache/uniffle/common/web/JettyServerTest.java 
b/common/src/test/java/org/apache/uniffle/common/web/JettyServerTest.java
index 353c74d3b..9d4def387 100644
--- a/common/src/test/java/org/apache/uniffle/common/web/JettyServerTest.java
+++ b/common/src/test/java/org/apache/uniffle/common/web/JettyServerTest.java
@@ -17,43 +17,26 @@
 
 package org.apache.uniffle.common.web;
 
-import java.io.FileNotFoundException;
-
 import org.eclipse.jetty.server.Handler;
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.ServerConnector;
 import org.eclipse.jetty.servlet.ServletContextHandler;
 import org.eclipse.jetty.util.thread.ExecutorThreadPool;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 import org.apache.uniffle.common.config.RssBaseConf;
-import org.apache.uniffle.common.port.PortRegistry;
 import org.apache.uniffle.common.util.ExitUtils;
 import org.apache.uniffle.common.util.ExitUtils.ExitException;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 public class JettyServerTest {
-
-  int port;
-
-  @BeforeEach
-  public void beforeEach() {
-    port = PortRegistry.reservePort();
-  }
-
-  @AfterEach
-  public void afterEach() {
-    PortRegistry.release(port);
-  }
-
   @Test
-  public void jettyServerTest() throws FileNotFoundException {
+  public void jettyServerTest() throws Exception {
     RssBaseConf conf = new RssBaseConf();
-    conf.setInteger("rss.jetty.http.port", port);
+    conf.setInteger("rss.jetty.http.port", 0);
     JettyServer jettyServer = new JettyServer(conf);
     Server server = jettyServer.getServer();
 
@@ -65,21 +48,26 @@ public class JettyServerTest {
     assertEquals(server, server.getHandler().getServer());
     assertTrue(server.getConnectors()[0] instanceof ServerConnector);
     ServerConnector connector = (ServerConnector) server.getConnectors()[0];
-    assertEquals(port, connector.getPort());
+    assertEquals(0, connector.getPort());
+    jettyServer.start();
+    assertEquals(jettyServer.getHttpPort(), connector.getLocalPort());
+    assertNotEquals(jettyServer.getHttpPort(), 0);
 
     assertEquals(1, server.getHandlers().length);
     Handler handler = server.getHandler();
     assertTrue(handler instanceof ServletContextHandler);
+    jettyServer.stop();
   }
 
   @Test
   public void jettyServerStartTest() throws Exception {
     RssBaseConf conf = new RssBaseConf();
-    conf.setInteger("rss.jetty.http.port", port);
+    conf.setInteger("rss.jetty.http.port", 0);
     JettyServer jettyServer1 = new JettyServer(conf);
-    JettyServer jettyServer2 = new JettyServer(conf);
-    jettyServer1.start();
 
+    int portExist = jettyServer1.start();
+    conf.setInteger("rss.jetty.http.port", portExist);
+    JettyServer jettyServer2 = new JettyServer(conf);
     ExitUtils.disableSystemExit();
     final String expectMessage = "Fail to start jetty http server";
     final int expectStatus = 1;
diff --git 
a/coordinator/src/main/java/org/apache/uniffle/coordinator/CoordinatorServer.java
 
b/coordinator/src/main/java/org/apache/uniffle/coordinator/CoordinatorServer.java
index 74d34bac6..d8df820b7 100644
--- 
a/coordinator/src/main/java/org/apache/uniffle/coordinator/CoordinatorServer.java
+++ 
b/coordinator/src/main/java/org/apache/uniffle/coordinator/CoordinatorServer.java
@@ -63,6 +63,7 @@ public class CoordinatorServer {
   private final CoordinatorConf coordinatorConf;
   private final long startTimeMs;
   private JettyServer jettyServer;
+  private int jettyPort;
   private ServerInterface server;
   private ClusterManager clusterManager;
   private AssignmentStrategy assignmentStrategy;
@@ -106,7 +107,7 @@ public class CoordinatorServer {
   public void start() throws Exception {
     LOG.info(
         "{} version: {}", this.getClass().getSimpleName(), 
Constants.VERSION_AND_REVISION_SHORT);
-    jettyServer.start();
+    jettyPort = jettyServer.start();
     rpcListenPort = server.start();
     if (metricReporter != null) {
       metricReporter.start();
@@ -285,4 +286,8 @@ public class CoordinatorServer {
   public int getRpcListenPort() {
     return rpcListenPort;
   }
+
+  public int getJettyPort() {
+    return jettyPort;
+  }
 }
diff --git 
a/coordinator/src/test/java/org/apache/uniffle/coordinator/CoordinatorServerTest.java
 
b/coordinator/src/test/java/org/apache/uniffle/coordinator/CoordinatorServerTest.java
index 357bf6986..69811de02 100644
--- 
a/coordinator/src/test/java/org/apache/uniffle/coordinator/CoordinatorServerTest.java
+++ 
b/coordinator/src/test/java/org/apache/uniffle/coordinator/CoordinatorServerTest.java
@@ -31,11 +31,11 @@ public class CoordinatorServerTest {
   public void test() throws Exception {
     CoordinatorConf coordinatorConf = new CoordinatorConf();
     coordinatorConf.setInteger("rss.rpc.server.port", 9537);
-    coordinatorConf.setInteger("rss.jetty.http.port", 9528);
+    coordinatorConf.setInteger("rss.jetty.http.port", 0);
     coordinatorConf.setInteger("rss.rpc.executor.size", 10);
 
     CoordinatorServer cs1 = new CoordinatorServer(coordinatorConf);
-    CoordinatorServer cs2 = new CoordinatorServer(coordinatorConf);
+    CoordinatorServer cs2 = null;
     CoordinatorServer cs3 = null;
     try {
       cs1.start();
@@ -44,6 +44,8 @@ public class CoordinatorServerTest {
       String expectMessage = "Fail to start jetty http server";
       final int expectStatus = 1;
       try {
+        coordinatorConf.setInteger("rss.jetty.http.port", cs1.getJettyPort());
+        cs2 = new CoordinatorServer(coordinatorConf);
         cs2.start();
       } catch (Exception e) {
         assertTrue(e.getMessage().startsWith(expectMessage));
@@ -53,7 +55,7 @@ public class CoordinatorServerTest {
         cs2.stopServer();
       }
 
-      coordinatorConf.setInteger("rss.jetty.http.port", 9529);
+      coordinatorConf.setInteger("rss.jetty.http.port", 0);
       cs3 = new CoordinatorServer(coordinatorConf);
       expectMessage = "Fail to start grpc server";
       try {
diff --git a/server/src/main/java/org/apache/uniffle/server/ShuffleServer.java 
b/server/src/main/java/org/apache/uniffle/server/ShuffleServer.java
index b5582b7d9..ac2481aad 100644
--- a/server/src/main/java/org/apache/uniffle/server/ShuffleServer.java
+++ b/server/src/main/java/org/apache/uniffle/server/ShuffleServer.java
@@ -98,6 +98,7 @@ public class ShuffleServer {
   private int nettyPort;
   private ShuffleServerConf shuffleServerConf;
   private JettyServer jettyServer;
+  private int jettyPort;
   private ShuffleTaskManager shuffleTaskManager;
   private ServerInterface server;
   private ShuffleFlushManager shuffleFlushManager;
@@ -152,7 +153,7 @@ public class ShuffleServer {
   public void start() throws Exception {
     LOG.info(
         "{} version: {}", this.getClass().getSimpleName(), 
Constants.VERSION_AND_REVISION_SHORT);
-    jettyServer.start();
+    jettyPort = jettyServer.start();
     grpcPort = server.start();
     if (nettyServerEnabled) {
       nettyPort = streamServer.start();
@@ -578,7 +579,7 @@ public class ShuffleServer {
   }
 
   public int getJettyPort() {
-    return jettyServer.getHttpPort();
+    return jettyPort;
   }
 
   public String getEncodedTags() {
diff --git 
a/server/src/test/java/org/apache/uniffle/server/ShuffleServerTest.java 
b/server/src/test/java/org/apache/uniffle/server/ShuffleServerTest.java
index 3356f6790..050659c62 100644
--- a/server/src/test/java/org/apache/uniffle/server/ShuffleServerTest.java
+++ b/server/src/test/java/org/apache/uniffle/server/ShuffleServerTest.java
@@ -61,7 +61,7 @@ public class ShuffleServerTest {
       assertEquals(expectStatus, ((ExitException) e).getStatus());
     }
 
-    serverConf.setInteger("rss.jetty.http.port", 9529);
+    serverConf.setInteger("rss.jetty.http.port", 0);
     ss2 = new ShuffleServer(serverConf);
     expectMessage = "Fail to start grpc server";
     try {

Reply via email to