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.io.IOException;
22  import java.util.List;
23  
24  import org.apache.hadoop.classification.InterfaceAudience;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.io.HeapSize;
27  import org.apache.hadoop.hbase.io.hfile.BlockType.BlockCategory;
28  import org.apache.hadoop.hbase.io.hfile.bucket.BucketCache;
29  
30  /**
31   * CombinedBlockCache is an abstraction layer that combines
32   * {@link LruBlockCache} and {@link BucketCache}. The smaller lruCache is used
33   * to cache bloom blocks and index blocks , the larger bucketCache is used to
34   * cache data blocks. getBlock reads first from the smaller lruCache before
35   * looking for the block in the bucketCache. Metrics are the combined size and
36   * hits and misses of both caches.
37   * 
38   **/
39  @InterfaceAudience.Private
40  public class CombinedBlockCache implements BlockCache, HeapSize {
41  
42    private final LruBlockCache lruCache;
43    private final BucketCache bucketCache;
44    private final CombinedCacheStats combinedCacheStats;
45  
46    public CombinedBlockCache(LruBlockCache lruCache, BucketCache bucketCache) {
47      this.lruCache = lruCache;
48      this.bucketCache = bucketCache;
49      this.combinedCacheStats = new CombinedCacheStats(lruCache.getStats(),
50          bucketCache.getStats());
51    }
52  
53    @Override
54    public long heapSize() {
55      return lruCache.heapSize() + bucketCache.heapSize();
56    }
57  
58  
59    @Override
60    public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory) {
61      boolean isMetaBlock = buf.getBlockType().getCategory() != BlockCategory.DATA;
62      if (isMetaBlock) {
63        lruCache.cacheBlock(cacheKey, buf, inMemory);
64      } else {
65        bucketCache.cacheBlock(cacheKey, buf, inMemory);
66      }
67    }
68  
69  
70    @Override
71    public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf) {
72      cacheBlock(cacheKey, buf, false);
73    }
74  
75    @Override
76    public Cacheable getBlock(BlockCacheKey cacheKey, boolean caching,
77        boolean repeat) {
78      if (lruCache.containsBlock(cacheKey)) {
79        return lruCache.getBlock(cacheKey, caching, repeat);
80      }
81      return bucketCache.getBlock(cacheKey, caching, repeat);
82  
83    }
84  
85    @Override
86    public boolean evictBlock(BlockCacheKey cacheKey) {
87      return lruCache.evictBlock(cacheKey) || bucketCache.evictBlock(cacheKey);
88    }
89  
90    @Override
91    public int evictBlocksByHfileName(String hfileName) {
92      return lruCache.evictBlocksByHfileName(hfileName)
93          + bucketCache.evictBlocksByHfileName(hfileName);
94    }
95  
96    @Override
97    public CacheStats getStats() {
98      return this.combinedCacheStats;
99    }
100 
101   @Override
102   public void shutdown() {
103     lruCache.shutdown();
104     bucketCache.shutdown();
105     
106   }
107 
108   @Override
109   public long size() {
110     return lruCache.size() + bucketCache.size();
111   }
112 
113   @Override
114   public long getFreeSize() {
115     return lruCache.getFreeSize() + bucketCache.getFreeSize();
116   }
117 
118   @Override
119   public long getCurrentSize() {
120     return lruCache.getCurrentSize() + bucketCache.getCurrentSize();
121   }
122 
123   @Override
124   public long getEvictedCount() {
125     return lruCache.getEvictedCount() + bucketCache.getEvictedCount();
126   }
127 
128   @Override
129   public long getBlockCount() {
130     return lruCache.getBlockCount() + bucketCache.getBlockCount();
131   }
132 
133   @Override
134   public List<BlockCacheColumnFamilySummary> getBlockCacheColumnFamilySummaries(
135       Configuration conf) throws IOException {
136     throw new UnsupportedOperationException();
137   }
138 
139   private static class CombinedCacheStats extends CacheStats {
140     private final CacheStats lruCacheStats;
141     private final CacheStats bucketCacheStats;
142 
143     CombinedCacheStats(CacheStats lbcStats, CacheStats fcStats) {
144       this.lruCacheStats = lbcStats;
145       this.bucketCacheStats = fcStats;
146     }
147 
148     @Override
149     public long getRequestCount() {
150       return lruCacheStats.getRequestCount()
151           + bucketCacheStats.getRequestCount();
152     }
153 
154     @Override
155     public long getRequestCachingCount() {
156       return lruCacheStats.getRequestCachingCount()
157           + bucketCacheStats.getRequestCachingCount();
158     }
159 
160     @Override
161     public long getMissCount() {
162       return lruCacheStats.getMissCount() + bucketCacheStats.getMissCount();
163     }
164 
165     @Override
166     public long getMissCachingCount() {
167       return lruCacheStats.getMissCachingCount()
168           + bucketCacheStats.getMissCachingCount();
169     }
170 
171     @Override
172     public long getHitCount() {
173       return lruCacheStats.getHitCount() + bucketCacheStats.getHitCount();
174     }
175 
176     @Override
177     public long getHitCachingCount() {
178       return lruCacheStats.getHitCachingCount()
179           + bucketCacheStats.getHitCachingCount();
180     }
181 
182     @Override
183     public long getEvictionCount() {
184       return lruCacheStats.getEvictionCount()
185           + bucketCacheStats.getEvictionCount();
186     }
187 
188     @Override
189     public long getEvictedCount() {
190       return lruCacheStats.getEvictedCount()
191           + bucketCacheStats.getEvictedCount();
192     }
193 
194     @Override
195     public double getHitRatioPastNPeriods() {
196       double ratio = ((double) (lruCacheStats.getSumHitCountsPastNPeriods() + bucketCacheStats
197           .getSumHitCountsPastNPeriods()) / (double) (lruCacheStats
198           .getSumRequestCountsPastNPeriods() + bucketCacheStats
199           .getSumRequestCountsPastNPeriods()));
200       return Double.isNaN(ratio) ? 0 : ratio;
201     }
202 
203     @Override
204     public double getHitCachingRatioPastNPeriods() {
205       double ratio = ((double) (lruCacheStats
206           .getSumHitCachingCountsPastNPeriods() + bucketCacheStats
207           .getSumHitCachingCountsPastNPeriods()) / (double) (lruCacheStats
208           .getSumRequestCachingCountsPastNPeriods() + bucketCacheStats
209           .getSumRequestCachingCountsPastNPeriods()));
210       return Double.isNaN(ratio) ? 0 : ratio;
211     }
212 
213   }
214 
215 }