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.junit.jupiter.api.Assertions.assertEquals;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.fs.FileStatus;
024import org.apache.hadoop.fs.FileSystem;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.HBaseConfiguration;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.Put;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.apache.hadoop.hbase.testclassification.RegionServerTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.apache.hadoop.hbase.util.CommonFSUtils;
034import org.apache.hadoop.util.ToolRunner;
035import org.junit.jupiter.api.AfterEach;
036import org.junit.jupiter.api.BeforeEach;
037import org.junit.jupiter.api.Tag;
038import org.junit.jupiter.api.Test;
039
040@Tag(MediumTests.TAG)
041@Tag(RegionServerTests.TAG)
042public class TestCompactionTool {
043
044  private final HBaseTestingUtil testUtil = new HBaseTestingUtil();
045
046  private HRegion region;
047  private final static byte[] qualifier = Bytes.toBytes("qf");
048  private Path rootDir;
049  private final TableName tableName = TableName.valueOf(getClass().getSimpleName());
050
051  @BeforeEach
052  public void setUp() throws Exception {
053    this.testUtil.startMiniCluster();
054    testUtil.createTable(tableName, HBaseTestingUtil.fam1);
055    rootDir = testUtil.getDefaultRootDirPath();
056    this.region = testUtil.getMiniHBaseCluster().getRegions(tableName).get(0);
057  }
058
059  @AfterEach
060  public void tearDown() throws Exception {
061    this.testUtil.shutdownMiniCluster();
062    testUtil.cleanupTestDir();
063  }
064
065  @Test
066  public void testCompactedFilesArchived() throws Exception {
067    for (int i = 0; i < 10; i++) {
068      this.putAndFlush(i);
069    }
070    HStore store = region.getStore(HBaseTestingUtil.fam1);
071    assertEquals(10, store.getStorefilesCount());
072    Path tableDir = CommonFSUtils.getTableDir(rootDir, region.getRegionInfo().getTable());
073    FileSystem fs = store.getFileSystem();
074    String storePath = tableDir + "/" + region.getRegionInfo().getEncodedName() + "/"
075      + Bytes.toString(HBaseTestingUtil.fam1);
076    FileStatus[] regionDirFiles = fs.listStatus(new Path(storePath));
077    assertEquals(10, regionDirFiles.length);
078    String defaultFS = testUtil.getMiniHBaseCluster().getConfiguration().get("fs.defaultFS");
079    Configuration config = HBaseConfiguration.create();
080    config.set("fs.defaultFS", defaultFS);
081    int result = ToolRunner.run(config, new CompactionTool(),
082      new String[] { "-compactOnce", "-major", storePath });
083    assertEquals(0, result);
084    regionDirFiles = fs.listStatus(new Path(storePath));
085    assertEquals(1, regionDirFiles.length);
086  }
087
088  private void putAndFlush(int key) throws Exception {
089    Put put = new Put(Bytes.toBytes(key));
090    put.addColumn(HBaseTestingUtil.fam1, qualifier, Bytes.toBytes("val" + key));
091    region.put(put);
092    region.flush(true);
093  }
094
095}