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

This commit is contained in:
Vinayakumar B 2017-02-21 18:13:19 +05:30
parent 6ba61d20d3
commit 4804050630
3 changed files with 33 additions and 16 deletions

View File

@ -18,6 +18,7 @@
package org.apache.hadoop.hdfs.tools; package org.apache.hadoop.hdfs.tools;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.hdfs.DFSUtil; import org.apache.hadoop.hdfs.DFSUtil;
@ -26,6 +27,7 @@
import org.apache.hadoop.tools.TableListing; import org.apache.hadoop.tools.TableListing;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.util.List; import java.util.List;
/** /**
@ -48,6 +50,16 @@ static DistributedFileSystem getDFS(Configuration conf)
return (DistributedFileSystem)fs; 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. * 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. * When it's a known error, pretty-print the error and squish the stack trace.

View File

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

View File

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