001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.hfile;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.util.Arrays;
025import java.util.Collection;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.HColumnDescriptor;
029import org.apache.hadoop.hbase.client.Get;
030import org.apache.hadoop.hbase.client.Put;
031import org.apache.hadoop.hbase.io.compress.Compression;
032import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
033import org.apache.hadoop.hbase.regionserver.BloomType;
034import org.apache.hadoop.hbase.regionserver.HRegion;
035import org.apache.hadoop.hbase.testclassification.IOTests;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.junit.Before;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042import org.junit.runner.RunWith;
043import org.junit.runners.Parameterized;
044import org.junit.runners.Parameterized.Parameters;
045
046/**
047 * Make sure we always cache important block types, such as index blocks, as
048 * long as we have a block cache, even though block caching might be disabled
049 * for the column family.
050 *
051 * <p>TODO: This test writes a lot of data and only tests the most basic of metrics.  Cache stats
052 * need to reveal more about what is being cached whether DATA or INDEX blocks and then we could
053 * do more verification in this test.
054 */
055@Category({IOTests.class, MediumTests.class})
056@RunWith(Parameterized.class)
057public class TestForceCacheImportantBlocks {
058
059  @ClassRule
060  public static final HBaseClassTestRule CLASS_RULE =
061      HBaseClassTestRule.forClass(TestForceCacheImportantBlocks.class);
062
063  private final HBaseTestingUtility TEST_UTIL = HBaseTestingUtility.createLocalHTU();
064
065  private static final String TABLE = "myTable";
066  private static final String CF = "myCF";
067  private static final byte[] CF_BYTES = Bytes.toBytes(CF);
068  private static final int MAX_VERSIONS = 3;
069  private static final int NUM_HFILES = 5;
070
071  private static final int ROWS_PER_HFILE = 100;
072  private static final int NUM_ROWS = NUM_HFILES * ROWS_PER_HFILE;
073  private static final int NUM_COLS_PER_ROW = 50;
074  private static final int NUM_TIMESTAMPS_PER_COL = 50;
075
076  /** Extremely small block size, so that we can get some index blocks */
077  private static final int BLOCK_SIZE = 256;
078
079  private static final Algorithm COMPRESSION_ALGORITHM =
080      Compression.Algorithm.GZ;
081  private static final BloomType BLOOM_TYPE = BloomType.ROW;
082
083  @SuppressWarnings("unused")
084  // Currently unused.
085  private final int hfileVersion;
086  private final boolean cfCacheEnabled;
087
088  @Parameters
089  public static Collection<Object[]> parameters() {
090    // HFile versions
091    return Arrays.asList(
092      new Object[] { 3, true },
093      new Object[] { 3, false }
094    );
095  }
096
097  public TestForceCacheImportantBlocks(int hfileVersion, boolean cfCacheEnabled) {
098    this.hfileVersion = hfileVersion;
099    this.cfCacheEnabled = cfCacheEnabled;
100    TEST_UTIL.getConfiguration().setInt(HFile.FORMAT_VERSION_KEY, hfileVersion);
101  }
102
103  @Before
104  public void setup() {
105    // Make sure we make a new one each time.
106    CacheConfig.clearGlobalInstances();
107    HFile.DATABLOCK_READ_COUNT.reset();
108    CacheConfig.instantiateBlockCache(TEST_UTIL.getConfiguration());
109  }
110
111  @Test
112  public void testCacheBlocks() throws IOException {
113    // Set index block size to be the same as normal block size.
114    TEST_UTIL.getConfiguration().setInt(HFileBlockIndex.MAX_CHUNK_SIZE_KEY, BLOCK_SIZE);
115    HColumnDescriptor hcd = new HColumnDescriptor(Bytes.toBytes(CF)).setMaxVersions(MAX_VERSIONS).
116      setCompressionType(COMPRESSION_ALGORITHM).
117      setBloomFilterType(BLOOM_TYPE);
118    hcd.setBlocksize(BLOCK_SIZE);
119    hcd.setBlockCacheEnabled(cfCacheEnabled);
120    HRegion region = TEST_UTIL.createTestRegion(TABLE, hcd);
121    BlockCache cache = region.getStore(hcd.getName()).getCacheConfig().getBlockCache();
122    CacheStats stats = cache.getStats();
123    writeTestData(region);
124    assertEquals(0, stats.getHitCount());
125    assertEquals(0, HFile.DATABLOCK_READ_COUNT.sum());
126    // Do a single get, take count of caches.  If we are NOT caching DATA blocks, the miss
127    // count should go up.  Otherwise, all should be cached and the miss count should not rise.
128    region.get(new Get(Bytes.toBytes("row" + 0)));
129    assertTrue(stats.getHitCount() > 0);
130    assertTrue(HFile.DATABLOCK_READ_COUNT.sum() > 0);
131    long missCount = stats.getMissCount();
132    region.get(new Get(Bytes.toBytes("row" + 0)));
133    if (this.cfCacheEnabled) assertEquals(missCount, stats.getMissCount());
134    else assertTrue(stats.getMissCount() > missCount);
135  }
136
137  private void writeTestData(HRegion region) throws IOException {
138    for (int i = 0; i < NUM_ROWS; ++i) {
139      Put put = new Put(Bytes.toBytes("row" + i));
140      for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
141        for (long ts = 1; ts < NUM_TIMESTAMPS_PER_COL; ++ts) {
142          put.addColumn(CF_BYTES, Bytes.toBytes("col" + j), ts,
143                  Bytes.toBytes("value" + i + "_" + j + "_" + ts));
144        }
145      }
146      region.put(put);
147      if ((i + 1) % ROWS_PER_HFILE == 0) {
148        region.flush(true);
149      }
150    }
151  }
152}