HDFS-12948. DiskBalancer report command top option should only take positive numeric values. Contributed by Shashikant Banerjee.

This commit is contained in:
Yiqun Lin 2018-01-04 10:48:44 +08:00
parent 7a55044803
commit 2a48b3594c
3 changed files with 16 additions and 2 deletions

View File

@ -501,7 +501,8 @@ public abstract class Command extends Configured implements Closeable {
* Parse top number of nodes to be processed. * Parse top number of nodes to be processed.
* @return 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 = ""; String outputLine = "";
int nodes = 0; int nodes = 0;
final String topVal = cmd.getOptionValue(DiskBalancerCLI.TOP); final String topVal = cmd.getOptionValue(DiskBalancerCLI.TOP);
@ -523,6 +524,10 @@ public abstract class Command extends Configured implements Closeable {
result.appendln(outputLine); result.appendln(outputLine);
nodes = getDefaultTop(); nodes = getDefaultTop();
} }
if (nodes <= 0) {
throw new IllegalArgumentException(
"Top limit input should be a positive numeric value");
}
} }
return Math.min(nodes, cluster.getNodes().size()); return Math.min(nodes, cluster.getNodes().size());

View File

@ -100,7 +100,7 @@ public class ReportCommand extends Command {
} }
private void handleTopReport(final CommandLine cmd, final StrBuilder result, private void handleTopReport(final CommandLine cmd, final StrBuilder result,
final String nodeFormat) { final String nodeFormat) throws IllegalArgumentException {
Collections.sort(getCluster().getNodes(), Collections.reverseOrder()); Collections.sort(getCluster().getNodes(), Collections.reverseOrder());
/* extract value that identifies top X DataNode(s) */ /* extract value that identifies top X DataNode(s) */

View File

@ -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 less than 64 DataNode(s) as total, e.g., -report -top 32 */
@Test(timeout = 60000) @Test(timeout = 60000)
public void testReportLessThanTotal() throws Exception { public void testReportLessThanTotal() throws Exception {