From a2af3a76a22bf4e8e07396606927b58c6da7c771 Mon Sep 17 00:00:00 2001 From: Zhihong Yu Date: Fri, 19 Apr 2013 17:08:00 +0000 Subject: [PATCH] HBASE-8374 NullPointerException when launching the balancer due to unknown region location (Ted Yu) git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1469954 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop/hbase/master/balancer/BaseLoadBalancer.java | 9 ++++++++- .../hbase/master/balancer/StochasticLoadBalancer.java | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.java index 50c774f0cac..94149a5dbb5 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.java @@ -105,10 +105,15 @@ public abstract class BaseLoadBalancer implements LoadBalancer { regionLocations = new int[numRegions][]; int tableIndex = 0, serverIndex = 0, regionIndex = 0, regionPerServerIndex = 0; + // populate serversToIndex first for (Entry> entry : clusterState.entrySet()) { servers[serverIndex] = entry.getKey(); regionsPerServer[serverIndex] = new int[entry.getValue().size()]; serversToIndex.put(servers[serverIndex], Integer.valueOf(serverIndex)); + serverIndex++; + } + serverIndex = 0; + for (Entry> entry : clusterState.entrySet()) { regionPerServerIndex = 0; for (HRegionInfo region : entry.getValue()) { byte[] tableName = region.getTableName(); @@ -142,7 +147,9 @@ public abstract class BaseLoadBalancer implements LoadBalancer { List loc = regionFinder.getTopBlockLocations(region); regionLocations[regionIndex] = new int[loc.size()]; for (int i=0; i < loc.size(); i++) { - regionLocations[regionIndex][i] = serversToIndex.get(loc.get(i)); + regionLocations[regionIndex][i] = + loc.get(i) == null ? -1 : + (serversToIndex.get(loc.get(i)) == null ? -1 : serversToIndex.get(loc.get(i))); } } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/StochasticLoadBalancer.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/StochasticLoadBalancer.java index 50e557046c0..330e9b18ad7 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/StochasticLoadBalancer.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/StochasticLoadBalancer.java @@ -505,7 +505,7 @@ public class StochasticLoadBalancer extends BaseLoadBalancer { int index = -1; for (int j = 0; j < regionLocations.length; j++) { - if (regionLocations[j] == serverIndex) { + if (regionLocations[j] >= 0 && regionLocations[j] == serverIndex) { index = j; break; }