1818package org .apache .hadoop .ozone ;
1919
2020import java .io .File ;
21+ import java .net .ServerSocket ;
2122import java .util .ArrayList ;
2223import java .util .List ;
2324import java .util .Optional ;
6364import java .util .UUID ;
6465import java .util .concurrent .TimeUnit ;
6566import java .util .concurrent .TimeoutException ;
67+ import java .util .concurrent .atomic .AtomicInteger ;
6668
6769import static org .apache .hadoop .hdds .HddsConfigKeys .HDDS_HEARTBEAT_INTERVAL ;
6870import static org .apache .hadoop .hdds .protocol .proto .HddsProtos .NodeState
@@ -388,6 +390,9 @@ private void setCAClient(CertificateClient client) {
388390 */
389391 public static class Builder extends MiniOzoneCluster .Builder {
390392
393+ private static AtomicInteger lastUsedPort =
394+ new AtomicInteger (1000 );
395+
391396 /**
392397 * Creates a new Builder.
393398 *
@@ -529,14 +534,16 @@ OzoneManager createOM()
529534 */
530535 List <HddsDatanodeService > createHddsDatanodes (
531536 StorageContainerManager scm ) throws IOException {
532- configureHddsDatanodes ();
533- String scmAddress = scm .getDatanodeRpcAddress ().getHostString () +
537+
538+ String scmAddress = scm .getDatanodeRpcAddress ().getHostString () +
534539 ":" + scm .getDatanodeRpcAddress ().getPort ();
535540 String [] args = new String [] {};
536541 conf .setStrings (ScmConfigKeys .OZONE_SCM_NAMES , scmAddress );
542+
537543 List <HddsDatanodeService > hddsDatanodes = new ArrayList <>();
538544 for (int i = 0 ; i < numOfDatanodes ; i ++) {
539545 OzoneConfiguration dnConf = new OzoneConfiguration (conf );
546+ configureHddsDatanodes (dnConf );
540547 String datanodeBaseDir = path + "/datanode-" + Integer .toString (i );
541548 Path metaDir = Paths .get (datanodeBaseDir , "meta" );
542549 Path dataDir = Paths .get (datanodeBaseDir , "data" , "containers" );
@@ -563,10 +570,14 @@ List<HddsDatanodeService> createHddsDatanodes(
563570 }
564571
565572 private void configureSCM () {
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" );
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 ());
570581 conf .setInt (ScmConfigKeys .OZONE_SCM_HANDLER_COUNT_KEY , numOfScmHandlers );
571582 configureSCMheartbeat ();
572583 }
@@ -597,19 +608,42 @@ private void configureSCMheartbeat() {
597608
598609
599610 private void configureOM () {
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" );
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 ());
602614 conf .setInt (OMConfigKeys .OZONE_OM_HANDLER_COUNT_KEY , numOfOmHandlers );
603615 }
604616
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 ,
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 ,
609643 "org.apache.hadoop.ozone.web.OzoneHddsDatanodeService" );
610- conf .setBoolean (OzoneConfigKeys .DFS_CONTAINER_IPC_RANDOM_PORT ,
644+ dnConf .setBoolean (OzoneConfigKeys .DFS_CONTAINER_IPC_RANDOM_PORT ,
611645 randomContainerPort );
612- conf .setBoolean (OzoneConfigKeys .DFS_CONTAINER_RATIS_IPC_RANDOM_PORT ,
646+ dnConf .setBoolean (OzoneConfigKeys .DFS_CONTAINER_RATIS_IPC_RANDOM_PORT ,
613647 randomContainerPort );
614648 }
615649
0 commit comments