1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.regionserver;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.hadoop.hbase.Cell;
23 import org.apache.hadoop.hbase.HBaseTestCase;
24 import org.apache.hadoop.hbase.HBaseTestingUtility;
25 import org.apache.hadoop.hbase.HColumnDescriptor;
26 import org.apache.hadoop.hbase.HTableDescriptor;
27 import org.apache.hadoop.hbase.KeyValueUtil;
28 import org.apache.hadoop.hbase.TableName;
29 import org.apache.hadoop.hbase.client.Scan;
30 import org.apache.hadoop.hbase.io.compress.Compression;
31 import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
32 import org.apache.hadoop.hbase.io.hfile.CacheConfig;
33 import org.apache.hadoop.hbase.io.hfile.CacheStats;
34 import org.apache.hadoop.hbase.testclassification.SmallTests;
35 import org.apache.hadoop.hbase.util.Bytes;
36 import org.junit.Assert;
37 import org.junit.Test;
38 import org.junit.experimental.categories.Category;
39
40 @SuppressWarnings("deprecation")
41 @Category(SmallTests.class)
42 public class TestBlocksScanned extends HBaseTestCase {
43 private static byte [] FAMILY = Bytes.toBytes("family");
44 private static byte [] COL = Bytes.toBytes("col");
45 private static byte [] START_KEY = Bytes.toBytes("aaa");
46 private static byte [] END_KEY = Bytes.toBytes("zzz");
47 private static int BLOCK_SIZE = 70;
48
49 private static HBaseTestingUtility TEST_UTIL = null;
50
51 @Override
52 public void setUp() throws Exception {
53 super.setUp();
54
55 TEST_UTIL = new HBaseTestingUtility();
56 }
57
58 @Test
59 public void testBlocksScanned() throws Exception {
60 byte [] tableName = Bytes.toBytes("TestBlocksScanned");
61 HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tableName));
62
63 table.addFamily(
64 new HColumnDescriptor(FAMILY)
65 .setMaxVersions(10)
66 .setBlockCacheEnabled(true)
67 .setBlocksize(BLOCK_SIZE)
68 .setCompressionType(Compression.Algorithm.NONE)
69 );
70 _testBlocksScanned(table);
71 }
72
73 @Test
74 public void testBlocksScannedWithEncoding() throws Exception {
75 byte [] tableName = Bytes.toBytes("TestBlocksScannedWithEncoding");
76 HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tableName));
77
78 table.addFamily(
79 new HColumnDescriptor(FAMILY)
80 .setMaxVersions(10)
81 .setBlockCacheEnabled(true)
82 .setDataBlockEncoding(DataBlockEncoding.FAST_DIFF)
83 .setBlocksize(BLOCK_SIZE)
84 .setCompressionType(Compression.Algorithm.NONE)
85 );
86 _testBlocksScanned(table);
87 }
88
89 private void _testBlocksScanned(HTableDescriptor table) throws Exception {
90 Region r = createNewHRegion(table, START_KEY, END_KEY, TEST_UTIL.getConfiguration());
91 addContent(r, FAMILY, COL);
92 r.flush(true);
93
94 CacheStats stats = new CacheConfig(TEST_UTIL.getConfiguration()).getBlockCache().getStats();
95 long before = stats.getHitCount() + stats.getMissCount();
96
97 Scan scan = new Scan(Bytes.toBytes("aaa"), Bytes.toBytes("aaz"));
98 scan.addColumn(FAMILY, COL);
99 scan.setMaxVersions(1);
100
101 InternalScanner s = r.getScanner(scan);
102 List<Cell> results = new ArrayList<Cell>();
103 while (s.next(results))
104 ;
105 s.close();
106
107 int expectResultSize = 'z' - 'a';
108 assertEquals(expectResultSize, results.size());
109
110 int kvPerBlock = (int) Math.ceil(BLOCK_SIZE /
111 (double) KeyValueUtil.ensureKeyValue(results.get(0)).getLength());
112 Assert.assertEquals(2, kvPerBlock);
113
114 long expectDataBlockRead = (long) Math.ceil(expectResultSize / (double) kvPerBlock);
115 long expectIndexBlockRead = expectDataBlockRead;
116
117 assertEquals(expectIndexBlockRead+expectDataBlockRead, stats.getHitCount() + stats.getMissCount() - before);
118 }
119 }