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