View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  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,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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.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   * DoubleBlockCache is an abstraction layer that combines two caches, the
33   * smaller onHeapCache and the larger offHeapCache. CacheBlock attempts to cache
34   * the block in both caches, while readblock reads first from the faster on heap
35   * cache before looking for the block in the off heap cache. Metrics are the
36   * combined size and hits and misses of both caches.
37   *
38   * @deprecated As of 1.0, replaced by {@link org.apache.hadoop.hbase.io.hfile.bucket.BucketCache}.
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     * Default constructor. Specify maximum size and expected average block size
52     * (approximation is fine).
53     * <p>
54     * All other factors will be calculated based on defaults specified in this
55     * class.
56     *
57     * @param onHeapSize maximum size of the onHeapCache, in bytes.
58     * @param offHeapSize maximum size of the offHeapCache, in bytes.
59     * @param onHeapBlockSize average block size of the on heap cache.
60     * @param offHeapBlockSize average block size for the off heap cache
61     * @param conf configuration file. currently used only by the off heap cache.
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 }