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