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



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

Review comment:
       Update the javadoc

##########
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 + " ]");
+  }

Review comment:
       ```suggestion
     public static int findOpenPort(int basePort) {
       while (!available(basePort)) {
         basePort++;
       }
       return basePort;
     }
   ```

##########
File path: 
pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/ClusterTest.java
##########
@@ -125,6 +128,15 @@ protected void startBrokers(int numBrokers, int basePort, 
String zkStr)
       brokerStarter.start();
       _brokerStarters.add(brokerStarter);
     }
+    _brokerBaseApiUrl = "http://localhost:"; + _brokerPorts.get(0);
+  }
+
+  protected int getBrokerPort(int index) {

Review comment:
       Do not pass in index but return a random broker port?

##########
File path: 
pinot-common/src/main/java/org/apache/pinot/common/utils/ZkStarter.java
##########
@@ -34,16 +35,22 @@
 public class ZkStarter {
   private static final Logger LOGGER = 
LoggerFactory.getLogger(ZkStarter.class);
   public static final int DEFAULT_ZK_TEST_PORT = 2191;
-  public static final String DEFAULT_ZK_STR = "localhost:" + 
DEFAULT_ZK_TEST_PORT;
   private static final int DEFAULT_ZK_CLIENT_RETRIES = 10;
+  private static int ZK_TEST_PORT = 2191;

Review comment:
       +1

##########
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:
       Probably more readable returning false within the catch?




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