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.regionserver; 019 020import static org.junit.Assert.assertEquals; 021 022import java.util.ArrayList; 023import java.util.List; 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.HBaseTestingUtil; 026import org.apache.hadoop.hbase.HConstants; 027import org.apache.hadoop.hbase.TableName; 028import org.apache.hadoop.hbase.client.Admin; 029import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 030import org.apache.hadoop.hbase.client.Put; 031import org.apache.hadoop.hbase.client.Scan; 032import org.apache.hadoop.hbase.client.Table; 033import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 034import org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner; 035import org.apache.hadoop.hbase.regionserver.compactions.CompactionConfiguration; 036import org.apache.hadoop.hbase.testclassification.LargeTests; 037import org.apache.hadoop.hbase.testclassification.RegionServerTests; 038import org.apache.hadoop.hbase.util.Bytes; 039import org.apache.hadoop.hbase.util.JVMClusterUtil; 040import org.junit.After; 041import org.junit.AfterClass; 042import org.junit.Before; 043import org.junit.BeforeClass; 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({ LargeTests.class, RegionServerTests.class }) 051public class TestNotCleanupCompactedFileWhenRegionWarmup { 052 private static final Logger LOG = 053 LoggerFactory.getLogger(TestNotCleanupCompactedFileWhenRegionWarmup.class); 054 055 @ClassRule 056 public static final HBaseClassTestRule CLASS_RULE = 057 HBaseClassTestRule.forClass(TestNotCleanupCompactedFileWhenRegionWarmup.class); 058 059 private static HBaseTestingUtil TEST_UTIL; 060 private static Admin admin; 061 private static Table table; 062 063 private static TableName TABLE_NAME = TableName.valueOf("TestCleanupCompactedFileAfterFailover"); 064 private static byte[] ROW = Bytes.toBytes("row"); 065 private static byte[] FAMILY = Bytes.toBytes("cf"); 066 private static byte[] QUALIFIER = Bytes.toBytes("cq"); 067 private static byte[] VALUE = Bytes.toBytes("value"); 068 069 @BeforeClass 070 public static void beforeClass() throws Exception { 071 TEST_UTIL = new HBaseTestingUtil(); 072 // Set the scanner lease to 20min, so the scanner can't be closed by RegionServer 073 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD, 1200000); 074 TEST_UTIL.getConfiguration().setInt(CompactionConfiguration.HBASE_HSTORE_COMPACTION_MIN_KEY, 075 100); 076 TEST_UTIL.getConfiguration().set("dfs.blocksize", "64000"); 077 TEST_UTIL.getConfiguration().set("dfs.namenode.fs-limits.min-block-size", "1024"); 078 TEST_UTIL.getConfiguration().set(TimeToLiveHFileCleaner.TTL_CONF_KEY, "0"); 079 TEST_UTIL.startMiniCluster(1); 080 admin = TEST_UTIL.getAdmin(); 081 } 082 083 @AfterClass 084 public static void afterClass() throws Exception { 085 TEST_UTIL.shutdownMiniCluster(); 086 } 087 088 @Before 089 public void before() throws Exception { 090 TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TABLE_NAME); 091 builder.setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)); 092 admin.createTable(builder.build()); 093 TEST_UTIL.waitTableAvailable(TABLE_NAME); 094 table = TEST_UTIL.getConnection().getTable(TABLE_NAME); 095 } 096 097 @After 098 public void after() throws Exception { 099 admin.disableTable(TABLE_NAME); 100 admin.deleteTable(TABLE_NAME); 101 } 102 103 @Test 104 public void testRegionWarmup() throws Exception { 105 List<HRegion> regions = new ArrayList<>(); 106 for (JVMClusterUtil.RegionServerThread rsThread : TEST_UTIL.getHBaseCluster() 107 .getLiveRegionServerThreads()) { 108 HRegionServer rs = rsThread.getRegionServer(); 109 if (rs.getOnlineTables().contains(TABLE_NAME)) { 110 regions.addAll(rs.getRegions(TABLE_NAME)); 111 } 112 } 113 assertEquals("Table should only have one region", 1, regions.size()); 114 HRegion region = regions.get(0); 115 HStore store = region.getStore(FAMILY); 116 117 writeDataAndFlush(3, region); 118 assertEquals(3, store.getStorefilesCount()); 119 120 // Open a scanner and not close, then the storefile will be referenced 121 store.getScanner(new Scan(), null, 0); 122 region.compact(true); 123 assertEquals(1, store.getStorefilesCount()); 124 // The compacted file should not be archived as there are references by user scanner 125 assertEquals(3, store.getStoreEngine().getStoreFileManager().getCompactedfiles().size()); 126 127 HStore newStore = region.instantiateHStore(ColumnFamilyDescriptorBuilder.of(FAMILY), true); 128 // Should not archive the compacted storefiles when region warmup 129 assertEquals(4, newStore.getStorefilesCount()); 130 131 newStore = region.instantiateHStore(ColumnFamilyDescriptorBuilder.of(FAMILY), false); 132 // Archived the compacted storefiles when region real open 133 assertEquals(1, newStore.getStorefilesCount()); 134 } 135 136 private void writeDataAndFlush(int fileNum, HRegion region) throws Exception { 137 for (int i = 0; i < fileNum; i++) { 138 for (int j = 0; j < 100; j++) { 139 table.put(new Put(concat(ROW, j)).addColumn(FAMILY, QUALIFIER, concat(VALUE, j))); 140 } 141 region.flush(true); 142 } 143 } 144 145 private byte[] concat(byte[] base, int index) { 146 return Bytes.toBytes(Bytes.toString(base) + "-" + index); 147 } 148}