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