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.apache.hadoop.hbase.regionserver.DefaultStoreEngine.DEFAULT_COMPACTOR_CLASS_KEY; 021 022import java.io.IOException; 023import java.util.List; 024import java.util.concurrent.atomic.AtomicBoolean; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.fs.Path; 027import org.apache.hadoop.hbase.Cell; 028import org.apache.hadoop.hbase.HBaseClassTestRule; 029import org.apache.hadoop.hbase.HBaseTestingUtil; 030import org.apache.hadoop.hbase.HConstants; 031import org.apache.hadoop.hbase.KeyValue.KeyOnlyKeyValue; 032import org.apache.hadoop.hbase.TableName; 033import org.apache.hadoop.hbase.client.Put; 034import org.apache.hadoop.hbase.client.Table; 035import org.apache.hadoop.hbase.io.ByteBuffAllocator; 036import org.apache.hadoop.hbase.io.hfile.HFileWriterImpl; 037import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequestImpl; 038import org.apache.hadoop.hbase.regionserver.compactions.DefaultCompactor; 039import org.apache.hadoop.hbase.testclassification.MediumTests; 040import org.apache.hadoop.hbase.testclassification.RegionServerTests; 041import org.apache.hadoop.hbase.util.Bytes; 042import org.junit.AfterClass; 043import org.junit.Assert; 044import org.junit.BeforeClass; 045import org.junit.ClassRule; 046import org.junit.Rule; 047import org.junit.Test; 048import org.junit.experimental.categories.Category; 049import org.junit.rules.TestName; 050 051@Category({ RegionServerTests.class, MediumTests.class }) 052public class TestCompactorMemLeak { 053 054 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 055 private static final Configuration CONF = UTIL.getConfiguration(); 056 private static final AtomicBoolean IS_LAST_CELL_ON_HEAP = new AtomicBoolean(false); 057 private static final byte[] FAMILY = Bytes.toBytes("f"); 058 private static final byte[] QUALIFIER = Bytes.toBytes("q"); 059 private static final byte[] VALUE = Bytes.toBytes("value"); 060 061 @Rule 062 public TestName name = new TestName(); 063 064 @ClassRule 065 public static final HBaseClassTestRule CLASS_RULE = 066 HBaseClassTestRule.forClass(TestCompactorMemLeak.class); 067 068 @BeforeClass 069 public static void setUp() throws Exception { 070 IS_LAST_CELL_ON_HEAP.set(false); 071 // Must use the ByteBuffAllocator here 072 CONF.setBoolean(ByteBuffAllocator.ALLOCATOR_POOL_ENABLED_KEY, true); 073 // Must use OFF-HEAP BucketCache here. 074 CONF.setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0.1f); 075 CONF.set(HConstants.BUCKET_CACHE_IOENGINE_KEY, "offheap"); 076 // 32MB for BucketCache. 077 CONF.setFloat(HConstants.BUCKET_CACHE_SIZE_KEY, 32); 078 // Use the MyCompactor 079 CONF.set(DEFAULT_COMPACTOR_CLASS_KEY, MyCompactor.class.getName()); 080 UTIL.startMiniCluster(); 081 } 082 083 @AfterClass 084 public static void tearDown() throws Exception { 085 IS_LAST_CELL_ON_HEAP.set(false); 086 UTIL.shutdownMiniCluster(); 087 } 088 089 private void assertMajorCompactionOK(TableName tableName) { 090 List<HRegion> regions = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer() 091 .getRegions(tableName); 092 Assert.assertEquals(regions.size(), 1); 093 HRegion region = regions.get(0); 094 Assert.assertEquals(region.getStores().size(), 1); 095 HStore store = region.getStore(FAMILY); 096 Assert.assertEquals(store.getStorefilesCount(), 1); 097 } 098 099 @Test 100 public void testMemLeak() throws IOException, InterruptedException { 101 TableName tableName = TableName.valueOf(name.getMethodName()); 102 Table table = UTIL.createTable(tableName, FAMILY); 103 104 // Put and Flush #1 105 Put put = new Put(Bytes.toBytes("row1")).addColumn(FAMILY, QUALIFIER, VALUE); 106 table.put(put); 107 UTIL.getAdmin().flush(tableName); 108 109 // Put and Flush #2 110 put = new Put(Bytes.toBytes("row2")).addColumn(FAMILY, QUALIFIER, VALUE); 111 table.put(put); 112 UTIL.getAdmin().flush(tableName); 113 114 // Major compact 115 UTIL.getAdmin().majorCompact(tableName); 116 Thread.sleep(6000); 117 assertMajorCompactionOK(tableName); 118 119 // The last cell before Compactor#commitWriter must be an heap one. 120 Assert.assertTrue(IS_LAST_CELL_ON_HEAP.get()); 121 } 122 123 public static class MyCompactor extends DefaultCompactor { 124 125 public MyCompactor(Configuration conf, HStore store) { 126 super(conf, store); 127 } 128 129 @Override 130 protected List<Path> commitWriter(StoreFileWriter writer, FileDetails fd, 131 CompactionRequestImpl request) throws IOException { 132 HFileWriterImpl writerImpl = (HFileWriterImpl) writer.getLiveFileWriter(); 133 Cell cell = writerImpl.getLastCell(); 134 // The cell should be backend with an KeyOnlyKeyValue. 135 IS_LAST_CELL_ON_HEAP.set(cell instanceof KeyOnlyKeyValue); 136 return super.commitWriter(writer, fd, request); 137 } 138 } 139 140}