001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to you under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.hadoop.hbase.quotas;
018
019import static org.junit.Assert.assertEquals;
020import static org.junit.Assert.assertNotNull;
021import static org.junit.Assert.assertTrue;
022import static org.mockito.Mockito.mock;
023import static org.mockito.Mockito.when;
024
025import java.util.Arrays;
026import java.util.Collections;
027import java.util.HashSet;
028import java.util.concurrent.TimeUnit;
029
030import org.apache.hadoop.conf.Configuration;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.HBaseConfiguration;
033import org.apache.hadoop.hbase.TableName;
034import org.apache.hadoop.hbase.client.RegionInfo;
035import org.apache.hadoop.hbase.client.RegionInfoBuilder;
036import org.apache.hadoop.hbase.regionserver.HRegionServer;
037import org.apache.hadoop.hbase.testclassification.SmallTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042
043@Category({SmallTests.class})
044public class TestRegionSizeReportingChore {
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047      HBaseClassTestRule.forClass(TestRegionSizeReportingChore.class);
048
049  @Test
050  public void testDefaultConfigurationProperties() {
051    final Configuration conf = getDefaultHBaseConfiguration();
052    final HRegionServer rs = mockRegionServer(conf);
053    RegionSizeReportingChore chore = new RegionSizeReportingChore(rs);
054    assertEquals(
055        RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_DELAY_DEFAULT,
056        chore.getInitialDelay());
057    assertEquals(
058        RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_PERIOD_DEFAULT, chore.getPeriod());
059    assertEquals(
060        TimeUnit.valueOf(RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_TIMEUNIT_DEFAULT),
061        chore.getTimeUnit());
062  }
063
064  @Test
065  public void testNonDefaultConfigurationProperties() {
066    final Configuration conf = getDefaultHBaseConfiguration();
067    final HRegionServer rs = mockRegionServer(conf);
068    final int period = RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_PERIOD_DEFAULT + 1;
069    final long delay = RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_DELAY_DEFAULT + 1L;
070    final String timeUnit = TimeUnit.SECONDS.name();
071    conf.setInt(RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_PERIOD_KEY, period);
072    conf.setLong(RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_DELAY_KEY, delay);
073    conf.set(RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_TIMEUNIT_KEY, timeUnit);
074    RegionSizeReportingChore chore = new RegionSizeReportingChore(rs);
075    assertEquals(delay, chore.getInitialDelay());
076    assertEquals(period, chore.getPeriod());
077    assertEquals(TimeUnit.valueOf(timeUnit), chore.getTimeUnit());
078  }
079
080  @Test
081  public void testRemovableOfNonOnlineRegions() {
082    final Configuration conf = getDefaultHBaseConfiguration();
083    final HRegionServer rs = mockRegionServer(conf);
084    RegionSizeReportingChore chore = new RegionSizeReportingChore(rs);
085
086    RegionInfo infoA = RegionInfoBuilder.newBuilder(TableName.valueOf("T1"))
087        .setStartKey(Bytes.toBytes("a")).setEndKey(Bytes.toBytes("b")).build();
088    RegionInfo infoB = RegionInfoBuilder.newBuilder(TableName.valueOf("T1"))
089        .setStartKey(Bytes.toBytes("b")).setEndKey(Bytes.toBytes("d")).build();
090    RegionInfo infoC = RegionInfoBuilder.newBuilder(TableName.valueOf("T1"))
091        .setStartKey(Bytes.toBytes("c")).setEndKey(Bytes.toBytes("d")).build();
092
093    RegionSizeStore store = new RegionSizeStoreImpl();
094    store.put(infoA, 1024L);
095    store.put(infoB, 1024L);
096    store.put(infoC, 1024L);
097
098    // If there are no online regions, all entries should be removed.
099    chore.removeNonOnlineRegions(store, Collections.<RegionInfo> emptySet());
100    assertTrue(store.isEmpty());
101
102    store.put(infoA, 1024L);
103    store.put(infoB, 1024L);
104    store.put(infoC, 1024L);
105
106    // Remove a single region
107    chore.removeNonOnlineRegions(store, new HashSet<>(Arrays.asList(infoA, infoC)));
108    assertEquals(2, store.size());
109    assertNotNull(store.getRegionSize(infoA));
110    assertNotNull(store.getRegionSize(infoC));
111  }
112
113  /**
114   * Creates an HBase Configuration object for the default values.
115   */
116  private Configuration getDefaultHBaseConfiguration() {
117    final Configuration conf = HBaseConfiguration.create();
118    conf.addResource("hbase-default.xml");
119    return conf;
120  }
121
122  private HRegionServer mockRegionServer(Configuration conf) {
123    HRegionServer rs = mock(HRegionServer.class);
124    when(rs.getConfiguration()).thenReturn(conf);
125    return rs;
126  }
127}