From 092f8caf7af19506ed6488a1f3aa4fd087d3fb91 Mon Sep 17 00:00:00 2001 From: Ray Mattingly Date: Mon, 17 Mar 2025 13:11:56 -0400 Subject: [PATCH] HBASE-29186 RegionPlanConditionals can produce a null pointer (#6796) Co-authored-by: Ray Mattingly Signed-off-by: Nick Dimiduk --- .../balancer/RegionPlanConditional.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) 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()); + } }