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