001/* 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.regionserver; 020 021import static org.junit.Assert.assertEquals; 022import static org.junit.Assert.assertFalse; 023import static org.junit.Assert.assertNotNull; 024import static org.junit.Assert.assertTrue; 025 026import java.util.Collection; 027 028import org.apache.hadoop.hbase.HBaseClassTestRule; 029import org.apache.hadoop.hbase.HBaseTestingUtility; 030import org.apache.hadoop.hbase.TableName; 031import org.apache.hadoop.hbase.client.Delete; 032import org.apache.hadoop.hbase.client.Get; 033import org.apache.hadoop.hbase.client.HBaseAdmin; 034import org.apache.hadoop.hbase.client.Put; 035import org.apache.hadoop.hbase.client.Result; 036import org.apache.hadoop.hbase.client.ResultScanner; 037import org.apache.hadoop.hbase.client.Scan; 038import org.apache.hadoop.hbase.client.Table; 039import org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner; 040import org.apache.hadoop.hbase.regionserver.compactions.CompactionConfiguration; 041import org.apache.hadoop.hbase.testclassification.MediumTests; 042import org.apache.hadoop.hbase.util.Bytes; 043 044import org.junit.AfterClass; 045import org.junit.BeforeClass; 046import org.junit.ClassRule; 047import org.junit.Test; 048import org.junit.experimental.categories.Category; 049 050@Category({MediumTests.class}) 051public class TestCleanupCompactedFileOnRegionClose { 052 053 @ClassRule 054 public static final HBaseClassTestRule CLASS_RULE = 055 HBaseClassTestRule.forClass(TestCleanupCompactedFileOnRegionClose.class); 056 057 private static HBaseTestingUtility util; 058 059 @BeforeClass 060 public static void beforeClass() throws Exception { 061 util = new HBaseTestingUtility(); 062 util.getConfiguration().setInt(CompactionConfiguration.HBASE_HSTORE_COMPACTION_MIN_KEY,100); 063 util.getConfiguration().set("dfs.blocksize", "64000"); 064 util.getConfiguration().set("dfs.namenode.fs-limits.min-block-size", "1024"); 065 util.getConfiguration().set(TimeToLiveHFileCleaner.TTL_CONF_KEY,"0"); 066 util.startMiniCluster(2); 067 } 068 069 @AfterClass 070 public static void afterclass() throws Exception { 071 util.shutdownMiniCluster(); 072 } 073 074 @Test 075 public void testCleanupOnClose() throws Exception { 076 TableName tableName = TableName.valueOf("testCleanupOnClose"); 077 String familyName = "f"; 078 byte[] familyNameBytes = Bytes.toBytes(familyName); 079 util.createTable(tableName, familyName); 080 081 HBaseAdmin hBaseAdmin = util.getHBaseAdmin(); 082 Table table = util.getConnection().getTable(tableName); 083 084 HRegionServer rs = util.getRSForFirstRegionInTable(tableName); 085 Region region = rs.getRegions(tableName).get(0); 086 087 int refSFCount = 4; 088 for (int i = 0; i < refSFCount; i++) { 089 for (int j = 0; j < refSFCount; j++) { 090 Put put = new Put(Bytes.toBytes(j)); 091 put.addColumn(familyNameBytes, Bytes.toBytes(i), Bytes.toBytes(j)); 092 table.put(put); 093 } 094 util.flush(tableName); 095 } 096 assertEquals(refSFCount, region.getStoreFileList(new byte[][]{familyNameBytes}).size()); 097 098 //add a delete, to test wether we end up with an inconsistency post region close 099 Delete delete = new Delete(Bytes.toBytes(refSFCount-1)); 100 table.delete(delete); 101 util.flush(tableName); 102 assertFalse(table.exists(new Get(Bytes.toBytes(refSFCount-1)))); 103 104 //Create a scanner and keep it open to add references to StoreFileReaders 105 Scan scan = new Scan(); 106 scan.setStopRow(Bytes.toBytes(refSFCount-2)); 107 scan.setCaching(1); 108 ResultScanner scanner = table.getScanner(scan); 109 Result res = scanner.next(); 110 assertNotNull(res); 111 assertEquals(refSFCount, res.getFamilyMap(familyNameBytes).size()); 112 113 114 //Verify the references 115 int count = 0; 116 for (HStoreFile sf : (Collection<HStoreFile>)region.getStore(familyNameBytes).getStorefiles()) { 117 synchronized (sf) { 118 if (count < refSFCount) { 119 assertTrue(sf.isReferencedInReads()); 120 } else { 121 assertFalse(sf.isReferencedInReads()); 122 } 123 } 124 count++; 125 } 126 127 //Major compact to produce compacted storefiles that need to be cleaned up 128 util.compact(tableName, true); 129 assertEquals(1, region.getStoreFileList(new byte[][]{familyNameBytes}).size()); 130 assertEquals(refSFCount+1, 131 ((HStore)region.getStore(familyNameBytes)).getStoreEngine().getStoreFileManager() 132 .getCompactedfiles().size()); 133 134 //close then open the region to determine wether compacted storefiles get cleaned up on close 135 hBaseAdmin.unassign(region.getRegionInfo().getRegionName(), false); 136 hBaseAdmin.assign(region.getRegionInfo().getRegionName()); 137 util.waitUntilNoRegionsInTransition(10000); 138 139 140 assertFalse("Deleted row should not exist", 141 table.exists(new Get(Bytes.toBytes(refSFCount-1)))); 142 143 rs = util.getRSForFirstRegionInTable(tableName); 144 region = rs.getRegions(tableName).get(0); 145 assertEquals(1, region.getStoreFileList(new byte[][]{familyNameBytes}).size()); 146 assertEquals(0, 147 ((HStore)region.getStore(familyNameBytes)).getStoreEngine().getStoreFileManager() 148 .getCompactedfiles().size()); 149 } 150}