CRUNCH-695: Fix NullPointerException in RegionLocationTable (#32)
Co-authored-by: Andrew Olson <aolson1@cerner.com>
diff --git a/crunch-hbase/src/it/java/org/apache/crunch/io/hbase/RegionLocationTableTest.java b/crunch-hbase/src/it/java/org/apache/crunch/io/hbase/RegionLocationTableTest.java
index 0b44994..e018e74 100644
--- a/crunch-hbase/src/it/java/org/apache/crunch/io/hbase/RegionLocationTableTest.java
+++ b/crunch-hbase/src/it/java/org/apache/crunch/io/hbase/RegionLocationTableTest.java
@@ -128,10 +128,36 @@
deserialized.getPreferredNodeForRow(new byte[] { 10 }));
}
+ @Test
+ public void testNullRegionInfo() {
+ RegionLocationTable table = RegionLocationTable.create(TABLE_NAME,
+ ImmutableList.of(location(null, serverName("serverA"))));
+ assertNull(
+ table.getPreferredNodeForRow(new byte[] { 15 }));
+ }
+
+ @Test
+ public void testNullServerName() {
+ RegionLocationTable table = RegionLocationTable.create(TABLE_NAME,
+ ImmutableList.of(location(regionInfo(new byte[] { 10 }, new byte[] { 20 }), null)));
+ assertNull(
+ table.getPreferredNodeForRow(new byte[] { 15 }));
+ }
+
private static HRegionLocation location(byte[] startKey, byte[] endKey, String hostName) {
- return new HRegionLocation(
- new HRegionInfo(TableName.valueOf(TABLE_NAME), startKey, endKey),
- ServerName.valueOf(hostName, 60020, System.currentTimeMillis()));
+ return location(regionInfo(startKey, endKey), serverName(hostName));
+ }
+
+ private static HRegionLocation location(HRegionInfo regionInfo, ServerName serverName) {
+ return new HRegionLocation(regionInfo, serverName);
+ }
+
+ private static HRegionInfo regionInfo(byte[] startKey, byte[] endKey) {
+ return new HRegionInfo(TableName.valueOf(TABLE_NAME), startKey, endKey);
+ }
+
+ private static ServerName serverName(String hostName) {
+ return ServerName.valueOf(hostName, 60020, System.currentTimeMillis());
}
}
\ No newline at end of file
diff --git a/crunch-hbase/src/main/java/org/apache/crunch/io/hbase/RegionLocationTable.java b/crunch-hbase/src/main/java/org/apache/crunch/io/hbase/RegionLocationTable.java
index fa012af..05724fa 100644
--- a/crunch-hbase/src/main/java/org/apache/crunch/io/hbase/RegionLocationTable.java
+++ b/crunch-hbase/src/main/java/org/apache/crunch/io/hbase/RegionLocationTable.java
@@ -29,7 +29,9 @@
import com.google.common.collect.Maps;
import org.apache.hadoop.hbase.HConstants;
+import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.util.Bytes;
/**
@@ -54,13 +56,18 @@
public static RegionLocationTable create(String tableName, List<HRegionLocation> regionLocationList) {
NavigableMap<byte[], String> regionStartToServerHostName = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
for (HRegionLocation regionLocation : regionLocationList) {
- byte[] startKey = regionLocation.getRegionInfo().getStartKey();
+ HRegionInfo regionInfo = regionLocation.getRegionInfo();
+ if (regionInfo == null) {
+ continue;
+ }
+ byte[] startKey = regionInfo.getStartKey();
if (startKey == null) {
startKey = HConstants.EMPTY_START_ROW;
}
- regionStartToServerHostName.put(
- startKey,
- regionLocation.getServerName().getHostname());
+ ServerName serverName = regionLocation.getServerName();
+ if (serverName != null) {
+ regionStartToServerHostName.put(startKey, serverName.getHostname());
+ }
}
return new RegionLocationTable(tableName, regionStartToServerHostName);
}