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.util.compaction;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.Admin;
026import org.apache.hadoop.hbase.client.Connection;
027import org.apache.hadoop.hbase.client.Table;
028import org.apache.hadoop.hbase.testclassification.MediumTests;
029import org.apache.hadoop.hbase.testclassification.MiscTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
032import org.junit.jupiter.api.AfterEach;
033import org.junit.jupiter.api.BeforeEach;
034import org.junit.jupiter.api.Tag;
035import org.junit.jupiter.api.Test;
036import org.junit.jupiter.api.TestInfo;
037
038import org.apache.hbase.thirdparty.com.google.common.collect.Sets;
039
040@Tag(MiscTests.TAG)
041@Tag(MediumTests.TAG)
042public class TestMajorCompactor {
043
044  public static final byte[] FAMILY = Bytes.toBytes("a");
045  protected HBaseTestingUtil utility;
046  protected Admin admin;
047
048  @BeforeEach
049  public void setUp() throws Exception {
050    utility = new HBaseTestingUtil();
051    utility.getConfiguration().setInt("hbase.hfile.compaction.discharger.interval", 10);
052    utility.startMiniCluster();
053  }
054
055  @AfterEach
056  public void tearDown() throws Exception {
057    utility.shutdownMiniCluster();
058  }
059
060  @Test
061  public void testCompactingATable(TestInfo testInfo) throws Exception {
062    TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName());
063    utility.createMultiRegionTable(tableName, FAMILY, 5);
064    utility.waitTableAvailable(tableName);
065    Connection connection = utility.getConnection();
066    Table table = connection.getTable(tableName);
067    // write data and flush multiple store files:
068    for (int i = 0; i < 5; i++) {
069      utility.loadRandomRows(table, FAMILY, 50, 100);
070      utility.flush(tableName);
071    }
072    table.close();
073    int numberOfRegions = utility.getAdmin().getRegions(tableName).size();
074    int numHFiles = utility.getNumHFiles(tableName, FAMILY);
075    // we should have a table with more store files than we would before we major compacted.
076    assertTrue(numberOfRegions < numHFiles);
077
078    MajorCompactor compactor = new MajorCompactor(utility.getConfiguration(), tableName,
079      Sets.newHashSet(Bytes.toString(FAMILY)), 1, EnvironmentEdgeManager.currentTime(), 200);
080    compactor.initializeWorkQueues();
081    compactor.compactAllRegions();
082    compactor.shutdown();
083
084    // verify that the store has been completely major compacted.
085    numberOfRegions = utility.getAdmin().getRegions(tableName).size();
086    numHFiles = utility.getNumHFiles(tableName, FAMILY);
087    assertEquals(numHFiles, numberOfRegions);
088  }
089}