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;
021import static org.junit.jupiter.api.Assertions.assertEquals;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.io.IOException;
025import java.util.List;
026import java.util.concurrent.atomic.AtomicBoolean;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.fs.Path;
029import org.apache.hadoop.hbase.Cell;
030import org.apache.hadoop.hbase.HBaseTestingUtil;
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.jupiter.api.AfterAll;
044import org.junit.jupiter.api.BeforeAll;
045import org.junit.jupiter.api.BeforeEach;
046import org.junit.jupiter.api.Tag;
047import org.junit.jupiter.api.Test;
048import org.junit.jupiter.api.TestInfo;
049
050@Tag(RegionServerTests.TAG)
051@Tag(MediumTests.TAG)
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  private String name;
061
062  @BeforeEach
063  public void setTestName(TestInfo testInfo) {
064    this.name = testInfo.getTestMethod().get().getName();
065  }
066
067  @BeforeAll
068  public static void setUp() throws Exception {
069    IS_LAST_CELL_ON_HEAP.set(false);
070    // Must use the ByteBuffAllocator here
071    CONF.setBoolean(ByteBuffAllocator.ALLOCATOR_POOL_ENABLED_KEY, true);
072    // Must use OFF-HEAP BucketCache here.
073    CONF.setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0.1f);
074    CONF.set(HConstants.BUCKET_CACHE_IOENGINE_KEY, "offheap");
075    // 32MB for BucketCache.
076    CONF.setFloat(HConstants.BUCKET_CACHE_SIZE_KEY, 32);
077    // Use the MyCompactor
078    CONF.set(DEFAULT_COMPACTOR_CLASS_KEY, MyCompactor.class.getName());
079    UTIL.startMiniCluster();
080  }
081
082  @AfterAll
083  public static void tearDown() throws Exception {
084    IS_LAST_CELL_ON_HEAP.set(false);
085    UTIL.shutdownMiniCluster();
086  }
087
088  private void assertMajorCompactionOK(TableName tableName) {
089    List<HRegion> regions = UTIL.getHBaseCluster().getRegionServerThreads().get(0).getRegionServer()
090      .getRegions(tableName);
091    assertEquals(1, regions.size());
092    HRegion region = regions.get(0);
093    assertEquals(1, region.getStores().size());
094    HStore store = region.getStore(FAMILY);
095    assertEquals(1, store.getStorefilesCount());
096  }
097
098  @Test
099  public void testMemLeak() throws IOException, InterruptedException {
100    TableName tableName = TableName.valueOf(name);
101    Table table = UTIL.createTable(tableName, FAMILY);
102
103    // Put and Flush #1
104    Put put = new Put(Bytes.toBytes("row1")).addColumn(FAMILY, QUALIFIER, VALUE);
105    table.put(put);
106    UTIL.getAdmin().flush(tableName);
107
108    // Put and Flush #2
109    put = new Put(Bytes.toBytes("row2")).addColumn(FAMILY, QUALIFIER, VALUE);
110    table.put(put);
111    UTIL.getAdmin().flush(tableName);
112
113    // Major compact
114    UTIL.getAdmin().majorCompact(tableName);
115    Thread.sleep(6000);
116    assertMajorCompactionOK(tableName);
117
118    // The last cell before Compactor#commitWriter must be an heap one.
119    assertTrue(IS_LAST_CELL_ON_HEAP.get());
120  }
121
122  public static class MyCompactor extends DefaultCompactor {
123
124    public MyCompactor(Configuration conf, HStore store) {
125      super(conf, store);
126    }
127
128    @Override
129    protected List<Path> commitWriter(StoreFileWriter writer, FileDetails fd,
130      CompactionRequestImpl request) throws IOException {
131      HFileWriterImpl writerImpl = (HFileWriterImpl) writer.getLiveFileWriter();
132      Cell cell = writerImpl.getLastCell();
133      // The cell should be backend with an KeyOnlyKeyValue.
134      IS_LAST_CELL_ON_HEAP.set(cell instanceof KeyOnlyKeyValue);
135      return super.commitWriter(writer, fd, request);
136    }
137  }
138}