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