001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to you under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.hadoop.hbase.quotas; 018 019import static org.junit.Assert.assertEquals; 020import static org.junit.Assert.assertFalse; 021import static org.junit.Assert.assertNotNull; 022import static org.junit.Assert.assertNull; 023import static org.junit.Assert.assertTrue; 024 025import java.util.HashMap; 026import java.util.Map; 027import java.util.Map.Entry; 028 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}