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.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.Compression.Algorithm;
36  import org.apache.hadoop.hbase.io.hfile.HFile;
37  import org.apache.hadoop.hbase.io.hfile.LruBlockCache;
38  import org.apache.hadoop.hbase.regionserver.HRegion;
39  import org.apache.hadoop.hbase.regionserver.StoreFile.BloomType;
40  import org.apache.hadoop.hbase.util.Bytes;
41  import org.apache.hadoop.hbase.util.LoadTestKVGenerator;
42  import org.apache.hadoop.hbase.util.MultiThreadedWriter;
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(SmallTests.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 MIN_VALUE_SIZE = 30;
62    private static final int MAX_VALUE_SIZE = 60;
63    private static final int NUM_ROWS = 1000;
64    private static final int NUM_COLS_PER_ROW = 20;
65    private static final int NUM_HFILES = 4;
66    private static final int NUM_ROWS_PER_FLUSH = NUM_ROWS / NUM_HFILES;
67  
68    private final HBaseTestingUtility testUtil = new HBaseTestingUtility();
69    private final DataBlockEncoding encoding;
70    private final boolean encodeOnDisk;
71  
72    /** Enable when debugging */
73    private static final boolean VERBOSE = false;
74  
75    @Parameters
76    public static Collection<Object[]> parameters() {
77      List<Object[]> paramList = new ArrayList<Object[]>();
78      for (DataBlockEncoding encoding : DataBlockEncoding.values()) {
79        for (boolean encodeOnDisk : new boolean[]{false, true}) {
80          paramList.add(new Object[] { encoding, encodeOnDisk });
81        }
82      }
83      return paramList;
84    }
85  
86    public TestEncodedSeekers(DataBlockEncoding encoding, boolean encodeOnDisk) {
87      this.encoding = encoding;
88      this.encodeOnDisk = encodeOnDisk;
89    }
90  
91    @Test
92    public void testEncodedSeeker() throws IOException {
93      System.err.println("Testing encoded seekers for encoding " + encoding);
94      LruBlockCache cache = (LruBlockCache)
95      new CacheConfig(testUtil.getConfiguration()).getBlockCache();
96      cache.clearCache();
97  
98      HRegion region = testUtil.createTestRegion(
99          TABLE_NAME, new HColumnDescriptor(CF_NAME)
100             .setMaxVersions(MAX_VERSIONS)
101             .setDataBlockEncoding(encoding)
102             .setEncodeOnDisk(encodeOnDisk)
103     );
104     LoadTestKVGenerator dataGenerator = new LoadTestKVGenerator(
105         MIN_VALUE_SIZE, MAX_VALUE_SIZE);
106 
107     // Write
108     for (int i = 0; i < NUM_ROWS; ++i) {
109       byte[] key = MultiThreadedWriter.longToByteArrayKey(i);
110       for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
111         Put put = new Put(key);
112         String colAsStr = String.valueOf(j);
113         byte[] value = dataGenerator.generateRandomSizeValue(i, colAsStr);
114         put.add(CF_BYTES, Bytes.toBytes(colAsStr), value);
115         region.put(put);
116       }
117       if (i % NUM_ROWS_PER_FLUSH == 0) {
118         region.flushcache();
119       }
120     }
121 
122     for (int doneCompaction = 0; doneCompaction <= 1; ++doneCompaction) {
123       // Read
124       for (int i = 0; i < NUM_ROWS; ++i) {
125         final byte[] rowKey = MultiThreadedWriter.longToByteArrayKey(i);
126         for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
127           if (VERBOSE) {
128             System.err.println("Reading row " + i + ", column " +  j);
129           }
130           final String qualStr = String.valueOf(j);
131           final byte[] qualBytes = Bytes.toBytes(qualStr);
132           Get get = new Get(rowKey);
133           get.addColumn(CF_BYTES, qualBytes);
134           Result result = region.get(get, null);
135           assertEquals(1, result.size());
136           assertTrue(LoadTestKVGenerator.verify(Bytes.toString(rowKey), qualStr,
137               result.getValue(CF_BYTES, qualBytes)));
138         }
139       }
140 
141       if (doneCompaction == 0) {
142         // Compact, then read again at the next loop iteration.
143         region.compactStores();
144       }
145     }
146 
147     Map<DataBlockEncoding, Integer> encodingCounts =
148         cache.getEncodingCountsForTest();
149 
150     // Ensure that compactions don't pollute the cache with unencoded blocks
151     // in case of in-cache-only encoding.
152     System.err.println("encodingCounts=" + encodingCounts);
153     assertEquals(1, encodingCounts.size());
154     DataBlockEncoding encodingInCache =
155         encodingCounts.keySet().iterator().next();
156     assertEquals(encoding, encodingInCache);
157     assertTrue(encodingCounts.get(encodingInCache) > 0);
158   }
159 
160 }