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.io.hfile; 019 020import static org.apache.hadoop.hbase.HConstants.BUCKET_CACHE_IOENGINE_KEY; 021import static org.apache.hadoop.hbase.io.hfile.CacheConfig.CACHE_BLOCKS_ON_WRITE_KEY; 022import static org.junit.jupiter.api.Assertions.assertEquals; 023import static org.junit.jupiter.api.Assertions.assertNotEquals; 024import static org.junit.jupiter.api.Assertions.assertTrue; 025 026import java.io.IOException; 027import org.apache.hadoop.conf.Configuration; 028import org.apache.hadoop.fs.Path; 029import org.apache.hadoop.hbase.HBaseTestingUtil; 030import org.apache.hadoop.hbase.SingleProcessHBaseCluster; 031import org.apache.hadoop.hbase.StartTestingClusterOption; 032import org.apache.hadoop.hbase.TableName; 033import org.apache.hadoop.hbase.Waiter; 034import org.apache.hadoop.hbase.client.Admin; 035import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 036import org.apache.hadoop.hbase.client.Put; 037import org.apache.hadoop.hbase.client.RegionInfo; 038import org.apache.hadoop.hbase.client.Table; 039import org.apache.hadoop.hbase.client.TableDescriptor; 040import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 041import org.apache.hadoop.hbase.regionserver.HRegionServer; 042import org.apache.hadoop.hbase.testclassification.IOTests; 043import org.apache.hadoop.hbase.testclassification.MediumTests; 044import org.apache.hadoop.hbase.util.Bytes; 045import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 046import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster; 047import org.junit.jupiter.api.AfterEach; 048import org.junit.jupiter.api.BeforeEach; 049import org.junit.jupiter.api.Tag; 050import org.junit.jupiter.api.Test; 051import org.slf4j.Logger; 052import org.slf4j.LoggerFactory; 053 054@Tag(IOTests.TAG) 055@Tag(MediumTests.TAG) 056public class TestBlockEvictionOnRegionMovement { 057 058 private static final Logger LOG = 059 LoggerFactory.getLogger(TestBlockEvictionOnRegionMovement.class); 060 061 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 062 063 private Configuration conf; 064 Path testDir; 065 MiniZooKeeperCluster zkCluster; 066 SingleProcessHBaseCluster cluster; 067 StartTestingClusterOption option = 068 StartTestingClusterOption.builder().numRegionServers(2).build(); 069 070 @BeforeEach 071 public void setup() throws Exception { 072 conf = TEST_UTIL.getConfiguration(); 073 testDir = TEST_UTIL.getDataTestDir(); 074 TEST_UTIL.getTestFileSystem().mkdirs(testDir); 075 076 conf.setBoolean(CacheConfig.PREFETCH_BLOCKS_ON_OPEN_KEY, true); 077 conf.set(BUCKET_CACHE_IOENGINE_KEY, "file:" + testDir + "/bucket.cache"); 078 conf.setInt("hbase.bucketcache.size", 400); 079 conf.set("hbase.bucketcache.persistent.path", testDir + "/bucket.persistence"); 080 conf.setLong(CacheConfig.BUCKETCACHE_PERSIST_INTERVAL_KEY, 100); 081 conf.setBoolean(CacheConfig.EVICT_BLOCKS_ON_CLOSE_KEY, true); 082 conf.setBoolean(CACHE_BLOCKS_ON_WRITE_KEY, true); 083 zkCluster = TEST_UTIL.startMiniZKCluster(); 084 cluster = TEST_UTIL.startMiniHBaseCluster(option); 085 cluster.setConf(conf); 086 } 087 088 @Test 089 public void testBlockEvictionOnRegionMove() throws Exception { 090 // Write to table and flush 091 TableName tableRegionMove = writeDataToTable("testBlockEvictionOnRegionMove"); 092 093 HRegionServer regionServingRS = 094 cluster.getRegionServer(1).getRegions(tableRegionMove).size() == 1 095 ? cluster.getRegionServer(1) 096 : cluster.getRegionServer(0); 097 assertTrue(regionServingRS.getBlockCache().isPresent()); 098 099 // wait for running prefetch threads to be completed. 100 Waiter.waitFor(this.conf, 200, () -> PrefetchExecutor.getPrefetchFutures().isEmpty()); 101 102 long oldUsedCacheSize = 103 regionServingRS.getBlockCache().get().getBlockCaches()[1].getCurrentSize(); 104 assertNotEquals(0, oldUsedCacheSize); 105 106 Admin admin = TEST_UTIL.getAdmin(); 107 RegionInfo regionToMove = regionServingRS.getRegions(tableRegionMove).get(0).getRegionInfo(); 108 admin.move(regionToMove.getEncodedNameAsBytes(), 109 TEST_UTIL.getOtherRegionServer(regionServingRS).getServerName()); 110 assertEquals(0, regionServingRS.getRegions(tableRegionMove).size()); 111 112 long newUsedCacheSize = 113 regionServingRS.getBlockCache().get().getBlockCaches()[1].getCurrentSize(); 114 assertTrue(oldUsedCacheSize > newUsedCacheSize); 115 assertEquals(0, regionServingRS.getBlockCache().get().getBlockCaches()[1].getBlockCount()); 116 } 117 118 @Test 119 public void testBlockEvictionOnGracefulStop() throws Exception { 120 // Write to table and flush 121 TableName tableRegionClose = writeDataToTable("testBlockEvictionOnGracefulStop"); 122 123 HRegionServer regionServingRS = 124 cluster.getRegionServer(1).getRegions(tableRegionClose).size() == 1 125 ? cluster.getRegionServer(1) 126 : cluster.getRegionServer(0); 127 128 assertTrue(regionServingRS.getBlockCache().isPresent()); 129 long oldUsedCacheSize = 130 regionServingRS.getBlockCache().get().getBlockCaches()[1].getCurrentSize(); 131 assertNotEquals(0, regionServingRS.getBlockCache().get().getBlockCaches()[1].getBlockCount()); 132 133 cluster.stopRegionServer(regionServingRS.getServerName()); 134 Thread.sleep(500); 135 cluster.startRegionServer(); 136 Thread.sleep(500); 137 138 regionServingRS.getBlockCache().get().waitForCacheInitialization(10000); 139 long newUsedCacheSize = 140 regionServingRS.getBlockCache().get().getBlockCaches()[1].getCurrentSize(); 141 assertEquals(oldUsedCacheSize, newUsedCacheSize); 142 assertNotEquals(0, regionServingRS.getBlockCache().get().getBlockCaches()[1].getBlockCount()); 143 } 144 145 public TableName writeDataToTable(String testName) throws IOException, InterruptedException { 146 TableName tableName = TableName.valueOf(testName + EnvironmentEdgeManager.currentTime()); 147 byte[] row0 = Bytes.toBytes("row1"); 148 byte[] row1 = Bytes.toBytes("row2"); 149 byte[] family = Bytes.toBytes("family"); 150 byte[] qf1 = Bytes.toBytes("qf1"); 151 byte[] qf2 = Bytes.toBytes("qf2"); 152 byte[] value1 = Bytes.toBytes("value1"); 153 byte[] value2 = Bytes.toBytes("value2"); 154 155 TableDescriptor td = TableDescriptorBuilder.newBuilder(tableName) 156 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build(); 157 Table table = TEST_UTIL.createTable(td, null); 158 try { 159 // put data 160 Put put0 = new Put(row0); 161 put0.addColumn(family, qf1, 1, value1); 162 table.put(put0); 163 Put put1 = new Put(row1); 164 put1.addColumn(family, qf2, 1, value2); 165 table.put(put1); 166 TEST_UTIL.flush(tableName); 167 } finally { 168 Thread.sleep(1000); 169 } 170 assertEquals(1, cluster.getRegions(tableName).size()); 171 return tableName; 172 } 173 174 @AfterEach 175 public void tearDown() throws Exception { 176 TEST_UTIL.shutdownMiniCluster(); 177 TEST_UTIL.cleanupDataTestDirOnTestFS(String.valueOf(testDir)); 178 if (zkCluster != null) { 179 zkCluster.shutdown(); 180 } 181 } 182}