[
https://issues.apache.org/jira/browse/HDFS-10367?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15275604#comment-15275604
]
Masatake Iwasaki commented on HDFS-10367:
-----------------------------------------
{code}
while (true) {
if ((tries > 0 || tryPort == 0) && !needSamePort) {
tryPort = port + rand.nextInt(65535 - port);
}
if (tryPort == 0) {
continue;
}
try (ServerSocket s = new ServerSocket(tryPort)) {
LOG.info("Using port " + tryPort);
return tryPort;
{code}
Do you know the reason why manually choosing random number? I think we can get
available random port by {{new ServerSocket(0).getLocalPort()}} and it's more
portable. Assuming {{ServerSocketUtil#getPort}} could be implemented like
below, we can add new method like {{waitPort(int port)}} by using
{{isPortAvailable}}.
{code}
public static int getPort(int port) throws IOException {
if (isPortAvailable(port)) {
return port;
} else {
return getRandomPort();
}
}
private static boolean isPortAvailable(int port) {
try (ServerSocket s = new ServerSocket(port)) {
return true;
} catch (IOException e) {
return false;
}
}
public static int getRandomPort() throws IOException {
try (ServerSocket s = new ServerSocket(0)) {
int port = s.getLocalPort();
LOG.info("Using port " + port);
return port;
}
}
{code}
Though the code of ServerSocketUtil is not from this issue...
> TestDFSShell.testMoveWithTargetPortEmpty fails with Address bind exception.
> ---------------------------------------------------------------------------
>
> Key: HDFS-10367
> URL: https://issues.apache.org/jira/browse/HDFS-10367
> Project: Hadoop HDFS
> Issue Type: Bug
> Components: test
> Reporter: Brahma Reddy Battula
> Assignee: Brahma Reddy Battula
> Attachments: HDFS-10367-002.patch, HDFS-10367.patch
>
>
> {noformat}
> Problem binding to [localhost:9820] java.net.BindException: Address already
> in use; For more details see: http://wiki.apache.org/hadoop/BindException
> Stack Trace:
> java.net.BindException: Problem binding to [localhost:9820]
> java.net.BindException: Address already in use; For more details see:
> http://wiki.apache.org/hadoop/BindException
> at sun.nio.ch.Net.bind0(Native Method)
> at sun.nio.ch.Net.bind(Net.java:444)
> at sun.nio.ch.Net.bind(Net.java:436)
> at
> sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
> at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
> at org.apache.hadoop.ipc.Server.bind(Server.java:530)
> at org.apache.hadoop.ipc.Server$Listener.<init>(Server.java:793)
> at org.apache.hadoop.ipc.Server.<init>(Server.java:2592)
> at org.apache.hadoop.ipc.RPC$Server.<init>(RPC.java:958)
> at
> org.apache.hadoop.ipc.ProtobufRpcEngine$Server.<init>(ProtobufRpcEngine.java:563)
> at
> org.apache.hadoop.ipc.ProtobufRpcEngine.getServer(ProtobufRpcEngine.java:538)
> at org.apache.hadoop.ipc.RPC$Builder.build(RPC.java:800)
> at
> org.apache.hadoop.hdfs.server.namenode.NameNodeRpcServer.<init>(NameNodeRpcServer.java:426)
> at
> org.apache.hadoop.hdfs.server.namenode.NameNode.createRpcServer(NameNode.java:783)
> at
> org.apache.hadoop.hdfs.server.namenode.NameNode.initialize(NameNode.java:710)
> at
> org.apache.hadoop.hdfs.server.namenode.NameNode.<init>(NameNode.java:924)
> at
> org.apache.hadoop.hdfs.server.namenode.NameNode.<init>(NameNode.java:903)
> at
> org.apache.hadoop.hdfs.server.namenode.NameNode.createNameNode(NameNode.java:1620)
> at
> org.apache.hadoop.hdfs.MiniDFSCluster.createNameNode(MiniDFSCluster.java:1247)
> at
> org.apache.hadoop.hdfs.MiniDFSCluster.configureNameService(MiniDFSCluster.java:1016)
> at
> org.apache.hadoop.hdfs.MiniDFSCluster.createNameNodesAndSetConf(MiniDFSCluster.java:891)
> at
> org.apache.hadoop.hdfs.MiniDFSCluster.initMiniDFSCluster(MiniDFSCluster.java:823)
> at org.apache.hadoop.hdfs.MiniDFSCluster.<init>(MiniDFSCluster.java:482)
> at
> org.apache.hadoop.hdfs.MiniDFSCluster$Builder.build(MiniDFSCluster.java:441)
> at
> org.apache.hadoop.hdfs.TestDFSShell.testMoveWithTargetPortEmpty(TestDFSShell.java:567)
> {noformat}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]