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