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.junit.jupiter.api.Assertions.assertEquals;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.io.File;
025import java.io.IOException;
026import java.util.ArrayList;
027import java.util.List;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.fs.Path;
030import org.apache.hadoop.hbase.HBaseTestingUtil;
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.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.regionserver.HStoreFile;
043import org.apache.hadoop.hbase.testclassification.IOTests;
044import org.apache.hadoop.hbase.testclassification.LargeTests;
045import org.apache.hadoop.hbase.util.Bytes;
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(LargeTests.TAG)
056public class TestPrefetchRSClose {
057
058  private static final Logger LOG = LoggerFactory.getLogger(TestPrefetchRSClose.class);
059
060  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
061
062  private Configuration conf;
063  Path testDir;
064  MiniZooKeeperCluster zkCluster;
065  SingleProcessHBaseCluster cluster;
066  StartTestingClusterOption option =
067    StartTestingClusterOption.builder().numRegionServers(1).build();
068
069  @BeforeEach
070  public void setup() throws Exception {
071    conf = TEST_UTIL.getConfiguration();
072    testDir = TEST_UTIL.getDataTestDir();
073    TEST_UTIL.getTestFileSystem().mkdirs(testDir);
074
075    conf.setBoolean(CacheConfig.PREFETCH_BLOCKS_ON_OPEN_KEY, true);
076    conf.set(BUCKET_CACHE_IOENGINE_KEY, "file:" + testDir + "/bucket.cache");
077    conf.setInt("hbase.bucketcache.size", 400);
078    conf.set("hbase.bucketcache.persistent.path", testDir + "/bucket.persistence");
079    zkCluster = TEST_UTIL.startMiniZKCluster();
080    cluster = TEST_UTIL.startMiniHBaseCluster(option);
081    cluster.setConf(conf);
082  }
083
084  @Test
085  public void testPrefetchPersistence() throws Exception {
086
087    // Write to table and flush
088    TableName tableName = TableName.valueOf("table1");
089    byte[] row0 = Bytes.toBytes("row1");
090    byte[] row1 = Bytes.toBytes("row2");
091    byte[] family = Bytes.toBytes("family");
092    byte[] qf1 = Bytes.toBytes("qf1");
093    byte[] qf2 = Bytes.toBytes("qf2");
094    byte[] value1 = Bytes.toBytes("value1");
095    byte[] value2 = Bytes.toBytes("value2");
096
097    TableDescriptor td = TableDescriptorBuilder.newBuilder(tableName)
098      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build();
099    Table table = TEST_UTIL.createTable(td, null);
100    try {
101      // put data
102      Put put0 = new Put(row0);
103      put0.addColumn(family, qf1, 1, value1);
104      table.put(put0);
105      Put put1 = new Put(row1);
106      put1.addColumn(family, qf2, 1, value2);
107      table.put(put1);
108      TEST_UTIL.flush(tableName);
109    } finally {
110      Thread.sleep(2000);
111    }
112
113    // Default interval for cache persistence is 1000ms. So after 1000ms, both the persistence files
114    // should exist.
115    HRegionServer regionServingRS = cluster.getRegionServer(0);
116    Admin admin = TEST_UTIL.getAdmin();
117    List<String> cachedFilesList = new ArrayList<>();
118    Waiter.waitFor(conf, 5000, () -> {
119      try {
120        cachedFilesList.addAll(admin.getCachedFilesList(regionServingRS.getServerName()));
121      } catch (IOException e) {
122        // let the test try again
123      }
124      return cachedFilesList.size() > 0;
125    });
126    assertEquals(1, cachedFilesList.size());
127    for (HStoreFile h : regionServingRS.getRegions().get(0).getStores().get(0).getStorefiles()) {
128      assertTrue(cachedFilesList.contains(h.getPath().getName()));
129    }
130
131    // Stop the RS
132    cluster.stopRegionServer(0);
133    LOG.info("Stopped Region Server 0.");
134    Thread.sleep(1000);
135    assertTrue(new File(testDir + "/bucket.persistence").exists());
136  }
137
138  @AfterEach
139  public void tearDown() throws Exception {
140    TEST_UTIL.shutdownMiniCluster();
141    TEST_UTIL.cleanupDataTestDirOnTestFS(String.valueOf(testDir));
142    if (zkCluster != null) {
143      zkCluster.shutdown();
144    }
145  }
146}