diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/diskbalancer/command/Command.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/diskbalancer/command/Command.java index eeb7241cfbb..8eacdecf7b6 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/diskbalancer/command/Command.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/diskbalancer/command/Command.java @@ -501,7 +501,8 @@ public abstract class Command extends Configured implements Closeable { * Parse top number of nodes to be processed. * @return top number of nodes to be processed. */ - protected int parseTopNodes(final CommandLine cmd, final StrBuilder result) { + protected int parseTopNodes(final CommandLine cmd, final StrBuilder result) + throws IllegalArgumentException { String outputLine = ""; int nodes = 0; final String topVal = cmd.getOptionValue(DiskBalancerCLI.TOP); @@ -523,6 +524,10 @@ public abstract class Command extends Configured implements Closeable { result.appendln(outputLine); nodes = getDefaultTop(); } + if (nodes <= 0) { + throw new IllegalArgumentException( + "Top limit input should be a positive numeric value"); + } } return Math.min(nodes, cluster.getNodes().size()); diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/diskbalancer/command/ReportCommand.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/diskbalancer/command/ReportCommand.java index b224b11d47c..58ef5ce51ad 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/diskbalancer/command/ReportCommand.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/diskbalancer/command/ReportCommand.java @@ -100,7 +100,7 @@ public class ReportCommand extends Command { } private void handleTopReport(final CommandLine cmd, final StrBuilder result, - final String nodeFormat) { + final String nodeFormat) throws IllegalArgumentException { Collections.sort(getCluster().getNodes(), Collections.reverseOrder()); /* extract value that identifies top X DataNode(s) */ diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/diskbalancer/command/TestDiskBalancerCommand.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/diskbalancer/command/TestDiskBalancerCommand.java index 1cebae0d546..6fde209b2a4 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/diskbalancer/command/TestDiskBalancerCommand.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/diskbalancer/command/TestDiskBalancerCommand.java @@ -244,6 +244,15 @@ public class TestDiskBalancerCommand { } + /* test basic report with negative top limit */ + @Test(timeout = 60000) + public void testReportWithNegativeTopLimit() + throws Exception { + final String cmdLine = "hdfs diskbalancer -report -top -32"; + thrown.expect(java.lang.IllegalArgumentException.class); + thrown.expectMessage("Top limit input should be a positive numeric value"); + runCommand(cmdLine); + } /* test less than 64 DataNode(s) as total, e.g., -report -top 32 */ @Test(timeout = 60000) public void testReportLessThanTotal() throws Exception {