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