From 07295260b10cd2376660a673ceb9d8ce0570b528 Mon Sep 17 00:00:00 2001 From: Uma Maheswara Rao G Date: Sun, 8 Jul 2012 18:39:09 +0000 Subject: [PATCH] HDFS-3482. hdfs balancer throws ArrayIndexOutOfBoundsException if option is specified without values. Contributed by Madhukara Phatak. Submitted by: Madhukara Phatak. Reviewed by: Uma Maheswara Rao G. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1358812 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 3 ++ .../hadoop/hdfs/server/balancer/Balancer.java | 2 ++ .../hdfs/server/balancer/TestBalancer.java | 33 +++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index 94f6d9a71f..4b47924caf 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -177,6 +177,9 @@ Trunk (unreleased changes) HDFS-3541. Deadlock between recovery, xceiver and packet responder (Vinay via umamahesh) + HDFS-3482. hdfs balancer throws ArrayIndexOutOfBoundsException + if option is specified without values. ( Madhukara Phatak via umamahesh) + Branch-2 ( Unreleased changes ) INCOMPATIBLE CHANGES diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/balancer/Balancer.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/balancer/Balancer.java index 0576f895c3..e38320e7b0 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/balancer/Balancer.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/balancer/Balancer.java @@ -74,6 +74,7 @@ import org.apache.hadoop.util.StringUtils; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; +import static com.google.common.base.Preconditions.checkArgument; /**

The balancer is a tool that balances disk space usage on an HDFS cluster * when some datanodes become full or when new empty nodes join the cluster. @@ -1501,6 +1502,7 @@ static Parameters parse(String[] args) { if (args != null) { try { for(int i = 0; i < args.length; i++) { + checkArgument(args.length >= 2, "args = " + Arrays.toString(args)); if ("-threshold".equalsIgnoreCase(args[i])) { i++; try { diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/balancer/TestBalancer.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/balancer/TestBalancer.java index 11dbfc89e0..8f1077e73e 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/balancer/TestBalancer.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/balancer/TestBalancer.java @@ -453,6 +453,39 @@ private void testBalancerDefaultConstructor(Configuration conf, } } + /** + * Test parse method in Balancer#Cli class with wrong number of params + */ + + @Test + public void testBalancerCliParseWithWrongParams() { + String parameters[] = new String[] { "-threshold" }; + String reason = + "IllegalArgumentException is expected when value is not specified"; + try { + Balancer.Cli.parse(parameters); + fail(reason); + } catch (IllegalArgumentException e) { + + } + parameters = new String[] { "-policy" }; + try { + Balancer.Cli.parse(parameters); + fail(reason); + } catch (IllegalArgumentException e) { + + } + parameters = new String[] { "-threshold 1 -policy" }; + try { + Balancer.Cli.parse(parameters); + fail(reason); + } catch (IllegalArgumentException e) { + + } + + } + + /** * @param args */