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