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.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
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    HFile.DATABLOCK_READ_COUNT.reset();
107  }
108
109  @Test
110  public void testCacheBlocks() throws IOException {
111    // Set index block size to be the same as normal block size.
112    TEST_UTIL.getConfiguration().setInt(HFileBlockIndex.MAX_CHUNK_SIZE_KEY, BLOCK_SIZE);
113    BlockCache blockCache = BlockCacheFactory.createBlockCache(TEST_UTIL.getConfiguration());
114    ColumnFamilyDescriptor cfd =
115        ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(CF)).setMaxVersions(MAX_VERSIONS)
116            .setCompressionType(COMPRESSION_ALGORITHM).setBloomFilterType(BLOOM_TYPE)
117            .setBlocksize(BLOCK_SIZE).setBlockCacheEnabled(cfCacheEnabled).build();
118    HRegion region = TEST_UTIL.createTestRegion(TABLE, cfd, blockCache);
119    CacheStats stats = blockCache.getStats();
120    writeTestData(region);
121    assertEquals(0, stats.getHitCount());
122    assertEquals(0, HFile.DATABLOCK_READ_COUNT.sum());
123    // Do a single get, take count of caches.  If we are NOT caching DATA blocks, the miss
124    // count should go up.  Otherwise, all should be cached and the miss count should not rise.
125    region.get(new Get(Bytes.toBytes("row" + 0)));
126    assertTrue(stats.getHitCount() > 0);
127    assertTrue(HFile.DATABLOCK_READ_COUNT.sum() > 0);
128    long missCount = stats.getMissCount();
129    region.get(new Get(Bytes.toBytes("row" + 0)));
130    if (this.cfCacheEnabled) assertEquals(missCount, stats.getMissCount());
131    else assertTrue(stats.getMissCount() > missCount);
132  }
133
134  private void writeTestData(HRegion region) throws IOException {
135    for (int i = 0; i < NUM_ROWS; ++i) {
136      Put put = new Put(Bytes.toBytes("row" + i));
137      for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
138        for (long ts = 1; ts < NUM_TIMESTAMPS_PER_COL; ++ts) {
139          put.addColumn(CF_BYTES, Bytes.toBytes("col" + j), ts,
140                  Bytes.toBytes("value" + i + "_" + j + "_" + ts));
141        }
142      }
143      region.put(put);
144      if ((i + 1) % ROWS_PER_HFILE == 0) {
145        region.flush(true);
146      }
147    }
148  }
149}