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.hadoop.hbase.classification.InterfaceAudience; 24 import org.apache.hadoop.hbase.io.hfile.Cacheable.MemoryType; 25 26 /** 27 * Block cache interface. Anything that implements the {@link Cacheable} 28 * interface can be put in the cache. 29 */ 30 @InterfaceAudience.Private 31 public interface BlockCache extends Iterable<CachedBlock> { 32 /** 33 * Add block to cache. 34 * @param cacheKey The block's cache key. 35 * @param buf The block contents wrapped in a ByteBuffer. 36 * @param inMemory Whether block should be treated as in-memory 37 * @param cacheDataInL1 If multi-tier block cache deploy -- i.e. has an L1 and L2 tier -- then 38 * if this flag is true, cache data blocks up in the L1 tier (meta blocks are probably being 39 * cached in L1 already). 40 */ 41 void cacheBlock(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory, boolean cacheDataInL1); 42 43 /** 44 * Add block to cache (defaults to not in-memory). 45 * @param cacheKey The block's cache key. 46 * @param buf The object to cache. 47 */ 48 void cacheBlock(BlockCacheKey cacheKey, Cacheable buf); 49 50 /** 51 * Fetch block from cache. 52 * @param cacheKey Block to fetch. 53 * @param caching Whether this request has caching enabled (used for stats) 54 * @param repeat Whether this is a repeat lookup for the same block 55 * (used to avoid double counting cache misses when doing double-check locking) 56 * @param updateCacheMetrics Whether to update cache metrics or not 57 * @return Block or null if block is not in 2 cache. 58 */ 59 Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat, 60 boolean updateCacheMetrics); 61 62 /** 63 * Evict block from cache. 64 * @param cacheKey Block to evict 65 * @return true if block existed and was evicted, false if not 66 */ 67 boolean evictBlock(BlockCacheKey cacheKey); 68 69 /** 70 * Evicts all blocks for the given HFile. 71 * 72 * @return the number of blocks evicted 73 */ 74 int evictBlocksByHfileName(String hfileName); 75 76 /** 77 * Get the statistics for this block cache. 78 * @return Stats 79 */ 80 CacheStats getStats(); 81 82 /** 83 * Shutdown the cache. 84 */ 85 void shutdown(); 86 87 /** 88 * Returns the total size of the block cache, in bytes. 89 * @return size of cache, in bytes 90 */ 91 long size(); 92 93 /** 94 * Returns the free size of the block cache, in bytes. 95 * @return free space in cache, in bytes 96 */ 97 long getFreeSize(); 98 99 /** 100 * Returns the occupied size of the block cache, in bytes. 101 * @return occupied space in cache, in bytes 102 */ 103 long getCurrentSize(); 104 105 /** 106 * Returns the number of blocks currently cached in the block cache. 107 * @return number of blocks in the cache 108 */ 109 long getBlockCount(); 110 111 /** 112 * @return Iterator over the blocks in the cache. 113 */ 114 Iterator<CachedBlock> iterator(); 115 116 /** 117 * @return The list of sub blockcaches that make up this one; returns null if no sub caches. 118 */ 119 BlockCache [] getBlockCaches(); 120 121 /** 122 * Called when the scanner using the block decides to return the block once its usage 123 * is over. 124 * This API should be called after the block is used, failing to do so may have adverse effects 125 * by preventing the blocks from being evicted because of which it will prevent new hot blocks 126 * from getting added to the block cache. The implementation of the BlockCache will decide 127 * on what to be done with the block based on the memory type of the block's {@link MemoryType}. 128 * @param cacheKey the cache key of the block 129 * @param block the hfileblock to be returned 130 */ 131 void returnBlock(BlockCacheKey cacheKey, Cacheable block); 132 }