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 static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.hadoop.hbase.HBaseTestingUtility;
30 import org.apache.hadoop.hbase.HColumnDescriptor;
31 import org.apache.hadoop.hbase.MediumTests;
32 import org.apache.hadoop.hbase.client.Get;
33 import org.apache.hadoop.hbase.client.Put;
34 import org.apache.hadoop.hbase.io.hfile.BlockType.BlockCategory;
35 import org.apache.hadoop.hbase.io.hfile.Compression.Algorithm;
36 import org.apache.hadoop.hbase.regionserver.HRegion;
37 import org.apache.hadoop.hbase.regionserver.StoreFile.BloomType;
38 import org.apache.hadoop.hbase.regionserver.metrics.SchemaMetrics;
39 import org.apache.hadoop.hbase.regionserver.metrics.SchemaMetrics.BlockMetricType;
40 import org.apache.hadoop.hbase.util.Bytes;
41 import org.junit.Test;
42 import org.junit.experimental.categories.Category;
43 import org.junit.runner.RunWith;
44 import org.junit.runners.Parameterized;
45 import org.junit.runners.Parameterized.Parameters;
46
47
48
49
50
51
52 @Category(MediumTests.class)
53 @RunWith(Parameterized.class)
54 public class TestForceCacheImportantBlocks {
55
56 private final HBaseTestingUtility TEST_UTIL =
57 new HBaseTestingUtility();
58
59 private static final String TABLE = "myTable";
60 private static final String CF = "myCF";
61 private static final byte[] CF_BYTES = Bytes.toBytes(CF);
62 private static final int MAX_VERSIONS = 3;
63 private static final int NUM_HFILES = 5;
64
65 private static final int ROWS_PER_HFILE = 100;
66 private static final int NUM_ROWS = NUM_HFILES * ROWS_PER_HFILE;
67 private static final int NUM_COLS_PER_ROW = 50;
68 private static final int NUM_TIMESTAMPS_PER_COL = 50;
69
70
71 private static final int BLOCK_SIZE = 256;
72
73 private static final Algorithm COMPRESSION_ALGORITHM =
74 Compression.Algorithm.GZ;
75 private static final BloomType BLOOM_TYPE = BloomType.ROW;
76
77 private final int hfileVersion;
78 private final boolean cfCacheEnabled;
79
80 @Parameters
81 public static Collection<Object[]> parameters() {
82
83 return Arrays.asList(new Object[][] {
84 new Object[] { new Integer(1), false },
85 new Object[] { new Integer(1), true },
86 new Object[] { new Integer(2), false },
87 new Object[] { new Integer(2), true }
88 });
89 }
90
91 public TestForceCacheImportantBlocks(int hfileVersion,
92 boolean cfCacheEnabled) {
93 this.hfileVersion = hfileVersion;
94 this.cfCacheEnabled = cfCacheEnabled;
95 TEST_UTIL.getConfiguration().setInt(HFile.FORMAT_VERSION_KEY,
96 hfileVersion);
97 }
98
99 @Test
100 public void testCacheBlocks() throws IOException {
101
102 TEST_UTIL.getConfiguration().setInt(HFileBlockIndex.MAX_CHUNK_SIZE_KEY,
103 BLOCK_SIZE);
104
105 SchemaMetrics.setUseTableNameInTest(false);
106 HColumnDescriptor hcd =
107 new HColumnDescriptor(Bytes.toBytes(CF))
108 .setMaxVersions(MAX_VERSIONS)
109 .setCompressionType(COMPRESSION_ALGORITHM)
110 .setBloomFilterType(BLOOM_TYPE);
111 hcd.setBlocksize(BLOCK_SIZE);
112 hcd.setBlockCacheEnabled(cfCacheEnabled);
113 HRegion region = TEST_UTIL.createTestRegion(TABLE, hcd);
114 writeTestData(region);
115 Map<String, Long> metricsBefore = SchemaMetrics.getMetricsSnapshot();
116 for (int i = 0; i < NUM_ROWS; ++i) {
117 Get get = new Get(Bytes.toBytes("row" + i));
118 region.get(get, null);
119 }
120 SchemaMetrics.validateMetricChanges(metricsBefore);
121 Map<String, Long> metricsAfter = SchemaMetrics.getMetricsSnapshot();
122 Map<String, Long> metricsDelta = SchemaMetrics.diffMetrics(metricsBefore,
123 metricsAfter);
124 SchemaMetrics metrics = SchemaMetrics.getInstance(TABLE, CF);
125 List<BlockCategory> importantBlockCategories =
126 new ArrayList<BlockCategory>();
127 importantBlockCategories.add(BlockCategory.BLOOM);
128 if (hfileVersion == 2) {
129
130 importantBlockCategories.add(BlockCategory.INDEX);
131 }
132
133 for (BlockCategory category : importantBlockCategories) {
134 String hitsMetricName = getMetricName(metrics, category);
135 assertTrue("Metric " + hitsMetricName + " was not incremented",
136 metricsDelta.containsKey(hitsMetricName));
137 long hits = metricsDelta.get(hitsMetricName);
138 assertTrue("Invalid value of " + hitsMetricName + ": " + hits, hits > 0);
139 }
140
141 if (!cfCacheEnabled) {
142
143
144 String dataHitMetricName = getMetricName(metrics, BlockCategory.DATA);
145 assertFalse("Nonzero value for metric " + dataHitMetricName,
146 metricsDelta.containsKey(dataHitMetricName));
147 }
148 }
149
150 private String getMetricName(SchemaMetrics metrics, BlockCategory category) {
151 String hitsMetricName =
152 metrics.getBlockMetricName(category, SchemaMetrics.NO_COMPACTION,
153 BlockMetricType.CACHE_HIT);
154 return hitsMetricName;
155 }
156
157 private void writeTestData(HRegion region) throws IOException {
158 for (int i = 0; i < NUM_ROWS; ++i) {
159 Put put = new Put(Bytes.toBytes("row" + i));
160 for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
161 for (long ts = 1; ts < NUM_TIMESTAMPS_PER_COL; ++ts) {
162 put.add(CF_BYTES, Bytes.toBytes("col" + j), ts,
163 Bytes.toBytes("value" + i + "_" + j + "_" + ts));
164 }
165 }
166 region.put(put);
167 if ((i + 1) % ROWS_PER_HFILE == 0) {
168 region.flushcache();
169 }
170 }
171 }
172
173 }
174