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