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.hfile;
18  
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.Collection;
23  import java.util.List;
24  
25  import org.apache.hadoop.hbase.HBaseTestingUtility;
26  import org.apache.hadoop.hbase.HColumnDescriptor;
27  import org.apache.hadoop.hbase.testclassification.MediumTests;
28  import org.apache.hadoop.hbase.client.Get;
29  import org.apache.hadoop.hbase.client.Put;
30  import org.apache.hadoop.hbase.io.compress.Compression;
31  import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
32  import org.apache.hadoop.hbase.io.hfile.BlockType.BlockCategory;
33  import org.apache.hadoop.hbase.regionserver.BloomType;
34  import org.apache.hadoop.hbase.regionserver.HRegion;
35  import org.apache.hadoop.hbase.util.Bytes;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  import org.junit.runner.RunWith;
39  import org.junit.runners.Parameterized;
40  import org.junit.runners.Parameterized.Parameters;
41  
42  /**
43   * Make sure we always cache important block types, such as index blocks, as
44   * long as we have a block cache, even though block caching might be disabled
45   * for the column family.
46   */
47  @Category(MediumTests.class)
48  @RunWith(Parameterized.class)
49  public class TestForceCacheImportantBlocks {
50  
51    private final HBaseTestingUtility TEST_UTIL = HBaseTestingUtility.createLocalHTU();
52  
53    private static final String TABLE = "myTable";
54    private static final String CF = "myCF";
55    private static final byte[] CF_BYTES = Bytes.toBytes(CF);
56    private static final int MAX_VERSIONS = 3;
57    private static final int NUM_HFILES = 5;
58  
59    private static final int ROWS_PER_HFILE = 100;
60    private static final int NUM_ROWS = NUM_HFILES * ROWS_PER_HFILE;
61    private static final int NUM_COLS_PER_ROW = 50;
62    private static final int NUM_TIMESTAMPS_PER_COL = 50;
63  
64    /** Extremely small block size, so that we can get some index blocks */
65    private static final int BLOCK_SIZE = 256;
66    
67    private static final Algorithm COMPRESSION_ALGORITHM =
68        Compression.Algorithm.GZ;
69    private static final BloomType BLOOM_TYPE = BloomType.ROW;
70  
71    private final int hfileVersion;
72    private final boolean cfCacheEnabled;
73  
74    @Parameters
75    public static Collection<Object[]> parameters() {
76      // HFile versions
77      return Arrays.asList(
78        new Object[] { 2, true },
79        new Object[] { 2, false },
80        new Object[] { 3, true },
81        new Object[] { 3, false }
82      );
83    }
84  
85    public TestForceCacheImportantBlocks(int hfileVersion, boolean cfCacheEnabled) {
86      this.hfileVersion = hfileVersion;
87      this.cfCacheEnabled = cfCacheEnabled;
88      TEST_UTIL.getConfiguration().setInt(HFile.FORMAT_VERSION_KEY,
89          hfileVersion);
90    }
91  
92    @Test
93    public void testCacheBlocks() throws IOException {
94      // Set index block size to be the same as normal block size.
95      TEST_UTIL.getConfiguration().setInt(HFileBlockIndex.MAX_CHUNK_SIZE_KEY,
96          BLOCK_SIZE);
97  
98  
99      HColumnDescriptor hcd =
100         new HColumnDescriptor(Bytes.toBytes(CF))
101             .setMaxVersions(MAX_VERSIONS)
102             .setCompressionType(COMPRESSION_ALGORITHM)
103             .setBloomFilterType(BLOOM_TYPE);
104     hcd.setBlocksize(BLOCK_SIZE);
105     hcd.setBlockCacheEnabled(cfCacheEnabled);
106     HRegion region = TEST_UTIL.createTestRegion(TABLE, hcd);
107     writeTestData(region);
108 
109     for (int i = 0; i < NUM_ROWS; ++i) {
110       Get get = new Get(Bytes.toBytes("row" + i));
111       region.get(get);
112     }
113 
114     List<BlockCategory> importantBlockCategories =
115         new ArrayList<BlockCategory>();
116     importantBlockCategories.add(BlockCategory.BLOOM);
117     if (hfileVersion == 2) {
118       // We only have index blocks for HFile v2.
119       importantBlockCategories.add(BlockCategory.INDEX);
120     }
121   }
122 
123   private void writeTestData(HRegion region) throws IOException {
124     for (int i = 0; i < NUM_ROWS; ++i) {
125       Put put = new Put(Bytes.toBytes("row" + i));
126       for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
127         for (long ts = 1; ts < NUM_TIMESTAMPS_PER_COL; ++ts) {
128           put.add(CF_BYTES, Bytes.toBytes("col" + j), ts,
129               Bytes.toBytes("value" + i + "_" + j + "_" + ts));
130         }
131       }
132       region.put(put);
133       if ((i + 1) % ROWS_PER_HFILE == 0) {
134         region.flushcache();
135       }
136     }
137   }
138 }