001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.io.hfile; 019 020import static java.util.Objects.requireNonNull; 021 022import java.lang.ref.WeakReference; 023import java.util.EnumMap; 024import java.util.Iterator; 025import java.util.List; 026import java.util.Map; 027import java.util.PriorityQueue; 028import java.util.concurrent.ConcurrentHashMap; 029import java.util.concurrent.Executors; 030import java.util.concurrent.ScheduledExecutorService; 031import java.util.concurrent.TimeUnit; 032import java.util.concurrent.atomic.AtomicLong; 033import java.util.concurrent.atomic.LongAdder; 034import java.util.concurrent.locks.ReentrantLock; 035import org.apache.hadoop.conf.Configuration; 036import org.apache.hadoop.hbase.io.HeapSize; 037import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding; 038import org.apache.hadoop.hbase.util.ClassSize; 039import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 040import org.apache.hadoop.util.StringUtils; 041import org.apache.yetus.audience.InterfaceAudience; 042import org.slf4j.Logger; 043import org.slf4j.LoggerFactory; 044 045import org.apache.hbase.thirdparty.com.google.common.base.MoreObjects; 046import org.apache.hbase.thirdparty.com.google.common.base.Objects; 047import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder; 048 049/** 050 * <b>This realisation improve performance of classical LRU cache up to 3 times via reduce GC 051 * job.</b> 052 * </p> 053 * The classical block cache implementation that is memory-aware using {@link HeapSize}, 054 * memory-bound using an LRU eviction algorithm, and concurrent: backed by a 055 * {@link ConcurrentHashMap} and with a non-blocking eviction thread giving constant-time 056 * {@link #cacheBlock} and {@link #getBlock} operations. 057 * </p> 058 * Contains three levels of block priority to allow for scan-resistance and in-memory families 059 * {@link org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder#setInMemory(boolean)} (An 060 * in-memory column family is a column family that should be served from memory if possible): 061 * single-access, multiple-accesses, and in-memory priority. A block is added with an in-memory 062 * priority flag if {@link org.apache.hadoop.hbase.client.ColumnFamilyDescriptor#isInMemory()}, 063 * otherwise a block becomes a single access priority the first time it is read into this block 064 * cache. If a block is accessed again while in cache, it is marked as a multiple access priority 065 * block. This delineation of blocks is used to prevent scans from thrashing the cache adding a 066 * least-frequently-used element to the eviction algorithm. 067 * <p/> 068 * Each priority is given its own chunk of the total cache to ensure fairness during eviction. Each 069 * priority will retain close to its maximum size, however, if any priority is not using its entire 070 * chunk the others are able to grow beyond their chunk size. 071 * <p/> 072 * Instantiated at a minimum with the total size and average block size. All sizes are in bytes. The 073 * block size is not especially important as this cache is fully dynamic in its sizing of blocks. It 074 * is only used for pre-allocating data structures and in initial heap estimation of the map. 075 * <p/> 076 * The detailed constructor defines the sizes for the three priorities (they should total to the 077 * <code>maximum size</code> defined). It also sets the levels that trigger and control the eviction 078 * thread. 079 * <p/> 080 * The <code>acceptable size</code> is the cache size level which triggers the eviction process to 081 * start. It evicts enough blocks to get the size below the minimum size specified. 082 * <p/> 083 * Eviction happens in a separate thread and involves a single full-scan of the map. It determines 084 * how many bytes must be freed to reach the minimum size, and then while scanning determines the 085 * fewest least-recently-used blocks necessary from each of the three priorities (would be 3 times 086 * bytes to free). It then uses the priority chunk sizes to evict fairly according to the relative 087 * sizes and usage. 088 * <p/> 089 * Adaptive LRU cache lets speed up performance while we are reading much more data than can fit 090 * into BlockCache and it is the cause of a high rate of evictions. This in turn leads to heavy 091 * Garbage Collector works. So a lot of blocks put into BlockCache but never read, but spending a 092 * lot of CPU resources for cleaning. We could avoid this situation via parameters: 093 * <p/> 094 * <b>hbase.lru.cache.heavy.eviction.count.limit</b> - set how many times we have to run the 095 * eviction process that starts to avoid putting data to BlockCache. By default it is 0 and it meats 096 * the feature will start at the beginning. But if we have some times short reading the same data 097 * and some times long-term reading - we can divide it by this parameter. For example we know that 098 * our short reading used to be about 1 minutes, then we have to set the parameter about 10 and it 099 * will enable the feature only for long time massive reading (after ~100 seconds). So when we use 100 * short-reading and want all of them in the cache we will have it (except for eviction of course). 101 * When we use long-term heavy reading the feature will be enabled after some time and bring better 102 * performance. 103 * <p/> 104 * <b>hbase.lru.cache.heavy.eviction.mb.size.limit</b> - set how many bytes in 10 seconds desirable 105 * putting into BlockCache (and evicted from it). The feature will try to reach this value and 106 * maintain it. Don't try to set it too small because it leads to premature exit from this mode. For 107 * powerful CPUs (about 20-40 physical cores) it could be about 400-500 MB. Average system (~10 108 * cores) 200-300 MB. Some weak systems (2-5 cores) may be good with 50-100 MB. How it works: we set 109 * the limit and after each ~10 second calculate how many bytes were freed. Overhead = Freed Bytes 110 * Sum (MB) * 100 / Limit (MB) - 100; For example we set the limit = 500 and were evicted 2000 MB. 111 * Overhead is: 2000 * 100 / 500 - 100 = 300% The feature is going to reduce a percent caching data 112 * blocks and fit evicted bytes closer to 100% (500 MB). Some kind of an auto-scaling. If freed 113 * bytes less then the limit we have got negative overhead. For example if were freed 200 MB: 200 * 114 * 100 / 500 - 100 = -60% The feature will increase the percent of caching blocks. That leads to fit 115 * evicted bytes closer to 100% (500 MB). The current situation we can find out in the log of 116 * RegionServer: BlockCache evicted (MB): 0, overhead (%): -100, heavy eviction counter: 0, current 117 * caching DataBlock (%): 100 - means no eviction, 100% blocks is caching BlockCache evicted (MB): 118 * 2000, overhead (%): 300, heavy eviction counter: 1, current caching DataBlock (%): 97 - means 119 * eviction begin, reduce of caching blocks by 3%. It help to tune your system and find out what 120 * value is better set. Don't try to reach 0% overhead, it is impossible. Quite good 50-100% 121 * overhead, it prevents premature exit from this mode. 122 * <p/> 123 * <b>hbase.lru.cache.heavy.eviction.overhead.coefficient</b> - set how fast we want to get the 124 * result. If we know that our reading is heavy for a long time, we don't want to wait and can 125 * increase the coefficient and get good performance sooner. But if we aren't sure we can do it 126 * slowly and it could prevent premature exit from this mode. So, when the coefficient is higher we 127 * can get better performance when heavy reading is stable. But when reading is changing we can 128 * adjust to it and set the coefficient to lower value. For example, we set the coefficient = 0.01. 129 * It means the overhead (see above) will be multiplied by 0.01 and the result is the value of 130 * reducing percent caching blocks. For example, if the overhead = 300% and the coefficient = 0.01, 131 * then percent of caching blocks will reduce by 3%. Similar logic when overhead has got negative 132 * value (overshooting). Maybe it is just short-term fluctuation and we will try to stay in this 133 * mode. It helps avoid premature exit during short-term fluctuation. Backpressure has simple logic: 134 * more overshooting - more caching blocks. 135 * <p/> 136 * Find more information about improvement: https://issues.apache.org/jira/browse/HBASE-23887 137 */ 138@InterfaceAudience.Private 139public class LruAdaptiveBlockCache implements FirstLevelBlockCache { 140 141 private static final Logger LOG = LoggerFactory.getLogger(LruAdaptiveBlockCache.class); 142 143 /** 144 * Percentage of total size that eviction will evict until; e.g. if set to .8, then we will keep 145 * evicting during an eviction run till the cache size is down to 80% of the total. 146 */ 147 private static final String LRU_MIN_FACTOR_CONFIG_NAME = "hbase.lru.blockcache.min.factor"; 148 149 /** 150 * Acceptable size of cache (no evictions if {@code size < acceptable}) 151 */ 152 private static final String LRU_ACCEPTABLE_FACTOR_CONFIG_NAME = 153 "hbase.lru.blockcache.acceptable.factor"; 154 155 /** 156 * Hard capacity limit of cache, will reject any put if size > this * acceptable 157 */ 158 static final String LRU_HARD_CAPACITY_LIMIT_FACTOR_CONFIG_NAME = 159 "hbase.lru.blockcache.hard.capacity.limit.factor"; 160 private static final String LRU_SINGLE_PERCENTAGE_CONFIG_NAME = 161 "hbase.lru.blockcache.single.percentage"; 162 private static final String LRU_MULTI_PERCENTAGE_CONFIG_NAME = 163 "hbase.lru.blockcache.multi.percentage"; 164 private static final String LRU_MEMORY_PERCENTAGE_CONFIG_NAME = 165 "hbase.lru.blockcache.memory.percentage"; 166 167 /** 168 * Configuration key to force data-block always (except in-memory are too much) cached in memory 169 * for in-memory hfile, unlike inMemory, which is a column-family configuration, inMemoryForceMode 170 * is a cluster-wide configuration 171 */ 172 private static final String LRU_IN_MEMORY_FORCE_MODE_CONFIG_NAME = 173 "hbase.lru.rs.inmemoryforcemode"; 174 175 /* Default Configuration Parameters */ 176 177 /* Backing Concurrent Map Configuration */ 178 static final float DEFAULT_LOAD_FACTOR = 0.75f; 179 static final int DEFAULT_CONCURRENCY_LEVEL = 16; 180 181 /* Eviction thresholds */ 182 private static final float DEFAULT_MIN_FACTOR = 0.95f; 183 static final float DEFAULT_ACCEPTABLE_FACTOR = 0.99f; 184 185 /* Priority buckets */ 186 private static final float DEFAULT_SINGLE_FACTOR = 0.25f; 187 private static final float DEFAULT_MULTI_FACTOR = 0.50f; 188 private static final float DEFAULT_MEMORY_FACTOR = 0.25f; 189 190 private static final float DEFAULT_HARD_CAPACITY_LIMIT_FACTOR = 1.2f; 191 192 private static final boolean DEFAULT_IN_MEMORY_FORCE_MODE = false; 193 194 /* Statistics thread */ 195 private static final int STAT_THREAD_PERIOD = 60 * 5; 196 private static final String LRU_MAX_BLOCK_SIZE = "hbase.lru.max.block.size"; 197 private static final long DEFAULT_MAX_BLOCK_SIZE = 16L * 1024L * 1024L; 198 199 private static final String LRU_CACHE_HEAVY_EVICTION_COUNT_LIMIT = 200 "hbase.lru.cache.heavy.eviction.count.limit"; 201 // Default value actually equal to disable feature of increasing performance. 202 // Because 2147483647 is about ~680 years (after that it will start to work) 203 // We can set it to 0-10 and get the profit right now. 204 // (see details https://issues.apache.org/jira/browse/HBASE-23887). 205 private static final int DEFAULT_LRU_CACHE_HEAVY_EVICTION_COUNT_LIMIT = Integer.MAX_VALUE; 206 207 private static final String LRU_CACHE_HEAVY_EVICTION_MB_SIZE_LIMIT = 208 "hbase.lru.cache.heavy.eviction.mb.size.limit"; 209 private static final long DEFAULT_LRU_CACHE_HEAVY_EVICTION_MB_SIZE_LIMIT = 500; 210 211 private static final String LRU_CACHE_HEAVY_EVICTION_OVERHEAD_COEFFICIENT = 212 "hbase.lru.cache.heavy.eviction.overhead.coefficient"; 213 private static final float DEFAULT_LRU_CACHE_HEAVY_EVICTION_OVERHEAD_COEFFICIENT = 0.01f; 214 215 /** 216 * Defined the cache map as {@link ConcurrentHashMap} here, because in 217 * {@link LruAdaptiveBlockCache#getBlock}, we need to guarantee the atomicity of 218 * map#computeIfPresent (key, func). Besides, the func method must execute exactly once only when 219 * the key is present and under the lock context, otherwise the reference count will be messed up. 220 * Notice that the {@link java.util.concurrent.ConcurrentSkipListMap} can not guarantee that. 221 */ 222 private transient final ConcurrentHashMap<BlockCacheKey, LruCachedBlock> map; 223 224 /** Eviction lock (locked when eviction in process) */ 225 private transient final ReentrantLock evictionLock = new ReentrantLock(true); 226 227 private final long maxBlockSize; 228 229 /** Volatile boolean to track if we are in an eviction process or not */ 230 private volatile boolean evictionInProgress = false; 231 232 /** Eviction thread */ 233 private transient final EvictionThread evictionThread; 234 235 /** Statistics thread schedule pool (for heavy debugging, could remove) */ 236 private transient final ScheduledExecutorService scheduleThreadPool = 237 Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder() 238 .setNameFormat("LruAdaptiveBlockCacheStatsExecutor").setDaemon(true).build()); 239 240 /** Current size of cache */ 241 private final AtomicLong size; 242 243 /** Current size of data blocks */ 244 private final LongAdder dataBlockSize; 245 246 /** Current number of cached elements */ 247 private final AtomicLong elements; 248 249 /** Current number of cached data block elements */ 250 private final LongAdder dataBlockElements; 251 252 /** Cache access count (sequential ID) */ 253 private final AtomicLong count; 254 255 /** hard capacity limit */ 256 private final float hardCapacityLimitFactor; 257 258 /** Cache statistics */ 259 private final CacheStats stats; 260 261 /** Maximum allowable size of cache (block put if size > max, evict) */ 262 private long maxSize; 263 264 /** Approximate block size */ 265 private final long blockSize; 266 267 /** Acceptable size of cache (no evictions if {@code size < acceptable}) */ 268 private final float acceptableFactor; 269 270 /** Minimum threshold of cache (when evicting, evict until {@code size < min}) */ 271 private final float minFactor; 272 273 /** Single access bucket size */ 274 private final float singleFactor; 275 276 /** Multiple access bucket size */ 277 private final float multiFactor; 278 279 /** In-memory bucket size */ 280 private final float memoryFactor; 281 282 /** Overhead of the structure itself */ 283 private final long overhead; 284 285 /** Whether in-memory hfile's data block has higher priority when evicting */ 286 private boolean forceInMemory; 287 288 /** 289 * Where to send victims (blocks evicted/missing from the cache). This is used only when we use an 290 * external cache as L2. Note: See org.apache.hadoop.hbase.io.hfile.MemcachedBlockCache 291 */ 292 private transient BlockCache victimHandler = null; 293 294 /** Percent of cached data blocks */ 295 private volatile int cacheDataBlockPercent; 296 297 /** Limit of count eviction process when start to avoid to cache blocks */ 298 private final int heavyEvictionCountLimit; 299 300 /** Limit of volume eviction process when start to avoid to cache blocks */ 301 private final long heavyEvictionMbSizeLimit; 302 303 /** Adjust auto-scaling via overhead of evition rate */ 304 private final float heavyEvictionOverheadCoefficient; 305 306 /** 307 * Default constructor. Specify maximum size and expected average block size (approximation is 308 * fine). 309 * <p> 310 * All other factors will be calculated based on defaults specified in this class. 311 * @param maxSize maximum size of cache, in bytes 312 * @param blockSize approximate size of each block, in bytes 313 */ 314 public LruAdaptiveBlockCache(long maxSize, long blockSize) { 315 this(maxSize, blockSize, true); 316 } 317 318 /** 319 * Constructor used for testing. Allows disabling of the eviction thread. 320 */ 321 public LruAdaptiveBlockCache(long maxSize, long blockSize, boolean evictionThread) { 322 this(maxSize, blockSize, evictionThread, (int) Math.ceil(1.2 * maxSize / blockSize), 323 DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL, DEFAULT_MIN_FACTOR, DEFAULT_ACCEPTABLE_FACTOR, 324 DEFAULT_SINGLE_FACTOR, DEFAULT_MULTI_FACTOR, DEFAULT_MEMORY_FACTOR, 325 DEFAULT_HARD_CAPACITY_LIMIT_FACTOR, false, DEFAULT_MAX_BLOCK_SIZE, 326 DEFAULT_LRU_CACHE_HEAVY_EVICTION_COUNT_LIMIT, DEFAULT_LRU_CACHE_HEAVY_EVICTION_MB_SIZE_LIMIT, 327 DEFAULT_LRU_CACHE_HEAVY_EVICTION_OVERHEAD_COEFFICIENT); 328 } 329 330 public LruAdaptiveBlockCache(long maxSize, long blockSize, boolean evictionThread, 331 Configuration conf) { 332 this(maxSize, blockSize, evictionThread, (int) Math.ceil(1.2 * maxSize / blockSize), 333 DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL, 334 conf.getFloat(LRU_MIN_FACTOR_CONFIG_NAME, DEFAULT_MIN_FACTOR), 335 conf.getFloat(LRU_ACCEPTABLE_FACTOR_CONFIG_NAME, DEFAULT_ACCEPTABLE_FACTOR), 336 conf.getFloat(LRU_SINGLE_PERCENTAGE_CONFIG_NAME, DEFAULT_SINGLE_FACTOR), 337 conf.getFloat(LRU_MULTI_PERCENTAGE_CONFIG_NAME, DEFAULT_MULTI_FACTOR), 338 conf.getFloat(LRU_MEMORY_PERCENTAGE_CONFIG_NAME, DEFAULT_MEMORY_FACTOR), 339 conf.getFloat(LRU_HARD_CAPACITY_LIMIT_FACTOR_CONFIG_NAME, DEFAULT_HARD_CAPACITY_LIMIT_FACTOR), 340 conf.getBoolean(LRU_IN_MEMORY_FORCE_MODE_CONFIG_NAME, DEFAULT_IN_MEMORY_FORCE_MODE), 341 conf.getLong(LRU_MAX_BLOCK_SIZE, DEFAULT_MAX_BLOCK_SIZE), 342 conf.getInt(LRU_CACHE_HEAVY_EVICTION_COUNT_LIMIT, 343 DEFAULT_LRU_CACHE_HEAVY_EVICTION_COUNT_LIMIT), 344 conf.getLong(LRU_CACHE_HEAVY_EVICTION_MB_SIZE_LIMIT, 345 DEFAULT_LRU_CACHE_HEAVY_EVICTION_MB_SIZE_LIMIT), 346 conf.getFloat(LRU_CACHE_HEAVY_EVICTION_OVERHEAD_COEFFICIENT, 347 DEFAULT_LRU_CACHE_HEAVY_EVICTION_OVERHEAD_COEFFICIENT)); 348 } 349 350 public LruAdaptiveBlockCache(long maxSize, long blockSize, Configuration conf) { 351 this(maxSize, blockSize, true, conf); 352 } 353 354 /** 355 * Configurable constructor. Use this constructor if not using defaults. 356 * @param maxSize maximum size of this cache, in bytes 357 * @param blockSize expected average size of blocks, in bytes 358 * @param evictionThread whether to run evictions in a bg thread or not 359 * @param mapInitialSize initial size of backing ConcurrentHashMap 360 * @param mapLoadFactor initial load factor of backing ConcurrentHashMap 361 * @param mapConcurrencyLevel initial concurrency factor for backing CHM 362 * @param minFactor percentage of total size that eviction will evict until 363 * @param acceptableFactor percentage of total size that triggers eviction 364 * @param singleFactor percentage of total size for single-access blocks 365 * @param multiFactor percentage of total size for multiple-access blocks 366 * @param memoryFactor percentage of total size for in-memory blocks 367 * @param hardLimitFactor hard capacity limit 368 * @param forceInMemory in-memory hfile's data block has higher priority when 369 * evicting 370 * @param maxBlockSize maximum block size for caching 371 * @param heavyEvictionCountLimit when starts AdaptiveLRU algoritm work 372 * @param heavyEvictionMbSizeLimit how many bytes desirable putting into BlockCache 373 * @param heavyEvictionOverheadCoefficient how aggressive AdaptiveLRU will reduce GC 374 */ 375 public LruAdaptiveBlockCache(long maxSize, long blockSize, boolean evictionThread, 376 int mapInitialSize, float mapLoadFactor, int mapConcurrencyLevel, float minFactor, 377 float acceptableFactor, float singleFactor, float multiFactor, float memoryFactor, 378 float hardLimitFactor, boolean forceInMemory, long maxBlockSize, int heavyEvictionCountLimit, 379 long heavyEvictionMbSizeLimit, float heavyEvictionOverheadCoefficient) { 380 this.maxBlockSize = maxBlockSize; 381 if ( 382 singleFactor + multiFactor + memoryFactor != 1 || singleFactor < 0 || multiFactor < 0 383 || memoryFactor < 0 384 ) { 385 throw new IllegalArgumentException( 386 "Single, multi, and memory factors " + " should be non-negative and total 1.0"); 387 } 388 if (minFactor >= acceptableFactor) { 389 throw new IllegalArgumentException("minFactor must be smaller than acceptableFactor"); 390 } 391 if (minFactor >= 1.0f || acceptableFactor >= 1.0f) { 392 throw new IllegalArgumentException("all factors must be < 1"); 393 } 394 this.maxSize = maxSize; 395 this.blockSize = blockSize; 396 this.forceInMemory = forceInMemory; 397 map = new ConcurrentHashMap<>(mapInitialSize, mapLoadFactor, mapConcurrencyLevel); 398 this.minFactor = minFactor; 399 this.acceptableFactor = acceptableFactor; 400 this.singleFactor = singleFactor; 401 this.multiFactor = multiFactor; 402 this.memoryFactor = memoryFactor; 403 this.stats = new CacheStats(this.getClass().getSimpleName()); 404 this.count = new AtomicLong(0); 405 this.elements = new AtomicLong(0); 406 this.dataBlockElements = new LongAdder(); 407 this.dataBlockSize = new LongAdder(); 408 this.overhead = calculateOverhead(maxSize, blockSize, mapConcurrencyLevel); 409 this.size = new AtomicLong(this.overhead); 410 this.hardCapacityLimitFactor = hardLimitFactor; 411 if (evictionThread) { 412 this.evictionThread = new EvictionThread(this); 413 this.evictionThread.start(); // FindBugs SC_START_IN_CTOR 414 } else { 415 this.evictionThread = null; 416 } 417 418 // check the bounds 419 this.heavyEvictionCountLimit = Math.max(heavyEvictionCountLimit, 0); 420 this.heavyEvictionMbSizeLimit = Math.max(heavyEvictionMbSizeLimit, 1); 421 this.cacheDataBlockPercent = 100; 422 heavyEvictionOverheadCoefficient = Math.min(heavyEvictionOverheadCoefficient, 1.0f); 423 heavyEvictionOverheadCoefficient = Math.max(heavyEvictionOverheadCoefficient, 0.001f); 424 this.heavyEvictionOverheadCoefficient = heavyEvictionOverheadCoefficient; 425 426 // TODO: Add means of turning this off. Bit obnoxious running thread just to make a log 427 // every five minutes. 428 this.scheduleThreadPool.scheduleAtFixedRate(new StatisticsThread(this), STAT_THREAD_PERIOD, 429 STAT_THREAD_PERIOD, TimeUnit.SECONDS); 430 } 431 432 @Override 433 public void setVictimCache(BlockCache victimCache) { 434 if (victimHandler != null) { 435 throw new IllegalArgumentException("The victim cache has already been set"); 436 } 437 victimHandler = requireNonNull(victimCache); 438 } 439 440 @Override 441 public void setMaxSize(long maxSize) { 442 this.maxSize = maxSize; 443 if (this.size.get() > acceptableSize() && !evictionInProgress) { 444 runEviction(); 445 } 446 } 447 448 public int getCacheDataBlockPercent() { 449 return cacheDataBlockPercent; 450 } 451 452 /** 453 * The block cached in LruAdaptiveBlockCache will always be an heap block: on the one side, the 454 * heap access will be more faster then off-heap, the small index block or meta block cached in 455 * CombinedBlockCache will benefit a lot. on other side, the LruAdaptiveBlockCache size is always 456 * calculated based on the total heap size, if caching an off-heap block in LruAdaptiveBlockCache, 457 * the heap size will be messed up. Here we will clone the block into an heap block if it's an 458 * off-heap block, otherwise just use the original block. The key point is maintain the refCnt of 459 * the block (HBASE-22127): <br> 460 * 1. if cache the cloned heap block, its refCnt is an totally new one, it's easy to handle; <br> 461 * 2. if cache the original heap block, we're sure that it won't be tracked in ByteBuffAllocator's 462 * reservoir, if both RPC and LruAdaptiveBlockCache release the block, then it can be garbage 463 * collected by JVM, so need a retain here. 464 * @param buf the original block 465 * @return an block with an heap memory backend. 466 */ 467 private Cacheable asReferencedHeapBlock(Cacheable buf) { 468 if (buf instanceof HFileBlock) { 469 HFileBlock blk = ((HFileBlock) buf); 470 if (blk.isSharedMem()) { 471 return HFileBlock.deepCloneOnHeap(blk); 472 } 473 } 474 // The block will be referenced by this LruAdaptiveBlockCache, 475 // so should increase its refCnt here. 476 return buf.retain(); 477 } 478 479 // BlockCache implementation 480 481 /** 482 * Cache the block with the specified name and buffer. 483 * <p> 484 * It is assumed this will NOT be called on an already cached block. In rare cases (HBASE-8547) 485 * this can happen, for which we compare the buffer contents. 486 * @param cacheKey block's cache key 487 * @param buf block buffer 488 * @param inMemory if block is in-memory 489 */ 490 @Override 491 public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory) { 492 493 // Some data blocks will not put into BlockCache when eviction rate too much. 494 // It is good for performance 495 // (see details: https://issues.apache.org/jira/browse/HBASE-23887) 496 // How to calculate it can find inside EvictionThread class. 497 if (cacheDataBlockPercent != 100 && buf.getBlockType().isData()) { 498 // It works like filter - blocks which two last digits of offset 499 // more than we calculate in Eviction Thread will not put into BlockCache 500 if (cacheKey.getOffset() % 100 >= cacheDataBlockPercent) { 501 return; 502 } 503 } 504 505 if (buf.heapSize() > maxBlockSize) { 506 // If there are a lot of blocks that are too 507 // big this can make the logs way too noisy. 508 // So we log 2% 509 if (stats.failInsert() % 50 == 0) { 510 LOG.warn("Trying to cache too large a block " + cacheKey.getHfileName() + " @ " 511 + cacheKey.getOffset() + " is " + buf.heapSize() + " which is larger than " 512 + maxBlockSize); 513 } 514 return; 515 } 516 517 LruCachedBlock cb = map.get(cacheKey); 518 if (cb != null && !BlockCacheUtil.shouldReplaceExistingCacheBlock(this, cacheKey, buf)) { 519 return; 520 } 521 long currentSize = size.get(); 522 long currentAcceptableSize = acceptableSize(); 523 long hardLimitSize = (long) (hardCapacityLimitFactor * currentAcceptableSize); 524 if (currentSize >= hardLimitSize) { 525 stats.failInsert(); 526 if (LOG.isTraceEnabled()) { 527 LOG.trace("LruAdaptiveBlockCache current size " + StringUtils.byteDesc(currentSize) 528 + " has exceeded acceptable size " + StringUtils.byteDesc(currentAcceptableSize) + "." 529 + " The hard limit size is " + StringUtils.byteDesc(hardLimitSize) 530 + ", failed to put cacheKey:" + cacheKey + " into LruAdaptiveBlockCache."); 531 } 532 if (!evictionInProgress) { 533 runEviction(); 534 } 535 return; 536 } 537 // Ensure that the block is an heap one. 538 buf = asReferencedHeapBlock(buf); 539 cb = new LruCachedBlock(cacheKey, buf, count.incrementAndGet(), inMemory); 540 long newSize = updateSizeMetrics(cb, false); 541 LruCachedBlock previous = map.put(cacheKey, cb); 542 if (previous != null) { 543 // We replaced an existing block (see BlockCacheUtil#shouldReplaceExistingCacheBlock), so 544 // take the replaced block out of the size and element counters, otherwise it is counted 545 // forever and the cache fills up with blocks it no longer holds. 546 newSize = updateSizeMetrics(previous, true); 547 elements.decrementAndGet(); 548 if (previous.getBuffer().getBlockType().isData()) { 549 dataBlockElements.decrement(); 550 } 551 // The cache no longer references the replaced block. 552 previous.getBuffer().release(); 553 } 554 long val = elements.incrementAndGet(); 555 if (buf.getBlockType().isData()) { 556 dataBlockElements.increment(); 557 } 558 if (LOG.isTraceEnabled()) { 559 long size = map.size(); 560 assertCounterSanity(size, val); 561 } 562 if (newSize > currentAcceptableSize && !evictionInProgress) { 563 runEviction(); 564 } 565 } 566 567 /** 568 * Sanity-checking for parity between actual block cache content and metrics. Intended only for 569 * use with TRACE level logging and -ea JVM. 570 */ 571 private static void assertCounterSanity(long mapSize, long counterVal) { 572 if (counterVal < 0) { 573 LOG.trace("counterVal overflow. Assertions unreliable. counterVal=" + counterVal 574 + ", mapSize=" + mapSize); 575 return; 576 } 577 if (mapSize < Integer.MAX_VALUE) { 578 double pct_diff = Math.abs((((double) counterVal) / ((double) mapSize)) - 1.); 579 if (pct_diff > 0.05) { 580 LOG.trace("delta between reported and actual size > 5%. counterVal=" + counterVal 581 + ", mapSize=" + mapSize); 582 } 583 } 584 } 585 586 /** 587 * Cache the block with the specified name and buffer. 588 * <p> 589 * TODO after HBASE-22005, we may cache an block which allocated from off-heap, but our LRU cache 590 * sizing is based on heap size, so we should handle this in HBASE-22127. It will introduce an 591 * switch whether make the LRU on-heap or not, if so we may need copy the memory to on-heap, 592 * otherwise the caching size is based on off-heap. 593 * @param cacheKey block's cache key 594 * @param buf block buffer 595 */ 596 @Override 597 public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf) { 598 cacheBlock(cacheKey, buf, false); 599 } 600 601 /** 602 * Helper function that updates the local size counter and also updates any per-cf or 603 * per-blocktype metrics it can discern from given {@link LruCachedBlock} 604 */ 605 private long updateSizeMetrics(LruCachedBlock cb, boolean evict) { 606 long heapsize = cb.heapSize(); 607 BlockType bt = cb.getBuffer().getBlockType(); 608 if (evict) { 609 heapsize *= -1; 610 } 611 if (bt != null && bt.isData()) { 612 dataBlockSize.add(heapsize); 613 } 614 return size.addAndGet(heapsize); 615 } 616 617 /** 618 * Get the buffer of the block with the specified name. 619 * @param cacheKey block's cache key 620 * @param caching true if the caller caches blocks on cache misses 621 * @param repeat Whether this is a repeat lookup for the same block (used to avoid 622 * double counting cache misses when doing double-check locking) 623 * @param updateCacheMetrics Whether to update cache metrics or not 624 * @return buffer of specified cache key, or null if not in cache 625 */ 626 @Override 627 public Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat, 628 boolean updateCacheMetrics) { 629 LruCachedBlock cb = map.computeIfPresent(cacheKey, (key, val) -> { 630 // It will be referenced by RPC path, so increase here. NOTICE: Must do the retain inside 631 // this block. because if retain outside the map#computeIfPresent, the evictBlock may remove 632 // the block and release, then we're retaining a block with refCnt=0 which is disallowed. 633 // see HBASE-22422. 634 val.getBuffer().retain(); 635 return val; 636 }); 637 if (cb == null) { 638 if (!repeat && updateCacheMetrics) { 639 stats.miss(caching, cacheKey.isPrimary(), cacheKey.getBlockType()); 640 } 641 // If there is another block cache then try and read there. 642 // However if this is a retry ( second time in double checked locking ) 643 // And it's already a miss then the l2 will also be a miss. 644 if (victimHandler != null && !repeat) { 645 // The handler will increase result's refCnt for RPC, so need no extra retain. 646 Cacheable result = victimHandler.getBlock(cacheKey, caching, repeat, updateCacheMetrics); 647 // Promote this to L1. 648 if (result != null) { 649 if (caching) { 650 cacheBlock(cacheKey, result, /* inMemory = */ false); 651 } 652 } 653 return result; 654 } 655 return null; 656 } 657 if (updateCacheMetrics) { 658 stats.hit(caching, cacheKey.isPrimary(), cacheKey.getBlockType()); 659 } 660 cb.access(count.incrementAndGet()); 661 return cb.getBuffer(); 662 } 663 664 /** 665 * Whether the cache contains block with specified cacheKey 666 * @return true if contains the block 667 */ 668 @Override 669 public boolean containsBlock(BlockCacheKey cacheKey) { 670 return map.containsKey(cacheKey); 671 } 672 673 @Override 674 public boolean evictBlock(BlockCacheKey cacheKey) { 675 LruCachedBlock cb = map.get(cacheKey); 676 return cb != null && evictBlock(cb, false) > 0; 677 } 678 679 /** 680 * Evicts all blocks for a specific HFile. This is an expensive operation implemented as a 681 * linear-time search through all blocks in the cache. Ideally this should be a search in a 682 * log-access-time map. 683 * <p> 684 * This is used for evict-on-close to remove all blocks of a specific HFile. 685 * @return the number of blocks evicted 686 */ 687 @Override 688 public int evictBlocksByHfileName(String hfileName) { 689 int numEvicted = (int) map.keySet().stream().filter(key -> key.getHfileName().equals(hfileName)) 690 .filter(this::evictBlock).count(); 691 if (victimHandler != null) { 692 numEvicted += victimHandler.evictBlocksByHfileName(hfileName); 693 } 694 return numEvicted; 695 } 696 697 /** 698 * Evict the block, and it will be cached by the victim handler if exists && block may be 699 * read again later 700 * @param evictedByEvictionProcess true if the given block is evicted by EvictionThread 701 * @return the heap size of evicted block 702 */ 703 protected long evictBlock(LruCachedBlock block, boolean evictedByEvictionProcess) { 704 // The block actually removed from the map. It can differ from the passed block if the same key 705 // was replaced in the meantime (see BlockCacheUtil#shouldReplaceExistingCacheBlock), so all 706 // the accounting below must use the removed block, not the passed one. 707 LruCachedBlock previous = map.remove(block.getCacheKey()); 708 if (previous == null) { 709 return 0; 710 } 711 updateSizeMetrics(previous, true); 712 long val = elements.decrementAndGet(); 713 if (LOG.isTraceEnabled()) { 714 long size = map.size(); 715 assertCounterSanity(size, val); 716 } 717 if (previous.getBuffer().getBlockType().isData()) { 718 dataBlockElements.decrement(); 719 } 720 if (evictedByEvictionProcess) { 721 // When the eviction of the block happened because of invalidation of HFiles, no need to 722 // update the stats counter. 723 stats.evicted(previous.getCachedTime(), previous.getCacheKey().isPrimary()); 724 if (victimHandler != null) { 725 victimHandler.cacheBlock(previous.getCacheKey(), previous.getBuffer()); 726 } 727 } 728 // Decrease the block's reference count, and if refCount is 0, then it'll auto-deallocate. DO 729 // NOT move this up because if do that then the victimHandler may access the buffer with 730 // refCnt = 0 which is disallowed. 731 previous.getBuffer().release(); 732 return previous.heapSize(); 733 } 734 735 /** 736 * Multi-threaded call to run the eviction process. 737 */ 738 private void runEviction() { 739 if (evictionThread == null) { 740 evict(); 741 } else { 742 evictionThread.evict(); 743 } 744 } 745 746 boolean isEvictionInProgress() { 747 return evictionInProgress; 748 } 749 750 long getOverhead() { 751 return overhead; 752 } 753 754 /** 755 * Eviction method. Evict items in order of use, allowing delete items which haven't been used for 756 * the longest amount of time. 757 * @return how many bytes were freed 758 */ 759 long evict() { 760 761 // Ensure only one eviction at a time 762 if (!evictionLock.tryLock()) { 763 return 0; 764 } 765 766 long bytesToFree = 0L; 767 768 try { 769 evictionInProgress = true; 770 long currentSize = this.size.get(); 771 bytesToFree = currentSize - minSize(); 772 773 if (LOG.isTraceEnabled()) { 774 LOG.trace("Block cache LRU eviction started; Attempting to free " 775 + StringUtils.byteDesc(bytesToFree) + " of total=" + StringUtils.byteDesc(currentSize)); 776 } 777 778 if (bytesToFree <= 0) { 779 return 0; 780 } 781 782 // Instantiate priority buckets 783 BlockBucket bucketSingle = new BlockBucket("single", bytesToFree, blockSize, singleSize()); 784 BlockBucket bucketMulti = new BlockBucket("multi", bytesToFree, blockSize, multiSize()); 785 BlockBucket bucketMemory = new BlockBucket("memory", bytesToFree, blockSize, memorySize()); 786 787 // Scan entire map putting into appropriate buckets 788 for (LruCachedBlock cachedBlock : map.values()) { 789 switch (cachedBlock.getPriority()) { 790 case SINGLE: { 791 bucketSingle.add(cachedBlock); 792 break; 793 } 794 case MULTI: { 795 bucketMulti.add(cachedBlock); 796 break; 797 } 798 case MEMORY: { 799 bucketMemory.add(cachedBlock); 800 break; 801 } 802 } 803 } 804 805 long bytesFreed = 0; 806 if (forceInMemory || memoryFactor > 0.999f) { 807 long s = bucketSingle.totalSize(); 808 long m = bucketMulti.totalSize(); 809 if (bytesToFree > (s + m)) { 810 // this means we need to evict blocks in memory bucket to make room, 811 // so the single and multi buckets will be emptied 812 bytesFreed = bucketSingle.free(s); 813 bytesFreed += bucketMulti.free(m); 814 if (LOG.isTraceEnabled()) { 815 LOG.trace( 816 "freed " + StringUtils.byteDesc(bytesFreed) + " from single and multi buckets"); 817 } 818 bytesFreed += bucketMemory.free(bytesToFree - bytesFreed); 819 if (LOG.isTraceEnabled()) { 820 LOG.trace( 821 "freed " + StringUtils.byteDesc(bytesFreed) + " total from all three buckets "); 822 } 823 } else { 824 // this means no need to evict block in memory bucket, 825 // and we try best to make the ratio between single-bucket and 826 // multi-bucket is 1:2 827 long bytesRemain = s + m - bytesToFree; 828 if (3 * s <= bytesRemain) { 829 // single-bucket is small enough that no eviction happens for it 830 // hence all eviction goes from multi-bucket 831 bytesFreed = bucketMulti.free(bytesToFree); 832 } else if (3 * m <= 2 * bytesRemain) { 833 // multi-bucket is small enough that no eviction happens for it 834 // hence all eviction goes from single-bucket 835 bytesFreed = bucketSingle.free(bytesToFree); 836 } else { 837 // both buckets need to evict some blocks 838 bytesFreed = bucketSingle.free(s - bytesRemain / 3); 839 if (bytesFreed < bytesToFree) { 840 bytesFreed += bucketMulti.free(bytesToFree - bytesFreed); 841 } 842 } 843 } 844 } else { 845 PriorityQueue<BlockBucket> bucketQueue = new PriorityQueue<>(3); 846 847 bucketQueue.add(bucketSingle); 848 bucketQueue.add(bucketMulti); 849 bucketQueue.add(bucketMemory); 850 851 int remainingBuckets = bucketQueue.size(); 852 853 BlockBucket bucket; 854 while ((bucket = bucketQueue.poll()) != null) { 855 long overflow = bucket.overflow(); 856 if (overflow > 0) { 857 long bucketBytesToFree = 858 Math.min(overflow, (bytesToFree - bytesFreed) / remainingBuckets); 859 bytesFreed += bucket.free(bucketBytesToFree); 860 } 861 remainingBuckets--; 862 } 863 } 864 if (LOG.isTraceEnabled()) { 865 long single = bucketSingle.totalSize(); 866 long multi = bucketMulti.totalSize(); 867 long memory = bucketMemory.totalSize(); 868 LOG.trace( 869 "Block cache LRU eviction completed; " + "freed=" + StringUtils.byteDesc(bytesFreed) 870 + ", " + "total=" + StringUtils.byteDesc(this.size.get()) + ", " + "single=" 871 + StringUtils.byteDesc(single) + ", " + "multi=" + StringUtils.byteDesc(multi) + ", " 872 + "memory=" + StringUtils.byteDesc(memory)); 873 } 874 } finally { 875 stats.evict(); 876 evictionInProgress = false; 877 evictionLock.unlock(); 878 } 879 return bytesToFree; 880 } 881 882 @Override 883 public String toString() { 884 return MoreObjects.toStringHelper(this).add("blockCount", getBlockCount()) 885 .add("currentSize", StringUtils.byteDesc(getCurrentSize())) 886 .add("freeSize", StringUtils.byteDesc(getFreeSize())) 887 .add("maxSize", StringUtils.byteDesc(getMaxSize())) 888 .add("heapSize", StringUtils.byteDesc(heapSize())) 889 .add("minSize", StringUtils.byteDesc(minSize())).add("minFactor", minFactor) 890 .add("multiSize", StringUtils.byteDesc(multiSize())).add("multiFactor", multiFactor) 891 .add("singleSize", StringUtils.byteDesc(singleSize())).add("singleFactor", singleFactor) 892 .toString(); 893 } 894 895 /** 896 * Used to group blocks into priority buckets. There will be a BlockBucket for each priority 897 * (single, multi, memory). Once bucketed, the eviction algorithm takes the appropriate number of 898 * elements out of each according to configuration parameters and their relatives sizes. 899 */ 900 private class BlockBucket implements Comparable<BlockBucket> { 901 902 private final String name; 903 private final LruCachedBlockQueue queue; 904 private long totalSize = 0; 905 private final long bucketSize; 906 907 public BlockBucket(String name, long bytesToFree, long blockSize, long bucketSize) { 908 this.name = name; 909 this.bucketSize = bucketSize; 910 queue = new LruCachedBlockQueue(bytesToFree, blockSize); 911 totalSize = 0; 912 } 913 914 public void add(LruCachedBlock block) { 915 totalSize += block.heapSize(); 916 queue.add(block); 917 } 918 919 public long free(long toFree) { 920 if (LOG.isTraceEnabled()) { 921 LOG.trace("freeing {} from {}", StringUtils.byteDesc(toFree), this); 922 } 923 LruCachedBlock cb; 924 long freedBytes = 0; 925 while ((cb = queue.pollLast()) != null) { 926 freedBytes += evictBlock(cb, true); 927 if (freedBytes >= toFree) { 928 return freedBytes; 929 } 930 } 931 if (LOG.isTraceEnabled()) { 932 LOG.trace("freeing {} from {}", StringUtils.byteDesc(toFree), this); 933 } 934 return freedBytes; 935 } 936 937 public long overflow() { 938 return totalSize - bucketSize; 939 } 940 941 public long totalSize() { 942 return totalSize; 943 } 944 945 @Override 946 public int compareTo(BlockBucket that) { 947 return Long.compare(this.overflow(), that.overflow()); 948 } 949 950 @Override 951 public boolean equals(Object that) { 952 if (!(that instanceof BlockBucket)) { 953 return false; 954 } 955 return compareTo((BlockBucket) that) == 0; 956 } 957 958 @Override 959 public int hashCode() { 960 return Objects.hashCode(name, bucketSize, queue, totalSize); 961 } 962 963 @Override 964 public String toString() { 965 return MoreObjects.toStringHelper(this).add("name", name) 966 .add("totalSize", StringUtils.byteDesc(totalSize)) 967 .add("bucketSize", StringUtils.byteDesc(bucketSize)).toString(); 968 } 969 } 970 971 /** 972 * Get the maximum size of this cache. 973 * @return max size in bytes 974 */ 975 976 @Override 977 public long getMaxSize() { 978 return this.maxSize; 979 } 980 981 @Override 982 public long getCurrentSize() { 983 return this.size.get(); 984 } 985 986 @Override 987 public long getCurrentDataSize() { 988 return this.dataBlockSize.sum(); 989 } 990 991 @Override 992 public long getFreeSize() { 993 return getMaxSize() - getCurrentSize(); 994 } 995 996 @Override 997 public long size() { 998 return getMaxSize(); 999 } 1000 1001 @Override 1002 public long getBlockCount() { 1003 return this.elements.get(); 1004 } 1005 1006 @Override 1007 public long getDataBlockCount() { 1008 return this.dataBlockElements.sum(); 1009 } 1010 1011 EvictionThread getEvictionThread() { 1012 return this.evictionThread; 1013 } 1014 1015 /* 1016 * Eviction thread. Sits in waiting state until an eviction is triggered when the cache size grows 1017 * above the acceptable level.<p> Thread is triggered into action by {@link 1018 * LruAdaptiveBlockCache#runEviction()} 1019 */ 1020 static class EvictionThread extends Thread { 1021 1022 private WeakReference<LruAdaptiveBlockCache> cache; 1023 private volatile boolean go = true; 1024 // flag set after enter the run method, used for test 1025 private boolean enteringRun = false; 1026 1027 public EvictionThread(LruAdaptiveBlockCache cache) { 1028 super(Thread.currentThread().getName() + ".LruAdaptiveBlockCache.EvictionThread"); 1029 setDaemon(true); 1030 this.cache = new WeakReference<>(cache); 1031 } 1032 1033 @Override 1034 public void run() { 1035 enteringRun = true; 1036 long freedSumMb = 0; 1037 int heavyEvictionCount = 0; 1038 int freedDataOverheadPercent = 0; 1039 long startTime = EnvironmentEdgeManager.currentTime(); 1040 while (this.go) { 1041 synchronized (this) { 1042 try { 1043 this.wait(1000 * 10/* Don't wait for ever */); 1044 } catch (InterruptedException e) { 1045 LOG.warn("Interrupted eviction thread ", e); 1046 Thread.currentThread().interrupt(); 1047 } 1048 } 1049 LruAdaptiveBlockCache cache = this.cache.get(); 1050 if (cache == null) { 1051 break; 1052 } 1053 freedSumMb += cache.evict() / 1024 / 1024; 1054 /* 1055 * Sometimes we are reading more data than can fit into BlockCache and it is the cause a 1056 * high rate of evictions. This in turn leads to heavy Garbage Collector works. So a lot of 1057 * blocks put into BlockCache but never read, but spending a lot of CPU resources. Here we 1058 * will analyze how many bytes were freed and decide decide whether the time has come to 1059 * reduce amount of caching blocks. It help avoid put too many blocks into BlockCache when 1060 * evict() works very active and save CPU for other jobs. More delails: 1061 * https://issues.apache.org/jira/browse/HBASE-23887 1062 */ 1063 1064 // First of all we have to control how much time 1065 // has passed since previuos evict() was launched 1066 // This is should be almost the same time (+/- 10s) 1067 // because we get comparable volumes of freed bytes each time. 1068 // 10s because this is default period to run evict() (see above this.wait) 1069 long stopTime = EnvironmentEdgeManager.currentTime(); 1070 if ((stopTime - startTime) > 1000 * 10 - 1) { 1071 // Here we have to calc what situation we have got. 1072 // We have the limit "hbase.lru.cache.heavy.eviction.bytes.size.limit" 1073 // and can calculte overhead on it. 1074 // We will use this information to decide, 1075 // how to change percent of caching blocks. 1076 freedDataOverheadPercent = 1077 (int) (freedSumMb * 100 / cache.heavyEvictionMbSizeLimit) - 100; 1078 if (freedSumMb > cache.heavyEvictionMbSizeLimit) { 1079 // Now we are in the situation when we are above the limit 1080 // But maybe we are going to ignore it because it will end quite soon 1081 heavyEvictionCount++; 1082 if (heavyEvictionCount > cache.heavyEvictionCountLimit) { 1083 // It is going for a long time and we have to reduce of caching 1084 // blocks now. So we calculate here how many blocks we want to skip. 1085 // It depends on: 1086 // 1. Overhead - if overhead is big we could more aggressive 1087 // reducing amount of caching blocks. 1088 // 2. How fast we want to get the result. If we know that our 1089 // heavy reading for a long time, we don't want to wait and can 1090 // increase the coefficient and get good performance quite soon. 1091 // But if we don't sure we can do it slowly and it could prevent 1092 // premature exit from this mode. So, when the coefficient is 1093 // higher we can get better performance when heavy reading is stable. 1094 // But when reading is changing we can adjust to it and set 1095 // the coefficient to lower value. 1096 int change = 1097 (int) (freedDataOverheadPercent * cache.heavyEvictionOverheadCoefficient); 1098 // But practice shows that 15% of reducing is quite enough. 1099 // We are not greedy (it could lead to premature exit). 1100 change = Math.min(15, change); 1101 change = Math.max(0, change); // I think it will never happen but check for sure 1102 // So this is the key point, here we are reducing % of caching blocks 1103 cache.cacheDataBlockPercent -= change; 1104 // If we go down too deep we have to stop here, 1% any way should be. 1105 cache.cacheDataBlockPercent = Math.max(1, cache.cacheDataBlockPercent); 1106 } 1107 } else { 1108 // Well, we have got overshooting. 1109 // Mayby it is just short-term fluctuation and we can stay in this mode. 1110 // It help avoid permature exit during short-term fluctuation. 1111 // If overshooting less than 90%, we will try to increase the percent of 1112 // caching blocks and hope it is enough. 1113 if (freedSumMb >= cache.heavyEvictionMbSizeLimit * 0.1) { 1114 // Simple logic: more overshooting - more caching blocks (backpressure) 1115 int change = (int) (-freedDataOverheadPercent * 0.1 + 1); 1116 cache.cacheDataBlockPercent += change; 1117 // But it can't be more then 100%, so check it. 1118 cache.cacheDataBlockPercent = Math.min(100, cache.cacheDataBlockPercent); 1119 } else { 1120 // Looks like heavy reading is over. 1121 // Just exit form this mode. 1122 heavyEvictionCount = 0; 1123 cache.cacheDataBlockPercent = 100; 1124 } 1125 } 1126 LOG.info( 1127 "BlockCache evicted (MB): {}, overhead (%): {}, " + "heavy eviction counter: {}, " 1128 + "current caching DataBlock (%): {}", 1129 freedSumMb, freedDataOverheadPercent, heavyEvictionCount, cache.cacheDataBlockPercent); 1130 1131 freedSumMb = 0; 1132 startTime = stopTime; 1133 } 1134 } 1135 } 1136 1137 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "NN_NAKED_NOTIFY", 1138 justification = "This is what we want") 1139 public void evict() { 1140 synchronized (this) { 1141 this.notifyAll(); 1142 } 1143 } 1144 1145 synchronized void shutdown() { 1146 this.go = false; 1147 this.notifyAll(); 1148 } 1149 1150 /** 1151 * Used for the test. 1152 */ 1153 boolean isEnteringRun() { 1154 return this.enteringRun; 1155 } 1156 } 1157 1158 /* 1159 * Statistics thread. Periodically prints the cache statistics to the log. 1160 */ 1161 static class StatisticsThread extends Thread { 1162 1163 private final LruAdaptiveBlockCache lru; 1164 1165 public StatisticsThread(LruAdaptiveBlockCache lru) { 1166 super("LruAdaptiveBlockCacheStats"); 1167 setDaemon(true); 1168 this.lru = lru; 1169 } 1170 1171 @Override 1172 public void run() { 1173 lru.logStats(); 1174 } 1175 } 1176 1177 public void logStats() { 1178 // Log size 1179 long totalSize = heapSize(); 1180 long freeSize = maxSize - totalSize; 1181 LruAdaptiveBlockCache.LOG 1182 .info("totalSize=" + StringUtils.byteDesc(totalSize) + ", " + "freeSize=" 1183 + StringUtils.byteDesc(freeSize) + ", " + "max=" + StringUtils.byteDesc(this.maxSize) + ", " 1184 + "blockCount=" + getBlockCount() + ", " + "accesses=" + stats.getRequestCount() + ", " 1185 + "hits=" + stats.getHitCount() + ", " + "hitRatio=" 1186 + (stats.getHitCount() == 0 1187 ? "0" 1188 : (StringUtils.formatPercent(stats.getHitRatio(), 2) + ", ")) 1189 + ", " + "cachingAccesses=" + stats.getRequestCachingCount() + ", " + "cachingHits=" 1190 + stats.getHitCachingCount() + ", " + "cachingHitsRatio=" 1191 + (stats.getHitCachingCount() == 0 1192 ? "0," 1193 : (StringUtils.formatPercent(stats.getHitCachingRatio(), 2) + ", ")) 1194 + "evictions=" + stats.getEvictionCount() + ", " + "evicted=" + stats.getEvictedCount() 1195 + ", " + "evictedPerRun=" + stats.evictedPerEviction()); 1196 } 1197 1198 /** 1199 * Get counter statistics for this cache. 1200 * <p> 1201 * Includes: total accesses, hits, misses, evicted blocks, and runs of the eviction processes. 1202 */ 1203 @Override 1204 public CacheStats getStats() { 1205 return this.stats; 1206 } 1207 1208 public final static long CACHE_FIXED_OVERHEAD = 1209 ClassSize.estimateBase(LruAdaptiveBlockCache.class, false); 1210 1211 @Override 1212 public long heapSize() { 1213 return getCurrentSize(); 1214 } 1215 1216 private static long calculateOverhead(long maxSize, long blockSize, int concurrency) { 1217 // FindBugs ICAST_INTEGER_MULTIPLY_CAST_TO_LONG 1218 return CACHE_FIXED_OVERHEAD + ClassSize.CONCURRENT_HASHMAP 1219 + ((long) Math.ceil(maxSize * 1.2 / blockSize) * ClassSize.CONCURRENT_HASHMAP_ENTRY) 1220 + ((long) concurrency * ClassSize.CONCURRENT_HASHMAP_SEGMENT); 1221 } 1222 1223 @Override 1224 public Iterator<CachedBlock> iterator() { 1225 final Iterator<LruCachedBlock> iterator = map.values().iterator(); 1226 1227 return new Iterator<CachedBlock>() { 1228 private final long now = System.nanoTime(); 1229 1230 @Override 1231 public boolean hasNext() { 1232 return iterator.hasNext(); 1233 } 1234 1235 @Override 1236 public CachedBlock next() { 1237 final LruCachedBlock b = iterator.next(); 1238 return new CachedBlock() { 1239 @Override 1240 public String toString() { 1241 return BlockCacheUtil.toString(this, now); 1242 } 1243 1244 @Override 1245 public BlockPriority getBlockPriority() { 1246 return b.getPriority(); 1247 } 1248 1249 @Override 1250 public BlockType getBlockType() { 1251 return b.getBuffer().getBlockType(); 1252 } 1253 1254 @Override 1255 public long getOffset() { 1256 return b.getCacheKey().getOffset(); 1257 } 1258 1259 @Override 1260 public long getSize() { 1261 return b.getBuffer().heapSize(); 1262 } 1263 1264 @Override 1265 public long getCachedTime() { 1266 return b.getCachedTime(); 1267 } 1268 1269 @Override 1270 public String getFilename() { 1271 return b.getCacheKey().getHfileName(); 1272 } 1273 1274 @Override 1275 public int compareTo(CachedBlock other) { 1276 int diff = this.getFilename().compareTo(other.getFilename()); 1277 if (diff != 0) { 1278 return diff; 1279 } 1280 diff = Long.compare(this.getOffset(), other.getOffset()); 1281 if (diff != 0) { 1282 return diff; 1283 } 1284 if (other.getCachedTime() < 0 || this.getCachedTime() < 0) { 1285 throw new IllegalStateException(this.getCachedTime() + ", " + other.getCachedTime()); 1286 } 1287 return Long.compare(other.getCachedTime(), this.getCachedTime()); 1288 } 1289 1290 @Override 1291 public int hashCode() { 1292 return b.hashCode(); 1293 } 1294 1295 @Override 1296 public boolean equals(Object obj) { 1297 if (obj instanceof CachedBlock) { 1298 CachedBlock cb = (CachedBlock) obj; 1299 return compareTo(cb) == 0; 1300 } else { 1301 return false; 1302 } 1303 } 1304 }; 1305 } 1306 1307 @Override 1308 public void remove() { 1309 throw new UnsupportedOperationException(); 1310 } 1311 }; 1312 } 1313 1314 // Simple calculators of sizes given factors and maxSize 1315 1316 long acceptableSize() { 1317 return (long) Math.floor(this.maxSize * this.acceptableFactor); 1318 } 1319 1320 private long minSize() { 1321 return (long) Math.floor(this.maxSize * this.minFactor); 1322 } 1323 1324 private long singleSize() { 1325 return (long) Math.floor(this.maxSize * this.singleFactor * this.minFactor); 1326 } 1327 1328 private long multiSize() { 1329 return (long) Math.floor(this.maxSize * this.multiFactor * this.minFactor); 1330 } 1331 1332 private long memorySize() { 1333 return (long) Math.floor(this.maxSize * this.memoryFactor * this.minFactor); 1334 } 1335 1336 @Override 1337 public void shutdown() { 1338 if (victimHandler != null) { 1339 victimHandler.shutdown(); 1340 } 1341 this.scheduleThreadPool.shutdown(); 1342 for (int i = 0; i < 10; i++) { 1343 if (!this.scheduleThreadPool.isShutdown()) { 1344 try { 1345 Thread.sleep(10); 1346 } catch (InterruptedException e) { 1347 LOG.warn("Interrupted while sleeping"); 1348 Thread.currentThread().interrupt(); 1349 break; 1350 } 1351 } 1352 } 1353 1354 if (!this.scheduleThreadPool.isShutdown()) { 1355 List<Runnable> runnables = this.scheduleThreadPool.shutdownNow(); 1356 LOG.debug("Still running " + runnables); 1357 } 1358 this.evictionThread.shutdown(); 1359 } 1360 1361 /** Clears the cache. Used in tests. */ 1362 public void clearCache() { 1363 this.map.clear(); 1364 this.elements.set(0); 1365 } 1366 1367 public Map<DataBlockEncoding, Integer> getEncodingCountsForTest() { 1368 Map<DataBlockEncoding, Integer> counts = new EnumMap<>(DataBlockEncoding.class); 1369 for (LruCachedBlock block : map.values()) { 1370 DataBlockEncoding encoding = ((HFileBlock) block.getBuffer()).getDataBlockEncoding(); 1371 Integer count = counts.get(encoding); 1372 counts.put(encoding, (count == null ? 0 : count) + 1); 1373 } 1374 return counts; 1375 } 1376 1377 Map<BlockCacheKey, LruCachedBlock> getMapForTests() { 1378 return map; 1379 } 1380 1381 @Override 1382 public BlockCache[] getBlockCaches() { 1383 if (victimHandler != null) { 1384 return new BlockCache[] { this, this.victimHandler }; 1385 } 1386 return null; 1387 } 1388}