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.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023import static org.mockito.ArgumentMatchers.any;
024import static org.mockito.ArgumentMatchers.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;
033import java.util.Set;
034import java.util.stream.Collectors;
035import org.apache.commons.lang3.RandomStringUtils;
036import org.apache.hadoop.fs.FileStatus;
037import org.apache.hadoop.fs.FileSystem;
038import org.apache.hadoop.fs.Path;
039import org.apache.hadoop.hbase.HBaseClassTestRule;
040import org.apache.hadoop.hbase.HBaseTestingUtil;
041import org.apache.hadoop.hbase.TableName;
042import org.apache.hadoop.hbase.client.Connection;
043import org.apache.hadoop.hbase.client.RegionInfo;
044import org.apache.hadoop.hbase.client.RegionInfoBuilder;
045import org.apache.hadoop.hbase.client.TableDescriptor;
046import org.apache.hadoop.hbase.regionserver.HRegion;
047import org.apache.hadoop.hbase.regionserver.HRegionFileSystem;
048import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
049import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerForTest;
050import org.apache.hadoop.hbase.testclassification.SmallTests;
051import org.apache.hadoop.hbase.util.Bytes;
052import org.junit.Before;
053import org.junit.ClassRule;
054import org.junit.Test;
055import org.junit.experimental.categories.Category;
056
057import org.apache.hbase.thirdparty.com.google.common.collect.Iterables;
058import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
059import org.apache.hbase.thirdparty.com.google.common.collect.Sets;
060
061@Category({ SmallTests.class })
062public class TestMajorCompactionRequest {
063  @ClassRule
064  public static final HBaseClassTestRule CLASS_RULE =
065    HBaseClassTestRule.forClass(TestMajorCompactionRequest.class);
066
067  protected static final HBaseTestingUtil UTILITY = new HBaseTestingUtil();
068  protected static final String FAMILY = "a";
069  protected Path rootRegionDir;
070  protected Path regionStoreDir;
071
072  @Before
073  public void setUp() throws Exception {
074    rootRegionDir = UTILITY.getDataTestDirOnTestFS("TestMajorCompactionRequest");
075    regionStoreDir = new Path(rootRegionDir, FAMILY);
076  }
077
078  @Test
079  public void testStoresNeedingCompaction() throws Exception {
080    // store files older than timestamp
081    List<StoreFileInfo> storeFiles = mockStoreFiles(regionStoreDir, 5, 10);
082    MajorCompactionRequest request = makeMockRequest(storeFiles, false);
083    Optional<MajorCompactionRequest> result =
084      request.createRequest(mock(Connection.class), Sets.newHashSet(FAMILY), 100);
085    assertTrue(result.isPresent());
086
087    // store files newer than timestamp
088    storeFiles = mockStoreFiles(regionStoreDir, 5, 101);
089    request = makeMockRequest(storeFiles, false);
090    result = request.createRequest(mock(Connection.class), Sets.newHashSet(FAMILY), 100);
091    assertFalse(result.isPresent());
092  }
093
094  @Test
095  public void testIfWeHaveNewReferenceFilesButOldStoreFiles() throws Exception {
096    // this tests that reference files that are new, but have older timestamps for the files
097    // they reference still will get compacted.
098    TableName tableName = TableName.valueOf("TestMajorCompactor");
099    TableDescriptor htd = UTILITY.createTableDescriptor(tableName, Bytes.toBytes(FAMILY));
100    RegionInfo hri = RegionInfoBuilder.newBuilder(htd.getTableName()).build();
101    HRegion region =
102      HBaseTestingUtil.createRegionAndWAL(hri, rootRegionDir, UTILITY.getConfiguration(), htd);
103
104    Connection connection = mock(Connection.class);
105    // the reference file timestamp is newer
106    List<StoreFileInfo> storeFiles = mockStoreFiles(regionStoreDir, 4, 101);
107    List<Path> paths = storeFiles.stream().map(StoreFileInfo::getPath).collect(Collectors.toList());
108    // the files that are referenced are older, thus we still compact.
109    HRegionFileSystem fileSystem = mockFileSystem(region.getRegionInfo(), true, storeFiles, 50);
110    MajorCompactionRequest majorCompactionRequest =
111      spy(new MajorCompactionRequest(connection, region.getRegionInfo(), Sets.newHashSet(FAMILY)));
112    doReturn(paths).when(majorCompactionRequest).getReferenceFilePaths(any(FileSystem.class),
113      any(Path.class));
114    StoreFileTrackerForTest sft = mockSFT(true, storeFiles);
115    doReturn(fileSystem).when(majorCompactionRequest).getFileSystem();
116    doReturn(sft).when(majorCompactionRequest).getStoreFileTracker(any(), any());
117    doReturn(UTILITY.getConfiguration()).when(connection).getConfiguration();
118    Set<String> result =
119      majorCompactionRequest.getStoresRequiringCompaction(Sets.newHashSet("a"), 100);
120    assertEquals(FAMILY, Iterables.getOnlyElement(result));
121  }
122
123  protected StoreFileTrackerForTest mockSFT(boolean references, List<StoreFileInfo> storeFiles)
124    throws IOException {
125    StoreFileTrackerForTest sft = mock(StoreFileTrackerForTest.class);
126    doReturn(references).when(sft).hasReferences();
127    doReturn(storeFiles).when(sft).load();
128    return sft;
129  }
130
131  protected HRegionFileSystem mockFileSystem(RegionInfo info, boolean hasReferenceFiles,
132    List<StoreFileInfo> storeFiles) throws IOException {
133    long timestamp = storeFiles.stream().findFirst().get().getModificationTime();
134    return mockFileSystem(info, hasReferenceFiles, storeFiles, timestamp);
135  }
136
137  private HRegionFileSystem mockFileSystem(RegionInfo info, boolean hasReferenceFiles,
138    List<StoreFileInfo> storeFiles, long referenceFileTimestamp) throws IOException {
139    FileSystem fileSystem = mock(FileSystem.class);
140    if (hasReferenceFiles) {
141      FileStatus fileStatus = mock(FileStatus.class);
142      doReturn(referenceFileTimestamp).when(fileStatus).getModificationTime();
143      doReturn(fileStatus).when(fileSystem).getFileLinkStatus(isA(Path.class));
144    }
145    HRegionFileSystem mockSystem = mock(HRegionFileSystem.class);
146    doReturn(info).when(mockSystem).getRegionInfo();
147    doReturn(regionStoreDir).when(mockSystem).getStoreDir(FAMILY);
148    doReturn(fileSystem).when(mockSystem).getFileSystem();
149    return mockSystem;
150  }
151
152  protected List<StoreFileInfo> mockStoreFiles(Path regionStoreDir, int howMany, long timestamp)
153    throws IOException {
154    List<StoreFileInfo> infos = Lists.newArrayList();
155    int i = 0;
156    while (i < howMany) {
157      StoreFileInfo storeFileInfo = mock(StoreFileInfo.class);
158      doReturn(timestamp).doReturn(timestamp).when(storeFileInfo).getModificationTime();
159      doReturn(new Path(regionStoreDir, RandomStringUtils.randomAlphabetic(10))).when(storeFileInfo)
160        .getPath();
161      infos.add(storeFileInfo);
162      i++;
163    }
164    return infos;
165  }
166
167  private MajorCompactionRequest makeMockRequest(List<StoreFileInfo> storeFiles, boolean references)
168    throws IOException {
169    Connection connection = mock(Connection.class);
170    RegionInfo regionInfo = mock(RegionInfo.class);
171    when(regionInfo.getEncodedName()).thenReturn("HBase");
172    when(regionInfo.getTable()).thenReturn(TableName.valueOf("foo"));
173    MajorCompactionRequest request =
174      new MajorCompactionRequest(connection, regionInfo, Sets.newHashSet("a"));
175    MajorCompactionRequest spy = spy(request);
176    HRegionFileSystem fileSystem = mockFileSystem(regionInfo, references, storeFiles);
177    StoreFileTrackerForTest sft = mockSFT(references, storeFiles);
178    doReturn(sft).when(spy).getStoreFileTracker(any(), any());
179    doReturn(fileSystem).when(spy).getFileSystem();
180    return spy;
181  }
182}