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.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertFalse; 022import static org.junit.jupiter.api.Assertions.assertTrue; 023import static org.mockito.ArgumentMatchers.any; 024import static org.mockito.ArgumentMatchers.anyInt; 025import static org.mockito.ArgumentMatchers.anyLong; 026import static org.mockito.Mockito.mock; 027import static org.mockito.Mockito.times; 028import static org.mockito.Mockito.verify; 029import static org.mockito.Mockito.when; 030 031import java.util.ArrayList; 032import java.util.Arrays; 033import java.util.OptionalLong; 034import org.apache.hadoop.conf.Configuration; 035import org.apache.hadoop.fs.Path; 036import org.apache.hadoop.hbase.CellComparatorImpl; 037import org.apache.hadoop.hbase.HBaseConfiguration; 038import org.apache.hadoop.hbase.client.RegionInfoBuilder; 039import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext; 040import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequestImpl; 041import org.apache.hadoop.hbase.regionserver.compactions.StripeCompactionPolicy; 042import org.apache.hadoop.hbase.regionserver.compactions.StripeCompactor; 043import org.apache.hadoop.hbase.regionserver.throttle.NoLimitThroughputController; 044import org.apache.hadoop.hbase.testclassification.RegionServerTests; 045import org.apache.hadoop.hbase.testclassification.SmallTests; 046import org.junit.jupiter.api.Tag; 047import org.junit.jupiter.api.Test; 048 049@Tag(RegionServerTests.TAG) 050@Tag(SmallTests.TAG) 051public class TestStripeStoreEngine { 052 053 @Test 054 public void testCreateBasedOnConfig() throws Exception { 055 Configuration conf = HBaseConfiguration.create(); 056 conf.set(StoreEngine.STORE_ENGINE_CLASS_KEY, TestStoreEngine.class.getName()); 057 StripeStoreEngine se = createEngine(conf); 058 assertTrue(se.getCompactionPolicy() instanceof StripeCompactionPolicy); 059 } 060 061 public static class TestStoreEngine extends StripeStoreEngine { 062 public void setCompactorOverride(StripeCompactor compactorOverride) { 063 this.compactor = compactorOverride; 064 } 065 } 066 067 @Test 068 public void testCompactionContextForceSelect() throws Exception { 069 Configuration conf = HBaseConfiguration.create(); 070 int targetCount = 2; 071 conf.setInt(StripeStoreConfig.INITIAL_STRIPE_COUNT_KEY, targetCount); 072 conf.setInt(StripeStoreConfig.MIN_FILES_L0_KEY, 2); 073 conf.set(StoreEngine.STORE_ENGINE_CLASS_KEY, TestStoreEngine.class.getName()); 074 TestStoreEngine se = createEngine(conf); 075 StripeCompactor mockCompactor = mock(StripeCompactor.class); 076 se.setCompactorOverride(mockCompactor); 077 when( 078 mockCompactor.compact(any(), anyInt(), anyLong(), any(), any(), any(), any(), any(), any())) 079 .thenReturn(new ArrayList<>()); 080 081 // Produce 3 L0 files. 082 HStoreFile sf = createFile(); 083 ArrayList<HStoreFile> compactUs = al(sf, createFile(), createFile()); 084 se.getStoreFileManager().loadFiles(compactUs); 085 // Create a compaction that would want to split the stripe. 086 CompactionContext compaction = se.createCompaction(); 087 compaction.select(al(), false, false, false); 088 assertEquals(3, compaction.getRequest().getFiles().size()); 089 // Override the file list. Granted, overriding this compaction in this manner will 090 // break things in real world, but we only want to verify the override. 091 compactUs.remove(sf); 092 CompactionRequestImpl req = new CompactionRequestImpl(compactUs); 093 compaction.forceSelect(req); 094 assertEquals(2, compaction.getRequest().getFiles().size()); 095 assertFalse(compaction.getRequest().getFiles().contains(sf)); 096 // Make sure the correct method it called on compactor. 097 compaction.compact(NoLimitThroughputController.INSTANCE, null); 098 verify(mockCompactor, times(1)).compact(compaction.getRequest(), targetCount, 0L, 099 StripeStoreFileManager.OPEN_KEY, StripeStoreFileManager.OPEN_KEY, null, null, 100 NoLimitThroughputController.INSTANCE, null); 101 } 102 103 private static HStoreFile createFile() throws Exception { 104 HStoreFile sf = mock(HStoreFile.class); 105 when(sf.getMetadataValue(any())).thenReturn(StripeStoreFileManager.INVALID_KEY); 106 when(sf.getReader()).thenReturn(mock(StoreFileReader.class)); 107 when(sf.getPath()).thenReturn(new Path("moo")); 108 when(sf.getBulkLoadTimestamp()).thenReturn(OptionalLong.empty()); 109 return sf; 110 } 111 112 private static TestStoreEngine createEngine(Configuration conf) throws Exception { 113 HRegion region = mock(HRegion.class); 114 HStore store = mock(HStore.class); 115 store.conf = conf; 116 when(store.getRegionInfo()).thenReturn(RegionInfoBuilder.FIRST_META_REGIONINFO); 117 when(store.getHRegion()).thenReturn(region); 118 CellComparatorImpl kvComparator = mock(CellComparatorImpl.class); 119 return (TestStoreEngine) StoreEngine.create(store, conf, kvComparator); 120 } 121 122 private static ArrayList<HStoreFile> al(HStoreFile... sfs) { 123 return new ArrayList<>(Arrays.asList(sfs)); 124 } 125}