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