|
18 | 18 | package org.apache.hadoop.hbase.regionserver;
|
19 | 19 |
|
20 | 20 | import static org.apache.hadoop.hbase.HConstants.BUCKET_CACHE_SIZE_KEY;
|
| 21 | +import static org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.DEFAULT_ERROR_TOLERATION_DURATION; |
21 | 22 | import static org.junit.Assert.assertEquals;
|
22 | 23 | import static org.junit.Assert.assertTrue;
|
23 | 24 | import static org.junit.Assert.fail;
|
|
51 | 52 | import org.apache.hadoop.hbase.io.hfile.BlockCacheKey;
|
52 | 53 | import org.apache.hadoop.hbase.io.hfile.BlockType;
|
53 | 54 | import org.apache.hadoop.hbase.io.hfile.CacheConfig;
|
| 55 | +import org.apache.hadoop.hbase.io.hfile.CacheTestUtils; |
54 | 56 | import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;
|
| 57 | +import org.apache.hadoop.hbase.io.hfile.bucket.BucketCache; |
55 | 58 | import org.apache.hadoop.hbase.testclassification.RegionServerTests;
|
56 | 59 | import org.apache.hadoop.hbase.testclassification.SmallTests;
|
57 | 60 | import org.apache.hadoop.hbase.util.Bytes;
|
@@ -245,6 +248,181 @@ public void testColdDataFiles() {
|
245 | 248 | }
|
246 | 249 | }
|
247 | 250 |
|
| 251 | + @Test |
| 252 | + public void testPickColdDataFiles() { |
| 253 | + Map<String, String> coldDataFiles = dataTieringManager.getColdFilesList(); |
| 254 | + assertEquals(1, coldDataFiles.size()); |
| 255 | + // hStoreFiles[3] is the cold file. |
| 256 | + assert (coldDataFiles.containsKey(hStoreFiles.get(3).getFileInfo().getActiveFileName())); |
| 257 | + } |
| 258 | + |
| 259 | + /* |
| 260 | + * Verify that two cold blocks(both) are evicted when bucket reaches its capacity. The hot file |
| 261 | + * remains in the cache. |
| 262 | + */ |
| 263 | + @Test |
| 264 | + public void testBlockEvictions() throws Exception { |
| 265 | + long capacitySize = 40 * 1024; |
| 266 | + int writeThreads = 3; |
| 267 | + int writerQLen = 64; |
| 268 | + int[] bucketSizes = new int[] { 8 * 1024 + 1024 }; |
| 269 | + |
| 270 | + // Setup: Create a bucket cache with lower capacity |
| 271 | + BucketCache bucketCache = new BucketCache("file:" + testDir + "/bucket.cache", capacitySize, |
| 272 | + 8192, bucketSizes, writeThreads, writerQLen, testDir + "/bucket.persistence", |
| 273 | + DEFAULT_ERROR_TOLERATION_DURATION, defaultConf); |
| 274 | + |
| 275 | + // Create three Cache keys with cold data files and a block with hot data. |
| 276 | + // hStoreFiles.get(3) is a cold data file, while hStoreFiles.get(0) is a hot file. |
| 277 | + Set<BlockCacheKey> cacheKeys = new HashSet<>(); |
| 278 | + cacheKeys.add(new BlockCacheKey(hStoreFiles.get(3).getPath(), 0, true, BlockType.DATA)); |
| 279 | + cacheKeys.add(new BlockCacheKey(hStoreFiles.get(3).getPath(), 8192, true, BlockType.DATA)); |
| 280 | + cacheKeys.add(new BlockCacheKey(hStoreFiles.get(0).getPath(), 0, true, BlockType.DATA)); |
| 281 | + |
| 282 | + // Create dummy data to be cached and fill the cache completely. |
| 283 | + CacheTestUtils.HFileBlockPair[] blocks = CacheTestUtils.generateHFileBlocks(8192, 3); |
| 284 | + |
| 285 | + int blocksIter = 0; |
| 286 | + for (BlockCacheKey key : cacheKeys) { |
| 287 | + bucketCache.cacheBlock(key, blocks[blocksIter++].getBlock()); |
| 288 | + // Ensure that the block is persisted to the file. |
| 289 | + Waiter.waitFor(defaultConf, 10000, 100, () -> (bucketCache.getBackingMap().containsKey(key))); |
| 290 | + } |
| 291 | + |
| 292 | + // Verify that the bucket cache contains 3 blocks. |
| 293 | + assertEquals(3, bucketCache.getBackingMap().keySet().size()); |
| 294 | + |
| 295 | + // Add an additional block into cache with hot data which should trigger the eviction |
| 296 | + BlockCacheKey newKey = new BlockCacheKey(hStoreFiles.get(2).getPath(), 0, true, BlockType.DATA); |
| 297 | + CacheTestUtils.HFileBlockPair[] newBlock = CacheTestUtils.generateHFileBlocks(8192, 1); |
| 298 | + |
| 299 | + bucketCache.cacheBlock(newKey, newBlock[0].getBlock()); |
| 300 | + Waiter.waitFor(defaultConf, 10000, 100, |
| 301 | + () -> (bucketCache.getBackingMap().containsKey(newKey))); |
| 302 | + |
| 303 | + // Verify that the bucket cache now contains 2 hot blocks blocks only. |
| 304 | + // Both cold blocks of 8KB will be evicted to make room for 1 block of 8KB + an additional |
| 305 | + // space. |
| 306 | + validateBlocks(bucketCache.getBackingMap().keySet(), 2, 2, 0); |
| 307 | + } |
| 308 | + |
| 309 | + /* |
| 310 | + * Verify that two cold blocks(both) are evicted when bucket reaches its capacity, but one cold |
| 311 | + * block remains in the cache since the required space is freed. |
| 312 | + */ |
| 313 | + @Test |
| 314 | + public void testBlockEvictionsAllColdBlocks() throws Exception { |
| 315 | + long capacitySize = 40 * 1024; |
| 316 | + int writeThreads = 3; |
| 317 | + int writerQLen = 64; |
| 318 | + int[] bucketSizes = new int[] { 8 * 1024 + 1024 }; |
| 319 | + |
| 320 | + // Setup: Create a bucket cache with lower capacity |
| 321 | + BucketCache bucketCache = new BucketCache("file:" + testDir + "/bucket.cache", capacitySize, |
| 322 | + 8192, bucketSizes, writeThreads, writerQLen, testDir + "/bucket.persistence", |
| 323 | + DEFAULT_ERROR_TOLERATION_DURATION, defaultConf); |
| 324 | + |
| 325 | + // Create three Cache keys with three cold data blocks. |
| 326 | + // hStoreFiles.get(3) is a cold data file. |
| 327 | + Set<BlockCacheKey> cacheKeys = new HashSet<>(); |
| 328 | + cacheKeys.add(new BlockCacheKey(hStoreFiles.get(3).getPath(), 0, true, BlockType.DATA)); |
| 329 | + cacheKeys.add(new BlockCacheKey(hStoreFiles.get(3).getPath(), 8192, true, BlockType.DATA)); |
| 330 | + cacheKeys.add(new BlockCacheKey(hStoreFiles.get(3).getPath(), 16384, true, BlockType.DATA)); |
| 331 | + |
| 332 | + // Create dummy data to be cached and fill the cache completely. |
| 333 | + CacheTestUtils.HFileBlockPair[] blocks = CacheTestUtils.generateHFileBlocks(8192, 3); |
| 334 | + |
| 335 | + int blocksIter = 0; |
| 336 | + for (BlockCacheKey key : cacheKeys) { |
| 337 | + bucketCache.cacheBlock(key, blocks[blocksIter++].getBlock()); |
| 338 | + // Ensure that the block is persisted to the file. |
| 339 | + Waiter.waitFor(defaultConf, 10000, 100, () -> (bucketCache.getBackingMap().containsKey(key))); |
| 340 | + } |
| 341 | + |
| 342 | + // Verify that the bucket cache contains 3 blocks. |
| 343 | + assertEquals(3, bucketCache.getBackingMap().keySet().size()); |
| 344 | + |
| 345 | + // Add an additional block into cache with hot data which should trigger the eviction |
| 346 | + BlockCacheKey newKey = new BlockCacheKey(hStoreFiles.get(2).getPath(), 0, true, BlockType.DATA); |
| 347 | + CacheTestUtils.HFileBlockPair[] newBlock = CacheTestUtils.generateHFileBlocks(8192, 1); |
| 348 | + |
| 349 | + bucketCache.cacheBlock(newKey, newBlock[0].getBlock()); |
| 350 | + Waiter.waitFor(defaultConf, 10000, 100, |
| 351 | + () -> (bucketCache.getBackingMap().containsKey(newKey))); |
| 352 | + |
| 353 | + // Verify that the bucket cache now contains 1 cold block and a newly added hot block. |
| 354 | + validateBlocks(bucketCache.getBackingMap().keySet(), 2, 1, 1); |
| 355 | + } |
| 356 | + |
| 357 | + /* |
| 358 | + * Verify that a hot block evicted along with a cold block when bucket reaches its capacity. |
| 359 | + */ |
| 360 | + @Test |
| 361 | + public void testBlockEvictionsHotBlocks() throws Exception { |
| 362 | + long capacitySize = 40 * 1024; |
| 363 | + int writeThreads = 3; |
| 364 | + int writerQLen = 64; |
| 365 | + int[] bucketSizes = new int[] { 8 * 1024 + 1024 }; |
| 366 | + |
| 367 | + // Setup: Create a bucket cache with lower capacity |
| 368 | + BucketCache bucketCache = new BucketCache("file:" + testDir + "/bucket.cache", capacitySize, |
| 369 | + 8192, bucketSizes, writeThreads, writerQLen, testDir + "/bucket.persistence", |
| 370 | + DEFAULT_ERROR_TOLERATION_DURATION, defaultConf); |
| 371 | + |
| 372 | + // Create three Cache keys with two hot data blocks and one cold data block |
| 373 | + // hStoreFiles.get(0) is a hot data file and hStoreFiles.get(3) is a cold data file. |
| 374 | + Set<BlockCacheKey> cacheKeys = new HashSet<>(); |
| 375 | + cacheKeys.add(new BlockCacheKey(hStoreFiles.get(0).getPath(), 0, true, BlockType.DATA)); |
| 376 | + cacheKeys.add(new BlockCacheKey(hStoreFiles.get(0).getPath(), 8192, true, BlockType.DATA)); |
| 377 | + cacheKeys.add(new BlockCacheKey(hStoreFiles.get(3).getPath(), 0, true, BlockType.DATA)); |
| 378 | + |
| 379 | + // Create dummy data to be cached and fill the cache completely. |
| 380 | + CacheTestUtils.HFileBlockPair[] blocks = CacheTestUtils.generateHFileBlocks(8192, 3); |
| 381 | + |
| 382 | + int blocksIter = 0; |
| 383 | + for (BlockCacheKey key : cacheKeys) { |
| 384 | + bucketCache.cacheBlock(key, blocks[blocksIter++].getBlock()); |
| 385 | + // Ensure that the block is persisted to the file. |
| 386 | + Waiter.waitFor(defaultConf, 10000, 100, () -> (bucketCache.getBackingMap().containsKey(key))); |
| 387 | + } |
| 388 | + |
| 389 | + // Verify that the bucket cache contains 3 blocks. |
| 390 | + assertEquals(3, bucketCache.getBackingMap().keySet().size()); |
| 391 | + |
| 392 | + // Add an additional block which should evict the only cold block with an additional hot block. |
| 393 | + BlockCacheKey newKey = new BlockCacheKey(hStoreFiles.get(2).getPath(), 0, true, BlockType.DATA); |
| 394 | + CacheTestUtils.HFileBlockPair[] newBlock = CacheTestUtils.generateHFileBlocks(8192, 1); |
| 395 | + |
| 396 | + bucketCache.cacheBlock(newKey, newBlock[0].getBlock()); |
| 397 | + Waiter.waitFor(defaultConf, 10000, 100, |
| 398 | + () -> (bucketCache.getBackingMap().containsKey(newKey))); |
| 399 | + |
| 400 | + // Verify that the bucket cache now contains 2 hot blocks. |
| 401 | + // Only one of the older hot blocks is retained and other one is the newly added hot block. |
| 402 | + validateBlocks(bucketCache.getBackingMap().keySet(), 2, 2, 0); |
| 403 | + } |
| 404 | + |
| 405 | + private void validateBlocks(Set<BlockCacheKey> keys, int expectedTotalKeys, int expectedHotBlocks, |
| 406 | + int expectedColdBlocks) { |
| 407 | + int numHotBlocks = 0, numColdBlocks = 0; |
| 408 | + |
| 409 | + assertEquals(expectedTotalKeys, keys.size()); |
| 410 | + int iter = 0; |
| 411 | + for (BlockCacheKey key : keys) { |
| 412 | + try { |
| 413 | + if (dataTieringManager.isHotData(key)) { |
| 414 | + numHotBlocks++; |
| 415 | + } else { |
| 416 | + numColdBlocks++; |
| 417 | + } |
| 418 | + } catch (Exception e) { |
| 419 | + fail("Unexpected exception!"); |
| 420 | + } |
| 421 | + } |
| 422 | + assertEquals(expectedHotBlocks, numHotBlocks); |
| 423 | + assertEquals(expectedColdBlocks, numColdBlocks); |
| 424 | + } |
| 425 | + |
248 | 426 | private void testDataTieringMethodWithPath(DataTieringMethodCallerWithPath caller, Path path,
|
249 | 427 | boolean expectedResult, DataTieringException exception) {
|
250 | 428 | try {
|
|
0 commit comments