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 */ 018 019package org.apache.hadoop.hbase.util.compaction; 020 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertTrue; 023import static org.mockito.Matchers.eq; 024import static org.mockito.Matchers.isA; 025import static org.mockito.Mockito.doReturn; 026import static org.mockito.Mockito.mock; 027import static org.mockito.Mockito.spy; 028import static org.mockito.Mockito.when; 029 030import java.io.IOException; 031import java.util.List; 032import java.util.Optional; 033 034import org.apache.hadoop.conf.Configuration; 035import org.apache.hadoop.fs.Path; 036import org.apache.hadoop.hbase.HBaseClassTestRule; 037import org.apache.hadoop.hbase.TableName; 038import org.apache.hadoop.hbase.client.Connection; 039import org.apache.hadoop.hbase.client.RegionInfo; 040import org.apache.hadoop.hbase.regionserver.HRegionFileSystem; 041import org.apache.hadoop.hbase.regionserver.StoreFileInfo; 042import org.apache.hadoop.hbase.testclassification.SmallTests; 043import org.junit.Before; 044import org.junit.ClassRule; 045import org.junit.Test; 046import org.junit.experimental.categories.Category; 047import org.apache.hbase.thirdparty.com.google.common.collect.Lists; 048import org.apache.hbase.thirdparty.com.google.common.collect.Sets; 049 050@Category({SmallTests.class}) 051public class TestMajorCompactionTTLRequest extends TestMajorCompactionRequest { 052 @ClassRule 053 public static final HBaseClassTestRule CLASS_RULE = 054 HBaseClassTestRule.forClass(TestMajorCompactionTTLRequest.class); 055 056 @Before 057 @Override 058 public void setUp() throws Exception { 059 rootRegionDir = UTILITY.getDataTestDirOnTestFS("TestMajorCompactionTTLRequest"); 060 regionStoreDir = new Path(rootRegionDir, FAMILY); 061 } 062 063 @Test 064 public void testStoresNeedingCompaction() throws Exception { 065 // store files older than timestamp 10 066 List<StoreFileInfo> storeFiles1 = mockStoreFiles(regionStoreDir, 5, 10); 067 // store files older than timestamp 100 068 List<StoreFileInfo> storeFiles2 = mockStoreFiles(regionStoreDir, 5, 100); 069 List<StoreFileInfo> storeFiles = Lists.newArrayList(storeFiles1); 070 storeFiles.addAll(storeFiles2); 071 072 MajorCompactionTTLRequest request = makeMockRequest(storeFiles); 073 // All files are <= 100, so region should not be compacted. 074 Optional<MajorCompactionRequest> result = 075 request.createRequest(mock(Configuration.class), Sets.newHashSet(FAMILY), 10); 076 assertFalse(result.isPresent()); 077 078 // All files are <= 100, so region should not be compacted yet. 079 result = request.createRequest(mock(Configuration.class), Sets.newHashSet(FAMILY), 100); 080 assertFalse(result.isPresent()); 081 082 // All files are <= 100, so they should be considered for compaction 083 result = request.createRequest(mock(Configuration.class), Sets.newHashSet(FAMILY), 101); 084 assertTrue(result.isPresent()); 085 } 086 087 private MajorCompactionTTLRequest makeMockRequest(List<StoreFileInfo> storeFiles) 088 throws IOException { 089 Configuration configuration = mock(Configuration.class); 090 RegionInfo regionInfo = mock(RegionInfo.class); 091 when(regionInfo.getEncodedName()).thenReturn("HBase"); 092 when(regionInfo.getTable()).thenReturn(TableName.valueOf("foo")); 093 MajorCompactionTTLRequest request = new MajorCompactionTTLRequest(configuration, regionInfo); 094 MajorCompactionTTLRequest spy = spy(request); 095 HRegionFileSystem fileSystem = mockFileSystem(regionInfo, false, storeFiles); 096 doReturn(fileSystem).when(spy).getFileSystem(isA(Connection.class)); 097 doReturn(mock(Connection.class)).when(spy).getConnection(eq(configuration)); 098 return spy; 099 } 100}