HDFS-9444. Add utility to find set of available ephemeral ports to ServerSocketUtil. Contributed by Masatake Iwasaki

This commit is contained in:
Brahma Reddy Battula 2016-09-27 07:04:37 +05:30
parent 059058f961
commit edf0d0f8b2
3 changed files with 75 additions and 24 deletions

View File

@ -102,4 +102,27 @@ public static int waitForPort(int port, int retries)
} }
} }
} }
/**
* Find the specified number of unique ports available.
* The ports are all closed afterwards,
* so other network services started may grab those same ports.
*
* @param numPorts number of required port nubmers
* @return array of available port numbers
* @throws IOException
*/
public static int[] getPorts(int numPorts) throws IOException {
ServerSocket[] sockets = new ServerSocket[numPorts];
int[] ports = new int[numPorts];
for (int i = 0; i < numPorts; i++) {
ServerSocket sock = new ServerSocket(0);
sockets[i] = sock;
ports[i] = sock.getLocalPort();
}
for (ServerSocket sock : sockets) {
sock.close();
}
return ports;
}
} }

View File

@ -47,6 +47,7 @@
import javax.management.ObjectName; import javax.management.ObjectName;
import java.io.File; import java.io.File;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.net.BindException;
import java.net.URI; import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -59,6 +60,7 @@
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/** /**
* Class for testing {@link NameNodeMXBean} implementation * Class for testing {@link NameNodeMXBean} implementation
@ -431,17 +433,29 @@ public void testQueueLength() throws Exception {
public void testNNDirectorySize() throws Exception{ public void testNNDirectorySize() throws Exception{
Configuration conf = new Configuration(); Configuration conf = new Configuration();
conf.setInt(DFSConfigKeys.DFS_HA_TAILEDITS_PERIOD_KEY, 1); conf.setInt(DFSConfigKeys.DFS_HA_TAILEDITS_PERIOD_KEY, 1);
// Have to specify IPC ports so the NNs can talk to each other. MiniDFSCluster cluster = null;
MiniDFSNNTopology topology = new MiniDFSNNTopology() for (int i = 0; i < 5; i++) {
.addNameservice(new MiniDFSNNTopology.NSConf("ns1") try{
.addNN(new MiniDFSNNTopology.NNConf("nn1") // Have to specify IPC ports so the NNs can talk to each other.
.setIpcPort(ServerSocketUtil.getPort(0, 100))) int[] ports = ServerSocketUtil.getPorts(2);
.addNN(new MiniDFSNNTopology.NNConf("nn2") MiniDFSNNTopology topology = new MiniDFSNNTopology()
.setIpcPort(ServerSocketUtil.getPort(0, 100)))); .addNameservice(new MiniDFSNNTopology.NSConf("ns1")
.addNN(new MiniDFSNNTopology.NNConf("nn1").setIpcPort(ports[0]))
.addNN(
new MiniDFSNNTopology.NNConf("nn2").setIpcPort(ports[1])));
MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf) cluster = new MiniDFSCluster.Builder(conf)
.nnTopology(topology).numDataNodes(0) .nnTopology(topology).numDataNodes(0)
.build(); .build();
break;
} catch (BindException e) {
// retry if race on ports given by ServerSocketUtil#getPorts
continue;
}
}
if (cluster == null) {
fail("failed to start mini cluster.");
}
FileSystem fs = null; FileSystem fs = null;
try { try {
cluster.waitActive(); cluster.waitActive();

View File

@ -24,6 +24,7 @@
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.BindException;
import java.net.URI; import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -166,20 +167,33 @@ private static void testStandbyTriggersLogRolls(int activeIndex)
conf.setInt(DFSConfigKeys.DFS_HA_TAILEDITS_PERIOD_KEY, 1); conf.setInt(DFSConfigKeys.DFS_HA_TAILEDITS_PERIOD_KEY, 1);
conf.setInt(DFSConfigKeys.DFS_HA_TAILEDITS_ALL_NAMESNODES_RETRY_KEY, 100); conf.setInt(DFSConfigKeys.DFS_HA_TAILEDITS_ALL_NAMESNODES_RETRY_KEY, 100);
// Have to specify IPC ports so the NNs can talk to each other. MiniDFSCluster cluster = null;
MiniDFSNNTopology topology = new MiniDFSNNTopology() for (int i = 0; i < 5; i++) {
.addNameservice(new MiniDFSNNTopology.NSConf("ns1") try {
.addNN(new MiniDFSNNTopology.NNConf("nn1") // Have to specify IPC ports so the NNs can talk to each other.
.setIpcPort(ServerSocketUtil.getPort(0, 100))) int[] ports = ServerSocketUtil.getPorts(3);
.addNN(new MiniDFSNNTopology.NNConf("nn2") MiniDFSNNTopology topology = new MiniDFSNNTopology()
.setIpcPort(ServerSocketUtil.getPort(0, 100))) .addNameservice(new MiniDFSNNTopology.NSConf("ns1")
.addNN(new MiniDFSNNTopology.NNConf("nn3") .addNN(new MiniDFSNNTopology.NNConf("nn1")
.setIpcPort(ServerSocketUtil.getPort(0, 100)))); .setIpcPort(ports[0]))
.addNN(new MiniDFSNNTopology.NNConf("nn2")
.setIpcPort(ports[1]))
.addNN(new MiniDFSNNTopology.NNConf("nn3")
.setIpcPort(ports[2])));
MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf) cluster = new MiniDFSCluster.Builder(conf)
.nnTopology(topology) .nnTopology(topology)
.numDataNodes(0) .numDataNodes(0)
.build(); .build();
break;
} catch (BindException e) {
// retry if race on ports given by ServerSocketUtil#getPorts
continue;
}
}
if (cluster == null) {
fail("failed to start mini cluster.");
}
try { try {
cluster.transitionToActive(activeIndex); cluster.transitionToActive(activeIndex);
waitForLogRollInSharedDir(cluster, 3); waitForLogRollInSharedDir(cluster, 3);