From d3ffd7d26e6bb49b4be4764b1758889dd6e3a532 Mon Sep 17 00:00:00 2001 From: WangYuanben <48795318+YuanbenWang@users.noreply.github.com> Date: Fri, 17 May 2024 15:28:11 +0800 Subject: [PATCH] HDDS-10861. Ozone cli supports default ozone.om.service.id (#6680) --- .../hadoop/ozone/shell/OzoneAddress.java | 37 +++++++++++-------- .../shell/TestOzoneAddressClientCreation.java | 30 +++++++++++++++ 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/shell/OzoneAddress.java b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/shell/OzoneAddress.java index 68a2065f4ae..f2fa1a8c4f3 100644 --- a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/shell/OzoneAddress.java +++ b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/shell/OzoneAddress.java @@ -39,6 +39,7 @@ import static org.apache.hadoop.ozone.OzoneConsts.OZONE_HTTP_SCHEME; import static org.apache.hadoop.ozone.OzoneConsts.OZONE_RPC_SCHEME; import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_ADDRESS_KEY; +import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_INTERNAL_SERVICE_ID; import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_SERVICE_IDS_KEY; import org.apache.http.client.utils.URIBuilder; @@ -157,21 +158,23 @@ public OzoneClient createClient(MutableConfigurationSource conf) ozoneURI.getPort(), conf); } } else { // When host is not specified - - Collection omServiceIds = conf.getTrimmedStringCollection( - OZONE_OM_SERVICE_IDS_KEY); - - if (omServiceIds.size() > 1) { - throw new OzoneClientException("Service ID or host name must not" - + " be omitted when multiple ozone.om.service.ids is defined."); - } else if (omServiceIds.size() == 1) { - client = createRpcClientFromServiceId(omServiceIds.iterator().next(), - conf); + String localOmServiceId = conf.getTrimmed(OZONE_OM_INTERNAL_SERVICE_ID); + if (localOmServiceId == null) { + Collection omServiceIds = conf.getTrimmedStringCollection( + OZONE_OM_SERVICE_IDS_KEY); + if (omServiceIds.size() > 1) { + throw new OzoneClientException("Service ID or host name must not" + + " be omitted when multiple ozone.om.service.ids is defined."); + } else if (omServiceIds.size() == 1) { + client = createRpcClientFromServiceId(omServiceIds.iterator().next(), + conf); + } else { + client = createRpcClient(conf); + } } else { - client = createRpcClient(conf); + client = createRpcClientFromServiceId(localOmServiceId, conf); } } - return client; } @@ -194,7 +197,7 @@ public OzoneClient createClientForS3Commands( if (omServiceID != null) { // OM HA cluster if (OmUtils.isOmHAServiceId(conf, omServiceID)) { - return OzoneClientFactory.getRpcClient(omServiceID, conf); + return createRpcClientFromServiceId(omServiceID, conf); } else { throw new OzoneClientException("Service ID specified does not match" + " with " + OZONE_OM_SERVICE_IDS_KEY + " defined in the " + @@ -202,8 +205,12 @@ public OzoneClient createClientForS3Commands( serviceIds); } } else if (serviceIds.size() > 1) { - // If multiple om service ids are there, + // If multiple om service ids are there and default value isn't set, // throw an error "om service ID must not be omitted" + String localOmServiceId = conf.getTrimmed(OZONE_OM_INTERNAL_SERVICE_ID); + if (!localOmServiceId.isEmpty()) { + return createRpcClientFromServiceId(localOmServiceId, conf); + } throw new OzoneClientException("Service ID must not" + " be omitted when cluster has multiple OM Services." + " Configured " + OZONE_OM_SERVICE_IDS_KEY + " are " @@ -211,7 +218,7 @@ public OzoneClient createClientForS3Commands( } // for non-HA cluster and HA cluster with only 1 service ID // get service ID from configurations - return OzoneClientFactory.getRpcClient(conf); + return createRpcClient(conf); } /** diff --git a/hadoop-ozone/tools/src/test/java/org/apache/hadoop/ozone/shell/TestOzoneAddressClientCreation.java b/hadoop-ozone/tools/src/test/java/org/apache/hadoop/ozone/shell/TestOzoneAddressClientCreation.java index 2457a00fe52..620142c244b 100644 --- a/hadoop-ozone/tools/src/test/java/org/apache/hadoop/ozone/shell/TestOzoneAddressClientCreation.java +++ b/hadoop-ozone/tools/src/test/java/org/apache/hadoop/ozone/shell/TestOzoneAddressClientCreation.java @@ -22,10 +22,12 @@ import org.apache.hadoop.hdds.conf.ConfigurationSource; import org.apache.hadoop.hdds.conf.MutableConfigurationSource; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; import org.apache.hadoop.ozone.client.OzoneClient; import org.apache.hadoop.ozone.client.OzoneClientException; import org.apache.hadoop.hdds.conf.InMemoryConfiguration; +import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_INTERNAL_SERVICE_ID; import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_SERVICE_IDS_KEY; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -68,6 +70,34 @@ public void implicitHaMultipleServiceId() "service1,service2"))); } + @Test + public void implicitHaMultipleServiceIdWithDefaultServiceId() + throws OzoneClientException, IOException { + TestableOzoneAddress address = + new TestableOzoneAddress("/vol1/bucket1/key1"); + InMemoryConfiguration conf = new InMemoryConfiguration(OZONE_OM_SERVICE_IDS_KEY, + "service1,service2"); + conf.set(OZONE_OM_INTERNAL_SERVICE_ID, "service2"); + + address.createClient(conf); + assertFalse(address.simpleCreation); + assertEquals("service2", address.serviceId); + } + + @Test + public void implicitHaMultipleServiceIdWithDefaultServiceIdForS3() + throws OzoneClientException, IOException { + TestableOzoneAddress address = + new TestableOzoneAddress("/vol1/bucket1/key1"); + OzoneConfiguration conf = new OzoneConfiguration(); + conf.set(OZONE_OM_SERVICE_IDS_KEY, "service1,service2"); + conf.set(OZONE_OM_INTERNAL_SERVICE_ID, "service2"); + + address.createClientForS3Commands(conf, null); + assertFalse(address.simpleCreation); + assertEquals("service2", address.serviceId); + } + @Test public void explicitHaMultipleServiceId() throws OzoneClientException, IOException {