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