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