diff --git a/hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/RegionPlanConditional.java b/hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/RegionPlanConditional.java index 8de371d341cd..063f3ba5f726 100644 --- a/hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/RegionPlanConditional.java +++ b/hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/RegionPlanConditional.java @@ -18,6 +18,7 @@ package org.apache.hadoop.hbase.master.balancer; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -85,8 +86,8 @@ boolean isViolating(RegionPlan regionPlan) { // Check Server int[] destinationRegionIndices = cluster.regionsPerServer[destinationServerIdx]; - Set serverRegions = Arrays.stream(cluster.regionsPerServer[destinationServerIdx]) - .mapToObj(idx -> cluster.regions[idx]).collect(Collectors.toSet()); + Set serverRegions = + getRegionsFromIndex(destinationServerIdx, cluster.regionsPerServer); for (int regionIdx : destinationRegionIndices) { serverRegions.add(cluster.regions[regionIdx]); } @@ -100,8 +101,7 @@ boolean isViolating(RegionPlan regionPlan) { // Check Host int hostIdx = cluster.serverIndexToHostIndex[destinationServerIdx]; - Set hostRegions = Arrays.stream(cluster.regionsPerHost[hostIdx]) - .mapToObj(idx -> cluster.regions[idx]).collect(Collectors.toSet()); + Set hostRegions = getRegionsFromIndex(hostIdx, cluster.regionsPerHost); if (isViolatingHost(regionPlan, hostRegions)) { return true; } @@ -112,8 +112,7 @@ boolean isViolating(RegionPlan regionPlan) { // Check Rack int rackIdx = cluster.serverIndexToRackIndex[destinationServerIdx]; - Set rackRegions = Arrays.stream(cluster.regionsPerRack[rackIdx]) - .mapToObj(idx -> cluster.regions[idx]).collect(Collectors.toSet()); + Set rackRegions = getRegionsFromIndex(rackIdx, cluster.regionsPerRack); if (isViolatingRack(regionPlan, rackRegions)) { return true; } @@ -130,4 +129,13 @@ boolean isViolatingHost(RegionPlan regionPlan, Set destinationRegion boolean isViolatingRack(RegionPlan regionPlan, Set destinationRegions) { return false; } + + private Set getRegionsFromIndex(int index, int[][] regionsPerIndex) { + int[] regionIndices = regionsPerIndex[index]; + if (regionIndices == null) { + return Collections.emptySet(); + } + return Arrays.stream(regionIndices).mapToObj(idx -> cluster.regions[idx]) + .collect(Collectors.toSet()); + } }