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