zentol commented on a change in pull request #7263: [FLINK-11081] Support 
binding port range for REST server
URL: https://github.com/apache/flink/pull/7263#discussion_r255054389
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/rest/RestServerEndpoint.java
 ##########
 @@ -181,14 +185,45 @@ protected void initChannel(SocketChannel ch) {
                                .channel(NioServerSocketChannel.class)
                                .childHandler(initializer);
 
-                       log.debug("Binding rest endpoint to {}:{}.", 
restBindAddress, restBindPort);
-                       final ChannelFuture channel;
-                       if (restBindAddress == null) {
-                               channel = bootstrap.bind(restBindPort);
-                       } else {
-                               channel = bootstrap.bind(restBindAddress, 
restBindPort);
+                       ChannelFuture channel;
+
+                       // parse port range definition and create port iterator
+                       Iterator<Integer> portsIterator;
+                       try {
+                               portsIterator = 
NetUtils.getPortRangeFromString(restBindPort);
+                       } catch (IllegalConfigurationException e) {
+                               throw e;
+                       } catch (Exception e) {
+                               throw new IllegalArgumentException("Invalid 
port range definition: " + restBindPort);
+                       }
+
+                       int chosenPort = 0;
+                       while (portsIterator.hasNext()) {
+                               try {
+                                       chosenPort = portsIterator.next();
+                                       if (restBindAddress == null) {
+                                               channel = 
bootstrap.bind(chosenPort);
+                                       } else {
+                                               channel = 
bootstrap.bind(restBindAddress, chosenPort);
+                                       }
+                                       serverChannel = 
channel.syncUninterruptibly().channel();
+                                       break;
+                               } catch (Exception e) {
+                                       // we can continue the loop to try 
follow-up ports if this contains a netty channel exception
+                                       if (!(e instanceof 
org.jboss.netty.channel.ChannelException ||
+                                               e instanceof 
java.net.BindException)) {
+                                               throw e;
+                                       } // else fall through the loop and try 
the next port
+                               }
                        }
-                       serverChannel = channel.syncUninterruptibly().channel();
+
+                       if (serverChannel == null) {
+                               // if we come here, we have exhausted the port 
range
+                               throw new BindException("Could not start task 
manager on any port in port range "
 
 Review comment:
   should say rest server endpoint

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to