View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations
15   * under the License.
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.HConstants;
31  import org.apache.hadoop.hbase.KeyValue;
32  import org.apache.hadoop.hbase.MediumTests;
33  import org.apache.hadoop.hbase.Tag;
34  import org.apache.hadoop.hbase.client.Get;
35  import org.apache.hadoop.hbase.client.Put;
36  import org.apache.hadoop.hbase.client.Result;
37  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
38  import org.apache.hadoop.hbase.io.hfile.HFile;
39  import org.apache.hadoop.hbase.io.hfile.LruBlockCache;
40  import org.apache.hadoop.hbase.regionserver.BloomType;
41  import org.apache.hadoop.hbase.regionserver.HRegion;
42  import org.apache.hadoop.hbase.util.Bytes;
43  import org.apache.hadoop.hbase.util.Strings;
44  import org.apache.hadoop.hbase.util.test.LoadTestKVGenerator;
45  import org.junit.Test;
46  import org.junit.experimental.categories.Category;
47  import org.junit.runner.RunWith;
48  import org.junit.runners.Parameterized;
49  import org.junit.runners.Parameterized.Parameters;
50  
51  /**
52   * Tests encoded seekers by loading and reading values.
53   */
54  @Category(MediumTests.class)
55  @RunWith(Parameterized.class)
56  public class TestEncodedSeekers {
57  
58    private static final String TABLE_NAME = "encodedSeekersTable";
59    private static final String CF_NAME = "encodedSeekersCF";
60    private static final byte[] CF_BYTES = Bytes.toBytes(CF_NAME);
61    private static final int MAX_VERSIONS = 5;
62  
63    private static final int BLOCK_SIZE = 64 * 1024;
64    private static final int MIN_VALUE_SIZE = 30;
65    private static final int MAX_VALUE_SIZE = 60;
66    private static final int NUM_ROWS = 1003;
67    private static final int NUM_COLS_PER_ROW = 20;
68    private static final int NUM_HFILES = 4;
69    private static final int NUM_ROWS_PER_FLUSH = NUM_ROWS / NUM_HFILES;
70  
71    private final HBaseTestingUtility testUtil = HBaseTestingUtility.createLocalHTU();
72    private final DataBlockEncoding encoding;
73    private final boolean encodeOnDisk;
74    private final boolean includeTags;
75    private final boolean compressTags;
76  
77    /** Enable when debugging */
78    private static final boolean VERBOSE = false;
79  
80    @Parameters
81    public static Collection<Object[]> parameters() {
82      List<Object[]> paramList = new ArrayList<Object[]>();
83      for (DataBlockEncoding encoding : DataBlockEncoding.values()) {
84        for (boolean includeTags : new boolean[] { false, true }) {
85          for (boolean encodeOnDisk : new boolean[] { false, true }) {
86            for (boolean compressTags : new boolean[] { false, true }) {
87              paramList.add(new Object[] { encoding, encodeOnDisk, includeTags, compressTags });
88            }
89          }
90        }
91      }
92      return paramList;
93    }
94  
95    public TestEncodedSeekers(DataBlockEncoding encoding, boolean encodeOnDisk, boolean includeTags,
96        boolean compressTags) {
97      this.encoding = encoding;
98      this.encodeOnDisk = encodeOnDisk;
99      this.includeTags = includeTags;
100     this.compressTags = compressTags;
101   }
102 
103   @Test
104   public void testEncodedSeeker() throws IOException {
105     System.err.println("Testing encoded seekers for encoding : " + encoding + ", encodeOnDisk : "
106         + encodeOnDisk + ", includeTags : " + includeTags + ", compressTags : " + compressTags);
107     if(includeTags) {
108       testUtil.getConfiguration().setInt(HFile.FORMAT_VERSION_KEY, 3);
109     }
110     LruBlockCache cache =
111       (LruBlockCache)new CacheConfig(testUtil.getConfiguration()).getBlockCache();
112     cache.clearCache();
113     // Need to disable default row bloom filter for this test to pass.
114     HColumnDescriptor hcd = (new HColumnDescriptor(CF_NAME)).setMaxVersions(MAX_VERSIONS).
115         setDataBlockEncoding(encoding).
116         setBlocksize(BLOCK_SIZE).
117         setBloomFilterType(BloomType.NONE).
118         setCompressTags(compressTags);
119     HRegion region = testUtil.createTestRegion(TABLE_NAME, hcd);
120 
121     //write the data, but leave some in the memstore
122     doPuts(region);
123 
124     //verify correctness when memstore contains data
125     doGets(region);
126 
127     //verify correctness again after compacting
128     region.compactStores();
129     doGets(region);
130 
131 
132     Map<DataBlockEncoding, Integer> encodingCounts = cache.getEncodingCountsForTest();
133 
134     // Ensure that compactions don't pollute the cache with unencoded blocks
135     // in case of in-cache-only encoding.
136     System.err.println("encodingCounts=" + encodingCounts);
137     assertEquals(1, encodingCounts.size());
138     DataBlockEncoding encodingInCache = encodingCounts.keySet().iterator().next();
139     assertEquals(encoding, encodingInCache);
140     assertTrue(encodingCounts.get(encodingInCache) > 0);
141   }
142 
143 
144   private void doPuts(HRegion region) throws IOException{
145     LoadTestKVGenerator dataGenerator = new LoadTestKVGenerator(MIN_VALUE_SIZE, MAX_VALUE_SIZE);
146      for (int i = 0; i < NUM_ROWS; ++i) {
147       byte[] key = LoadTestKVGenerator.md5PrefixedKey(i).getBytes();
148       for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
149         Put put = new Put(key);
150         byte[] col = Bytes.toBytes(String.valueOf(j));
151         byte[] value = dataGenerator.generateRandomSizeValue(key, col);
152         if (includeTags) {
153           Tag[] tag = new Tag[1];
154           tag[0] = new Tag((byte) 1, "Visibility");
155           KeyValue kv = new KeyValue(key, CF_BYTES, col, HConstants.LATEST_TIMESTAMP, value, tag);
156           put.add(kv);
157         } else {
158           put.add(CF_BYTES, col, value);
159         }
160         if(VERBOSE){
161           KeyValue kvPut = new KeyValue(key, CF_BYTES, col, value);
162           System.err.println(Strings.padFront(i+"", ' ', 4)+" "+kvPut);
163         }
164         region.put(put);
165       }
166       if (i % NUM_ROWS_PER_FLUSH == 0) {
167         region.flushcache();
168       }
169     }
170   }
171 
172 
173   private void doGets(HRegion region) throws IOException{
174     for (int i = 0; i < NUM_ROWS; ++i) {
175       final byte[] rowKey = LoadTestKVGenerator.md5PrefixedKey(i).getBytes();
176       for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
177         final String qualStr = String.valueOf(j);
178         if (VERBOSE) {
179           System.err.println("Reading row " + i + ", column " + j + " " + Bytes.toString(rowKey)+"/"
180               +qualStr);
181         }
182         final byte[] qualBytes = Bytes.toBytes(qualStr);
183         Get get = new Get(rowKey);
184         get.addColumn(CF_BYTES, qualBytes);
185         Result result = region.get(get);
186         assertEquals(1, result.size());
187         byte[] value = result.getValue(CF_BYTES, qualBytes);
188         assertTrue(LoadTestKVGenerator.verify(value, rowKey, qualBytes));
189       }
190     }
191   }
192 
193 }