1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.io.hfile;
20
21 import java.util.Iterator;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.hbase.classification.InterfaceAudience;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.io.HeapSize;
28 import org.apache.hadoop.hbase.io.hfile.slab.SlabCache;
29 import org.apache.hadoop.util.StringUtils;
30
31
32
33
34
35
36
37
38
39
40 @InterfaceAudience.Private
41 @Deprecated
42 public class DoubleBlockCache implements BlockCache, HeapSize {
43
44 static final Log LOG = LogFactory.getLog(DoubleBlockCache.class.getName());
45
46 private final LruBlockCache onHeapCache;
47 private final SlabCache offHeapCache;
48 private final CacheStats stats;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public DoubleBlockCache(long onHeapSize, long offHeapSize,
64 long onHeapBlockSize, long offHeapBlockSize, Configuration conf) {
65
66 LOG.info("Creating on-heap cache of size "
67 + StringUtils.humanReadableInt(onHeapSize)
68 + "bytes with an average block size of "
69 + StringUtils.humanReadableInt(onHeapBlockSize) + " bytes.");
70 onHeapCache = new LruBlockCache(onHeapSize, onHeapBlockSize, conf);
71
72 LOG.info("Creating off-heap cache of size "
73 + StringUtils.humanReadableInt(offHeapSize)
74 + "bytes with an average block size of "
75 + StringUtils.humanReadableInt(offHeapBlockSize) + " bytes.");
76 offHeapCache = new SlabCache(offHeapSize, offHeapBlockSize);
77
78 offHeapCache.addSlabByConf(conf);
79 this.stats = new CacheStats();
80 }
81
82 @Override
83 public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory) {
84 onHeapCache.cacheBlock(cacheKey, buf, inMemory);
85 offHeapCache.cacheBlock(cacheKey, buf);
86 }
87
88 @Override
89 public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf) {
90 onHeapCache.cacheBlock(cacheKey, buf);
91 offHeapCache.cacheBlock(cacheKey, buf);
92 }
93
94 @Override
95 public Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat,
96 boolean updateCacheMetrics) {
97 Cacheable cachedBlock;
98
99 if ((cachedBlock = onHeapCache.getBlock(cacheKey, caching, repeat,
100 updateCacheMetrics)) != null) {
101 if (updateCacheMetrics) stats.hit(caching);
102 return cachedBlock;
103
104 } else if ((cachedBlock = offHeapCache.getBlock(cacheKey, caching, repeat,
105 updateCacheMetrics)) != null) {
106 if (caching) {
107 onHeapCache.cacheBlock(cacheKey, cachedBlock);
108 }
109 if (updateCacheMetrics) stats.hit(caching);
110 return cachedBlock;
111 }
112
113 if (!repeat && updateCacheMetrics) stats.miss(caching);
114 return null;
115 }
116
117 @Override
118 public boolean evictBlock(BlockCacheKey cacheKey) {
119 stats.evict();
120 boolean cacheA = onHeapCache.evictBlock(cacheKey);
121 boolean cacheB = offHeapCache.evictBlock(cacheKey);
122 boolean evicted = cacheA || cacheB;
123 if (evicted) {
124 stats.evicted();
125 }
126 return evicted;
127 }
128
129 @Override
130 public CacheStats getStats() {
131 return this.stats;
132 }
133
134 @Override
135 public void shutdown() {
136 onHeapCache.shutdown();
137 offHeapCache.shutdown();
138 }
139
140 @Override
141 public long heapSize() {
142 return onHeapCache.heapSize() + offHeapCache.heapSize();
143 }
144
145 public long size() {
146 return onHeapCache.size() + offHeapCache.size();
147 }
148
149 public long getFreeSize() {
150 return onHeapCache.getFreeSize() + offHeapCache.getFreeSize();
151 }
152
153 public long getCurrentSize() {
154 return onHeapCache.getCurrentSize() + offHeapCache.getCurrentSize();
155 }
156
157 @Override
158 public int evictBlocksByHfileName(String hfileName) {
159 onHeapCache.evictBlocksByHfileName(hfileName);
160 offHeapCache.evictBlocksByHfileName(hfileName);
161 return 0;
162 }
163
164 @Override
165 public long getBlockCount() {
166 return onHeapCache.getBlockCount() + offHeapCache.getBlockCount();
167 }
168
169 @Override
170 public Iterator<CachedBlock> iterator() {
171 return new BlockCachesIterator(getBlockCaches());
172 }
173
174 @Override
175 public BlockCache[] getBlockCaches() {
176 return new BlockCache [] {this.onHeapCache, this.offHeapCache};
177 }
178 }