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