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