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