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.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertNotNull; 023import static org.junit.Assert.assertNull; 024import static org.junit.Assert.assertTrue; 025 026import java.util.HashMap; 027import java.util.Map; 028import java.util.Map.Entry; 029import org.apache.hadoop.hbase.HBaseClassTestRule; 030import org.apache.hadoop.hbase.TableName; 031import org.apache.hadoop.hbase.client.RegionInfo; 032import org.apache.hadoop.hbase.client.RegionInfoBuilder; 033import org.apache.hadoop.hbase.testclassification.SmallTests; 034import org.apache.hadoop.hbase.util.Bytes; 035import org.junit.ClassRule; 036import org.junit.Test; 037import org.junit.experimental.categories.Category; 038 039@Category({ SmallTests.class }) 040public class TestRegionSizeStoreImpl { 041 @ClassRule 042 public static final HBaseClassTestRule CLASS_RULE = 043 HBaseClassTestRule.forClass(TestRegionSizeStoreImpl.class); 044 045 private static final RegionInfo INFOA = RegionInfoBuilder.newBuilder(TableName.valueOf("TEST")) 046 .setStartKey(Bytes.toBytes("a")).setEndKey(Bytes.toBytes("b")).build(); 047 private static final RegionInfo INFOB = RegionInfoBuilder.newBuilder(TableName.valueOf("TEST")) 048 .setStartKey(Bytes.toBytes("b")).setEndKey(Bytes.toBytes("c")).build(); 049 050 @Test 051 public void testSizeUpdates() { 052 RegionSizeStore store = new RegionSizeStoreImpl(); 053 assertTrue(store.isEmpty()); 054 assertEquals(0, store.size()); 055 056 store.put(INFOA, 1024L); 057 058 assertFalse(store.isEmpty()); 059 assertEquals(1, store.size()); 060 assertEquals(1024L, store.getRegionSize(INFOA).getSize()); 061 062 store.put(INFOA, 2048L); 063 assertEquals(1, store.size()); 064 assertEquals(2048L, store.getRegionSize(INFOA).getSize()); 065 066 store.incrementRegionSize(INFOA, 512L); 067 assertEquals(1, store.size()); 068 assertEquals(2048L + 512L, store.getRegionSize(INFOA).getSize()); 069 070 store.remove(INFOA); 071 assertTrue(store.isEmpty()); 072 assertEquals(0, store.size()); 073 074 store.put(INFOA, 64L); 075 store.put(INFOB, 128L); 076 077 assertEquals(2, store.size()); 078 Map<RegionInfo, RegionSize> records = new HashMap<>(); 079 for (Entry<RegionInfo, RegionSize> entry : store) { 080 records.put(entry.getKey(), entry.getValue()); 081 } 082 083 assertEquals(64L, records.remove(INFOA).getSize()); 084 assertEquals(128L, records.remove(INFOB).getSize()); 085 assertTrue(records.isEmpty()); 086 } 087 088 @Test 089 public void testNegativeDeltaForMissingRegion() { 090 RegionSizeStore store = new RegionSizeStoreImpl(); 091 092 assertNull(store.getRegionSize(INFOA)); 093 094 // We shouldn't allow a negative size to enter the RegionSizeStore. Getting a negative size 095 // like this shouldn't be possible, but we can prevent the bad state from propagating and 096 // getting worse. 097 store.incrementRegionSize(INFOA, -5); 098 assertNotNull(store.getRegionSize(INFOA)); 099 assertEquals(0, store.getRegionSize(INFOA).getSize()); 100 } 101}