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; 019 020import static org.apache.hadoop.hbase.HConstants.BUCKET_CACHE_IOENGINE_KEY; 021import static org.apache.hadoop.hbase.HConstants.BUCKET_CACHE_SIZE_KEY; 022import static org.apache.hadoop.hbase.io.hfile.CacheConfig.CACHE_BLOCKS_ON_WRITE_KEY; 023import static org.apache.hadoop.hbase.io.hfile.CacheConfig.EVICT_BLOCKS_ON_CLOSE_KEY; 024import static org.apache.hadoop.hbase.io.hfile.CacheConfig.EVICT_BLOCKS_ON_SPLIT_KEY; 025import static org.apache.hadoop.hbase.io.hfile.CacheConfig.PREFETCH_BLOCKS_ON_OPEN_KEY; 026import static org.junit.Assert.assertTrue; 027 028import java.io.IOException; 029import java.util.ArrayList; 030import java.util.Collection; 031import java.util.List; 032import java.util.Map; 033import java.util.function.BiConsumer; 034import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 035import org.apache.hadoop.hbase.client.Put; 036import org.apache.hadoop.hbase.client.Table; 037import org.apache.hadoop.hbase.client.TableDescriptor; 038import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 039import org.apache.hadoop.hbase.regionserver.HStoreFile; 040import org.apache.hadoop.hbase.testclassification.MediumTests; 041import org.apache.hadoop.hbase.testclassification.MiscTests; 042import org.apache.hadoop.hbase.util.Bytes; 043import org.apache.hadoop.hbase.util.Pair; 044import org.junit.BeforeClass; 045import org.junit.ClassRule; 046import org.junit.Test; 047import org.junit.experimental.categories.Category; 048import org.slf4j.Logger; 049import org.slf4j.LoggerFactory; 050 051@Category({ MiscTests.class, MediumTests.class }) 052public class TestCacheEviction { 053 054 @ClassRule 055 public static final HBaseClassTestRule CLASS_RULE = 056 HBaseClassTestRule.forClass(TestCacheEviction.class); 057 058 private static final Logger LOG = LoggerFactory.getLogger(TestCacheEviction.class); 059 060 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 061 062 @BeforeClass 063 public static void setUp() throws Exception { 064 UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_META_OPERATION_TIMEOUT, 1000); 065 UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2); 066 UTIL.getConfiguration().setBoolean(CACHE_BLOCKS_ON_WRITE_KEY, true); 067 UTIL.getConfiguration().setBoolean(PREFETCH_BLOCKS_ON_OPEN_KEY, true); 068 UTIL.getConfiguration().set(BUCKET_CACHE_IOENGINE_KEY, "offheap"); 069 UTIL.getConfiguration().setInt(BUCKET_CACHE_SIZE_KEY, 200); 070 } 071 072 @Test 073 public void testEvictOnSplit() throws Exception { 074 doTestEvictOnSplit("testEvictOnSplit", true, 075 (f, m) -> Waiter.waitFor(UTIL.getConfiguration(), 1000, () -> m.get(f) != null), 076 (f, m) -> Waiter.waitFor(UTIL.getConfiguration(), 1000, () -> m.get(f) == null)); 077 } 078 079 @Test 080 public void testDoesntEvictOnSplit() throws Exception { 081 doTestEvictOnSplit("testDoesntEvictOnSplit", false, 082 (f, m) -> Waiter.waitFor(UTIL.getConfiguration(), 1000, () -> m.get(f) != null), 083 (f, m) -> Waiter.waitFor(UTIL.getConfiguration(), 1000, () -> m.get(f) != null)); 084 } 085 086 @Test 087 public void testEvictOnClose() throws Exception { 088 doTestEvictOnClose("testEvictOnClose", true, 089 (f, m) -> Waiter.waitFor(UTIL.getConfiguration(), 1000, () -> m.get(f) != null), 090 (f, m) -> Waiter.waitFor(UTIL.getConfiguration(), 1000, () -> m.get(f) == null)); 091 } 092 093 @Test 094 public void testDoesntEvictOnClose() throws Exception { 095 doTestEvictOnClose("testDoesntEvictOnClose", false, 096 (f, m) -> Waiter.waitFor(UTIL.getConfiguration(), 1000, () -> m.get(f) != null), 097 (f, m) -> Waiter.waitFor(UTIL.getConfiguration(), 1000, () -> m.get(f) != null)); 098 } 099 100 private void doTestEvictOnSplit(String table, boolean evictOnSplit, 101 BiConsumer<String, Map<String, Pair<String, Long>>> predicateBeforeSplit, 102 BiConsumer<String, Map<String, Pair<String, Long>>> predicateAfterSplit) throws Exception { 103 UTIL.startMiniCluster(1); 104 try { 105 TableName tableName = TableName.valueOf(table); 106 createAndCacheTable(tableName); 107 Collection<HStoreFile> files = 108 UTIL.getMiniHBaseCluster().getRegions(tableName).get(0).getStores().get(0).getStorefiles(); 109 checkCacheForBlocks(tableName, files, predicateBeforeSplit); 110 UTIL.getMiniHBaseCluster().getRegionServer(0).getConfiguration() 111 .setBoolean(EVICT_BLOCKS_ON_SPLIT_KEY, evictOnSplit); 112 UTIL.getAdmin().split(tableName, Bytes.toBytes("row-500")); 113 Waiter.waitFor(UTIL.getConfiguration(), 30000, 114 () -> UTIL.getMiniHBaseCluster().getRegions(tableName).size() == 2); 115 UTIL.waitUntilNoRegionsInTransition(); 116 checkCacheForBlocks(tableName, files, predicateAfterSplit); 117 } finally { 118 UTIL.shutdownMiniCluster(); 119 } 120 } 121 122 private void doTestEvictOnClose(String table, boolean evictOnClose, 123 BiConsumer<String, Map<String, Pair<String, Long>>> predicateBeforeClose, 124 BiConsumer<String, Map<String, Pair<String, Long>>> predicateAfterClose) throws Exception { 125 UTIL.startMiniCluster(1); 126 try { 127 TableName tableName = TableName.valueOf(table); 128 createAndCacheTable(tableName); 129 Collection<HStoreFile> files = 130 UTIL.getMiniHBaseCluster().getRegions(tableName).get(0).getStores().get(0).getStorefiles(); 131 checkCacheForBlocks(tableName, files, predicateBeforeClose); 132 UTIL.getMiniHBaseCluster().getRegionServer(0).getConfiguration() 133 .setBoolean(EVICT_BLOCKS_ON_CLOSE_KEY, evictOnClose); 134 UTIL.getAdmin().disableTable(tableName); 135 UTIL.waitUntilNoRegionsInTransition(); 136 checkCacheForBlocks(tableName, files, predicateAfterClose); 137 } finally { 138 UTIL.shutdownMiniCluster(); 139 } 140 } 141 142 private void createAndCacheTable(TableName tableName) throws IOException, InterruptedException { 143 byte[] family = Bytes.toBytes("CF"); 144 TableDescriptor td = TableDescriptorBuilder.newBuilder(tableName) 145 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build(); 146 UTIL.getAdmin().createTable(td); 147 UTIL.waitTableAvailable(tableName); 148 Table tbl = UTIL.getConnection().getTable(tableName); 149 List<Put> puts = new ArrayList<>(); 150 for (int i = 0; i < 1000; i++) { 151 Put p = new Put(Bytes.toBytes("row-" + i)); 152 p.addColumn(family, Bytes.toBytes(1), Bytes.toBytes("val-" + i)); 153 puts.add(p); 154 } 155 tbl.put(puts); 156 UTIL.getAdmin().flush(tableName); 157 } 158 159 private void checkCacheForBlocks(TableName tableName, Collection<HStoreFile> files, 160 BiConsumer<String, Map<String, Pair<String, Long>>> checker) { 161 files.forEach(f -> { 162 UTIL.getMiniHBaseCluster().getRegionServer(0).getBlockCache().ifPresent(cache -> { 163 cache.getFullyCachedFiles().ifPresent(m -> { 164 checker.accept(f.getPath().getName(), m); 165 }); 166 assertTrue(cache.getFullyCachedFiles().isPresent()); 167 }); 168 }); 169 } 170}