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