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 @@ protected void recordOutput(final StrBuilder result,
* 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 @@ protected int parseTopNodes(final CommandLine cmd, final StrBuilder result) {
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());

View File

@ -100,7 +100,7 @@ public void execute(CommandLine cmd) throws Exception {
}
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) */

View File

@ -244,6 +244,15 @@ public void testReportSimple() throws Exception {
}
/* 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 {