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