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.regionserver.Region;
036import org.apache.hadoop.hbase.testclassification.IOTests;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.junit.Before;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.junit.runner.RunWith;
044import org.junit.runners.Parameterized;
045import org.junit.runners.Parameterized.Parameters;
046
047/**
048 * Make sure we always cache important block types, such as index blocks, as
049 * long as we have a block cache, even though block caching might be disabled
050 * for the column family.
051 *
052 * <p>TODO: This test writes a lot of data and only tests the most basic of metrics.  Cache stats
053 * need to reveal more about what is being cached whether DATA or INDEX blocks and then we could
054 * do more verification in this test.
055 */
056@Category({IOTests.class, MediumTests.class})
057@RunWith(Parameterized.class)
058public class TestForceCacheImportantBlocks {
059
060  @ClassRule
061  public static final HBaseClassTestRule CLASS_RULE =
062      HBaseClassTestRule.forClass(TestForceCacheImportantBlocks.class);
063
064  private final HBaseTestingUtility TEST_UTIL = HBaseTestingUtility.createLocalHTU();
065
066  private static final String TABLE = "myTable";
067  private static final String CF = "myCF";
068  private static final byte[] CF_BYTES = Bytes.toBytes(CF);
069  private static final int MAX_VERSIONS = 3;
070  private static final int NUM_HFILES = 5;
071
072  private static final int ROWS_PER_HFILE = 100;
073  private static final int NUM_ROWS = NUM_HFILES * ROWS_PER_HFILE;
074  private static final int NUM_COLS_PER_ROW = 50;
075  private static final int NUM_TIMESTAMPS_PER_COL = 50;
076
077  /** Extremely small block size, so that we can get some index blocks */
078  private static final int BLOCK_SIZE = 256;
079
080  private static final Algorithm COMPRESSION_ALGORITHM =
081      Compression.Algorithm.GZ;
082  private static final BloomType BLOOM_TYPE = BloomType.ROW;
083
084  @SuppressWarnings("unused")
085  // Currently unused.
086  private final int hfileVersion;
087  private final boolean cfCacheEnabled;
088
089  @Parameters
090  public static Collection<Object[]> parameters() {
091    // HFile versions
092    return Arrays.asList(
093      new Object[] { 3, true },
094      new Object[] { 3, false }
095    );
096  }
097
098  public TestForceCacheImportantBlocks(int hfileVersion, boolean cfCacheEnabled) {
099    this.hfileVersion = hfileVersion;
100    this.cfCacheEnabled = cfCacheEnabled;
101    TEST_UTIL.getConfiguration().setInt(HFile.FORMAT_VERSION_KEY, hfileVersion);
102  }
103
104  @Before
105  public void setup() {
106    // Make sure we make a new one each time.
107    CacheConfig.clearGlobalInstances();
108    HFile.DATABLOCK_READ_COUNT.reset();
109    CacheConfig.instantiateBlockCache(TEST_UTIL.getConfiguration());
110  }
111
112  @Test
113  public void testCacheBlocks() throws IOException {
114    // Set index block size to be the same as normal block size.
115    TEST_UTIL.getConfiguration().setInt(HFileBlockIndex.MAX_CHUNK_SIZE_KEY, BLOCK_SIZE);
116    HColumnDescriptor hcd = new HColumnDescriptor(Bytes.toBytes(CF)).setMaxVersions(MAX_VERSIONS).
117      setCompressionType(COMPRESSION_ALGORITHM).
118      setBloomFilterType(BLOOM_TYPE);
119    hcd.setBlocksize(BLOCK_SIZE);
120    hcd.setBlockCacheEnabled(cfCacheEnabled);
121    HRegion region = TEST_UTIL.createTestRegion(TABLE, hcd);
122    BlockCache cache = region.getStore(hcd.getName()).getCacheConfig().getBlockCache();
123    CacheStats stats = cache.getStats();
124    writeTestData(region);
125    assertEquals(0, stats.getHitCount());
126    assertEquals(0, HFile.DATABLOCK_READ_COUNT.sum());
127    // Do a single get, take count of caches.  If we are NOT caching DATA blocks, the miss
128    // count should go up.  Otherwise, all should be cached and the miss count should not rise.
129    region.get(new Get(Bytes.toBytes("row" + 0)));
130    assertTrue(stats.getHitCount() > 0);
131    assertTrue(HFile.DATABLOCK_READ_COUNT.sum() > 0);
132    long missCount = stats.getMissCount();
133    region.get(new Get(Bytes.toBytes("row" + 0)));
134    if (this.cfCacheEnabled) assertEquals(missCount, stats.getMissCount());
135    else assertTrue(stats.getMissCount() > missCount);
136  }
137
138  private void writeTestData(HRegion region) throws IOException {
139    for (int i = 0; i < NUM_ROWS; ++i) {
140      Put put = new Put(Bytes.toBytes("row" + i));
141      for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
142        for (long ts = 1; ts < NUM_TIMESTAMPS_PER_COL; ++ts) {
143          put.addColumn(CF_BYTES, Bytes.toBytes("col" + j), ts,
144                  Bytes.toBytes("value" + i + "_" + j + "_" + ts));
145        }
146      }
147      region.put(put);
148      if ((i + 1) % ROWS_PER_HFILE == 0) {
149        region.flush(true);
150      }
151    }
152  }
153}