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 ResizableBlockCache, 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.byteDesc(onHeapSize)
68          + " with an average block size of "
69          + StringUtils.byteDesc(onHeapBlockSize));
70      onHeapCache = new LruBlockCache(onHeapSize, onHeapBlockSize, conf);
71  
72      LOG.info("Creating off-heap cache of size "
73          + StringUtils.byteDesc(offHeapSize)
74          + "with an average block size of "
75          + StringUtils.byteDesc(offHeapBlockSize));
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        final boolean cacheDataInL1) {
85      onHeapCache.cacheBlock(cacheKey, buf, inMemory, cacheDataInL1);
86      offHeapCache.cacheBlock(cacheKey, buf);
87    }
88  
89    @Override
90    public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf) {
91      onHeapCache.cacheBlock(cacheKey, buf);
92      offHeapCache.cacheBlock(cacheKey, buf);
93    }
94  
95    @Override
96    public Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat,
97        boolean updateCacheMetrics) {
98      Cacheable cachedBlock;
99  
100     if ((cachedBlock = onHeapCache.getBlock(cacheKey, caching, repeat,
101         updateCacheMetrics)) != null) {
102       if (updateCacheMetrics) stats.hit(caching);
103       return cachedBlock;
104 
105     } else if ((cachedBlock = offHeapCache.getBlock(cacheKey, caching, repeat,
106         updateCacheMetrics)) != null) {
107       if (caching) {
108         onHeapCache.cacheBlock(cacheKey, cachedBlock);
109       }
110       if (updateCacheMetrics) stats.hit(caching);
111       return cachedBlock;
112     }
113 
114     if (!repeat && updateCacheMetrics) stats.miss(caching);
115     return null;
116   }
117 
118   @Override
119   public boolean evictBlock(BlockCacheKey cacheKey) {
120     stats.evict();
121     boolean cacheA = onHeapCache.evictBlock(cacheKey);
122     boolean cacheB = offHeapCache.evictBlock(cacheKey);
123     boolean evicted = cacheA || cacheB;
124     if (evicted) {
125       stats.evicted();
126     }
127     return evicted;
128   }
129 
130   @Override
131   public CacheStats getStats() {
132     return this.stats;
133   }
134 
135   @Override
136   public void shutdown() {
137     onHeapCache.shutdown();
138     offHeapCache.shutdown();
139   }
140 
141   @Override
142   public long heapSize() {
143     return onHeapCache.heapSize() + offHeapCache.heapSize();
144   }
145 
146   public long size() {
147     return onHeapCache.size() + offHeapCache.size();
148   }
149 
150   public long getFreeSize() {
151     return onHeapCache.getFreeSize() + offHeapCache.getFreeSize();
152   }
153 
154   public long getCurrentSize() {
155     return onHeapCache.getCurrentSize() + offHeapCache.getCurrentSize();
156   }
157 
158   @Override
159   public int evictBlocksByHfileName(String hfileName) {
160     onHeapCache.evictBlocksByHfileName(hfileName);
161     offHeapCache.evictBlocksByHfileName(hfileName);
162     return 0;
163   }
164 
165   @Override
166   public long getBlockCount() {
167     return onHeapCache.getBlockCount() + offHeapCache.getBlockCount();
168   }
169 
170   @Override
171   public void setMaxSize(long size) {
172     this.onHeapCache.setMaxSize(size);
173   }
174 
175   @Override
176   public Iterator<CachedBlock> iterator() {
177     return new BlockCachesIterator(getBlockCaches());
178   }
179 
180   @Override
181   public BlockCache[] getBlockCaches() {
182     return new BlockCache [] {this.onHeapCache, this.offHeapCache};
183   }
184 }