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 = ServerName.valueOf("local-rs", DEFAULT_REGIONSERVER_PORT,
054      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(
062        mockRegion("region1", 123),
063        mockRegion("region3", 1232),
064        mockRegion("region2",  54321)
065    );
066
067    RegionSizeCalculator calculator = new RegionSizeCalculator(regionLocator, admin);
068
069    assertEquals(123 * megabyte, calculator.getRegionSize("region1".getBytes()));
070    assertEquals(54321 * megabyte, calculator.getRegionSize("region2".getBytes()));
071    assertEquals(1232 * megabyte, calculator.getRegionSize("region3".getBytes()));
072    // if regionCalculator does not know about a region, it should return 0
073    assertEquals(0 * megabyte, calculator.getRegionSize("otherTableRegion".getBytes()));
074
075    assertEquals(3, calculator.getRegionSizeMap().size());
076  }
077
078
079  /**
080   * When size of region in megabytes is larger than largest possible integer there could be
081   * error caused by lost of precision.
082   * */
083  @Test
084  public void testLargeRegion() throws Exception {
085
086    RegionLocator regionLocator = mockRegionLocator("largeRegion");
087
088    Admin admin = mockAdmin(
089        mockRegion("largeRegion", Integer.MAX_VALUE)
090    );
091
092    RegionSizeCalculator calculator = new RegionSizeCalculator(regionLocator, admin);
093
094    assertEquals(((long) Integer.MAX_VALUE) * megabyte, calculator.getRegionSize("largeRegion".getBytes()));
095  }
096
097  /** When calculator is disabled, it should return 0 for each request.*/
098  @Test
099  public void testDisabled() throws Exception {
100    String regionName = "cz.goout:/index.html";
101    RegionLocator table = mockRegionLocator(regionName);
102
103    Admin admin = mockAdmin(
104        mockRegion(regionName, 999)
105    );
106
107    //first request on enabled calculator
108    RegionSizeCalculator calculator = new RegionSizeCalculator(table, admin);
109    assertEquals(999 * megabyte, calculator.getRegionSize(regionName.getBytes()));
110
111    //then disabled calculator.
112    configuration.setBoolean(RegionSizeCalculator.ENABLE_REGIONSIZECALCULATOR, false);
113    RegionSizeCalculator disabledCalculator = new RegionSizeCalculator(table, admin);
114    assertEquals(0 * megabyte, disabledCalculator.getRegionSize(regionName.getBytes()));
115
116    assertEquals(0, disabledCalculator.getRegionSizeMap().size());
117  }
118
119  /**
120   * Makes some table with given region names.
121   * */
122  private RegionLocator mockRegionLocator(String... regionNames) throws IOException {
123    RegionLocator mockedTable = Mockito.mock(RegionLocator.class);
124    when(mockedTable.getName()).thenReturn(TableName.valueOf("sizeTestTable"));
125    List<HRegionLocation> regionLocations = new ArrayList<>(regionNames.length);
126    when(mockedTable.getAllRegionLocations()).thenReturn(regionLocations);
127
128    for (String regionName : regionNames) {
129      HRegionInfo info = Mockito.mock(HRegionInfo.class);
130      when(info.getRegionName()).thenReturn(regionName.getBytes());
131      regionLocations.add(new HRegionLocation(info, sn));
132    }
133
134    return mockedTable;
135  }
136
137  /**
138   * Creates mock returning RegionLoad info about given servers.
139  */
140  private Admin mockAdmin(RegionMetrics... regionLoadArray) throws Exception {
141    Admin mockAdmin = Mockito.mock(Admin.class);
142    List<RegionMetrics> regionLoads = new ArrayList<>();
143    for (RegionMetrics regionLoad : regionLoadArray) {
144      regionLoads.add(regionLoad);
145    }
146    when(mockAdmin.getConfiguration()).thenReturn(configuration);
147    when(mockAdmin.getRegionMetrics(sn, TableName.valueOf("sizeTestTable")))
148        .thenReturn(regionLoads);
149    return mockAdmin;
150  }
151
152  /**
153   * Creates mock of region with given name and size.
154   *
155   * @param  fileSizeMb number of megabytes occupied by region in file store in megabytes
156   * */
157  private RegionMetrics mockRegion(String regionName, int fileSizeMb) {
158    RegionMetrics region = Mockito.mock(RegionMetrics.class);
159    when(region.getRegionName()).thenReturn(regionName.getBytes());
160    when(region.getNameAsString()).thenReturn(regionName);
161    when(region.getStoreFileSize()).thenReturn(new Size(fileSizeMb, Size.Unit.MEGABYTE));
162    return region;
163  }
164}