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.Assert.assertEquals;
022import static org.junit.Assert.assertFalse;
023import static org.junit.Assert.assertTrue;
024
025import java.io.File;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
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.client.ColumnFamilyDescriptorBuilder;
034import org.apache.hadoop.hbase.client.Put;
035import org.apache.hadoop.hbase.client.Table;
036import org.apache.hadoop.hbase.client.TableDescriptor;
037import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
038import org.apache.hadoop.hbase.testclassification.IOTests;
039import org.apache.hadoop.hbase.testclassification.LargeTests;
040import org.apache.hadoop.hbase.util.Bytes;
041import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
042import org.junit.After;
043import org.junit.Before;
044import org.junit.ClassRule;
045import org.junit.Test;
046import org.junit.experimental.categories.Category;
047import org.slf4j.Logger;
048import org.slf4j.LoggerFactory;
049
050@Category({ IOTests.class, LargeTests.class })
051public class TestPrefetchRSClose {
052
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055    HBaseClassTestRule.forClass(TestPrefetchRSClose.class);
056
057  private static final Logger LOG = LoggerFactory.getLogger(TestPrefetchRSClose.class);
058
059  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
060
061  private Configuration conf;
062  Path testDir;
063  MiniZooKeeperCluster zkCluster;
064  SingleProcessHBaseCluster cluster;
065  StartTestingClusterOption option =
066    StartTestingClusterOption.builder().numRegionServers(2).build();
067
068  @Before
069  public void setup() throws Exception {
070    conf = TEST_UTIL.getConfiguration();
071    testDir = TEST_UTIL.getDataTestDir();
072    TEST_UTIL.getTestFileSystem().mkdirs(testDir);
073
074    conf.setBoolean(CacheConfig.PREFETCH_BLOCKS_ON_OPEN_KEY, true);
075    conf.set(BUCKET_CACHE_IOENGINE_KEY, "file:" + testDir + "/bucket.cache");
076    conf.setInt("hbase.bucketcache.size", 400);
077    conf.set("hbase.bucketcache.persistent.path", testDir + "/bucket.persistence");
078    conf.set(CacheConfig.PREFETCH_PERSISTENCE_PATH_KEY, testDir + "/prefetch.persistence");
079    zkCluster = TEST_UTIL.startMiniZKCluster();
080    cluster = TEST_UTIL.startMiniHBaseCluster(option);
081    assertEquals(2, cluster.getRegionServerThreads().size());
082    cluster.setConf(conf);
083  }
084
085  @Test
086  public void testPrefetchPersistence() throws Exception {
087
088    // Write to table and flush
089    TableName tableName = TableName.valueOf("table1");
090    byte[] row0 = Bytes.toBytes("row1");
091    byte[] row1 = Bytes.toBytes("row2");
092    byte[] family = Bytes.toBytes("family");
093    byte[] qf1 = Bytes.toBytes("qf1");
094    byte[] qf2 = Bytes.toBytes("qf2");
095    byte[] value1 = Bytes.toBytes("value1");
096    byte[] value2 = Bytes.toBytes("value2");
097
098    TableDescriptor td = TableDescriptorBuilder.newBuilder(tableName)
099      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build();
100    Table table = TEST_UTIL.createTable(td, null);
101    try {
102      // put data
103      Put put0 = new Put(row0);
104      put0.addColumn(family, qf1, 1, value1);
105      table.put(put0);
106      Put put1 = new Put(row1);
107      put1.addColumn(family, qf2, 1, value2);
108      table.put(put1);
109      TEST_UTIL.flush(tableName);
110    } finally {
111      Thread.sleep(2000);
112    }
113
114    // Default interval for cache persistence is 1000ms. So after 1000ms, both the persistence files
115    // should exist.
116    assertTrue(new File(testDir + "/bucket.persistence").exists());
117    assertTrue(new File(testDir + "/prefetch.persistence").exists());
118
119    // Stop the RS
120    cluster.stopRegionServer(0);
121    LOG.info("Stopped Region Server 0.");
122    Thread.sleep(1000);
123    assertTrue(new File(testDir + "/bucket.persistence").exists());
124    assertTrue(new File(testDir + "/prefetch.persistence").exists());
125
126    // Start the RS and validate
127    cluster.startRegionServer();
128    assertFalse(new File(testDir + "/prefetch.persistence").exists());
129    assertFalse(new File(testDir + "/bucket.persistence").exists());
130  }
131
132  @After
133  public void tearDown() throws Exception {
134    TEST_UTIL.shutdownMiniCluster();
135    TEST_UTIL.cleanupDataTestDirOnTestFS(String.valueOf(testDir));
136    if (zkCluster != null) {
137      zkCluster.shutdown();
138    }
139  }
140}