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