2323
2424import java .io .IOException ;
2525import java .util .ArrayList ;
26+ import java .util .Arrays ;
27+ import java .util .Collections ;
2628import java .util .List ;
2729import org .apache .hadoop .conf .Configuration ;
2830import org .apache .hadoop .hbase .HBaseClassTestRule ;
3638import org .apache .hadoop .hbase .client .RegionLocator ;
3739import org .apache .hadoop .hbase .testclassification .MiscTests ;
3840import org .apache .hadoop .hbase .testclassification .SmallTests ;
41+ import org .apache .hadoop .hbase .util .Bytes ;
3942import org .junit .ClassRule ;
4043import org .junit .Test ;
4144import org .junit .experimental .categories .Category ;
@@ -63,11 +66,12 @@ public void testSimpleTestCase() throws Exception {
6366
6467 RegionSizeCalculator calculator = new RegionSizeCalculator (regionLocator , admin );
6568
66- assertEquals (123 * megabyte , calculator .getRegionSize ("region1" .getBytes ()));
67- assertEquals (54321 * megabyte , calculator .getRegionSize ("region2" .getBytes ()));
68- assertEquals (1232 * megabyte , calculator .getRegionSize ("region3" .getBytes ()));
69+ assertEquals (123 * megabyte , calculator .getRegionSize (Bytes .toBytes ("region1" )));
70+ assertEquals (54321 * megabyte , calculator .getRegionSize (Bytes .toBytes ("region2" )));
71+ assertEquals (1232 * megabyte , calculator .getRegionSize (Bytes .toBytes ("region3" )));
72+
6973 // if regionCalculator does not know about a region, it should return 0
70- assertEquals (0 * megabyte , calculator .getRegionSize ("otherTableRegion" . getBytes ( )));
74+ assertEquals (0 , calculator .getRegionSize (Bytes . toBytes ( "otherTableRegion" )));
7175
7276 assertEquals (3 , calculator .getRegionSizeMap ().size ());
7377 }
@@ -104,24 +108,37 @@ public void testDisabled() throws Exception {
104108 // then disabled calculator.
105109 configuration .setBoolean (RegionSizeCalculator .ENABLE_REGIONSIZECALCULATOR , false );
106110 RegionSizeCalculator disabledCalculator = new RegionSizeCalculator (table , admin );
107- assertEquals (0 * megabyte , disabledCalculator .getRegionSize (regionName .getBytes ()));
108-
111+ assertEquals (0 , disabledCalculator .getRegionSize (Bytes .toBytes (regionName )));
109112 assertEquals (0 , disabledCalculator .getRegionSizeMap ().size ());
110113 }
111114
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+
112124 /**
113125 * Makes some table with given region names.
114126 */
115127 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 {
116133 RegionLocator mockedTable = Mockito .mock (RegionLocator .class );
117134 when (mockedTable .getName ()).thenReturn (TableName .valueOf ("sizeTestTable" ));
118- List <HRegionLocation > regionLocations = new ArrayList <>(regionNames .length );
135+ List <HRegionLocation > regionLocations = new ArrayList <>(regionNames .size () );
119136 when (mockedTable .getAllRegionLocations ()).thenReturn (regionLocations );
120137
121138 for (String regionName : regionNames ) {
122139 HRegionInfo info = Mockito .mock (HRegionInfo .class );
123- when (info .getRegionName ()).thenReturn (regionName . getBytes ( ));
124- regionLocations .add (new HRegionLocation (info , sn ));
140+ when (info .getRegionName ()).thenReturn (Bytes . toBytes ( regionName ));
141+ regionLocations .add (new HRegionLocation (info , serverName ));
125142 }
126143
127144 return mockedTable ;
@@ -132,10 +149,7 @@ private RegionLocator mockRegionLocator(String... regionNames) throws IOExceptio
132149 */
133150 private Admin mockAdmin (RegionMetrics ... regionLoadArray ) throws Exception {
134151 Admin mockAdmin = Mockito .mock (Admin .class );
135- List <RegionMetrics > regionLoads = new ArrayList <>();
136- for (RegionMetrics regionLoad : regionLoadArray ) {
137- regionLoads .add (regionLoad );
138- }
152+ List <RegionMetrics > regionLoads = new ArrayList <>(Arrays .asList (regionLoadArray ));
139153 when (mockAdmin .getConfiguration ()).thenReturn (configuration );
140154 when (mockAdmin .getRegionMetrics (sn , TableName .valueOf ("sizeTestTable" )))
141155 .thenReturn (regionLoads );
0 commit comments