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

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

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


##########
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:
    `PORT_RANGE_MIN` and `PORT_RANGE_MAX` should be configured.



##########
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:
   Port could be used by other processes, if we have many processes in one machine. For example,
   We find 888 port is available, then 888 is used by other process, we start to bind port. We fail.



##########
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:
   If we release a port, the port will be unavailable in short time because of `TIME_WAIT`.



-- 
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