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.HRegionInfo;
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(),
084        any(), any(), any(),
085        any(), any()))
086        .thenReturn(new ArrayList<>());
087
088    // Produce 3 L0 files.
089    HStoreFile sf = createFile();
090    ArrayList<HStoreFile> compactUs = al(sf, createFile(), createFile());
091    se.getStoreFileManager().loadFiles(compactUs);
092    // Create a compaction that would want to split the stripe.
093    CompactionContext compaction = se.createCompaction();
094    compaction.select(al(), false, false, false);
095    assertEquals(3, compaction.getRequest().getFiles().size());
096    // Override the file list. Granted, overriding this compaction in this manner will
097    // break things in real world, but we only want to verify the override.
098    compactUs.remove(sf);
099    CompactionRequestImpl req = new CompactionRequestImpl(compactUs);
100    compaction.forceSelect(req);
101    assertEquals(2, compaction.getRequest().getFiles().size());
102    assertFalse(compaction.getRequest().getFiles().contains(sf));
103    // Make sure the correct method it called on compactor.
104    compaction.compact(NoLimitThroughputController.INSTANCE, null);
105    verify(mockCompactor, times(1)).compact(compaction.getRequest(), targetCount, 0L,
106      StripeStoreFileManager.OPEN_KEY, StripeStoreFileManager.OPEN_KEY, null, null,
107      NoLimitThroughputController.INSTANCE, null);
108  }
109
110  private static HStoreFile createFile() throws Exception {
111    HStoreFile sf = mock(HStoreFile.class);
112    when(sf.getMetadataValue(any()))
113      .thenReturn(StripeStoreFileManager.INVALID_KEY);
114    when(sf.getReader()).thenReturn(mock(StoreFileReader.class));
115    when(sf.getPath()).thenReturn(new Path("moo"));
116    when(sf.getBulkLoadTimestamp()).thenReturn(OptionalLong.empty());
117    return sf;
118  }
119
120  private static TestStoreEngine createEngine(Configuration conf) throws Exception {
121    HStore store = mock(HStore.class);
122    when(store.getRegionInfo()).thenReturn(HRegionInfo.FIRST_META_REGIONINFO);
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}