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 java.util.Optional; 021import org.apache.hadoop.conf.Configuration; 022import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; 023import org.apache.hadoop.hbase.conf.ConfigurationManager; 024import org.apache.hadoop.hbase.conf.PropagatingConfigurationObserver; 025import org.apache.hadoop.hbase.io.ByteBuffAllocator; 026import org.apache.hadoop.hbase.io.hfile.BlockType.BlockCategory; 027import org.apache.yetus.audience.InterfaceAudience; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031/** 032 * Stores all of the cache objects and configuration for a single HFile. 033 */ 034@InterfaceAudience.Private 035public class CacheConfig implements PropagatingConfigurationObserver { 036 private static final Logger LOG = LoggerFactory.getLogger(CacheConfig.class.getName()); 037 038 /** 039 * Disabled cache configuration 040 */ 041 public static final CacheConfig DISABLED = new CacheConfig(); 042 043 /** 044 * Configuration key to cache data blocks on read. Bloom blocks and index blocks are always be 045 * cached if the block cache is enabled. 046 */ 047 public static final String CACHE_DATA_ON_READ_KEY = "hbase.block.data.cacheonread"; 048 049 /** 050 * Configuration key to cache data blocks on write. There are separate switches for bloom blocks 051 * and non-root index blocks. 052 */ 053 public static final String CACHE_BLOCKS_ON_WRITE_KEY = "hbase.rs.cacheblocksonwrite"; 054 055 /** 056 * Configuration key to cache leaf and intermediate-level index blocks on write. 057 */ 058 public static final String CACHE_INDEX_BLOCKS_ON_WRITE_KEY = "hfile.block.index.cacheonwrite"; 059 060 /** 061 * Configuration key to cache compound bloom filter blocks on write. 062 */ 063 public static final String CACHE_BLOOM_BLOCKS_ON_WRITE_KEY = "hfile.block.bloom.cacheonwrite"; 064 065 /** 066 * Configuration key to cache data blocks in compressed and/or encrypted format. 067 */ 068 public static final String CACHE_DATA_BLOCKS_COMPRESSED_KEY = "hbase.block.data.cachecompressed"; 069 070 /** 071 * Configuration key to evict all blocks of a given file from the block cache when the file is 072 * closed. 073 */ 074 public static final String EVICT_BLOCKS_ON_CLOSE_KEY = "hbase.rs.evictblocksonclose"; 075 076 /** 077 * Configuration key to evict all blocks of a parent region from the block cache when the region 078 * split or merge. 079 */ 080 public static final String EVICT_BLOCKS_ON_SPLIT_KEY = "hbase.rs.evictblocksonsplit"; 081 082 /** 083 * Configuration key to prefetch all blocks of a given file into the block cache when the file is 084 * opened. 085 */ 086 public static final String PREFETCH_BLOCKS_ON_OPEN_KEY = "hbase.rs.prefetchblocksonopen"; 087 088 /** 089 * Configuration key to cache blocks when a compacted file is written 090 */ 091 public static final String CACHE_COMPACTED_BLOCKS_ON_WRITE_KEY = 092 "hbase.rs.cachecompactedblocksonwrite"; 093 094 /** 095 * Configuration key to determine total size in bytes of compacted files beyond which we do not 096 * cache blocks on compaction 097 */ 098 public static final String CACHE_COMPACTED_BLOCKS_ON_WRITE_THRESHOLD_KEY = 099 "hbase.rs.cachecompactedblocksonwrite.threshold"; 100 101 public static final String DROP_BEHIND_CACHE_COMPACTION_KEY = 102 "hbase.hfile.drop.behind.compaction"; 103 104 /** 105 * Configuration key to set interval for persisting bucket cache to disk. 106 */ 107 public static final String BUCKETCACHE_PERSIST_INTERVAL_KEY = 108 "hbase.bucketcache.persist.intervalinmillis"; 109 110 /** 111 * Configuration key to set the heap usage threshold limit once prefetch threads should be 112 * interrupted. 113 */ 114 public static final String PREFETCH_HEAP_USAGE_THRESHOLD = "hbase.rs.prefetchheapusage"; 115 116 // Defaults 117 public static final boolean DEFAULT_CACHE_DATA_ON_READ = true; 118 public static final boolean DEFAULT_CACHE_DATA_ON_WRITE = false; 119 public static final boolean DEFAULT_IN_MEMORY = false; 120 public static final boolean DEFAULT_CACHE_INDEXES_ON_WRITE = false; 121 public static final boolean DEFAULT_CACHE_BLOOMS_ON_WRITE = false; 122 public static final boolean DEFAULT_EVICT_ON_CLOSE = false; 123 public static final boolean DEFAULT_EVICT_ON_SPLIT = true; 124 public static final boolean DEFAULT_CACHE_DATA_COMPRESSED = false; 125 public static final boolean DEFAULT_PREFETCH_ON_OPEN = false; 126 public static final boolean DEFAULT_CACHE_COMPACTED_BLOCKS_ON_WRITE = false; 127 public static final boolean DROP_BEHIND_CACHE_COMPACTION_DEFAULT = true; 128 public static final long DEFAULT_CACHE_COMPACTED_BLOCKS_ON_WRITE_THRESHOLD = Long.MAX_VALUE; 129 public static final double DEFAULT_PREFETCH_HEAP_USAGE_THRESHOLD = 1d; 130 131 /** 132 * Whether blocks should be cached on read (default is on if there is a cache but this can be 133 * turned off on a per-family or per-request basis). If off we will STILL cache meta blocks; i.e. 134 * INDEX and BLOOM types. This cannot be disabled. 135 */ 136 private volatile boolean cacheDataOnRead; 137 138 /** Whether blocks should be flagged as in-memory when being cached */ 139 private boolean inMemory; 140 141 /** Whether data blocks should be cached when new files are written */ 142 private volatile boolean cacheDataOnWrite; 143 144 /** Whether index blocks should be cached when new files are written */ 145 private boolean cacheIndexesOnWrite; 146 147 /** Whether compound bloom filter blocks should be cached on write */ 148 private boolean cacheBloomsOnWrite; 149 150 /** Whether blocks of a file should be evicted when the file is closed */ 151 private volatile boolean evictOnClose; 152 153 /** 154 * Whether blocks of a parent region should be evicted from cache when the region split or merge 155 */ 156 private boolean evictOnSplit; 157 158 /** Whether data blocks should be stored in compressed and/or encrypted form in the cache */ 159 private boolean cacheDataCompressed; 160 161 /** Whether data blocks should be prefetched into the cache */ 162 private boolean prefetchOnOpen; 163 164 /** 165 * Whether data blocks should be cached when compacted file is written 166 */ 167 private boolean cacheCompactedDataOnWrite; 168 169 /** 170 * Determine threshold beyond which we do not cache blocks on compaction 171 */ 172 private long cacheCompactedDataOnWriteThreshold; 173 174 private boolean dropBehindCompaction; 175 176 // Local reference to the block cache 177 private final BlockCache blockCache; 178 179 private final ByteBuffAllocator byteBuffAllocator; 180 181 private double heapUsageThreshold; 182 183 /** 184 * Create a cache configuration using the specified configuration object and defaults for family 185 * level settings. Only use if no column family context. 186 * @param conf hbase configuration 187 */ 188 public CacheConfig(Configuration conf) { 189 this(conf, null); 190 } 191 192 public CacheConfig(Configuration conf, BlockCache blockCache) { 193 this(conf, null, blockCache, ByteBuffAllocator.HEAP); 194 } 195 196 /** 197 * Create a cache configuration using the specified configuration object and family descriptor. 198 * @param conf hbase configuration 199 * @param family column family configuration 200 */ 201 public CacheConfig(Configuration conf, ColumnFamilyDescriptor family, BlockCache blockCache, 202 ByteBuffAllocator byteBuffAllocator) { 203 if (family == null || family.isBlockCacheEnabled()) { 204 this.cacheDataOnRead = conf.getBoolean(CACHE_DATA_ON_READ_KEY, DEFAULT_CACHE_DATA_ON_READ); 205 this.inMemory = family == null ? DEFAULT_IN_MEMORY : family.isInMemory(); 206 this.cacheDataCompressed = 207 conf.getBoolean(CACHE_DATA_BLOCKS_COMPRESSED_KEY, DEFAULT_CACHE_DATA_COMPRESSED); 208 this.dropBehindCompaction = 209 conf.getBoolean(DROP_BEHIND_CACHE_COMPACTION_KEY, DROP_BEHIND_CACHE_COMPACTION_DEFAULT); 210 // For the following flags we enable them regardless of per-schema settings 211 // if they are enabled in the global configuration. 212 this.cacheDataOnWrite = 213 conf.getBoolean(CACHE_BLOCKS_ON_WRITE_KEY, DEFAULT_CACHE_DATA_ON_WRITE) 214 || (family != null && family.isCacheDataOnWrite()); 215 this.cacheIndexesOnWrite = 216 conf.getBoolean(CACHE_INDEX_BLOCKS_ON_WRITE_KEY, DEFAULT_CACHE_INDEXES_ON_WRITE) 217 || (family != null && family.isCacheIndexesOnWrite()); 218 this.cacheBloomsOnWrite = 219 conf.getBoolean(CACHE_BLOOM_BLOCKS_ON_WRITE_KEY, DEFAULT_CACHE_BLOOMS_ON_WRITE) 220 || (family != null && family.isCacheBloomsOnWrite()); 221 this.evictOnClose = conf.getBoolean(EVICT_BLOCKS_ON_CLOSE_KEY, DEFAULT_EVICT_ON_CLOSE) 222 || (family != null && family.isEvictBlocksOnClose()); 223 this.evictOnSplit = conf.getBoolean(EVICT_BLOCKS_ON_SPLIT_KEY, DEFAULT_EVICT_ON_SPLIT); 224 this.prefetchOnOpen = conf.getBoolean(PREFETCH_BLOCKS_ON_OPEN_KEY, DEFAULT_PREFETCH_ON_OPEN) 225 || (family != null && family.isPrefetchBlocksOnOpen()); 226 this.cacheCompactedDataOnWrite = conf.getBoolean(CACHE_COMPACTED_BLOCKS_ON_WRITE_KEY, 227 DEFAULT_CACHE_COMPACTED_BLOCKS_ON_WRITE); 228 this.cacheCompactedDataOnWriteThreshold = getCacheCompactedBlocksOnWriteThreshold(conf); 229 this.heapUsageThreshold = 230 conf.getDouble(PREFETCH_HEAP_USAGE_THRESHOLD, DEFAULT_PREFETCH_HEAP_USAGE_THRESHOLD); 231 } 232 this.blockCache = blockCache; 233 this.byteBuffAllocator = byteBuffAllocator; 234 } 235 236 /** 237 * Constructs a cache configuration copied from the specified configuration. 238 */ 239 public CacheConfig(CacheConfig cacheConf) { 240 this.cacheDataOnRead = cacheConf.cacheDataOnRead; 241 this.inMemory = cacheConf.inMemory; 242 this.cacheDataOnWrite = cacheConf.cacheDataOnWrite; 243 this.cacheIndexesOnWrite = cacheConf.cacheIndexesOnWrite; 244 this.cacheBloomsOnWrite = cacheConf.cacheBloomsOnWrite; 245 this.evictOnClose = cacheConf.evictOnClose; 246 this.evictOnSplit = cacheConf.evictOnSplit; 247 this.cacheDataCompressed = cacheConf.cacheDataCompressed; 248 this.prefetchOnOpen = cacheConf.prefetchOnOpen; 249 this.cacheCompactedDataOnWrite = cacheConf.cacheCompactedDataOnWrite; 250 this.cacheCompactedDataOnWriteThreshold = cacheConf.cacheCompactedDataOnWriteThreshold; 251 this.dropBehindCompaction = cacheConf.dropBehindCompaction; 252 this.blockCache = cacheConf.blockCache; 253 this.byteBuffAllocator = cacheConf.byteBuffAllocator; 254 this.heapUsageThreshold = cacheConf.heapUsageThreshold; 255 } 256 257 private CacheConfig() { 258 this.cacheDataOnRead = false; 259 this.inMemory = false; 260 this.cacheDataOnWrite = false; 261 this.cacheIndexesOnWrite = false; 262 this.cacheBloomsOnWrite = false; 263 this.evictOnClose = false; 264 this.evictOnSplit = false; 265 this.cacheDataCompressed = false; 266 this.prefetchOnOpen = false; 267 this.cacheCompactedDataOnWrite = false; 268 this.cacheCompactedDataOnWriteThreshold = DEFAULT_CACHE_COMPACTED_BLOCKS_ON_WRITE_THRESHOLD; 269 this.dropBehindCompaction = false; 270 this.blockCache = null; 271 this.byteBuffAllocator = ByteBuffAllocator.HEAP; 272 this.heapUsageThreshold = DEFAULT_PREFETCH_HEAP_USAGE_THRESHOLD; 273 } 274 275 /** 276 * Returns whether the DATA blocks of this HFile should be cached on read or not (we always cache 277 * the meta blocks, the INDEX and BLOOM blocks). 278 * @return true if blocks should be cached on read, false if not 279 */ 280 public boolean shouldCacheDataOnRead() { 281 return cacheDataOnRead; 282 } 283 284 public boolean shouldDropBehindCompaction() { 285 return dropBehindCompaction; 286 } 287 288 /** 289 * Should we cache a block of a particular category? We always cache important blocks such as 290 * index blocks, as long as the block cache is available. 291 */ 292 public boolean shouldCacheBlockOnRead(BlockCategory category) { 293 return cacheDataOnRead || category == BlockCategory.INDEX || category == BlockCategory.BLOOM 294 || (prefetchOnOpen && (category != BlockCategory.META && category != BlockCategory.UNKNOWN)); 295 } 296 297 public boolean shouldCacheBlockOnRead(BlockCategory category, HFileInfo hFileInfo, 298 Configuration conf) { 299 Optional<Boolean> cacheFileBlock = Optional.of(true); 300 // For DATA blocks only, if BucketCache is in use, we don't need to cache block again 301 if (getBlockCache().isPresent() && category.equals(BlockCategory.DATA)) { 302 Optional<Boolean> result = getBlockCache().get().shouldCacheFile(hFileInfo, conf); 303 if (result.isPresent()) { 304 cacheFileBlock = result; 305 } 306 } 307 return shouldCacheBlockOnRead(category) && cacheFileBlock.get(); 308 } 309 310 /** Returns true if blocks in this file should be flagged as in-memory */ 311 public boolean isInMemory() { 312 return this.inMemory; 313 } 314 315 /** 316 * @return true if data blocks should be written to the cache when an HFile is written, false if 317 * not 318 */ 319 public boolean shouldCacheDataOnWrite() { 320 return this.cacheDataOnWrite; 321 } 322 323 /** 324 * @param cacheDataOnWrite whether data blocks should be written to the cache when an HFile is 325 * written 326 */ 327 public void setCacheDataOnWrite(boolean cacheDataOnWrite) { 328 this.cacheDataOnWrite = cacheDataOnWrite; 329 } 330 331 /** 332 * Enable cache on write including: cacheDataOnWrite cacheIndexesOnWrite cacheBloomsOnWrite 333 */ 334 public void enableCacheOnWrite() { 335 this.cacheDataOnWrite = true; 336 this.cacheIndexesOnWrite = true; 337 this.cacheBloomsOnWrite = true; 338 } 339 340 /** 341 * @return true if index blocks should be written to the cache when an HFile is written, false if 342 * not 343 */ 344 public boolean shouldCacheIndexesOnWrite() { 345 return this.cacheIndexesOnWrite; 346 } 347 348 /** 349 * @return true if bloom blocks should be written to the cache when an HFile is written, false if 350 * not 351 */ 352 public boolean shouldCacheBloomsOnWrite() { 353 return this.cacheBloomsOnWrite; 354 } 355 356 /** 357 * @return true if blocks should be evicted from the cache when an HFile reader is closed, false 358 * if not 359 */ 360 public boolean shouldEvictOnClose() { 361 return this.evictOnClose; 362 } 363 364 /** 365 * @param evictOnClose whether blocks should be evicted from the cache when an HFile reader is 366 * closed 367 */ 368 public void setEvictOnClose(boolean evictOnClose) { 369 this.evictOnClose = evictOnClose; 370 } 371 372 /** 373 * @return true if blocks of parent region should be evicted from the cache when the region split 374 * or merge, false if not 375 */ 376 public boolean shouldEvictOnSplit() { 377 return this.evictOnSplit; 378 } 379 380 /** Returns true if data blocks should be compressed in the cache, false if not */ 381 public boolean shouldCacheDataCompressed() { 382 return this.cacheDataOnRead && this.cacheDataCompressed; 383 } 384 385 /** 386 * Returns true if this {@link BlockCategory} should be compressed in BlockCache, false otherwise 387 */ 388 public boolean shouldCacheCompressed(BlockCategory category) { 389 switch (category) { 390 case DATA: 391 return this.cacheDataOnRead && this.cacheDataCompressed; 392 default: 393 return false; 394 } 395 } 396 397 /** Returns true if blocks should be prefetched into the cache on open, false if not */ 398 public boolean shouldPrefetchOnOpen() { 399 return this.prefetchOnOpen && this.cacheDataOnRead; 400 } 401 402 /** Returns true if blocks should be cached while writing during compaction, false if not */ 403 public boolean shouldCacheCompactedBlocksOnWrite() { 404 return this.cacheCompactedDataOnWrite; 405 } 406 407 /** Returns total file size in bytes threshold for caching while writing during compaction */ 408 public long getCacheCompactedBlocksOnWriteThreshold() { 409 return this.cacheCompactedDataOnWriteThreshold; 410 } 411 412 /** 413 * Return true if we may find this type of block in block cache. 414 * <p> 415 * TODO: today {@code family.isBlockCacheEnabled()} only means {@code cacheDataOnRead}, so here we 416 * consider lots of other configurations such as {@code cacheDataOnWrite}. We should fix this in 417 * the future, {@code cacheDataOnWrite} should honor the CF level {@code isBlockCacheEnabled} 418 * configuration. 419 */ 420 public boolean shouldReadBlockFromCache(BlockType blockType) { 421 if (cacheDataOnRead) { 422 return true; 423 } 424 if (prefetchOnOpen) { 425 return true; 426 } 427 if (cacheDataOnWrite) { 428 return true; 429 } 430 if (blockType == null) { 431 return true; 432 } 433 if ( 434 blockType.getCategory() == BlockCategory.BLOOM 435 || blockType.getCategory() == BlockCategory.INDEX 436 ) { 437 return true; 438 } 439 return false; 440 } 441 442 /** 443 * Checks if the current heap usage is below the threshold configured by 444 * "hbase.rs.prefetchheapusage" (0.8 by default). 445 */ 446 public boolean isHeapUsageBelowThreshold() { 447 double total = Runtime.getRuntime().maxMemory(); 448 double available = Runtime.getRuntime().freeMemory(); 449 double usedRatio = 1d - (available / total); 450 return heapUsageThreshold > usedRatio; 451 } 452 453 /** 454 * If we make sure the block could not be cached, we will not acquire the lock otherwise we will 455 * acquire lock 456 */ 457 public boolean shouldLockOnCacheMiss(BlockType blockType) { 458 if (blockType == null) { 459 return true; 460 } 461 return shouldCacheBlockOnRead(blockType.getCategory()); 462 } 463 464 /** 465 * Returns the block cache. 466 * @return the block cache, or null if caching is completely disabled 467 */ 468 public Optional<BlockCache> getBlockCache() { 469 return Optional.ofNullable(this.blockCache); 470 } 471 472 public boolean isCombinedBlockCache() { 473 return blockCache instanceof CombinedBlockCache; 474 } 475 476 public ByteBuffAllocator getByteBuffAllocator() { 477 return this.byteBuffAllocator; 478 } 479 480 public double getHeapUsageThreshold() { 481 return heapUsageThreshold; 482 } 483 484 private long getCacheCompactedBlocksOnWriteThreshold(Configuration conf) { 485 long cacheCompactedBlocksOnWriteThreshold = 486 conf.getLong(CACHE_COMPACTED_BLOCKS_ON_WRITE_THRESHOLD_KEY, 487 DEFAULT_CACHE_COMPACTED_BLOCKS_ON_WRITE_THRESHOLD); 488 489 if (cacheCompactedBlocksOnWriteThreshold < 0) { 490 LOG.warn( 491 "cacheCompactedBlocksOnWriteThreshold value : {} is less than 0, resetting it to: {}", 492 cacheCompactedBlocksOnWriteThreshold, DEFAULT_CACHE_COMPACTED_BLOCKS_ON_WRITE_THRESHOLD); 493 cacheCompactedBlocksOnWriteThreshold = DEFAULT_CACHE_COMPACTED_BLOCKS_ON_WRITE_THRESHOLD; 494 } 495 496 return cacheCompactedBlocksOnWriteThreshold; 497 } 498 499 @Override 500 public String toString() { 501 return "cacheDataOnRead=" + shouldCacheDataOnRead() + ", cacheDataOnWrite=" 502 + shouldCacheDataOnWrite() + ", cacheIndexesOnWrite=" + shouldCacheIndexesOnWrite() 503 + ", cacheBloomsOnWrite=" + shouldCacheBloomsOnWrite() + ", cacheEvictOnClose=" 504 + shouldEvictOnClose() + ", cacheEvictOnSplit=" + shouldEvictOnSplit() 505 + ", cacheDataCompressed=" + shouldCacheDataCompressed() + ", prefetchOnOpen=" 506 + shouldPrefetchOnOpen() + ", cacheCompactedDataOnWrite=" 507 + shouldCacheCompactedBlocksOnWrite() + ", cacheCompactedDataOnWriteThreshold=" 508 + getCacheCompactedBlocksOnWriteThreshold() + ", dropBehindCompaction=" 509 + shouldDropBehindCompaction(); 510 } 511 512 @Override 513 public void onConfigurationChange(Configuration conf) { 514 cacheDataOnRead = conf.getBoolean(CACHE_DATA_ON_READ_KEY, DEFAULT_CACHE_DATA_ON_READ); 515 cacheDataOnWrite = conf.getBoolean(CACHE_BLOCKS_ON_WRITE_KEY, DEFAULT_CACHE_DATA_ON_WRITE); 516 evictOnClose = conf.getBoolean(EVICT_BLOCKS_ON_CLOSE_KEY, DEFAULT_EVICT_ON_CLOSE); 517 LOG.info( 518 "Config hbase.block.data.cacheonread is changed to {}, " 519 + "hbase.rs.cacheblocksonwrite is changed to {}, " 520 + "hbase.rs.evictblocksonclose is changed to {}", 521 cacheDataOnRead, cacheDataOnWrite, evictOnClose); 522 } 523 524 @Override 525 public void registerChildren(ConfigurationManager manager) { 526 manager.registerObserver(blockCache); 527 } 528 529 @Override 530 public void deregisterChildren(ConfigurationManager manager) { 531 manager.deregisterObserver(blockCache); 532 } 533}