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 Algorithm COMPRESSION_ALGORITHM =
74        Compression.Algorithm.GZ;
75    private static final BloomType BLOOM_TYPE = BloomType.ROW;
76  
77    private final int hfileVersion;
78    private final boolean cfCacheEnabled;
79  
80    @Parameters
81    public static Collection<Object[]> parameters() {
82      // HFile versions
83      return Arrays.asList(new Object[][] {
84          new Object[] { new Integer(1), false },
85          new Object[] { new Integer(1), true },
86          new Object[] { new Integer(2), false },
87          new Object[] { new Integer(2), true }
88      });
89    }
90  
91    public TestForceCacheImportantBlocks(int hfileVersion,
92        boolean cfCacheEnabled) {
93      this.hfileVersion = hfileVersion;
94      this.cfCacheEnabled = cfCacheEnabled;
95      TEST_UTIL.getConfiguration().setInt(HFile.FORMAT_VERSION_KEY,
96          hfileVersion);
97    }
98  
99    @Test
100   public void testCacheBlocks() throws IOException {
101     // Set index block size to be the same as normal block size.
102     TEST_UTIL.getConfiguration().setInt(HFileBlockIndex.MAX_CHUNK_SIZE_KEY,
103         BLOCK_SIZE);
104 
105     SchemaMetrics.setUseTableNameInTest(false);
106     HColumnDescriptor hcd =
107         new HColumnDescriptor(Bytes.toBytes(CF))
108             .setMaxVersions(MAX_VERSIONS)
109             .setCompressionType(COMPRESSION_ALGORITHM)
110             .setBloomFilterType(BLOOM_TYPE);
111     hcd.setBlocksize(BLOCK_SIZE);
112     hcd.setBlockCacheEnabled(cfCacheEnabled);
113     HRegion region = TEST_UTIL.createTestRegion(TABLE, hcd);
114     writeTestData(region);
115     Map<String, Long> metricsBefore = SchemaMetrics.getMetricsSnapshot();
116     for (int i = 0; i < NUM_ROWS; ++i) {
117       Get get = new Get(Bytes.toBytes("row" + i));
118       region.get(get, null);
119     }
120     SchemaMetrics.validateMetricChanges(metricsBefore);
121     Map<String, Long> metricsAfter = SchemaMetrics.getMetricsSnapshot();
122     Map<String, Long> metricsDelta = SchemaMetrics.diffMetrics(metricsBefore,
123         metricsAfter);
124     SchemaMetrics metrics = SchemaMetrics.getInstance(TABLE, CF);
125     List<BlockCategory> importantBlockCategories =
126         new ArrayList<BlockCategory>();
127     importantBlockCategories.add(BlockCategory.BLOOM);
128     if (hfileVersion == 2) {
129       // We only have index blocks for HFile v2.
130       importantBlockCategories.add(BlockCategory.INDEX);
131     }
132 
133     for (BlockCategory category : importantBlockCategories) {
134       String hitsMetricName = getMetricName(metrics, category);
135       assertTrue("Metric " + hitsMetricName + " was not incremented",
136           metricsDelta.containsKey(hitsMetricName));
137       long hits = metricsDelta.get(hitsMetricName);
138       assertTrue("Invalid value of " + hitsMetricName + ": " + hits, hits > 0);
139     }
140 
141     if (!cfCacheEnabled) {
142       // Caching is turned off for the CF, so make sure we are not caching data
143       // blocks.
144       String dataHitMetricName = getMetricName(metrics, BlockCategory.DATA);
145       assertFalse("Nonzero value for metric " + dataHitMetricName,
146           metricsDelta.containsKey(dataHitMetricName));
147     }
148   }
149 
150   private String getMetricName(SchemaMetrics metrics, BlockCategory category) {
151     String hitsMetricName =
152         metrics.getBlockMetricName(category, SchemaMetrics.NO_COMPACTION,
153             BlockMetricType.CACHE_HIT);
154     return hitsMetricName;
155   }
156 
157   private void writeTestData(HRegion region) throws IOException {
158     for (int i = 0; i < NUM_ROWS; ++i) {
159       Put put = new Put(Bytes.toBytes("row" + i));
160       for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
161         for (long ts = 1; ts < NUM_TIMESTAMPS_PER_COL; ++ts) {
162           put.add(CF_BYTES, Bytes.toBytes("col" + j), ts,
163               Bytes.toBytes("value" + i + "_" + j + "_" + ts));
164         }
165       }
166       region.put(put);
167       if ((i + 1) % ROWS_PER_HFILE == 0) {
168         region.flushcache();
169       }
170     }
171   }
172 
173 }
174