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.hfile.BlockType.BlockCategory;
35  import org.apache.hadoop.hbase.io.hfile.Compression.Algorithm;
36  import org.apache.hadoop.hbase.regionserver.HRegion;
37  import org.apache.hadoop.hbase.regionserver.StoreFile.BloomType;
38  import org.apache.hadoop.hbase.regionserver.metrics.SchemaMetrics;
39  import org.apache.hadoop.hbase.regionserver.metrics.SchemaMetrics.BlockMetricType;
40  import org.apache.hadoop.hbase.util.Bytes;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  import org.junit.runner.RunWith;
44  import org.junit.runners.Parameterized;
45  import org.junit.runners.Parameterized.Parameters;
46  
47  /**W
48   * Make sure we always cache important block types, such as index blocks, as
49   * long as we have a block cache, even though block caching might be disabled
50   * for the column family.
51   */
52  @Category(MediumTests.class)
53  @RunWith(Parameterized.class)
54  public class TestForceCacheImportantBlocks {
55  
56    private final HBaseTestingUtility TEST_UTIL =
57        new HBaseTestingUtility();
58  
59    private static final String TABLE = "myTable";
60    private static final String CF = "myCF";
61    private static final byte[] CF_BYTES = Bytes.toBytes(CF);
62    private static final int MAX_VERSIONS = 3;
63    private static final int NUM_HFILES = 5;
64  
65    private static final int ROWS_PER_HFILE = 100;
66    private static final int NUM_ROWS = NUM_HFILES * ROWS_PER_HFILE;
67    private static final int NUM_COLS_PER_ROW = 50;
68    private static final int NUM_TIMESTAMPS_PER_COL = 50;
69  
70    /** Extremely small block size, so that we can get some index blocks */
71    private static final int BLOCK_SIZE = 256;
72    
73    private static final BloomType BLOOM_TYPE = BloomType.ROW;
74  
75    private final int hfileVersion;
76    private final boolean cfCacheEnabled;
77    private final Algorithm compressionAlgorithm;
78  
79    @Parameters
80    public static Collection<Object[]> parameters() {
81      // HFile versions
82      return Arrays.asList(new Object[][] {
83          new Object[] { new Integer(1), false, Compression.Algorithm.NONE },
84          new Object[] { new Integer(1), true, Compression.Algorithm.NONE },
85          new Object[] { new Integer(2), false, Compression.Algorithm.NONE },
86          new Object[] { new Integer(2), true, Compression.Algorithm.NONE },
87          new Object[] { new Integer(2), false, Compression.Algorithm.GZ },
88          new Object[] { new Integer(2), true, Compression.Algorithm.GZ }
89      });
90    }
91  
92    public TestForceCacheImportantBlocks(int hfileVersion,
93        boolean cfCacheEnabled, Algorithm compression) {
94      this.hfileVersion = hfileVersion;
95      this.cfCacheEnabled = cfCacheEnabled;
96      this.compressionAlgorithm = compression;
97      TEST_UTIL.getConfiguration().setInt(HFile.FORMAT_VERSION_KEY,
98          hfileVersion);
99    }
100 
101   @Test
102   public void testCacheBlocks() throws IOException {
103     // Set index block size to be the same as normal block size.
104     TEST_UTIL.getConfiguration().setInt(HFileBlockIndex.MAX_CHUNK_SIZE_KEY,
105         BLOCK_SIZE);
106 
107     SchemaMetrics.setUseTableNameInTest(false);
108     HColumnDescriptor hcd =
109         new HColumnDescriptor(Bytes.toBytes(CF))
110             .setMaxVersions(MAX_VERSIONS)
111             .setCompressionType(compressionAlgorithm)
112             .setBloomFilterType(BLOOM_TYPE);
113     hcd.setBlocksize(BLOCK_SIZE);
114     hcd.setBlockCacheEnabled(cfCacheEnabled);
115     HRegion region = TEST_UTIL.createTestRegion(TABLE, hcd);
116     writeTestData(region);
117     Map<String, Long> metricsBefore = SchemaMetrics.getMetricsSnapshot();
118     for (int i = 0; i < NUM_ROWS; ++i) {
119       Get get = new Get(Bytes.toBytes("row" + i));
120       region.get(get, null);
121     }
122     SchemaMetrics.validateMetricChanges(metricsBefore);
123     Map<String, Long> metricsAfter = SchemaMetrics.getMetricsSnapshot();
124     Map<String, Long> metricsDelta = SchemaMetrics.diffMetrics(metricsBefore,
125         metricsAfter);
126     SchemaMetrics metrics = SchemaMetrics.getInstance(TABLE, CF);
127     List<BlockCategory> importantBlockCategories =
128         new ArrayList<BlockCategory>();
129     importantBlockCategories.add(BlockCategory.BLOOM);
130     if (hfileVersion == 2) {
131       // We only have index blocks for HFile v2.
132       importantBlockCategories.add(BlockCategory.INDEX);
133     }
134 
135     for (BlockCategory category : importantBlockCategories) {
136       String hitsMetricName = getMetricName(metrics, category);
137       assertTrue("Metric " + hitsMetricName + " was not incremented",
138           metricsDelta.containsKey(hitsMetricName));
139       long hits = metricsDelta.get(hitsMetricName);
140       assertTrue("Invalid value of " + hitsMetricName + ": " + hits, hits > 0);
141     }
142 
143     if (!cfCacheEnabled) {
144       // Caching is turned off for the CF, so make sure we are not caching data
145       // blocks.
146       String dataHitMetricName = getMetricName(metrics, BlockCategory.DATA);
147       assertFalse("Nonzero value for metric " + dataHitMetricName,
148           metricsDelta.containsKey(dataHitMetricName));
149     }
150   }
151 
152   private String getMetricName(SchemaMetrics metrics, BlockCategory category) {
153     String hitsMetricName =
154         metrics.getBlockMetricName(category, SchemaMetrics.NO_COMPACTION,
155             BlockMetricType.CACHE_HIT);
156     return hitsMetricName;
157   }
158 
159   private void writeTestData(HRegion region) throws IOException {
160     for (int i = 0; i < NUM_ROWS; ++i) {
161       Put put = new Put(Bytes.toBytes("row" + i));
162       for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
163         for (long ts = 1; ts < NUM_TIMESTAMPS_PER_COL; ++ts) {
164           put.add(CF_BYTES, Bytes.toBytes("col" + j), ts,
165               Bytes.toBytes("value" + i + "_" + j + "_" + ts));
166         }
167       }
168       region.put(put);
169       if ((i + 1) % ROWS_PER_HFILE == 0) {
170         region.flushcache();
171       }
172     }
173   }
174 
175 }
176