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