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