VladRodionov commented on code in PR #8231: URL: https://github.com/apache/hbase/pull/8231#discussion_r3238331804
########## hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/cache/TestBlockCacheBackedCacheAccessService.java: ########## @@ -0,0 +1,210 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.io.hfile.cache; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Optional; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.io.hfile.BlockCache; +import org.apache.hadoop.hbase.io.hfile.BlockCacheKey; +import org.apache.hadoop.hbase.io.hfile.BlockType; +import org.apache.hadoop.hbase.io.hfile.CacheStats; +import org.apache.hadoop.hbase.io.hfile.Cacheable; +import org.apache.hadoop.hbase.io.hfile.HFileBlock; +import org.apache.hadoop.hbase.testclassification.IOTests; +import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +/** + * Tests for {@link BlockCacheBackedCacheAccessService} and related service helpers. + */ +@Tag(IOTests.TAG) +@Tag(SmallTests.TAG) +public class TestBlockCacheBackedCacheAccessService { + + /** + * Verifies that context-based lookup delegates to the block-type aware legacy lookup method. + */ + @Test + void testGetBlockWithBlockTypeDelegatesToBlockCache() { + BlockCache blockCache = mock(BlockCache.class); + CacheAccessService service = new BlockCacheBackedCacheAccessService(blockCache); + BlockCacheKey key = new BlockCacheKey("file", 1L); + Cacheable block = mock(Cacheable.class); + + when(blockCache.getBlock(key, true, true, false, BlockType.DATA)).thenReturn(block); + + CacheRequestContext context = CacheRequestContext.newBuilder().setCaching(true).setRepeat(true) + .setUpdateCacheMetrics(false).setBlockType(BlockType.DATA).build(); + + assertSame(block, service.getBlock(key, context)); + verify(blockCache).getBlock(key, true, true, false, BlockType.DATA); + } + + /** + * Verifies that context-based lookup delegates to the legacy lookup method without block type. + */ + @Test + void testGetBlockWithoutBlockTypeDelegatesToBlockCache() { + BlockCache blockCache = mock(BlockCache.class); + CacheAccessService service = new BlockCacheBackedCacheAccessService(blockCache); + BlockCacheKey key = new BlockCacheKey("file", 1L); + Cacheable block = mock(Cacheable.class); + + when(blockCache.getBlock(key, true, false, true)).thenReturn(block); + + CacheRequestContext context = CacheRequestContext.newBuilder().setCaching(true).setRepeat(false) + .setUpdateCacheMetrics(true).build(); + + assertSame(block, service.getBlock(key, context)); + verify(blockCache).getBlock(key, true, false, true); + } + + /** + * Verifies that context-based insertion delegates in-memory and wait flags correctly. + */ + @Test + void testCacheBlockDelegatesToBlockCache() { + BlockCache blockCache = mock(BlockCache.class); + CacheAccessService service = new BlockCacheBackedCacheAccessService(blockCache); + BlockCacheKey key = new BlockCacheKey("file", 1L); + Cacheable block = mock(Cacheable.class); + + CacheWriteContext context = CacheWriteContext.newBuilder().setInMemory(true) + .setWaitWhenCache(true).setSource(CacheWriteSource.READ_MISS).build(); + + service.cacheBlock(key, block, context); + + verify(blockCache).cacheBlock(key, block, true, true); + } + + /** + * Verifies that invalidation methods delegate to the wrapped block cache. + */ + @Test + void testEvictionDelegatesToBlockCache() { + BlockCache blockCache = mock(BlockCache.class); + CacheAccessService service = new BlockCacheBackedCacheAccessService(blockCache); + BlockCacheKey key = new BlockCacheKey("file", 1L); Review Comment: Good point. I’ll replace the repeated string/offset literals with class-level constants so the test is less typo-prone. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
