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 final 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 final boolean cacheDataCompressed; 151 152 /** Whether data blocks should be prefetched into the cache */ 153 private final boolean prefetchOnOpen; 154 155 /** 156 * Whether data blocks should be cached when compacted file is written 157 */ 158 private final boolean cacheCompactedDataOnWrite; 159 160 /** 161 * Determine threshold beyond which we do not cache blocks on compaction 162 */ 163 private long cacheCompactedDataOnWriteThreshold; 164 165 private final boolean dropBehindCompaction; 166 167 // Local reference to the block cache 168 private final BlockCache blockCache; 169 170 private final ByteBuffAllocator byteBuffAllocator; 171 172 private final 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 this.cacheDataOnRead = conf.getBoolean(CACHE_DATA_ON_READ_KEY, DEFAULT_CACHE_DATA_ON_READ) 195 && (family == null ? true : family.isBlockCacheEnabled()); 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 = conf.getBoolean(CACHE_BLOCKS_ON_WRITE_KEY, DEFAULT_CACHE_DATA_ON_WRITE) 204 || (family == null ? false : family.isCacheDataOnWrite()); 205 this.cacheIndexesOnWrite = 206 conf.getBoolean(CACHE_INDEX_BLOCKS_ON_WRITE_KEY, DEFAULT_CACHE_INDEXES_ON_WRITE) 207 || (family == null ? false : family.isCacheIndexesOnWrite()); 208 this.cacheBloomsOnWrite = 209 conf.getBoolean(CACHE_BLOOM_BLOCKS_ON_WRITE_KEY, DEFAULT_CACHE_BLOOMS_ON_WRITE) 210 || (family == null ? false : family.isCacheBloomsOnWrite()); 211 this.evictOnClose = conf.getBoolean(EVICT_BLOCKS_ON_CLOSE_KEY, DEFAULT_EVICT_ON_CLOSE) 212 || (family == null ? false : family.isEvictBlocksOnClose()); 213 this.prefetchOnOpen = conf.getBoolean(PREFETCH_BLOCKS_ON_OPEN_KEY, DEFAULT_PREFETCH_ON_OPEN) 214 || (family == null ? false : family.isPrefetchBlocksOnOpen()); 215 this.cacheCompactedDataOnWrite = 216 conf.getBoolean(CACHE_COMPACTED_BLOCKS_ON_WRITE_KEY, DEFAULT_CACHE_COMPACTED_BLOCKS_ON_WRITE); 217 this.cacheCompactedDataOnWriteThreshold = getCacheCompactedBlocksOnWriteThreshold(conf); 218 this.heapUsageThreshold = 219 conf.getDouble(PREFETCH_HEAP_USAGE_THRESHOLD, DEFAULT_PREFETCH_HEAP_USAGE_THRESHOLD); 220 this.blockCache = blockCache; 221 this.byteBuffAllocator = byteBuffAllocator; 222 } 223 224 /** 225 * Constructs a cache configuration copied from the specified configuration. 226 */ 227 public CacheConfig(CacheConfig cacheConf) { 228 this.cacheDataOnRead = cacheConf.cacheDataOnRead; 229 this.inMemory = cacheConf.inMemory; 230 this.cacheDataOnWrite = cacheConf.cacheDataOnWrite; 231 this.cacheIndexesOnWrite = cacheConf.cacheIndexesOnWrite; 232 this.cacheBloomsOnWrite = cacheConf.cacheBloomsOnWrite; 233 this.evictOnClose = cacheConf.evictOnClose; 234 this.cacheDataCompressed = cacheConf.cacheDataCompressed; 235 this.prefetchOnOpen = cacheConf.prefetchOnOpen; 236 this.cacheCompactedDataOnWrite = cacheConf.cacheCompactedDataOnWrite; 237 this.cacheCompactedDataOnWriteThreshold = cacheConf.cacheCompactedDataOnWriteThreshold; 238 this.dropBehindCompaction = cacheConf.dropBehindCompaction; 239 this.blockCache = cacheConf.blockCache; 240 this.byteBuffAllocator = cacheConf.byteBuffAllocator; 241 this.heapUsageThreshold = cacheConf.heapUsageThreshold; 242 } 243 244 private CacheConfig() { 245 this.cacheDataOnRead = false; 246 this.inMemory = false; 247 this.cacheDataOnWrite = false; 248 this.cacheIndexesOnWrite = false; 249 this.cacheBloomsOnWrite = false; 250 this.evictOnClose = false; 251 this.cacheDataCompressed = false; 252 this.prefetchOnOpen = false; 253 this.cacheCompactedDataOnWrite = false; 254 this.dropBehindCompaction = false; 255 this.blockCache = null; 256 this.byteBuffAllocator = ByteBuffAllocator.HEAP; 257 this.heapUsageThreshold = DEFAULT_PREFETCH_HEAP_USAGE_THRESHOLD; 258 } 259 260 /** 261 * Returns whether the DATA blocks of this HFile should be cached on read or not (we always cache 262 * the meta blocks, the INDEX and BLOOM blocks). 263 * @return true if blocks should be cached on read, false if not 264 */ 265 public boolean shouldCacheDataOnRead() { 266 return cacheDataOnRead; 267 } 268 269 public boolean shouldDropBehindCompaction() { 270 return dropBehindCompaction; 271 } 272 273 /** 274 * Should we cache a block of a particular category? We always cache important blocks such as 275 * index blocks, as long as the block cache is available. 276 */ 277 public boolean shouldCacheBlockOnRead(BlockCategory category) { 278 return cacheDataOnRead || category == BlockCategory.INDEX || category == BlockCategory.BLOOM 279 || (prefetchOnOpen && (category != BlockCategory.META && category != BlockCategory.UNKNOWN)); 280 } 281 282 public boolean shouldCacheBlockOnRead(BlockCategory category, HFileInfo hFileInfo, 283 Configuration conf) { 284 Optional<Boolean> cacheFileBlock = Optional.of(true); 285 // For DATA blocks only, if BuckeCache is in use, we don't need to cache block again 286 if (getBlockCache().isPresent() && category.equals(BlockCategory.DATA)) { 287 Optional<Boolean> result = getBlockCache().get().shouldCacheFile(hFileInfo, conf); 288 if (result.isPresent()) { 289 cacheFileBlock = result; 290 } 291 } 292 return shouldCacheBlockOnRead(category) && cacheFileBlock.get(); 293 } 294 295 /** Returns true if blocks in this file should be flagged as in-memory */ 296 public boolean isInMemory() { 297 return this.inMemory; 298 } 299 300 /** 301 * @return true if data blocks should be written to the cache when an HFile is written, false if 302 * not 303 */ 304 public boolean shouldCacheDataOnWrite() { 305 return this.cacheDataOnWrite; 306 } 307 308 /** 309 * @param cacheDataOnWrite whether data blocks should be written to the cache when an HFile is 310 * written 311 */ 312 public void setCacheDataOnWrite(boolean cacheDataOnWrite) { 313 this.cacheDataOnWrite = cacheDataOnWrite; 314 } 315 316 /** 317 * Enable cache on write including: cacheDataOnWrite cacheIndexesOnWrite cacheBloomsOnWrite 318 */ 319 public void enableCacheOnWrite() { 320 this.cacheDataOnWrite = true; 321 this.cacheIndexesOnWrite = true; 322 this.cacheBloomsOnWrite = true; 323 } 324 325 /** 326 * @return true if index blocks should be written to the cache when an HFile is written, false if 327 * not 328 */ 329 public boolean shouldCacheIndexesOnWrite() { 330 return this.cacheIndexesOnWrite; 331 } 332 333 /** 334 * @return true if bloom blocks should be written to the cache when an HFile is written, false if 335 * not 336 */ 337 public boolean shouldCacheBloomsOnWrite() { 338 return this.cacheBloomsOnWrite; 339 } 340 341 /** 342 * @return true if blocks should be evicted from the cache when an HFile reader is closed, false 343 * if not 344 */ 345 public boolean shouldEvictOnClose() { 346 return this.evictOnClose; 347 } 348 349 /** 350 * Only used for testing. 351 * @param evictOnClose whether blocks should be evicted from the cache when an HFile reader is 352 * closed 353 */ 354 public void setEvictOnClose(boolean evictOnClose) { 355 this.evictOnClose = evictOnClose; 356 } 357 358 /** Returns true if data blocks should be compressed in the cache, false if not */ 359 public boolean shouldCacheDataCompressed() { 360 return this.cacheDataOnRead && this.cacheDataCompressed; 361 } 362 363 /** 364 * Returns true if this {@link BlockCategory} should be compressed in blockcache, false otherwise 365 */ 366 public boolean shouldCacheCompressed(BlockCategory category) { 367 switch (category) { 368 case DATA: 369 return this.cacheDataOnRead && this.cacheDataCompressed; 370 default: 371 return false; 372 } 373 } 374 375 /** Returns true if blocks should be prefetched into the cache on open, false if not */ 376 public boolean shouldPrefetchOnOpen() { 377 return this.prefetchOnOpen && this.cacheDataOnRead; 378 } 379 380 /** Returns true if blocks should be cached while writing during compaction, false if not */ 381 public boolean shouldCacheCompactedBlocksOnWrite() { 382 return this.cacheCompactedDataOnWrite; 383 } 384 385 /** Returns total file size in bytes threshold for caching while writing during compaction */ 386 public long getCacheCompactedBlocksOnWriteThreshold() { 387 return this.cacheCompactedDataOnWriteThreshold; 388 } 389 390 /** 391 * Return true if we may find this type of block in block cache. 392 * <p> 393 * TODO: today {@code family.isBlockCacheEnabled()} only means {@code cacheDataOnRead}, so here we 394 * consider lots of other configurations such as {@code cacheDataOnWrite}. We should fix this in 395 * the future, {@code cacheDataOnWrite} should honor the CF level {@code isBlockCacheEnabled} 396 * configuration. 397 */ 398 public boolean shouldReadBlockFromCache(BlockType blockType) { 399 if (cacheDataOnRead) { 400 return true; 401 } 402 if (prefetchOnOpen) { 403 return true; 404 } 405 if (cacheDataOnWrite) { 406 return true; 407 } 408 if (blockType == null) { 409 return true; 410 } 411 if ( 412 blockType.getCategory() == BlockCategory.BLOOM 413 || blockType.getCategory() == BlockCategory.INDEX 414 ) { 415 return true; 416 } 417 return false; 418 } 419 420 /** 421 * Checks if the current heap usage is below the threshold configured by 422 * "hbase.rs.prefetchheapusage" (0.8 by default). 423 */ 424 public boolean isHeapUsageBelowThreshold() { 425 double total = Runtime.getRuntime().maxMemory(); 426 double available = Runtime.getRuntime().freeMemory(); 427 double usedRatio = 1d - (available / total); 428 return heapUsageThreshold > usedRatio; 429 } 430 431 /** 432 * If we make sure the block could not be cached, we will not acquire the lock otherwise we will 433 * acquire lock 434 */ 435 public boolean shouldLockOnCacheMiss(BlockType blockType) { 436 if (blockType == null) { 437 return true; 438 } 439 return shouldCacheBlockOnRead(blockType.getCategory()); 440 } 441 442 /** 443 * Returns the block cache. 444 * @return the block cache, or null if caching is completely disabled 445 */ 446 public Optional<BlockCache> getBlockCache() { 447 return Optional.ofNullable(this.blockCache); 448 } 449 450 public boolean isCombinedBlockCache() { 451 return blockCache instanceof CombinedBlockCache; 452 } 453 454 public ByteBuffAllocator getByteBuffAllocator() { 455 return this.byteBuffAllocator; 456 } 457 458 public double getHeapUsageThreshold() { 459 return heapUsageThreshold; 460 } 461 462 private long getCacheCompactedBlocksOnWriteThreshold(Configuration conf) { 463 long cacheCompactedBlocksOnWriteThreshold = 464 conf.getLong(CACHE_COMPACTED_BLOCKS_ON_WRITE_THRESHOLD_KEY, 465 DEFAULT_CACHE_COMPACTED_BLOCKS_ON_WRITE_THRESHOLD); 466 467 if (cacheCompactedBlocksOnWriteThreshold < 0) { 468 LOG.warn( 469 "cacheCompactedBlocksOnWriteThreshold value : {} is less than 0, resetting it to: {}", 470 cacheCompactedBlocksOnWriteThreshold, DEFAULT_CACHE_COMPACTED_BLOCKS_ON_WRITE_THRESHOLD); 471 cacheCompactedBlocksOnWriteThreshold = DEFAULT_CACHE_COMPACTED_BLOCKS_ON_WRITE_THRESHOLD; 472 } 473 474 return cacheCompactedBlocksOnWriteThreshold; 475 } 476 477 @Override 478 public String toString() { 479 return "cacheDataOnRead=" + shouldCacheDataOnRead() + ", cacheDataOnWrite=" 480 + shouldCacheDataOnWrite() + ", cacheIndexesOnWrite=" + shouldCacheIndexesOnWrite() 481 + ", cacheBloomsOnWrite=" + shouldCacheBloomsOnWrite() + ", cacheEvictOnClose=" 482 + shouldEvictOnClose() + ", cacheDataCompressed=" + shouldCacheDataCompressed() 483 + ", prefetchOnOpen=" + shouldPrefetchOnOpen(); 484 } 485 486 @Override 487 public void onConfigurationChange(Configuration conf) { 488 cacheDataOnRead = conf.getBoolean(CACHE_DATA_ON_READ_KEY, DEFAULT_CACHE_DATA_ON_READ); 489 cacheDataOnWrite = conf.getBoolean(CACHE_BLOCKS_ON_WRITE_KEY, DEFAULT_CACHE_DATA_ON_WRITE); 490 evictOnClose = conf.getBoolean(EVICT_BLOCKS_ON_CLOSE_KEY, DEFAULT_EVICT_ON_CLOSE); 491 LOG.info( 492 "Config hbase.block.data.cacheonread is changed to {}, " 493 + "hbase.rs.cacheblocksonwrite is changed to {}, " 494 + "hbase.rs.evictblocksonclose is changed to {}", 495 cacheDataOnRead, cacheDataOnWrite, evictOnClose); 496 } 497 498 @Override 499 public void registerChildren(ConfigurationManager manager) { 500 manager.registerObserver(blockCache); 501 } 502 503 @Override 504 public void deregisterChildren(ConfigurationManager manager) { 505 manager.deregisterObserver(blockCache); 506 } 507}