This is an automated email from the ASF dual-hosted git repository.

adoroszlai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 07bb734e76 HDDS-8041. Let Ozone Client fail faster with wrong OM 
address in URI. (#4325)
07bb734e76 is described below

commit 07bb734e7696141c90e43a2287e6c46c28603fb7
Author: Sadanand Shenoy <[email protected]>
AuthorDate: Thu Mar 16 14:05:15 2023 +0530

    HDDS-8041. Let Ozone Client fail faster with wrong OM address in URI. 
(#4325)
---
 .../hadoop/ozone/client/OzoneClientFactory.java    |  1 +
 .../main/java/org/apache/hadoop/ozone/OmUtils.java | 27 +++++++++++
 .../apache/hadoop/fs/ozone/TestOzoneFsHAURLs.java  | 54 ++++++++++++++++++++++
 3 files changed, 82 insertions(+)

diff --git 
a/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/OzoneClientFactory.java
 
b/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/OzoneClientFactory.java
index 81215a0c3c..44239aafce 100644
--- 
a/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/OzoneClientFactory.java
+++ 
b/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/OzoneClientFactory.java
@@ -89,6 +89,7 @@ public final class OzoneClientFactory {
     Preconditions.checkNotNull(omHost);
     Preconditions.checkNotNull(omRpcPort);
     Preconditions.checkNotNull(config);
+    OmUtils.resolveOmHost(omHost, omRpcPort);
     config.set(OZONE_OM_ADDRESS_KEY, omHost + ":" + omRpcPort);
     return getRpcClient(getClientProtocol(config), config);
   }
diff --git 
a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/OmUtils.java 
b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/OmUtils.java
index 249bc48e84..932f017814 100644
--- a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/OmUtils.java
+++ b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/OmUtils.java
@@ -22,6 +22,7 @@ import com.google.common.base.Preconditions;
 import java.io.File;
 import java.io.IOException;
 import java.net.InetSocketAddress;
+import java.net.UnknownHostException;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
@@ -791,4 +792,30 @@ public final class OmUtils {
       return sb.toString();
     }
   }
+
+  /**
+   * @param omHost
+   * @param omPort
+   * If the authority in the URI is not one of the service ID's,
+   * it is treated as a hostname. Check if this hostname can be resolved
+   * and if it's reachable.
+   */
+  public static void resolveOmHost(String omHost, int omPort)
+      throws IOException {
+    InetSocketAddress omHostAddress = NetUtils.createSocketAddr(omHost, 
omPort);
+    if (omHostAddress.isUnresolved()) {
+      throw new IOException(
+          "Cannot resolve OM host " + omHost + " in the URI",
+          new UnknownHostException());
+    }
+    try {
+      if (!omHostAddress.getAddress().isReachable(5000)) {
+        throw new IOException(
+            "OM host " + omHost + " unreachable in the URI");
+      }
+    } catch (IOException e) {
+      LOG.error("Failure in resolving OM host address", e);
+      throw e;
+    }
+  }
 }
diff --git 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/TestOzoneFsHAURLs.java
 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/TestOzoneFsHAURLs.java
index 41539625dd..eccb8386ed 100644
--- 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/TestOzoneFsHAURLs.java
+++ 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/TestOzoneFsHAURLs.java
@@ -88,6 +88,13 @@ public class TestOzoneFsHAURLs {
   private final String o3fsImplValue =
       "org.apache.hadoop.fs.ozone.OzoneFileSystem";
 
+  private final String ofsImplKey =
+      "fs." + OzoneConsts.OZONE_OFS_URI_SCHEME + ".impl";
+
+  private final String ofsImplValue =
+      "org.apache.hadoop.fs.ozone.RootedOzoneFileSystem";
+
+
   @BeforeClass
   public static void initClass() throws Exception {
     OzoneConfiguration conf = new OzoneConfiguration();
@@ -350,4 +357,51 @@ public class TestOzoneFsHAURLs {
         OzoneConsts.OZONE_URI_SCHEME, bucketName, volumeName);
     testWithDefaultFS(unqualifiedFs2);
   }
+
+  @Test
+  public void testIncorrectAuthorityInURI() throws Exception {
+    OzoneConfiguration clientConf = new OzoneConfiguration(conf);
+    clientConf.setQuietMode(false);
+    clientConf.set(o3fsImplKey, o3fsImplValue);
+    clientConf.set(ofsImplKey, ofsImplValue);
+    FsShell shell = new FsShell(clientConf);
+    String incorrectSvcId = "dummy";
+    String o3fsPathWithCorrectSvcId =
+        String.format("%s://%s.%s.%s/", OzoneConsts.OZONE_URI_SCHEME,
+            bucketName, volumeName, omServiceId);
+    String o3fsPathWithInCorrectSvcId =
+        String.format("%s://%s.%s.%s/", OzoneConsts.OZONE_URI_SCHEME,
+            bucketName, volumeName, incorrectSvcId);
+    String ofsPathWithCorrectSvcId = "ofs://" + omServiceId + "/";
+    String ofsPathWithIncorrectSvcId = "ofs://" + incorrectSvcId + "/";
+    try {
+      int res = ToolRunner.run(shell,
+          new String[] {"-ls", ofsPathWithCorrectSvcId });
+      Assert.assertEquals(0, res);
+      res = ToolRunner.run(shell,
+          new String[] {"-ls", o3fsPathWithCorrectSvcId });
+      Assert.assertEquals(0, res);
+
+      try (GenericTestUtils.SystemErrCapturer capture = new
+          GenericTestUtils.SystemErrCapturer()) {
+        res = ToolRunner.run(shell,
+            new String[] {"-ls", ofsPathWithIncorrectSvcId });
+        Assert.assertEquals(1, res);
+        Assert.assertTrue(
+            capture.getOutput().contains("Cannot resolve OM host"));
+      }
+
+      try (GenericTestUtils.SystemErrCapturer capture = new
+          GenericTestUtils.SystemErrCapturer()) {
+        res = ToolRunner.run(shell,
+            new String[] {"-ls", o3fsPathWithInCorrectSvcId });
+        Assert.assertEquals(1, res);
+        Assert.assertTrue(
+            capture.getOutput().contains("Cannot resolve OM host"));
+      }
+    } finally {
+      shell.close();
+    }
+
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to