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