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