1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.io.encoding;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.hadoop.hbase.HBaseTestingUtility;
29 import org.apache.hadoop.hbase.HColumnDescriptor;
30 import org.apache.hadoop.hbase.SmallTests;
31 import org.apache.hadoop.hbase.client.Get;
32 import org.apache.hadoop.hbase.client.Put;
33 import org.apache.hadoop.hbase.client.Result;
34 import org.apache.hadoop.hbase.io.hfile.CacheConfig;
35 import org.apache.hadoop.hbase.io.hfile.LruBlockCache;
36 import org.apache.hadoop.hbase.regionserver.HRegion;
37 import org.apache.hadoop.hbase.util.Bytes;
38 import org.apache.hadoop.hbase.util.LoadTestKVGenerator;
39
40 import org.junit.Test;
41 import org.junit.experimental.categories.Category;
42 import org.junit.runner.RunWith;
43 import org.junit.runners.Parameterized;
44 import org.junit.runners.Parameterized.Parameters;
45
46
47
48
49 @Category(SmallTests.class)
50 @RunWith(Parameterized.class)
51 public class TestEncodedSeekers {
52
53 private static final String TABLE_NAME = "encodedSeekersTable";
54 private static final String CF_NAME = "encodedSeekersCF";
55 private static final byte[] CF_BYTES = Bytes.toBytes(CF_NAME);
56 private static final int MAX_VERSIONS = 5;
57
58 private static final int MIN_VALUE_SIZE = 30;
59 private static final int MAX_VALUE_SIZE = 60;
60 private static final int NUM_ROWS = 1000;
61 private static final int NUM_COLS_PER_ROW = 20;
62 private static final int NUM_HFILES = 4;
63 private static final int NUM_ROWS_PER_FLUSH = NUM_ROWS / NUM_HFILES;
64
65 private final HBaseTestingUtility testUtil = new HBaseTestingUtility();
66 private final DataBlockEncoding encoding;
67 private final boolean encodeOnDisk;
68
69
70 private static final boolean VERBOSE = false;
71
72 @Parameters
73 public static Collection<Object[]> parameters() {
74 List<Object[]> paramList = new ArrayList<Object[]>();
75 for (DataBlockEncoding encoding : DataBlockEncoding.values()) {
76 for (boolean encodeOnDisk : new boolean[]{false, true}) {
77 paramList.add(new Object[] { encoding, encodeOnDisk });
78 }
79 }
80 return paramList;
81 }
82
83 public TestEncodedSeekers(DataBlockEncoding encoding, boolean encodeOnDisk) {
84 this.encoding = encoding;
85 this.encodeOnDisk = encodeOnDisk;
86 }
87
88 @Test
89 public void testEncodedSeeker() throws IOException {
90 System.err.println("Testing encoded seekers for encoding " + encoding);
91 LruBlockCache cache = (LruBlockCache)
92 new CacheConfig(testUtil.getConfiguration()).getBlockCache();
93 cache.clearCache();
94
95 HRegion region = testUtil.createTestRegion(
96 TABLE_NAME, new HColumnDescriptor(CF_NAME)
97 .setMaxVersions(MAX_VERSIONS)
98 .setDataBlockEncoding(encoding)
99 .setEncodeOnDisk(encodeOnDisk)
100 );
101
102 LoadTestKVGenerator dataGenerator = new LoadTestKVGenerator(
103 MIN_VALUE_SIZE, MAX_VALUE_SIZE);
104
105
106 for (int i = 0; i < NUM_ROWS; ++i) {
107 byte[] key = LoadTestKVGenerator.md5PrefixedKey(i).getBytes();
108 for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
109 Put put = new Put(key);
110 byte[] col = Bytes.toBytes(String.valueOf(j));
111 byte[] value = dataGenerator.generateRandomSizeValue(key, col);
112 put.add(CF_BYTES, col, value);
113 region.put(put);
114 }
115 if (i % NUM_ROWS_PER_FLUSH == 0) {
116 region.flushcache();
117 }
118 }
119
120 for (int doneCompaction = 0; doneCompaction <= 1; ++doneCompaction) {
121
122 for (int i = 0; i < NUM_ROWS; ++i) {
123 byte[] rowKey = LoadTestKVGenerator.md5PrefixedKey(i).getBytes();
124 for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
125 if (VERBOSE) {
126 System.err.println("Reading row " + i + ", column " + j);
127 }
128 final String qualStr = String.valueOf(j);
129 final byte[] qualBytes = Bytes.toBytes(qualStr);
130 Get get = new Get(rowKey);
131 get.addColumn(CF_BYTES, qualBytes);
132 Result result = region.get(get);
133 assertEquals(1, result.size());
134 byte[] value = result.getValue(CF_BYTES, qualBytes);
135 assertTrue(LoadTestKVGenerator.verify(value, rowKey, qualBytes));
136 }
137 }
138
139 if (doneCompaction == 0) {
140
141 region.compactStores();
142 }
143 }
144
145 Map<DataBlockEncoding, Integer> encodingCounts =
146 cache.getEncodingCountsForTest();
147
148
149
150 System.err.println("encodingCounts=" + encodingCounts);
151 assertEquals(1, encodingCounts.size());
152 DataBlockEncoding encodingInCache =
153 encodingCounts.keySet().iterator().next();
154 assertEquals(encoding, encodingInCache);
155 assertTrue(encodingCounts.get(encodingInCache) > 0);
156 }
157
158 }