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.Matchers.anyString;
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.HBaseTestingUtility;
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.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 HBaseTestingUtility UTILITY = new HBaseTestingUtility();
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 table = TableName.valueOf("TestMajorCompactor");
099    TableDescriptor htd = UTILITY.createTableDescriptor(table, Bytes.toBytes(FAMILY));
100    RegionInfo hri = RegionInfoBuilder.newBuilder(htd.getTableName()).build();
101    HRegion region =
102      HBaseTestingUtility.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    doReturn(fileSystem).when(majorCompactionRequest).getFileSystem();
115    Set<String> result =
116      majorCompactionRequest.getStoresRequiringCompaction(Sets.newHashSet("a"), 100);
117    assertEquals(FAMILY, Iterables.getOnlyElement(result));
118  }
119
120  protected HRegionFileSystem mockFileSystem(RegionInfo info, boolean hasReferenceFiles,
121    List<StoreFileInfo> storeFiles) throws IOException {
122    long timestamp = storeFiles.stream().findFirst().get().getModificationTime();
123    return mockFileSystem(info, hasReferenceFiles, storeFiles, timestamp);
124  }
125
126  private HRegionFileSystem mockFileSystem(RegionInfo info, boolean hasReferenceFiles,
127    List<StoreFileInfo> storeFiles, long referenceFileTimestamp) throws IOException {
128    FileSystem fileSystem = mock(FileSystem.class);
129    if (hasReferenceFiles) {
130      FileStatus fileStatus = mock(FileStatus.class);
131      doReturn(referenceFileTimestamp).when(fileStatus).getModificationTime();
132      doReturn(fileStatus).when(fileSystem).getFileLinkStatus(isA(Path.class));
133    }
134    HRegionFileSystem mockSystem = mock(HRegionFileSystem.class);
135    doReturn(info).when(mockSystem).getRegionInfo();
136    doReturn(regionStoreDir).when(mockSystem).getStoreDir(FAMILY);
137    doReturn(hasReferenceFiles).when(mockSystem).hasReferences(anyString());
138    doReturn(storeFiles).when(mockSystem).getStoreFiles(anyString());
139    doReturn(fileSystem).when(mockSystem).getFileSystem();
140    return mockSystem;
141  }
142
143  protected List<StoreFileInfo> mockStoreFiles(Path regionStoreDir, int howMany, long timestamp)
144    throws IOException {
145    List<StoreFileInfo> infos = Lists.newArrayList();
146    int i = 0;
147    while (i < howMany) {
148      StoreFileInfo storeFileInfo = mock(StoreFileInfo.class);
149      doReturn(timestamp).doReturn(timestamp).when(storeFileInfo).getModificationTime();
150      doReturn(new Path(regionStoreDir, RandomStringUtils.randomAlphabetic(10))).when(storeFileInfo)
151        .getPath();
152      infos.add(storeFileInfo);
153      i++;
154    }
155    return infos;
156  }
157
158  private MajorCompactionRequest makeMockRequest(List<StoreFileInfo> storeFiles, boolean references)
159    throws IOException {
160    Connection connection = mock(Connection.class);
161    RegionInfo regionInfo = mock(RegionInfo.class);
162    when(regionInfo.getEncodedName()).thenReturn("HBase");
163    when(regionInfo.getTable()).thenReturn(TableName.valueOf("foo"));
164    MajorCompactionRequest request =
165      new MajorCompactionRequest(connection, regionInfo, Sets.newHashSet("a"));
166    MajorCompactionRequest spy = spy(request);
167    HRegionFileSystem fileSystem = mockFileSystem(regionInfo, references, storeFiles);
168    doReturn(fileSystem).when(spy).getFileSystem();
169    return spy;
170  }
171}