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 zkCluster = TEST_UTIL.startMiniZKCluster(); 079 cluster = TEST_UTIL.startMiniHBaseCluster(option); 080 assertEquals(2, cluster.getRegionServerThreads().size()); 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 assertTrue(new File(testDir + "/bucket.persistence").exists()); 116 117 // Stop the RS 118 cluster.stopRegionServer(0); 119 LOG.info("Stopped Region Server 0."); 120 Thread.sleep(1000); 121 assertTrue(new File(testDir + "/bucket.persistence").exists()); 122 123 // Start the RS and validate 124 cluster.startRegionServer(); 125 assertFalse(new File(testDir + "/bucket.persistence").exists()); 126 } 127 128 @After 129 public void tearDown() throws Exception { 130 TEST_UTIL.shutdownMiniCluster(); 131 TEST_UTIL.cleanupDataTestDirOnTestFS(String.valueOf(testDir)); 132 if (zkCluster != null) { 133 zkCluster.shutdown(); 134 } 135 } 136}