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