-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[STORM-1523] util.clj available-port conversion to java #1073
Conversation
socket = new ServerSocket(port); | ||
localPort = socket.getLocalPort(); | ||
} catch (IOException exp) { | ||
getAvailablePort(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
localPort = getAvailablePort(0);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also this should be done only if the input port is non-zero. similar to what is happening in clojure. since there is no control on recursion depth here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch thanks, I will do that.
@revans2 @abhishekagarwal87 fixed the issues I presume. Let me know if you have any concerns |
@@ -1374,5 +1393,21 @@ public static RuntimeException wrapInRuntime(Exception e){ | |||
return new RuntimeException(e); | |||
} | |||
} | |||
|
|||
public static int getAvailablePort(int port) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will be good to rename the port to preferredPort since it only makes a best effort guarantee.
@abhishekagarwal87 @knusbaum please take a look at it, it must go into Utils.java |
+1 |
+1 lets squash |
0d9c7f2
to
ab96d3a
Compare
@revans squashed |
return getAvailablePort(0); | ||
} | ||
} | ||
return localPort; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one minor comment. previous version never returns -1. can we just bubble up the exception in catch block if preferredPort is not greater than zero and not return a negative port? Other than that +1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abhishekagarwal87 socket.getLocalPort() returns -1 if no port is found as per the internal implementation. No harm in assigning localPort with -1
/**
* Returns the port number on which this socket is listening.
*
* If the socket was bound prior to being {@link #close closed},
* then this method will continue to return the port number
* after the socket is closed.
*
* @return the port number to which this socket is listening or
* -1 if the socket is not bound yet.
*/
public int getLocalPort() {
if (!isBound())
return -1;
try {
return getImpl().getLocalPort();
} catch (SocketException e) {
// nothing
// If we're bound, the impl has been created
// so we shouldn't get here
}
return -1;
}
@abhishekagarwal87 Good catch. I doubt it has any practical effects, but it's good to be consistent. |
Although, if we want to be consistent with the original code, removing the |
If there is an exception, tests are going to fail nonetheless. If the exception is bubbled up, test failure will show the right error. Though yes I agree, that in practice binding to port zero is not likely to result in an exception. |
Trying with value 0 on failure was a potentially important behavior of the previous implementation. Exceptions will still bubble up if it fails if my solution is implemented. |
@knusbaum @abhishekagarwal87 The original implementation would return -1 in any case if the port is not bound yet. If we not changing the final outcome and unless we are concerned about throwing an exception when the port is not bound, I guess it would be fine. Moreover, removing if condition would keep trying to find the port recursively until it finds it I presume. The original code only tries once as per my understanding. |
Yes. Original code tries only once and if condition should definitely be kept. It's fine with me. |
@revans2 @knusbaum @abhishekagarwal87 still +1 ? Hope it is languishing too long out here. |
Yes still +1 |
+1 |
@redsanket can you upmerge. |
ab96d3a
to
ae619f3
Compare
@harshach upmerged and squashed |
YSTORM-4740: Update the version of netty
Please note this is a small change when compared to a bigger util.clj to java PR.
It is being put first as the work needed to convert the entire file is taken care and will be up soon.
Just did not want to depend on other patches so putting this up first.