hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ecl...@apache.org
Subject svn commit: r1430560 - in /hbase/trunk/hbase-server/src: main/java/org/apache/hadoop/hbase/HDFSBlocksDistribution.java test/java/org/apache/hadoop/hbase/TestHDFSBlocksDistribution.java
Date Tue, 08 Jan 2013 21:50:45 GMT
Author: eclark
Date: Tue Jan  8 21:50:38 2013
New Revision: 1430560

URL: http://svn.apache.org/viewvc?rev=1430560&view=rev
Log:
HBASE-7513 HDFSBlocksDistribution shouldn't send NPEs when something goes wrong

Added:
    hbase/trunk/hbase-server/src/test/java/org/apache/hadoop/hbase/TestHDFSBlocksDistribution.java
Modified:
    hbase/trunk/hbase-server/src/main/java/org/apache/hadoop/hbase/HDFSBlocksDistribution.java

Modified: hbase/trunk/hbase-server/src/main/java/org/apache/hadoop/hbase/HDFSBlocksDistribution.java
URL: http://svn.apache.org/viewvc/hbase/trunk/hbase-server/src/main/java/org/apache/hadoop/hbase/HDFSBlocksDistribution.java?rev=1430560&r1=1430559&r2=1430560&view=diff
==============================================================================
--- hbase/trunk/hbase-server/src/main/java/org/apache/hadoop/hbase/HDFSBlocksDistribution.java
(original)
+++ hbase/trunk/hbase-server/src/main/java/org/apache/hadoop/hbase/HDFSBlocksDistribution.java
Tue Jan  8 21:50:38 2013
@@ -30,7 +30,9 @@ import org.apache.hadoop.classification.
 
 
 /**
- * Data structure to describe the distribution of HDFS blocks amount hosts
+ * Data structure to describe the distribution of HDFS blocks amount hosts.
+ *
+ * Adding erroneous data will be ignored silently.
  */
 @InterfaceAudience.Private
 public class HDFSBlocksDistribution {
@@ -122,8 +124,10 @@ public class HDFSBlocksDistribution {
    */
   public void addHostsAndBlockWeight(String[] hosts, long weight) {
     if (hosts == null || hosts.length == 0) {
-      throw new NullPointerException("empty hosts");
+      // erroneous data
+      return;
     }
+
     addUniqueWeight(weight);
     for (String hostname : hosts) {
       addHostAndBlockWeight(hostname, weight);
@@ -146,7 +150,8 @@ public class HDFSBlocksDistribution {
    */
   private void addHostAndBlockWeight(String host, long weight) {
     if (host == null) {
-      throw new NullPointerException("Passed hostname is null");
+      // erroneous data
+      return;
     }
 
     HostAndWeight hostAndWeight = this.hostAndWeights.get(host);

Added: hbase/trunk/hbase-server/src/test/java/org/apache/hadoop/hbase/TestHDFSBlocksDistribution.java
URL: http://svn.apache.org/viewvc/hbase/trunk/hbase-server/src/test/java/org/apache/hadoop/hbase/TestHDFSBlocksDistribution.java?rev=1430560&view=auto
==============================================================================
--- hbase/trunk/hbase-server/src/test/java/org/apache/hadoop/hbase/TestHDFSBlocksDistribution.java
(added)
+++ hbase/trunk/hbase-server/src/test/java/org/apache/hadoop/hbase/TestHDFSBlocksDistribution.java
Tue Jan  8 21:50:38 2013
@@ -0,0 +1,69 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase;
+
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static junit.framework.Assert.assertEquals;
+
+@Category(SmallTests.class)
+public class TestHDFSBlocksDistribution {
+  @Test
+  public void testAddHostsAndBlockWeight() throws Exception {
+    HDFSBlocksDistribution distribution = new HDFSBlocksDistribution();
+    distribution.addHostsAndBlockWeight(null, 100);
+    assertEquals("Expecting no hosts weights", 0, distribution.getHostAndWeights().size());
+    distribution.addHostsAndBlockWeight(new String[0], 100);
+    assertEquals("Expecting no hosts weights", 0, distribution.getHostAndWeights().size());
+    distribution.addHostsAndBlockWeight(new String[] {"test"}, 101);
+    assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
+    distribution.addHostsAndBlockWeight(new String[] {"test"}, 202);
+    assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
+    assertEquals("test host should have weight 303", 303,
+        distribution.getHostAndWeights().get("test").getWeight());
+    distribution.addHostsAndBlockWeight(new String[] {"testTwo"}, 222);
+    assertEquals("Should be two hosts", 2, distribution.getHostAndWeights().size());
+    assertEquals("Total weight should be 525", 525, distribution.getUniqueBlocksTotalWeight());
+  }
+
+  public class MockHDFSBlocksDistribution extends HDFSBlocksDistribution {
+    public Map<String,HostAndWeight> getHostAndWeights() {
+      HashMap<String, HostAndWeight> map = new HashMap<String, HostAndWeight>();
+      map.put("test", new HostAndWeight(null, 100));
+      return map;
+    }
+
+  }
+
+  @Test
+  public void testAdd() throws Exception {
+    HDFSBlocksDistribution distribution = new HDFSBlocksDistribution();
+    distribution.add(new MockHDFSBlocksDistribution());
+    assertEquals("Expecting no hosts weights", 0, distribution.getHostAndWeights().size());
+    distribution.addHostsAndBlockWeight(new String[]{"test"}, 10);
+    assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
+    distribution.add(new MockHDFSBlocksDistribution());
+    assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
+    assertEquals("Total weight should be 10", 10, distribution.getUniqueBlocksTotalWeight());
+  }
+}



Mime
View raw message