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