xiangfu0 commented on a change in pull request #6872:
URL: https://github.com/apache/incubator-pinot/pull/6872#discussion_r626298375



##########
File path: pinot-spi/src/main/java/org/apache/pinot/spi/utils/NetUtils.java
##########
@@ -57,4 +59,62 @@ public static String getHostnameOrAddress() {
       }
     }
   }
+
+  /**
+   * Find an open port.
+   * @return an open port
+   * @throws IOException
+   */
+  public static int findOpenPort()
+      throws IOException {
+    try (ServerSocket socket = new ServerSocket(0)) {
+      return socket.getLocalPort();
+    }
+  }
+
+  /**
+   * Find an open port,otherwise use given default port.
+   * @param defaultPort
+   * @return an open port otherwise default port
+   */
+  public static int findOpenPort(int defaultPort) {
+    if (available(defaultPort)) {
+      return defaultPort;
+    }
+    int port = defaultPort;
+    while (available(++port)) {
+      return port;
+    }
+    throw new RuntimeException("Unable to find an open port from range: [ " + 
defaultPort + ", " + port + " ]");
+  }
+
+  /**
+   * Checks to see if a specific port is available.
+   *
+   * @param port the port to check for availability
+   */
+  public static boolean available(int port) {
+    ServerSocket ss = null;
+    DatagramSocket ds = null;
+    try {
+      ss = new ServerSocket(port);
+      ss.setReuseAddress(true);
+      ds = new DatagramSocket(port);
+      ds.setReuseAddress(true);
+      return true;
+    } catch (IOException e) {

Review comment:
       done.




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

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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to