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.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   * Tests encoded seekers by loading and reading values.
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    /** Enable when debugging */
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     // Write
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       // Read
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         // Compact, then read again at the next loop iteration.
141         region.compactStores();
142       }
143     }
144 
145     Map<DataBlockEncoding, Integer> encodingCounts =
146         cache.getEncodingCountsForTest();
147 
148     // Ensure that compactions don't pollute the cache with unencoded blocks
149     // in case of in-cache-only encoding.
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 }