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