001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.regionserver; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertNotNull; 022import static org.junit.Assert.assertTrue; 023import static org.mockito.Matchers.any; 024import static org.mockito.Matchers.anyLong; 025import static org.mockito.Mockito.doCallRealMethod; 026import static org.mockito.Mockito.mock; 027 028import java.util.HashMap; 029import java.util.Map; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.TableName; 032import org.apache.hadoop.hbase.client.RegionInfo; 033import org.apache.hadoop.hbase.client.RegionInfoBuilder; 034import org.apache.hadoop.hbase.testclassification.SmallTests; 035import org.apache.hadoop.hbase.util.Bytes; 036import org.junit.ClassRule; 037import org.junit.Test; 038import org.junit.experimental.categories.Category; 039 040import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 041import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionSpaceUse; 042import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionSpaceUseReportRequest; 043 044/** 045 * Test class for isolated (non-cluster) tests surrounding the report 046 * of Region space use to the Master by RegionServers. 047 */ 048@Category(SmallTests.class) 049public class TestRegionServerRegionSpaceUseReport { 050 051 @ClassRule 052 public static final HBaseClassTestRule CLASS_RULE = 053 HBaseClassTestRule.forClass(TestRegionServerRegionSpaceUseReport.class); 054 055 @Test 056 public void testConversion() { 057 TableName tn = TableName.valueOf("table1"); 058 059 RegionInfo hri1 = RegionInfoBuilder.newBuilder(tn) 060 .setStartKey(Bytes.toBytes("a")) 061 .setEndKey(Bytes.toBytes("b")) 062 .build(); 063 RegionInfo hri2 = RegionInfoBuilder.newBuilder(tn) 064 .setStartKey(Bytes.toBytes("b")) 065 .setEndKey(Bytes.toBytes("c")) 066 .build(); 067 RegionInfo hri3 = RegionInfoBuilder.newBuilder(tn) 068 .setStartKey(Bytes.toBytes("c")) 069 .setEndKey(Bytes.toBytes("d")) 070 .build(); 071 Map<RegionInfo,Long> sizes = new HashMap<>(); 072 sizes.put(hri1, 1024L * 1024L); 073 sizes.put(hri2, 1024L * 1024L * 8L); 074 sizes.put(hri3, 1024L * 1024L * 32L); 075 076 // Call the real method to convert the map into a protobuf 077 HRegionServer rs = mock(HRegionServer.class); 078 doCallRealMethod().when(rs).buildRegionSpaceUseReportRequest(any()); 079 doCallRealMethod().when(rs).convertRegionSize(any(), anyLong()); 080 081 RegionSpaceUseReportRequest requests = rs.buildRegionSpaceUseReportRequest(sizes); 082 assertEquals(sizes.size(), requests.getSpaceUseCount()); 083 for (RegionSpaceUse spaceUse : requests.getSpaceUseList()) { 084 RegionInfo hri = ProtobufUtil.toRegionInfo(spaceUse.getRegionInfo()); 085 Long expectedSize = sizes.remove(hri); 086 assertNotNull("Could not find size for HRI: " + hri, expectedSize); 087 assertEquals(expectedSize.longValue(), spaceUse.getRegionSize()); 088 } 089 assertTrue("Should not have any space use entries left: " + sizes, sizes.isEmpty()); 090 } 091 092 @Test(expected = NullPointerException.class) 093 public void testNullMap() { 094 // Call the real method to convert the map into a protobuf 095 HRegionServer rs = mock(HRegionServer.class); 096 doCallRealMethod().when(rs).buildRegionSpaceUseReportRequest(any()); 097 doCallRealMethod().when(rs).convertRegionSize(any(), anyLong()); 098 099 rs.buildRegionSpaceUseReportRequest(null); 100 } 101 102 @Test(expected = NullPointerException.class) 103 public void testMalformedMap() { 104 TableName tn = TableName.valueOf("table1"); 105 RegionInfo hri1 = RegionInfoBuilder.newBuilder(tn) 106 .setStartKey(Bytes.toBytes("a")) 107 .setEndKey(Bytes.toBytes("b")) 108 .build(); 109 Map<RegionInfo,Long> sizes = new HashMap<>(); 110 sizes.put(hri1, null); 111 112 // Call the real method to convert the map into a protobuf 113 HRegionServer rs = mock(HRegionServer.class); 114 doCallRealMethod().when(rs).buildRegionSpaceUseReportRequest(any()); 115 doCallRealMethod().when(rs).convertRegionSize(any(), anyLong()); 116 117 rs.buildRegionSpaceUseReportRequest(sizes); 118 } 119}