VladRodionov commented on code in PR #8231: URL: https://github.com/apache/hbase/pull/8231#discussion_r3236326980
########## hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/cache/BlockCacheBackedCacheAccessService.java: ########## @@ -0,0 +1,298 @@ +/* + * 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 java.util.Objects; +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.yetus.audience.InterfaceAudience; + +/** + * {@link CacheAccessService} implementation backed by an existing {@link BlockCache} instance. + * <p> + * This adapter is the compatibility bridge for the first migration step. It allows new callers to + * depend on {@link CacheAccessService} while the runtime implementation still uses the current + * {@link BlockCache} hierarchy, including LruBlockCache, BucketCache, CombinedBlockCache, and other + * existing implementations. + * </p> + * <p> + * The adapter should not introduce new policy, placement, admission, representation, promotion, or + * topology behavior. Its purpose is to translate the new context-based service API into the current + * {@code BlockCache} API with no intentional behavior change. + * </p> + * <p> + * A future topology-backed service can replace this adapter after call sites have migrated to + * {@link CacheAccessService}. That future implementation may use {@link CacheTopology}, + * {@link CachePlacementAdmissionPolicy}, and {@link CacheEngine}; this adapter deliberately does + * not. + * </p> + */ [email protected] +public class BlockCacheBackedCacheAccessService implements CacheAccessService { + + private final BlockCache blockCache; + + /** + * Creates a cache access service backed by the supplied legacy block cache. + * @param blockCache block cache to wrap + */ + public BlockCacheBackedCacheAccessService(BlockCache blockCache) { + this.blockCache = Objects.requireNonNull(blockCache, "blockCache must not be null"); + } + + /** + * Returns the wrapped {@link BlockCache} instance. + * <p> + * This accessor is intended for tests and transitional wiring only. New read/write path code + * should use {@link CacheAccessService} methods instead of unwrapping the legacy cache. + * </p> + * @return wrapped block cache + */ + public BlockCache getBlockCache() { + return blockCache; + } + + /** + * Returns a human-readable service name. + * @return service name + */ + @Override + public String getName() { + return blockCache.getClass().getSimpleName(); + } + + /** + * Fetches a block by delegating to the wrapped {@link BlockCache}. + * @param cacheKey block to fetch + * @param context cache request context + * @return cached block, or {@code null} if not present + */ + @Override + public Cacheable getBlock(BlockCacheKey cacheKey, CacheRequestContext context) { + Objects.requireNonNull(cacheKey, "cacheKey must not be null"); + Objects.requireNonNull(context, "context must not be null"); + + Optional<BlockType> blockType = context.getBlockType(); + if (blockType.isPresent()) { + return blockCache.getBlock(cacheKey, context.isCaching(), context.isRepeat(), + context.isUpdateCacheMetrics(), blockType.get()); + } + return blockCache.getBlock(cacheKey, context.isCaching(), context.isRepeat(), + context.isUpdateCacheMetrics()); + } + + /** + * Caches a block by delegating to the wrapped {@link BlockCache}. + * @param cacheKey block cache key + * @param block block contents + * @param context cache write context + */ + @Override + public void cacheBlock(BlockCacheKey cacheKey, Cacheable block, CacheWriteContext context) { + Objects.requireNonNull(cacheKey, "cacheKey must not be null"); + Objects.requireNonNull(block, "block must not be null"); + Objects.requireNonNull(context, "context must not be null"); + + blockCache.cacheBlock(cacheKey, block, context.isInMemory(), context.isWaitWhenCache()); + } + + /** + * Evicts a single block by delegating to the wrapped {@link BlockCache}. + * @param cacheKey block to remove + * @return {@code true} if the block existed and was removed, {@code false} otherwise + */ + @Override + public boolean evictBlock(BlockCacheKey cacheKey) { + Objects.requireNonNull(cacheKey, "cacheKey must not be null"); + return blockCache.evictBlock(cacheKey); + } + + /** + * Evicts all cached blocks for the given HFile by delegating to the wrapped {@link BlockCache}. + * @param hfileName HFile name + * @return number of blocks removed + */ + @Override + public int evictBlocksByHfileName(String hfileName) { + Objects.requireNonNull(hfileName, "hfileName must not be null"); + return blockCache.evictBlocksByHfileName(hfileName); + } + + /** + * Evicts cached blocks for an HFile range if the wrapped cache supports it. + * @param hfileName HFile name + * @param initOffset inclusive start offset + * @param endOffset inclusive end offset + * @return number of blocks removed + */ + @Override + public int evictBlocksRangeByHfileName(String hfileName, long initOffset, long endOffset) { + Objects.requireNonNull(hfileName, "hfileName must not be null"); + return blockCache.evictBlocksRangeByHfileName(hfileName, initOffset, endOffset); + } + + /** + * Evicts cached blocks for a region if the wrapped cache supports it. + * @param regionName region name + * @return number of blocks removed + */ + @Override + public int evictBlocksByRegionName(String regionName) { + return 0; // BlockCache does not support region-based eviction, so we return 0 to indicate no + // blocks removed Review Comment: Done -- 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]
