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