Github user ahgittin commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/662#discussion_r31166051
  
    --- Diff: utils/common/src/main/java/brooklyn/util/net/Networking.java ---
    @@ -129,24 +127,89 @@ public static boolean isPortAvailable(InetAddress 
localAddress, int port) {
                     } catch (SocketException e) {
                         throw Exceptions.propagate(e);
                     }
    +                // When using a specific interface saw failures not caused 
by port already bound:
    +                //   * java.net.SocketException: No such device
    +                //   * java.net.BindException: Cannot assign requested 
address
    +                //   * probably many more
    +                // Check if the address is still valid before marking the 
port as not available.
    +                boolean foundAvailableInterface = false;
                     while (nis.hasMoreElements()) {
                         NetworkInterface ni = nis.nextElement();
                         Enumeration<InetAddress> as = ni.getInetAddresses();
                         while (as.hasMoreElements()) {
                             InetAddress a = as.nextElement();
    -                        if (!isPortAvailable(a, port))
    -                            return false;
    +                        if (!isPortAvailable(a, port)) {
    +                            if (isAddressValid(a)) {
    +                                if (log.isTraceEnabled()) log.trace("Port 
{} : {} @ {} is taken and the address is valid", new Object[] {a, port, nis});
    +                                return false;
    +                            }
    +                        } else {
    +                            foundAvailableInterface = true;
    +                        }
                         }
                     }
    +                if (!foundAvailableInterface) {
    +                    //Aborting with an error, even nextAvailablePort won't 
be able to find a free port.
    +                    throw new RuntimeException("Unable to bind on any 
network interface, even when letting the OS pick a port. Possible causes 
include file handle exhaustion, port exhaustion. Failed on request for " + 
localAddress + ":" + port + ".");
    +                }
                 }
    -            
    +
                 return true;
             } finally {
                 // Until timeout was added, was taking 1min5secs for 
/fe80:0:0:0:1cc5:1ff:fe81:a61d%8 : 8081
    -            if (log.isTraceEnabled()) log.trace("Took {} to determine if 
port {} : {} was available", 
    -                    new Object[] 
{Time.makeTimeString(watch.elapsed(TimeUnit.MILLISECONDS), true), localAddress, 
port});
    +            // Svet - Probably caused by the now gone new 
Socket().connect() call, SO_TIMEOUT doesn't
    +            // influence bind(). Doesn't hurt having it though.
    +            long elapsed = watch.elapsed(TimeUnit.SECONDS);
    +            boolean isDelayed = (elapsed >= 1);
    +            boolean isDelayedByMuch = (elapsed >= 30);
    +            if (isDelayed || log.isTraceEnabled()) {
    +                String msg = "Took {} to determine if port was available 
for {} : {}";
    +                Object[] args = new Object[] 
{Time.makeTimeString(watch.elapsed(TimeUnit.MILLISECONDS), true), localAddress, 
port};
    +                if (isDelayedByMuch) {
    +                    log.warn(msg, args);
    +                } else if (isDelayed) {
    +                    log.debug(msg, args);
    +                } else {
    +                    log.trace(msg, args);
    +                }
    +            }
             }
         }
    +
    +    /**
    +     * Bind to the specified IP, but let the OS pick a port.
    +     * If the operation fails we know it's not because of
    +     * non-available port, the interface could be down.
    +     * 
    +     * If there's port exhaustion on a single interface we won't catch it
    +     * and declare the port is free. Doesn't matter really because the
    +     * subsequent bind of the caller will fail anyway and nextAvailablePort
    +     * wouldn't be able to find a free one either.
    +     */
    +    private static boolean isAddressValid(InetAddress addr) {
    +        ServerSocket ss;
    +        try {
    +            ss = new ServerSocket();
    +            ss.setSoTimeout(250);
    +        } catch (IOException e) {
    +            throw Exceptions.propagate(e);
    +        }
    +        try {
    +            ss.bind(new InetSocketAddress(addr, 0));
    +            return true;
    +        } catch (IOException e) {
    +            Exceptions.propagateIfFatal(e);
    +            if (log.isTraceEnabled()) log.trace("Binding on {} failed, 
interface could be down, being reconfigured, file handle exhaustion, port 
exhaustion, etc.", addr);
    +            return false;
    +        } finally {
    +            try {
    +                ss.close();
    +            } catch (IOException e) {
    +                Exceptions.propagateIfFatal(e);
    --- End diff --
    
    as above - maybe a `closeQuietly` method?


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

Reply via email to