Skip to content
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

Merged
merged 1 commit into from
Mar 15, 2016

Conversation

redsanket
Copy link

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.

socket = new ServerSocket(port);
localPort = socket.getLocalPort();
} catch (IOException exp) {
getAvailablePort(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

localPort = getAvailablePort(0);

Copy link
Contributor

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.

Copy link
Author

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.

@redsanket
Copy link
Author

@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) {
Copy link
Contributor

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.

@redsanket
Copy link
Author

@abhishekagarwal87 @knusbaum please take a look at it, it must go into Utils.java

@knusbaum
Copy link
Contributor

+1

@revans2
Copy link
Contributor

revans2 commented Feb 12, 2016

+1 lets squash

@redsanket
Copy link
Author

@revans squashed

return getAvailablePort(0);
}
}
return localPort;
Copy link
Contributor

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.

Copy link
Author

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

@knusbaum
Copy link
Contributor

@abhishekagarwal87 Good catch. I doubt it has any practical effects, but it's good to be consistent.

@knusbaum
Copy link
Contributor

Although, if we want to be consistent with the original code, removing the if and the final return statement is the solution.

@abhishekagarwal87
Copy link
Contributor

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.

@knusbaum
Copy link
Contributor

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.

@redsanket
Copy link
Author

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

@abhishekagarwal87
Copy link
Contributor

Yes. Original code tries only once and if condition should definitely be kept. It's fine with me.

@redsanket
Copy link
Author

@revans2 @knusbaum @abhishekagarwal87 still +1 ? Hope it is languishing too long out here.

@revans2
Copy link
Contributor

revans2 commented Feb 19, 2016

Yes still +1

@abhishekagarwal87
Copy link
Contributor

+1

@harshach
Copy link
Contributor

@redsanket can you upmerge.

@redsanket
Copy link
Author

@harshach upmerged and squashed

@asfgit asfgit merged commit ae619f3 into apache:master Mar 15, 2016
revans2 pushed a commit to revans2/incubator-storm that referenced this pull request May 7, 2018
YSTORM-4740:  Update the version of netty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants