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.HBaseTestingUtil;
028import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
029import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
030import org.apache.hadoop.hbase.client.Get;
031import org.apache.hadoop.hbase.client.Put;
032import org.apache.hadoop.hbase.io.compress.Compression;
033import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
034import org.apache.hadoop.hbase.regionserver.BloomType;
035import org.apache.hadoop.hbase.regionserver.HRegion;
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 long as we have a block
049 * cache, even though block caching might be disabled for the column family.
050 * <p>
051 * TODO: This test writes a lot of data and only tests the most basic of metrics. Cache stats need
052 * to reveal more about what is being cached whether DATA or INDEX blocks and then we could do more
053 * 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 HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
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 = Compression.Algorithm.GZ;
080  private static final BloomType BLOOM_TYPE = BloomType.ROW;
081
082  @SuppressWarnings("unused")
083  // Currently unused.
084  private final int hfileVersion;
085  private final boolean cfCacheEnabled;
086
087  @Parameters
088  public static Collection<Object[]> parameters() {
089    // HFile versions
090    return Arrays.asList(new Object[] { 3, true }, new Object[] { 3, false });
091  }
092
093  public TestForceCacheImportantBlocks(int hfileVersion, boolean cfCacheEnabled) {
094    this.hfileVersion = hfileVersion;
095    this.cfCacheEnabled = cfCacheEnabled;
096    TEST_UTIL.getConfiguration().setInt(HFile.FORMAT_VERSION_KEY, hfileVersion);
097  }
098
099  @Before
100  public void setup() {
101    HFile.DATABLOCK_READ_COUNT.reset();
102  }
103
104  @Test
105  public void testCacheBlocks() throws IOException {
106    // Set index block size to be the same as normal block size.
107    TEST_UTIL.getConfiguration().setInt(HFileBlockIndex.MAX_CHUNK_SIZE_KEY, BLOCK_SIZE);
108    BlockCache blockCache = BlockCacheFactory.createBlockCache(TEST_UTIL.getConfiguration());
109    ColumnFamilyDescriptor cfd =
110      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(CF)).setMaxVersions(MAX_VERSIONS)
111        .setCompressionType(COMPRESSION_ALGORITHM).setBloomFilterType(BLOOM_TYPE)
112        .setBlocksize(BLOCK_SIZE).setBlockCacheEnabled(cfCacheEnabled).build();
113    HRegion region = TEST_UTIL.createTestRegion(TABLE, cfd, blockCache);
114    CacheStats stats = blockCache.getStats();
115    writeTestData(region);
116    assertEquals(0, stats.getHitCount());
117    assertEquals(0, HFile.DATABLOCK_READ_COUNT.sum());
118    // Do a single get, take count of caches. If we are NOT caching DATA blocks, the miss
119    // count should go up. Otherwise, all should be cached and the miss count should not rise.
120    region.get(new Get(Bytes.toBytes("row" + 0)));
121    assertTrue(stats.getHitCount() > 0);
122    assertTrue(HFile.DATABLOCK_READ_COUNT.sum() > 0);
123    long missCount = stats.getMissCount();
124    region.get(new Get(Bytes.toBytes("row" + 0)));
125    if (this.cfCacheEnabled) assertEquals(missCount, stats.getMissCount());
126    else assertTrue(stats.getMissCount() > missCount);
127  }
128
129  private void writeTestData(HRegion region) throws IOException {
130    for (int i = 0; i < NUM_ROWS; ++i) {
131      Put put = new Put(Bytes.toBytes("row" + i));
132      for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
133        for (long ts = 1; ts < NUM_TIMESTAMPS_PER_COL; ++ts) {
134          put.addColumn(CF_BYTES, Bytes.toBytes("col" + j), ts,
135            Bytes.toBytes("value" + i + "_" + j + "_" + ts));
136        }
137      }
138      region.put(put);
139      if ((i + 1) % ROWS_PER_HFILE == 0) {
140        region.flush(true);
141      }
142    }
143  }
144}