VladRodionov commented on code in PR #8231:
URL: https://github.com/apache/hbase/pull/8231#discussion_r3236468980


##########
hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/cache/TestTopologyBackedCacheAccessService.java:
##########
@@ -0,0 +1,414 @@
+/*
+ * 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.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.List;
+import java.util.Optional;
+import org.apache.hadoop.conf.Configuration;
+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 TopologyBackedCacheAccessService}.
+ */
+@Tag(IOTests.TAG)
+@Tag(SmallTests.TAG)
+public class TestTopologyBackedCacheAccessService {
+
+  /**
+   * Verifies that lookup checks topology tiers in order and returns the first 
cached block found.
+   */
+  @Test
+  void testGetBlockChecksTiersInOrder() {
+    BlockCacheKey key = new BlockCacheKey("file", 1L);
+    Cacheable block = mock(Cacheable.class);
+    CacheTopology topology = mock(CacheTopology.class);
+    CacheTopologyView topologyView = mock(CacheTopologyView.class);
+    CachePlacementAdmissionPolicy policy = 
mock(CachePlacementAdmissionPolicy.class);
+    CacheEngine l1 = mock(CacheEngine.class);
+    CacheEngine l2 = mock(CacheEngine.class);
+    CacheRequestContext context = requestContext();
+
+    when(topology.getName()).thenReturn("tiered");
+    when(topology.getView()).thenReturn(topologyView);
+    when(topology.getTiers()).thenReturn(List.of(CacheTier.L1, CacheTier.L2));
+    when(topology.getEngine(CacheTier.L1)).thenReturn(Optional.of(l1));
+    when(topology.getEngine(CacheTier.L2)).thenReturn(Optional.of(l2));
+    when(l1.getBlock(key, true, false, true, BlockType.DATA)).thenReturn(null);
+    when(l2.getBlock(key, true, false, true, 
BlockType.DATA)).thenReturn(block);
+    when(policy.shouldPromote(key, block, CacheTier.L2, context, topologyView))
+      .thenReturn(PromotionDecision.none());
+
+    CacheAccessService service = new 
TopologyBackedCacheAccessService(topology, policy);
+
+    assertSame(block, service.getBlock(key, context));
+
+    verify(l1).getBlock(key, true, false, true, BlockType.DATA);
+    verify(l2).getBlock(key, true, false, true, BlockType.DATA);
+    verify(policy).shouldPromote(key, block, CacheTier.L2, context, 
topologyView);
+  }
+
+  /**
+   * Verifies that a hit can trigger topology-level promotion when requested 
by policy.
+   */
+  @Test
+  void testGetBlockPromotesOnPolicyDecision() {
+    BlockCacheKey key = new BlockCacheKey("file", 1L);
+    Cacheable block = mock(Cacheable.class);
+    CacheTopology topology = mock(CacheTopology.class);
+    CacheTopologyView topologyView = mock(CacheTopologyView.class);
+    CachePlacementAdmissionPolicy policy = 
mock(CachePlacementAdmissionPolicy.class);
+    CacheEngine l1 = mock(CacheEngine.class);
+    CacheEngine l2 = mock(CacheEngine.class);
+    CacheRequestContext context = requestContext();
+
+    when(topology.getView()).thenReturn(topologyView);
+    when(topology.getTiers()).thenReturn(List.of(CacheTier.L1, CacheTier.L2));
+    when(topology.getEngine(CacheTier.L1)).thenReturn(Optional.of(l1));
+    when(topology.getEngine(CacheTier.L2)).thenReturn(Optional.of(l2));
+    when(l1.getBlock(key, true, false, true, BlockType.DATA)).thenReturn(null);
+    when(l2.getBlock(key, true, false, true, 
BlockType.DATA)).thenReturn(block);
+    when(policy.shouldPromote(key, block, CacheTier.L2, context, topologyView))
+      .thenReturn(PromotionDecision.promoteTo(CacheTier.L1, false));
+    when(topology.promote(key, block, l2, l1)).thenReturn(true);
+
+    CacheAccessService service = new 
TopologyBackedCacheAccessService(topology, policy);
+
+    assertSame(block, service.getBlock(key, context));
+
+    verify(policy).shouldPromote(key, block, CacheTier.L2, context, 
topologyView);
+    verify(topology).promote(key, block, l2, l1);
+  }
+
+  /**
+   * Verifies that cache insertion is skipped when admission policy rejects 
the block.
+   */
+  @Test
+  void testCacheBlockSkipsInsertionWhenRejected() {
+    BlockCacheKey key = new BlockCacheKey("file", 1L);
+    Cacheable block = mock(Cacheable.class);
+    CacheTopology topology = mock(CacheTopology.class);
+    CacheTopologyView topologyView = mock(CacheTopologyView.class);
+    CachePlacementAdmissionPolicy policy = 
mock(CachePlacementAdmissionPolicy.class);
+    CacheEngine engine = mock(CacheEngine.class);
+    CacheWriteContext context = writeContext();
+
+    when(topology.getView()).thenReturn(topologyView);
+    when(topology.getEngine(CacheTier.SINGLE)).thenReturn(Optional.of(engine));
+    when(policy.shouldAdmit(key, block, context, AdmissionPriority.NORMAL, 
topologyView))
+      .thenReturn(AdmissionDecision.reject("rejected"));
+
+    CacheAccessService service = new 
TopologyBackedCacheAccessService(topology, policy);
+
+    service.cacheBlock(key, block, context);
+
+    verify(policy).shouldAdmit(key, block, context, AdmissionPriority.NORMAL, 
topologyView);
+    verify(policy, never()).selectTier(key, block, context, topologyView);
+    verify(engine, never()).cacheBlock(key, block, true, true);
+  }
+
+  /**
+   * Verifies that admitted blocks are inserted into policy-selected tiers.
+   */
+  @Test
+  void testCacheBlockInsertsIntoSelectedTier() {
+    BlockCacheKey key = new BlockCacheKey("file", 1L);
+    Cacheable block = mock(Cacheable.class);
+    CacheTopology topology = mock(CacheTopology.class);
+    CacheTopologyView topologyView = mock(CacheTopologyView.class);
+    CachePlacementAdmissionPolicy policy = 
mock(CachePlacementAdmissionPolicy.class);
+    CacheEngine engine = mock(CacheEngine.class);
+    CacheWriteContext context = writeContext();
+
+    when(topology.getView()).thenReturn(topologyView);
+    when(topology.getEngine(CacheTier.SINGLE)).thenReturn(Optional.of(engine));
+    when(policy.shouldAdmit(key, block, context, AdmissionPriority.NORMAL, 
topologyView))
+      .thenReturn(AdmissionDecision.admit());
+    when(policy.selectRepresentation(key, block, context, topologyView))
+      .thenReturn(RepresentationDecision.CURRENT_HBASE_DEFAULT);

Review Comment:
   Good question. No, I do not expect multiple TopologyBackedCacheAccessService 
implementations per cache engine.
   
   The intended model is:
   
   ```text 
   CacheAccessService
     -> CacheTopology
         -> CacheEngine(s)
     -> CachePlacementAdmissionPolicy
   ``` 
   
   So TopologyBackedCacheAccessService would be the common topology-based 
service. Different implementations plug in below it as different CacheEngine 
implementations, and different behavior plugs in as different 
CachePlacementAdmissionPolicy implementations.
   
   For example, future wiring could choose either:
   
   ```text
    BlockCacheBackedCacheAccessService -> existing BlockCache 
   ```
   for compatibility mode, or:
   
   ```text
   TopologyBackedCacheAccessService
     -> TieredExclusiveTopology
     -> LruCacheEngine + BucketCacheEngine
     -> Default policy
   ```
   and later swap only the engine/policy, for example:
   
   ```text
    LruCacheEngine + CarrotCacheEngine Carrot-aware policy
   ``` 
   
   This PR does not wire runtime configuration yet, but future configuration 
would likely happen in BlockCacheFactory, using settings such as service type, 
topology type, L1/L2 engines, and policy.
   
   Example:
   ```properties
   hbase.blockcache.access.service = topology
   hbase.blockcache.topology = tiered-exclusive
   hbase.blockcache.l1.engine = lru
   hbase.blockcache.l2.engine = bucket
   hbase.blockcache.policy = default
   ```



-- 
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]

Reply via email to