You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@uniffle.apache.org by "advancedxy (via GitHub)" <gi...@apache.org> on 2023/03/16 03:32:08 UTC

[GitHub] [incubator-uniffle] advancedxy commented on a diff in pull request #723: [#720] feat(netty): Support random netty port

advancedxy commented on code in PR #723:
URL: https://github.com/apache/incubator-uniffle/pull/723#discussion_r1138053613


##########
common/src/main/java/org/apache/uniffle/common/util/ServerPortUtils.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.uniffle.common.util;
+
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.util.Random;
+import javax.net.ServerSocketFactory;
+
+
+public class ServerPortUtils {
+
+  private static final int PORT_RANGE_MIN = 40000;

Review Comment:
   👍 



##########
common/src/test/java/org/apache/uniffle/common/util/RssUtilsTest.java:
##########
@@ -87,6 +87,19 @@ public void testGetHostIp() {
     }
   }
 
+  @Test
+  public void testSelectNettyPort() {
+    int nettyPortConf = -1;
+    assertEquals(RssUtils.selectNettyPort(nettyPortConf), nettyPortConf);
+    nettyPortConf = 28888;
+    assertEquals(RssUtils.selectNettyPort(nettyPortConf), nettyPortConf);
+    nettyPortConf = 0;
+    for (int i = 0; i < 100; i++) {
+      assertTrue(ServerPortUtils.isPortAvailable(RssUtils.selectNettyPort(nettyPortConf)));

Review Comment:
   I think this might not enough.. you may need to add port conflict cases.



##########
common/src/main/java/org/apache/uniffle/common/util/ServerPortUtils.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.uniffle.common.util;
+
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.util.Random;
+import javax.net.ServerSocketFactory;
+
+
+public class ServerPortUtils {
+
+  private static final int PORT_RANGE_MIN = 40000;
+  private static final int PORT_RANGE_MAX = 65535;
+  private static final int PORT_RANGE = PORT_RANGE_MAX - PORT_RANGE_MIN;
+  private static final int MAX_ATTEMPTS = 1_000;
+  private static final Random random = new Random(System.nanoTime());
+
+  public static int findAvailableTcpPort() {
+    int candidatePort;
+    int searchCounter = 0;
+    do {
+      if (searchCounter > MAX_ATTEMPTS) {
+        throw new IllegalStateException(String.format(
+            "Could not find an available TCP port in the range [%d, %d] after %d attempts",
+            PORT_RANGE_MIN, PORT_RANGE_MAX, MAX_ATTEMPTS));
+      }
+      candidatePort = PORT_RANGE_MIN + random.nextInt(PORT_RANGE + 1);
+      searchCounter++;
+    } while (!isPortAvailable(candidatePort));
+
+    return candidatePort;
+  }
+
+  /**
+   * Determine if the specified TCP port is currently available on {@code localhost}.
+   */
+  public static boolean isPortAvailable(int port) {
+    try {

Review Comment:
   +1. Could we try to bind grpc service in the shuffle server? you can refer how Spark bind service. I need to bind ports in ranges.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org