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.regionserver; 019 020import static org.apache.hadoop.hbase.HTestConst.addContent; 021import static org.junit.jupiter.api.Assertions.assertEquals; 022 023import java.util.ArrayList; 024import java.util.List; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.fs.Path; 027import org.apache.hadoop.hbase.ExtendedCell; 028import org.apache.hadoop.hbase.HBaseTestingUtil; 029import org.apache.hadoop.hbase.KeyValueUtil; 030import org.apache.hadoop.hbase.TableName; 031import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 032import org.apache.hadoop.hbase.client.RegionInfo; 033import org.apache.hadoop.hbase.client.RegionInfoBuilder; 034import org.apache.hadoop.hbase.client.Scan; 035import org.apache.hadoop.hbase.client.TableDescriptor; 036import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 037import org.apache.hadoop.hbase.io.compress.Compression; 038import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding; 039import org.apache.hadoop.hbase.io.hfile.BlockCache; 040import org.apache.hadoop.hbase.io.hfile.BlockCacheFactory; 041import org.apache.hadoop.hbase.io.hfile.CacheStats; 042import org.apache.hadoop.hbase.testclassification.RegionServerTests; 043import org.apache.hadoop.hbase.testclassification.SmallTests; 044import org.apache.hadoop.hbase.util.Bytes; 045import org.junit.jupiter.api.BeforeEach; 046import org.junit.jupiter.api.Tag; 047import org.junit.jupiter.api.Test; 048 049@SuppressWarnings("deprecation") 050@Tag(RegionServerTests.TAG) 051@Tag(SmallTests.TAG) 052public class TestBlocksScanned { 053 054 private static byte[] FAMILY = Bytes.toBytes("family"); 055 private static byte[] COL = Bytes.toBytes("col"); 056 private static byte[] START_KEY = Bytes.toBytes("aaa"); 057 private static byte[] END_KEY = Bytes.toBytes("zzz"); 058 private static int BLOCK_SIZE = 70; 059 060 private static HBaseTestingUtil TEST_UTIL = null; 061 private Configuration conf; 062 private Path testDir; 063 064 @BeforeEach 065 public void setUp() throws Exception { 066 TEST_UTIL = new HBaseTestingUtil(); 067 conf = TEST_UTIL.getConfiguration(); 068 testDir = TEST_UTIL.getDataTestDir("TestBlocksScanned"); 069 } 070 071 @Test 072 public void testBlocksScanned() throws Exception { 073 byte[] tableName = Bytes.toBytes("TestBlocksScanned"); 074 TableDescriptor tableDescriptor = 075 TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName)) 076 .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).setMaxVersions(10) 077 .setBlockCacheEnabled(true).setBlocksize(BLOCK_SIZE) 078 .setCompressionType(Compression.Algorithm.NONE).build()) 079 .build(); 080 _testBlocksScanned(tableDescriptor); 081 } 082 083 @Test 084 public void testBlocksScannedWithEncoding() throws Exception { 085 byte[] tableName = Bytes.toBytes("TestBlocksScannedWithEncoding"); 086 TableDescriptor tableDescriptor = 087 TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName)) 088 .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).setMaxVersions(10) 089 .setBlockCacheEnabled(true).setDataBlockEncoding(DataBlockEncoding.FAST_DIFF) 090 .setBlocksize(BLOCK_SIZE).setCompressionType(Compression.Algorithm.NONE).build()) 091 .build(); 092 _testBlocksScanned(tableDescriptor); 093 } 094 095 private void _testBlocksScanned(TableDescriptor td) throws Exception { 096 BlockCache blockCache = BlockCacheFactory.createBlockCache(conf); 097 RegionInfo regionInfo = RegionInfoBuilder.newBuilder(td.getTableName()).setStartKey(START_KEY) 098 .setEndKey(END_KEY).build(); 099 HRegion r = HBaseTestingUtil.createRegionAndWAL(regionInfo, testDir, conf, td, blockCache); 100 addContent(r, FAMILY, COL); 101 r.flush(true); 102 103 CacheStats stats = blockCache.getStats(); 104 long before = stats.getHitCount() + stats.getMissCount(); 105 // Do simple test of getting one row only first. 106 Scan scan = new Scan().withStartRow(Bytes.toBytes("aaa")).withStopRow(Bytes.toBytes("aaz")) 107 .setReadType(Scan.ReadType.PREAD); 108 scan.addColumn(FAMILY, COL); 109 scan.readVersions(1); 110 111 InternalScanner s = r.getScanner(scan); 112 List<ExtendedCell> results = new ArrayList<>(); 113 while (s.next(results)) 114 ; 115 s.close(); 116 117 int expectResultSize = 'z' - 'a'; 118 assertEquals(expectResultSize, results.size()); 119 120 int kvPerBlock = (int) Math 121 .ceil(BLOCK_SIZE / (double) KeyValueUtil.ensureKeyValue(results.get(0)).getLength()); 122 assertEquals(2, kvPerBlock); 123 124 long expectDataBlockRead = (long) Math.ceil(expectResultSize / (double) kvPerBlock); 125 long expectIndexBlockRead = expectDataBlockRead; 126 127 assertEquals(expectIndexBlockRead + expectDataBlockRead, 128 stats.getHitCount() + stats.getMissCount() - before); 129 } 130}