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.hadoop.hbase.classification.InterfaceAudience;
24 import org.apache.hadoop.hbase.io.HeapSize;
25 import org.apache.hadoop.hbase.io.hfile.BlockType.BlockCategory;
26 import org.apache.hadoop.hbase.io.hfile.bucket.BucketCache;
27
28
29
30
31
32
33
34
35
36
37 @InterfaceAudience.Private
38 public class CombinedBlockCache implements BlockCache, HeapSize {
39
40 private final LruBlockCache lruCache;
41 private final BucketCache bucketCache;
42 private final CombinedCacheStats combinedCacheStats;
43
44 public CombinedBlockCache(LruBlockCache lruCache, BucketCache bucketCache) {
45 this.lruCache = lruCache;
46 this.bucketCache = bucketCache;
47 this.combinedCacheStats = new CombinedCacheStats(lruCache.getStats(),
48 bucketCache.getStats());
49 }
50
51 @Override
52 public long heapSize() {
53 return lruCache.heapSize() + bucketCache.heapSize();
54 }
55
56 @Override
57 public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory) {
58 boolean isMetaBlock = buf.getBlockType().getCategory() != BlockCategory.DATA;
59 if (isMetaBlock) {
60 lruCache.cacheBlock(cacheKey, buf, inMemory);
61 } else {
62 bucketCache.cacheBlock(cacheKey, buf, inMemory);
63 }
64 }
65
66 @Override
67 public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf) {
68 cacheBlock(cacheKey, buf, false);
69 }
70
71 @Override
72 public Cacheable getBlock(BlockCacheKey cacheKey, boolean caching,
73 boolean repeat, boolean updateCacheMetrics) {
74 if (lruCache.containsBlock(cacheKey)) {
75 return lruCache.getBlock(cacheKey, caching, repeat, updateCacheMetrics);
76 }
77 return bucketCache.getBlock(cacheKey, caching, repeat, updateCacheMetrics);
78 }
79
80 @Override
81 public boolean evictBlock(BlockCacheKey cacheKey) {
82 return lruCache.evictBlock(cacheKey) || bucketCache.evictBlock(cacheKey);
83 }
84
85 @Override
86 public int evictBlocksByHfileName(String hfileName) {
87 return lruCache.evictBlocksByHfileName(hfileName)
88 + bucketCache.evictBlocksByHfileName(hfileName);
89 }
90
91 @Override
92 public CacheStats getStats() {
93 return this.combinedCacheStats;
94 }
95
96 @Override
97 public void shutdown() {
98 lruCache.shutdown();
99 bucketCache.shutdown();
100 }
101
102 @Override
103 public long size() {
104 return lruCache.size() + bucketCache.size();
105 }
106
107 @Override
108 public long getFreeSize() {
109 return lruCache.getFreeSize() + bucketCache.getFreeSize();
110 }
111
112 @Override
113 public long getCurrentSize() {
114 return lruCache.getCurrentSize() + bucketCache.getCurrentSize();
115 }
116
117 @Override
118 public long getBlockCount() {
119 return lruCache.getBlockCount() + bucketCache.getBlockCount();
120 }
121
122 private static class CombinedCacheStats extends CacheStats {
123 private final CacheStats lruCacheStats;
124 private final CacheStats bucketCacheStats;
125
126 CombinedCacheStats(CacheStats lbcStats, CacheStats fcStats) {
127 this.lruCacheStats = lbcStats;
128 this.bucketCacheStats = fcStats;
129 }
130
131 @Override
132 public long getDataMissCount() {
133 return lruCacheStats.getDataMissCount() + bucketCacheStats.getDataMissCount();
134 }
135
136 @Override
137 public long getLeafIndexMissCount() {
138 return lruCacheStats.getLeafIndexMissCount() + bucketCacheStats.getLeafIndexMissCount();
139 }
140
141 @Override
142 public long getBloomChunkMissCount() {
143 return lruCacheStats.getBloomChunkMissCount() + bucketCacheStats.getBloomChunkMissCount();
144 }
145
146 @Override
147 public long getMetaMissCount() {
148 return lruCacheStats.getMetaMissCount() + bucketCacheStats.getMetaMissCount();
149 }
150
151 @Override
152 public long getRootIndexMissCount() {
153 return lruCacheStats.getRootIndexMissCount() + bucketCacheStats.getRootIndexMissCount();
154 }
155
156 @Override
157 public long getIntermediateIndexMissCount() {
158 return lruCacheStats.getIntermediateIndexMissCount() +
159 bucketCacheStats.getIntermediateIndexMissCount();
160 }
161
162 @Override
163 public long getFileInfoMissCount() {
164 return lruCacheStats.getFileInfoMissCount() + bucketCacheStats.getFileInfoMissCount();
165 }
166
167 @Override
168 public long getGeneralBloomMetaMissCount() {
169 return lruCacheStats.getGeneralBloomMetaMissCount() +
170 bucketCacheStats.getGeneralBloomMetaMissCount();
171 }
172
173 @Override
174 public long getDeleteFamilyBloomMissCount() {
175 return lruCacheStats.getDeleteFamilyBloomMissCount() +
176 bucketCacheStats.getDeleteFamilyBloomMissCount();
177 }
178
179 @Override
180 public long getTrailerMissCount() {
181 return lruCacheStats.getTrailerMissCount() + bucketCacheStats.getTrailerMissCount();
182 }
183
184 @Override
185 public long getDataHitCount() {
186 return lruCacheStats.getDataHitCount() + bucketCacheStats.getDataHitCount();
187 }
188
189 @Override
190 public long getLeafIndexHitCount() {
191 return lruCacheStats.getLeafIndexHitCount() + bucketCacheStats.getLeafIndexHitCount();
192 }
193
194 @Override
195 public long getBloomChunkHitCount() {
196 return lruCacheStats.getBloomChunkHitCount() + bucketCacheStats.getBloomChunkHitCount();
197 }
198
199 @Override
200 public long getMetaHitCount() {
201 return lruCacheStats.getMetaHitCount() + bucketCacheStats.getMetaHitCount();
202 }
203
204 @Override
205 public long getRootIndexHitCount() {
206 return lruCacheStats.getRootIndexHitCount() + bucketCacheStats.getRootIndexHitCount();
207 }
208
209 @Override
210 public long getIntermediateIndexHitCount() {
211 return lruCacheStats.getIntermediateIndexHitCount() +
212 bucketCacheStats.getIntermediateIndexHitCount();
213 }
214
215 @Override
216 public long getFileInfoHitCount() {
217 return lruCacheStats.getFileInfoHitCount() + bucketCacheStats.getFileInfoHitCount();
218 }
219
220 @Override
221 public long getGeneralBloomMetaHitCount() {
222 return lruCacheStats.getGeneralBloomMetaHitCount() +
223 bucketCacheStats.getGeneralBloomMetaHitCount();
224 }
225
226 @Override
227 public long getDeleteFamilyBloomHitCount() {
228 return lruCacheStats.getDeleteFamilyBloomHitCount() +
229 bucketCacheStats.getDeleteFamilyBloomHitCount();
230 }
231
232 @Override
233 public long getTrailerHitCount() {
234 return lruCacheStats.getTrailerHitCount() + bucketCacheStats.getTrailerHitCount();
235 }
236
237 @Override
238 public long getRequestCount() {
239 return lruCacheStats.getRequestCount()
240 + bucketCacheStats.getRequestCount();
241 }
242
243 @Override
244 public long getRequestCachingCount() {
245 return lruCacheStats.getRequestCachingCount()
246 + bucketCacheStats.getRequestCachingCount();
247 }
248
249 @Override
250 public long getMissCount() {
251 return lruCacheStats.getMissCount() + bucketCacheStats.getMissCount();
252 }
253
254 @Override
255 public long getMissCachingCount() {
256 return lruCacheStats.getMissCachingCount()
257 + bucketCacheStats.getMissCachingCount();
258 }
259
260 @Override
261 public long getHitCount() {
262 return lruCacheStats.getHitCount() + bucketCacheStats.getHitCount();
263 }
264
265 @Override
266 public long getHitCachingCount() {
267 return lruCacheStats.getHitCachingCount()
268 + bucketCacheStats.getHitCachingCount();
269 }
270
271 @Override
272 public long getEvictionCount() {
273 return lruCacheStats.getEvictionCount()
274 + bucketCacheStats.getEvictionCount();
275 }
276
277 @Override
278 public long getEvictedCount() {
279 return lruCacheStats.getEvictedCount()
280 + bucketCacheStats.getEvictedCount();
281 }
282
283 @Override
284 public double getHitRatioPastNPeriods() {
285 double ratio = ((double) (lruCacheStats.getSumHitCountsPastNPeriods() + bucketCacheStats
286 .getSumHitCountsPastNPeriods()) / (double) (lruCacheStats
287 .getSumRequestCountsPastNPeriods() + bucketCacheStats
288 .getSumRequestCountsPastNPeriods()));
289 return Double.isNaN(ratio) ? 0 : ratio;
290 }
291
292 @Override
293 public double getHitCachingRatioPastNPeriods() {
294 double ratio = ((double) (lruCacheStats
295 .getSumHitCachingCountsPastNPeriods() + bucketCacheStats
296 .getSumHitCachingCountsPastNPeriods()) / (double) (lruCacheStats
297 .getSumRequestCachingCountsPastNPeriods() + bucketCacheStats
298 .getSumRequestCachingCountsPastNPeriods()));
299 return Double.isNaN(ratio) ? 0 : ratio;
300 }
301 }
302
303 @Override
304 public Iterator<CachedBlock> iterator() {
305 return new BlockCachesIterator(getBlockCaches());
306 }
307
308 @Override
309 public BlockCache[] getBlockCaches() {
310 return new BlockCache [] {this.lruCache, this.bucketCache};
311 }
312 }
313