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.regionserver;
019
020import static org.mockito.ArgumentMatchers.any;
021import static org.mockito.ArgumentMatchers.anyInt;
022import static org.mockito.ArgumentMatchers.eq;
023import static org.mockito.ArgumentMatchers.isA;
024import static org.mockito.Mockito.mock;
025import static org.mockito.Mockito.times;
026import static org.mockito.Mockito.verify;
027import static org.mockito.Mockito.when;
028import static org.mockito.hamcrest.MockitoHamcrest.argThat;
029
030import java.io.IOException;
031import java.util.ArrayList;
032import java.util.List;
033
034import org.apache.hadoop.fs.Path;
035import org.apache.hadoop.hbase.HBaseClassTestRule;
036import org.apache.hadoop.hbase.TableName;
037import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
038import org.apache.hadoop.hbase.client.RegionInfo;
039import org.apache.hadoop.hbase.client.RegionInfoBuilder;
040import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
041import org.apache.hadoop.hbase.regionserver.compactions.CompactionLifeCycleTracker;
042import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequester;
043import org.apache.hadoop.hbase.testclassification.SmallTests;
044import org.apache.hadoop.hbase.util.Pair;
045import org.apache.hadoop.hbase.wal.WALEdit;
046import org.apache.hadoop.hbase.wal.WALKeyImpl;
047import org.junit.ClassRule;
048import org.junit.Test;
049import org.junit.experimental.categories.Category;
050import org.mockito.Mockito;
051import org.mockito.invocation.InvocationOnMock;
052import org.mockito.stubbing.Answer;
053
054@Category(SmallTests.class)
055public class TestCompactionAfterBulkLoad extends TestBulkloadBase {
056  private final RegionServerServices regionServerServices = mock(RegionServerServices.class);
057  private final CompactionRequester compactionRequester = mock(CompactSplit.class);
058
059  @ClassRule
060  public static final HBaseClassTestRule CLASS_RULE =
061    HBaseClassTestRule.forClass(TestCompactionAfterBulkLoad.class);
062
063  @Override
064  protected HRegion testRegionWithFamiliesAndSpecifiedTableName(TableName tableName,
065      byte[]... families) throws IOException {
066    RegionInfo hRegionInfo = RegionInfoBuilder.newBuilder(tableName).build();
067    TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
068
069    for (byte[] family : families) {
070      builder.setColumnFamily(ColumnFamilyDescriptorBuilder.of(family));
071    }
072    ChunkCreator.initialize(MemStoreLAB.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null,
073      MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT);
074    // TODO We need a way to do this without creating files
075    return HRegion.createHRegion(hRegionInfo, new Path(testFolder.newFolder().toURI()), conf,
076      builder.build(), log, true, regionServerServices);
077
078  }
079
080  @Test
081  public void shouldRequestCompactionAfterBulkLoad() throws IOException {
082    List<Pair<byte[], String>> familyPaths = new ArrayList<>();
083    // enough hfile to request compaction
084    for (int i = 0; i < 5; i++) {
085      familyPaths.addAll(withFamilyPathsFor(family1, family2, family3));
086    }
087    when(regionServerServices.getConfiguration()).thenReturn(conf);
088    when(regionServerServices.getCompactionRequestor()).thenReturn(compactionRequester);
089    when(log.appendMarker(any(), any(), argThat(bulkLogWalEditType(WALEdit.BULK_LOAD))))
090        .thenAnswer(new Answer() {
091          @Override
092          public Object answer(InvocationOnMock invocation) {
093            WALKeyImpl walKey = invocation.getArgument(1);
094            MultiVersionConcurrencyControl mvcc = walKey.getMvcc();
095            if (mvcc != null) {
096              MultiVersionConcurrencyControl.WriteEntry we = mvcc.begin();
097              walKey.setWriteEntry(we);
098            }
099            return 01L;
100          }
101        });
102
103    Mockito.doNothing().when(compactionRequester).requestCompaction(any(), any(), any(), anyInt(),
104      any(), any());
105    testRegionWithFamilies(family1, family2, family3).bulkLoadHFiles(familyPaths, false, null);
106    // invoke three times for 3 families
107    verify(compactionRequester, times(3)).requestCompaction(isA(HRegion.class), isA(HStore.class),
108      isA(String.class), anyInt(), eq(CompactionLifeCycleTracker.DUMMY), eq(null));
109  }
110}