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.apache.hadoop.hbase.util.Bytes.toBytes;
021import static org.junit.jupiter.api.Assertions.assertEquals;
022import static org.junit.jupiter.api.Assertions.assertNotNull;
023import static org.mockito.Mockito.mock;
024
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.client.RegionInfo;
027import org.apache.hadoop.hbase.client.RegionInfoBuilder;
028import org.apache.hadoop.hbase.master.MasterServices;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.junit.jupiter.api.Tag;
031import org.junit.jupiter.api.Test;
032
033@Tag(SmallTests.TAG)
034public class TestMasterQuotaManager {
035
036  @Test
037  public void testUninitializedQuotaManangerDoesNotFail() {
038    MasterServices masterServices = mock(MasterServices.class);
039    MasterQuotaManager manager = new MasterQuotaManager(masterServices);
040    manager.addRegionSize(null, 0, 0);
041    assertNotNull(manager.snapshotRegionSizes());
042  }
043
044  private RegionInfo createRegionInfo(TableName tableName, byte[] startKey, byte[] endKey) {
045    return RegionInfoBuilder.newBuilder(tableName).setStartKey(startKey).setEndKey(endKey).build();
046  }
047
048  @Test
049  public void testOldEntriesRemoved() {
050    MasterServices masterServices = mock(MasterServices.class);
051    MasterQuotaManager manager = new MasterQuotaManager(masterServices);
052    manager.initializeRegionSizes();
053    // Mock out some regions
054    TableName tableName = TableName.valueOf("foo");
055    RegionInfo region1 = createRegionInfo(tableName, null, toBytes("a"));
056    RegionInfo region2 = createRegionInfo(tableName, toBytes("a"), toBytes("b"));
057    RegionInfo region3 = createRegionInfo(tableName, toBytes("b"), toBytes("c"));
058    RegionInfo region4 = createRegionInfo(tableName, toBytes("c"), toBytes("d"));
059    RegionInfo region5 = createRegionInfo(tableName, toBytes("d"), null);
060
061    final long size = 0;
062    long time1 = 10;
063    manager.addRegionSize(region1, size, time1);
064    manager.addRegionSize(region2, size, time1);
065
066    long time2 = 20;
067    manager.addRegionSize(region3, size, time2);
068    manager.addRegionSize(region4, size, time2);
069
070    long time3 = 30;
071    manager.addRegionSize(region5, size, time3);
072
073    assertEquals(5, manager.snapshotRegionSizes().size());
074
075    QuotaObserverChore chore = mock(QuotaObserverChore.class);
076    // Prune nothing
077    assertEquals(0, manager.pruneEntriesOlderThan(0, chore));
078    assertEquals(5, manager.snapshotRegionSizes().size());
079    assertEquals(0, manager.pruneEntriesOlderThan(10, chore));
080    assertEquals(5, manager.snapshotRegionSizes().size());
081
082    // Prune the elements at time1
083    assertEquals(2, manager.pruneEntriesOlderThan(15, chore));
084    assertEquals(3, manager.snapshotRegionSizes().size());
085
086    // Prune the elements at time2
087    assertEquals(2, manager.pruneEntriesOlderThan(30, chore));
088    assertEquals(1, manager.snapshotRegionSizes().size());
089  }
090}