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.regionserver; 019 020import static org.junit.Assert.assertEquals; 021 022import java.io.IOException; 023import java.util.ArrayList; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.hbase.CacheEvictionStats; 026import org.apache.hadoop.hbase.Cell; 027import org.apache.hadoop.hbase.HBaseClassTestRule; 028import org.apache.hadoop.hbase.HBaseTestingUtility; 029import org.apache.hadoop.hbase.HConstants; 030import org.apache.hadoop.hbase.MiniHBaseCluster; 031import org.apache.hadoop.hbase.TableName; 032import org.apache.hadoop.hbase.client.Admin; 033import org.apache.hadoop.hbase.client.AsyncAdmin; 034import org.apache.hadoop.hbase.client.AsyncConnection; 035import org.apache.hadoop.hbase.client.ConnectionFactory; 036import org.apache.hadoop.hbase.client.Scan; 037import org.apache.hadoop.hbase.client.Table; 038import org.apache.hadoop.hbase.io.hfile.BlockCache; 039import org.apache.hadoop.hbase.testclassification.LargeTests; 040import org.apache.hadoop.hbase.util.Bytes; 041import org.junit.After; 042import org.junit.Before; 043import org.junit.ClassRule; 044import org.junit.Test; 045import org.junit.experimental.categories.Category; 046import org.junit.runner.RunWith; 047import org.junit.runners.Parameterized; 048import org.slf4j.Logger; 049import org.slf4j.LoggerFactory; 050 051@Category(LargeTests.class) 052@RunWith(Parameterized.class) 053public class TestClearRegionBlockCache { 054 055 @ClassRule 056 public static final HBaseClassTestRule CLASS_RULE = 057 HBaseClassTestRule.forClass(TestClearRegionBlockCache.class); 058 059 private static final Logger LOG = LoggerFactory.getLogger(TestClearRegionBlockCache.class); 060 private static final TableName TABLE_NAME = TableName.valueOf("testClearRegionBlockCache"); 061 private static final byte[] FAMILY = Bytes.toBytes("family"); 062 private static final byte[][] SPLIT_KEY = new byte[][] { Bytes.toBytes("5") }; 063 private static final int NUM_RS = 2; 064 065 private final HBaseTestingUtility HTU = new HBaseTestingUtility(); 066 067 private Configuration CONF = HTU.getConfiguration(); 068 private Table table; 069 private HRegionServer rs1, rs2; 070 private MiniHBaseCluster cluster; 071 072 @Parameterized.Parameter public String cacheType; 073 074 @Parameterized.Parameters(name = "{index}: {0}") 075 public static Object[] data() { 076 return new Object[] { "lru", "bucket" }; 077 } 078 079 @Before 080 public void setup() throws Exception { 081 if (cacheType.equals("bucket")) { 082 CONF.set(HConstants.BUCKET_CACHE_IOENGINE_KEY, "offheap"); 083 CONF.setInt(HConstants.BUCKET_CACHE_SIZE_KEY, 30); 084 } 085 086 cluster = HTU.startMiniCluster(NUM_RS); 087 rs1 = cluster.getRegionServer(0); 088 rs2 = cluster.getRegionServer(1); 089 090 // Create table 091 table = HTU.createTable(TABLE_NAME, FAMILY, SPLIT_KEY); 092 093 HTU.loadNumericRows(table, FAMILY, 1, 10); 094 HTU.flush(TABLE_NAME); 095 } 096 097 @After 098 public void teardown() throws Exception { 099 HTU.shutdownMiniCluster(); 100 } 101 102 @Test 103 public void testClearBlockCache() throws Exception { 104 BlockCache blockCache1 = rs1.getBlockCache().get(); 105 BlockCache blockCache2 = rs2.getBlockCache().get(); 106 107 long initialBlockCount1 = blockCache1.getBlockCount(); 108 long initialBlockCount2 = blockCache2.getBlockCount(); 109 110 // scan will cause blocks to be added in BlockCache 111 scanAllRegionsForRS(rs1); 112 assertEquals(blockCache1.getBlockCount() - initialBlockCount1, 113 HTU.getNumHFilesForRS(rs1, TABLE_NAME, FAMILY)); 114 clearRegionBlockCache(rs1); 115 116 scanAllRegionsForRS(rs2); 117 assertEquals(blockCache2.getBlockCount() - initialBlockCount2, 118 HTU.getNumHFilesForRS(rs2, TABLE_NAME, FAMILY)); 119 clearRegionBlockCache(rs2); 120 121 assertEquals("" + blockCache1.getBlockCount(), 122 initialBlockCount1, blockCache1.getBlockCount()); 123 assertEquals("" + blockCache2.getBlockCount(), 124 initialBlockCount2, blockCache2.getBlockCount()); 125 } 126 127 @Test 128 public void testClearBlockCacheFromAdmin() throws Exception { 129 Admin admin = HTU.getAdmin(); 130 131 BlockCache blockCache1 = rs1.getBlockCache().get(); 132 BlockCache blockCache2 = rs2.getBlockCache().get(); 133 long initialBlockCount1 = blockCache1.getBlockCount(); 134 long initialBlockCount2 = blockCache2.getBlockCount(); 135 136 // scan will cause blocks to be added in BlockCache 137 scanAllRegionsForRS(rs1); 138 assertEquals(blockCache1.getBlockCount() - initialBlockCount1, 139 HTU.getNumHFilesForRS(rs1, TABLE_NAME, FAMILY)); 140 scanAllRegionsForRS(rs2); 141 assertEquals(blockCache2.getBlockCount() - initialBlockCount2, 142 HTU.getNumHFilesForRS(rs2, TABLE_NAME, FAMILY)); 143 144 CacheEvictionStats stats = admin.clearBlockCache(TABLE_NAME); 145 assertEquals(stats.getEvictedBlocks(), HTU.getNumHFilesForRS(rs1, TABLE_NAME, FAMILY) 146 + HTU.getNumHFilesForRS(rs2, TABLE_NAME, FAMILY)); 147 assertEquals(initialBlockCount1, blockCache1.getBlockCount()); 148 assertEquals(initialBlockCount2, blockCache2.getBlockCount()); 149 } 150 151 @Test 152 public void testClearBlockCacheFromAsyncAdmin() throws Exception { 153 try (AsyncConnection conn = ConnectionFactory.createAsyncConnection(HTU.getConfiguration()) 154 .get()) { 155 AsyncAdmin admin = conn.getAdmin(); 156 157 BlockCache blockCache1 = rs1.getBlockCache().get(); 158 BlockCache blockCache2 = rs2.getBlockCache().get(); 159 long initialBlockCount1 = blockCache1.getBlockCount(); 160 long initialBlockCount2 = blockCache2.getBlockCount(); 161 162 // scan will cause blocks to be added in BlockCache 163 scanAllRegionsForRS(rs1); 164 assertEquals(blockCache1.getBlockCount() - initialBlockCount1, 165 HTU.getNumHFilesForRS(rs1, TABLE_NAME, FAMILY)); 166 scanAllRegionsForRS(rs2); 167 assertEquals(blockCache2.getBlockCount() - initialBlockCount2, 168 HTU.getNumHFilesForRS(rs2, TABLE_NAME, FAMILY)); 169 170 CacheEvictionStats stats = admin.clearBlockCache(TABLE_NAME).get(); 171 assertEquals(stats.getEvictedBlocks(), HTU.getNumHFilesForRS(rs1, TABLE_NAME, FAMILY) + HTU 172 .getNumHFilesForRS(rs2, TABLE_NAME, FAMILY)); 173 assertEquals(initialBlockCount1, blockCache1.getBlockCount()); 174 assertEquals(initialBlockCount2, blockCache2.getBlockCount()); 175 } 176 } 177 178 private void scanAllRegionsForRS(HRegionServer rs) throws IOException { 179 for (Region region : rs.getRegions(TABLE_NAME)) { 180 RegionScanner scanner = region.getScanner(new Scan()); 181 while (scanner.next(new ArrayList<Cell>())); 182 } 183 } 184 185 private void clearRegionBlockCache(HRegionServer rs) { 186 for (Region region : rs.getRegions(TABLE_NAME)) { 187 rs.clearRegionBlockCache(region); 188 } 189 } 190}