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 getRequestCount() {
133       return lruCacheStats.getRequestCount()
134           + bucketCacheStats.getRequestCount();
135     }
136 
137     @Override
138     public long getRequestCachingCount() {
139       return lruCacheStats.getRequestCachingCount()
140           + bucketCacheStats.getRequestCachingCount();
141     }
142 
143     @Override
144     public long getMissCount() {
145       return lruCacheStats.getMissCount() + bucketCacheStats.getMissCount();
146     }
147 
148     @Override
149     public long getMissCachingCount() {
150       return lruCacheStats.getMissCachingCount()
151           + bucketCacheStats.getMissCachingCount();
152     }
153 
154     @Override
155     public long getHitCount() {
156       return lruCacheStats.getHitCount() + bucketCacheStats.getHitCount();
157     }
158 
159     @Override
160     public long getHitCachingCount() {
161       return lruCacheStats.getHitCachingCount()
162           + bucketCacheStats.getHitCachingCount();
163     }
164 
165     @Override
166     public long getEvictionCount() {
167       return lruCacheStats.getEvictionCount()
168           + bucketCacheStats.getEvictionCount();
169     }
170 
171     @Override
172     public long getEvictedCount() {
173       return lruCacheStats.getEvictedCount()
174           + bucketCacheStats.getEvictedCount();
175     }
176 
177     @Override
178     public double getHitRatioPastNPeriods() {
179       double ratio = ((double) (lruCacheStats.getSumHitCountsPastNPeriods() + bucketCacheStats
180           .getSumHitCountsPastNPeriods()) / (double) (lruCacheStats
181           .getSumRequestCountsPastNPeriods() + bucketCacheStats
182           .getSumRequestCountsPastNPeriods()));
183       return Double.isNaN(ratio) ? 0 : ratio;
184     }
185 
186     @Override
187     public double getHitCachingRatioPastNPeriods() {
188       double ratio = ((double) (lruCacheStats
189           .getSumHitCachingCountsPastNPeriods() + bucketCacheStats
190           .getSumHitCachingCountsPastNPeriods()) / (double) (lruCacheStats
191           .getSumRequestCachingCountsPastNPeriods() + bucketCacheStats
192           .getSumRequestCachingCountsPastNPeriods()));
193       return Double.isNaN(ratio) ? 0 : ratio;
194     }
195   }
196 
197   @Override
198   public Iterator<CachedBlock> iterator() {
199     return new BlockCachesIterator(getBlockCaches());
200   }
201 
202   @Override
203   public BlockCache[] getBlockCaches() {
204     return new BlockCache [] {this.lruCache, this.bucketCache};
205   }
206 }
207