1818package org .apache .hadoop .ozone ;
1919
2020import java .io .File ;
21- import java .net .ServerSocket ;
2221import java .util .ArrayList ;
2322import java .util .List ;
2423import java .util .Optional ;
6463import java .util .UUID ;
6564import java .util .concurrent .TimeUnit ;
6665import java .util .concurrent .TimeoutException ;
67- import java .util .concurrent .atomic .AtomicInteger ;
6866
6967import static org .apache .hadoop .hdds .HddsConfigKeys .HDDS_HEARTBEAT_INTERVAL ;
7068import static org .apache .hadoop .hdds .protocol .proto .HddsProtos .NodeState
@@ -390,9 +388,6 @@ private void setCAClient(CertificateClient client) {
390388 */
391389 public static class Builder extends MiniOzoneCluster .Builder {
392390
393- private static AtomicInteger lastUsedPort =
394- new AtomicInteger (1000 );
395-
396391 /**
397392 * Creates a new Builder.
398393 *
@@ -534,16 +529,14 @@ OzoneManager createOM()
534529 */
535530 List <HddsDatanodeService > createHddsDatanodes (
536531 StorageContainerManager scm ) throws IOException {
537-
538- String scmAddress = scm .getDatanodeRpcAddress ().getHostString () +
532+ configureHddsDatanodes ();
533+ String scmAddress = scm .getDatanodeRpcAddress ().getHostString () +
539534 ":" + scm .getDatanodeRpcAddress ().getPort ();
540535 String [] args = new String [] {};
541536 conf .setStrings (ScmConfigKeys .OZONE_SCM_NAMES , scmAddress );
542-
543537 List <HddsDatanodeService > hddsDatanodes = new ArrayList <>();
544538 for (int i = 0 ; i < numOfDatanodes ; i ++) {
545539 OzoneConfiguration dnConf = new OzoneConfiguration (conf );
546- configureHddsDatanodes (dnConf );
547540 String datanodeBaseDir = path + "/datanode-" + Integer .toString (i );
548541 Path metaDir = Paths .get (datanodeBaseDir , "meta" );
549542 Path dataDir = Paths .get (datanodeBaseDir , "data" , "containers" );
@@ -570,14 +563,10 @@ List<HddsDatanodeService> createHddsDatanodes(
570563 }
571564
572565 private void configureSCM () {
573- conf .set (ScmConfigKeys .OZONE_SCM_CLIENT_ADDRESS_KEY ,
574- "127.0.0.1:" + findPort ());
575- conf .set (ScmConfigKeys .OZONE_SCM_BLOCK_CLIENT_ADDRESS_KEY ,
576- "127.0.0.1:" + findPort ());
577- conf .set (ScmConfigKeys .OZONE_SCM_DATANODE_ADDRESS_KEY ,
578- "127.0.0.1:" + findPort ());
579- conf .set (ScmConfigKeys .OZONE_SCM_HTTP_ADDRESS_KEY ,
580- "127.0.0.1:" + findPort ());
566+ conf .set (ScmConfigKeys .OZONE_SCM_CLIENT_ADDRESS_KEY , "127.0.0.1:0" );
567+ conf .set (ScmConfigKeys .OZONE_SCM_BLOCK_CLIENT_ADDRESS_KEY , "127.0.0.1:0" );
568+ conf .set (ScmConfigKeys .OZONE_SCM_DATANODE_ADDRESS_KEY , "127.0.0.1:0" );
569+ conf .set (ScmConfigKeys .OZONE_SCM_HTTP_ADDRESS_KEY , "127.0.0.1:0" );
581570 conf .setInt (ScmConfigKeys .OZONE_SCM_HANDLER_COUNT_KEY , numOfScmHandlers );
582571 configureSCMheartbeat ();
583572 }
@@ -608,42 +597,19 @@ private void configureSCMheartbeat() {
608597
609598
610599 private void configureOM () {
611- conf .set (OMConfigKeys .OZONE_OM_ADDRESS_KEY , "127.0.0.1:" + findPort ());
612- conf .set (OMConfigKeys .OZONE_OM_HTTP_ADDRESS_KEY ,
613- "127.0.0.1:" + findPort ());
600+ conf .set (OMConfigKeys .OZONE_OM_ADDRESS_KEY , "127.0.0.1:0" );
601+ conf .set (OMConfigKeys .OZONE_OM_HTTP_ADDRESS_KEY , "127.0.0.1:0" );
614602 conf .setInt (OMConfigKeys .OZONE_OM_HANDLER_COUNT_KEY , numOfOmHandlers );
615603 }
616604
617- /**
618- * Return an available TCP port if available.
619- * <p>
620- * As we have a static counter the port should be unique inside the JVM..
621- */
622- private int findPort () {
623- while (lastUsedPort .get () < 65536 ) {
624- try {
625- int nextPort = lastUsedPort .incrementAndGet ();
626- ServerSocket socket = new ServerSocket (nextPort );
627- socket .close ();
628- return nextPort ;
629- } catch (IOException ex ) {
630- //port is not available, let's try the next one.
631- continue ;
632- }
633- }
634- throw new RuntimeException ("No available port" );
635- }
636-
637- private void configureHddsDatanodes (OzoneConfiguration dnConf ) {
638- dnConf .set (ScmConfigKeys .HDDS_REST_HTTP_ADDRESS_KEY ,
639- "0.0.0.0:" + findPort ());
640- dnConf .set (HddsConfigKeys .HDDS_DATANODE_HTTP_ADDRESS_KEY ,
641- "0.0.0.0:" + findPort ());
642- dnConf .set (HDDS_DATANODE_PLUGINS_KEY ,
605+ private void configureHddsDatanodes () {
606+ conf .set (ScmConfigKeys .HDDS_REST_HTTP_ADDRESS_KEY , "0.0.0.0:0" );
607+ conf .set (HddsConfigKeys .HDDS_DATANODE_HTTP_ADDRESS_KEY , "0.0.0.0:0" );
608+ conf .set (HDDS_DATANODE_PLUGINS_KEY ,
643609 "org.apache.hadoop.ozone.web.OzoneHddsDatanodeService" );
644- dnConf .setBoolean (OzoneConfigKeys .DFS_CONTAINER_IPC_RANDOM_PORT ,
610+ conf .setBoolean (OzoneConfigKeys .DFS_CONTAINER_IPC_RANDOM_PORT ,
645611 randomContainerPort );
646- dnConf .setBoolean (OzoneConfigKeys .DFS_CONTAINER_RATIS_IPC_RANDOM_PORT ,
612+ conf .setBoolean (OzoneConfigKeys .DFS_CONTAINER_RATIS_IPC_RANDOM_PORT ,
647613 randomContainerPort );
648614 }
649615
0 commit comments