World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#11961] refactor(core): Simplify EntityCache and remove relation data from CaffeineEntityCache (#12006)
Qi Yu39029b3c9807
diff --git a/core/src/jcstress/java/org/apache/gravitino/cache/TestCacheIndexCoherence.java b/core/src/jcstress/java/org/apache/gravitino/cache/TestCacheIndexCoherence.javaindex d74f32b22c..edbaee90b2 100644--- a/core/src/jcstress/java/org/apache/gravitino/cache/TestCacheIndexCoherence.java+++ b/core/src/jcstress/java/org/apache/gravitino/cache/TestCacheIndexCoherence.java@@ -29,10 +29,8 @@ import java.util.Objects;import org.apache.gravitino.Entity;import org.apache.gravitino.NameIdentifier;import org.apache.gravitino.Namespace;-import org.apache.gravitino.SupportsRelationOperations;import org.apache.gravitino.meta.AuditInfo;import org.apache.gravitino.meta.SchemaEntity;-import org.apache.gravitino.utils.NameIdentifierUtil;import org.openjdk.jcstress.annotations.Actor;import org.openjdk.jcstress.annotations.Arbiter;import org.openjdk.jcstress.annotations.Description;@@ -78,13 +76,13 @@ public class TestCacheIndexCoherence {+ "atomicity or thread-safety guarantees.")@Statepublic static class InsertSameKeyCoherenceTest {- private final RadixTree<EntityCacheRelationKey> indexTree =+ private final RadixTree<EntityCacheKey> indexTree =new ConcurrentRadixTree<>(new DefaultCharArrayNodeFactory());private final NameIdentifier ident = NameIdentifier.of("metalake1", "catalog1", "schema1");private final Entity entity =getTestSchemaEntity(123L, "schema1", Namespace.of("metalake1", "catalog1"), "ident1");- private final EntityCacheRelationKey key = EntityCacheRelationKey.of(ident, entity.type());+ private final EntityCacheKey key = EntityCacheKey.of(ident, entity.type());private final String keyStr = key.toString();@Actor@@ -99,50 +97,7 @@ public class TestCacheIndexCoherence {@Arbiterpublic void arbiter(I_Result r) {- EntityCacheRelationKey valueForExactKey = indexTree.getValueForExactKey(keyStr);- r.r1 =- (valueForExactKey != null- && Objects.equals(valueForExactKey, key)- && indexTree.size() == 1)- ? 1- : 0;- }- }-- @JCStressTest- @Outcome.Outcomes({- @Outcome(id = "1", expect = Expect.ACCEPTABLE, desc = "Key inserted and visible"),- @Outcome(id = "0", expect = Expect.FORBIDDEN, desc = "Key not found")- })- @Description(- "Tests that ConcurrentRadixTree handles concurrent put() operations for the same key with a "- + "relation type. Both threads insert the same key-value pair where the key includes a "- + "relation type (ROLE_USER_REL). The test expects the tree to maintain a single entry. A "- + "forbidden result indicates incorrect key handling or data loss under concurrency.")- @State- public static class InsertSameKeyWithRelationTypeCoherenceTest {- private final RadixTree<EntityCacheRelationKey> indexTree =- new ConcurrentRadixTree<>(new DefaultCharArrayNodeFactory());-- private final NameIdentifier ident = NameIdentifierUtil.ofRole("metalake", "role");- private final EntityCacheRelationKey key =- EntityCacheRelationKey.of(- ident, Entity.EntityType.ROLE, SupportsRelationOperations.Type.ROLE_USER_REL);- private final String keyStr = key.toString();-- @Actor- public void actor1() {- indexTree.put(keyStr, key);- }-- @Actor- public void actor2() {- indexTree.put(keyStr, key);- }-- @Arbiter- public void arbiter(I_Result r) {- EntityCacheRelationKey valueForExactKey = indexTree.getValueForExactKey(keyStr);+ EntityCacheKey valueForExactKey = indexTree.getValueForExactKey(keyStr);r.r1 =(valueForExactKey != null&& Objects.equals(valueForExactKey, key)@@ -164,7 +119,7 @@ public class TestCacheIndexCoherence {+ "indicates data loss or broken concurrency guarantees.")@Statepublic static class InsertMultipleKeyCoherenceTest {- private final RadixTree<EntityCacheRelationKey> indexTree =+ private final RadixTree<EntityCacheKey> indexTree =new ConcurrentRadixTree<>(new DefaultCharArrayNodeFactory());private final NameIdentifier ident1 = NameIdentifier.of("metalake1", "catalog1", "schema1");@@ -173,60 +128,8 @@ public class TestCacheIndexCoherence {private final NameIdentifier ident2 = NameIdentifier.of("metalake1", "catalog1", "schema2");private final Entity entity2 =getTestSchemaEntity(456L, "schema2", Namespace.of("metalake1", "catalog1"), "ident2");- private final EntityCacheRelationKey key1 = EntityCacheRelationKey.of(ident1, entity1.type());- private final EntityCacheRelationKey key2 = EntityCacheRelationKey.of(ident2, entity2.type());- private final String key1Str = key1.toString();- private final String key2Str = key2.toString();-- @Actor- public void actor1() {- indexTree.put(key1Str, key1);- }-- @Actor- public void actor2() {- indexTree.put(key2Str, key2);- }-- @Arbiter- public void arbiter(I_Result r) {- EntityCacheRelationKey valueForExactKey1 = indexTree.getValueForExactKey(key1Str);- EntityCacheRelationKey valueForExactKey2 = indexTree.getValueForExactKey(key2Str);- r.r1 =- (valueForExactKey1 != null- && valueForExactKey2 != null- && Objects.equals(valueForExactKey1, key1)- && Objects.equals(valueForExactKey2, key2)- && indexTree.size() == 2)- ? 1- : 0;- }- }-- @JCStressTest- @Outcome.Outcomes({- @Outcome(id = "1", expect = Expect.ACCEPTABLE, desc = "multiple Key inserted and visible"),- @Outcome(id = "0", expect = Expect.FORBIDDEN, desc = "some Key not found")- })- @Description(- "Tests ConcurrentRadixTree under concurrent insertions of different keys with relation types. "- + "Thread 1 inserts a key with ROLE_USER_REL, and Thread 2 inserts a key with ROLE_GROUP_REL. "- + "Both keys share the same NameIdentifier but differ by relation type. The tree should retain "- + "both entries. A forbidden result indicates relation type is not correctly distinguished.")- @State- public static class InsertMultipleKeyWithRelationTypeCoherenceTest {- private final RadixTree<EntityCacheRelationKey> indexTree =- new ConcurrentRadixTree<>(new DefaultCharArrayNodeFactory());-- private final NameIdentifier ident1 = NameIdentifierUtil.ofRole("metalake", "role1");- private final NameIdentifier ident2 = NameIdentifierUtil.ofRole("metalake", "role1");-- private final EntityCacheRelationKey key1 =- EntityCacheRelationKey.of(- ident1, Entity.EntityType.ROLE, SupportsRelationOperations.Type.ROLE_USER_REL);- private final EntityCacheRelationKey key2 =- EntityCacheRelationKey.of(- ident2, Entity.EntityType.ROLE, SupportsRelationOperations.Type.ROLE_GROUP_REL);+ private final EntityCacheKey key1 = EntityCacheKey.of(ident1, entity1.type());+ private final EntityCacheKey key2 = EntityCacheKey.of(ident2, entity2.type());private final String key1Str = key1.toString();private final String key2Str = key2.toString();@@ -242,8 +145,8 @@ public class TestCacheIndexCoherence {@Arbiterpublic void arbiter(I_Result r) {- EntityCacheRelationKey valueForExactKey1 = indexTree.getValueForExactKey(key1Str);- EntityCacheRelationKey valueForExactKey2 = indexTree.getValueForExactKey(key2Str);+ EntityCacheKey valueForExactKey1 = indexTree.getValueForExactKey(key1Str);+ EntityCacheKey valueForExactKey2 = indexTree.getValueForExactKey(key2Str);r.r1 =(valueForExactKey1 != null&& valueForExactKey2 != null@@ -292,49 +195,6 @@ public class TestCacheIndexCoherence {}}- @JCStressTest- @Outcome.Outcomes({- @Outcome(id = "1", expect = Expect.ACCEPTABLE, desc = "Key does not exist after remove."),- @Outcome(- id = "0",- expect = Expect.ACCEPTABLE_INTERESTING,- desc = "Key still exists due to put/remove race.")- })- @Description(- "Tests the race condition between put() and remove() on a key with relation type in "- + "ConcurrentRadixTree. Thread 1 inserts a key that includes a relation type "- + "(ROLE_USER_REL), while Thread 2 concurrently removes the same key. If remove "- + "happens after put, the key is deleted, which is acceptable. If put happens after remove, "- + "the key remains, which is interesting but not forbidden.")- @State- public static class PutRemoveSameKeyWithRelationTypeCoherenceTest {-- private final RadixTree<EntityCacheRelationKey> indexTree =- new ConcurrentRadixTree<>(new DefaultCharArrayNodeFactory());-- private final NameIdentifier ident = NameIdentifierUtil.ofRole("metalake", "role");- private final EntityCacheRelationKey key =- EntityCacheRelationKey.of(- ident, Entity.EntityType.ROLE, SupportsRelationOperations.Type.ROLE_USER_REL);- private final String keyStr = key.toString();-- @Actor- public void actorPut() {- indexTree.put(keyStr, key);- }-- @Actor- public void actorRemove() {- indexTree.remove(keyStr);- }-- @Arbiter- public void arbiter(I_Result r) {- EntityCacheRelationKey value = indexTree.getValueForExactKey(keyStr);- r.r1 = (value == null) ? 1 : 0;- }- }-@JCStressTest@Outcome.Outcomes({@Outcome(@@ -388,68 +248,6 @@ public class TestCacheIndexCoherence {}}- @JCStressTest- @Outcome.Outcomes({- @Outcome(- id = "1, 1",- expect = Expect.ACCEPTABLE,- desc = "Both values are visible — correct and expected."),- @Outcome(- id = "1, 0",- expect = Expect.FORBIDDEN,- desc = "Only v1 is visible — inconsistent cache state."),- @Outcome(- id = "0, 1",- expect = Expect.FORBIDDEN,- desc = "Only v2 is visible — inconsistent cache state."),- @Outcome(- id = "0, 0",- expect = Expect.FORBIDDEN,- desc = "No value visible — inconsistent cache state.")- })- @Description(- "Tests race conditions between concurrent put() operations on keys with different relation "- + "types and a prefix-based scan. Thread 1 inserts a ROLE_USER_REL key, Thread 2 inserts "- + "a ROLE_GROUP_REL key. The arbiter uses getValuesForKeysStartingWith() to verify both "- + "entries are visible. Visibility of only one is a race condition and forbidden. Missing "- + "both values indicates an inconsistent cache state and is also forbidden.")- @State- public static class PutAndPrefixScanWithRelationTypeCoherenceTest {-- private final RadixTree<EntityCacheRelationKey> indexTree =- new ConcurrentRadixTree<>(new DefaultCharArrayNodeFactory());-- private final NameIdentifier ident1 = NameIdentifierUtil.ofRole("metalake", "role1");- private final NameIdentifier ident2 = NameIdentifierUtil.ofRole("metalake", "role2");-- private final EntityCacheRelationKey key1 =- EntityCacheRelationKey.of(- ident1, Entity.EntityType.ROLE, SupportsRelationOperations.Type.ROLE_USER_REL);- private final EntityCacheRelationKey key2 =- EntityCacheRelationKey.of(- ident2, Entity.EntityType.ROLE, SupportsRelationOperations.Type.ROLE_GROUP_REL);- private final String key1Str = key1.toString();- private final String key2Str = key2.toString();-- @Actor- public void actorPut1() {- indexTree.put(key1Str, key1);- }-- @Actor- public void actorPut2() {- indexTree.put(key2Str, key2);- }-- @Arbiter- public void arbiter(II_Result r) {- ImmutableList<EntityCacheRelationKey> values =- ImmutableList.copyOf(indexTree.getValuesForKeysStartingWith("metalake"));- r.r1 = values.contains(key1) ? 1 : 0;- r.r2 = values.contains(key2) ? 1 : 0;- }- }-@JCStressTest@Outcome.Outcomes({@Outcome(id = "1", expect = Expect.ACCEPTABLE, desc = "Get sees the value"),@@ -479,38 +277,4 @@ public class TestCacheIndexCoherence {r.r1 = indexTree.getValueForExactKey(key) != null ? 1 : 0;}}-- @JCStressTest- @Outcome.Outcomes({- @Outcome(id = "1", expect = Expect.ACCEPTABLE, desc = "Get sees the value"),- @Outcome(- id = "0",- expect = Expect.ACCEPTABLE_INTERESTING,- desc = "Get sees nothing due to timing")- })- @Description(- "Tests race condition between put() and getValueForExactKey() for relation-typed cache keys. "- + "Thread 1 inserts a ROLE_USER_REL key into the radix tree. Thread 2 concurrently attempts to "- + "retrieve the same key. If the get observes the result of the put, it returns a non-null value. "- + "Missing value is interesting but not forbidden, depending on execution timing.")- @State- public static class PutAndGetWithRelationTypeCoherenceTest {- private final RadixTree<EntityCacheRelationKey> indexTree =- new ConcurrentRadixTree<>(new DefaultCharArrayNodeFactory());- private final NameIdentifier ident = NameIdentifierUtil.ofRole("metalake", "role");- private final EntityCacheRelationKey key =- EntityCacheRelationKey.of(- ident, Entity.EntityType.ROLE, SupportsRelationOperations.Type.ROLE_USER_REL);- private final String keyStr = key.toString();-- @Actor- public void actorPut() {- indexTree.put(keyStr, key);- }-- @Actor- public void actorGet(I_Result r) {- r.r1 = indexTree.getValueForExactKey(keyStr) != null ? 1 : 0;- }- }}diff --git a/core/src/jcstress/java/org/apache/gravitino/cache/TestCaffeineEntityCacheCoherence.java b/core/src/jcstress/java/org/apache/gravitino/cache/TestCaffeineEntityCacheCoherence.javaindex df1978adc4..783e0315e2 100644--- a/core/src/jcstress/java/org/apache/gravitino/cache/TestCaffeineEntityCacheCoherence.java+++ b/core/src/jcstress/java/org/apache/gravitino/cache/TestCaffeineEntityCacheCoherence.java@@ -25,21 +25,15 @@ import static org.mockito.Mockito.when;import com.google.common.collect.ImmutableList;import com.google.common.collect.ImmutableMap;import java.time.Instant;-import java.util.List;import java.util.Optional;import org.apache.gravitino.Config;import org.apache.gravitino.Entity;import org.apache.gravitino.Namespace;-import org.apache.gravitino.SupportsRelationOperations;import org.apache.gravitino.meta.AuditInfo;import org.apache.gravitino.meta.ColumnEntity;-import org.apache.gravitino.meta.GroupEntity;-import org.apache.gravitino.meta.RoleEntity;import org.apache.gravitino.meta.SchemaEntity;import org.apache.gravitino.meta.TableEntity;-import org.apache.gravitino.meta.UserEntity;import org.apache.gravitino.rel.types.Types;-import org.apache.gravitino.utils.NamespaceUtil;import org.openjdk.jcstress.annotations.Actor;import org.openjdk.jcstress.annotations.Arbiter;import org.openjdk.jcstress.annotations.Description;@@ -55,11 +49,6 @@ public class TestCaffeineEntityCacheCoherence {getTestSchemaEntity(1L, "schema1", Namespace.of("metalake1", "catalog1"), "test_schema1");private static final TableEntity tableEntity =getTestTableEntity(3L, "table1", Namespace.of("metalake1", "catalog1", "schema1"));- private static final GroupEntity groupEntity =- getTestGroupEntity(4L, "group1", "metalake1", ImmutableList.of("role1"));- private static final UserEntity userEntity =- getTestUserEntity(5L, "user1", "metalake1", ImmutableList.of(6L));- private static final RoleEntity roleEntity = getTestRoleEntity(6L, "role1", "metalake1");private static SchemaEntity getTestSchemaEntity(long id, String name, Namespace namespace, String comment) {@@ -83,38 +72,6 @@ public class TestCaffeineEntityCacheCoherence {.build();}- private static RoleEntity getTestRoleEntity(long id, String name, String metalake) {- return RoleEntity.builder()- .withId(id)- .withName(name)- .withNamespace(NamespaceUtil.ofRole(metalake))- .withAuditInfo(getTestAuditInfo())- .withSecurableObjects(ImmutableList.of())- .build();- }-- private static GroupEntity getTestGroupEntity(- long id, String name, String metalake, List<String> roles) {- return GroupEntity.builder()- .withId(id)- .withName(name)- .withNamespace(NamespaceUtil.ofGroup(metalake))- .withAuditInfo(getTestAuditInfo())- .withRoleNames(roles)- .build();- }-- private static UserEntity getTestUserEntity(- long id, String name, String metalake, List<Long> roles) {- return UserEntity.builder()- .withId(id)- .withName(name)- .withNamespace(NamespaceUtil.ofUser(metalake))- .withAuditInfo(getTestAuditInfo())- .withRoleIds(roles)- .build();- }-private static AuditInfo getTestAuditInfo() {return AuditInfo.builder().withCreator("admin")@@ -338,69 +295,6 @@ public class TestCaffeineEntityCacheCoherence {}}- @JCStressTest- @Outcome.Outcomes({- @Outcome(- id = "2",- expect = Expect.ACCEPTABLE,- desc = "Both put() calls succeeded; both entries are visible in the cache."),- @Outcome(- id = "1",- expect = Expect.FORBIDDEN,- desc = "Only one entry is visible; potential visibility or atomicity issue."),- @Outcome(- id = "0",- expect = Expect.FORBIDDEN,- desc =- "Neither entry is visible; indicates a serious failure in write propagation or cache logic.")- })- @Description("Concurrent put() with different ROLE relation types; expect both visible (2).")- @State- public static class ConcurrentPutDifferentKeysWithRelationTest {- private final EntityCache cache;-- public ConcurrentPutDifferentKeysWithRelationTest() {- this.cache = new CaffeineEntityCache(new Config() {});- }-- @Actor- public void actor1() {- cache.put(- roleEntity.nameIdentifier(),- Entity.EntityType.ROLE,- SupportsRelationOperations.Type.ROLE_GROUP_REL,- ImmutableList.of(groupEntity));- }-- @Actor- public void actor2() {- cache.put(- roleEntity.nameIdentifier(),- Entity.EntityType.ROLE,- SupportsRelationOperations.Type.ROLE_USER_REL,- ImmutableList.of(userEntity));- }-- @Arbiter- public void arbiter(I_Result r) {- int count = 0;- if (cache.contains(- roleEntity.nameIdentifier(),- Entity.EntityType.ROLE,- SupportsRelationOperations.Type.ROLE_USER_REL)) {- count++;- }- if (cache.contains(- roleEntity.nameIdentifier(),- Entity.EntityType.ROLE,- SupportsRelationOperations.Type.ROLE_GROUP_REL)) {- count++;- }-- r.r1 = count;- }- }-@JCStressTest@Outcome.Outcomes({@Outcome(@@ -438,58 +332,6 @@ public class TestCaffeineEntityCacheCoherence {}}- @JCStressTest- @Outcome.Outcomes({- @Outcome(- id = "1",- expect = Expect.ACCEPTABLE,- desc = "Entry is visible; concurrent put() calls succeeded."),- @Outcome(- id = "0",- expect = Expect.FORBIDDEN,- desc = "Entry is missing; indicates visibility or atomicity issue.")- })- @Description(- "Tests concurrent put() on the same key with relation type. "- + "Entry must remain visible after concurrent writes; missing indicates a bug.")- @State- public static class ConcurrentPutSameKeyWithRelationTest {- private final EntityCache cache;-- public ConcurrentPutSameKeyWithRelationTest() {- this.cache = new CaffeineEntityCache(new Config() {});- }-- @Actor- public void actor1() {- cache.put(- roleEntity.nameIdentifier(),- Entity.EntityType.ROLE,- SupportsRelationOperations.Type.ROLE_USER_REL,- ImmutableList.of(userEntity));- }-- @Actor- public void actor2() {- cache.put(- roleEntity.nameIdentifier(),- Entity.EntityType.ROLE,- SupportsRelationOperations.Type.ROLE_USER_REL,- ImmutableList.of(userEntity));- }-- @Arbiter- public void arbiter(I_Result r) {- r.r1 =- cache.contains(- roleEntity.nameIdentifier(),- Entity.EntityType.ROLE,- SupportsRelationOperations.Type.ROLE_USER_REL)- ? 1- : 0;- }- }-@JCStressTest@Outcome.Outcomes({@Outcome(diff --git a/core/src/jmh/java/org/apache/gravitino/cache/AbstractEntityBenchmark.java b/core/src/jmh/java/org/apache/gravitino/cache/AbstractEntityBenchmark.javaindex a90ce8208c..f97cd6ebbb 100644--- a/core/src/jmh/java/org/apache/gravitino/cache/AbstractEntityBenchmark.java+++ b/core/src/jmh/java/org/apache/gravitino/cache/AbstractEntityBenchmark.java@@ -20,15 +20,10 @@package org.apache.gravitino.cache;import java.util.List;-import java.util.Map;import java.util.Random;import java.util.concurrent.TimeUnit;import org.apache.gravitino.Config;-import org.apache.gravitino.Entity;-import org.apache.gravitino.HasIdentifier;-import org.apache.gravitino.SupportsRelationOperations;import org.apache.gravitino.meta.ModelEntity;-import org.apache.gravitino.meta.RoleEntity;import org.openjdk.jmh.annotations.BenchmarkMode;import org.openjdk.jmh.annotations.Level;import org.openjdk.jmh.annotations.Mode;@@ -42,21 +37,19 @@ import org.openjdk.jmh.annotations.State;* AbstractEntityBenchmark is a JMH-based benchmark base class designed to evaluate the performance* of the {@link EntityCache} implementation in Gravitino.*- * <p>This class sets up a controlled environment where a configurable number of entities and their- * associated relations are preloaded into the cache before each iteration. It supports benchmarking- * cache operations such as put, get, and relation-based access under varying data volumes.+ * <p>This class sets up a controlled environment where a configurable number of entities are+ * preloaded into the cache before each iteration. It supports benchmarking cache operations such as+ * put, get, contains, and invalidate under varying data volumes.** <p>Features:** <ul>* <li>Supports benchmarking with different entity counts (e.g., 10, 100, 1000)* <li>Measures both throughput and average execution time using JMH modes- * <li>Preloads both simple entities and relation entities (e.g., user-role mappings)* </ul>** <p>Subclasses should implement benchmark methods to evaluate specific cache operations.*- * @param <E> the type of entity under test, must implement {@link Entity} and {@link HasIdentifier}* @see org.apache.gravitino.cache.CaffeineEntityCache* @see org.openjdk.jmh.annotations.Benchmark* @see org.apache.gravitino.cache.BenchmarkHelper@@ -64,13 +57,12 @@ import org.openjdk.jmh.annotations.State;@BenchmarkMode({Mode.Throughput, Mode.AverageTime})@OutputTimeUnit(TimeUnit.SECONDS)@State(Scope.Thread)-public abstract class AbstractEntityBenchmark<E extends Entity & HasIdentifier> {+public abstract class AbstractEntityBenchmark {@Param({"10", "100", "1000"})public int totalCnt;protected EntityCache cache;protected List<ModelEntity> entities;- protected Map<RoleEntity, List<E>> entitiesWithRelations;protected Random random = new Random();@Setup(Level.Iteration)@@ -78,15 +70,7 @@ public abstract class AbstractEntityBenchmark<E extends Entity & HasIdentifier>Config config = new Config() {};this.cache = new CaffeineEntityCache(config);this.entities = BenchmarkHelper.getEntities(totalCnt);- this.entitiesWithRelations = BenchmarkHelper.getRelationEntities(totalCnt);entities.forEach(cache::put);- entitiesWithRelations.forEach(- (roleEntity, userList) ->- cache.put(- roleEntity.nameIdentifier(),- roleEntity.type(),- SupportsRelationOperations.Type.ROLE_USER_REL,- userList));}}diff --git a/core/src/jmh/java/org/apache/gravitino/cache/BenchmarkHelper.java b/core/src/jmh/java/org/apache/gravitino/cache/BenchmarkHelper.javaindex 7d80a36c47..93c65b56d3 100644--- a/core/src/jmh/java/org/apache/gravitino/cache/BenchmarkHelper.java+++ b/core/src/jmh/java/org/apache/gravitino/cache/BenchmarkHelper.java@@ -20,8 +20,6 @@package org.apache.gravitino.cache;import com.google.common.base.Preconditions;-import com.google.common.collect.ImmutableList;-import com.google.common.collect.Maps;import java.util.ArrayList;import java.util.List;import java.util.Map;@@ -30,37 +28,15 @@ import org.apache.gravitino.Entity;import org.apache.gravitino.HasIdentifier;import org.apache.gravitino.NameIdentifier;import org.apache.gravitino.meta.ModelEntity;-import org.apache.gravitino.meta.RoleEntity;import org.apache.gravitino.utils.TestUtil;public class BenchmarkHelper {- public static final int RELATION_CNT = 5;private static final Random random = new Random();private BenchmarkHelper() {// Utility class, no constructor needed}- /**- * Generates a map of entities with their relations.- *- * @param entityCnt the count of entities to generate.- * @return a map of entities with their relations.- * @param <E> the type of the entity.- */- public static <E extends Entity & HasIdentifier> Map<RoleEntity, List<E>> getRelationEntities(- int entityCnt) {- Map<RoleEntity, List<E>> entitiesWithRelations = Maps.newHashMap();- for (int i = 0; i < entityCnt; i++) {- RoleEntity testRoleEntity = TestUtil.getTestRoleEntity();- int userCnt = random.nextInt(RELATION_CNT);- List<E> userList = BaseEntityCache.convertEntities(getUserList(testRoleEntity, userCnt));- entitiesWithRelations.put(testRoleEntity, userList);- }-- return entitiesWithRelations;- }-/*** Generates a list of entities with the specified count.*@@ -117,15 +93,4 @@ public class BenchmarkHelper {Preconditions.checkArgument(entity instanceof HasIdentifier, "Unsupported EntityType: " + entity.type());}-- private static List<Entity> getUserList(RoleEntity roleEntity, int userCnt) {- List<Entity> userList = new ArrayList<>(userCnt);- List<Long> roleIds = ImmutableList.of(roleEntity.id());-- for (int i = 0; i < userCnt; i++) {- userList.add(TestUtil.getTestUserEntity(roleIds));- }-- return userList;- }}diff --git a/core/src/jmh/java/org/apache/gravitino/cache/ClearEntityCacheBenchmark.java b/core/src/jmh/java/org/apache/gravitino/cache/ClearEntityCacheBenchmark.javaindex 4ccaea4079..b73e4739ff 100644--- a/core/src/jmh/java/org/apache/gravitino/cache/ClearEntityCacheBenchmark.java+++ b/core/src/jmh/java/org/apache/gravitino/cache/ClearEntityCacheBenchmark.java@@ -19,9 +19,6 @@package org.apache.gravitino.cache;-import org.apache.gravitino.Entity;-import org.apache.gravitino.HasIdentifier;-import org.apache.gravitino.meta.ModelEntity;import org.openjdk.jmh.annotations.Benchmark;import org.openjdk.jmh.annotations.Level;import org.openjdk.jmh.annotations.Setup;@@ -36,19 +33,15 @@ import org.openjdk.jmh.annotations.Setup;* <p>Before each invocation, the cache is pre-populated with a predefined list of entities to* ensure the {@code clear()} method operates on a non-empty cache.*- * @param <E> the entity type being tested, must extend {@link Entity} and implement {@link- * HasIdentifier}* @see org.apache.gravitino.cache.EntityCache* @see org.openjdk.jmh.annotations.Benchmark*/-public class ClearEntityCacheBenchmark<E extends Entity & HasIdentifier>- extends AbstractEntityBenchmark {+public class ClearEntityCacheBenchmark extends AbstractEntityBenchmark {@Setup(Level.Invocation)- @SuppressWarnings("unchecked")public void prepareCacheForClear() {cache.clear();- entities.forEach(e -> cache.put((ModelEntity) e));+ entities.forEach(e -> cache.put(e));}@Benchmarkdiff --git a/core/src/jmh/java/org/apache/gravitino/cache/ContainsEntityCacheBenchmark.java b/core/src/jmh/java/org/apache/gravitino/cache/ContainsEntityCacheBenchmark.javaindex 905500d1af..13c4ec2030 100644--- a/core/src/jmh/java/org/apache/gravitino/cache/ContainsEntityCacheBenchmark.java+++ b/core/src/jmh/java/org/apache/gravitino/cache/ContainsEntityCacheBenchmark.java@@ -19,26 +19,15 @@package org.apache.gravitino.cache;-import org.apache.gravitino.SupportsRelationOperations;import org.apache.gravitino.meta.ModelEntity;-import org.apache.gravitino.meta.RoleEntity;import org.openjdk.jmh.annotations.Benchmark;/*** EntityCacheContainsBenchmark benchmarks the performance of the {@link EntityCache#contains}- * operation for both regular entities and relation-based entries.+ * operation for individual entities.*- * <p>This benchmark evaluates how efficiently the cache can determine whether a given entity or a- * relationship entry exists, under different data volumes (e.g., 10, 100, 1000).- *- * <p>It includes two benchmark methods:- *- * <ul>- * <li>{@code benchmarkContains}: Checks the presence of a single {@link ModelEntity} in the- * cache.- * <li>{@code benchmarkContainsWithRelation}: Checks whether a relation entry (e.g., role-to-user- * mapping) exists for a given {@link RoleEntity} and relation type.- * </ul>+ * <p>This benchmark evaluates how efficiently the cache can determine whether a given entity+ * exists, under different data volumes (e.g., 10, 100, 1000).** @see org.apache.gravitino.cache.EntityCache* @see org.openjdk.jmh.annotations.Benchmark@@ -48,19 +37,8 @@ public class ContainsEntityCacheBenchmark extends AbstractEntityBenchmark {@Benchmarkpublic boolean benchmarkContains() {int idx = random.nextInt(entities.size());- ModelEntity sampleEntity = (ModelEntity) entities.get(idx);+ ModelEntity sampleEntity = entities.get(idx);return cache.contains(sampleEntity.nameIdentifier(), sampleEntity.type());}-- @Benchmark- @SuppressWarnings("unchecked")- public boolean benchmarkContainsWithRelation() {- RoleEntity sampleRoleEntity = (RoleEntity) BenchmarkHelper.getRandomKey(entitiesWithRelations);-- return cache.contains(- sampleRoleEntity.nameIdentifier(),- sampleRoleEntity.type(),- SupportsRelationOperations.Type.ROLE_USER_REL);- }}diff --git a/core/src/jmh/java/org/apache/gravitino/cache/GetEntityCacheBenchmark.java b/core/src/jmh/java/org/apache/gravitino/cache/GetEntityCacheBenchmark.javaindex b19a5e1ae2..abf6102335 100644--- a/core/src/jmh/java/org/apache/gravitino/cache/GetEntityCacheBenchmark.java+++ b/core/src/jmh/java/org/apache/gravitino/cache/GetEntityCacheBenchmark.java@@ -19,57 +19,27 @@package org.apache.gravitino.cache;-import java.util.List;-import java.util.Optional;import org.apache.gravitino.Entity;-import org.apache.gravitino.HasIdentifier;-import org.apache.gravitino.SupportsRelationOperations;import org.apache.gravitino.meta.ModelEntity;-import org.apache.gravitino.meta.RoleEntity;import org.openjdk.jmh.annotations.Benchmark;/*** EntityCacheGetBenchmark benchmarks the performance of the {@link EntityCache#getIfPresent}- * operation for both individual entities and entity relations.+ * operation for individual entities.** <p>This benchmark measures the efficiency of cache lookups under varying data sizes (e.g., 10,* 100, 1000 entries), helping evaluate the speed and consistency of the cache's get operations.*- * <p>It includes two benchmark methods:- *- * <ul>- * <li>{@code benchmarkGet}: Retrieves a {@link ModelEntity} by its identifier and type.- * <li>{@code benchmarkGetWithRelations}: Retrieves a list of related entities (e.g., users under- * a role) using a relation type and a {@link RoleEntity} as the lookup key.- * </ul>- *- * @param <E> the type of related entity, extending {@link Entity} and implementing {@link- * HasIdentifier}* @see org.apache.gravitino.cache.EntityCache* @see org.openjdk.jmh.annotations.Benchmark*/-public class GetEntityCacheBenchmark<E extends Entity & HasIdentifier>- extends AbstractEntityBenchmark {+public class GetEntityCacheBenchmark extends AbstractEntityBenchmark {@Benchmarkpublic Entity benchmarkGet() {int idx = random.nextInt(entities.size());- ModelEntity sampleEntity = (ModelEntity) entities.get(idx);+ ModelEntity sampleEntity = entities.get(idx);return cache.getIfPresent(sampleEntity.nameIdentifier(), sampleEntity.type()).orElse(null);}-- @Benchmark- @SuppressWarnings("unchecked")- public List<E> benchmarkGetWithRelations() {- RoleEntity sampleRoleEntity = (RoleEntity) BenchmarkHelper.getRandomKey(entitiesWithRelations);-- Optional<List<E>> relationFromCache =- cache.getIfPresent(- SupportsRelationOperations.Type.ROLE_USER_REL,- sampleRoleEntity.nameIdentifier(),- sampleRoleEntity.type());-- return relationFromCache.orElse(null);- }}diff --git a/core/src/jmh/java/org/apache/gravitino/cache/InvalidateEntityCacheBenchmark.java b/core/src/jmh/java/org/apache/gravitino/cache/InvalidateEntityCacheBenchmark.javaindex 9eb3f202a3..27dfcfe535 100644--- a/core/src/jmh/java/org/apache/gravitino/cache/InvalidateEntityCacheBenchmark.java+++ b/core/src/jmh/java/org/apache/gravitino/cache/InvalidateEntityCacheBenchmark.java@@ -21,7 +21,6 @@ package org.apache.gravitino.cache;import org.apache.gravitino.Entity;import org.apache.gravitino.NameIdentifier;-import org.apache.gravitino.meta.ModelEntity;import org.openjdk.jmh.annotations.Benchmark;import org.openjdk.jmh.annotations.Level;import org.openjdk.jmh.annotations.Setup;@@ -49,7 +48,7 @@ public class InvalidateEntityCacheBenchmark extends AbstractEntityBenchmark {@Setup(Level.Invocation)@SuppressWarnings("unchecked")public void prepareCacheForClear() {- entities.forEach(e -> cache.put((ModelEntity) e));+ entities.forEach(e -> cache.put(e));}@Benchmarkdiff --git a/core/src/jmh/java/org/apache/gravitino/cache/MeasureSizeEntityCacheBenchmark.java b/core/src/jmh/java/org/apache/gravitino/cache/MeasureSizeEntityCacheBenchmark.javaindex 5643feb10e..eeedf97365 100644--- a/core/src/jmh/java/org/apache/gravitino/cache/MeasureSizeEntityCacheBenchmark.java+++ b/core/src/jmh/java/org/apache/gravitino/cache/MeasureSizeEntityCacheBenchmark.java@@ -19,22 +19,15 @@package org.apache.gravitino.cache;-import java.util.List;-import org.apache.gravitino.Config;-import org.apache.gravitino.Entity;-import org.apache.gravitino.HasIdentifier;-import org.apache.gravitino.SupportsRelationOperations;-import org.apache.gravitino.meta.ModelEntity;-import org.apache.gravitino.meta.RoleEntity;import org.openjdk.jmh.annotations.Benchmark;/*** EntityCacheSizeBenchmark benchmarks the performance and overhead of querying the cache size via* {@link EntityCache#size()} under varying data volumes.*- * <p>During setup, both standard entities and relation-based entities are preloaded into the cache- * using the configured entity count. This ensures that the {@code size()} method operates on a- * fully populated cache with realistic structure and distribution.+ * <p>During setup, entities are preloaded into the cache using the configured entity count. This+ * ensures that the {@code size()} method operates on a fully populated cache with realistic+ * structure and distribution.** <p>The benchmark includes a single method:*@@ -43,31 +36,10 @@ import org.openjdk.jmh.annotations.Benchmark;* cached entries.* </ul>*- * @param <E> the type of related entity, extending {@link Entity} and implementing {@link- * HasIdentifier}* @see org.apache.gravitino.cache.EntityCache* @see org.openjdk.jmh.annotations.Benchmark*/-public class MeasureSizeEntityCacheBenchmark<E extends Entity & HasIdentifier>- extends AbstractEntityBenchmark {-- @Override- @SuppressWarnings("unchecked")- public void setup() {- Config config = new Config() {};- this.cache = new CaffeineEntityCache(config);- this.entities = BenchmarkHelper.getEntities(totalCnt);- this.entitiesWithRelations = BenchmarkHelper.getRelationEntities(totalCnt);-- entities.forEach(e -> cache.put((ModelEntity) e));- entitiesWithRelations.forEach(- (roleEntity, userList) ->- cache.put(- ((RoleEntity) roleEntity).nameIdentifier(),- ((RoleEntity) roleEntity).type(),- SupportsRelationOperations.Type.ROLE_USER_REL,- (List<E>) userList));- }+public class MeasureSizeEntityCacheBenchmark extends AbstractEntityBenchmark {@Benchmarkpublic long entityCacheSize() {diff --git a/core/src/jmh/java/org/apache/gravitino/cache/PutEntityCacheBenchmark.java b/core/src/jmh/java/org/apache/gravitino/cache/PutEntityCacheBenchmark.javaindex 1a20a08549..796f76bfe9 100644--- a/core/src/jmh/java/org/apache/gravitino/cache/PutEntityCacheBenchmark.java+++ b/core/src/jmh/java/org/apache/gravitino/cache/PutEntityCacheBenchmark.java@@ -19,38 +19,21 @@package org.apache.gravitino.cache;-import java.util.List;-import org.apache.gravitino.Entity;-import org.apache.gravitino.HasIdentifier;-import org.apache.gravitino.SupportsRelationOperations;-import org.apache.gravitino.meta.ModelEntity;-import org.apache.gravitino.meta.RoleEntity;import org.openjdk.jmh.annotations.Benchmark;import org.openjdk.jmh.annotations.Level;import org.openjdk.jmh.annotations.Setup;/**- * EntityCachePutBenchmark benchmarks the performance of {@link EntityCache#put} operations for both- * standard entities and relation-based entity groups.+ * EntityCachePutBenchmark benchmarks the performance of {@link EntityCache#put} operations for+ * standard entities.** <p>Each benchmark invocation starts with a cleared cache to ensure that measurements reflect* fresh insertions without side effects from previous state.*- * <p>This benchmark covers two insertion scenarios:- *- * <ul>- * <li>{@code benchmarkPut}: Inserts a batch of {@link ModelEntity} instances into the cache.- * <li>{@code benchmarkPutWithRelation}: Inserts relation mappings between {@link RoleEntity} and- * related entities (e.g., users), using a relation type as part of the cache key.- * </ul>- *- * @param <E> the type of related entity, extending {@link Entity} and implementing {@link- * HasIdentifier}* @see org.apache.gravitino.cache.EntityCache* @see org.openjdk.jmh.annotations.Benchmark*/-public class PutEntityCacheBenchmark<E extends Entity & HasIdentifier>- extends AbstractEntityBenchmark {+public class PutEntityCacheBenchmark extends AbstractEntityBenchmark {@Setup(Level.Invocation)public void prepareForCachePut() {@@ -58,20 +41,7 @@ public class PutEntityCacheBenchmark<E extends Entity & HasIdentifier>}@Benchmark- @SuppressWarnings("unchecked")public void benchmarkPut() {- entities.forEach(e -> cache.put((ModelEntity) e));- }-- @Benchmark- @SuppressWarnings("unchecked")- public void benchmarkPutWithRelation() {- entitiesWithRelations.forEach(- (role, relationEntities) ->- cache.put(- ((RoleEntity) role).nameIdentifier(),- ((RoleEntity) role).type(),- SupportsRelationOperations.Type.ROLE_USER_REL,- (List<E>) relationEntities));+ entities.forEach(e -> cache.put(e));}}diff --git a/core/src/main/java/org/apache/gravitino/cache/BaseEntityCache.java b/core/src/main/java/org/apache/gravitino/cache/BaseEntityCache.javaindex 4f4b34e5e7..0ff75c94f8 100644--- a/core/src/main/java/org/apache/gravitino/cache/BaseEntityCache.java+++ b/core/src/main/java/org/apache/gravitino/cache/BaseEntityCache.java@@ -20,7 +20,9 @@package org.apache.gravitino.cache;import com.google.common.base.Preconditions;+import com.google.common.collect.Sets;import java.util.List;+import java.util.Set;import org.apache.gravitino.Config;import org.apache.gravitino.Entity;import org.apache.gravitino.HasIdentifier;@@ -30,8 +32,49 @@ import org.apache.gravitino.NameIdentifier;* An abstract class that provides a basic implementation for the {@link EntityCache} interface.* This class is abstract and cannot be instantiated directly, it is designed to be a base class for* other entity cache implementations.+ *+ * <p>This class enforces the cacheable entity type allowlist documented on {@link+ * SupportsEntityStoreCache#put(Entity)}: {@link #put(Entity)} is final and drops entities that have+ * not been explicitly approved before delegating to {@link #doPut(Entity)}, so no subclass can+ * accidentally cache them.*/public abstract class BaseEntityCache implements EntityCache {++ /**+ * Entity types that have been explicitly approved for caching, see {@link+ * SupportsEntityStoreCache#put(Entity)} for the contract. New entity types are excluded by+ * default until their invalidation behavior has been validated.+ *+ * <p>{@code USER}, {@code GROUP} and {@code ROLE} are materialized with relation-derived data+ * joined in at load time: a role carries its securable objects, and a user/group carries its role+ * names. A mutation on the entity itself invalidates its own key through the write path, but this+ * embedded data also goes stale through a mutation on a different entity. For example, deleting+ * or renaming a securable object changes a role's materialized form, and deleting or renaming a+ * role changes a user's/group's role names. Such a mutation touches neither this entity's own key+ * nor any hierarchy ancestor of it, so neither the write-path invalidation nor a prefix cascade+ * over the entity hierarchy would evict it. Caching them would therefore serve stale+ * authorization data.+ */+ private static final Set<Entity.EntityType> CACHEABLE_TYPES =+ Sets.immutableEnumSet(+ Entity.EntityType.METALAKE,+ Entity.EntityType.CATALOG,+ Entity.EntityType.SCHEMA,+ Entity.EntityType.TABLE,+ Entity.EntityType.VIEW,+ Entity.EntityType.COLUMN,+ Entity.EntityType.FILESET,+ Entity.EntityType.TOPIC,+ Entity.EntityType.TAG,+ Entity.EntityType.MODEL,+ Entity.EntityType.MODEL_VERSION,+ Entity.EntityType.POLICY,+ Entity.EntityType.TABLE_STATISTIC,+ Entity.EntityType.JOB_TEMPLATE,+ Entity.EntityType.JOB,+ Entity.EntityType.AUDIT,+ Entity.EntityType.FUNCTION);+protected final Config cacheConfig;/**@@ -45,6 +88,45 @@ public abstract class BaseEntityCache implements EntityCache {this.cacheConfig = config;}+ /**+ * Returns whether entities of the given type may be cached.+ *+ * @param type The entity type to check.+ * @return {@code true} if entities of this type may be cached, {@code false} otherwise.+ */+ public static boolean isCacheable(Entity.EntityType type) {+ Preconditions.checkArgument(type != null, "Entity type cannot be null");++ return CACHEABLE_TYPES.contains(type);+ }++ /** {@inheritDoc} */+ @Override+ public final <E extends Entity & HasIdentifier> void put(E entity) {+ Preconditions.checkArgument(entity != null, "Entity cannot be null");++ // Called before the cacheability check and before any subclass takes this entity's lock: it+ // may take another key's lock (e.g. the model key when inserting a model version), and nesting+ // two entity locks could deadlock. A non-cacheable entity can still invalidate a cacheable one,+ // so this runs for every entity.+ invalidateOnKeyChange(entity);++ if (!isCacheable(entity.type())) {+ return;+ }++ doPut(entity);+ }++ /**+ * Caches the given entity. Called by {@link #put(Entity)} once the entity is known to be+ * non-null, cacheable, and any related cache entries have been invalidated.+ *+ * @param entity The entity to cache.+ * @param <E> The class of the entity.+ */+ protected abstract <E extends Entity & HasIdentifier> void doPut(E entity);+/*** Returns the {@link NameIdentifier} of the entity based on its type.*diff --git a/core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.java b/core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.javaindex 7d9ba181b7..bf433fcf4d 100644--- a/core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.java+++ b/core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.java@@ -26,38 +26,41 @@ import com.github.benmanes.caffeine.cache.stats.CacheStats;import com.google.common.annotations.VisibleForTesting;import com.google.common.base.Preconditions;import com.google.common.collect.Lists;-import com.google.common.collect.Sets;import com.googlecode.concurrenttrees.radix.ConcurrentRadixTree;import com.googlecode.concurrenttrees.radix.RadixTree;import com.googlecode.concurrenttrees.radix.node.concrete.DefaultCharArrayNodeFactory;-import java.util.ArrayDeque;-import java.util.ArrayList;import java.util.List;import java.util.Objects;import java.util.Optional;-import java.util.Queue;-import java.util.Set;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;-import java.util.stream.Collectors;-import org.apache.commons.lang3.ArrayUtils;-import org.apache.commons.lang3.StringUtils;import org.apache.gravitino.Config;import org.apache.gravitino.Configs;import org.apache.gravitino.Entity;import org.apache.gravitino.HasIdentifier;import org.apache.gravitino.NameIdentifier;-import org.apache.gravitino.SupportsRelationOperations;-import org.apache.gravitino.meta.GenericEntity;import org.apache.gravitino.meta.ModelVersionEntity;import org.slf4j.Logger;import org.slf4j.LoggerFactory;-/** This class implements the {@link org.apache.gravitino.cache.EntityCache} using Caffeine */+/**+ * This class implements the {@link org.apache.gravitino.cache.EntityCache} using Caffeine.+ *+ * <p>The cache stores one entry per entity, keyed by the entity's {@code NameIdentifier} and type.+ * A radix-tree prefix index over the cache keys implements cascading removal: invalidating an+ * entity also drops every cached descendant entry (e.g. invalidating a catalog drops the cached+ * schemas and tables under it).+ *+ * <p>Relation query results are NOT cached by this implementation; relation and list operations+ * always fall back to the {@code EntityStore}. Entity types whose materialized form embeds+ * relation-derived data ({@code USER}, {@code GROUP}, {@code ROLE}) are excluded from caching+ * entirely by {@link BaseEntityCache#put}, because without relation tracking their entries could+ * not be invalidated when the referenced entities change.+ */public class CaffeineEntityCache extends BaseEntityCache {private static final int CACHE_CLEANUP_CORE_THREADS = 1;private static final int CACHE_CLEANUP_MAX_THREADS = 1;@@ -84,29 +87,19 @@ public class CaffeineEntityCache extends BaseEntityCache {private final SegmentedLock segmentedLock;/** Cache data structure. */- private final Cache<EntityCacheRelationKey, List<Entity>> cacheData;+ private final Cache<EntityCacheKey, Entity> cacheData;- /** Cache reverse index structure. */- private ReverseIndexCache reverseIndex;-- /** Cache Index structure. */- private RadixTree<EntityCacheRelationKey> cacheIndex;+ /**+ * Prefix index over cache keys, used for cascading removal of descendant entries.+ *+ * <p>The tree itself is thread-safe, but {@link #clear()} swaps in a brand-new tree, so the field+ * is {@code volatile} to publish that swap to readers that do not hold a segment lock (see {@link+ * #size()}).+ */+ private volatile RadixTree<EntityCacheKey> cacheIndex;private ScheduledExecutorService scheduler;- @VisibleForTesting- public ReverseIndexCache getReverseIndex() {- return reverseIndex;- }-- private static final Set<SupportsRelationOperations.Type> RELATION_TYPES =- Sets.newHashSet(- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL,- SupportsRelationOperations.Type.ROLE_USER_REL,- SupportsRelationOperations.Type.ROLE_GROUP_REL,- SupportsRelationOperations.Type.POLICY_METADATA_OBJECT_REL,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);-/*** Constructs a new {@link CaffeineEntityCache}.*@@ -115,13 +108,12 @@ public class CaffeineEntityCache extends BaseEntityCache {public CaffeineEntityCache(Config cacheConfig) {super(cacheConfig);this.cacheIndex = new ConcurrentRadixTree<>(new DefaultCharArrayNodeFactory());- this.reverseIndex = new ReverseIndexCache();// Initialize segmented lockint lockSegments = cacheConfig.get(Configs.CACHE_LOCK_SEGMENTS);this.segmentedLock = new SegmentedLock(lockSegments);- Caffeine<EntityCacheKey, List<Entity>> cacheDataBuilder = newBaseBuilder(cacheConfig);+ Caffeine<EntityCacheKey, Entity> cacheDataBuilder = newBaseBuilder(cacheConfig);cacheDataBuilder.executor(CLEANUP_EXECUTOR)@@ -151,137 +143,49 @@ public class CaffeineEntityCache extends BaseEntityCache {}@VisibleForTesting- public Cache<EntityCacheRelationKey, List<Entity>> getCacheData() {+ public Cache<EntityCacheKey, Entity> getCacheData() {return this.cacheData;}- /** {@inheritDoc} */- @Override- public <E extends Entity & HasIdentifier> Optional<List<E>> getIfPresent(- SupportsRelationOperations.Type relType,- NameIdentifier nameIdentifier,- Entity.EntityType identType) {- checkArguments(nameIdentifier, identType, relType);-- List<Entity> entitiesFromCache =- cacheData.getIfPresent(EntityCacheRelationKey.of(nameIdentifier, identType, relType));- return Optional.ofNullable(entitiesFromCache).map(BaseEntityCache::convertEntities);- }-/** {@inheritDoc} */@Overridepublic <E extends Entity & HasIdentifier> Optional<E> getIfPresent(NameIdentifier ident, Entity.EntityType type) {checkArguments(ident, type);- List<Entity> entitiesFromCache = cacheData.getIfPresent(EntityCacheRelationKey.of(ident, type));+ Entity entityFromCache = cacheData.getIfPresent(EntityCacheKey.of(ident, type));- return Optional.ofNullable(entitiesFromCache)- .filter(l -> !l.isEmpty())- .map(entities -> convertEntity(entities.get(0)));- }-- /** {@inheritDoc} */- @Override- public boolean invalidate(- NameIdentifier ident, Entity.EntityType type, SupportsRelationOperations.Type relType) {- checkArguments(ident, type, relType);-- return segmentedLock.withLock(- EntityCacheRelationKey.of(ident, type, relType),- () -> {- invalidateEntities(ident, type, Optional.of(relType));- return true;- });- }-- /** {@inheritDoc} */- @Override- public boolean invalidateRelationEntry(- NameIdentifier ident, Entity.EntityType type, SupportsRelationOperations.Type relType) {- checkArguments(ident, type, relType);- EntityCacheRelationKey key = EntityCacheRelationKey.of(ident, type, relType);- return segmentedLock.withLock(- key,- () -> {- // Drop the cached relation result, its index entry, and the reverse-index bookkeeping- // for this relation key only. Do NOT cascade through the reverse index to other- // entities: the reverse index is shared (e.g. all roles bound to one metadata object),- // and a BFS cascade would evict their mappings. cacheData.invalidate is explicit so it- // bypasses the removal listener; reverseIndex.remove(key) then cleans up only this- // entry's own bookkeeping (entityToReverseIndexMap + reverseIndex references to it).- cacheData.invalidate(key);- reverseIndex.remove(key);- cacheIndex.remove(key.toString());- return true;- });+ return Optional.ofNullable(entityFromCache).map(BaseEntityCache::convertEntity);}/** {@inheritDoc} */@Overridepublic boolean invalidate(NameIdentifier ident, Entity.EntityType type) {checkArguments(ident, type);+ EntityCacheKey key = EntityCacheKey.of(ident, type);return segmentedLock.withLock(- EntityCacheRelationKey.of(ident, type),+ key,() -> {- // Clear possible relation first, then clear the main entity cache.- // For example, if a tag has been updated, apart from invalidating the relation:- // metadata_object_to_tag_rel, we also need to invalidate the tag_to_metadata_object_rel.- // Assuming a tag "tag1" is related to a metadata object "catalog", when "catalog" is- // renamed to `catalog_new`, we need to invalidate both relations to avoid stale data.- // that is: before: tag1:TAG_METADATA_OBJECT_REL -> catalog,- // catalog:TAG_METADATA_OBJECT_REL -> tag1, after: tag1:TAG_METADATA_OBJECT_REL -> null,- // catalog:TAG_METADATA_OBJECT_REL -> null.- RELATION_TYPES.forEach(- relType -> {- List<Entity> relatedEntities =- cacheData.getIfPresent(EntityCacheRelationKey.of(ident, type, relType));- if (relatedEntities != null) {- relatedEntities.stream()- .filter(e -> StringUtils.isNotBlank(((HasIdentifier) e).name()))- .forEach(- entity -> {- NameIdentifier identifier = ((HasIdentifier) entity).nameIdentifier();- if (entity instanceof GenericEntity) {- String metalakeName = ident.namespace().level(0);- String[] names =- ArrayUtils.addFirst(- identifier.namespace().levels(), metalakeName);- names = ArrayUtils.add(names, identifier.name());- identifier = NameIdentifier.of(names);- }-- invalidateEntities(identifier, entity.type(), Optional.of(relType));- });- }- });-- RELATION_TYPES.forEach(- relType -> {- invalidateEntities(ident, type, Optional.of(relType));- });-- invalidateEntities(ident, type, Optional.empty());+ invalidateHierarchy(key);return true;});}- /** {@inheritDoc} */- @Override- public boolean contains(- NameIdentifier ident, Entity.EntityType type, SupportsRelationOperations.Type relType) {- checkArguments(ident, type, relType);- return cacheData.getIfPresent(EntityCacheRelationKey.of(ident, type, relType)) != null;- }-/** {@inheritDoc} */@Overridepublic boolean contains(NameIdentifier ident, Entity.EntityType type) {checkArguments(ident, type);- return cacheData.getIfPresent(EntityCacheRelationKey.of(ident, type)) != null;+ return cacheData.getIfPresent(EntityCacheKey.of(ident, type)) != null;}- /** {@inheritDoc} */+ /**+ * {@inheritDoc}+ *+ * <p>Read from the prefix index without holding a segment lock, so the result is a point-in-time+ * estimate: entries concurrently added or cascaded away may or may not be counted. It may also+ * briefly exceed the number of live entries, because entries evicted by Caffeine are removed from+ * the index asynchronously by the removal listener.+ */@Overridepublic long size() {return cacheIndex.size();@@ -293,40 +197,24 @@ public class CaffeineEntityCache extends BaseEntityCache {segmentedLock.withGlobalLock(() -> {cacheData.invalidateAll();+ cacheIndex = new ConcurrentRadixTree<>(new DefaultCharArrayNodeFactory());});}/** {@inheritDoc} */@Override- public <E extends Entity & HasIdentifier> void put(- NameIdentifier ident,- Entity.EntityType type,- SupportsRelationOperations.Type relType,- List<E> entities) {- checkArguments(ident, type, relType);- Preconditions.checkArgument(entities != null, "Entities cannot be null");- EntityCacheRelationKey entityCacheKey = EntityCacheRelationKey.of(ident, type, relType);- segmentedLock.withLock(- entityCacheKey,- () -> {- syncEntitiesToCache(- entityCacheKey, entities.stream().map(e -> (Entity) e).collect(Collectors.toList()));- });- }-- /** {@inheritDoc} */- @Override- public <E extends Entity & HasIdentifier> void put(E entity) {- Preconditions.checkArgument(entity != null, "Entity cannot be null");-+ protected <E extends Entity & HasIdentifier> void doPut(E entity) {NameIdentifier identifier = getIdentFromEntity(entity);- EntityCacheRelationKey entityCacheKey = EntityCacheRelationKey.of(identifier, entity.type());+ EntityCacheKey entityCacheKey = EntityCacheKey.of(identifier, entity.type());segmentedLock.withLock(entityCacheKey,() -> {- invalidateOnKeyChange(entity);- syncEntitiesToCache(entityCacheKey, Lists.newArrayList(entity));+ cacheData.put(entityCacheKey, entity);+ // If the entry was rejected (e.g. it exceeds the maximum weight), skip indexing it.+ if (cacheData.policy().getIfPresentQuietly(entityCacheKey) != null) {+ cacheIndex.put(entityCacheKey.toString(), entityCacheKey);+ }});}@@ -362,16 +250,6 @@ public class CaffeineEntityCache extends BaseEntityCache {return segmentedLock.withLockAndThrow(key, action);}- /** {@inheritDoc} */- @Override- public <T, E extends Exception> T withMultipleKeyCacheLock(- List<EntityCacheKey> keys, EntityCache.ThrowingSupplier<T, E> action) throws E {- Preconditions.checkArgument(keys != null, "Keys cannot be null");- Preconditions.checkArgument(action != null, "Action cannot be null");-- return segmentedLock.withMultipleKeyLockAndThrow(keys, action);- }-/*** Removes the expired entity from the cache. This method is a hook method for the Cache, when an* entry expires, it will call this method.@@ -383,35 +261,28 @@ public class CaffeineEntityCache extends BaseEntityCache {segmentedLock.withLock(key,() -> {- reverseIndex.remove(key);cacheIndex.remove(key.toString());});}/**- * Syncs the entities to the cache, if entities are too big and cannot put to the cache, then it- * will be removed from the cache, and cacheIndex will not be updated.+ * Removes the entry for the given key and all cached descendant entries. Descendants are found+ * through the prefix index: every child identifier starts with {@code parent identifier + "."},+ * so the scan is exact for children and never matches siblings sharing a name prefix (e.g. {@code+ * catalog1} vs {@code catalog10}).*- * @param key The key of the entities.- * @param newEntities The new entities to sync to the cache.+ * @param key The key of the entity whose subtree should be invalidated*/- private void syncEntitiesToCache(EntityCacheRelationKey key, List<Entity> newEntities) {- List<Entity> existingEntities = cacheData.getIfPresent(key);-- if (existingEntities != null && key.relationType() != null) {- Set<Entity> merged = Sets.newLinkedHashSet(existingEntities);- merged.addAll(newEntities);- newEntities = new ArrayList<>(merged);- }-- cacheData.put(key, newEntities);-- for (Entity entity : newEntities) {- reverseIndex.indexEntity(entity, key);- }-- if (cacheData.policy().getIfPresentQuietly(key) != null) {- cacheIndex.put(key.toString(), key);+ private void invalidateHierarchy(EntityCacheKey key) {+ cacheData.invalidate(key);+ cacheIndex.remove(key.toString());++ String childPrefix = key.identifier().toString() + ".";+ List<EntityCacheKey> childKeys =+ Lists.newArrayList(cacheIndex.getValuesForKeysStartingWith(childPrefix));+ for (EntityCacheKey childKey : childKeys) {+ cacheData.invalidate(childKey);+ cacheIndex.remove(childKey.toString());}}@@ -446,80 +317,6 @@ public class CaffeineEntityCache extends BaseEntityCache {return (Caffeine<KEY, VALUE>) builder;}- /**- * Invalidate entities with iterative BFS algorithm.- *- * @param identifier The identifier of the entity to invalidate- */- private boolean invalidateEntities(- NameIdentifier identifier,- Entity.EntityType type,- Optional<SupportsRelationOperations.Type> relTypeOpt) {- Queue<EntityCacheKey> queue = new ArrayDeque<>();-- EntityCacheRelationKey valueForExactKey =- cacheIndex.getValueForExactKey(- relTypeOpt.isEmpty()- ? EntityCacheRelationKey.of(identifier, type).toString()- : EntityCacheRelationKey.of(identifier, type, relTypeOpt.get()).toString());-- if (valueForExactKey == null) {- // It means the key does not exist in the cache. However, we still need to handle some cases.- // For example, we have stored a role entity in the cache and entity to role mapping in the- // reverse index. This is: cache data: role identifier -> role entity, reverse index:- // the securable object -> role. When we update the securable object, we need to invalidate- // the role entity from the cache though the securable object is not in the cache data.- valueForExactKey = EntityCacheRelationKey.of(identifier, type, relTypeOpt.orElse(null));- }-- // The visited set to avoid processing the same key multiple times and thus causing infinite- // loop.- Set<EntityCacheKey> visited = Sets.newHashSet();- queue.offer(valueForExactKey);- while (!queue.isEmpty()) {- EntityCacheKey currentKeyToRemove = queue.poll();- if (visited.contains(currentKeyToRemove)) {- continue;- }- visited.add(currentKeyToRemove);-- cacheData.invalidate(currentKeyToRemove);- cacheIndex.remove(currentKeyToRemove.toString());-- // Remove related entity keys- List<EntityCacheKey> relatedEntityKeysToRemove =- Lists.newArrayList(- cacheIndex.getValuesForKeysStartingWith(currentKeyToRemove.identifier().toString()));- queue.addAll(relatedEntityKeysToRemove);-- // Look up from reverse index to go to next depth- List<List<EntityCacheKey>> reverseKeysToRemove =- Lists.newArrayList(- reverseIndex.getValuesForKeysStartingWith(- currentKeyToRemove.identifier().toString()));-- reverseKeysToRemove.forEach(- key -> {- // Remove from reverse index- // Convert EntityCacheRelationKey to EntityCacheKey- key.stream()- .forEach(- k ->- reverseIndex- .getValuesForKeysStartingWith(k.toString())- .forEach(rsk -> rsk.forEach(v -> reverseIndex.remove(v))));- });-- reverseIndex.remove(currentKeyToRemove);- Set<EntityCacheKey> toAdd =- Sets.newHashSet(- reverseKeysToRemove.stream().flatMap(List::stream).collect(Collectors.toList()));- queue.addAll(toAdd);- }-- return true;- }-/** Starts the cache stats monitor. */private void startCacheStatsMonitor() {scheduler.scheduleAtFixedRate(@@ -539,19 +336,6 @@ public class CaffeineEntityCache extends BaseEntityCache {TimeUnit.MINUTES);}- /**- * Checks the arguments for the methods. All arguments must not be null.- *- * @param ident The identifier of the entity to check- * @param type The type of the entity to check- * @param relType The relation type of the entity to check- */- private void checkArguments(- NameIdentifier ident, Entity.EntityType type, SupportsRelationOperations.Type relType) {- checkArguments(ident, type);- Preconditions.checkArgument(relType != null, "relType cannot be null");- }-/*** Checks the arguments for the methods. All arguments must not be null.*diff --git a/core/src/main/java/org/apache/gravitino/cache/EntityCache.java b/core/src/main/java/org/apache/gravitino/cache/EntityCache.javaindex 218c5099d0..0ea458bf36 100644--- a/core/src/main/java/org/apache/gravitino/cache/EntityCache.java+++ b/core/src/main/java/org/apache/gravitino/cache/EntityCache.java@@ -19,16 +19,15 @@package org.apache.gravitino.cache;-import java.util.List;-import org.apache.gravitino.Entity;-/*** {@code EntityCache} is a cache interface in Gravitino designed to accelerate metadata access for- * entities. It maintains a bidirectional mapping between an {@link Entity} and its {@code- * NameIdentifier}. The cache also supports cascading removal of entries, ensuring related- * sub-entities are cleared together.+ * individual entities keyed by their {@code NameIdentifier} and type. The cache supports cascading+ * removal of entries, ensuring cached sub-entities are cleared together with their parent.+ *+ * <p>Relation query results (e.g. role bindings of a metadata object) are intentionally NOT part of+ * this SPI; relation and list operations always fall back to the {@code EntityStore}.*/-public interface EntityCache extends SupportsEntityStoreCache, SupportsRelationEntityCache {+public interface EntityCache extends SupportsEntityStoreCache {/*** Clears all entries from the cache, including data and index, resetting it to an empty state.*/@@ -62,20 +61,6 @@ public interface EntityCache extends SupportsEntityStoreCache, SupportsRelationE<T, E extends Exception> T withCacheLock(EntityCacheKey key, ThrowingSupplier<T, E> action)throws E;- /**- * Acquires locks for multiple cache keys and executes the action, returning the result. Keys are- * locked in a consistent order to avoid deadlocks.- *- * @param keys The cache keys to lock- * @param action The action to execute while holding all locks- * @param <T> The type of the result- * @param <E> The type of exception that may be thrown- * @return The result of the action- * @throws E if the action throws an exception of type E- */- <T, E extends Exception> T withMultipleKeyCacheLock(- List<EntityCacheKey> keys, ThrowingSupplier<T, E> action) throws E;-/*** A functional interface that represents a supplier that may throw an exception.*diff --git a/core/src/main/java/org/apache/gravitino/cache/EntityCacheRelationKey.java b/core/src/main/java/org/apache/gravitino/cache/EntityCacheRelationKey.javadeleted file mode 100644index eac5950d4f..0000000000--- a/core/src/main/java/org/apache/gravitino/cache/EntityCacheRelationKey.java+++ /dev/null@@ -1,121 +0,0 @@-/*- * 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.gravitino.cache;--import java.util.Objects;-import org.apache.gravitino.Entity;-import org.apache.gravitino.NameIdentifier;-import org.apache.gravitino.SupportsRelationOperations;--/** Key for Entity cache. */-public class EntityCacheRelationKey extends EntityCacheKey {- private final SupportsRelationOperations.Type relationType;-- /**- * Creates a new instance of {@link EntityCacheRelationKey} with the given arguments.- *- * @param ident The identifier of the entity.- * @param type The type of the entity.- * @param relationType The type of the relation, it can be null.- * @return A new instance of {@link EntityCacheRelationKey}.- */- public static EntityCacheRelationKey of(- NameIdentifier ident, Entity.EntityType type, SupportsRelationOperations.Type relationType) {- return new EntityCacheRelationKey(ident, type, relationType);- }-- /**- * Creates a new instance of {@link EntityCacheRelationKey} with the given arguments.- *- * @param ident The identifier of the entity.- * @param type The type of the entity.- * @return A new instance of {@link EntityCacheRelationKey}.- */- public static EntityCacheRelationKey of(NameIdentifier ident, Entity.EntityType type) {- return new EntityCacheRelationKey(ident, type, null);- }-- /**- * Creates a new instance of {@link EntityCacheRelationKey} with the given parameters.- *- * @param identifier The identifier of the entity.- * @param type The type of the entity.- * @param relationType The type of the relation.- */- private EntityCacheRelationKey(- NameIdentifier identifier,- Entity.EntityType type,- SupportsRelationOperations.Type relationType) {- super(identifier, type);- this.relationType = relationType;- }-- /**- * Returns the type of the relation.- *- * @return The type of the relation.- */- public SupportsRelationOperations.Type relationType() {- return relationType;- }-- /**- * Compares two instances of {@link EntityCacheRelationKey} for equality. The comparison is done- * by comparing the identifier, type, and relationType of the instances.- *- * @param obj The object to compare to.- * @return {@code true} if the objects are equal, {@code false} otherwise.- */- @Override- public boolean equals(Object obj) {- if (obj == this) return true;- if (!(obj instanceof EntityCacheRelationKey)) return false;- EntityCacheRelationKey other = (EntityCacheRelationKey) obj;-- return super.equals(obj) && Objects.equals(relationType, other.relationType);- }-- /**- * Returns a hash code for this instance. The hash code is calculated by hashing the identifier,- * type, and relationType of the instance.- *- * @return A hash code for this instance.- */- @Override- public int hashCode() {- return Objects.hash(super.identifier(), super.entityType(), relationType);- }-- /**- * Returns a string representation of this instance. The string is formatted as- * "identifier:type:relationType".- *- * @return A string representation of this instance.- */- @Override- public String toString() {- String stringExpr = super.identifier().toString() + ":" + super.entityType().toString();- if (relationType != null) {- stringExpr += ":" + relationType.name();- }-- return stringExpr;- }-}diff --git a/core/src/main/java/org/apache/gravitino/cache/EntityCacheWeigher.java b/core/src/main/java/org/apache/gravitino/cache/EntityCacheWeigher.javaindex f9f1212dd7..0fde7e2414 100644--- a/core/src/main/java/org/apache/gravitino/cache/EntityCacheWeigher.java+++ b/core/src/main/java/org/apache/gravitino/cache/EntityCacheWeigher.java@@ -23,13 +23,10 @@ import com.github.benmanes.caffeine.cache.Weigher;import com.google.common.annotations.VisibleForTesting;import com.google.common.base.Preconditions;import com.google.common.collect.ImmutableMap;-import java.util.List;import java.util.Map;import lombok.NonNull;import org.apache.gravitino.Entity;import org.checkerframework.checker.index.qual.NonNegative;-import org.slf4j.Logger;-import org.slf4j.LoggerFactory;/*** A {@link Weigher} implementation that calculates the weight of an entity based on its type. In@@ -53,14 +50,13 @@ import org.slf4j.LoggerFactory;* <p>Note: Caffeine's W-TinyLFU algorithm considers both access frequency and weight. Frequently* accessed heavier entries may still be retained over infrequently accessed lighter entries.*/-public class EntityCacheWeigher implements Weigher<EntityCacheKey, List<Entity>> {+public class EntityCacheWeigher implements Weigher<EntityCacheKey, Entity> {public static final int METALAKE_WEIGHT = 0; // 0 means never evictpublic static final int CATALOG_WEIGHT = 0;public static final int SCHEMA_WEIGHT = 100; // Lower weight = higher retention prioritypublic static final int OTHER_WEIGHT = 200;public static final int TAG_WEIGHT = 500;public static final int POLICY_WEIGHT = 500;- private static final Logger LOG = LoggerFactory.getLogger(EntityCacheWeigher.class.getName());private static final EntityCacheWeigher INSTANCE = new EntityCacheWeigher();private static final Map<Entity.EntityType, Integer> ENTITY_WEIGHTS =ImmutableMap.of(@@ -124,17 +120,8 @@ public class EntityCacheWeigher implements Weigher<EntityCacheKey, List<Entity>>/** {@inheritDoc} */@Overridepublic @NonNegative int weigh(- @NonNull EntityCacheKey storeEntityCacheKey, @NonNull List<Entity> entities) {- int weight = 0;- for (Entity entity : entities) {- weight += calculateWeight(entity.type());- }-- if (weight > getMaxWeight()) {- LOG.warn("Entity group exceeds max weight: {}", weight);- }-- return weight;+ @NonNull EntityCacheKey storeEntityCacheKey, @NonNull Entity entity) {+ return calculateWeight(entity.type());}private int calculateWeight(Entity.EntityType entityType) {diff --git a/core/src/main/java/org/apache/gravitino/cache/NoOpsCache.java b/core/src/main/java/org/apache/gravitino/cache/NoOpsCache.javaindex fad08c2c2b..8a8aab9956 100644--- a/core/src/main/java/org/apache/gravitino/cache/NoOpsCache.java+++ b/core/src/main/java/org/apache/gravitino/cache/NoOpsCache.java@@ -19,13 +19,11 @@package org.apache.gravitino.cache;-import java.util.List;import java.util.Optional;import org.apache.gravitino.Config;import org.apache.gravitino.Entity;import org.apache.gravitino.HasIdentifier;import org.apache.gravitino.NameIdentifier;-import org.apache.gravitino.SupportsRelationOperations;/** A cache implementation that does not cache anything. */public class NoOpsCache extends BaseEntityCache {@@ -71,13 +69,6 @@ public class NoOpsCache extends BaseEntityCache {return opLock.withLockAndThrow(key, action);}- /** {@inheritDoc} */- @Override- public <T, E extends Exception> T withMultipleKeyCacheLock(- List<EntityCacheKey> keys, ThrowingSupplier<T, E> action) throws E {- return opLock.withMultipleKeyLockAndThrow(keys, action);- }-/** {@inheritDoc} */@Overridepublic <E extends Entity & HasIdentifier> Optional<E> getIfPresent(@@ -99,7 +90,7 @@ public class NoOpsCache extends BaseEntityCache {/** {@inheritDoc} */@Override- public <E extends Entity & HasIdentifier> void put(E entity) {+ protected <E extends Entity & HasIdentifier> void doPut(E entity) {// do nothing}@@ -108,44 +99,4 @@ public class NoOpsCache extends BaseEntityCache {public <E extends Entity & HasIdentifier> void invalidateOnKeyChange(E entity) {// do nothing}-- /** {@inheritDoc} */- @Override- public <E extends Entity & HasIdentifier> Optional<List<E>> getIfPresent(- SupportsRelationOperations.Type relType,- NameIdentifier nameIdentifier,- Entity.EntityType identType) {- return Optional.empty();- }-- /** {@inheritDoc} */- @Override- public boolean invalidate(- NameIdentifier ident, Entity.EntityType type, SupportsRelationOperations.Type relType) {- return false;- }-- /** {@inheritDoc} */- @Override- public boolean invalidateRelationEntry(- NameIdentifier ident, Entity.EntityType type, SupportsRelationOperations.Type relType) {- return false;- }-- /** {@inheritDoc} */- @Override- public boolean contains(- NameIdentifier ident, Entity.EntityType type, SupportsRelationOperations.Type relType) {- return false;- }-- /** {@inheritDoc} */- @Override- public <E extends Entity & HasIdentifier> void put(- NameIdentifier ident,- Entity.EntityType type,- SupportsRelationOperations.Type relType,- List<E> entities) {- // do nothing- }}diff --git a/core/src/main/java/org/apache/gravitino/cache/ReverseIndexCache.java b/core/src/main/java/org/apache/gravitino/cache/ReverseIndexCache.javadeleted file mode 100644index a2956dbf06..0000000000--- a/core/src/main/java/org/apache/gravitino/cache/ReverseIndexCache.java+++ /dev/null@@ -1,183 +0,0 @@-/*- * 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.gravitino.cache;--import com.google.common.base.Preconditions;-import com.google.common.collect.HashMultimap;-import com.google.common.collect.Lists;-import com.google.common.collect.Maps;-import com.google.common.collect.Multimap;-import com.googlecode.concurrenttrees.radix.ConcurrentRadixTree;-import com.googlecode.concurrenttrees.radix.RadixTree;-import com.googlecode.concurrenttrees.radix.node.concrete.DefaultCharArrayNodeFactory;-import java.util.Collection;-import java.util.List;-import java.util.Map;-import org.apache.commons.collections4.CollectionUtils;-import org.apache.gravitino.Entity;-import org.apache.gravitino.HasIdentifier;-import org.apache.gravitino.NameIdentifier;-import org.apache.gravitino.meta.GenericEntity;-import org.apache.gravitino.meta.GroupEntity;-import org.apache.gravitino.meta.PolicyEntity;-import org.apache.gravitino.meta.RoleEntity;-import org.apache.gravitino.meta.TagEntity;-import org.apache.gravitino.meta.UserEntity;--/**- * Reverse index cache for managing entity relationships. This cache uses a radix tree to- * efficiently store and retrieve relationships between entities based on their keys.- */-public class ReverseIndexCache {- private final RadixTree<List<EntityCacheKey>> reverseIndex;-- /** Registers a reverse index processor for a specific entity class. */- private final Multimap<Class<? extends Entity>, ReverseIndexRule> reverseIndexRules =- HashMultimap.create();-- /**- * Map from data entity key to a list of entity cache relation keys. This is used for reverse- * indexing.- *- * <p>For example, a role entity may be related to multiple securable objects, so we need to- * maintain a mapping from the role entity key to the list of securable object keys. That is- * entityToReverseIndexMap: roleEntityKey -> [securableObjectKey1, securableObjectKey2, ...]- *- * <p>This map is used to quickly find all the related entity cache keys when we need to- * invalidate in the reverse index if a role entity is updated. The following is an example: a- * Role a has securable objects s1 and s2, so we have the following mapping: <br>- * cacheData: role1 -> role entity </br> <br>- * reverseIndex: s1 -> [role1], s2 -> [role1] </br>- *- * <p>This map will be: <br>- * role1 -> [s1, s2] </br>- *- * <p>When we update role1, we need to invalidate s1 and s2 from the reverse index via this map,- * or the data will be in the memory forever.- */- private final Map<EntityCacheKey, List<EntityCacheKey>> entityToReverseIndexMap =- Maps.newConcurrentMap();-- public ReverseIndexCache() {- this.reverseIndex = new ConcurrentRadixTree<>(new DefaultCharArrayNodeFactory());-- registerReverseRule(UserEntity.class, ReverseIndexRules.USER_ROLE_REVERSE_RULE);- registerReverseRule(UserEntity.class, ReverseIndexRules.USER_OWNERSHIP_REVERSE_RULE);- registerReverseRule(GroupEntity.class, ReverseIndexRules.GROUP_ROLE_REVERSE_RULE);- registerReverseRule(GroupEntity.class, ReverseIndexRules.GROUP_OWNERSHIP_REVERSE_RULE);- registerReverseRule(RoleEntity.class, ReverseIndexRules.ROLE_SECURABLE_OBJECT_REVERSE_RULE);- registerReverseRule(PolicyEntity.class, ReverseIndexRules.POLICY_SECURABLE_OBJECT_REVERSE_RULE);- registerReverseRule(TagEntity.class, ReverseIndexRules.TAG_SECURABLE_OBJECT_REVERSE_RULE);- registerReverseRule(- GenericEntity.class, ReverseIndexRules.GENERIC_METADATA_OBJECT_REVERSE_RULE);- }-- public Iterable<List<EntityCacheKey>> getValuesForKeysStartingWith(String keyPrefix) {- return reverseIndex.getValuesForKeysStartingWith(keyPrefix);- }-- public boolean remove(EntityCacheKey key) {- List<EntityCacheKey> relatedKeys = entityToReverseIndexMap.remove(key);- if (CollectionUtils.isNotEmpty(relatedKeys)) {- for (EntityCacheKey relatedKey : relatedKeys) {- List<EntityCacheKey> existingKeys = reverseIndex.getValueForExactKey(relatedKey.toString());- if (existingKeys != null && existingKeys.contains(key)) {- List<EntityCacheKey> newValues = Lists.newArrayList(existingKeys);- newValues.remove(key);- if (newValues.isEmpty()) {- reverseIndex.remove(relatedKey.toString());- } else {- reverseIndex.put(relatedKey.toString(), newValues);- }- }- }- }-- return reverseIndex.remove(key.toString());- }-- public int size() {- return reverseIndex.size();- }-- public void put(- NameIdentifier nameIdentifier, Entity.EntityType type, EntityCacheRelationKey key) {- EntityCacheKey entityCacheKey = EntityCacheKey.of(nameIdentifier, type);- entityToReverseIndexMap.computeIfAbsent(key, k -> Lists.newArrayList()).add(entityCacheKey);-- List<EntityCacheKey> existingKeys = reverseIndex.getValueForExactKey(entityCacheKey.toString());- if (existingKeys == null) {- reverseIndex.put(entityCacheKey.toString(), List.of(key));- } else {- if (existingKeys.contains(key)) {- return;- }-- List<EntityCacheKey> newValues = Lists.newArrayList(existingKeys);- newValues.add(key);- reverseIndex.put(entityCacheKey.toString(), newValues);- }- }-- public List<EntityCacheKey> get(NameIdentifier nameIdentifier, Entity.EntityType type) {- EntityCacheKey entityCacheKey = EntityCacheKey.of(nameIdentifier, type);- return reverseIndex.getValueForExactKey(entityCacheKey.toString());- }-- public void put(Entity entity, EntityCacheRelationKey key) {- Preconditions.checkArgument(entity != null, "EntityCacheRelationKey cannot be null");-- if (entity instanceof HasIdentifier) {- NameIdentifier nameIdent = ((HasIdentifier) entity).nameIdentifier();- put(nameIdent, entity.type(), key);- }- }-- public void registerReverseRule(Class<? extends Entity> entityClass, ReverseIndexRule rule) {- reverseIndexRules.put(entityClass, rule);- }-- /** Processes an entity and updates the reverse index accordingly. */- public void indexEntity(Entity entity, EntityCacheRelationKey key) {- Collection<ReverseIndexRule> rules = reverseIndexRules.get(entity.getClass());- if (!rules.isEmpty()) {- for (ReverseIndexRule rule : rules) {- rule.indexEntity(entity, key, this);- }- }- }-- /** Functional interface for processing reverse index rules. */- @FunctionalInterface- interface ReverseIndexRule {- void indexEntity(Entity entity, EntityCacheRelationKey key, ReverseIndexCache cache);- }-- @Override- public String toString() {- Iterable<CharSequence> keys = reverseIndex.getKeysStartingWith("");- StringBuilder sb = new StringBuilder();- for (CharSequence key : keys) {- sb.append(key).append(" -> ").append(reverseIndex.getValueForExactKey(key.toString()));- sb.append("\n");- }-- return sb.toString();- }-}diff --git a/core/src/main/java/org/apache/gravitino/cache/ReverseIndexRules.java b/core/src/main/java/org/apache/gravitino/cache/ReverseIndexRules.javadeleted file mode 100644index 822f0459fd..0000000000--- a/core/src/main/java/org/apache/gravitino/cache/ReverseIndexRules.java+++ /dev/null@@ -1,164 +0,0 @@-/*- * 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.gravitino.cache;--import org.apache.commons.lang3.ArrayUtils;-import org.apache.gravitino.Entity;-import org.apache.gravitino.Entity.EntityType;-import org.apache.gravitino.NameIdentifier;-import org.apache.gravitino.Namespace;-import org.apache.gravitino.SupportsRelationOperations;-import org.apache.gravitino.meta.GenericEntity;-import org.apache.gravitino.meta.GroupEntity;-import org.apache.gravitino.meta.PolicyEntity;-import org.apache.gravitino.meta.RoleEntity;-import org.apache.gravitino.meta.TagEntity;-import org.apache.gravitino.meta.UserEntity;-import org.apache.gravitino.utils.MetadataObjectUtil;-import org.apache.gravitino.utils.NamespaceUtil;--/**- * Reverse index rules for different entity types. This class defines how to process reverse- * indexing for UserEntity, GroupEntity, and RoleEntity. <br>- * For example: <br>- * - UserEntity role is {metalake-name}.system.user.{user-name}:USER-{serial-number} <br>- * - GroupEntity role is {metalake-name}.system.group.{group-name}:GROUP-{serial-number} <br>- * - RoleEntity role is {metalake-name}.system.role.{role-name}:ROLE-{serial-number} <br>- */-public class ReverseIndexRules {-- /** UserEntity reverse index processor */- public static final ReverseIndexCache.ReverseIndexRule USER_ROLE_REVERSE_RULE =- (entity, key, reverseIndexCache) -> {- UserEntity userEntity = (UserEntity) entity;- if (userEntity.roleNames() != null) {- userEntity- .roleNames()- .forEach(- role -> {- Namespace ns = NamespaceUtil.ofRole(userEntity.namespace().level(0));- NameIdentifier nameIdentifier = NameIdentifier.of(ns, role);- reverseIndexCache.put(nameIdentifier, Entity.EntityType.ROLE, key);- });- }- };-- public static final ReverseIndexCache.ReverseIndexRule USER_OWNERSHIP_REVERSE_RULE =- (entity, key, reverseIndexCache) -> {- UserEntity userEntity = (UserEntity) entity;- // Handle Securable Objects -> User reverse index, so the key type is User and the value- // type is securable Object.- if (key.relationType() == SupportsRelationOperations.Type.OWNER_REL) {- reverseIndexCache.put(userEntity.nameIdentifier(), EntityType.USER, key);- }- };-- /** GroupEntity reverse index processor */- public static final ReverseIndexCache.ReverseIndexRule GROUP_ROLE_REVERSE_RULE =- (entity, key, reverseIndexCache) -> {- GroupEntity groupEntity = (GroupEntity) entity;- if (groupEntity.roleNames() != null) {- groupEntity- .roleNames()- .forEach(- role -> {- Namespace ns = NamespaceUtil.ofRole(groupEntity.namespace().level(0));- NameIdentifier nameIdentifier = NameIdentifier.of(ns, role);- reverseIndexCache.put(nameIdentifier, Entity.EntityType.ROLE, key);- });- }- };-- public static final ReverseIndexCache.ReverseIndexRule GROUP_OWNERSHIP_REVERSE_RULE =- (entity, key, reverseIndexCache) -> {- GroupEntity groupEntity = (GroupEntity) entity;- // Handle Securable Objects -> Group reverse index, so the key type is group and the value- // type is securable Object.- if (key.relationType() == SupportsRelationOperations.Type.OWNER_REL) {- reverseIndexCache.put(groupEntity.nameIdentifier(), EntityType.GROUP, key);- }- };-- /** RoleEntity reverse index processor */- public static final ReverseIndexCache.ReverseIndexRule ROLE_SECURABLE_OBJECT_REVERSE_RULE =- (entity, key, reverseIndexCache) -> {- RoleEntity roleEntity = (RoleEntity) entity;- if (roleEntity.securableObjects() != null) {- roleEntity- .securableObjects()- .forEach(- securableObject -> {- NameIdentifier securableObjectIdent =- MetadataObjectUtil.toEntityIdent(- roleEntity.namespace().level(0), securableObject);- EntityType entityType = MetadataObjectUtil.toEntityType(securableObject.type());- reverseIndexCache.put(securableObjectIdent, entityType, key);- });- }-- // When this role entity is stored as part of a relation cache entry (e.g.,- // METADATA_OBJECT_ROLE_REL keyed by a schema), also add a reverse mapping from the- // role's own identifier to that relation cache key. Without this, invalidating the- // role entity (e.g., after revokePrivilegesFromRole) would not propagate to the- // schema's METADATA_OBJECT_ROLE_REL cache entry, leaving stale data visible via- // listBindingRoleNames().- if (key.relationType() != null) {- reverseIndexCache.put(roleEntity.nameIdentifier(), EntityType.ROLE, key);- }- };-- // Keep policies/tags to objects reverse index for metadata objects, so the key are objects and- // the values are policies/tags.- // Only processes GenericEntity objects without namespace (metadata objects from tag/policy- // queries).- // Entities with namespace (views, tables) are skipped.- public static final ReverseIndexCache.ReverseIndexRule GENERIC_METADATA_OBJECT_REVERSE_RULE =- (entity, key, reverseIndexCache) -> {- GenericEntity genericEntity = (GenericEntity) entity;- EntityType type = entity.type();- if (genericEntity.name() != null- && (genericEntity.namespace() == null || genericEntity.namespace().isEmpty())) {- // Name contains catalog.schema.object format without metalake, so prepend it.- String[] levels = genericEntity.name().split("\\.");- String metalakeName = key.identifier().namespace().levels()[0];- NameIdentifier objectNameIdentifier =- NameIdentifier.of(ArrayUtils.addFirst(levels, metalakeName));- reverseIndexCache.put(objectNameIdentifier, type, key);- }- };-- // Keep objects to policies reverse index for policy objects, so the key are policies and the- // values are objects.- public static final ReverseIndexCache.ReverseIndexRule POLICY_SECURABLE_OBJECT_REVERSE_RULE =- (entity, key, reverseIndexCache) -> {- PolicyEntity policyEntity = (PolicyEntity) entity;- NameIdentifier nameIdentifier =- NameIdentifier.of(policyEntity.namespace(), policyEntity.name());- reverseIndexCache.put(nameIdentifier, Entity.EntityType.POLICY, key);- };-- // Keep objects to tags reverse index for tag objects, so the key are tags and the- // values are objects.- public static final ReverseIndexCache.ReverseIndexRule TAG_SECURABLE_OBJECT_REVERSE_RULE =- (entity, key, reverseIndexCache) -> {- TagEntity tagEntity = (TagEntity) entity;- NameIdentifier nameIdentifier = NameIdentifier.of(tagEntity.namespace(), tagEntity.name());- reverseIndexCache.put(nameIdentifier, Entity.EntityType.TAG, key);- };-}diff --git a/core/src/main/java/org/apache/gravitino/cache/SegmentedLock.java b/core/src/main/java/org/apache/gravitino/cache/SegmentedLock.javaindex c620125900..cfbb8d649e 100644--- a/core/src/main/java/org/apache/gravitino/cache/SegmentedLock.java+++ b/core/src/main/java/org/apache/gravitino/cache/SegmentedLock.java@@ -21,14 +21,9 @@ package org.apache.gravitino.cache;import com.google.common.annotations.VisibleForTesting;import com.google.common.util.concurrent.Striped;-import java.util.Comparator;-import java.util.List;-import java.util.Map;import java.util.concurrent.CountDownLatch;import java.util.concurrent.atomic.AtomicReference;import java.util.concurrent.locks.Lock;-import java.util.stream.Collectors;-import java.util.stream.IntStream;/*** Segmented lock for improved concurrency. Divides locks into segments to reduce contention.@@ -39,12 +34,6 @@ public class SegmentedLock {private final Striped<Lock> stripedLocks;- /**- * Stable index for each stripe lock, built once at construction. Used to sort locks in {@link- * #getDistinctSortedLocks} with a guaranteed total order (no hash-collision ties).- */- private final Map<Lock, Integer> lockIndices;-/** CountDownLatch for global operations - null when no operation is in progress */private final AtomicReference<CountDownLatch> globalOperationLatch = new AtomicReference<>();@@ -62,10 +51,6 @@ public class SegmentedLock {}this.stripedLocks = Striped.lock(numSegments);- this.lockIndices =- IntStream.range(0, this.stripedLocks.size())- .boxed()- .collect(Collectors.toMap(this.stripedLocks::getAt, i -> i));}/**@@ -192,50 +177,6 @@ public class SegmentedLock {}}- /**- * Acquires locks for multiple keys in a consistent order (sorted by stripe index) to avoid- * deadlocks, then executes the action.- *- * @param keys The keys to lock- * @param action Action to run- * @param <E> Exception type- * @throws E Exception from the action- */- public <E extends Exception> void withMultipleKeyLockAndThrow(- List<? extends Object> keys, EntityCache.ThrowingRunnable<E> action) throws E {- waitForGlobalComplete();- List<Lock> sortedLocks = getDistinctSortedLocks(keys);- acquireAllLocks(sortedLocks);- try {- action.run();- } finally {- releaseAllLocks(sortedLocks);- }- }-- /**- * Acquires locks for multiple keys in a consistent order (sorted by stripe index) to avoid- * deadlocks, then executes the action and returns the result.- *- * @param keys The keys to lock- * @param action Action to run- * @param <T> Result type- * @param <E> Exception type- * @return Action result- * @throws E Exception from the action- */- public <T, E extends Exception> T withMultipleKeyLockAndThrow(- List<? extends Object> keys, EntityCache.ThrowingSupplier<T, E> action) throws E {- waitForGlobalComplete();- List<Lock> sortedLocks = getDistinctSortedLocks(keys);- acquireAllLocks(sortedLocks);- try {- return action.get();- } finally {- releaseAllLocks(sortedLocks);- }- }-/** Checks if a global operation is currently in progress. */@VisibleForTestingpublic boolean isClearing() {@@ -277,38 +218,6 @@ public class SegmentedLock {}}- private List<Lock> getDistinctSortedLocks(List<? extends Object> keys) {- return keys.stream()- .map(this::getSegmentLock)- .distinct()- .sorted(Comparator.comparingInt(lockIndices::get))- .collect(Collectors.toList());- }-- private void acquireAllLocks(List<Lock> locks) {- int lockedCount = 0;- try {- for (Lock lock : locks) {- lock.lockInterruptibly();- lockedCount++;- }- } catch (InterruptedException e) {- releaseLocks(locks, lockedCount);- Thread.currentThread().interrupt();- throw new IllegalStateException("Thread was interrupted while acquiring batch lock", e);- }- }-- private static void releaseLocks(List<Lock> locks, int lockedCount) {- for (int i = lockedCount - 1; i >= 0; i--) {- locks.get(i).unlock();- }- }-- private void releaseAllLocks(List<Lock> locks) {- releaseLocks(locks, locks.size());- }-/*** Waits for any ongoing global operation to complete. This method is called by regular operations* to ensure they don't interfere with global operations.diff --git a/core/src/main/java/org/apache/gravitino/cache/SupportsEntityStoreCache.java b/core/src/main/java/org/apache/gravitino/cache/SupportsEntityStoreCache.javaindex ed42084fb0..dce181822e 100644--- a/core/src/main/java/org/apache/gravitino/cache/SupportsEntityStoreCache.java+++ b/core/src/main/java/org/apache/gravitino/cache/SupportsEntityStoreCache.java@@ -62,6 +62,23 @@ public interface SupportsEntityStoreCache {/*** Puts an entity into the cache.*+ * <p><b>Implementations MUST cache only entity types explicitly approved by {@link+ * BaseEntityCache#isCacheable(Entity.EntityType)}.</b> This allowlist is a correctness+ * constraint, not an optimization: a new entity type may contain data that no existing+ * invalidation path covers, so it must remain non-cacheable until its invalidation behavior has+ * been validated. Implementations extending {@link BaseEntityCache} get this check for free.+ *+ * <p>{@code USER}, {@code GROUP} and {@code ROLE} are deliberately excluded because they are+ * materialized with relation-derived data joined in at load time (a role carries its securable+ * objects, and a user/group carries its role names). That embedded data goes stale through+ * mutations on other entities that touch neither this entity's own key nor any of its hierarchy+ * ancestors. Since no invalidation path covers them, caching them serves stale authorization+ * data.+ *+ * <p>Implementations must also invoke {@link #invalidateOnKeyChange(Entity)} for every entity,+ * including non-cacheable ones, since a non-cacheable entity may still invalidate a cacheable+ * one.+ ** @param entity The entity to cache* @param <E> The class of the entity*/diff --git a/core/src/main/java/org/apache/gravitino/cache/SupportsRelationEntityCache.java b/core/src/main/java/org/apache/gravitino/cache/SupportsRelationEntityCache.javadeleted file mode 100644index 51b12113b5..0000000000--- a/core/src/main/java/org/apache/gravitino/cache/SupportsRelationEntityCache.java+++ /dev/null@@ -1,105 +0,0 @@-/*- * 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.gravitino.cache;--import java.util.List;-import java.util.Optional;-import org.apache.gravitino.Entity;-import org.apache.gravitino.HasIdentifier;-import org.apache.gravitino.NameIdentifier;-import org.apache.gravitino.SupportsRelationOperations;--/**- * {@code RelationEntityCache} defines relation-specific caching behavior for entity-to-entity- * relationships.- */-public interface SupportsRelationEntityCache {- /**- * Retrieves a list of related entities from the cache, if present.- *- * @param relType the relation type- * @param nameIdentifier the name identifier of the entity to find related entities for- * @param identType the identifier type of the related entities to find- * @return a list of related entities, or an empty list if none are found- * @param <E> The class of the related entities- */- <E extends Entity & HasIdentifier> Optional<List<E>> getIfPresent(- SupportsRelationOperations.Type relType,- NameIdentifier nameIdentifier,- Entity.EntityType identType);-- /**- * Invalidates the cached relation for the given entity and relation type.- *- * @param ident the name identifier- * @param type the entity type- * @param relType the relation type- * @return true if the cache entry was removed- */- boolean invalidate(- NameIdentifier ident, Entity.EntityType type, SupportsRelationOperations.Type relType);-- /**- * Invalidates the cached relation result for the given key and cleans up the reverse-index- * bookkeeping for that entry, without cascading through the reverse index to other entities.- *- * <p>Unlike {@link #invalidate(NameIdentifier, Entity.EntityType,- * SupportsRelationOperations.Type)}, this does NOT perform a BFS cascade: it leaves other- * entities' caches and the reverse-index mappings of the entities referenced by this relation- * (e.g. all roles bound to one metadata object) intact. It only drops this relation result and- * the reverse-index references that point at this relation key, so the entry is rebuilt on the- * next read. Use it when a relation result is known to be stale and a full cascade would- * incorrectly evict other entities' state.- *- * @param ident the name identifier- * @param type the entity type- * @param relType the relation type- * @return true if the cache entry was removed- */- boolean invalidateRelationEntry(- NameIdentifier ident, Entity.EntityType type, SupportsRelationOperations.Type relType);-- /**- * Checks whether an entity with the given name identifier, type, and relation type is present in- * the cache.- *- * @param ident the name identifier of the entity- * @param type the type of the entity- * @param relType the relation type- * @return {@code true} if the entity is cached; {@code false} otherwise- */- boolean contains(- NameIdentifier ident, Entity.EntityType type, SupportsRelationOperations.Type relType);-- /**- * Puts a list of related entities into the cache.- *- * @param ident The name identifier of the entity to cache the related entities for- * @param type The type of the entity to cache the related entities for- * @param relType The relation type to cache the related entities for- * @param entities The list of related entities to cache- * @param <E> The class of the related entities- */- <E extends Entity & HasIdentifier> void put(- NameIdentifier ident,- Entity.EntityType type,- SupportsRelationOperations.Type relType,- List<E> entities);-}diff --git a/core/src/main/java/org/apache/gravitino/policy/PolicyManager.java b/core/src/main/java/org/apache/gravitino/policy/PolicyManager.javaindex bcb8bb0439..2367a4a2ef 100644--- a/core/src/main/java/org/apache/gravitino/policy/PolicyManager.java+++ b/core/src/main/java/org/apache/gravitino/policy/PolicyManager.java@@ -393,7 +393,9 @@ public class PolicyManager implements PolicyDispatcher {entityType,policyIdent);} catch (NoSuchEntityException e) {- if (e.getMessage().contains("No such entity")) {+ // The store reports a missing policy and a missing metadata object with the same+ // exception type, so the message is the only thing that tells them apart.+ if (isMissingEntity(e, Entity.EntityType.POLICY, policyName)) {throw new NoSuchPolicyException(e, "Policy %s does not exist for metadata object %s", policyName, metadataObject);} else {@@ -519,4 +521,18 @@ public class PolicyManager implements PolicyDispatcher {return builder.build();}++ /**+ * Tells a "the related entity does not exist" failure apart from a "the metadata object does not+ * exist" one. The store signals both with {@link NoSuchEntityException}, so the message built by+ * the relational services is the only discriminator; it is rebuilt here from the same constant+ * and the same lowercasing behavior they use.+ */+ private static boolean isMissingEntity(+ NoSuchEntityException e, Entity.EntityType type, String name) {+ String expected =+ String.format(+ NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE, type.name().toLowerCase(), name);+ return expected.equals(e.getMessage());+ }}diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java b/core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.javaindex b762320169..fb175d5a1e 100644--- a/core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java+++ b/core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java@@ -26,10 +26,8 @@ import com.google.common.collect.ImmutableMap;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;-import java.util.HashMap;import java.util.List;import java.util.Locale;-import java.util.Map;import java.util.Optional;import java.util.concurrent.TimeUnit;import java.util.function.Function;@@ -50,21 +48,14 @@ import org.apache.gravitino.RelationalEntity;import org.apache.gravitino.SupportsExternalIdOperations;import org.apache.gravitino.SupportsIdOperations;import org.apache.gravitino.SupportsRelationOperations;-import org.apache.gravitino.authorization.SecurableObject;import org.apache.gravitino.cache.CacheFactory;import org.apache.gravitino.cache.CachedEntityIdResolver;import org.apache.gravitino.cache.EntityCache;import org.apache.gravitino.cache.EntityCacheKey;-import org.apache.gravitino.cache.EntityCacheRelationKey;import org.apache.gravitino.cache.NoOpsCache;import org.apache.gravitino.exceptions.NoSuchEntityException;-import org.apache.gravitino.meta.GroupEntity;-import org.apache.gravitino.meta.RoleEntity;-import org.apache.gravitino.meta.UserEntity;import org.apache.gravitino.storage.relational.service.EntityIdService;import org.apache.gravitino.utils.Executable;-import org.apache.gravitino.utils.MetadataObjectUtil;-import org.apache.gravitino.utils.NamespaceUtil;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -171,7 +162,6 @@ public class RelationalEntityStorethrows IOException, EntityAlreadyExistsException {backend.insert(e, overwritten);cache.put(e);- invalidateAggregatedRoleRelationCache(e);}@Override@@ -180,7 +170,6 @@ public class RelationalEntityStorethrows IOException, NoSuchEntityException, EntityAlreadyExistsException {E updatedEntity = backend.update(ident, entityType, updater);cache.invalidate(ident, entityType);- invalidateAggregatedRoleRelationCache(updatedEntity);return updatedEntity;}@@ -189,7 +178,7 @@ public class RelationalEntityStoreNameIdentifier ident, Entity.EntityType entityType, Class<E> e)throws NoSuchEntityException, IOException {return cache.withCacheLock(- EntityCacheRelationKey.of(ident, entityType),+ EntityCacheKey.of(ident, entityType),() -> {Optional<E> entityFromCache = cache.getIfPresent(ident, entityType);if (entityFromCache.isPresent()) {@@ -375,22 +364,7 @@ public class RelationalEntityStorepublic <E extends Entity & HasIdentifier> List<E> listEntitiesByRelation(Type relType, NameIdentifier nameIdentifier, Entity.EntityType identType, boolean allFields)throws IOException {- return cache.withCacheLock(- EntityCacheRelationKey.of(nameIdentifier, identType, relType),- () -> {- Optional<List<E>> entities = cache.getIfPresent(relType, nameIdentifier, identType);- if (entities.isPresent()) {- return entities.get();- }-- // Use allFields=true to cache complete entities- List<E> backendEntities =- backend.listEntitiesByRelation(relType, nameIdentifier, identType, true);-- cache.put(nameIdentifier, identType, relType, backendEntities);-- return backendEntities;- });+ return backend.listEntitiesByRelation(relType, nameIdentifier, identType, allFields);}@Override@@ -400,38 +374,7 @@ public class RelationalEntityStoreif (nameIdentifiers == null || nameIdentifiers.isEmpty()) {return new ArrayList<>();}-- List<EntityCacheKey> lockKeys = new ArrayList<>();- for (NameIdentifier id : nameIdentifiers) {- lockKeys.add(EntityCacheRelationKey.of(id, identType, relType));- }-- return cache.withMultipleKeyCacheLock(- lockKeys,- () -> {- List<RelationalEntity<?>> result = new ArrayList<>();- List<NameIdentifier> uncachedIdentifiers = new ArrayList<>();-- for (NameIdentifier nameIdentifier : nameIdentifiers) {- Optional<List<RelationalEntity<?>>> cachedRelations =- getCachedRelations(relType, nameIdentifier, identType);- if (cachedRelations.isPresent()) {- result.addAll(cachedRelations.get());- } else {- uncachedIdentifiers.add(nameIdentifier);- }- }-- if (!uncachedIdentifiers.isEmpty()) {- List<RelationalEntity<?>> backendRelations =- backend.batchListEntitiesByRelation(relType, uncachedIdentifiers, identType);- result.addAll(backendRelations);-- batchPopulateRelationCache(relType, identType, uncachedIdentifiers, backendRelations);- }-- return result;- });+ return backend.batchListEntitiesByRelation(relType, nameIdentifiers, identType);}@Override@@ -441,37 +384,7 @@ public class RelationalEntityStoreEntity.EntityType srcType,NameIdentifier destEntityIdent)throws IOException, NoSuchEntityException {- return cache.withCacheLock(- EntityCacheRelationKey.of(srcIdentifier, srcType, relType),- () -> {- Optional<List<E>> entities = cache.getIfPresent(relType, srcIdentifier, srcType);- if (entities.isPresent()) {- return entities.get().stream()- .filter(e -> e.nameIdentifier().equals(destEntityIdent))- .findFirst()- .orElseThrow(- () ->- new NoSuchEntityException(- "No such entity with ident: %s", destEntityIdent));- }-- // Use allFields=true to cache complete entities- List<E> backendEntities =- backend.listEntitiesByRelation(relType, srcIdentifier, srcType, true);-- E r =- backendEntities.stream()- .filter(e -> e.nameIdentifier().equals(destEntityIdent))- .findFirst()- .orElseThrow(- () ->- new NoSuchEntityException(- "No such entity with ident: %s", destEntityIdent));-- cache.put(srcIdentifier, srcType, relType, backendEntities);-- return r;- });+ return backend.getEntityByRelation(relType, srcIdentifier, srcType, destEntityIdent);}@Override@@ -484,8 +397,14 @@ public class RelationalEntityStoreboolean override)throws IOException {backend.insertRelation(relType, srcIdentifier, srcType, dstIdentifier, dstType, override);- cache.invalidate(srcIdentifier, srcType, relType);- cache.invalidate(dstIdentifier, dstType, relType);+ // Relation query results themselves are not cached, but both endpoints may be cached entities+ // (OWNER_REL, TAG_/POLICY_METADATA_OBJECT_REL and METADATA_OBJECT_ROLE_REL are keyed by+ // catalog/schema/table/... on the source side), so drop their entries conservatively: a+ // relation write can change data materialized into the endpoint entity. Note this is not free —+ // EntityCache#invalidate cascades over the identifier hierarchy, so invalidating a catalog also+ // drops every cached schema and table beneath it.+ cache.invalidate(srcIdentifier, srcType);+ cache.invalidate(dstIdentifier, dstType);}@Override@@ -502,10 +421,12 @@ public class RelationalEntityStore}backend.batchInsertRelations(relType, srcIdentifiers, srcType, dstIdentifier, dstType, override);+ // Invalidate both endpoints for the same reason as insertRelation, including the hierarchy+ // cascade noted there.for (NameIdentifier ident : srcIdentifiers) {- cache.invalidate(ident, srcType, relType);+ cache.invalidate(ident, srcType);}- cache.invalidate(dstIdentifier, dstType, relType);+ cache.invalidate(dstIdentifier, dstType);}@Override@@ -521,8 +442,8 @@ public class RelationalEntityStorerelType,srcEntityIdent,srcEntityType,- toRelationEdgeTargets(relType, srcEntityType, destEntitiesToAdd),- toRelationEdgeTargets(relType, srcEntityType, destEntitiesToRemove)));+ toRelationEdgeTargets(relType, destEntitiesToAdd),+ toRelationEdgeTargets(relType, destEntitiesToRemove)));}@Override@@ -559,11 +480,12 @@ public class RelationalEntityStoretoNameIdentifiers(targetsToRemove));}- Entity.EntityType targetEntityType =- relationUpdateTargetType(update.relationType(), update.sourceEntityType());- cache.invalidate(update.sourceIdentifier(), update.sourceEntityType(), update.relationType());- invalidateRelationTargetCache(update.relationType(), targetEntityType, targetsToAdd);- invalidateRelationTargetCache(update.relationType(), targetEntityType, targetsToRemove);+ // Invalidate after the backend write, not before: invalidating first opens a window where a+ // concurrent read could repopulate the cache with stale pre-commit data.+ Entity.EntityType targetEntityType = relationUpdateTargetType(update.relationType());+ cache.invalidate(update.sourceIdentifier(), update.sourceEntityType());+ invalidateRelationTargetCache(targetEntityType, targetsToAdd);+ invalidateRelationTargetCache(targetEntityType, targetsToRemove);return result;}@@ -582,15 +504,14 @@ public class RelationalEntityStore}private void invalidateRelationTargetCache(- Type relType, Entity.EntityType targetEntityType, RelationEdgeTarget[] relationTargets) {+ Entity.EntityType targetEntityType, RelationEdgeTarget[] relationTargets) {for (RelationEdgeTarget relationTarget : relationTargets) {- cache.invalidate(relationTarget.nameIdentifier(), targetEntityType, relType);+ cache.invalidate(relationTarget.nameIdentifier(), targetEntityType);}}private static void validateRelationTargetTypes(RelationUpdate update) {- Entity.EntityType targetEntityType =- relationUpdateTargetType(update.relationType(), update.sourceEntityType());+ Entity.EntityType targetEntityType = relationUpdateTargetType(update.relationType());validateRelationTargetTypes(update.relationType(), targetEntityType, update.targetsToAdd());validateRelationTargetTypes(update.relationType(), targetEntityType, update.targetsToRemove());}@@ -608,12 +529,12 @@ public class RelationalEntityStore}private static RelationEdgeTarget[] toRelationEdgeTargets(- Type relType, Entity.EntityType srcEntityType, NameIdentifier[] nameIdentifiers) {+ Type relType, NameIdentifier[] nameIdentifiers) {if (nameIdentifiers == null) {return new RelationEdgeTarget[0];}- Entity.EntityType targetEntityType = relationUpdateTargetType(relType, srcEntityType);+ Entity.EntityType targetEntityType = relationUpdateTargetType(relType);return Arrays.stream(nameIdentifiers).map(nameIdentifier -> RelationEdgeTarget.of(nameIdentifier, targetEntityType, null)).toArray(RelationEdgeTarget[]::new);@@ -625,122 +546,15 @@ public class RelationalEntityStore.toArray(NameIdentifier[]::new);}- private static Entity.EntityType relationUpdateTargetType(- Type relType, Entity.EntityType srcEntityType) {+ private static Entity.EntityType relationUpdateTargetType(Type relType) {switch (relType) {case POLICY_METADATA_OBJECT_REL:return Entity.EntityType.POLICY;case TAG_METADATA_OBJECT_REL:return Entity.EntityType.TAG;default:- return srcEntityType;- }- }-- private <E extends Entity & HasIdentifier> Optional<List<RelationalEntity<?>>> getCachedRelations(- SupportsRelationOperations.Type relType,- NameIdentifier nameIdentifier,- Entity.EntityType identType) {- Optional<List<E>> entitiesOpt = cache.getIfPresent(relType, nameIdentifier, identType);- if (entitiesOpt.isPresent()) {- List<RelationalEntity<?>> cachedRelations = new ArrayList<>();- for (E entity : entitiesOpt.get()) {- cachedRelations.add(new RelationalEntity<>(relType, nameIdentifier, identType, entity));- }- return Optional.of(cachedRelations);- }- return Optional.empty();- }-- private <E extends Entity & HasIdentifier> void batchPopulateRelationCache(- SupportsRelationOperations.Type relType,- Entity.EntityType identType,- List<NameIdentifier> uncachedIdentifiers,- List<RelationalEntity<?>> backendRelations) {- Map<NameIdentifier, List<RelationalEntity<?>>> relationsBySource = new HashMap<>();- for (RelationalEntity<?> relation : backendRelations) {- relationsBySource.computeIfAbsent(relation.source(), k -> new ArrayList<>()).add(relation);- }-- for (NameIdentifier sourceId : uncachedIdentifiers) {- List<RelationalEntity<?>> sourceRelations = relationsBySource.get(sourceId);- List<E> entityList = new ArrayList<>();- if (sourceRelations != null) {- for (RelationalEntity<?> rel : sourceRelations) {- @SuppressWarnings("unchecked")- E entity = (E) rel.targetEntity();- entityList.add(entity);- }- }-- cache.put(sourceId, identType, relType, entityList);- }- }-- /**- * Invalidates the relation cache entries keyed by the counterpart of a role-aggregating entity- * after that entity is written, so that reverse lookups reflect the change immediately.- *- * <p>Three entity types aggregate role relations and are mutated through {@code store.update} /- * {@code store.put}, which only invalidate the entity itself:- *- * <ul>- * <li>{@link RoleEntity} via {@code securableObjects} -> {@code METADATA_OBJECT_ROLE_REL},- * invalidated per metadata object (catalog/schema/table/...);- * <li>{@link UserEntity} via {@code roleNames} -> {@code ROLE_USER_REL}, invalidated per role;- * <li>{@link GroupEntity} via {@code roleNames} -> {@code ROLE_GROUP_REL}, invalidated per- * role.- * </ul>- *- * <p>The role-side BFS invalidation ({@code invalidate(roleIdent, ROLE)}) only reaches a- * counterpart's relation entry when the entity had previously been cached against it; a freshly- * granted binding was never cached there, so without this explicit invalidation the stale- * relation result is served until the entry's TTL elapses. Each entry is dropped via {@link- * EntityCache#invalidateRelationEntry} (no BFS cascade), preserving other entities' mappings.- */- private void invalidateAggregatedRoleRelationCache(Entity entity) {- if (entity instanceof RoleEntity) {- RoleEntity roleEntity = (RoleEntity) entity;- List<SecurableObject> securableObjects = roleEntity.securableObjects();- if (securableObjects == null || securableObjects.isEmpty()) {- return;- }- String metalake = roleEntity.namespace().level(0);- for (SecurableObject securableObject : securableObjects) {- cache.invalidateRelationEntry(- MetadataObjectUtil.toEntityIdent(metalake, securableObject),- MetadataObjectUtil.toEntityType(securableObject.type()),- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL);- }- } else if (entity instanceof UserEntity) {- UserEntity userEntity = (UserEntity) entity;- invalidateRoleGranteeRelations(- userEntity.namespace().level(0),- userEntity.roleNames(),- SupportsRelationOperations.Type.ROLE_USER_REL);- } else if (entity instanceof GroupEntity) {- GroupEntity groupEntity = (GroupEntity) entity;- invalidateRoleGranteeRelations(- groupEntity.namespace().level(0),- groupEntity.roleNames(),- SupportsRelationOperations.Type.ROLE_GROUP_REL);- }- }-- /**- * Invalidates the {@code ROLE_USER_REL} / {@code ROLE_GROUP_REL} cache entries keyed by each role- * the grantee (user/group) is aggregated against.- */- private void invalidateRoleGranteeRelations(- String metalake, List<String> roleNames, SupportsRelationOperations.Type relType) {- if (roleNames == null || roleNames.isEmpty()) {- return;- }- for (String roleName : roleNames) {- cache.invalidateRelationEntry(- NameIdentifier.of(NamespaceUtil.ofRole(metalake), roleName),- Entity.EntityType.ROLE,- relType);+ throw new IllegalArgumentException(+ String.format("Doesn't support the relation type %s", relType));}}}diff --git a/core/src/main/java/org/apache/gravitino/tag/TagManager.java b/core/src/main/java/org/apache/gravitino/tag/TagManager.javaindex 49b8e112b4..c334a338c8 100644--- a/core/src/main/java/org/apache/gravitino/tag/TagManager.java+++ b/core/src/main/java/org/apache/gravitino/tag/TagManager.java@@ -296,7 +296,9 @@ public class TagManager implements TagDispatcher {entityType,tagIdent);} catch (NoSuchEntityException e) {- if (e.getMessage().contains("No such entity")) {+ // The store reports a missing tag and a missing metadata object with the same+ // exception type, so the message is the only thing that tells them apart.+ if (isMissingEntity(e, Entity.EntityType.TAG, name)) {throw new NoSuchTagException(e, "Tag %s does not exist for metadata object %s", name, metadataObject);} else {@@ -418,4 +420,18 @@ public class TagManager implements TagDispatcher {.build()).build();}++ /**+ * Tells a "the related entity does not exist" failure apart from a "the metadata object does not+ * exist" one. The store signals both with {@link NoSuchEntityException}, so the message built by+ * the relational services is the only discriminator; it is rebuilt here from the same constant+ * and the same lowercasing behavior they use.+ */+ private static boolean isMissingEntity(+ NoSuchEntityException e, Entity.EntityType type, String name) {+ String expected =+ String.format(+ NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE, type.name().toLowerCase(), name);+ return expected.equals(e.getMessage());+ }}diff --git a/core/src/test/java/org/apache/gravitino/cache/TestBaseEntityCache.java b/core/src/test/java/org/apache/gravitino/cache/TestBaseEntityCache.javanew file mode 100644index 0000000000..89da218569--- /dev/null+++ b/core/src/test/java/org/apache/gravitino/cache/TestBaseEntityCache.java@@ -0,0 +1,180 @@+/*+ * 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.gravitino.cache;++import com.google.common.collect.ImmutableSet;+import java.util.ArrayList;+import java.util.List;+import java.util.Optional;+import java.util.Set;+import org.apache.gravitino.Config;+import org.apache.gravitino.Entity;+import org.apache.gravitino.HasIdentifier;+import org.apache.gravitino.NameIdentifier;+import org.apache.gravitino.Namespace;+import org.apache.gravitino.meta.CatalogEntity;+import org.apache.gravitino.utils.TestUtil;+import org.junit.jupiter.api.Assertions;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;++/**+ * Tests the cacheable entity type allowlist that {@link BaseEntityCache} enforces on behalf of+ * every implementation.+ */+public class TestBaseEntityCache {++ private static final Set<Entity.EntityType> CACHEABLE_TYPES =+ ImmutableSet.of(+ Entity.EntityType.METALAKE,+ Entity.EntityType.CATALOG,+ Entity.EntityType.SCHEMA,+ Entity.EntityType.TABLE,+ Entity.EntityType.VIEW,+ Entity.EntityType.COLUMN,+ Entity.EntityType.FILESET,+ Entity.EntityType.TOPIC,+ Entity.EntityType.TAG,+ Entity.EntityType.MODEL,+ Entity.EntityType.MODEL_VERSION,+ Entity.EntityType.POLICY,+ Entity.EntityType.TABLE_STATISTIC,+ Entity.EntityType.JOB_TEMPLATE,+ Entity.EntityType.JOB,+ Entity.EntityType.AUDIT,+ Entity.EntityType.FUNCTION);++ private RecordingCache cache;++ @BeforeEach+ void setUp() {+ cache = new RecordingCache(new Config() {});+ }++ @Test+ void testIsCacheableAcceptsOnlyApprovedTypes() {+ for (Entity.EntityType type : Entity.EntityType.values()) {+ Assertions.assertEquals(+ CACHEABLE_TYPES.contains(type),+ BaseEntityCache.isCacheable(type),+ "Unexpected cacheability for " + type);+ }+ }++ @Test+ void testIsCacheableRejectsNullType() {+ Assertions.assertThrows(+ IllegalArgumentException.class, () -> BaseEntityCache.isCacheable(null));+ }++ @Test+ void testPutSkipsNonCacheableTypesForSubclasses() {+ cache.put(TestUtil.getTestUserEntity());+ cache.put(TestUtil.getTestGroupEntity());+ cache.put(TestUtil.getTestRoleEntity());++ Assertions.assertTrue(+ cache.cached.isEmpty(), "Subclasses must never see non-cacheable entities in doPut");+ }++ @Test+ void testPutDelegatesCacheableTypesToSubclass() {+ CatalogEntity catalog =+ TestUtil.getTestCatalogEntity(1L, "catalog1", Namespace.of("metalake"), "hive", "cmt");+ cache.put(catalog);++ Assertions.assertEquals(1, cache.cached.size());+ Assertions.assertEquals(catalog, cache.cached.get(0));+ }++ @Test+ void testPutInvalidatesRelatedKeysEvenForNonCacheableTypes() {+ cache.put(TestUtil.getTestUserEntity());++ Assertions.assertEquals(+ 1,+ cache.keyChanges.size(),+ "invalidateOnKeyChange must run for non-cacheable entities as well");+ }++ @Test+ void testPutRejectsNullEntity() {+ Assertions.assertThrows(IllegalArgumentException.class, () -> cache.put(null));+ }++ /** A minimal {@link BaseEntityCache} that records everything handed down to it. */+ private static class RecordingCache extends BaseEntityCache {+ private final List<Entity> cached = new ArrayList<>();+ private final List<Entity> keyChanges = new ArrayList<>();++ RecordingCache(Config config) {+ super(config);+ }++ @Override+ protected <E extends Entity & HasIdentifier> void doPut(E entity) {+ cached.add(entity);+ }++ @Override+ public <E extends Entity & HasIdentifier> void invalidateOnKeyChange(E entity) {+ keyChanges.add(entity);+ }++ @Override+ protected void invalidateExpiredItem(EntityCacheKey key) {}++ @Override+ public <E extends Entity & HasIdentifier> Optional<E> getIfPresent(+ NameIdentifier ident, Entity.EntityType type) {+ return Optional.empty();+ }++ @Override+ public boolean invalidate(NameIdentifier ident, Entity.EntityType type) {+ return false;+ }++ @Override+ public boolean contains(NameIdentifier ident, Entity.EntityType type) {+ return false;+ }++ @Override+ public void clear() {}++ @Override+ public long size() {+ return cached.size();+ }++ @Override+ public <E extends Exception> void withCacheLock(EntityCacheKey key, ThrowingRunnable<E> action)+ throws E {+ action.run();+ }++ @Override+ public <T, E extends Exception> T withCacheLock(+ EntityCacheKey key, ThrowingSupplier<T, E> action) throws E {+ return action.get();+ }+ }+}diff --git a/core/src/test/java/org/apache/gravitino/cache/TestCacheConfig.java b/core/src/test/java/org/apache/gravitino/cache/TestCacheConfig.javaindex f9dbb077be..1f9e422dfb 100644--- a/core/src/test/java/org/apache/gravitino/cache/TestCacheConfig.java+++ b/core/src/test/java/org/apache/gravitino/cache/TestCacheConfig.java@@ -23,7 +23,6 @@ import com.github.benmanes.caffeine.cache.Cache;import com.github.benmanes.caffeine.cache.Caffeine;import com.google.common.collect.ImmutableMap;import java.time.Duration;-import java.util.List;import java.util.stream.IntStream;import org.apache.gravitino.Catalog;import org.apache.gravitino.Config;@@ -62,7 +61,7 @@ public class TestCacheConfig {Caffeine<Object, Object> builder = Caffeine.newBuilder();builder.maximumWeight(5000);builder.weigher(EntityCacheWeigher.getInstance());- Cache<EntityCacheRelationKey, List<Entity>> cache = builder.build();+ Cache<EntityCacheKey, Entity> cache = builder.build();BaseMetalake baseMetalake =BaseMetalake.builder()@@ -72,8 +71,8 @@ public class TestCacheConfig {.withAuditInfo(AuditInfo.EMPTY).build();cache.put(- EntityCacheRelationKey.of(NameIdentifier.of("metalake1"), Entity.EntityType.METALAKE),- List.of(baseMetalake));+ EntityCacheKey.of(NameIdentifier.of("metalake1"), Entity.EntityType.METALAKE),+ baseMetalake);CatalogEntity catalogEntity =CatalogEntity.builder().withNamespace(Namespace.of("metalake1"))@@ -84,9 +83,9 @@ public class TestCacheConfig {.withType(Catalog.Type.RELATIONAL).build();cache.put(- EntityCacheRelationKey.of(+ EntityCacheKey.of(NameIdentifier.of(new String[] {"metalake1", "catalog1"}), Entity.EntityType.CATALOG),- List.of(catalogEntity));+ catalogEntity);SchemaEntity schemaEntity =SchemaEntity.builder()@@ -96,10 +95,10 @@ public class TestCacheConfig {.withId(1000L).build();cache.put(- EntityCacheRelationKey.of(+ EntityCacheKey.of(NameIdentifier.of(new String[] {"metalake1", "catalog1", "schema1"}),Entity.EntityType.SCHEMA),- List.of(schemaEntity));+ schemaEntity);for (int i = 0; i < 5; i++) {String filesetName = "fileset" + i;@@ -113,10 +112,10 @@ public class TestCacheConfig {.withFilesetType(Fileset.Type.MANAGED).build();cache.put(- EntityCacheRelationKey.of(+ EntityCacheKey.of(NameIdentifier.of(new String[] {"metalake1", "catalog1", "schema1", filesetName}),Entity.EntityType.FILESET),- List.of(fileset));+ fileset);}for (int i = 0; i < 10; i++) {@@ -129,7 +128,7 @@ public class TestCacheConfig {.withAuditInfo(AuditInfo.EMPTY).withId((long) (i + 1) * 100_000).build();- cache.put(EntityCacheRelationKey.of(tagNameIdent, Entity.EntityType.TAG), List.of(tagEntity));+ cache.put(EntityCacheKey.of(tagNameIdent, Entity.EntityType.TAG), tagEntity);}// The weight of the cache has exceeded 2000, some entities will be evicted if we continue to@@ -146,10 +145,10 @@ public class TestCacheConfig {.withFilesetType(Fileset.Type.MANAGED).build();cache.put(- EntityCacheRelationKey.of(+ EntityCacheKey.of(NameIdentifier.of("metalake1", "catalog1", "schema1", filesetName),Entity.EntityType.FILESET),- List.of(fileset));+ fileset);}// Access filesets 5-14 twice to increase their frequency to 5 (insert + 4 gets)@@ -157,7 +156,7 @@ public class TestCacheConfig {for (int i = 5; i < 15; i++) {String filesetName = "fileset" + i;cache.getIfPresent(- EntityCacheRelationKey.of(+ EntityCacheKey.of(NameIdentifier.of(new String[] {"metalake1", "catalog1", "schema1", filesetName}),Entity.EntityType.FILESET));}@@ -175,7 +174,7 @@ public class TestCacheConfig {.filter(filesetName ->cache.getIfPresent(- EntityCacheRelationKey.of(+ EntityCacheKey.of(NameIdentifier.of(new String[] {"metalake1", "catalog1", "schema1", filesetName}),Entity.EntityType.FILESET))@@ -188,8 +187,7 @@ public class TestCacheConfig {.mapToObj(i -> NameIdentifierUtil.ofTag("metalake", "tag" + i)).filter(tagNameIdent ->- cache.getIfPresent(- EntityCacheRelationKey.of(tagNameIdent, Entity.EntityType.TAG))+ cache.getIfPresent(EntityCacheKey.of(tagNameIdent, Entity.EntityType.TAG))!= null).count();@@ -217,7 +215,7 @@ public class TestCacheConfig {Caffeine<Object, Object> builder = Caffeine.newBuilder();builder.maximumWeight(5000);builder.weigher(EntityCacheWeigher.getInstance());- Cache<EntityCacheRelationKey, List<Entity>> cache = builder.build();+ Cache<EntityCacheKey, Entity> cache = builder.build();// Insert 3 metalakesfor (int i = 0; i < 3; i++) {@@ -229,8 +227,8 @@ public class TestCacheConfig {.withAuditInfo(AuditInfo.EMPTY).build();cache.put(- EntityCacheRelationKey.of(NameIdentifier.of("metalake" + i), Entity.EntityType.METALAKE),- List.of(baseMetalake));+ EntityCacheKey.of(NameIdentifier.of("metalake" + i), Entity.EntityType.METALAKE),+ baseMetalake);}// Insert 10 catalogs@@ -245,9 +243,8 @@ public class TestCacheConfig {.withType(Catalog.Type.RELATIONAL).build();cache.put(- EntityCacheRelationKey.of(- NameIdentifier.of("metalake1.catalog" + i), Entity.EntityType.CATALOG),- List.of(catalogEntity));+ EntityCacheKey.of(NameIdentifier.of("metalake1.catalog" + i), Entity.EntityType.CATALOG),+ catalogEntity);}// insert 100 schemas@@ -261,24 +258,23 @@ public class TestCacheConfig {.build();cache.put(- EntityCacheRelationKey.of(+ EntityCacheKey.of(NameIdentifier.of("metalake1.catalog1.schema" + i), Entity.EntityType.SCHEMA),- List.of(schemaEntity));+ schemaEntity);}// Three 3 metalakes still in cache.for (int i = 0; i < 3; i++) {Assertions.assertNotNull(cache.getIfPresent(- EntityCacheRelationKey.of(- NameIdentifier.of("metalake" + 1), Entity.EntityType.METALAKE)));+ EntityCacheKey.of(NameIdentifier.of("metalake" + 1), Entity.EntityType.METALAKE)));}// 10 catalogs still in cache.for (int i = 0; i < 10; i++) {Assertions.assertNotNull(cache.getIfPresent(- EntityCacheRelationKey.of(+ EntityCacheKey.of(NameIdentifier.of("metalake1.catalog" + i), Entity.EntityType.CATALOG)));}diff --git a/core/src/test/java/org/apache/gravitino/cache/TestCacheIndex.java b/core/src/test/java/org/apache/gravitino/cache/TestCacheIndex.javaindex cea1f2867a..91d55dc250 100644--- a/core/src/test/java/org/apache/gravitino/cache/TestCacheIndex.java+++ b/core/src/test/java/org/apache/gravitino/cache/TestCacheIndex.java@@ -26,14 +26,13 @@ import com.googlecode.concurrenttrees.radix.node.concrete.DefaultCharArrayNodeFaimport java.util.List;import org.apache.gravitino.Entity;import org.apache.gravitino.NameIdentifier;-import org.apache.gravitino.SupportsRelationOperations;import org.apache.gravitino.utils.NameIdentifierUtil;import org.junit.jupiter.api.Assertions;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;public class TestCacheIndex {- private RadixTree<EntityCacheRelationKey> indexTree;+ private RadixTree<EntityCacheKey> indexTree;private NameIdentifier ident1;private NameIdentifier ident2;@@ -48,18 +47,18 @@ public class TestCacheIndex {private NameIdentifier ident11;private NameIdentifier ident12;- private EntityCacheRelationKey key1;- private EntityCacheRelationKey key2;- private EntityCacheRelationKey key3;- private EntityCacheRelationKey key4;- private EntityCacheRelationKey key5;- private EntityCacheRelationKey key6;- private EntityCacheRelationKey key7;- private EntityCacheRelationKey key8;- private EntityCacheRelationKey key9;- private EntityCacheRelationKey key10;- private EntityCacheRelationKey key11;- private EntityCacheRelationKey key12;+ private EntityCacheKey key1;+ private EntityCacheKey key2;+ private EntityCacheKey key3;+ private EntityCacheKey key4;+ private EntityCacheKey key5;+ private EntityCacheKey key6;+ private EntityCacheKey key7;+ private EntityCacheKey key8;+ private EntityCacheKey key9;+ private EntityCacheKey key10;+ private EntityCacheKey key11;+ private EntityCacheKey key12;@BeforeEachvoid setUp() {@@ -80,24 +79,20 @@ public class TestCacheIndex {ident11 = NameIdentifierUtil.ofUser("metalake2", "user1");ident12 = NameIdentifierUtil.ofUser("metalake2", "user2");- key1 = EntityCacheRelationKey.of(ident1, Entity.EntityType.SCHEMA);- key2 = EntityCacheRelationKey.of(ident2, Entity.EntityType.SCHEMA);- key3 = EntityCacheRelationKey.of(ident3, Entity.EntityType.TABLE);- key4 = EntityCacheRelationKey.of(ident4, Entity.EntityType.TOPIC);- key5 = EntityCacheRelationKey.of(ident5, Entity.EntityType.TABLE);- key6 = EntityCacheRelationKey.of(ident6, Entity.EntityType.TABLE);-- key7 =- EntityCacheRelationKey.of(- ident7, Entity.EntityType.ROLE, SupportsRelationOperations.Type.ROLE_GROUP_REL);- key8 =- EntityCacheRelationKey.of(- ident8, Entity.EntityType.ROLE, SupportsRelationOperations.Type.ROLE_USER_REL);-- key9 = EntityCacheRelationKey.of(ident9, Entity.EntityType.GROUP);- key10 = EntityCacheRelationKey.of(ident10, Entity.EntityType.GROUP);- key11 = EntityCacheRelationKey.of(ident11, Entity.EntityType.USER);- key12 = EntityCacheRelationKey.of(ident12, Entity.EntityType.USER);+ key1 = EntityCacheKey.of(ident1, Entity.EntityType.SCHEMA);+ key2 = EntityCacheKey.of(ident2, Entity.EntityType.SCHEMA);+ key3 = EntityCacheKey.of(ident3, Entity.EntityType.TABLE);+ key4 = EntityCacheKey.of(ident4, Entity.EntityType.TOPIC);+ key5 = EntityCacheKey.of(ident5, Entity.EntityType.TABLE);+ key6 = EntityCacheKey.of(ident6, Entity.EntityType.TABLE);++ key7 = EntityCacheKey.of(ident7, Entity.EntityType.ROLE);+ key8 = EntityCacheKey.of(ident8, Entity.EntityType.ROLE);++ key9 = EntityCacheKey.of(ident9, Entity.EntityType.GROUP);+ key10 = EntityCacheKey.of(ident10, Entity.EntityType.GROUP);+ key11 = EntityCacheKey.of(ident11, Entity.EntityType.USER);+ key12 = EntityCacheKey.of(ident12, Entity.EntityType.USER);addIndex(indexTree, key12);addIndex(indexTree, key11);@@ -120,110 +115,106 @@ public class TestCacheIndex {@Testvoid testGetFromByMetalakePrefix() {- List<EntityCacheRelationKey> storeEntityCacheRelationKeys =+ List<EntityCacheKey> cacheKeys =ImmutableList.copyOf(indexTree.getValuesForKeysStartingWith("metalake1"));- Assertions.assertEquals(8, storeEntityCacheRelationKeys.size());- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key1));- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key3));- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key4));- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key5));- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key6));- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key7));- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key9));- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key10));-- List<EntityCacheRelationKey> storeEntityCacheRelationKeys2 =+ Assertions.assertEquals(8, cacheKeys.size());+ Assertions.assertTrue(cacheKeys.contains(key1));+ Assertions.assertTrue(cacheKeys.contains(key3));+ Assertions.assertTrue(cacheKeys.contains(key4));+ Assertions.assertTrue(cacheKeys.contains(key5));+ Assertions.assertTrue(cacheKeys.contains(key6));+ Assertions.assertTrue(cacheKeys.contains(key7));+ Assertions.assertTrue(cacheKeys.contains(key9));+ Assertions.assertTrue(cacheKeys.contains(key10));++ List<EntityCacheKey> cacheKeys2 =ImmutableList.copyOf(indexTree.getValuesForKeysStartingWith("metalake2"));- Assertions.assertEquals(4, storeEntityCacheRelationKeys2.size());- Assertions.assertTrue(storeEntityCacheRelationKeys2.contains(key2));- Assertions.assertTrue(storeEntityCacheRelationKeys2.contains(key8));- Assertions.assertTrue(storeEntityCacheRelationKeys2.contains(key11));- Assertions.assertTrue(storeEntityCacheRelationKeys2.contains(key12));+ Assertions.assertEquals(4, cacheKeys2.size());+ Assertions.assertTrue(cacheKeys2.contains(key2));+ Assertions.assertTrue(cacheKeys2.contains(key8));+ Assertions.assertTrue(cacheKeys2.contains(key11));+ Assertions.assertTrue(cacheKeys2.contains(key12));}@Testvoid testGetByCatalogPrefix() {- List<EntityCacheRelationKey> storeEntityCacheRelationKeys =+ List<EntityCacheKey> cacheKeys =ImmutableList.copyOf(indexTree.getValuesForKeysStartingWith("metalake1.catalog1"));- Assertions.assertEquals(4, storeEntityCacheRelationKeys.size());- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key1));- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key3));- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key4));- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key6));+ Assertions.assertEquals(4, cacheKeys.size());+ Assertions.assertTrue(cacheKeys.contains(key1));+ Assertions.assertTrue(cacheKeys.contains(key3));+ Assertions.assertTrue(cacheKeys.contains(key4));+ Assertions.assertTrue(cacheKeys.contains(key6));- storeEntityCacheRelationKeys =- ImmutableList.copyOf(indexTree.getValuesForKeysStartingWith("metalake1.catalog2"));- Assertions.assertEquals(1, storeEntityCacheRelationKeys.size());- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key5));+ cacheKeys = ImmutableList.copyOf(indexTree.getValuesForKeysStartingWith("metalake1.catalog2"));+ Assertions.assertEquals(1, cacheKeys.size());+ Assertions.assertTrue(cacheKeys.contains(key5));}@Testvoid testGetBySchemaPrefix() {- List<EntityCacheRelationKey> storeEntityCacheRelationKeys =+ List<EntityCacheKey> cacheKeys =ImmutableList.copyOf(indexTree.getValuesForKeysStartingWith("metalake1.catalog1.schema1"));- Assertions.assertEquals(3, storeEntityCacheRelationKeys.size());- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key1));- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key3));- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key4));+ Assertions.assertEquals(3, cacheKeys.size());+ Assertions.assertTrue(cacheKeys.contains(key1));+ Assertions.assertTrue(cacheKeys.contains(key3));+ Assertions.assertTrue(cacheKeys.contains(key4));- storeEntityCacheRelationKeys =+ cacheKeys =ImmutableList.copyOf(indexTree.getValuesForKeysStartingWith("metalake1.catalog1.schema2"));- Assertions.assertEquals(1, storeEntityCacheRelationKeys.size());- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key6));+ Assertions.assertEquals(1, cacheKeys.size());+ Assertions.assertTrue(cacheKeys.contains(key6));- storeEntityCacheRelationKeys =+ cacheKeys =ImmutableList.copyOf(indexTree.getValuesForKeysStartingWith("metalake1.catalog2.schema1"));- Assertions.assertEquals(1, storeEntityCacheRelationKeys.size());- Assertions.assertTrue(storeEntityCacheRelationKeys.contains(key5));+ Assertions.assertEquals(1, cacheKeys.size());+ Assertions.assertTrue(cacheKeys.contains(key5));}@Testvoid testGetByExactKey() {- EntityCacheRelationKey storeEntityCacheRelationKey =- indexTree.getValueForExactKey(key1.toString());- Assertions.assertEquals(key1, storeEntityCacheRelationKey);+ EntityCacheKey storeEntityCacheKey = indexTree.getValueForExactKey(key1.toString());+ Assertions.assertEquals(key1, storeEntityCacheKey);- storeEntityCacheRelationKey = indexTree.getValueForExactKey(key2.toString());- Assertions.assertEquals(key2, storeEntityCacheRelationKey);+ storeEntityCacheKey = indexTree.getValueForExactKey(key2.toString());+ Assertions.assertEquals(key2, storeEntityCacheKey);- storeEntityCacheRelationKey = indexTree.getValueForExactKey(key3.toString());- Assertions.assertEquals(key3, storeEntityCacheRelationKey);+ storeEntityCacheKey = indexTree.getValueForExactKey(key3.toString());+ Assertions.assertEquals(key3, storeEntityCacheKey);- storeEntityCacheRelationKey = indexTree.getValueForExactKey(key4.toString());- Assertions.assertEquals(key4, storeEntityCacheRelationKey);+ storeEntityCacheKey = indexTree.getValueForExactKey(key4.toString());+ Assertions.assertEquals(key4, storeEntityCacheKey);- storeEntityCacheRelationKey = indexTree.getValueForExactKey(key5.toString());- Assertions.assertEquals(key5, storeEntityCacheRelationKey);+ storeEntityCacheKey = indexTree.getValueForExactKey(key5.toString());+ Assertions.assertEquals(key5, storeEntityCacheKey);- storeEntityCacheRelationKey = indexTree.getValueForExactKey(key6.toString());- Assertions.assertEquals(key6, storeEntityCacheRelationKey);+ storeEntityCacheKey = indexTree.getValueForExactKey(key6.toString());+ Assertions.assertEquals(key6, storeEntityCacheKey);- storeEntityCacheRelationKey = indexTree.getValueForExactKey(key7.toString());- Assertions.assertEquals(key7, storeEntityCacheRelationKey);+ storeEntityCacheKey = indexTree.getValueForExactKey(key7.toString());+ Assertions.assertEquals(key7, storeEntityCacheKey);- storeEntityCacheRelationKey = indexTree.getValueForExactKey(key8.toString());- Assertions.assertEquals(key8, storeEntityCacheRelationKey);+ storeEntityCacheKey = indexTree.getValueForExactKey(key8.toString());+ Assertions.assertEquals(key8, storeEntityCacheKey);- storeEntityCacheRelationKey = indexTree.getValueForExactKey(key9.toString());- Assertions.assertEquals(key9, storeEntityCacheRelationKey);+ storeEntityCacheKey = indexTree.getValueForExactKey(key9.toString());+ Assertions.assertEquals(key9, storeEntityCacheKey);- storeEntityCacheRelationKey = indexTree.getValueForExactKey(key10.toString());- Assertions.assertEquals(key10, storeEntityCacheRelationKey);+ storeEntityCacheKey = indexTree.getValueForExactKey(key10.toString());+ Assertions.assertEquals(key10, storeEntityCacheKey);- storeEntityCacheRelationKey = indexTree.getValueForExactKey(key11.toString());- Assertions.assertEquals(key11, storeEntityCacheRelationKey);+ storeEntityCacheKey = indexTree.getValueForExactKey(key11.toString());+ Assertions.assertEquals(key11, storeEntityCacheKey);- storeEntityCacheRelationKey = indexTree.getValueForExactKey(key12.toString());- Assertions.assertEquals(key12, storeEntityCacheRelationKey);+ storeEntityCacheKey = indexTree.getValueForExactKey(key12.toString());+ Assertions.assertEquals(key12, storeEntityCacheKey);}- private void addIndex(- RadixTree<EntityCacheRelationKey> indexTree,- EntityCacheRelationKey storeEntityCacheRelationKey) {- indexTree.put(storeEntityCacheRelationKey.toString(), storeEntityCacheRelationKey);+ private void addIndex(RadixTree<EntityCacheKey> indexTree, EntityCacheKey storeEntityCacheKey) {+ indexTree.put(storeEntityCacheKey.toString(), storeEntityCacheKey);}}diff --git a/core/src/test/java/org/apache/gravitino/cache/TestCaffeineEntityCacheInvalidation.java b/core/src/test/java/org/apache/gravitino/cache/TestCaffeineEntityCacheInvalidation.javaindex 58c0450852..520eb42c11 100644--- a/core/src/test/java/org/apache/gravitino/cache/TestCaffeineEntityCacheInvalidation.java+++ b/core/src/test/java/org/apache/gravitino/cache/TestCaffeineEntityCacheInvalidation.java@@ -16,361 +16,155 @@* specific language governing permissions and limitations* under the License.*/+package org.apache.gravitino.cache;-import com.google.common.collect.Lists;-import java.time.Instant;-import java.util.List;-import java.util.Optional;+import com.google.common.collect.ImmutableList;+import com.google.common.collect.ImmutableMap;import org.apache.gravitino.Config;import org.apache.gravitino.Entity;-import org.apache.gravitino.NameIdentifier;-import org.apache.gravitino.SupportsRelationOperations;-import org.apache.gravitino.authorization.AuthorizationUtils;-import org.apache.gravitino.authorization.Privileges;-import org.apache.gravitino.authorization.SecurableObject;-import org.apache.gravitino.authorization.SecurableObjects;-import org.apache.gravitino.meta.AuditInfo;+import org.apache.gravitino.Namespace;+import org.apache.gravitino.meta.CatalogEntity;+import org.apache.gravitino.meta.GroupEntity;+import org.apache.gravitino.meta.ModelEntity;+import org.apache.gravitino.meta.ModelVersionEntity;import org.apache.gravitino.meta.RoleEntity;-import org.apache.gravitino.storage.RandomIdGenerator;-import org.apache.gravitino.utils.NameIdentifierUtil;+import org.apache.gravitino.meta.SchemaEntity;+import org.apache.gravitino.meta.TableEntity;+import org.apache.gravitino.meta.UserEntity;+import org.apache.gravitino.utils.TestUtil;import org.junit.jupiter.api.Assertions;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;-/**- * Unit tests for {@link CaffeineEntityCache} invalidation logic, specifically covering the bug- * where invalidating a role entity did not propagate to the METADATA_OBJECT_ROLE_REL relation cache- * entries for the role's securable objects.- *- * <p>See GitHub issue #11297: {@code listBindingRoleNames} returns stale role after {@code- * revokePrivilegesFromRole} removes the last privilege.- */+/** Tests hierarchical invalidation and cacheability rules of {@link CaffeineEntityCache}. */public class TestCaffeineEntityCacheInvalidation {private CaffeineEntityCache cache;- private AuditInfo auditInfo;@BeforeEachvoid setUp() {- Config config = new Config(false) {};- cache = new CaffeineEntityCache(config);- auditInfo = AuditInfo.builder().withCreator("test").withCreateTime(Instant.now()).build();+ cache = new CaffeineEntityCache(new Config() {});}- /**- * Builds a RoleEntity that has the given schema as its securable object.- *- * @param metalake the metalake name- * @param roleName the role name- * @param catalogName the catalog the schema belongs to- * @param schemaName the schema name used as the role's securable object- * @return the constructed RoleEntity- */- private RoleEntity buildRoleWithSchemaObject(- String metalake, String roleName, String catalogName, String schemaName) {- SecurableObject catalogObject = SecurableObjects.ofCatalog(catalogName, Lists.newArrayList());- SecurableObject schemaObject =- SecurableObjects.ofSchema(- catalogObject, schemaName, Lists.newArrayList(Privileges.UseSchema.allow()));- return RoleEntity.builder()- .withId(RandomIdGenerator.INSTANCE.nextId())- .withName(roleName)- .withNamespace(AuthorizationUtils.ofRoleNamespace(metalake))- .withProperties(null)- .withAuditInfo(auditInfo)- .withSecurableObjects(Lists.newArrayList(schemaObject))- .build();+ @Test+ void testInvalidateCatalogCascadesToChildren() {+ CatalogEntity catalog =+ TestUtil.getTestCatalogEntity(1L, "catalog1", Namespace.of("metalake"), "hive", "cmt");+ SchemaEntity schema =+ TestUtil.getTestSchemaEntity(2L, "schema1", Namespace.of("metalake", "catalog1"), "cmt");+ TableEntity table =+ TestUtil.getTestTableEntity(3L, "table1", Namespace.of("metalake", "catalog1", "schema1"));++ cache.put(catalog);+ cache.put(schema);+ cache.put(table);+ Assertions.assertEquals(3, cache.size());++ cache.invalidate(catalog.nameIdentifier(), Entity.EntityType.CATALOG);++ Assertions.assertFalse(cache.contains(catalog.nameIdentifier(), Entity.EntityType.CATALOG));+ Assertions.assertFalse(cache.contains(schema.nameIdentifier(), Entity.EntityType.SCHEMA));+ Assertions.assertFalse(cache.contains(table.nameIdentifier(), Entity.EntityType.TABLE));+ Assertions.assertEquals(0, cache.size());}- /**- * Core scenario reproducing GitHub issue #11297: after caching the METADATA_OBJECT_ROLE_REL- * relation for a schema, invalidating the role entity must also remove the stale relation cache.- *- * <p>Flow: grant → listBindingRoleNames (caches relation) → revoke (invalidates role) →- * listBindingRoleNames must miss cache and re-query.- */@Test- void testRoleInvalidationPropagatesToMetadataObjectRoleRelCache() {- String metalake = "metalake";- String catalogName = "catalog";- String schemaName = "test_schema";- String roleName = "test_role";-- RoleEntity role = buildRoleWithSchemaObject(metalake, roleName, catalogName, schemaName);- NameIdentifier schemaIdent = NameIdentifier.of(metalake, catalogName, schemaName);- NameIdentifier roleIdent = NameIdentifierUtil.ofRole(metalake, roleName);-- // Simulate caching the METADATA_OBJECT_ROLE_REL result (e.g., after listBindingRoleNames)- cache.put(- schemaIdent,- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL,- Lists.newArrayList(role));-- // Verify the relation cache is populated- Optional<List<RoleEntity>> cached =- cache.getIfPresent(- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL,- schemaIdent,- Entity.EntityType.SCHEMA);- Assertions.assertTrue(cached.isPresent(), "Relation cache should be populated after put");- Assertions.assertEquals(1, cached.get().size());- Assertions.assertEquals(roleName, cached.get().get(0).name());-- // Simulate revokePrivilegesFromRole: invalidate the role entity- cache.invalidate(roleIdent, Entity.EntityType.ROLE);-- // After invalidation the METADATA_OBJECT_ROLE_REL cache for the schema must be gone- Optional<List<RoleEntity>> afterRevoke =- cache.getIfPresent(- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL,- schemaIdent,- Entity.EntityType.SCHEMA);- Assertions.assertFalse(- afterRevoke.isPresent(),- "METADATA_OBJECT_ROLE_REL cache must be invalidated after revoking the role");+ void testInvalidateSchemaKeepsParentCatalog() {+ CatalogEntity catalog =+ TestUtil.getTestCatalogEntity(1L, "catalog1", Namespace.of("metalake"), "hive", "cmt");+ SchemaEntity schema =+ TestUtil.getTestSchemaEntity(2L, "schema1", Namespace.of("metalake", "catalog1"), "cmt");+ TableEntity table =+ TestUtil.getTestTableEntity(3L, "table1", Namespace.of("metalake", "catalog1", "schema1"));++ cache.put(catalog);+ cache.put(schema);+ cache.put(table);++ cache.invalidate(schema.nameIdentifier(), Entity.EntityType.SCHEMA);++ Assertions.assertTrue(cache.contains(catalog.nameIdentifier(), Entity.EntityType.CATALOG));+ Assertions.assertFalse(cache.contains(schema.nameIdentifier(), Entity.EntityType.SCHEMA));+ Assertions.assertFalse(cache.contains(table.nameIdentifier(), Entity.EntityType.TABLE));}- /**- * When the role entity was also separately cached (via a previous get), invalidating it must- * still propagate to the METADATA_OBJECT_ROLE_REL relation cache.- */@Test- void testRoleInvalidationWithSeparatelyCachedRoleEntity() {- String metalake = "metalake";- String catalogName = "catalog";- String schemaName = "schema1";- String roleName = "role1";+ void testInvalidateDoesNotEvictSiblingsWithSharedNamePrefix() {+ CatalogEntity catalog1 =+ TestUtil.getTestCatalogEntity(1L, "catalog1", Namespace.of("metalake"), "hive", "cmt");+ CatalogEntity catalog10 =+ TestUtil.getTestCatalogEntity(2L, "catalog10", Namespace.of("metalake"), "hive", "cmt");- RoleEntity role = buildRoleWithSchemaObject(metalake, roleName, catalogName, schemaName);- NameIdentifier schemaIdent = NameIdentifier.of(metalake, catalogName, schemaName);- NameIdentifier roleIdent = NameIdentifierUtil.ofRole(metalake, roleName);+ cache.put(catalog1);+ cache.put(catalog10);- // Simulate separate entity get (e.g., loadRole)- cache.put(role);+ cache.invalidate(catalog1.nameIdentifier(), Entity.EntityType.CATALOG);- // Simulate relation cache populated after listBindingRoleNames- cache.put(- schemaIdent,- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL,- Lists.newArrayList(role));-- // Both caches are present- Assertions.assertTrue(cache.contains(roleIdent, Entity.EntityType.ROLE));- Assertions.assertTrue(- cache.contains(- schemaIdent,- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL));-- // Invalidate role (simulates revokePrivilegesFromRole)- cache.invalidate(roleIdent, Entity.EntityType.ROLE);-- // Both the role entity cache and the relation cache must be gone- Assertions.assertFalse(- cache.contains(roleIdent, Entity.EntityType.ROLE),- "Role entity cache must be gone after invalidation");- Assertions.assertFalse(- cache.contains(- schemaIdent,- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL),- "METADATA_OBJECT_ROLE_REL cache must be invalidated after revoking the role");+ Assertions.assertFalse(cache.contains(catalog1.nameIdentifier(), Entity.EntityType.CATALOG));+ Assertions.assertTrue(cache.contains(catalog10.nameIdentifier(), Entity.EntityType.CATALOG));}- /**- * When multiple roles share the same schema as a securable object, invalidating one role must- * invalidate the METADATA_OBJECT_ROLE_REL cache for that schema (since the full list is stale).- *- * <p>Note: The existing BFS propagation logic also cascades from the cleared relation cache back- * to other role entities via the schema's reverse index (the same behaviour that clears stale- * role-entity caches on schema rename). Clearing role2's entity cache is a pre-existing- * performance trade-off, not a correctness bug: role2 will be re-fetched fresh from the DB on the- * next access.- */@Test- void testMultipleRolesInvalidationForSameSchema() {- String metalake = "metalake";- String catalogName = "catalog";- String schemaName = "schema";-- RoleEntity role1 = buildRoleWithSchemaObject(metalake, "role1", catalogName, schemaName);- RoleEntity role2 = buildRoleWithSchemaObject(metalake, "role2", catalogName, schemaName);+ void testRoleUserGroupAreNotCached() {+ RoleEntity role = TestUtil.getTestRoleEntity();+ UserEntity user = TestUtil.getTestUserEntity();+ GroupEntity group = TestUtil.getTestGroupEntity();- NameIdentifier schemaIdent = NameIdentifier.of(metalake, catalogName, schemaName);- NameIdentifier role1Ident = NameIdentifierUtil.ofRole(metalake, "role1");-- // Cache both role entities separately and the relation list- cache.put(role1);- cache.put(role2);- cache.put(- schemaIdent,- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL,- Lists.newArrayList(role1, role2));+ cache.put(role);+ cache.put(user);+ cache.put(group);+ Assertions.assertEquals(0, cache.size());+ Assertions.assertFalse(cache.contains(role.nameIdentifier(), Entity.EntityType.ROLE));+ Assertions.assertFalse(cache.contains(user.nameIdentifier(), Entity.EntityType.USER));+ Assertions.assertFalse(cache.contains(group.nameIdentifier(), Entity.EntityType.GROUP));Assertions.assertTrue(- cache.contains(- schemaIdent,- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL));-- // Revoke role1- cache.invalidate(role1Ident, Entity.EntityType.ROLE);-- // The METADATA_OBJECT_ROLE_REL cache must be gone — this is the core correctness fix- Assertions.assertFalse(- cache.contains(- schemaIdent,- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL),- "Relation cache must be cleared when any of its roles is revoked");+ cache.getIfPresent(role.nameIdentifier(), Entity.EntityType.ROLE).isEmpty());}- /**- * When a role has securable objects on multiple schemas, invalidating the role must invalidate- * the METADATA_OBJECT_ROLE_REL caches for ALL schemas.- */@Test- void testRoleWithMultipleSchemasInvalidatesAllRelationCaches() {- String metalake = "metalake";- String catalogName = "catalog";- String roleName = "multi_schema_role";-- SecurableObject cat = SecurableObjects.ofCatalog(catalogName, Lists.newArrayList());- SecurableObject schema1Obj =- SecurableObjects.ofSchema(cat, "schema1", Lists.newArrayList(Privileges.UseSchema.allow()));- SecurableObject schema2Obj =- SecurableObjects.ofSchema(cat, "schema2", Lists.newArrayList(Privileges.UseSchema.allow()));-- RoleEntity role =- RoleEntity.builder()- .withId(RandomIdGenerator.INSTANCE.nextId())- .withName(roleName)- .withNamespace(AuthorizationUtils.ofRoleNamespace(metalake))- .withProperties(null)- .withAuditInfo(auditInfo)- .withSecurableObjects(Lists.newArrayList(schema1Obj, schema2Obj))- .build();-- NameIdentifier schema1Ident = NameIdentifier.of(metalake, catalogName, "schema1");- NameIdentifier schema2Ident = NameIdentifier.of(metalake, catalogName, "schema2");- NameIdentifier roleIdent = NameIdentifierUtil.ofRole(metalake, roleName);-- // Cache METADATA_OBJECT_ROLE_REL for both schemas- cache.put(- schema1Ident,- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL,- Lists.newArrayList(role));- cache.put(- schema2Ident,- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL,- Lists.newArrayList(role));-- Assertions.assertTrue(- cache.contains(- schema1Ident,- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL));- Assertions.assertTrue(- cache.contains(- schema2Ident,- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL));-- // Revoke role (removes its last privilege on all schemas)- cache.invalidate(roleIdent, Entity.EntityType.ROLE);-- Assertions.assertFalse(- cache.contains(- schema1Ident,- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL),- "schema1 METADATA_OBJECT_ROLE_REL cache must be cleared");- Assertions.assertFalse(- cache.contains(- schema2Ident,- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL),- "schema2 METADATA_OBJECT_ROLE_REL cache must be cleared");+ void testPutModelVersionInvalidatesModel() {+ ModelEntity model = TestUtil.getTestModelEntity(1L, "model1", Namespace.of("m1", "c1", "s1"));+ cache.put(model);+ Assertions.assertTrue(cache.contains(model.nameIdentifier(), Entity.EntityType.MODEL));++ ModelVersionEntity version =+ TestUtil.getTestModelVersionEntity(+ model.nameIdentifier(),+ 1,+ ImmutableMap.of("unknown", "uri"),+ ImmutableMap.of(),+ "cmt",+ ImmutableList.of());+ cache.put(version);++ Assertions.assertFalse(cache.contains(model.nameIdentifier(), Entity.EntityType.MODEL));}- /**- * When the METADATA_OBJECT_ROLE_REL cache already contains the role (e.g., role was granted- * earlier and the relation was cached), granting an additional privilege (invalidates the role)- * must also clear the stale relation cache. This is symmetric with the revoke path.- */@Test- void testGrantPathInvalidatesRelationCacheWhenRoleWasPreviouslyCached() {- String metalake = "metalake";- String catalogName = "catalog";- String schemaName = "schema_grant";- String roleName = "grant_role";-- RoleEntity role = buildRoleWithSchemaObject(metalake, roleName, catalogName, schemaName);- NameIdentifier schemaIdent = NameIdentifier.of(metalake, catalogName, schemaName);- NameIdentifier roleIdent = NameIdentifierUtil.ofRole(metalake, roleName);-- // Simulate a prior listBindingRoleNames that already included the role- cache.put(- schemaIdent,- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL,- Lists.newArrayList(role));-- // Simulate grantPrivilegesToRole adding another privilege → role entity is updated and- // invalidated- cache.invalidate(roleIdent, Entity.EntityType.ROLE);-- // Relation cache should be gone, forcing a fresh DB query on next listBindingRoleNames- Optional<List<RoleEntity>> afterGrant =- cache.getIfPresent(- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL,- schemaIdent,- Entity.EntityType.SCHEMA);- Assertions.assertFalse(- afterGrant.isPresent(),- "METADATA_OBJECT_ROLE_REL cache must be cleared after grant so next read returns fresh data");+ void testGetIfPresentReturnsCachedEntity() {+ CatalogEntity catalog =+ TestUtil.getTestCatalogEntity(1L, "catalog1", Namespace.of("metalake"), "hive", "cmt");+ cache.put(catalog);++ Assertions.assertEquals(+ catalog,+ cache.getIfPresent(catalog.nameIdentifier(), Entity.EntityType.CATALOG).orElse(null));+ Assertions.assertTrue(+ cache.getIfPresent(catalog.nameIdentifier(), Entity.EntityType.SCHEMA).isEmpty());}- /**- * Verify the reverse index is cleaned up correctly after invalidation, preventing memory leaks.- */@Test- void testReverseIndexCleanupAfterRoleInvalidation() {- String metalake = "metalake";- String catalogName = "catalog";- String schemaName = "schema_cleanup";- String roleName = "cleanup_role";-- RoleEntity role = buildRoleWithSchemaObject(metalake, roleName, catalogName, schemaName);- NameIdentifier schemaIdent = NameIdentifier.of(metalake, catalogName, schemaName);- NameIdentifier roleIdent = NameIdentifierUtil.ofRole(metalake, roleName);+ void testClearResetsSizeAndIndex() {+ CatalogEntity catalog =+ TestUtil.getTestCatalogEntity(1L, "catalog1", Namespace.of("metalake"), "hive", "cmt");+ cache.put(catalog);+ Assertions.assertEquals(1, cache.size());- cache.put(role);- cache.put(- schemaIdent,- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL,- Lists.newArrayList(role));-- long sizeBeforeInvalidation = cache.size();- cache.invalidate(roleIdent, Entity.EntityType.ROLE);- long sizeAfterInvalidation = cache.size();-- // Both the role entity entry and the relation entry should be removed- Assertions.assertTrue(- sizeAfterInvalidation < sizeBeforeInvalidation,- "Cache size must decrease after invalidating role and its related relation entries");+ cache.clear();- // Reverse index for role should be empty- ReverseIndexCache reverseIndex = cache.getReverseIndex();- List<EntityCacheKey> roleReverseKeys = reverseIndex.get(roleIdent, Entity.EntityType.ROLE);- Assertions.assertNull(- roleReverseKeys, "Reverse index for role should be empty after invalidation");+ Assertions.assertEquals(0, cache.size());+ Assertions.assertFalse(cache.contains(catalog.nameIdentifier(), Entity.EntityType.CATALOG));}}diff --git a/core/src/test/java/org/apache/gravitino/cache/TestEntityCacheKey.java b/core/src/test/java/org/apache/gravitino/cache/TestEntityCacheKey.javaindex 6fbfd08f24..903dcb5819 100644--- a/core/src/test/java/org/apache/gravitino/cache/TestEntityCacheKey.java+++ b/core/src/test/java/org/apache/gravitino/cache/TestEntityCacheKey.java@@ -21,7 +21,6 @@ package org.apache.gravitino.cache;import org.apache.gravitino.Entity;import org.apache.gravitino.NameIdentifier;-import org.apache.gravitino.SupportsRelationOperations;import org.apache.gravitino.utils.NameIdentifierUtil;import org.junit.jupiter.api.Assertions;import org.junit.jupiter.api.Test;@@ -29,44 +28,27 @@ import org.junit.jupiter.api.Test;public class TestEntityCacheKey {@Test- void testCreateRelationEntityCacheRelationKeyUsingStaticMethod() {+ void testCreateEntityCacheKeyUsingStaticMethod() {NameIdentifier ident = NameIdentifierUtil.ofRole("metalake", "role1");- // test Relation Entity- EntityCacheRelationKey key =- EntityCacheRelationKey.of(- ident, Entity.EntityType.ROLE, SupportsRelationOperations.Type.ROLE_GROUP_REL);- Assertions.assertEquals("metalake.system.role.role1:ROLE:ROLE_GROUP_REL", key.toString());+ EntityCacheKey key = EntityCacheKey.of(ident, Entity.EntityType.ROLE);+ Assertions.assertEquals("metalake.system.role.role1:ROLE", key.toString());Assertions.assertEquals(NameIdentifier.of("metalake", "system", "role", "role1"), key.identifier());Assertions.assertEquals(Entity.EntityType.ROLE, key.entityType());- Assertions.assertEquals(SupportsRelationOperations.Type.ROLE_GROUP_REL, key.relationType());-- // test Store Entity- EntityCacheRelationKey key2 = EntityCacheRelationKey.of(ident, Entity.EntityType.ROLE, null);- Assertions.assertEquals("metalake.system.role.role1:ROLE", key2.toString());- Assertions.assertEquals(- NameIdentifier.of("metalake", "system", "role", "role1"), key2.identifier());- Assertions.assertEquals(Entity.EntityType.ROLE, key2.entityType());- Assertions.assertNull(key2.relationType());}@Test- void testCreateRelationEntityCacheRelationKeyWithNullArguments() {+ void testCreateEntityCacheKeyWithNullArguments() {NameIdentifier ident = NameIdentifierUtil.ofRole("metalake", "role1");Assertions.assertThrows(IllegalArgumentException.class,() -> {- EntityCacheRelationKey.of(- null, Entity.EntityType.ROLE, SupportsRelationOperations.Type.ROLE_GROUP_REL);+ EntityCacheKey.of(null, Entity.EntityType.ROLE);});Assertions.assertThrows(IllegalArgumentException.class,() -> {- EntityCacheRelationKey.of(ident, null, SupportsRelationOperations.Type.ROLE_GROUP_REL);- });- Assertions.assertDoesNotThrow(- () -> {- EntityCacheRelationKey.of(ident, Entity.EntityType.ROLE, null);+ EntityCacheKey.of(ident, null);});}@@ -75,37 +57,20 @@ public class TestEntityCacheKey {NameIdentifier ident1 = NameIdentifier.of("ns", "db", "tbl");Entity.EntityType type = Entity.EntityType.TABLE;- EntityCacheRelationKey key1 = EntityCacheRelationKey.of(ident1, type);- EntityCacheRelationKey key2 =- EntityCacheRelationKey.of(NameIdentifier.of("ns", "db", "tbl"), type);+ EntityCacheKey key1 = EntityCacheKey.of(ident1, type);+ EntityCacheKey key2 = EntityCacheKey.of(NameIdentifier.of("ns", "db", "tbl"), type);Assertions.assertEquals(key1, key2, "Keys with same ident and type should be equal");Assertions.assertEquals(key1.hashCode(), key2.hashCode(), "Hash codes must match for equal objects");}- @Test- public void testEqualsAndHashCodeWithRelationType() {- NameIdentifier ident = NameIdentifier.of("ns", "db", "tbl");- Entity.EntityType type = Entity.EntityType.TABLE;- SupportsRelationOperations.Type relType = SupportsRelationOperations.Type.OWNER_REL;-- EntityCacheRelationKey key1 = EntityCacheRelationKey.of(ident, type, relType);- EntityCacheRelationKey key2 =- EntityCacheRelationKey.of(NameIdentifier.of("ns", "db", "tbl"), type, relType);-- Assertions.assertEquals(- key1, key2, "Keys with same ident, type, and relationType should be equal");- Assertions.assertEquals(- key1.hashCode(), key2.hashCode(), "Hash codes must match for equal objects");- }-@Testpublic void testInequalityWithDifferentIdentifier() {- EntityCacheRelationKey key1 =- EntityCacheRelationKey.of(NameIdentifier.of("ns", "db", "tbl1"), Entity.EntityType.TABLE);- EntityCacheRelationKey key2 =- EntityCacheRelationKey.of(NameIdentifier.of("ns", "db", "tbl2"), Entity.EntityType.TABLE);+ EntityCacheKey key1 =+ EntityCacheKey.of(NameIdentifier.of("ns", "db", "tbl1"), Entity.EntityType.TABLE);+ EntityCacheKey key2 =+ EntityCacheKey.of(NameIdentifier.of("ns", "db", "tbl2"), Entity.EntityType.TABLE);Assertions.assertNotEquals(key1, key2, "Keys with different identifiers should not be equal");}@@ -113,44 +78,19 @@ public class TestEntityCacheKey {@Testpublic void testInequalityWithDifferentEntityType() {NameIdentifier ident = NameIdentifier.of("ns", "db", "obj");- EntityCacheRelationKey key1 = EntityCacheRelationKey.of(ident, Entity.EntityType.TABLE);- EntityCacheRelationKey key2 = EntityCacheRelationKey.of(ident, Entity.EntityType.FILESET);+ EntityCacheKey key1 = EntityCacheKey.of(ident, Entity.EntityType.TABLE);+ EntityCacheKey key2 = EntityCacheKey.of(ident, Entity.EntityType.FILESET);Assertions.assertNotEquals(key1, key2, "Keys with different entity types should not be equal");}@Test- public void testInequalityWithDifferentRelationType() {- NameIdentifier ident = NameIdentifier.of("ns", "db", "obj");- Entity.EntityType type = Entity.EntityType.TABLE;-- EntityCacheRelationKey key1 =- EntityCacheRelationKey.of(ident, type, SupportsRelationOperations.Type.OWNER_REL);- EntityCacheRelationKey key2 =- EntityCacheRelationKey.of(ident, type, SupportsRelationOperations.Type.ROLE_USER_REL);-- Assertions.assertNotEquals(- key1, key2, "Keys with different relation types should not be equal");- }-- @Test- public void testToStringWithoutRelationType() {+ public void testToString() {NameIdentifier ident = NameIdentifierUtil.ofUser("metalake", "user1");Entity.EntityType type = Entity.EntityType.USER;- EntityCacheRelationKey key = EntityCacheRelationKey.of(ident, type);+ EntityCacheKey key = EntityCacheKey.of(ident, type);Assertions.assertEquals("metalake.system.user.user1:USER", key.toString());}-- @Test- public void testToStringWithRelationType() {- NameIdentifier ident = NameIdentifierUtil.ofUser("metalake", "user1");- Entity.EntityType type = Entity.EntityType.USER;- SupportsRelationOperations.Type relationType = SupportsRelationOperations.Type.ROLE_USER_REL;-- EntityCacheRelationKey key = EntityCacheRelationKey.of(ident, type, relationType);-- Assertions.assertEquals("metalake.system.user.user1:USER:ROLE_USER_REL", key.toString());- }}diff --git a/core/src/test/java/org/apache/gravitino/storage/TestEntityStorageRelationCache.java b/core/src/test/java/org/apache/gravitino/storage/TestEntityStorageRelationCache.javaindex 5bbec6c5e3..396e24b861 100644--- a/core/src/test/java/org/apache/gravitino/storage/TestEntityStorageRelationCache.java+++ b/core/src/test/java/org/apache/gravitino/storage/TestEntityStorageRelationCache.java@@ -19,7 +19,6 @@package org.apache.gravitino.storage;-import com.github.benmanes.caffeine.cache.Cache;import com.google.common.collect.ImmutableMap;import com.google.common.collect.Lists;import java.time.Instant;@@ -41,10 +40,6 @@ import org.apache.gravitino.authorization.Privileges;import org.apache.gravitino.authorization.Role;import org.apache.gravitino.authorization.SecurableObject;import org.apache.gravitino.authorization.SecurableObjects;-import org.apache.gravitino.cache.CaffeineEntityCache;-import org.apache.gravitino.cache.EntityCacheKey;-import org.apache.gravitino.cache.EntityCacheRelationKey;-import org.apache.gravitino.cache.ReverseIndexCache;import org.apache.gravitino.function.FunctionDefinition;import org.apache.gravitino.function.FunctionDefinitions;import org.apache.gravitino.function.FunctionImpl;@@ -71,7 +66,6 @@ import org.apache.gravitino.rel.Column;import org.apache.gravitino.rel.Representation;import org.apache.gravitino.rel.SQLRepresentation;import org.apache.gravitino.rel.types.Types;-import org.apache.gravitino.storage.relational.RelationalEntityStore;import org.apache.gravitino.utils.NameIdentifierUtil;import org.apache.gravitino.utils.NamespaceUtil;import org.junit.jupiter.api.Assertions;@@ -211,36 +205,16 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {store.delete(readRole.nameIdentifier(), Entity.EntityType.ROLE);- ReverseIndexCache reverseIndexCache =- ((CaffeineEntityCache) ((RelationalEntityStore) store).getCache()).getReverseIndex();- List<EntityCacheKey> reverseIndexValue =- reverseIndexCache.get(- NameIdentifier.of("metalake", "newCatalogName", "schema", "fileset"),- Entity.EntityType.FILESET);- Assertions.assertEquals(1, reverseIndexValue.size());- Assertions.assertEquals(writeRole.nameIdentifier(), reverseIndexValue.get(0).identifier());+ Role reloadedWriteRole =+ store.get(writeRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);+ Assertions.assertEquals(1, reloadedWriteRole.securableObjects().size());store.put(readRole, true);store.get(readRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);- reverseIndexValue =- reverseIndexCache.get(- NameIdentifier.of("metalake", "newCatalogName", "schema", "fileset"),- Entity.EntityType.FILESET);- Assertions.assertEquals(2, reverseIndexValue.size());- List<NameIdentifier> ids =- reverseIndexValue.stream().map(EntityCacheKey::identifier).collect(Collectors.toList());- Assertions.assertTrue(ids.contains(readRole.nameIdentifier()));- Assertions.assertTrue(ids.contains(writeRole.nameIdentifier()));store.delete(readRole.nameIdentifier(), Entity.EntityType.ROLE);store.delete(writeRole.nameIdentifier(), Entity.EntityType.ROLE);- reverseIndexValue =- reverseIndexCache.get(- NameIdentifier.of("metalake", "newCatalogName", "schema", "fileset"),- Entity.EntityType.FILESET);- Assertions.assertNull(reverseIndexValue);-store.put(readRole, true);store.put(writeRole, true);store.get(readRole.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);@@ -374,23 +348,6 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {Assertions.assertEquals(1, tags.size());Assertions.assertEquals(tag1, tags.get(0));- RelationalEntityStore relationalEntityStore = (RelationalEntityStore) store;- CaffeineEntityCache caffeineEntityCache =- (CaffeineEntityCache) relationalEntityStore.getCache();- Cache<EntityCacheRelationKey, List<Entity>> cache = caffeineEntityCache.getCacheData();-- List<Entity> cachedTags =- cache.get(- EntityCacheRelationKey.of(- catalog.nameIdentifier(),- Entity.EntityType.CATALOG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),- k -> null);-- Assertions.assertNotNull(cachedTags);- Assertions.assertEquals(1, cachedTags.size());- Assertions.assertEquals(tag1, cachedTags.get(0));-List<GenericEntity> genericEntities =relationOperations.listEntitiesByRelation(SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,@@ -417,23 +374,6 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {CatalogEntity.class,Entity.EntityType.CATALOG,e -> updatedCatalog);- List<Entity> cachedTagsAfterCatalogUpdate =- cache.get(- EntityCacheRelationKey.of(- catalog.nameIdentifier(),- Entity.EntityType.CATALOG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),- k -> null);- Assertions.assertNull(cachedTagsAfterCatalogUpdate);-- List<Entity> cachedTagsByTagAfterCatalogUpdate =- cache.get(- EntityCacheRelationKey.of(- tag1.nameIdentifier(),- Entity.EntityType.TAG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),- k -> null);- Assertions.assertNull(cachedTagsByTagAfterCatalogUpdate);List<TagEntity> tagsAfterCatalogUpdate =relationOperations.listEntitiesByRelation(@@ -444,17 +384,6 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {Assertions.assertEquals(1, tagsAfterCatalogUpdate.size());Assertions.assertEquals(tag1, tagsAfterCatalogUpdate.get(0));- List<Entity> cachedTagsAfterReload =- cache.get(- EntityCacheRelationKey.of(- updatedCatalog.nameIdentifier(),- Entity.EntityType.CATALOG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),- k -> null);- Assertions.assertNotNull(cachedTagsAfterReload);- Assertions.assertEquals(1, cachedTagsAfterReload.size());- Assertions.assertEquals(tag1, cachedTagsAfterReload.get(0));-List<GenericEntity> genericEntitiesAfterCatalogUpdate =relationOperations.listEntitiesByRelation(SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,@@ -466,20 +395,6 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {Assertions.assertEquals(updatedCatalog.name(), genericEntitiesAfterCatalogUpdate.get(0).name());- List<Entity> cachedTagsByTagAfterReload =- cache.get(- EntityCacheRelationKey.of(- tag1.nameIdentifier(),- Entity.EntityType.TAG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),- k -> null);- Assertions.assertNotNull(cachedTagsByTagAfterReload);- Assertions.assertEquals(1, cachedTagsByTagAfterReload.size());- Assertions.assertEquals(- updatedCatalog.id(), ((GenericEntity) cachedTagsByTagAfterReload.get(0)).id());- Assertions.assertEquals(- updatedCatalog.name(), ((GenericEntity) cachedTagsByTagAfterReload.get(0)).name());-TagEntity updatedTag1 =TagEntity.builder().withId(tag1.id())@@ -490,24 +405,6 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {.build();store.update(tag1.nameIdentifier(), TagEntity.class, Entity.EntityType.TAG, e -> updatedTag1);- List<Entity> cachedTagsAfterTagUpdate =- cache.get(- EntityCacheRelationKey.of(- updatedCatalog.nameIdentifier(),- Entity.EntityType.CATALOG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),- k -> null);- Assertions.assertNull(cachedTagsAfterTagUpdate);-- List<Entity> cachedEntitiesAfterTagUpdate =- cache.get(- EntityCacheRelationKey.of(- tag1.nameIdentifier(),- Entity.EntityType.TAG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),- k -> null);- Assertions.assertNull(cachedEntitiesAfterTagUpdate);-List<TagEntity> tagsAfterTagUpdate =relationOperations.listEntitiesByRelation(SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,@@ -517,17 +414,6 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {Assertions.assertEquals(1, tagsAfterTagUpdate.size());Assertions.assertEquals(updatedTag1, tagsAfterTagUpdate.get(0));- List<Entity> cachedTagsAfterTagReload =- cache.get(- EntityCacheRelationKey.of(- updatedCatalog.nameIdentifier(),- Entity.EntityType.CATALOG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),- k -> null);- Assertions.assertNotNull(cachedTagsAfterTagReload);- Assertions.assertEquals(1, cachedTagsAfterTagReload.size());- Assertions.assertEquals(updatedTag1, cachedTagsAfterTagReload.get(0));-List<GenericEntity> genericEntitiesAfterTagUpdate =relationOperations.listEntitiesByRelation(SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,@@ -537,19 +423,6 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {Assertions.assertEquals(1, genericEntitiesAfterTagUpdate.size());Assertions.assertEquals(catalog.id(), genericEntitiesAfterTagUpdate.get(0).id());Assertions.assertEquals(updatedCatalog.name(), genericEntitiesAfterTagUpdate.get(0).name());- List<Entity> cachedTagsByTagAfterTagReload =- cache.get(- EntityCacheRelationKey.of(- updatedTag1.nameIdentifier(),- Entity.EntityType.TAG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),- k -> null);- Assertions.assertNotNull(cachedTagsByTagAfterTagReload);- Assertions.assertEquals(1, cachedTagsByTagAfterTagReload.size());- Assertions.assertEquals(- updatedCatalog.id(), ((GenericEntity) cachedTagsByTagAfterTagReload.get(0)).id());- Assertions.assertEquals(- updatedCatalog.name(), ((GenericEntity) cachedTagsByTagAfterTagReload.get(0)).name());destroy(type);}}@@ -878,16 +751,6 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {Entity.EntityType.FUNCTION,e -> renamedFunction);- if (enableCache && store instanceof RelationalEntityStore) {- RelationalEntityStore relationalEntityStore = (RelationalEntityStore) store;- if (relationalEntityStore.getCache() instanceof CaffeineEntityCache) {- CaffeineEntityCache cache = (CaffeineEntityCache) relationalEntityStore.getCache();- ReverseIndexCache reverseIndexCache = cache.getReverseIndex();- Assertions.assertNull(- reverseIndexCache.get(function.nameIdentifier(), Entity.EntityType.FUNCTION));- }- }-List<TagEntity> tagsAfterRename =relationOperations.listEntitiesByRelation(SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,@@ -914,7 +777,7 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {@ParameterizedTest@MethodSource("storageProvider")- void testViewNotIndexedInReverseCache(String type, boolean enableCache) throws Exception {+ void testViewCachedForSingleEntityLookup(String type, boolean enableCache) throws Exception {Config config = Mockito.mock(Config.class);Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(enableCache);init(type, config);@@ -960,28 +823,16 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {.withAuditInfo(auditInfo).build();- // Use store.put() to trigger the actual cache + reverse index flow+ // Use store.put() to trigger the single-entity cache flowstore.put(view, false);- // Verify view IS in forward cache (performance cache for get operations)+ // Verify the view can be read back through the store (served from cache when enabled)ViewEntity retrievedView =store.get(view.nameIdentifier(), Entity.EntityType.VIEW, ViewEntity.class);Assertions.assertNotNull(retrievedView);Assertions.assertEquals(view.id(), retrievedView.id());Assertions.assertEquals(view.name(), retrievedView.name());- // Get reverse index cache to verify view is NOT indexed- ReverseIndexCache reverseIndexCache =- ((CaffeineEntityCache) ((RelationalEntityStore) store).getCache()).getReverseIndex();-- // Verify view is NOT in reverse index cache- // Views have namespace and should be skipped by GENERIC_METADATA_OBJECT_REVERSE_RULE- List<EntityCacheKey> reverseIndexValue =- reverseIndexCache.get(view.nameIdentifier(), Entity.EntityType.VIEW);- Assertions.assertNull(- reverseIndexValue,- "Views should NOT be indexed in reverse cache - they have namespace and don't support tags/policies");-destroy(type);}}@@ -1022,25 +873,7 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {true);Assertions.assertTrue(tags.isEmpty());- // 2. Verify cache has empty list if cache is enabled- if (enableCache && store instanceof RelationalEntityStore) {- RelationalEntityStore relationalEntityStore = (RelationalEntityStore) store;- if (relationalEntityStore.getCache() instanceof CaffeineEntityCache) {- CaffeineEntityCache cache = (CaffeineEntityCache) relationalEntityStore.getCache();- List<Entity> cachedEntities =- cache- .getCacheData()- .getIfPresent(- EntityCacheRelationKey.of(- catalog.nameIdentifier(),- Entity.EntityType.CATALOG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL));- Assertions.assertNotNull(cachedEntities);- Assertions.assertTrue(cachedEntities.isEmpty());- }- }-- // 3. Create a tag and add relation+ // 2. Create a tag and add relationNamespace tagNamespace = NameIdentifierUtil.ofTag("metalake", "tag1").namespace();TagEntity tag1 =TagEntity.builder()@@ -1077,23 +910,6 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {true);Assertions.assertTrue(owners.isEmpty());- if (enableCache && store instanceof RelationalEntityStore) {- RelationalEntityStore relationalEntityStore = (RelationalEntityStore) store;- if (relationalEntityStore.getCache() instanceof CaffeineEntityCache) {- CaffeineEntityCache cache = (CaffeineEntityCache) relationalEntityStore.getCache();- List<Entity> cachedOwners =- cache- .getCacheData()- .getIfPresent(- EntityCacheRelationKey.of(- catalog.nameIdentifier(),- Entity.EntityType.CATALOG,- SupportsRelationOperations.Type.OWNER_REL));- Assertions.assertNotNull(cachedOwners);- Assertions.assertTrue(cachedOwners.isEmpty());- }- }-UserEntity ownerUser =createUserEntity(RandomIdGenerator.INSTANCE.nextId(),@@ -1172,25 +988,7 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {true);Assertions.assertTrue(entities.isEmpty());- // 2. Verify cache has empty list if cache is enabled- if (enableCache && store instanceof RelationalEntityStore) {- RelationalEntityStore relationalEntityStore = (RelationalEntityStore) store;- if (relationalEntityStore.getCache() instanceof CaffeineEntityCache) {- CaffeineEntityCache cache = (CaffeineEntityCache) relationalEntityStore.getCache();- List<Entity> cachedEntities =- cache- .getCacheData()- .getIfPresent(- EntityCacheRelationKey.of(- tag1.nameIdentifier(),- Entity.EntityType.TAG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL));- Assertions.assertNotNull(cachedEntities);- Assertions.assertTrue(cachedEntities.isEmpty());- }- }-- // 3. Add relation from Catalog (Source side)+ // 2. Add relation from Catalog (Source side)relationOperations.updateEntityRelations(SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,catalog.nameIdentifier(),@@ -1198,7 +996,7 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {new NameIdentifier[] {tag1.nameIdentifier()},new NameIdentifier[] {});- // 4. Fetch relation for Tag again, it should NOT be empty+ // 3. Fetch relation for Tag again, it should NOT be emptyentities =relationOperations.listEntitiesByRelation(SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,@@ -1276,40 +1074,14 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {.build();store.put(role, false);- // Read role to populate cache; ROLE_SECURABLE_OBJECT_REVERSE_RULE writes:- // funcIdent:FUNCTION -> [roleIdent:ROLE] into the reverse index.+ // Read the role once so a stale copy would be observable if it were cached.RoleEntity cachedRole =store.get(role.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);Assertions.assertEquals(3, cachedRole.securableObjects().size());- if (enableCache && store instanceof RelationalEntityStore) {- RelationalEntityStore relationalEntityStore = (RelationalEntityStore) store;- if (relationalEntityStore.getCache() instanceof CaffeineEntityCache) {- ReverseIndexCache reverseIndex =- ((CaffeineEntityCache) relationalEntityStore.getCache()).getReverseIndex();- List<EntityCacheKey> reverseKeys =- reverseIndex.get(function.nameIdentifier(), Entity.EntityType.FUNCTION);- Assertions.assertNotNull(reverseKeys);- Assertions.assertTrue(- reverseKeys.stream().anyMatch(k -> k.identifier().equals(role.nameIdentifier())));- }- }-- // Delete the function; BFS invalidation should evict the role from cache via reverse index.+ // Delete the function; the next role read must not serve a stale securable object list.store.delete(function.nameIdentifier(), Entity.EntityType.FUNCTION, false);- if (enableCache && store instanceof RelationalEntityStore) {- RelationalEntityStore relationalEntityStore = (RelationalEntityStore) store;- if (relationalEntityStore.getCache() instanceof CaffeineEntityCache) {- CaffeineEntityCache cache = (CaffeineEntityCache) relationalEntityStore.getCache();- Assertions.assertNull(- cache- .getCacheData()- .getIfPresent(- EntityCacheRelationKey.of(role.nameIdentifier(), Entity.EntityType.ROLE)));- }- }-// Re-read role; the function securable object should have been removed from DB via cascade.RoleEntity reloadedRole =store.get(role.nameIdentifier(), Entity.EntityType.ROLE, RoleEntity.class);@@ -1379,24 +1151,7 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {true);Assertions.assertTrue(owners.isEmpty());- if (enableCache && store instanceof RelationalEntityStore) {- RelationalEntityStore relationalEntityStore = (RelationalEntityStore) store;- if (relationalEntityStore.getCache() instanceof CaffeineEntityCache) {- CaffeineEntityCache cache = (CaffeineEntityCache) relationalEntityStore.getCache();- List<Entity> cachedOwners =- cache- .getCacheData()- .getIfPresent(- EntityCacheRelationKey.of(- function.nameIdentifier(),- Entity.EntityType.FUNCTION,- SupportsRelationOperations.Type.OWNER_REL));- Assertions.assertNotNull(cachedOwners);- Assertions.assertTrue(cachedOwners.isEmpty());- }- }-- // 2. Set an owner; cache for OWNER_REL should be invalidated+ // 2. Set an owner; the next read must reflect itUserEntity ownerUser =createUserEntity(RandomIdGenerator.INSTANCE.nextId(),@@ -1413,7 +1168,7 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {Entity.EntityType.USER,true);- // 3. Query owner again - should return the owner and be re-cached+ // 3. Query owner again - should return the ownerowners =relationOperations.listEntitiesByRelation(SupportsRelationOperations.Type.OWNER_REL,@@ -1423,7 +1178,7 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {Assertions.assertEquals(1, owners.size());Assertions.assertEquals(ownerUser.name(), owners.get(0).name());- // 4. Rename the function; old OWNER_REL cache entry should be evicted via BFS invalidation+ // 4. Rename the function; owner lookups via the new name must keep workingFunctionEntity renamedFunction =createFunctionEntity(function.id(),@@ -1436,23 +1191,6 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {Entity.EntityType.FUNCTION,e -> renamedFunction);- if (enableCache && store instanceof RelationalEntityStore) {- RelationalEntityStore relationalEntityStore = (RelationalEntityStore) store;- if (relationalEntityStore.getCache() instanceof CaffeineEntityCache) {- CaffeineEntityCache cache = (CaffeineEntityCache) relationalEntityStore.getCache();- // The old function's OWNER_REL cache should have been evicted by BFS invalidation- List<Entity> cachedOwners =- cache- .getCacheData()- .getIfPresent(- EntityCacheRelationKey.of(- function.nameIdentifier(),- Entity.EntityType.FUNCTION,- SupportsRelationOperations.Type.OWNER_REL));- Assertions.assertNull(cachedOwners);- }- }-// 5. Query owner via new name - should still return the correct ownerowners =relationOperations.listEntitiesByRelation(@@ -1540,27 +1278,8 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {Assertions.assertEquals(1, rolesBeforeRevoke.size());Assertions.assertEquals("test_role", rolesBeforeRevoke.get(0).name());- // Verify the relation is in cache when cache is enabled- if (enableCache && store instanceof RelationalEntityStore) {- RelationalEntityStore relStore = (RelationalEntityStore) store;- if (relStore.getCache() instanceof CaffeineEntityCache) {- CaffeineEntityCache caffeineCache = (CaffeineEntityCache) relStore.getCache();- List<Entity> cachedRoles =- caffeineCache- .getCacheData()- .getIfPresent(- EntityCacheRelationKey.of(- schema.nameIdentifier(),- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL));- Assertions.assertNotNull(cachedRoles, "Cache should be populated after first fetch");- Assertions.assertEquals(1, cachedRoles.size());- }- }-// Simulate revokePrivilegesFromRole: update the role to remove the schema securable object.- // RelationalEntityStore.update() calls cache.invalidate(roleIdent, ROLE) afterwards,- // which must also invalidate the schema's METADATA_OBJECT_ROLE_REL cache entry.+ // The next listEntitiesByRelation must reflect the change immediately.store.update(role.nameIdentifier(),RoleEntity.class,@@ -1575,25 +1294,6 @@ public class TestEntityStorageRelationCache extends AbstractEntityStorageTest {.withSecurableObjects(Lists.newArrayList()) // all privileges revoked.build());- // Verify METADATA_OBJECT_ROLE_REL cache is gone when cache is enabled- if (enableCache && store instanceof RelationalEntityStore) {- RelationalEntityStore relStore = (RelationalEntityStore) store;- if (relStore.getCache() instanceof CaffeineEntityCache) {- CaffeineEntityCache caffeineCache = (CaffeineEntityCache) relStore.getCache();- List<Entity> cachedAfterRevoke =- caffeineCache- .getCacheData()- .getIfPresent(- EntityCacheRelationKey.of(- schema.nameIdentifier(),- Entity.EntityType.SCHEMA,- SupportsRelationOperations.Type.METADATA_OBJECT_ROLE_REL));- Assertions.assertNull(- cachedAfterRevoke,- "METADATA_OBJECT_ROLE_REL cache must be invalidated after role update");- }- }-// The key correctness check: listEntitiesByRelation must not return the revoked roleList<RoleEntity> rolesAfterRevoke =relationOperations.listEntitiesByRelation(diff --git a/core/src/test/java/org/apache/gravitino/storage/relational/TestRelationalEntityStore.java b/core/src/test/java/org/apache/gravitino/storage/relational/TestRelationalEntityStore.javaindex e9451c5895..edc1cb9ebe 100644--- a/core/src/test/java/org/apache/gravitino/storage/relational/TestRelationalEntityStore.java+++ b/core/src/test/java/org/apache/gravitino/storage/relational/TestRelationalEntityStore.java@@ -39,6 +39,8 @@ import org.apache.gravitino.exceptions.NoSuchEntityException;import org.junit.jupiter.api.Assertions;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.CsvSource;import org.mockito.InOrder;import org.mockito.Mockito;@@ -111,16 +113,8 @@ public class TestRelationalEntityStore {Mockito.doAnswer(invocation -> {- Mockito.verify(cache, Mockito.never())- .invalidate(- src,- Entity.EntityType.TABLE,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);- Mockito.verify(cache, Mockito.never())- .invalidate(- dst,- Entity.EntityType.TAG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);+ Mockito.verify(cache, Mockito.never()).invalidate(src, Entity.EntityType.TABLE);+ Mockito.verify(cache, Mockito.never()).invalidate(dst, Entity.EntityType.TAG);return null;}).when(backend)@@ -150,86 +144,69 @@ public class TestRelationalEntityStore {dst,Entity.EntityType.TAG,true);- inOrder- .verify(cache)- .invalidate(- src, Entity.EntityType.TABLE, SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);- inOrder- .verify(cache)- .invalidate(- dst, Entity.EntityType.TAG, SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);+ inOrder.verify(cache).invalidate(src, Entity.EntityType.TABLE);+ inOrder.verify(cache).invalidate(dst, Entity.EntityType.TAG);}- @Test- void testUpdateEntityRelationsInvalidatesCacheAfterBackendUpdate()+ @ParameterizedTest+ @CsvSource({"TAG_METADATA_OBJECT_REL, TAG", "POLICY_METADATA_OBJECT_REL, POLICY"})+ void testUpdateEntityRelationsInvalidatesDestinationTypeAfterBackendUpdate(+ SupportsRelationOperations.Type relationType, Entity.EntityType destinationType)throws IOException, NoSuchEntityException, EntityAlreadyExistsException,IllegalAccessException {NameIdentifier src = NameIdentifier.of("metalake", "catalog", "schema", "table1");- NameIdentifier destToAdd = NameIdentifier.of("metalake", "tag1");- NameIdentifier destToRemove = NameIdentifier.of("metalake", "tag2");+ NameIdentifier destToAdd = NameIdentifier.of("metalake", "dest1");+ NameIdentifier destToRemove = NameIdentifier.of("metalake", "dest2");NameIdentifier[] destEntitiesToAdd = new NameIdentifier[] {destToAdd};NameIdentifier[] destEntitiesToRemove = new NameIdentifier[] {destToRemove};NoOpsCache cache = (NoOpsCache) FieldUtils.readField(store, "cache", true);Mockito.doAnswer(invocation -> {- Mockito.verify(cache, Mockito.never())- .invalidate(- src,- Entity.EntityType.TABLE,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);- Mockito.verify(cache, Mockito.never())- .invalidate(- destToAdd,- Entity.EntityType.TAG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);- Mockito.verify(cache, Mockito.never())- .invalidate(- destToRemove,- Entity.EntityType.TAG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);+ Mockito.verify(cache, Mockito.never()).invalidate(src, Entity.EntityType.TABLE);+ Mockito.verify(cache, Mockito.never()).invalidate(destToAdd, destinationType);+ Mockito.verify(cache, Mockito.never()).invalidate(destToRemove, destinationType);return List.of();}).when(backend).updateEntityRelations(- eq(SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),+ eq(relationType),eq(src),eq(Entity.EntityType.TABLE),any(NameIdentifier[].class),any(NameIdentifier[].class));store.updateEntityRelations(- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL,- src,- Entity.EntityType.TABLE,- destEntitiesToAdd,- destEntitiesToRemove);+ relationType, src, Entity.EntityType.TABLE, destEntitiesToAdd, destEntitiesToRemove);InOrder inOrder = Mockito.inOrder(backend, cache);inOrder.verify(backend).updateEntityRelations(- eq(SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL),+ eq(relationType),eq(src),eq(Entity.EntityType.TABLE),any(NameIdentifier[].class),any(NameIdentifier[].class));- inOrder- .verify(cache)- .invalidate(- src, Entity.EntityType.TABLE, SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);- inOrder- .verify(cache)- .invalidate(- destToAdd,- Entity.EntityType.TAG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);- inOrder- .verify(cache)- .invalidate(- destToRemove,- Entity.EntityType.TAG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);+ inOrder.verify(cache).invalidate(src, Entity.EntityType.TABLE);+ inOrder.verify(cache).invalidate(destToAdd, destinationType);+ inOrder.verify(cache).invalidate(destToRemove, destinationType);+ }++ @Test+ void testUpdateEntityRelationsRejectsUnsupportedRelationType() {+ NameIdentifier src = NameIdentifier.of("metalake", "catalog", "schema", "table1");++ Assertions.assertThrows(+ IllegalArgumentException.class,+ () ->+ store.updateEntityRelations(+ SupportsRelationOperations.Type.OWNER_REL,+ src,+ Entity.EntityType.TABLE,+ new NameIdentifier[0],+ new NameIdentifier[0]));+ Mockito.verifyNoInteractions(backend);}@Test@@ -272,21 +249,10 @@ public class TestRelationalEntityStore {Mockito.doAnswer(invocation -> {+ Mockito.verify(cache, Mockito.never()).invalidate(src, Entity.EntityType.TABLE);+ Mockito.verify(cache, Mockito.never()).invalidate(destToAdd, Entity.EntityType.TAG);Mockito.verify(cache, Mockito.never())- .invalidate(- src,- Entity.EntityType.TABLE,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);- Mockito.verify(cache, Mockito.never())- .invalidate(- destToAdd,- Entity.EntityType.TAG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);- Mockito.verify(cache, Mockito.never())- .invalidate(- destToRemove,- Entity.EntityType.TAG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);+ .invalidate(destToRemove, Entity.EntityType.TAG);return List.of();}).when(backend)@@ -296,22 +262,9 @@ public class TestRelationalEntityStore {InOrder inOrder = Mockito.inOrder(backend, cache);inOrder.verify(backend).updateEntityRelations(update);- inOrder- .verify(cache)- .invalidate(- src, Entity.EntityType.TABLE, SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);- inOrder- .verify(cache)- .invalidate(- destToAdd,- Entity.EntityType.TAG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);- inOrder- .verify(cache)- .invalidate(- destToRemove,- Entity.EntityType.TAG,- SupportsRelationOperations.Type.TAG_METADATA_OBJECT_REL);+ inOrder.verify(cache).invalidate(src, Entity.EntityType.TABLE);+ inOrder.verify(cache).invalidate(destToAdd, Entity.EntityType.TAG);+ inOrder.verify(cache).invalidate(destToRemove, Entity.EntityType.TAG);}@Test