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.security.User;
45  import org.apache.hadoop.hbase.testclassification.SmallTests;
46  import org.junit.Test;
47  import org.junit.experimental.categories.Category;
48  
49  @Category(SmallTests.class)
50  public class TestStripeStoreEngine {
51  
52    @Test
53    public void testCreateBasedOnConfig() throws Exception {
54      Configuration conf = HBaseConfiguration.create();
55      conf.set(StoreEngine.STORE_ENGINE_CLASS_KEY, TestStoreEngine.class.getName());
56      StripeStoreEngine se = createEngine(conf);
57      assertTrue(se.getCompactionPolicy() instanceof StripeCompactionPolicy);
58    }
59  
60    public static class TestStoreEngine extends StripeStoreEngine {
61      public void setCompactorOverride(StripeCompactor compactorOverride) {
62        this.compactor = compactorOverride;
63      }
64    }
65  
66    @Test
67    public void testCompactionContextForceSelect() throws Exception {
68      Configuration conf = HBaseConfiguration.create();
69      int targetCount = 2;
70      conf.setInt(StripeStoreConfig.INITIAL_STRIPE_COUNT_KEY, targetCount);
71      conf.setInt(StripeStoreConfig.MIN_FILES_L0_KEY, 2);
72      conf.set(StoreEngine.STORE_ENGINE_CLASS_KEY, TestStoreEngine.class.getName());
73      TestStoreEngine se = createEngine(conf);
74      StripeCompactor mockCompactor = mock(StripeCompactor.class);
75      se.setCompactorOverride(mockCompactor);
76      when(
77        mockCompactor.compact(any(CompactionRequest.class), anyInt(), anyLong(), any(byte[].class),
78          any(byte[].class), any(byte[].class), any(byte[].class),
79          any(CompactionThroughputController.class), any(User.class)))
80          .thenReturn(new ArrayList<Path>());
81  
82      // Produce 3 L0 files.
83      StoreFile sf = createFile();
84      ArrayList<StoreFile> compactUs = al(sf, createFile(), createFile());
85      se.getStoreFileManager().loadFiles(compactUs);
86      // Create a compaction that would want to split the stripe.
87      CompactionContext compaction = se.createCompaction();
88      compaction.select(al(), false, false, false);
89      assertEquals(3, compaction.getRequest().getFiles().size());
90      // Override the file list. Granted, overriding this compaction in this manner will
91      // break things in real world, but we only want to verify the override.
92      compactUs.remove(sf);
93      CompactionRequest req = new CompactionRequest(compactUs);
94      compaction.forceSelect(req);
95      assertEquals(2, compaction.getRequest().getFiles().size());
96      assertFalse(compaction.getRequest().getFiles().contains(sf));
97      // Make sure the correct method it called on compactor.
98      compaction.compact(NoLimitCompactionThroughputController.INSTANCE);
99      verify(mockCompactor, times(1)).compact(compaction.getRequest(), targetCount, 0L,
100       StripeStoreFileManager.OPEN_KEY, StripeStoreFileManager.OPEN_KEY, null, null,
101       NoLimitCompactionThroughputController.INSTANCE, null);
102   }
103 
104   private static StoreFile createFile() throws Exception {
105     StoreFile sf = mock(StoreFile.class);
106     when(sf.getMetadataValue(any(byte[].class)))
107       .thenReturn(StripeStoreFileManager.INVALID_KEY);
108     when(sf.getReader()).thenReturn(mock(StoreFile.Reader.class));
109     when(sf.getPath()).thenReturn(new Path("moo"));
110     return sf;
111   }
112 
113   private static TestStoreEngine createEngine(Configuration conf) throws Exception {
114     Store store = mock(Store.class);
115     KVComparator kvComparator = mock(KVComparator.class);
116     return (TestStoreEngine)StoreEngine.create(store, conf, kvComparator);
117   }
118 
119   private static ArrayList<StoreFile> al(StoreFile... sfs) {
120     return new ArrayList<StoreFile>(Arrays.asList(sfs));
121   }
122 }