Skip to content

Commit 1c41e86

Browse files
authored
HBASE-28354 RegionSizeCalculator throws NPE when regions are in transition (#5699)
When a region is in transition, it may briefly have a null ServerName in meta. The RegionSizeCalculator calls RegionLocator.getAllRegionLocations() and does not handle the possibility that a RegionLocation.getServerName() could be null. The ServerName is eventually passed into an Admin call, which results in an NPE. This has come up in other contexts. For example, taking a look at getAllRegionLocations() impl, we have checks to ensure that we don't call null server names. We need to similarly handle the possibility of nulls in RegionSizeCalculator. Signed-off-by: Nick Dimiduk <ndimiduk@apache.org> Signed-off-by: Hui Ruan <huiruan@apache.org>
1 parent 1e56034 commit 1c41e86

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/RegionSizeCalculator.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import java.util.Arrays;
2222
import java.util.Collections;
2323
import java.util.Map;
24+
import java.util.Objects;
2425
import java.util.Set;
2526
import java.util.TreeMap;
27+
import java.util.stream.Collectors;
2628
import org.apache.hadoop.conf.Configuration;
2729
import org.apache.hadoop.hbase.HRegionLocation;
2830
import org.apache.hadoop.hbase.RegionMetrics;
@@ -35,8 +37,6 @@
3537
import org.slf4j.Logger;
3638
import org.slf4j.LoggerFactory;
3739

38-
import org.apache.hbase.thirdparty.com.google.common.collect.Sets;
39-
4040
/**
4141
* Computes size of each region for given table and given column families. The value is used by
4242
* MapReduce for better scheduling.
@@ -96,12 +96,11 @@ private void init(RegionLocator regionLocator, Admin admin) throws IOException {
9696
}
9797

9898
private Set<ServerName> getRegionServersOfTable(RegionLocator regionLocator) throws IOException {
99-
100-
Set<ServerName> tableServers = Sets.newHashSet();
101-
for (HRegionLocation regionLocation : regionLocator.getAllRegionLocations()) {
102-
tableServers.add(regionLocation.getServerName());
103-
}
104-
return tableServers;
99+
// The region locations could contain `null` ServerName instances if the region is currently
100+
// in transition, we filter those out for now, which impacts the size calculation for these
101+
// regions temporarily until the ServerName gets filled in later
102+
return regionLocator.getAllRegionLocations().stream().map(HRegionLocation::getServerName)
103+
.filter(Objects::nonNull).collect(Collectors.toSet());
105104
}
106105

107106
boolean enabled(Configuration configuration) {

hbase-mapreduce/src/test/java/org/apache/hadoop/hbase/mapreduce/TestRegionSizeCalculator.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import java.io.IOException;
2525
import java.util.ArrayList;
26+
import java.util.Arrays;
27+
import java.util.Collections;
2628
import java.util.List;
2729
import org.apache.hadoop.conf.Configuration;
2830
import org.apache.hadoop.hbase.HBaseClassTestRule;
@@ -67,8 +69,9 @@ public void testSimpleTestCase() throws Exception {
6769
assertEquals(123 * megabyte, calculator.getRegionSize(Bytes.toBytes("region1")));
6870
assertEquals(54321 * megabyte, calculator.getRegionSize(Bytes.toBytes("region2")));
6971
assertEquals(1232 * megabyte, calculator.getRegionSize(Bytes.toBytes("region3")));
72+
7073
// if regionCalculator does not know about a region, it should return 0
71-
assertEquals(0 * megabyte, calculator.getRegionSize(Bytes.toBytes("otherTableRegion")));
74+
assertEquals(0, calculator.getRegionSize(Bytes.toBytes("otherTableRegion")));
7275

7376
assertEquals(3, calculator.getRegionSizeMap().size());
7477
}
@@ -105,24 +108,37 @@ public void testDisabled() throws Exception {
105108
// then disabled calculator.
106109
configuration.setBoolean(RegionSizeCalculator.ENABLE_REGIONSIZECALCULATOR, false);
107110
RegionSizeCalculator disabledCalculator = new RegionSizeCalculator(table, admin);
108-
assertEquals(0 * megabyte, disabledCalculator.getRegionSize(Bytes.toBytes(regionName)));
109-
111+
assertEquals(0, disabledCalculator.getRegionSize(Bytes.toBytes(regionName)));
110112
assertEquals(0, disabledCalculator.getRegionSizeMap().size());
111113
}
112114

115+
@Test
116+
public void testRegionWithNullServerName() throws Exception {
117+
RegionLocator regionLocator =
118+
mockRegionLocator(null, Collections.singletonList("someBigRegion"));
119+
Admin admin = mockAdmin(mockRegion("someBigRegion", Integer.MAX_VALUE));
120+
RegionSizeCalculator calculator = new RegionSizeCalculator(regionLocator, admin);
121+
assertEquals(0, calculator.getRegionSize(Bytes.toBytes("someBigRegion")));
122+
}
123+
113124
/**
114125
* Makes some table with given region names.
115126
*/
116127
private RegionLocator mockRegionLocator(String... regionNames) throws IOException {
128+
return mockRegionLocator(sn, Arrays.asList(regionNames));
129+
}
130+
131+
private RegionLocator mockRegionLocator(ServerName serverName, List<String> regionNames)
132+
throws IOException {
117133
RegionLocator mockedTable = Mockito.mock(RegionLocator.class);
118134
when(mockedTable.getName()).thenReturn(TableName.valueOf("sizeTestTable"));
119-
List<HRegionLocation> regionLocations = new ArrayList<>(regionNames.length);
135+
List<HRegionLocation> regionLocations = new ArrayList<>(regionNames.size());
120136
when(mockedTable.getAllRegionLocations()).thenReturn(regionLocations);
121137

122138
for (String regionName : regionNames) {
123139
RegionInfo info = Mockito.mock(RegionInfo.class);
124140
when(info.getRegionName()).thenReturn(Bytes.toBytes(regionName));
125-
regionLocations.add(new HRegionLocation(info, sn));
141+
regionLocations.add(new HRegionLocation(info, serverName));
126142
}
127143

128144
return mockedTable;
@@ -133,10 +149,7 @@ private RegionLocator mockRegionLocator(String... regionNames) throws IOExceptio
133149
*/
134150
private Admin mockAdmin(RegionMetrics... regionLoadArray) throws Exception {
135151
Admin mockAdmin = Mockito.mock(Admin.class);
136-
List<RegionMetrics> regionLoads = new ArrayList<>();
137-
for (RegionMetrics regionLoad : regionLoadArray) {
138-
regionLoads.add(regionLoad);
139-
}
152+
List<RegionMetrics> regionLoads = new ArrayList<>(Arrays.asList(regionLoadArray));
140153
when(mockAdmin.getConfiguration()).thenReturn(configuration);
141154
when(mockAdmin.getRegionMetrics(sn, TableName.valueOf("sizeTestTable")))
142155
.thenReturn(regionLoads);

0 commit comments

Comments
 (0)