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.mapreduce; 019 020import static org.apache.hadoop.hbase.HConstants.DEFAULT_REGIONSERVER_PORT; 021import static org.junit.Assert.assertEquals; 022import static org.mockito.Mockito.when; 023 024import java.io.IOException; 025import java.util.ArrayList; 026import java.util.Arrays; 027import java.util.Collections; 028import java.util.List; 029import org.apache.hadoop.conf.Configuration; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HRegionLocation; 032import org.apache.hadoop.hbase.RegionMetrics; 033import org.apache.hadoop.hbase.ServerName; 034import org.apache.hadoop.hbase.Size; 035import org.apache.hadoop.hbase.TableName; 036import org.apache.hadoop.hbase.client.Admin; 037import org.apache.hadoop.hbase.client.RegionInfo; 038import org.apache.hadoop.hbase.client.RegionLocator; 039import org.apache.hadoop.hbase.testclassification.MiscTests; 040import org.apache.hadoop.hbase.testclassification.SmallTests; 041import org.apache.hadoop.hbase.util.Bytes; 042import org.junit.ClassRule; 043import org.junit.Test; 044import org.junit.experimental.categories.Category; 045import org.mockito.Mockito; 046 047@Category({ MiscTests.class, SmallTests.class }) 048public class TestRegionSizeCalculator { 049 050 @ClassRule 051 public static final HBaseClassTestRule CLASS_RULE = 052 HBaseClassTestRule.forClass(TestRegionSizeCalculator.class); 053 054 private Configuration configuration = new Configuration(); 055 private final long megabyte = 1024L * 1024L; 056 private final ServerName sn = 057 ServerName.valueOf("local-rs", DEFAULT_REGIONSERVER_PORT, ServerName.NON_STARTCODE); 058 059 @Test 060 public void testSimpleTestCase() throws Exception { 061 062 RegionLocator regionLocator = mockRegionLocator("region1", "region2", "region3"); 063 064 Admin admin = mockAdmin(mockRegion("region1", 123, 321), mockRegion("region3", 1232, 2321), 065 mockRegion("region2", 54321, 12345), mockRegion("region4", 6789, 0), 066 mockRegion("region5", 0, 4567)); 067 068 RegionSizeCalculator calculator = new RegionSizeCalculator(regionLocator, admin); 069 070 assertEquals((123 + 321) * megabyte, calculator.getRegionSize(Bytes.toBytes("region1"))); 071 assertEquals((54321 + 12345) * megabyte, calculator.getRegionSize(Bytes.toBytes("region2"))); 072 assertEquals((1232 + 2321) * megabyte, calculator.getRegionSize(Bytes.toBytes("region3"))); 073 assertEquals(6789 * megabyte, calculator.getRegionSize(Bytes.toBytes("region4"))); 074 assertEquals(4567 * megabyte, calculator.getRegionSize(Bytes.toBytes("region5"))); 075 076 // if regionCalculator does not know about a region, it should return 0 077 assertEquals(0, calculator.getRegionSize(Bytes.toBytes("otherTableRegion"))); 078 079 assertEquals(5, calculator.getRegionSizeMap().size()); 080 } 081 082 /** 083 * When size of region in megabytes is larger than largest possible integer there could be error 084 * caused by lost of precision. 085 */ 086 @Test 087 public void testLargeRegion() throws Exception { 088 089 RegionLocator regionLocator = mockRegionLocator("largeRegion"); 090 091 Admin admin = mockAdmin(mockRegion("largeRegion", Integer.MAX_VALUE, Integer.MAX_VALUE)); 092 093 RegionSizeCalculator calculator = new RegionSizeCalculator(regionLocator, admin); 094 095 assertEquals(((long) Integer.MAX_VALUE) * 2L * megabyte, 096 calculator.getRegionSize(Bytes.toBytes("largeRegion"))); 097 } 098 099 /** When calculator is disabled, it should return 0 for each request. */ 100 @Test 101 public void testDisabled() throws Exception { 102 String regionName = "cz.goout:/index.html"; 103 RegionLocator table = mockRegionLocator(regionName); 104 105 Admin admin = mockAdmin(mockRegion(regionName, 999, 888)); 106 107 // first request on enabled calculator 108 RegionSizeCalculator calculator = new RegionSizeCalculator(table, admin); 109 assertEquals((999 + 888) * megabyte, calculator.getRegionSize(Bytes.toBytes(regionName))); 110 111 // then disabled calculator. 112 configuration.setBoolean(RegionSizeCalculator.ENABLE_REGIONSIZECALCULATOR, false); 113 RegionSizeCalculator disabledCalculator = new RegionSizeCalculator(table, admin); 114 assertEquals(0, disabledCalculator.getRegionSize(Bytes.toBytes(regionName))); 115 assertEquals(0, disabledCalculator.getRegionSizeMap().size()); 116 } 117 118 @Test 119 public void testRegionWithNullServerName() throws Exception { 120 RegionLocator regionLocator = 121 mockRegionLocator(null, Collections.singletonList("someBigRegion")); 122 Admin admin = mockAdmin(mockRegion("someBigRegion", Integer.MAX_VALUE, Integer.MAX_VALUE)); 123 RegionSizeCalculator calculator = new RegionSizeCalculator(regionLocator, admin); 124 assertEquals(0, calculator.getRegionSize(Bytes.toBytes("someBigRegion"))); 125 } 126 127 /** 128 * Makes some table with given region names. 129 */ 130 private RegionLocator mockRegionLocator(String... regionNames) throws IOException { 131 return mockRegionLocator(sn, Arrays.asList(regionNames)); 132 } 133 134 private RegionLocator mockRegionLocator(ServerName serverName, List<String> regionNames) 135 throws IOException { 136 RegionLocator mockedTable = Mockito.mock(RegionLocator.class); 137 when(mockedTable.getName()).thenReturn(TableName.valueOf("sizeTestTable")); 138 List<HRegionLocation> regionLocations = new ArrayList<>(regionNames.size()); 139 when(mockedTable.getAllRegionLocations()).thenReturn(regionLocations); 140 141 for (String regionName : regionNames) { 142 RegionInfo info = Mockito.mock(RegionInfo.class); 143 when(info.getRegionName()).thenReturn(Bytes.toBytes(regionName)); 144 regionLocations.add(new HRegionLocation(info, serverName)); 145 } 146 147 return mockedTable; 148 } 149 150 /** 151 * Creates mock returning RegionLoad info about given servers. 152 */ 153 private Admin mockAdmin(RegionMetrics... regionLoadArray) throws Exception { 154 Admin mockAdmin = Mockito.mock(Admin.class); 155 List<RegionMetrics> regionLoads = new ArrayList<>(Arrays.asList(regionLoadArray)); 156 when(mockAdmin.getConfiguration()).thenReturn(configuration); 157 when(mockAdmin.getRegionMetrics(sn, TableName.valueOf("sizeTestTable"))) 158 .thenReturn(regionLoads); 159 return mockAdmin; 160 } 161 162 /** 163 * Creates mock of region with given name and size. 164 * @param fileSizeMb number of megabytes occupied by region in file store in megabytes 165 * @param memStoreSize number of megabytes occupied by region in memstore in megabytes 166 */ 167 private RegionMetrics mockRegion(String regionName, int fileSizeMb, int memStoreSize) { 168 RegionMetrics region = Mockito.mock(RegionMetrics.class); 169 when(region.getRegionName()).thenReturn(Bytes.toBytes(regionName)); 170 when(region.getNameAsString()).thenReturn(regionName); 171 when(region.getStoreFileSize()).thenReturn(new Size(fileSizeMb, Size.Unit.MEGABYTE)); 172 when(region.getMemStoreSize()).thenReturn(new Size(memStoreSize, Size.Unit.MEGABYTE)); 173 return region; 174 } 175}