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