View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations
17   * under the License.
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   * CombinedBlockCache is an abstraction layer that combines
30   * {@link LruBlockCache} and {@link BucketCache}. The smaller lruCache is used
31   * to cache bloom blocks and index blocks , the larger bucketCache is used to
32   * cache data blocks. getBlock reads first from the smaller lruCache before
33   * looking for the block in the bucketCache. Metrics are the combined size and
34   * hits and misses of both caches.
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