Browse Source

HDFS-11177. 'storagepolicies -getStoragePolicy' command should accept URI based path. (Contributed by Surendra Singh Lilhore)

Vinayakumar B 8 years ago
parent
commit
4804050630

+ 12 - 0
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/AdminHelper.java

@@ -18,6 +18,7 @@
 package org.apache.hadoop.hdfs.tools;
 
 import com.google.common.base.Preconditions;
+
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.hdfs.DFSUtil;
@@ -26,6 +27,7 @@ import org.apache.hadoop.hdfs.protocol.CachePoolInfo;
 import org.apache.hadoop.tools.TableListing;
 
 import java.io.IOException;
+import java.net.URI;
 import java.util.List;
 
 /**
@@ -48,6 +50,16 @@ public class AdminHelper {
     return (DistributedFileSystem)fs;
   }
 
+  static DistributedFileSystem getDFS(URI uri, Configuration conf)
+      throws IOException {
+    FileSystem fs = FileSystem.get(uri, conf);
+    if (!(fs instanceof DistributedFileSystem)) {
+      throw new IllegalArgumentException("FileSystem " + fs.getUri()
+          + " is not an HDFS file system");
+    }
+    return (DistributedFileSystem) fs;
+  }
+
   /**
    * NN exceptions contain the stack trace as part of the exception message.
    * When it's a known error, pretty-print the error and squish the stack trace.

+ 13 - 10
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/StoragePolicyAdmin.java

@@ -148,9 +148,11 @@ public class StoragePolicyAdmin extends Configured implements Tool {
         return 1;
       }
 
-      final DistributedFileSystem dfs = AdminHelper.getDFS(conf);
+      Path p = new Path(path);
+      final DistributedFileSystem dfs = AdminHelper.getDFS(p.toUri(), conf);
       try {
-        HdfsFileStatus status = dfs.getClient().getFileInfo(path);
+        HdfsFileStatus status = dfs.getClient().getFileInfo(
+            Path.getPathWithoutSchemeAndAuthority(p).toString());
         if (status == null) {
           System.err.println("File/Directory does not exist: " + path);
           return 2;
@@ -161,9 +163,9 @@ public class StoragePolicyAdmin extends Configured implements Tool {
           return 0;
         }
         Collection<BlockStoragePolicy> policies = dfs.getAllStoragePolicies();
-        for (BlockStoragePolicy p : policies) {
-          if (p.getId() == storagePolicyId) {
-            System.out.println("The storage policy of " + path + ":\n" + p);
+        for (BlockStoragePolicy policy : policies) {
+          if (policy.getId() == storagePolicyId) {
+            System.out.println("The storage policy of " + path + ":\n" + policy);
             return 0;
           }
         }
@@ -215,10 +217,10 @@ public class StoragePolicyAdmin extends Configured implements Tool {
             getLongUsage());
         return 1;
       }
-
-      final DistributedFileSystem dfs = AdminHelper.getDFS(conf);
+      Path p = new Path(path);
+      final DistributedFileSystem dfs = AdminHelper.getDFS(p.toUri(), conf);
       try {
-        dfs.setStoragePolicy(new Path(path), policyName);
+        dfs.setStoragePolicy(p, policyName);
         System.out.println("Set storage policy " + policyName + " on " + path);
       } catch (Exception e) {
         System.err.println(AdminHelper.prettifyException(e));
@@ -261,9 +263,10 @@ public class StoragePolicyAdmin extends Configured implements Tool {
         return 1;
       }
 
-      final DistributedFileSystem dfs = AdminHelper.getDFS(conf);
+      Path p = new Path(path);
+      final DistributedFileSystem dfs = AdminHelper.getDFS(p.toUri(), conf);
       try {
-        dfs.unsetStoragePolicy(new Path(path));
+        dfs.unsetStoragePolicy(p);
         System.out.println("Unset storage policy from " + path);
       } catch (Exception e) {
         System.err.println(AdminHelper.prettifyException(e));

+ 8 - 6
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestStoragePolicyCommands.java

@@ -74,8 +74,9 @@ public class TestStoragePolicyCommands {
      * test: set storage policy
      */
     final StoragePolicyAdmin admin = new StoragePolicyAdmin(conf);
-    DFSTestUtil.toolRun(admin, "-setStoragePolicy -path /foo -policy WARM", 0,
-        "Set storage policy WARM on " + foo.toString());
+    DFSTestUtil.toolRun(admin, "-setStoragePolicy -path " + fs.getUri()
+        + "/foo -policy WARM", 0, "Set storage policy WARM on " + fs.getUri()
+        + "/foo");
     DFSTestUtil.toolRun(admin, "-setStoragePolicy -path /foo/bar -policy COLD",
         0, "Set storage policy COLD on " + bar.toString());
     DFSTestUtil.toolRun(admin, "-setStoragePolicy -path /foo/bar/wow -policy HOT",
@@ -91,8 +92,9 @@ public class TestStoragePolicyCommands {
     final BlockStoragePolicy warm = suite.getPolicy("WARM");
     final BlockStoragePolicy cold = suite.getPolicy("COLD");
     final BlockStoragePolicy hot = suite.getPolicy("HOT");
-    DFSTestUtil.toolRun(admin, "-getStoragePolicy -path /foo", 0,
-        "The storage policy of " + foo.toString() + ":\n" + warm);
+    DFSTestUtil.toolRun(admin, "-getStoragePolicy -path " + fs.getUri()
+        + "/foo", 0, "The storage policy of " + fs.getUri() + "/foo:\n"
+        + warm);
     DFSTestUtil.toolRun(admin, "-getStoragePolicy -path /foo/bar", 0,
         "The storage policy of " + bar.toString() + ":\n" + cold);
     DFSTestUtil.toolRun(admin, "-getStoragePolicy -path /foo/bar/wow", 0,
@@ -103,8 +105,8 @@ public class TestStoragePolicyCommands {
     /*
      * test: unset storage policy
      */
-    DFSTestUtil.toolRun(admin, "-unsetStoragePolicy -path /foo", 0,
-        "Unset storage policy from " + foo.toString());
+    DFSTestUtil.toolRun(admin, "-unsetStoragePolicy -path " + fs.getUri()
+        + "/foo", 0, "Unset storage policy from " + fs.getUri() + "/foo");
     DFSTestUtil.toolRun(admin, "-unsetStoragePolicy -path /foo/bar", 0,
         "Unset storage policy from " + bar.toString());
     DFSTestUtil.toolRun(admin, "-unsetStoragePolicy -path /foo/bar/wow", 0,