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.quotas;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertNotNull;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023import static org.mockito.Mockito.mock;
024import static org.mockito.Mockito.when;
025
026import java.util.Arrays;
027import java.util.Collections;
028import java.util.HashSet;
029import java.util.concurrent.TimeUnit;
030import org.apache.hadoop.conf.Configuration;
031import org.apache.hadoop.hbase.HBaseConfiguration;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.client.RegionInfo;
034import org.apache.hadoop.hbase.client.RegionInfoBuilder;
035import org.apache.hadoop.hbase.regionserver.HRegionServer;
036import org.apache.hadoop.hbase.testclassification.SmallTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.junit.jupiter.api.Tag;
039import org.junit.jupiter.api.Test;
040
041@Tag(SmallTests.TAG)
042public class TestRegionSizeReportingChore {
043
044  @Test
045  public void testDefaultConfigurationProperties() {
046    final Configuration conf = getDefaultHBaseConfiguration();
047    final HRegionServer rs = mockRegionServer(conf);
048    RegionSizeReportingChore chore = new RegionSizeReportingChore(rs);
049    assertEquals(RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_DELAY_DEFAULT,
050      chore.getInitialDelay());
051    assertEquals(RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_PERIOD_DEFAULT,
052      chore.getPeriod());
053    assertEquals(
054      TimeUnit.valueOf(RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_TIMEUNIT_DEFAULT),
055      chore.getTimeUnit());
056  }
057
058  @Test
059  public void testNonDefaultConfigurationProperties() {
060    final Configuration conf = getDefaultHBaseConfiguration();
061    final HRegionServer rs = mockRegionServer(conf);
062    final int period = RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_PERIOD_DEFAULT + 1;
063    final long delay = RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_DELAY_DEFAULT + 1L;
064    final String timeUnit = TimeUnit.SECONDS.name();
065    conf.setInt(RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_PERIOD_KEY, period);
066    conf.setLong(RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_DELAY_KEY, delay);
067    conf.set(RegionSizeReportingChore.REGION_SIZE_REPORTING_CHORE_TIMEUNIT_KEY, timeUnit);
068    RegionSizeReportingChore chore = new RegionSizeReportingChore(rs);
069    assertEquals(delay, chore.getInitialDelay());
070    assertEquals(period, chore.getPeriod());
071    assertEquals(TimeUnit.valueOf(timeUnit), chore.getTimeUnit());
072  }
073
074  @Test
075  public void testRemovableOfNonOnlineRegions() {
076    final Configuration conf = getDefaultHBaseConfiguration();
077    final HRegionServer rs = mockRegionServer(conf);
078    RegionSizeReportingChore chore = new RegionSizeReportingChore(rs);
079
080    RegionInfo infoA = RegionInfoBuilder.newBuilder(TableName.valueOf("T1"))
081      .setStartKey(Bytes.toBytes("a")).setEndKey(Bytes.toBytes("b")).build();
082    RegionInfo infoB = RegionInfoBuilder.newBuilder(TableName.valueOf("T1"))
083      .setStartKey(Bytes.toBytes("b")).setEndKey(Bytes.toBytes("d")).build();
084    RegionInfo infoC = RegionInfoBuilder.newBuilder(TableName.valueOf("T1"))
085      .setStartKey(Bytes.toBytes("c")).setEndKey(Bytes.toBytes("d")).build();
086
087    RegionSizeStore store = new RegionSizeStoreImpl();
088    store.put(infoA, 1024L);
089    store.put(infoB, 1024L);
090    store.put(infoC, 1024L);
091
092    // If there are no online regions, all entries should be removed.
093    chore.removeNonOnlineRegions(store, Collections.<RegionInfo> emptySet());
094    assertTrue(store.isEmpty());
095
096    store.put(infoA, 1024L);
097    store.put(infoB, 1024L);
098    store.put(infoC, 1024L);
099
100    // Remove a single region
101    chore.removeNonOnlineRegions(store, new HashSet<>(Arrays.asList(infoA, infoC)));
102    assertEquals(2, store.size());
103    assertNotNull(store.getRegionSize(infoA));
104    assertNotNull(store.getRegionSize(infoC));
105  }
106
107  /**
108   * Creates an HBase Configuration object for the default values.
109   */
110  private Configuration getDefaultHBaseConfiguration() {
111    final Configuration conf = HBaseConfiguration.create();
112    conf.addResource("hbase-default.xml");
113    return conf;
114  }
115
116  private HRegionServer mockRegionServer(Configuration conf) {
117    HRegionServer rs = mock(HRegionServer.class);
118    when(rs.getConfiguration()).thenReturn(conf);
119    return rs;
120  }
121}