diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/AdminHelper.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/AdminHelper.java index 153fb36a107..8bab5505ef5 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/AdminHelper.java +++ b/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. diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/StoragePolicyAdmin.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/StoragePolicyAdmin.java index 24079b3c25d..ba0aeb0a85c 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/StoragePolicyAdmin.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/StoragePolicyAdmin.java @@ -144,9 +144,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; @@ -157,9 +159,9 @@ public class StoragePolicyAdmin extends Configured implements Tool { return 0; } Collection 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; } } @@ -211,10 +213,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)); @@ -257,9 +259,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)); diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestStoragePolicyCommands.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestStoragePolicyCommands.java index 63f8484d76b..149dabb06f9 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestStoragePolicyCommands.java +++ b/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,