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 java.io.IOException;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.fs.FileStatus;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
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.jupiter.api.AfterAll;
037import org.junit.jupiter.api.AfterEach;
038import org.junit.jupiter.api.BeforeAll;
039import org.junit.jupiter.api.BeforeEach;
040import org.junit.jupiter.api.Tag;
041import org.junit.jupiter.api.Test;
042
043@Tag(MediumTests.TAG)
044@Tag(RegionServerTests.TAG)
045public class TestCompactionToolNpeFix {
046
047  private static final HBaseTestingUtil TESTUTIL = new HBaseTestingUtil();
048
049  private HRegion region;
050  private final static byte[] qualifier = Bytes.toBytes("qf");
051  private static Path rootDir;
052  private final TableName tableName = TableName.valueOf(getClass().getSimpleName());
053
054  @BeforeAll
055  public static void setUpAfterClass() throws Exception {
056    TESTUTIL.getConfiguration().setBoolean(MemStoreLAB.USEMSLAB_KEY, false);
057    TESTUTIL.startMiniCluster();
058    rootDir = TESTUTIL.getDefaultRootDirPath();
059    TESTUTIL.startMiniMapReduceCluster();
060
061  }
062
063  @AfterAll
064  public static void tearDown() throws Exception {
065    TESTUTIL.shutdownMiniMapReduceCluster();
066    TESTUTIL.shutdownMiniCluster();
067    TESTUTIL.cleanupTestDir();
068
069  }
070
071  @BeforeEach
072  public void setUp() throws IOException {
073    TESTUTIL.createTable(tableName, HBaseTestingUtil.fam1);
074    this.region = TESTUTIL.getMiniHBaseCluster().getRegions(tableName).get(0);
075  }
076
077  @AfterEach
078  public void after() throws IOException {
079    TESTUTIL.deleteTable(tableName);
080  }
081
082  private void putAndFlush(int key) throws Exception {
083    Put put = new Put(Bytes.toBytes(key));
084    put.addColumn(HBaseTestingUtil.fam1, qualifier, Bytes.toBytes("val" + key));
085    region.put(put);
086    TESTUTIL.flush(tableName);
087  }
088
089  private HStore prepareStoreWithMultiFiles() throws Exception {
090    for (int i = 0; i < 5; i++) {
091      this.putAndFlush(i);
092    }
093    HStore store = region.getStore(HBaseTestingUtil.fam1);
094    assertEquals(5, store.getStorefilesCount());
095    return store;
096  }
097
098  @Test
099  public void testCompactedFilesArchived() throws Exception {
100    HStore store = prepareStoreWithMultiFiles();
101    Path tableDir = CommonFSUtils.getTableDir(rootDir, region.getRegionInfo().getTable());
102    FileSystem fs = store.getFileSystem();
103    String storePath = tableDir + "/" + region.getRegionInfo().getEncodedName() + "/"
104      + Bytes.toString(HBaseTestingUtil.fam1);
105    FileStatus[] regionDirFiles = fs.listStatus(new Path(storePath));
106    assertEquals(5, regionDirFiles.length);
107    String defaultFS = TESTUTIL.getMiniHBaseCluster().getConfiguration().get("fs.defaultFS");
108    Configuration config = HBaseConfiguration.create();
109    config.set("fs.defaultFS", defaultFS);
110    int result = ToolRunner.run(config, new CompactionTool(),
111      new String[] { "-compactOnce", "-major", storePath });
112    assertEquals(0, result);
113    regionDirFiles = fs.listStatus(new Path(storePath));
114    assertEquals(1, regionDirFiles.length);
115  }
116
117  @Test
118  public void testCompactedFilesArchivedMapRed() throws Exception {
119    HStore store = prepareStoreWithMultiFiles();
120    Path tableDir = CommonFSUtils.getTableDir(rootDir, region.getRegionInfo().getTable());
121    FileSystem fs = store.getFileSystem();
122    String storePath = tableDir + "/" + region.getRegionInfo().getEncodedName() + "/"
123      + Bytes.toString(HBaseTestingUtil.fam1);
124    FileStatus[] regionDirFiles = fs.listStatus(new Path(storePath));
125    assertEquals(5, regionDirFiles.length);
126    String defaultFS = TESTUTIL.getMiniHBaseCluster().getConfiguration().get("fs.defaultFS");
127    Configuration config = HBaseConfiguration.create(TESTUTIL.getConfiguration());
128    config.setBoolean(MemStoreLAB.USEMSLAB_KEY, true);
129    config.set("fs.defaultFS", defaultFS);
130    int result = ToolRunner.run(config, new CompactionTool(),
131      new String[] { "-compactOnce", "-mapred", "-major", storePath });
132    assertEquals(0, result);
133    regionDirFiles = fs.listStatus(new Path(storePath));
134    assertEquals(1, regionDirFiles.length);
135  }
136}