Skip to content

Commit 45c8193

Browse files
committed
introduce a more elegant way to use jetty port
1 parent 1e92254 commit 45c8193

File tree

13 files changed

+55
-272
lines changed

13 files changed

+55
-272
lines changed

common/src/main/java/org/apache/uniffle/common/web/JettyServer.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,15 @@ public Server getServer() {
169169
return this.server;
170170
}
171171

172-
public void start() throws Exception {
172+
public int start() throws Exception {
173173
try {
174174
server.start();
175+
httpPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
175176
} catch (BindException e) {
176177
ExitUtils.terminate(1, "Fail to start jetty http server, port is " + httpPort, e, LOG);
177178
}
178179
LOG.info("Jetty http server started, listening on port {}", httpPort);
180+
return httpPort;
179181
}
180182

181183
public void stop() throws Exception {

common/src/test/java/org/apache/uniffle/common/port/PortRegistry.java

-178
This file was deleted.

common/src/test/java/org/apache/uniffle/common/web/JettyServerTest.java

+12-24
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,26 @@
1717

1818
package org.apache.uniffle.common.web;
1919

20-
import java.io.FileNotFoundException;
21-
2220
import org.eclipse.jetty.server.Handler;
2321
import org.eclipse.jetty.server.Server;
2422
import org.eclipse.jetty.server.ServerConnector;
2523
import org.eclipse.jetty.servlet.ServletContextHandler;
2624
import org.eclipse.jetty.util.thread.ExecutorThreadPool;
27-
import org.junit.jupiter.api.AfterEach;
28-
import org.junit.jupiter.api.BeforeEach;
2925
import org.junit.jupiter.api.Test;
3026

3127
import org.apache.uniffle.common.config.RssBaseConf;
32-
import org.apache.uniffle.common.port.PortRegistry;
3328
import org.apache.uniffle.common.util.ExitUtils;
3429
import org.apache.uniffle.common.util.ExitUtils.ExitException;
3530

3631
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
3733
import static org.junit.jupiter.api.Assertions.assertTrue;
3834

3935
public class JettyServerTest {
40-
41-
int port;
42-
43-
@BeforeEach
44-
public void beforeEach() {
45-
port = PortRegistry.reservePort();
46-
}
47-
48-
@AfterEach
49-
public void afterEach() {
50-
PortRegistry.release(port);
51-
}
52-
5336
@Test
54-
public void jettyServerTest() throws FileNotFoundException {
37+
public void jettyServerTest() throws Exception {
5538
RssBaseConf conf = new RssBaseConf();
56-
conf.setInteger("rss.jetty.http.port", port);
39+
conf.setInteger("rss.jetty.http.port", 0);
5740
JettyServer jettyServer = new JettyServer(conf);
5841
Server server = jettyServer.getServer();
5942

@@ -65,21 +48,26 @@ public void jettyServerTest() throws FileNotFoundException {
6548
assertEquals(server, server.getHandler().getServer());
6649
assertTrue(server.getConnectors()[0] instanceof ServerConnector);
6750
ServerConnector connector = (ServerConnector) server.getConnectors()[0];
68-
assertEquals(port, connector.getPort());
51+
assertEquals(0, connector.getPort());
52+
jettyServer.start();
53+
assertEquals(jettyServer.getHttpPort(), connector.getLocalPort());
54+
assertNotEquals(jettyServer.getHttpPort(), 0);
6955

7056
assertEquals(1, server.getHandlers().length);
7157
Handler handler = server.getHandler();
7258
assertTrue(handler instanceof ServletContextHandler);
59+
jettyServer.stop();
7360
}
7461

7562
@Test
7663
public void jettyServerStartTest() throws Exception {
7764
RssBaseConf conf = new RssBaseConf();
78-
conf.setInteger("rss.jetty.http.port", port);
65+
conf.setInteger("rss.jetty.http.port", 0);
7966
JettyServer jettyServer1 = new JettyServer(conf);
80-
JettyServer jettyServer2 = new JettyServer(conf);
81-
jettyServer1.start();
8267

68+
int portExist = jettyServer1.start();
69+
conf.setInteger("rss.jetty.http.port", portExist);
70+
JettyServer jettyServer2 = new JettyServer(conf);
8371
ExitUtils.disableSystemExit();
8472
final String expectMessage = "Fail to start jetty http server";
8573
final int expectStatus = 1;

coordinator/src/main/java/org/apache/uniffle/coordinator/CoordinatorServer.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class CoordinatorServer {
6363
private final CoordinatorConf coordinatorConf;
6464
private final long startTimeMs;
6565
private JettyServer jettyServer;
66+
private int jettyPort;
6667
private ServerInterface server;
6768
private ClusterManager clusterManager;
6869
private AssignmentStrategy assignmentStrategy;
@@ -106,7 +107,7 @@ public static void main(String[] args) throws Exception {
106107
public void start() throws Exception {
107108
LOG.info(
108109
"{} version: {}", this.getClass().getSimpleName(), Constants.VERSION_AND_REVISION_SHORT);
109-
jettyServer.start();
110+
jettyPort = jettyServer.start();
110111
rpcListenPort = server.start();
111112
if (metricReporter != null) {
112113
metricReporter.start();
@@ -285,4 +286,8 @@ public long getStartTimeMs() {
285286
public int getRpcListenPort() {
286287
return rpcListenPort;
287288
}
289+
290+
public int getJettyPort() {
291+
return jettyPort;
292+
}
288293
}

integration-test/common/src/test/java/org/apache/uniffle/test/AccessClusterTest.java

-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535

3636
import org.apache.uniffle.client.request.RssAccessClusterRequest;
3737
import org.apache.uniffle.client.response.RssAccessClusterResponse;
38-
import org.apache.uniffle.common.config.RssBaseConf;
3938
import org.apache.uniffle.common.rpc.ServerType;
4039
import org.apache.uniffle.common.rpc.StatusCode;
4140
import org.apache.uniffle.common.util.Constants;
@@ -171,12 +170,8 @@ public void test(@TempDir File tempDir) throws Exception {
171170
response = coordinatorClient.accessCluster(request);
172171
assertEquals(StatusCode.ACCESS_DENIED, response.getStatusCode());
173172
assertTrue(response.getMessage().startsWith("Denied by AccessClusterLoadChecker"));
174-
175-
List<Integer> ports = reserveJettyPorts(1);
176173
ShuffleServerConf shuffleServerConf = shuffleServerConfWithoutPort(0, tempDir, ServerType.GRPC);
177174
shuffleServerConf.setString("rss.coordinator.quorum", getQuorum());
178-
shuffleServerConf.setInteger(RssBaseConf.RPC_SERVER_PORT, 0);
179-
shuffleServerConf.setInteger(RssBaseConf.JETTY_HTTP_PORT, ports.get(0));
180175
ShuffleServer shuffleServer = new ShuffleServer(shuffleServerConf);
181176
shuffleServer.start();
182177
// this make sure the server can be shutdown

integration-test/common/src/test/java/org/apache/uniffle/test/CoordinatorAdminServiceTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static void setUp() throws Exception {
5252

5353
@BeforeEach
5454
public void createClient() {
55-
String hostUrl = String.format("http://%s:%d", LOCALHOST, jettyPorts.get(0));
55+
String hostUrl = String.format("http://%s:%d", LOCALHOST, coordinators.get(0).getJettyPort());
5656
adminRestApi = new AdminRestApi(UniffleRestClient.builder(hostUrl).build());
5757
}
5858

integration-test/common/src/test/java/org/apache/uniffle/test/CoordinatorGrpcTest.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ public void getShuffleAssignmentsTest(@TempDir File tmpDir) throws Exception {
143143
CoordinatorTestUtils.waitForRegister(coordinatorClient, 2);
144144

145145
grpcShuffleServers.get(0).stopServer();
146-
List<Integer> ports = reserveJettyPorts(1);
147146
ShuffleServerConf shuffleServerConf = shuffleServerConfWithoutPort(0, tmpDir, ServerType.GRPC);
148147
shuffleServerConf.set(ShuffleServerConf.STORAGE_MEDIA_PROVIDER_ENV_KEY, "RSS_ENV_KEY");
149148
String baseDir = shuffleServerConf.get(ShuffleServerConf.RSS_STORAGE_BASE_PATH).get(0);
@@ -154,7 +153,7 @@ public void getShuffleAssignmentsTest(@TempDir File tmpDir) throws Exception {
154153
() -> {
155154
shuffleServerConf.setString("rss.coordinator.quorum", getQuorum());
156155
shuffleServerConf.setInteger(RssBaseConf.RPC_SERVER_PORT, 0);
157-
shuffleServerConf.setInteger(RssBaseConf.JETTY_HTTP_PORT, ports.get(0));
156+
shuffleServerConf.setInteger(RssBaseConf.JETTY_HTTP_PORT, 0);
158157
ShuffleServer ss = new ShuffleServer(shuffleServerConf);
159158
ss.start();
160159
grpcShuffleServers.set(0, ss);
@@ -298,7 +297,6 @@ public void shuffleServerHeartbeatTest(@TempDir File tempDir) throws Exception {
298297
assertTrue(node.getTags().contains(Constants.SHUFFLE_SERVER_VERSION));
299298
assertTrue(scm.getTagToNodes().get(Constants.SHUFFLE_SERVER_VERSION).contains(node));
300299

301-
List<Integer> ports = reserveJettyPorts(1);
302300
ShuffleServerConf shuffleServerConf = shuffleServerConfWithoutPort(0, tempDir, ServerType.GRPC);
303301
shuffleServerConf.set(ShuffleServerConf.STORAGE_MEDIA_PROVIDER_ENV_KEY, "RSS_ENV_KEY");
304302
String baseDir = shuffleServerConf.get(ShuffleServerConf.RSS_STORAGE_BASE_PATH).get(0);
@@ -311,7 +309,7 @@ public void shuffleServerHeartbeatTest(@TempDir File tempDir) throws Exception {
311309
shuffleServerConf.set(ShuffleServerConf.TAGS, Lists.newArrayList("SSD"));
312310
shuffleServerConf.setString("rss.coordinator.quorum", getQuorum());
313311
shuffleServerConf.setInteger(RssBaseConf.RPC_SERVER_PORT, 0);
314-
shuffleServerConf.setInteger(RssBaseConf.JETTY_HTTP_PORT, ports.get(0));
312+
shuffleServerConf.setInteger(RssBaseConf.JETTY_HTTP_PORT, 0);
315313
ShuffleServer ss = new ShuffleServer(shuffleServerConf);
316314
ss.start();
317315
grpcShuffleServers.set(0, ss);

0 commit comments

Comments
 (0)