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