View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  import static org.mockito.Matchers.any;
24  import static org.mockito.Matchers.anyInt;
25  import static org.mockito.Matchers.anyLong;
26  import static org.mockito.Mockito.mock;
27  import static org.mockito.Mockito.times;
28  import static org.mockito.Mockito.verify;
29  import static org.mockito.Mockito.when;
30  
31  import java.util.ArrayList;
32  import java.util.Arrays;
33  
34  import org.apache.hadoop.conf.Configuration;
35  import org.apache.hadoop.fs.Path;
36  import org.apache.hadoop.hbase.HBaseConfiguration;
37  import org.apache.hadoop.hbase.KeyValue.KVComparator;
38  import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext;
39  import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequest;
40  import org.apache.hadoop.hbase.regionserver.compactions.CompactionThroughputController;
41  import org.apache.hadoop.hbase.regionserver.compactions.NoLimitCompactionThroughputController;
42  import org.apache.hadoop.hbase.regionserver.compactions.StripeCompactionPolicy;
43  import org.apache.hadoop.hbase.regionserver.compactions.StripeCompactor;
44  import org.apache.hadoop.hbase.testclassification.SmallTests;
45  import org.junit.Test;
46  import org.junit.experimental.categories.Category;
47  
48  @Category(SmallTests.class)
49  public class TestStripeStoreEngine {
50  
51    @Test
52    public void testCreateBasedOnConfig() throws Exception {
53      Configuration conf = HBaseConfiguration.create();
54      conf.set(StoreEngine.STORE_ENGINE_CLASS_KEY, TestStoreEngine.class.getName());
55      StripeStoreEngine se = createEngine(conf);
56      assertTrue(se.getCompactionPolicy() instanceof StripeCompactionPolicy);
57    }
58  
59    public static class TestStoreEngine extends StripeStoreEngine {
60      public void setCompactorOverride(StripeCompactor compactorOverride) {
61        this.compactor = compactorOverride;
62      }
63    }
64  
65    @Test
66    public void testCompactionContextForceSelect() throws Exception {
67      Configuration conf = HBaseConfiguration.create();
68      int targetCount = 2;
69      conf.setInt(StripeStoreConfig.INITIAL_STRIPE_COUNT_KEY, targetCount);
70      conf.setInt(StripeStoreConfig.MIN_FILES_L0_KEY, 2);
71      conf.set(StoreEngine.STORE_ENGINE_CLASS_KEY, TestStoreEngine.class.getName());
72      TestStoreEngine se = createEngine(conf);
73      StripeCompactor mockCompactor = mock(StripeCompactor.class);
74      se.setCompactorOverride(mockCompactor);
75      when(
76        mockCompactor.compact(any(CompactionRequest.class), anyInt(), anyLong(), any(byte[].class),
77          any(byte[].class), any(byte[].class), any(byte[].class),
78          any(CompactionThroughputController.class))).thenReturn(new ArrayList<Path>());
79  
80      // Produce 3 L0 files.
81      StoreFile sf = createFile();
82      ArrayList<StoreFile> compactUs = al(sf, createFile(), createFile());
83      se.getStoreFileManager().loadFiles(compactUs);
84      // Create a compaction that would want to split the stripe.
85      CompactionContext compaction = se.createCompaction();
86      compaction.select(al(), false, false, false);
87      assertEquals(3, compaction.getRequest().getFiles().size());
88      // Override the file list. Granted, overriding this compaction in this manner will
89      // break things in real world, but we only want to verify the override.
90      compactUs.remove(sf);
91      CompactionRequest req = new CompactionRequest(compactUs);
92      compaction.forceSelect(req);
93      assertEquals(2, compaction.getRequest().getFiles().size());
94      assertFalse(compaction.getRequest().getFiles().contains(sf));
95      // Make sure the correct method it called on compactor.
96      compaction.compact(NoLimitCompactionThroughputController.INSTANCE);
97      verify(mockCompactor, times(1)).compact(compaction.getRequest(), targetCount, 0L,
98        StripeStoreFileManager.OPEN_KEY, StripeStoreFileManager.OPEN_KEY, null, null,
99        NoLimitCompactionThroughputController.INSTANCE);
100   }
101 
102   private static StoreFile createFile() throws Exception {
103     StoreFile sf = mock(StoreFile.class);
104     when(sf.getMetadataValue(any(byte[].class)))
105       .thenReturn(StripeStoreFileManager.INVALID_KEY);
106     when(sf.getReader()).thenReturn(mock(StoreFile.Reader.class));
107     when(sf.getPath()).thenReturn(new Path("moo"));
108     return sf;
109   }
110 
111   private static TestStoreEngine createEngine(Configuration conf) throws Exception {
112     Store store = mock(Store.class);
113     KVComparator kvComparator = mock(KVComparator.class);
114     return (TestStoreEngine)StoreEngine.create(store, conf, kvComparator);
115   }
116 
117   private static ArrayList<StoreFile> al(StoreFile... sfs) {
118     return new ArrayList<StoreFile>(Arrays.asList(sfs));
119   }
120 }