Github user tillrohrmann commented on a diff in the pull request:
https://github.com/apache/flink/pull/3391#discussion_r103202459
--- Diff: flink-core/src/main/java/org/apache/flink/util/NetUtils.java ---
@@ -373,6 +374,46 @@ public static ServerSocket
createSocketFromPorts(Iterator<Integer> portsIterator
}
/**
+ * Tries to create a server from the given sets of ports.
+ *
+ * @param address An address to listen at.
+ * @param portRange A set of ports to choose from.
+ * @param serverFactory A factory for creating server.
+ * @return the created server.
+ * @throws BindException If port range is exhausted.
+ */
+ public static <T> T createServerFromPorts(String address,
Iterator<Integer> portRange, ServerFactory<T> serverFactory) throws Exception {
+
+ while (portRange.hasNext()) {
+ ServerSocket availableSocket =
NetUtils.createSocketFromPorts(
+ portRange,
+ new NetUtils.SocketFactory() {
+ @Override
+ public ServerSocket createSocket(int
port) throws IOException {
+ return new ServerSocket(port);
+ }
+ });
+
+ int port;
+ if (availableSocket == null) {
+ throw new BindException("Could not start server
on any port in port range: " + portRange);
+ } else {
+ port = availableSocket.getLocalPort();
+ try {
+ availableSocket.close();
+ } catch (IOException ignored) {}
+ }
+
+ try {
+ return serverFactory.create(address, port);
+ } catch (BindException e) {}
--- End diff --
What if the creation of the server fails for some other reason? Shouldn't
we retry the other ports as well?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---