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 // Retain the column family settings when reloading the global configuration. 132 private final boolean familyBlockCacheEnabled; 133 private final boolean familyCacheDataOnWrite; 134 private final boolean familyEvictOnClose; 135 136 /** 137 * Whether blocks should be cached on read (default is on if there is a cache but this can be 138 * turned off on a per-family or per-request basis). If off we will STILL cache meta blocks; i.e. 139 * INDEX and BLOOM types. This cannot be disabled. 140 */ 141 private volatile boolean cacheDataOnRead; 142 143 /** Whether blocks should be flagged as in-memory when being cached */ 144 private boolean inMemory; 145 146 /** Whether data blocks should be cached when new files are written */ 147 private volatile boolean cacheDataOnWrite; 148 149 /** Whether index blocks should be cached when new files are written */ 150 private boolean cacheIndexesOnWrite; 151 152 /** Whether compound bloom filter blocks should be cached on write */ 153 private boolean cacheBloomsOnWrite; 154 155 /** Whether blocks of a file should be evicted when the file is closed */ 156 private volatile boolean evictOnClose; 157 158 /** 159 * Whether blocks of a parent region should be evicted from cache when the region split or merge 160 */ 161 private boolean evictOnSplit; 162 163 /** Whether data blocks should be stored in compressed and/or encrypted form in the cache */ 164 private boolean cacheDataCompressed; 165 166 /** Whether data blocks should be prefetched into the cache */ 167 private boolean prefetchOnOpen; 168 169 /** 170 * Whether data blocks should be cached when compacted file is written 171 */ 172 private boolean cacheCompactedDataOnWrite; 173 174 /** 175 * Determine threshold beyond which we do not cache blocks on compaction 176 */ 177 private long cacheCompactedDataOnWriteThreshold; 178 179 private boolean dropBehindCompaction; 180 181 // Local reference to the block cache 182 private final BlockCache blockCache; 183 184 private final ByteBuffAllocator byteBuffAllocator; 185 186 private double heapUsageThreshold; 187 188 /** 189 * Create a cache configuration using the specified configuration object and defaults for family 190 * level settings. Only use if no column family context. 191 * @param conf hbase configuration 192 */ 193 public CacheConfig(Configuration conf) { 194 this(conf, null); 195 } 196 197 public CacheConfig(Configuration conf, BlockCache blockCache) { 198 this(conf, null, blockCache, ByteBuffAllocator.HEAP); 199 } 200 201 /** 202 * Create a cache configuration using the specified configuration object and family descriptor. 203 * @param conf hbase configuration 204 * @param family column family configuration 205 */ 206 public CacheConfig(Configuration conf, ColumnFamilyDescriptor family, BlockCache blockCache, 207 ByteBuffAllocator byteBuffAllocator) { 208 this.familyBlockCacheEnabled = family == null || family.isBlockCacheEnabled(); 209 this.familyCacheDataOnWrite = family != null && family.isCacheDataOnWrite(); 210 this.familyEvictOnClose = family != null && family.isEvictBlocksOnClose(); 211 updateCacheSettings(conf); 212 if (familyBlockCacheEnabled) { 213 this.inMemory = family == null ? DEFAULT_IN_MEMORY : family.isInMemory(); 214 this.cacheDataCompressed = 215 conf.getBoolean(CACHE_DATA_BLOCKS_COMPRESSED_KEY, DEFAULT_CACHE_DATA_COMPRESSED); 216 this.dropBehindCompaction = 217 conf.getBoolean(DROP_BEHIND_CACHE_COMPACTION_KEY, DROP_BEHIND_CACHE_COMPACTION_DEFAULT); 218 // For the following flags we enable them regardless of per-schema settings 219 // if they are enabled in the global configuration. 220 this.cacheIndexesOnWrite = 221 conf.getBoolean(CACHE_INDEX_BLOCKS_ON_WRITE_KEY, DEFAULT_CACHE_INDEXES_ON_WRITE) 222 || (family != null && family.isCacheIndexesOnWrite()); 223 this.cacheBloomsOnWrite = 224 conf.getBoolean(CACHE_BLOOM_BLOCKS_ON_WRITE_KEY, DEFAULT_CACHE_BLOOMS_ON_WRITE) 225 || (family != null && family.isCacheBloomsOnWrite()); 226 this.evictOnSplit = conf.getBoolean(EVICT_BLOCKS_ON_SPLIT_KEY, DEFAULT_EVICT_ON_SPLIT); 227 this.prefetchOnOpen = conf.getBoolean(PREFETCH_BLOCKS_ON_OPEN_KEY, DEFAULT_PREFETCH_ON_OPEN) 228 || (family != null && family.isPrefetchBlocksOnOpen()); 229 this.cacheCompactedDataOnWrite = conf.getBoolean(CACHE_COMPACTED_BLOCKS_ON_WRITE_KEY, 230 DEFAULT_CACHE_COMPACTED_BLOCKS_ON_WRITE); 231 this.cacheCompactedDataOnWriteThreshold = getCacheCompactedBlocksOnWriteThreshold(conf); 232 this.heapUsageThreshold = 233 conf.getDouble(PREFETCH_HEAP_USAGE_THRESHOLD, DEFAULT_PREFETCH_HEAP_USAGE_THRESHOLD); 234 } 235 this.blockCache = blockCache; 236 this.byteBuffAllocator = byteBuffAllocator; 237 } 238 239 /** 240 * Constructs a cache configuration copied from the specified configuration. 241 */ 242 public CacheConfig(CacheConfig cacheConf) { 243 this.familyBlockCacheEnabled = cacheConf.familyBlockCacheEnabled; 244 this.familyCacheDataOnWrite = cacheConf.familyCacheDataOnWrite; 245 this.familyEvictOnClose = cacheConf.familyEvictOnClose; 246 this.cacheDataOnRead = cacheConf.cacheDataOnRead; 247 this.inMemory = cacheConf.inMemory; 248 this.cacheDataOnWrite = cacheConf.cacheDataOnWrite; 249 this.cacheIndexesOnWrite = cacheConf.cacheIndexesOnWrite; 250 this.cacheBloomsOnWrite = cacheConf.cacheBloomsOnWrite; 251 this.evictOnClose = cacheConf.evictOnClose; 252 this.evictOnSplit = cacheConf.evictOnSplit; 253 this.cacheDataCompressed = cacheConf.cacheDataCompressed; 254 this.prefetchOnOpen = cacheConf.prefetchOnOpen; 255 this.cacheCompactedDataOnWrite = cacheConf.cacheCompactedDataOnWrite; 256 this.cacheCompactedDataOnWriteThreshold = cacheConf.cacheCompactedDataOnWriteThreshold; 257 this.dropBehindCompaction = cacheConf.dropBehindCompaction; 258 this.blockCache = cacheConf.blockCache; 259 this.byteBuffAllocator = cacheConf.byteBuffAllocator; 260 this.heapUsageThreshold = cacheConf.heapUsageThreshold; 261 } 262 263 private CacheConfig() { 264 this.familyBlockCacheEnabled = false; 265 this.familyCacheDataOnWrite = false; 266 this.familyEvictOnClose = false; 267 this.cacheDataOnRead = false; 268 this.inMemory = false; 269 this.cacheDataOnWrite = false; 270 this.cacheIndexesOnWrite = false; 271 this.cacheBloomsOnWrite = false; 272 this.evictOnClose = false; 273 this.evictOnSplit = false; 274 this.cacheDataCompressed = false; 275 this.prefetchOnOpen = false; 276 this.cacheCompactedDataOnWrite = false; 277 this.cacheCompactedDataOnWriteThreshold = DEFAULT_CACHE_COMPACTED_BLOCKS_ON_WRITE_THRESHOLD; 278 this.dropBehindCompaction = false; 279 this.blockCache = null; 280 this.byteBuffAllocator = ByteBuffAllocator.HEAP; 281 this.heapUsageThreshold = DEFAULT_PREFETCH_HEAP_USAGE_THRESHOLD; 282 } 283 284 /** 285 * Returns whether the DATA blocks of this HFile should be cached on read or not (we always cache 286 * the meta blocks, the INDEX and BLOOM blocks). 287 * @return true if blocks should be cached on read, false if not 288 */ 289 public boolean shouldCacheDataOnRead() { 290 return cacheDataOnRead; 291 } 292 293 public boolean shouldDropBehindCompaction() { 294 return dropBehindCompaction; 295 } 296 297 /** 298 * Should we cache a block of a particular category? We always cache important blocks such as 299 * index blocks, as long as the block cache is available. 300 */ 301 public boolean shouldCacheBlockOnRead(BlockCategory category) { 302 return cacheDataOnRead || category == BlockCategory.INDEX || category == BlockCategory.BLOOM 303 || (prefetchOnOpen && (category != BlockCategory.META && category != BlockCategory.UNKNOWN)); 304 } 305 306 public boolean shouldCacheBlockOnRead(BlockCategory category, HFileInfo hFileInfo, 307 Configuration conf) { 308 Optional<Boolean> cacheFileBlock = Optional.of(true); 309 // For DATA blocks only, if BucketCache is in use, we don't need to cache block again 310 if (getBlockCache().isPresent() && category.equals(BlockCategory.DATA)) { 311 Optional<Boolean> result = getBlockCache().get().shouldCacheFile(hFileInfo, conf); 312 if (result.isPresent()) { 313 cacheFileBlock = result; 314 } 315 } 316 return shouldCacheBlockOnRead(category) && cacheFileBlock.get(); 317 } 318 319 /** Returns true if blocks in this file should be flagged as in-memory */ 320 public boolean isInMemory() { 321 return this.inMemory; 322 } 323 324 /** 325 * @return true if data blocks should be written to the cache when an HFile is written, false if 326 * not 327 */ 328 public boolean shouldCacheDataOnWrite() { 329 return this.cacheDataOnWrite; 330 } 331 332 /** 333 * @param cacheDataOnWrite whether data blocks should be written to the cache when an HFile is 334 * written 335 */ 336 public void setCacheDataOnWrite(boolean cacheDataOnWrite) { 337 this.cacheDataOnWrite = cacheDataOnWrite; 338 } 339 340 /** 341 * Enable cache on write including: cacheDataOnWrite cacheIndexesOnWrite cacheBloomsOnWrite 342 */ 343 public void enableCacheOnWrite() { 344 this.cacheDataOnWrite = true; 345 this.cacheIndexesOnWrite = true; 346 this.cacheBloomsOnWrite = true; 347 } 348 349 /** 350 * @return true if index blocks should be written to the cache when an HFile is written, false if 351 * not 352 */ 353 public boolean shouldCacheIndexesOnWrite() { 354 return this.cacheIndexesOnWrite; 355 } 356 357 /** 358 * @return true if bloom blocks should be written to the cache when an HFile is written, false if 359 * not 360 */ 361 public boolean shouldCacheBloomsOnWrite() { 362 return this.cacheBloomsOnWrite; 363 } 364 365 /** 366 * @return true if blocks should be evicted from the cache when an HFile reader is closed, false 367 * if not 368 */ 369 public boolean shouldEvictOnClose() { 370 return this.evictOnClose; 371 } 372 373 /** 374 * @param evictOnClose whether blocks should be evicted from the cache when an HFile reader is 375 * closed 376 */ 377 public void setEvictOnClose(boolean evictOnClose) { 378 this.evictOnClose = evictOnClose; 379 } 380 381 /** 382 * @return true if blocks of parent region should be evicted from the cache when the region split 383 * or merge, false if not 384 */ 385 public boolean shouldEvictOnSplit() { 386 return this.evictOnSplit; 387 } 388 389 /** Returns true if data blocks should be compressed in the cache, false if not */ 390 public boolean shouldCacheDataCompressed() { 391 return this.cacheDataOnRead && this.cacheDataCompressed; 392 } 393 394 /** 395 * Returns true if this {@link BlockCategory} should be compressed in BlockCache, false otherwise 396 */ 397 public boolean shouldCacheCompressed(BlockCategory category) { 398 switch (category) { 399 case DATA: 400 return this.cacheDataOnRead && this.cacheDataCompressed; 401 default: 402 return false; 403 } 404 } 405 406 /** Returns true if blocks should be prefetched into the cache on open, false if not */ 407 public boolean shouldPrefetchOnOpen() { 408 return this.prefetchOnOpen && this.cacheDataOnRead; 409 } 410 411 /** Returns true if blocks should be cached while writing during compaction, false if not */ 412 public boolean shouldCacheCompactedBlocksOnWrite() { 413 return this.cacheCompactedDataOnWrite; 414 } 415 416 /** Returns total file size in bytes threshold for caching while writing during compaction */ 417 public long getCacheCompactedBlocksOnWriteThreshold() { 418 return this.cacheCompactedDataOnWriteThreshold; 419 } 420 421 /** 422 * Return true if we may find this type of block in block cache. 423 * <p> 424 * TODO: today {@code family.isBlockCacheEnabled()} only means {@code cacheDataOnRead}, so here we 425 * consider lots of other configurations such as {@code cacheDataOnWrite}. We should fix this in 426 * the future, {@code cacheDataOnWrite} should honor the CF level {@code isBlockCacheEnabled} 427 * configuration. 428 */ 429 public boolean shouldReadBlockFromCache(BlockType blockType) { 430 if (cacheDataOnRead) { 431 return true; 432 } 433 if (prefetchOnOpen) { 434 return true; 435 } 436 if (cacheDataOnWrite) { 437 return true; 438 } 439 if (blockType == null) { 440 return true; 441 } 442 if ( 443 blockType.getCategory() == BlockCategory.BLOOM 444 || blockType.getCategory() == BlockCategory.INDEX 445 ) { 446 return true; 447 } 448 return false; 449 } 450 451 /** 452 * Checks if the current heap usage is below the threshold configured by 453 * "hbase.rs.prefetchheapusage" (0.8 by default). 454 */ 455 public boolean isHeapUsageBelowThreshold() { 456 double total = Runtime.getRuntime().maxMemory(); 457 double available = Runtime.getRuntime().freeMemory(); 458 double usedRatio = 1d - (available / total); 459 return heapUsageThreshold > usedRatio; 460 } 461 462 /** 463 * If we make sure the block could not be cached, we will not acquire the lock otherwise we will 464 * acquire lock 465 */ 466 public boolean shouldLockOnCacheMiss(BlockType blockType) { 467 if (blockType == null) { 468 return true; 469 } 470 return shouldCacheBlockOnRead(blockType.getCategory()); 471 } 472 473 /** 474 * Returns the block cache. 475 * @return the block cache, or null if caching is completely disabled 476 */ 477 public Optional<BlockCache> getBlockCache() { 478 return Optional.ofNullable(this.blockCache); 479 } 480 481 public boolean isCombinedBlockCache() { 482 return blockCache instanceof CombinedBlockCache; 483 } 484 485 public ByteBuffAllocator getByteBuffAllocator() { 486 return this.byteBuffAllocator; 487 } 488 489 public double getHeapUsageThreshold() { 490 return heapUsageThreshold; 491 } 492 493 private long getCacheCompactedBlocksOnWriteThreshold(Configuration conf) { 494 long cacheCompactedBlocksOnWriteThreshold = 495 conf.getLong(CACHE_COMPACTED_BLOCKS_ON_WRITE_THRESHOLD_KEY, 496 DEFAULT_CACHE_COMPACTED_BLOCKS_ON_WRITE_THRESHOLD); 497 498 if (cacheCompactedBlocksOnWriteThreshold < 0) { 499 LOG.warn( 500 "cacheCompactedBlocksOnWriteThreshold value : {} is less than 0, resetting it to: {}", 501 cacheCompactedBlocksOnWriteThreshold, DEFAULT_CACHE_COMPACTED_BLOCKS_ON_WRITE_THRESHOLD); 502 cacheCompactedBlocksOnWriteThreshold = DEFAULT_CACHE_COMPACTED_BLOCKS_ON_WRITE_THRESHOLD; 503 } 504 505 return cacheCompactedBlocksOnWriteThreshold; 506 } 507 508 @Override 509 public String toString() { 510 return "cacheDataOnRead=" + shouldCacheDataOnRead() + ", cacheDataOnWrite=" 511 + shouldCacheDataOnWrite() + ", cacheIndexesOnWrite=" + shouldCacheIndexesOnWrite() 512 + ", cacheBloomsOnWrite=" + shouldCacheBloomsOnWrite() + ", cacheEvictOnClose=" 513 + shouldEvictOnClose() + ", cacheEvictOnSplit=" + shouldEvictOnSplit() 514 + ", cacheDataCompressed=" + shouldCacheDataCompressed() + ", prefetchOnOpen=" 515 + shouldPrefetchOnOpen() + ", cacheCompactedDataOnWrite=" 516 + shouldCacheCompactedBlocksOnWrite() + ", cacheCompactedDataOnWriteThreshold=" 517 + getCacheCompactedBlocksOnWriteThreshold() + ", dropBehindCompaction=" 518 + shouldDropBehindCompaction(); 519 } 520 521 @Override 522 public void onConfigurationChange(Configuration conf) { 523 updateCacheSettings(conf); 524 LOG.info("Effective cache settings after configuration change: cacheDataOnRead={}, " 525 + "cacheDataOnWrite={}, evictOnClose={}", cacheDataOnRead, cacheDataOnWrite, evictOnClose); 526 } 527 528 private void updateCacheSettings(Configuration conf) { 529 // A column family with block cache disabled ignores these global settings. Otherwise, the 530 // global cache on write and evict on close settings are combined with the family settings. 531 cacheDataOnRead = familyBlockCacheEnabled 532 && conf.getBoolean(CACHE_DATA_ON_READ_KEY, DEFAULT_CACHE_DATA_ON_READ); 533 cacheDataOnWrite = familyBlockCacheEnabled 534 && (conf.getBoolean(CACHE_BLOCKS_ON_WRITE_KEY, DEFAULT_CACHE_DATA_ON_WRITE) 535 || familyCacheDataOnWrite); 536 evictOnClose = familyBlockCacheEnabled 537 && (conf.getBoolean(EVICT_BLOCKS_ON_CLOSE_KEY, DEFAULT_EVICT_ON_CLOSE) || familyEvictOnClose); 538 } 539 540 @Override 541 public void registerChildren(ConfigurationManager manager) { 542 manager.registerObserver(blockCache); 543 } 544 545 @Override 546 public void deregisterChildren(ConfigurationManager manager) { 547 manager.deregisterObserver(blockCache); 548 } 549}