1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.io.hfile;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.List;
24
25 import org.apache.hadoop.hbase.HBaseTestingUtility;
26 import org.apache.hadoop.hbase.HColumnDescriptor;
27 import org.apache.hadoop.hbase.MediumTests;
28 import org.apache.hadoop.hbase.client.Get;
29 import org.apache.hadoop.hbase.client.Put;
30 import org.apache.hadoop.hbase.io.compress.Compression;
31 import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
32 import org.apache.hadoop.hbase.io.hfile.BlockType.BlockCategory;
33 import org.apache.hadoop.hbase.regionserver.BloomType;
34 import org.apache.hadoop.hbase.regionserver.HRegion;
35 import org.apache.hadoop.hbase.util.Bytes;
36 import org.junit.Test;
37 import org.junit.experimental.categories.Category;
38 import org.junit.runner.RunWith;
39 import org.junit.runners.Parameterized;
40 import org.junit.runners.Parameterized.Parameters;
41
42
43
44
45
46
47 @Category(MediumTests.class)
48 @RunWith(Parameterized.class)
49 public class TestForceCacheImportantBlocks {
50
51 private final HBaseTestingUtility TEST_UTIL = HBaseTestingUtility.createLocalHTU();
52
53 private static final String TABLE = "myTable";
54 private static final String CF = "myCF";
55 private static final byte[] CF_BYTES = Bytes.toBytes(CF);
56 private static final int MAX_VERSIONS = 3;
57 private static final int NUM_HFILES = 5;
58
59 private static final int ROWS_PER_HFILE = 100;
60 private static final int NUM_ROWS = NUM_HFILES * ROWS_PER_HFILE;
61 private static final int NUM_COLS_PER_ROW = 50;
62 private static final int NUM_TIMESTAMPS_PER_COL = 50;
63
64
65 private static final int BLOCK_SIZE = 256;
66
67 private static final Algorithm COMPRESSION_ALGORITHM =
68 Compression.Algorithm.GZ;
69 private static final BloomType BLOOM_TYPE = BloomType.ROW;
70
71 private final int hfileVersion;
72 private final boolean cfCacheEnabled;
73
74 @Parameters
75 public static Collection<Object[]> parameters() {
76
77 return Arrays.asList(new Object[][] {
78 new Object[] { new Integer(2), false },
79 new Object[] { new Integer(2), true }
80 });
81 }
82
83 public TestForceCacheImportantBlocks(int hfileVersion,
84 boolean cfCacheEnabled) {
85 this.hfileVersion = hfileVersion;
86 this.cfCacheEnabled = cfCacheEnabled;
87 TEST_UTIL.getConfiguration().setInt(HFile.FORMAT_VERSION_KEY,
88 hfileVersion);
89 }
90
91 @Test
92 public void testCacheBlocks() throws IOException {
93
94 TEST_UTIL.getConfiguration().setInt(HFileBlockIndex.MAX_CHUNK_SIZE_KEY,
95 BLOCK_SIZE);
96
97
98 HColumnDescriptor hcd =
99 new HColumnDescriptor(Bytes.toBytes(CF))
100 .setMaxVersions(MAX_VERSIONS)
101 .setCompressionType(COMPRESSION_ALGORITHM)
102 .setBloomFilterType(BLOOM_TYPE);
103 hcd.setBlocksize(BLOCK_SIZE);
104 hcd.setBlockCacheEnabled(cfCacheEnabled);
105 HRegion region = TEST_UTIL.createTestRegion(TABLE, hcd);
106 writeTestData(region);
107
108 for (int i = 0; i < NUM_ROWS; ++i) {
109 Get get = new Get(Bytes.toBytes("row" + i));
110 region.get(get);
111 }
112
113 List<BlockCategory> importantBlockCategories =
114 new ArrayList<BlockCategory>();
115 importantBlockCategories.add(BlockCategory.BLOOM);
116 if (hfileVersion == 2) {
117
118 importantBlockCategories.add(BlockCategory.INDEX);
119 }
120 }
121
122
123 private void writeTestData(HRegion region) throws IOException {
124 for (int i = 0; i < NUM_ROWS; ++i) {
125 Put put = new Put(Bytes.toBytes("row" + i));
126 for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
127 for (long ts = 1; ts < NUM_TIMESTAMPS_PER_COL; ++ts) {
128 put.add(CF_BYTES, Bytes.toBytes("col" + j), ts,
129 Bytes.toBytes("value" + i + "_" + j + "_" + ts));
130 }
131 }
132 region.put(put);
133 if ((i + 1) % ROWS_PER_HFILE == 0) {
134 region.flushcache();
135 }
136 }
137 }
138
139 }
140