View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations
15   * under the License.
16   */
17  package org.apache.hadoop.hbase.io.hfile;
18  
19  import static org.junit.Assert.assertEquals;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.Cell;
31  import org.apache.hadoop.hbase.HBaseTestingUtility;
32  import org.apache.hadoop.hbase.HColumnDescriptor;
33  import org.apache.hadoop.hbase.HRegionInfo;
34  import org.apache.hadoop.hbase.HTableDescriptor;
35  import org.apache.hadoop.hbase.TableName;
36  import org.apache.hadoop.hbase.client.Put;
37  import org.apache.hadoop.hbase.client.Scan;
38  import org.apache.hadoop.hbase.regionserver.BloomType;
39  import org.apache.hadoop.hbase.regionserver.HRegion;
40  import org.apache.hadoop.hbase.regionserver.InternalScanner;
41  import org.apache.hadoop.hbase.testclassification.SmallTests;
42  import org.apache.hadoop.hbase.util.Bytes;
43  import org.junit.AfterClass;
44  import org.junit.Test;
45  import org.junit.experimental.categories.Category;
46  import org.junit.runner.RunWith;
47  import org.junit.runners.Parameterized;
48  import org.junit.runners.Parameterized.Parameters;
49  
50  /**
51   * Test the optimization that does not scan files where all key ranges are excluded.
52   */
53  @RunWith(Parameterized.class)
54  @Category(SmallTests.class)
55  public class TestScannerSelectionUsingKeyRange {
56    private static final HBaseTestingUtility TEST_UTIL = HBaseTestingUtility.createLocalHTU();
57    private static TableName TABLE = TableName.valueOf("myTable");
58    private static String FAMILY = "myCF";
59    private static byte[] FAMILY_BYTES = Bytes.toBytes(FAMILY);
60    private static final int NUM_ROWS = 8;
61    private static final int NUM_COLS_PER_ROW = 5;
62    private static final int NUM_FILES = 2;
63    private static final Map<Object, Integer> TYPE_COUNT = new HashMap<Object, Integer>(3);
64    static {
65      TYPE_COUNT.put(BloomType.ROWCOL, 0);
66      TYPE_COUNT.put(BloomType.ROW, 0);
67      TYPE_COUNT.put(BloomType.NONE, 0);
68    }
69  
70    private BloomType bloomType;
71    private int expectedCount;
72  
73    @Parameters
74    public static Collection<Object[]> parameters() {
75      List<Object[]> params = new ArrayList<Object[]>();
76      for (Object type : TYPE_COUNT.keySet()) {
77        params.add(new Object[] { type, TYPE_COUNT.get(type) });
78      }
79      return params;
80    }
81  
82    public TestScannerSelectionUsingKeyRange(Object type, Object count) {
83      bloomType = (BloomType)type;
84      expectedCount = (Integer) count;
85    }
86    
87    @AfterClass
88    public static void tearDownAfterClass() throws Exception {
89      TEST_UTIL.cleanupTestDir();
90    }
91  
92    @Test
93    public void testScannerSelection() throws IOException {
94      Configuration conf = TEST_UTIL.getConfiguration();
95      conf.setInt("hbase.hstore.compactionThreshold", 10000);
96      HColumnDescriptor hcd = new HColumnDescriptor(FAMILY_BYTES).setBlockCacheEnabled(true)
97          .setBloomFilterType(bloomType);
98      HTableDescriptor htd = new HTableDescriptor(TABLE);
99      htd.addFamily(hcd);
100     HRegionInfo info = new HRegionInfo(TABLE);
101     HRegion region = HRegion.createHRegion(info, TEST_UTIL.getDataTestDir(), conf, htd);
102 
103     for (int iFile = 0; iFile < NUM_FILES; ++iFile) {
104       for (int iRow = 0; iRow < NUM_ROWS; ++iRow) {
105         Put put = new Put(Bytes.toBytes("row" + iRow));
106         for (int iCol = 0; iCol < NUM_COLS_PER_ROW; ++iCol) {
107           put.add(FAMILY_BYTES, Bytes.toBytes("col" + iCol),
108               Bytes.toBytes("value" + iFile + "_" + iRow + "_" + iCol));
109         }
110         region.put(put);
111       }
112       region.flush(true);
113     }
114 
115     Scan scan = new Scan(Bytes.toBytes("aaa"), Bytes.toBytes("aaz"));
116     CacheConfig.blockCacheDisabled = false;
117     CacheConfig cacheConf = new CacheConfig(conf);
118     LruBlockCache cache = (LruBlockCache) cacheConf.getBlockCache();
119     cache.clearCache();
120     InternalScanner scanner = region.getScanner(scan);
121     List<Cell> results = new ArrayList<Cell>();
122     while (scanner.next(results)) {
123     }
124     scanner.close();
125     assertEquals(0, results.size());
126     Set<String> accessedFiles = cache.getCachedFileNamesForTest();
127     assertEquals(expectedCount, accessedFiles.size());
128     region.close();
129   }
130 }