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.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022 023import java.io.IOException; 024import java.util.stream.Stream; 025import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate; 026import org.apache.hadoop.hbase.HBaseTestingUtil; 027import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; 028import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 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.jupiter.api.BeforeEach; 039import org.junit.jupiter.api.Tag; 040import org.junit.jupiter.api.TestTemplate; 041import org.junit.jupiter.params.provider.Arguments; 042 043/** 044 * Make sure we always cache important block types, such as index blocks, as long as we have a block 045 * cache, even though block caching might be disabled for the column family. 046 * <p> 047 * TODO: This test writes a lot of data and only tests the most basic of metrics. Cache stats need 048 * to reveal more about what is being cached whether DATA or INDEX blocks and then we could do more 049 * verification in this test. 050 */ 051@Tag(IOTests.TAG) 052@Tag(MediumTests.TAG) 053@HBaseParameterizedTestTemplate(name = "{index}: hfileVersion={0}, cfCacheEnabled={1}") 054public class TestForceCacheImportantBlocks { 055 056 private final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 057 058 private static final String TABLE = "myTable"; 059 private static final String CF = "myCF"; 060 private static final byte[] CF_BYTES = Bytes.toBytes(CF); 061 private static final int MAX_VERSIONS = 3; 062 private static final int NUM_HFILES = 5; 063 064 private static final int ROWS_PER_HFILE = 100; 065 private static final int NUM_ROWS = NUM_HFILES * ROWS_PER_HFILE; 066 private static final int NUM_COLS_PER_ROW = 50; 067 private static final int NUM_TIMESTAMPS_PER_COL = 50; 068 069 /** Extremely small block size, so that we can get some index blocks */ 070 private static final int BLOCK_SIZE = 256; 071 072 private static final Algorithm COMPRESSION_ALGORITHM = Compression.Algorithm.GZ; 073 private static final BloomType BLOOM_TYPE = BloomType.ROW; 074 075 @SuppressWarnings("unused") 076 // Currently unused. 077 private final int hfileVersion; 078 private final boolean cfCacheEnabled; 079 080 public static Stream<Arguments> parameters() { 081 return Stream.of(Arguments.of(3, true), Arguments.of(3, false)); 082 } 083 084 public TestForceCacheImportantBlocks(int hfileVersion, boolean cfCacheEnabled) { 085 this.hfileVersion = hfileVersion; 086 this.cfCacheEnabled = cfCacheEnabled; 087 TEST_UTIL.getConfiguration().setInt(HFile.FORMAT_VERSION_KEY, hfileVersion); 088 } 089 090 @BeforeEach 091 public void setup() { 092 HFile.DATABLOCK_READ_COUNT.reset(); 093 } 094 095 @TestTemplate 096 public void testCacheBlocks() throws IOException { 097 // Set index block size to be the same as normal block size. 098 TEST_UTIL.getConfiguration().setInt(HFileBlockIndex.MAX_CHUNK_SIZE_KEY, BLOCK_SIZE); 099 BlockCache blockCache = BlockCacheFactory.createBlockCache(TEST_UTIL.getConfiguration()); 100 ColumnFamilyDescriptor cfd = 101 ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(CF)).setMaxVersions(MAX_VERSIONS) 102 .setCompressionType(COMPRESSION_ALGORITHM).setBloomFilterType(BLOOM_TYPE) 103 .setBlocksize(BLOCK_SIZE).setBlockCacheEnabled(cfCacheEnabled).build(); 104 HRegion region = TEST_UTIL.createTestRegion(TABLE, cfd, blockCache); 105 CacheStats stats = blockCache.getStats(); 106 writeTestData(region); 107 assertEquals(0, stats.getHitCount()); 108 assertEquals(0, HFile.DATABLOCK_READ_COUNT.sum()); 109 // Do a single get, take count of caches. If we are NOT caching DATA blocks, the miss 110 // count should go up. Otherwise, all should be cached and the miss count should not rise. 111 region.get(new Get(Bytes.toBytes("row" + 0))); 112 assertTrue(stats.getHitCount() > 0); 113 assertTrue(HFile.DATABLOCK_READ_COUNT.sum() > 0); 114 long missCount = stats.getMissCount(); 115 region.get(new Get(Bytes.toBytes("row" + 0))); 116 if (this.cfCacheEnabled) assertEquals(missCount, stats.getMissCount()); 117 else assertTrue(stats.getMissCount() > missCount); 118 } 119 120 private void writeTestData(HRegion region) throws IOException { 121 for (int i = 0; i < NUM_ROWS; ++i) { 122 Put put = new Put(Bytes.toBytes("row" + i)); 123 for (int j = 0; j < NUM_COLS_PER_ROW; ++j) { 124 for (long ts = 1; ts < NUM_TIMESTAMPS_PER_COL; ++ts) { 125 put.addColumn(CF_BYTES, Bytes.toBytes("col" + j), ts, 126 Bytes.toBytes("value" + i + "_" + j + "_" + ts)); 127 } 128 } 129 region.put(put); 130 if ((i + 1) % ROWS_PER_HFILE == 0) { 131 region.flush(true); 132 } 133 } 134 } 135}