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