diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSClient.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSClient.java index 72b2113943756..5da1512eb3e94 100755 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSClient.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSClient.java @@ -2553,16 +2553,17 @@ void setQuota(String src, long namespaceQuota, long storagespaceQuota) throws IOException { checkOpen(); // sanity check - if ((namespaceQuota <= 0 && - namespaceQuota != HdfsConstants.QUOTA_DONT_SET && - namespaceQuota != HdfsConstants.QUOTA_RESET) || - (storagespaceQuota < 0 && + if (namespaceQuota <= 0 && + namespaceQuota != HdfsConstants.QUOTA_DONT_SET && + namespaceQuota != HdfsConstants.QUOTA_RESET){ + throw new IllegalArgumentException("Invalid values for " + + "namespace quota : " + namespaceQuota); + } + if (storagespaceQuota < 0 && storagespaceQuota != HdfsConstants.QUOTA_DONT_SET && - storagespaceQuota != HdfsConstants.QUOTA_RESET)) { - throw new IllegalArgumentException("Invalid values for quota : " + - namespaceQuota + " and " + - storagespaceQuota); - + storagespaceQuota != HdfsConstants.QUOTA_RESET) { + throw new IllegalArgumentException("Invalid values for " + + "storagespace quota : " + storagespaceQuota); } try (TraceScope ignored = newPathTraceScope("setQuota", src)) { // Pass null as storage type for traditional namespace/storagespace quota. diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DFSAdmin.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DFSAdmin.java index 04960e3c3e2ce..15d9d4cf8d423 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DFSAdmin.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DFSAdmin.java @@ -201,8 +201,19 @@ private static class SetQuotaCommand extends DFSAdminCommand { super(conf); CommandFormat c = new CommandFormat(2, Integer.MAX_VALUE); List parameters = c.parse(args, pos); - this.quota = - StringUtils.TraditionalBinaryPrefix.string2long(parameters.remove(0)); + String str = parameters.get(0).trim(); + try { + this.quota = StringUtils.TraditionalBinaryPrefix + .string2long(parameters.remove(0)); + } catch(NumberFormatException e){ + throw new IllegalArgumentException("\"" + str + + "\" is not a valid value for a quota."); + } + if (HdfsConstants.QUOTA_DONT_SET == this.quota) { + throw new IllegalArgumentException("WARN: \"" + this.quota + + "\" means QUOTA_DONT_SET, quota will not be set, " + + "it keep the old values."); + } this.args = parameters.toArray(new String[parameters.size()]); } @@ -323,6 +334,11 @@ private static class SetSpaceQuotaCommand extends DFSAdminCommand { } catch (NumberFormatException nfe) { throw new IllegalArgumentException("\"" + str + "\" is not a valid value for a quota."); } + if (HdfsConstants.QUOTA_DONT_SET == quota) { + throw new IllegalArgumentException("WARN: \"" + this.quota + + "\" means QUOTA_DONT_SET, quota will not be set, " + + "it keep the old values."); + } String storageTypeString = StringUtils.popOptionWithArgument("-storageType", parameters); if (storageTypeString != null) { diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestQuota.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestQuota.java index 7754b53653cf4..65ec8b3e189b4 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestQuota.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestQuota.java @@ -1216,6 +1216,81 @@ private void compareQuotaUsage(final QuotaUsage fromContentSummary, assertEquals(fromContentSummary, quotaUsage); } + /** + * Test to set quote with warn msg. + */ + @Test(timeout = 30000) + public void testSetQuotaWarningMsg() throws Exception { + + final DFSAdmin dfsAdmin = new DFSAdmin(conf); + final Path dir = new Path( + PathUtils.getTestDir(getClass()).getPath(), + GenericTestUtils.getMethodName()); + assertTrue(dfs.mkdirs(dir)); + + final List outs = Lists.newArrayList(); + + /* set quota HdfsConstants.QUOTA_DONT_SET */ + resetStream(); + outs.clear(); + final int ret = ToolRunner.run( + dfsAdmin, + new String[] {"-setQuota", + String.valueOf(HdfsConstants.QUOTA_DONT_SET), + dir.toString()}); + assertEquals(-1, ret); + scanIntoList(ERR_STREAM, outs); + assertEquals(2, outs.size()); + assertThat(outs.get(0), + is(allOf(containsString("WARN:"), + containsString("means QUOTA_DONT_SET, quota will " + + "not be set, it keep the old values.")))); + + final List outs1 = Lists.newArrayList(); + /* set quota 0 */ + resetStream(); + outs1.clear(); + final int ret1 = ToolRunner.run( + dfsAdmin, + new String[] {"-setQuota", "0", dir.toString()}); + assertEquals(-1, ret1); + scanIntoList(ERR_STREAM, outs1); + assertEquals(2, outs1.size()); + assertThat(outs1.get(0), + is(allOf(containsString("setQuota"), + containsString("Invalid values for namespace quota")))); + } + + /** + * Test to set quote with warn msg. + */ + @Test(timeout = 30000) + public void testSetSpaceQuotaWarningMsg() throws Exception { + + final DFSAdmin dfsAdmin = new DFSAdmin(conf); + final Path dir = new Path( + PathUtils.getTestDir(getClass()).getPath(), + GenericTestUtils.getMethodName()); + assertTrue(dfs.mkdirs(dir)); + + final List outs = Lists.newArrayList(); + + /* set quota HdfsConstants.QUOTA_DONT_SET */ + resetStream(); + outs.clear(); + final int ret = ToolRunner.run( + dfsAdmin, + new String[] {"-setSpaceQuota", + String.valueOf(HdfsConstants.QUOTA_DONT_SET), + dir.toString()}); + assertEquals(-1, ret); + scanIntoList(ERR_STREAM, outs); + assertEquals(2, outs.size()); + assertThat(outs.get(0), + is(allOf(containsString("WARN:"), + containsString("means QUOTA_DONT_SET, quota will " + + "not be set, it keep the old values.")))); + } /** * Test to set space quote using negative number.