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