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.Map; 025import java.util.NavigableSet; 026import java.util.Objects; 027import org.apache.hadoop.conf.Configuration; 028import org.apache.hadoop.hbase.HBaseConfiguration; 029import org.apache.hadoop.hbase.HConstants; 030import org.apache.hadoop.hbase.io.hfile.TestCacheConfig.DataCacheEntry; 031import org.apache.hadoop.hbase.io.hfile.TestCacheConfig.IndexCacheEntry; 032import org.apache.hadoop.hbase.testclassification.IOTests; 033import org.apache.hadoop.hbase.testclassification.SmallTests; 034import org.junit.jupiter.api.BeforeEach; 035import org.junit.jupiter.api.Tag; 036import org.junit.jupiter.api.Test; 037import org.slf4j.Logger; 038import org.slf4j.LoggerFactory; 039 040@Tag(IOTests.TAG) 041@Tag(SmallTests.TAG) 042public class TestBlockCacheReporting { 043 044 private static final Logger LOG = LoggerFactory.getLogger(TestBlockCacheReporting.class); 045 private Configuration conf; 046 047 @BeforeEach 048 public void setUp() throws Exception { 049 this.conf = HBaseConfiguration.create(); 050 } 051 052 private void addDataAndHits(final BlockCache bc, final int count) { 053 Cacheable dce = new DataCacheEntry(); 054 Cacheable ice = new IndexCacheEntry(); 055 for (int i = 0; i < count; i++) { 056 BlockCacheKey bckd = new BlockCacheKey("f", i); 057 BlockCacheKey bcki = new BlockCacheKey("f", i + count); 058 bc.getBlock(bckd, true, false, true); 059 bc.cacheBlock(bckd, dce); 060 bc.cacheBlock(bcki, ice); 061 bc.getBlock(bckd, true, false, true); 062 bc.getBlock(bcki, true, false, true); 063 } 064 assertEquals(2 * count /* Data and Index blocks */, bc.getStats().getHitCount()); 065 BlockCacheKey bckd = new BlockCacheKey("f", 0); 066 BlockCacheKey bcki = new BlockCacheKey("f", 0 + count); 067 bc.evictBlock(bckd); 068 bc.evictBlock(bcki); 069 bc.getStats().getEvictedCount(); 070 } 071 072 @Test 073 public void testBucketCache() throws IOException { 074 this.conf.set(HConstants.BUCKET_CACHE_IOENGINE_KEY, "offheap"); 075 this.conf.setInt(HConstants.BUCKET_CACHE_SIZE_KEY, 100); 076 BlockCache blockCache = BlockCacheFactory.createBlockCache(this.conf); 077 assertTrue(blockCache instanceof CombinedBlockCache); 078 logPerBlock(blockCache); 079 final int count = 3; 080 addDataAndHits(blockCache, count); 081 // The below has no asserts. It is just exercising toString and toJSON code. 082 LOG.info(Objects.toString(blockCache.getStats())); 083 BlockCacheUtil.CachedBlocksByFile cbsbf = logPerBlock(blockCache); 084 LOG.info(Objects.toString(cbsbf)); 085 logPerFile(cbsbf); 086 bucketCacheReport(blockCache); 087 LOG.info(BlockCacheUtil.toJSON(cbsbf)); 088 } 089 090 @Test 091 public void testLruBlockCache() throws IOException { 092 CacheConfig cc = new CacheConfig(this.conf); 093 assertTrue(CacheConfig.DEFAULT_IN_MEMORY == cc.isInMemory()); 094 BlockCache blockCache = BlockCacheFactory.createBlockCache(this.conf); 095 logPerBlock(blockCache); 096 addDataAndHits(blockCache, 3); 097 // The below has no asserts. It is just exercising toString and toJSON code. 098 LOG.info("count=" + blockCache.getBlockCount() + ", currentSize=" + blockCache.getCurrentSize() 099 + ", freeSize=" + blockCache.getFreeSize()); 100 LOG.info(Objects.toString(blockCache.getStats())); 101 BlockCacheUtil.CachedBlocksByFile cbsbf = logPerBlock(blockCache); 102 LOG.info(Objects.toString(cbsbf)); 103 logPerFile(cbsbf); 104 bucketCacheReport(blockCache); 105 LOG.info(BlockCacheUtil.toJSON(cbsbf)); 106 } 107 108 private void bucketCacheReport(final BlockCache bc) { 109 LOG.info(bc.getClass().getSimpleName() + ": " + bc.getStats()); 110 BlockCache[] bcs = bc.getBlockCaches(); 111 if (bcs != null) { 112 for (BlockCache sbc : bc.getBlockCaches()) { 113 LOG.info(bc.getClass().getSimpleName() + ": " + sbc.getStats()); 114 } 115 } 116 } 117 118 private void logPerFile(final BlockCacheUtil.CachedBlocksByFile cbsbf) throws IOException { 119 for (Map.Entry<String, NavigableSet<CachedBlock>> e : cbsbf.getCachedBlockStatsByFile() 120 .entrySet()) { 121 int count = 0; 122 long size = 0; 123 int countData = 0; 124 long sizeData = 0; 125 for (CachedBlock cb : e.getValue()) { 126 count++; 127 size += cb.getSize(); 128 BlockType bt = cb.getBlockType(); 129 if (bt != null && bt.isData()) { 130 countData++; 131 sizeData += cb.getSize(); 132 } 133 } 134 LOG.info("filename=" + e.getKey() + ", count=" + count + ", countData=" + countData 135 + ", size=" + size + ", sizeData=" + sizeData); 136 // LOG.info(BlockCacheUtil.toJSON(e.getKey(), e.getValue())); 137 } 138 } 139 140 private BlockCacheUtil.CachedBlocksByFile logPerBlock(final BlockCache bc) throws IOException { 141 BlockCacheUtil.CachedBlocksByFile cbsbf = new BlockCacheUtil.CachedBlocksByFile(); 142 for (CachedBlock cb : bc) { 143 LOG.info(cb.toString()); 144 // LOG.info(BlockCacheUtil.toJSON(bc)); 145 cbsbf.update(cb); 146 } 147 return cbsbf; 148 } 149}