View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.hadoop.hbase.quotas;
18  
19  import static org.apache.hadoop.hbase.util.Bytes.toBytes;
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  import static org.mockito.Mockito.mock;
23  
24  import org.apache.hadoop.hbase.HRegionInfo;
25  import org.apache.hadoop.hbase.TableName;
26  import org.apache.hadoop.hbase.master.MasterServices;
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  @Category(SmallTests.class)
32  public class TestMasterQuotaManager {
33  
34    @Test
35    public void testUninitializedQuotaManangerDoesNotFail() {
36      MasterServices masterServices = mock(MasterServices.class);
37      MasterQuotaManager manager = new MasterQuotaManager(masterServices);
38      manager.addRegionSize(null, 0, 0);
39      assertNotNull(manager.snapshotRegionSizes());
40    }
41  
42    @Test
43    public void testOldEntriesRemoved() {
44      MasterServices masterServices = mock(MasterServices.class);
45      MasterQuotaManager manager = new MasterQuotaManager(masterServices);
46      manager.initializeRegionSizes();
47      // Mock out some regions
48      TableName tableName = TableName.valueOf("foo");
49      HRegionInfo region1 = new HRegionInfo(tableName, null, toBytes("a"));
50      HRegionInfo region2 = new HRegionInfo(tableName, toBytes("a"), toBytes("b"));
51      HRegionInfo region3 = new HRegionInfo(tableName, toBytes("b"), toBytes("c"));
52      HRegionInfo region4 = new HRegionInfo(tableName, toBytes("c"), toBytes("d"));
53      HRegionInfo region5 = new HRegionInfo(tableName, toBytes("d"), null);
54  
55      final long size = 0;
56      long time1 = 10;
57      manager.addRegionSize(region1, size, time1);
58      manager.addRegionSize(region2, size, time1);
59  
60      long time2 = 20;
61      manager.addRegionSize(region3, size, time2);
62      manager.addRegionSize(region4, size, time2);
63  
64      long time3 = 30;
65      manager.addRegionSize(region5, size, time3);
66  
67      assertEquals(5, manager.snapshotRegionSizes().size());
68  
69      // Prune nothing
70      assertEquals(0, manager.pruneEntriesOlderThan(0));
71      assertEquals(5, manager.snapshotRegionSizes().size());
72      assertEquals(0, manager.pruneEntriesOlderThan(10));
73      assertEquals(5, manager.snapshotRegionSizes().size());
74  
75      // Prune the elements at time1
76      assertEquals(2, manager.pruneEntriesOlderThan(15));
77      assertEquals(3, manager.snapshotRegionSizes().size());
78  
79      // Prune the elements at time2
80      assertEquals(2, manager.pruneEntriesOlderThan(30));
81      assertEquals(1, manager.snapshotRegionSizes().size());
82    }
83  }