diff --git a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/OzoneClientProducer.java b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/OzoneClientProducer.java index 1f6e52d9832f..9b186e8b9ba2 100644 --- a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/OzoneClientProducer.java +++ b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/OzoneClientProducer.java @@ -28,6 +28,7 @@ import com.google.common.annotations.VisibleForTesting; import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.ozone.OmUtils; import org.apache.hadoop.ozone.client.OzoneClient; import org.apache.hadoop.ozone.client.OzoneClientFactory; import org.apache.hadoop.ozone.om.protocol.S3Auth; @@ -62,9 +63,6 @@ public class OzoneClientProducer { @Inject private OzoneConfiguration ozoneConfiguration; - @Inject - private String omServiceID; - @Context private ContainerRequestContext context; @@ -115,6 +113,7 @@ OzoneClient createOzoneClient() throws IOException { // Set the expected OM version if not set via config. ozoneConfiguration.setIfUnset(OZONE_OM_CLIENT_PROTOCOL_VERSION_KEY, OZONE_OM_CLIENT_PROTOCOL_VERSION); + String omServiceID = OmUtils.getOzoneManagerServiceId(ozoneConfiguration); if (omServiceID == null) { return OzoneClientFactory.getRpcClient(ozoneConfiguration); } else { diff --git a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/OzoneServiceProvider.java b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/OzoneServiceProvider.java deleted file mode 100644 index 351163c0449e..000000000000 --- a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/OzoneServiceProvider.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.ozone.s3; - -import org.apache.hadoop.hdds.conf.OzoneConfiguration; -import org.apache.hadoop.ozone.OmUtils; - -import javax.annotation.PostConstruct; -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.Produces; -import javax.inject.Inject; - -import java.util.Arrays; -import java.util.Collection; - -import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_NODES_KEY; -import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_SERVICE_IDS_KEY; - -/** - * This class creates the OM service . - */ -@ApplicationScoped -public class OzoneServiceProvider { - - private String omServiceID; - - @Inject - private OzoneConfiguration conf; - - @PostConstruct - public void init() { - Collection serviceIdList = - conf.getTrimmedStringCollection(OZONE_OM_SERVICE_IDS_KEY); - if (!serviceIdList.isEmpty()) { - // HA cluster. - //For now if multiple service id's are configured we throw exception. - // As if multiple service id's are configured, S3Gateway will not be - // knowing which one to talk to. In future, if OM federation is supported - // we can resolve this by having another property like - // ozone.om.internal.service.id. - // TODO: Revisit this later. - if (serviceIdList.size() > 1) { - throw new IllegalArgumentException("Multiple serviceIds are " + - "configured. " + Arrays.toString(serviceIdList.toArray())); - } else { - String serviceId = serviceIdList.iterator().next(); - Collection omNodeIds = OmUtils.getActiveOMNodeIds(conf, - serviceId); - if (omNodeIds.size() == 0) { - throw new IllegalArgumentException(OZONE_OM_NODES_KEY - + "." + serviceId + " is not defined"); - } - omServiceID = serviceId; - } - } - } - - @Produces - public String getOmServiceID() { - return omServiceID; - } - -} diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestOzoneClientProducer.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestOzoneClientProducer.java index e7e3371153e4..83305f029461 100644 --- a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestOzoneClientProducer.java +++ b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestOzoneClientProducer.java @@ -132,6 +132,39 @@ public void testGetClientFailure() { } } + @Test + public void testGetClientFailureWithMultipleServiceIds() { + try { + OzoneConfiguration configuration = new OzoneConfiguration(); + configuration.set(OMConfigKeys.OZONE_OM_SERVICE_IDS_KEY, "ozone1,ozone2"); + producer.setOzoneConfiguration(configuration); + producer.createClient(); + fail("testGetClientFailureWithMultipleServiceIds"); + } catch (Exception ex) { + Assert.assertTrue(ex instanceof IOException); + Assert.assertTrue(ex.getMessage().contains( + "More than 1 OzoneManager ServiceID")); + } + } + + @Test + public void testGetClientFailureWithMultipleServiceIdsAndInternalServiceId() { + try { + OzoneConfiguration configuration = new OzoneConfiguration(); + configuration.set(OMConfigKeys.OZONE_OM_INTERNAL_SERVICE_ID, "ozone1"); + configuration.set(OMConfigKeys.OZONE_OM_SERVICE_IDS_KEY, "ozone1,ozone2"); + producer.setOzoneConfiguration(configuration); + producer.createClient(); + fail("testGetClientFailureWithMultipleServiceIdsAndInternalServiceId"); + } catch (Exception ex) { + Assert.assertTrue(ex instanceof IOException); + // Still test will fail, as config is not complete. But it should pass + // the service id check. + Assert.assertFalse(ex.getMessage().contains( + "More than 1 OzoneManager ServiceID")); + } + } + private void setupContext() throws Exception { headerMap.putSingle(AUTHORIZATION_HEADER, authHeader); headerMap.putSingle(CONTENT_MD5, contentMd5);