diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionInfo.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionInfo.java
index 7f5d399eaaf..5bb4aef2df6 100644
--- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionInfo.java
+++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionInfo.java
@@ -18,6 +18,8 @@
*/
package org.apache.hadoop.hbase.client;
+import edu.umd.cs.findbugs.annotations.CheckForNull;
+
import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
@@ -271,6 +273,26 @@ public interface RegionInfo {
return encodedName;
}
+ @InterfaceAudience.Private
+ static String getRegionNameAsString(byte[] regionName) {
+ return getRegionNameAsString(null, regionName);
+ }
+
+ @InterfaceAudience.Private
+ static String getRegionNameAsString(@CheckForNull RegionInfo ri, byte[] regionName) {
+ if (RegionInfo.hasEncodedName(regionName)) {
+ // new format region names already have their encoded name.
+ return Bytes.toStringBinary(regionName);
+ }
+
+ // old format. regionNameStr doesn't have the region name.
+ if (ri == null) {
+ return Bytes.toStringBinary(regionName) + "." + RegionInfo.encodeRegionName(regionName);
+ } else {
+ return Bytes.toStringBinary(regionName) + "." + ri.getEncodedName();
+ }
+ }
+
/**
* @return Return a String of short, printable names for hris
* (usually encoded name) for us logging.
diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionInfoBuilder.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionInfoBuilder.java
index 3de98606f39..cd9e40b9524 100644
--- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionInfoBuilder.java
+++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionInfoBuilder.java
@@ -287,15 +287,7 @@ public class RegionInfoBuilder {
*/
@Override
public String getRegionNameAsString() {
- if (RegionInfo.hasEncodedName(this.regionName)) {
- // new format region names already have their encoded name.
- return Bytes.toStringBinary(this.regionName);
- }
-
- // old format. regionNameStr doesn't have the region name.
- //
- //
- return Bytes.toStringBinary(this.regionName) + "." + this.getEncodedName();
+ return RegionInfo.getRegionNameAsString(this, this.regionName);
}
/** @return the encoded region name */
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 30e4d494d49..cbc1a376078 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
@@ -45,7 +45,6 @@ import org.apache.hadoop.hbase.master.balancer.BaseLoadBalancer.Cluster.AssignRe
import org.apache.hadoop.hbase.master.balancer.BaseLoadBalancer.Cluster.LocalityType;
import org.apache.hadoop.hbase.master.balancer.BaseLoadBalancer.Cluster.MoveRegionAction;
import org.apache.hadoop.hbase.master.balancer.BaseLoadBalancer.Cluster.SwapRegionsAction;
-import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
import org.apache.yetus.audience.InterfaceAudience;
import org.slf4j.Logger;
@@ -533,14 +532,15 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
clusterStatus.getLiveServerMetrics().forEach((ServerName sn, ServerMetrics sm) -> {
sm.getRegionMetrics().forEach((byte[] regionName, RegionMetrics rm) -> {
- Deque rLoads = oldLoads.get(Bytes.toString(regionName));
+ String regionNameAsString = RegionInfo.getRegionNameAsString(regionName);
+ Deque rLoads = oldLoads.get(regionNameAsString);
if (rLoads == null) {
rLoads = new ArrayDeque<>(numRegionLoadsToRemember + 1);
} else if (rLoads.size() >= numRegionLoadsToRemember) {
rLoads.remove();
}
rLoads.add(new BalancerRegionLoad(rm));
- loads.put(Bytes.toString(regionName), rLoads);
+ loads.put(regionNameAsString, rLoads);
});
});
diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/balancer/TestStochasticLoadBalancer.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/balancer/TestStochasticLoadBalancer.java
index d25f92478b5..1ca64ef0eab 100644
--- a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/balancer/TestStochasticLoadBalancer.java
+++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/balancer/TestStochasticLoadBalancer.java
@@ -131,7 +131,7 @@ public class TestStochasticLoadBalancer extends BalancerTestBase {
when(rl.getWriteRequestCount()).thenReturn(0L);
when(rl.getMemStoreSize()).thenReturn(Size.ZERO);
when(rl.getStoreFileSize()).thenReturn(Size.ZERO);
- regionLoadMap.put(info.getEncodedNameAsBytes(), rl);
+ regionLoadMap.put(info.getRegionName(), rl);
}
when(serverMetrics.getRegionMetrics()).thenReturn(regionLoadMap);
return serverMetrics;
@@ -222,10 +222,12 @@ public class TestStochasticLoadBalancer extends BalancerTestBase {
loadBalancer.setClusterMetrics(clusterStatus);
}
- assertTrue(loadBalancer.loads.get(REGION_KEY) != null);
- assertTrue(loadBalancer.loads.get(REGION_KEY).size() == 15);
- Queue loads = loadBalancer.loads.get(REGION_KEY);
+ String regionNameAsString = RegionInfo.getRegionNameAsString(Bytes.toBytes(REGION_KEY));
+ assertTrue(loadBalancer.loads.get(regionNameAsString) != null);
+ assertTrue(loadBalancer.loads.get(regionNameAsString).size() == 15);
+
+ Queue loads = loadBalancer.loads.get(regionNameAsString);
int i = 0;
while(loads.size() > 0) {
BalancerRegionLoad rl = loads.remove();