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.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   * Tests encoded seekers by loading and reading values.
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    /** Enable when debugging */
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      // Need to disable default row bloom filter for this test to pass.
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     //write the data, but leave some in the memstore
107     doPuts(region);
108 
109     //verify correctness when memstore contains data
110     doGets(region);
111 
112     //verify correctness again after compacting
113     region.compactStores();
114     doGets(region);
115 
116 
117     Map<DataBlockEncoding, Integer> encodingCounts = cache.getEncodingCountsForTest();
118 
119     // Ensure that compactions don't pollute the cache with unencoded blocks
120     // in case of in-cache-only encoding.
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 }