View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.hadoop.hbase.regionserver;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  import static org.mockito.Matchers.any;
23  import static org.mockito.Matchers.anyLong;
24  import static org.mockito.Mockito.doCallRealMethod;
25  import static org.mockito.Mockito.mock;
26  
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionInfo;
33  import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.RegionSpaceUse;
34  import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.RegionSpaceUseReportRequest;
35  import org.apache.hadoop.hbase.testclassification.SmallTests;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.junit.Test;
38  import org.junit.experimental.categories.Category;
39  
40  /**
41   * Test class for isolated (non-cluster) tests surrounding the report
42   * of Region space use to the Master by RegionServers.
43   */
44  @Category(SmallTests.class)
45  public class TestRegionServerRegionSpaceUseReport {
46  
47    @Test
48    public void testConversion() {
49      TableName tn = TableName.valueOf("table1");
50      HRegionInfo hri1 = new HRegionInfo(tn, Bytes.toBytes("a"), Bytes.toBytes("b"));
51      HRegionInfo hri2 = new HRegionInfo(tn, Bytes.toBytes("b"), Bytes.toBytes("c"));
52      HRegionInfo hri3 = new HRegionInfo(tn, Bytes.toBytes("c"), Bytes.toBytes("d"));
53      Map<HRegionInfo,Long> sizes = new HashMap<>();
54      sizes.put(hri1, 1024L * 1024L);
55      sizes.put(hri2, 1024L * 1024L * 8L);
56      sizes.put(hri3, 1024L * 1024L * 32L);
57  
58      // Call the real method to convert the map into a protobuf
59      HRegionServer rs = mock(HRegionServer.class);
60      doCallRealMethod().when(rs).buildRegionSpaceUseReportRequest(any(Map.class));
61      doCallRealMethod().when(rs).convertRegionSize(any(HRegionInfo.class), anyLong());
62  
63      RegionSpaceUseReportRequest requests = rs.buildRegionSpaceUseReportRequest(sizes);
64      assertEquals(sizes.size(), requests.getSpaceUseCount());
65      for (RegionSpaceUse spaceUse : requests.getSpaceUseList()) {
66        RegionInfo ri = spaceUse.getRegionInfo();
67        HRegionInfo hri = HRegionInfo.convert(ri);
68        Long expectedSize = sizes.remove(hri);
69        assertNotNull("Could not find size for HRI: " + hri, expectedSize);
70        assertEquals(expectedSize.longValue(), spaceUse.getRegionSize());
71      }
72      assertTrue("Should not have any space use entries left: " + sizes, sizes.isEmpty());
73    }
74  
75    @Test(expected = NullPointerException.class)
76    public void testNullMap() {
77      // Call the real method to convert the map into a protobuf
78      HRegionServer rs = mock(HRegionServer.class);
79      doCallRealMethod().when(rs).buildRegionSpaceUseReportRequest(any(Map.class));
80      doCallRealMethod().when(rs).convertRegionSize(any(HRegionInfo.class), anyLong());
81  
82      rs.buildRegionSpaceUseReportRequest(null);
83    }
84  
85    @Test(expected = NullPointerException.class)
86    public void testMalformedMap() {
87      TableName tn = TableName.valueOf("table1");
88      HRegionInfo hri1 = new HRegionInfo(tn, Bytes.toBytes("a"), Bytes.toBytes("b"));
89      Map<HRegionInfo,Long> sizes = new HashMap<>();
90      sizes.put(hri1, null);
91  
92      // Call the real method to convert the map into a protobuf
93      HRegionServer rs = mock(HRegionServer.class);
94      doCallRealMethod().when(rs).buildRegionSpaceUseReportRequest(any(Map.class));
95      doCallRealMethod().when(rs).convertRegionSize(any(HRegionInfo.class), anyLong());
96  
97      rs.buildRegionSpaceUseReportRequest(sizes);
98    }
99  }