1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.io.hfile;
19
20 import static org.junit.Assert.*;
21
22 import java.io.IOException;
23 import java.util.Map;
24 import java.util.NavigableSet;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.hbase.HBaseConfiguration;
30 import org.apache.hadoop.hbase.HConstants;
31 import org.apache.hadoop.hbase.testclassification.SmallTests;
32 import org.apache.hadoop.hbase.io.hfile.TestCacheConfig.DataCacheEntry;
33 import org.apache.hadoop.hbase.io.hfile.TestCacheConfig.IndexCacheEntry;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.experimental.categories.Category;
38
39 @Category(SmallTests.class)
40 public class TestBlockCacheReporting {
41 private static final Log LOG = LogFactory.getLog(TestBlockCacheReporting.class);
42 private Configuration conf;
43
44 @Before
45 public void setUp() throws Exception {
46 CacheConfig.clearGlobalInstances();
47 this.conf = HBaseConfiguration.create();
48 }
49
50 @After
51 public void tearDown() throws Exception {
52
53 CacheConfig.GLOBAL_BLOCK_CACHE_INSTANCE = null;
54 }
55
56 private void addDataAndHits(final BlockCache bc, final int count) {
57 Cacheable dce = new DataCacheEntry();
58 Cacheable ice = new IndexCacheEntry();
59 for (int i = 0; i < count; i++) {
60 BlockCacheKey bckd = new BlockCacheKey("f", i);
61 BlockCacheKey bcki = new BlockCacheKey("f", i + count);
62 bc.getBlock(bckd, true, false, true);
63 bc.cacheBlock(bckd, dce);
64 bc.cacheBlock(bcki, ice);
65 bc.getBlock(bckd, true, false, true);
66 bc.getBlock(bcki, true, false, true);
67 }
68 assertEquals(2 * count
69 BlockCacheKey bckd = new BlockCacheKey("f", 0);
70 BlockCacheKey bcki = new BlockCacheKey("f", 0 + count);
71 bc.evictBlock(bckd);
72 bc.evictBlock(bcki);
73 bc.getStats().getEvictedCount();
74 }
75
76 @Test
77 public void testBucketCache() throws IOException {
78 this.conf.set(HConstants.BUCKET_CACHE_IOENGINE_KEY, "offheap");
79 this.conf.setInt(HConstants.BUCKET_CACHE_SIZE_KEY, 100);
80 CacheConfig cc = new CacheConfig(this.conf);
81 assertTrue(cc.getBlockCache() instanceof CombinedBlockCache);
82 logPerBlock(cc.getBlockCache());
83 final int count = 3;
84 addDataAndHits(cc.getBlockCache(), count);
85
86 LOG.info(cc.getBlockCache().getStats());
87 BlockCacheUtil.CachedBlocksByFile cbsbf = logPerBlock(cc.getBlockCache());
88 LOG.info(cbsbf);
89 logPerFile(cbsbf);
90 bucketCacheReport(cc.getBlockCache());
91 LOG.info(BlockCacheUtil.toJSON(cbsbf));
92 }
93
94 @Test
95 public void testLruBlockCache() throws IOException {
96 CacheConfig cc = new CacheConfig(this.conf);
97 assertTrue(cc.isBlockCacheEnabled());
98 assertTrue(CacheConfig.DEFAULT_IN_MEMORY == cc.isInMemory());
99 assertTrue(cc.getBlockCache() instanceof LruBlockCache);
100 logPerBlock(cc.getBlockCache());
101 addDataAndHits(cc.getBlockCache(), 3);
102
103 BlockCache bc = cc.getBlockCache();
104 LOG.info("count=" + bc.getBlockCount() + ", currentSize=" + bc.getCurrentSize() +
105 ", freeSize=" + bc.getFreeSize() );
106 LOG.info(cc.getBlockCache().getStats());
107 BlockCacheUtil.CachedBlocksByFile cbsbf = logPerBlock(cc.getBlockCache());
108 LOG.info(cbsbf);
109 logPerFile(cbsbf);
110 bucketCacheReport(cc.getBlockCache());
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 }