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