-
Notifications
You must be signed in to change notification settings - Fork 547
[SYSTEMDS-3959] Report an error when a federated worker cannot bind its port #2586
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
Open
gaturchenko
wants to merge
1
commit into
apache:main
Choose a base branch
from
gaturchenko:fed-worker-invalid-port
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.sysds.utils; | ||
|
|
||
| import java.net.BindException; | ||
|
|
||
| /** | ||
| * Helpers to validate TCP port numbers of the servers started from the command line, i.e., the federated worker (-w) | ||
| * and the federated monitoring backend (-fedMonitoring). | ||
| */ | ||
| public class PortUtils { | ||
| /** | ||
| * The lowest port a server is allowed to bind to. Port 0 is excluded as it binds an arbitrary free port, which | ||
| * peers of a federated worker cannot address. | ||
| */ | ||
| public static final int MIN_PORT = 1; | ||
|
|
||
| /** Highest port representable in the 16-bit TCP port field. */ | ||
| public static final int MAX_PORT = 65535; | ||
|
|
||
| /** Ports up to and including this one are reserved system ports on unix-like systems. */ | ||
| public static final int MAX_PRIVILEGED_PORT = 1023; | ||
|
|
||
| private PortUtils() { | ||
| // prevent instantiation of this utility class | ||
| } | ||
|
|
||
| /** | ||
| * Check whether the given port is inside the range of ports a server can be bound to. | ||
| * | ||
| * @param port the port to check | ||
| * @return true if the port is in [{@link #MIN_PORT}, {@link #MAX_PORT}] | ||
| */ | ||
| public static boolean isValidPort(int port) { | ||
| return port >= MIN_PORT && port <= MAX_PORT; | ||
| } | ||
|
|
||
| /** | ||
| * Check whether the given port is a reserved system port, i.e., a port that typically requires elevated privileges | ||
| * to bind to. | ||
| * | ||
| * @param port the port to check | ||
| * @return true if the port is a privileged port | ||
| */ | ||
| public static boolean isPrivilegedPort(int port) { | ||
| return port >= MIN_PORT && port <= MAX_PRIVILEGED_PORT; | ||
| } | ||
|
|
||
| /** | ||
| * Parse a port given as a command line argument. | ||
| * | ||
| * @param value the raw argument value | ||
| * @param option the name of the option the value belongs to, used for the error message | ||
| * @return the parsed port | ||
| * @throws IllegalArgumentException if the value is not an integer inside the valid port range | ||
| */ | ||
| public static int parsePort(String value, String option) { | ||
| final int port; | ||
| try { | ||
| port = Integer.parseInt(value.trim()); | ||
| } | ||
| catch(NumberFormatException e) { | ||
| throw new IllegalArgumentException( | ||
| "Invalid port '" + value + "' for option " + option + ": not an integer, " + rangeHint()); | ||
| } | ||
| if(!isValidPort(port)) | ||
| throw new IllegalArgumentException( | ||
| "Invalid port " + port + " for option " + option + ": out of range, " + rangeHint()); | ||
| return port; | ||
| } | ||
|
|
||
| /** The valid range, phrased for an error message. */ | ||
| private static String rangeHint() { | ||
| return "expected a port in [" + MIN_PORT + ", " + MAX_PORT + "]"; | ||
| } | ||
|
|
||
| /** | ||
| * Translate a failure of a server bind into a message that names the likely cause, since the exceptions of the | ||
| * underlying socket implementation are platform-specific and terse. | ||
| * | ||
| * @param port the port the server tried to bind to | ||
| * @param e the exception the bind failed with | ||
| * @return a human-readable explanation of the failure | ||
| */ | ||
| public static String explainBindFailure(int port, Throwable e) { | ||
| final String msg = (e.getMessage() != null) ? e.getMessage() : e.getClass().getSimpleName(); | ||
| if(!isValidPort(port)) | ||
| return "port " + port + " is out of range, " + rangeHint() + " (" + msg + ")"; | ||
| if(e instanceof BindException) { | ||
| if(msg.toLowerCase().contains("permission denied")) | ||
| return "no permission to bind port " + port + (isPrivilegedPort(port) ? ", ports below " | ||
| + (MAX_PRIVILEGED_PORT + 1) + " are reserved and require elevated privileges" : "") + " (" + msg | ||
| + ")"; | ||
| return "port " + port + " is already in use (" + msg + ")"; | ||
| } | ||
| return msg; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Only criticism. I am strongly against adding more logic to the systemds file. Can we not just have the error message and verification inside Java. This gives a single point of failure, rather than splitting the logic in two.
While there was some logic already verifying basic number compatibility, i think that should also just be cleaned up and moved into the java PortUtils you have defined.