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.io.hfile;
019
020import static org.junit.Assert.assertEquals;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.Collection;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028import java.util.Set;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hbase.Cell;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.HBaseTestingUtility;
033import org.apache.hadoop.hbase.HColumnDescriptor;
034import org.apache.hadoop.hbase.HRegionInfo;
035import org.apache.hadoop.hbase.HTableDescriptor;
036import org.apache.hadoop.hbase.TableName;
037import org.apache.hadoop.hbase.client.Put;
038import org.apache.hadoop.hbase.client.Scan;
039import org.apache.hadoop.hbase.regionserver.BloomType;
040import org.apache.hadoop.hbase.regionserver.HRegion;
041import org.apache.hadoop.hbase.regionserver.InternalScanner;
042import org.apache.hadoop.hbase.testclassification.IOTests;
043import org.apache.hadoop.hbase.testclassification.SmallTests;
044import org.apache.hadoop.hbase.util.Bytes;
045import org.junit.AfterClass;
046import org.junit.ClassRule;
047import org.junit.Test;
048import org.junit.experimental.categories.Category;
049import org.junit.runner.RunWith;
050import org.junit.runners.Parameterized;
051import org.junit.runners.Parameterized.Parameters;
052
053/**
054 * Test the optimization that does not scan files where all key ranges are excluded.
055 */
056@RunWith(Parameterized.class)
057@Category({ IOTests.class, SmallTests.class })
058public class TestScannerSelectionUsingKeyRange {
059
060  @ClassRule
061  public static final HBaseClassTestRule CLASS_RULE =
062    HBaseClassTestRule.forClass(TestScannerSelectionUsingKeyRange.class);
063
064  private static final HBaseTestingUtility TEST_UTIL = HBaseTestingUtility.createLocalHTU();
065  private static TableName TABLE = TableName.valueOf("myTable");
066  private static String FAMILY = "myCF";
067  private static byte[] FAMILY_BYTES = Bytes.toBytes(FAMILY);
068  private static final int NUM_ROWS = 8;
069  private static final int NUM_COLS_PER_ROW = 5;
070  private static final int NUM_FILES = 2;
071  private static final Map<Object, Integer> TYPE_COUNT = new HashMap<>(3);
072  static {
073    TYPE_COUNT.put(BloomType.ROWCOL, 0);
074    TYPE_COUNT.put(BloomType.ROW, 0);
075    TYPE_COUNT.put(BloomType.NONE, 0);
076  }
077
078  private BloomType bloomType;
079  private int expectedCount;
080
081  @Parameters
082  public static Collection<Object[]> parameters() {
083    List<Object[]> params = new ArrayList<>();
084    for (Object type : TYPE_COUNT.keySet()) {
085      params.add(new Object[] { type, TYPE_COUNT.get(type) });
086    }
087    return params;
088  }
089
090  public TestScannerSelectionUsingKeyRange(Object type, Object count) {
091    bloomType = (BloomType) type;
092    expectedCount = (Integer) count;
093  }
094
095  @AfterClass
096  public static void tearDownAfterClass() throws Exception {
097    TEST_UTIL.cleanupTestDir();
098  }
099
100  @Test
101  public void testScannerSelection() throws IOException {
102    Configuration conf = TEST_UTIL.getConfiguration();
103    conf.setInt("hbase.hstore.compactionThreshold", 10000);
104    HColumnDescriptor hcd =
105      new HColumnDescriptor(FAMILY_BYTES).setBlockCacheEnabled(true).setBloomFilterType(bloomType);
106    HTableDescriptor htd = new HTableDescriptor(TABLE);
107    htd.addFamily(hcd);
108    HRegionInfo info = new HRegionInfo(TABLE);
109    HRegion region =
110      HBaseTestingUtility.createRegionAndWAL(info, TEST_UTIL.getDataTestDir(), conf, htd);
111
112    for (int iFile = 0; iFile < NUM_FILES; ++iFile) {
113      for (int iRow = 0; iRow < NUM_ROWS; ++iRow) {
114        Put put = new Put(Bytes.toBytes("row" + iRow));
115        for (int iCol = 0; iCol < NUM_COLS_PER_ROW; ++iCol) {
116          put.addColumn(FAMILY_BYTES, Bytes.toBytes("col" + iCol),
117            Bytes.toBytes("value" + iFile + "_" + iRow + "_" + iCol));
118        }
119        region.put(put);
120      }
121      region.flush(true);
122    }
123
124    Scan scan = new Scan(Bytes.toBytes("aaa"), Bytes.toBytes("aaz"));
125    BlockCache cache = BlockCacheFactory.createBlockCache(conf);
126    InternalScanner scanner = region.getScanner(scan);
127    List<Cell> results = new ArrayList<>();
128    while (scanner.next(results)) {
129    }
130    scanner.close();
131    assertEquals(0, results.size());
132    if (cache instanceof LruBlockCache) {
133      Set<String> accessedFiles = ((LruBlockCache) cache).getCachedFileNamesForTest();
134      assertEquals(expectedCount, accessedFiles.size());
135    }
136    HBaseTestingUtility.closeRegionAndWAL(region);
137  }
138}