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