World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#7177] feat(core): Add more test case for CaffeineEntityCache (#7447)
Lord of Abyssb4bc8745f4b4
diff --git a/core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.java b/core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.javaindex 91148b4a78..7609f0efc8 100644--- a/core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.java+++ b/core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.java@@ -25,6 +25,7 @@ import com.github.benmanes.caffeine.cache.RemovalCause;import com.github.benmanes.caffeine.cache.stats.CacheStats;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;@@ -33,6 +34,7 @@ import java.util.ArrayList;import java.util.List;import java.util.Objects;import java.util.Optional;+import java.util.Set;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;@@ -285,14 +287,13 @@ public class CaffeineEntityCache extends BaseEntityCache {List<Entity> existingEntities = cacheData.getIfPresent(key);if (existingEntities != null && key.relationType() != null) {- List<Entity> merged = new ArrayList<>(existingEntities);+ Set<Entity> merged = Sets.newLinkedHashSet(existingEntities);merged.addAll(newEntities);-- cacheData.put(key, merged);- } else {- cacheData.put(key, newEntities);+ newEntities = new ArrayList<>(merged);}+ cacheData.put(key, newEntities);+if (cacheData.policy().getIfPresentQuietly(key) != null) {cacheIndex.put(key.toString(), key);}diff --git a/core/src/test/java/org/apache/gravitino/cache/TestCacheConfig.java b/core/src/test/java/org/apache/gravitino/cache/TestCacheConfig.javaindex 50d714d1fc..2320fa36ee 100644--- a/core/src/test/java/org/apache/gravitino/cache/TestCacheConfig.java+++ b/core/src/test/java/org/apache/gravitino/cache/TestCacheConfig.java@@ -19,9 +19,6 @@package org.apache.gravitino.cache;-import com.google.common.collect.Lists;-import java.util.ArrayList;-import java.util.Optional;import org.apache.gravitino.Config;import org.apache.gravitino.Configs;import org.junit.jupiter.api.Assertions;@@ -41,10 +38,18 @@ public class TestCacheConfig {}@Test- void test() {- ArrayList<Integer> list = Lists.newArrayList();- Optional<Integer> i =- Optional.ofNullable(list).filter(entities -> !entities.isEmpty()).map(l -> l.get(0));- Assertions.assertFalse(i.isPresent());+ void testSetConfigValues() {+ Config config = new Config(false) {};+ config.set(Configs.CACHE_ENABLED, false);+ config.set(Configs.CACHE_STATS_ENABLED, true);+ config.set(Configs.CACHE_WEIGHER_ENABLED, false);+ config.set(Configs.CACHE_MAX_ENTRIES, 5000);+ config.set(Configs.CACHE_EXPIRATION_TIME, 600_000L);++ Assertions.assertFalse(config.get(Configs.CACHE_ENABLED));+ Assertions.assertTrue(config.get(Configs.CACHE_STATS_ENABLED));+ Assertions.assertFalse(config.get(Configs.CACHE_WEIGHER_ENABLED));+ Assertions.assertEquals(5000, config.get(Configs.CACHE_MAX_ENTRIES));+ Assertions.assertEquals(600_000L, config.get(Configs.CACHE_EXPIRATION_TIME));}}diff --git a/core/src/test/java/org/apache/gravitino/cache/TestCaffeineEntityCache.java b/core/src/test/java/org/apache/gravitino/cache/TestCaffeineEntityCache.javaindex 68e414bf7c..d355425808 100644--- a/core/src/test/java/org/apache/gravitino/cache/TestCaffeineEntityCache.java+++ b/core/src/test/java/org/apache/gravitino/cache/TestCaffeineEntityCache.java@@ -21,6 +21,8 @@ package org.apache.gravitino.cache;import com.github.benmanes.caffeine.cache.Cache;import com.google.common.collect.ImmutableList;+import com.google.common.collect.ImmutableMap;+import com.google.common.collect.Sets;import java.util.List;import org.apache.commons.lang3.reflect.FieldUtils;import org.apache.gravitino.Config;@@ -48,36 +50,36 @@ import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.Test;import org.junit.jupiter.api.TestInstance;-@TestInstance(TestInstance.Lifecycle.PER_CLASS)+@TestInstance(TestInstance.Lifecycle.PER_METHOD)public class TestCaffeineEntityCache {- private NameIdentifier ident1;- private NameIdentifier ident2;- private NameIdentifier ident3;- private NameIdentifier ident4;- private NameIdentifier ident5;- private NameIdentifier ident6;- private NameIdentifier ident7;- private NameIdentifier ident8;- private NameIdentifier ident9;- private NameIdentifier ident10;- private NameIdentifier ident11;- private NameIdentifier ident12;- private NameIdentifier ident13;-- // Test Entities- private SchemaEntity entity1;- private SchemaEntity entity2;- private TableEntity entity3;- private TableEntity entity4;- private TableEntity entity5;- private CatalogEntity entity6;- private BaseMetalake entity7;- private UserEntity entity8;- private UserEntity entity9;- private GroupEntity entity10;- private GroupEntity entity11;- private RoleEntity entity12;- private RoleEntity entity13;+ private static NameIdentifier ident1;+ private static NameIdentifier ident2;+ private static NameIdentifier ident3;+ private static NameIdentifier ident4;+ private static NameIdentifier ident5;+ private static NameIdentifier ident6;+ private static NameIdentifier ident7;+ private static NameIdentifier ident8;+ private static NameIdentifier ident9;+ private static NameIdentifier ident10;+ private static NameIdentifier ident11;+ private static NameIdentifier ident12;+ private static NameIdentifier ident13;++ // Test Entities.+ private static SchemaEntity entity1;+ private static SchemaEntity entity2;+ private static TableEntity entity3;+ private static TableEntity entity4;+ private static TableEntity entity5;+ private static CatalogEntity entity6;+ private static BaseMetalake entity7;+ private static UserEntity entity8;+ private static UserEntity entity9;+ private static GroupEntity entity10;+ private static GroupEntity entity11;+ private static RoleEntity entity12;+ private static RoleEntity entity13;private static Object getCacheDataFrom(EntityCache cache) {try {@@ -93,7 +95,7 @@ public class TestCaffeineEntityCache {}@BeforeAll- void init() {+ static void init() {initTestNameIdentifier();initTestEntities();}@@ -315,6 +317,7 @@ public class TestCaffeineEntityCache {@Testvoid testClear() {EntityCache cache = new CaffeineEntityCache(new Config() {});+ Assertions.assertDoesNotThrow(cache::clear);cache.put(entity1);cache.put(entity2);@@ -508,6 +511,208 @@ public class TestCaffeineEntityCache {Assertions.assertFalse(cache.contains(ident3, Entity.EntityType.TABLE));}+ @Test+ void testPutRelationalEntitiesWithMerge() {+ EntityCache cache = new CaffeineEntityCache(new Config() {});+ RoleEntity testRoleEntity = TestUtil.getTestRoleEntity();+ GroupEntity testGroupEntity1 =+ TestUtil.getTestGroupEntity(+ 20L, "group1", "test_metalake", ImmutableList.of(testRoleEntity.name()));+ GroupEntity testGroupEntity2 =+ TestUtil.getTestGroupEntity(+ 21L, "group1", "test_metalake", ImmutableList.of(testRoleEntity.name()));++ cache.put(+ testRoleEntity.nameIdentifier(),+ Entity.EntityType.ROLE,+ SupportsRelationOperations.Type.ROLE_GROUP_REL,+ ImmutableList.of(testGroupEntity1));+ cache.put(+ testRoleEntity.nameIdentifier(),+ Entity.EntityType.ROLE,+ SupportsRelationOperations.Type.ROLE_GROUP_REL,+ ImmutableList.of(testGroupEntity2));++ Assertions.assertTrue(+ cache.contains(+ testRoleEntity.nameIdentifier(),+ testRoleEntity.type(),+ SupportsRelationOperations.Type.ROLE_GROUP_REL));+ Assertions.assertTrue(+ cache+ .getIfPresent(+ SupportsRelationOperations.Type.ROLE_GROUP_REL,+ testRoleEntity.nameIdentifier(),+ testRoleEntity.type())+ .isPresent());+ List<? extends Entity> entities =+ cache+ .getIfPresent(+ SupportsRelationOperations.Type.ROLE_GROUP_REL,+ testRoleEntity.nameIdentifier(),+ testRoleEntity.type())+ .get();+ Assertions.assertEquals(2, entities.size());+ Assertions.assertEquals(ImmutableList.of(testGroupEntity1, testGroupEntity2), entities);+ }++ @Test+ void testInvalidateOnKeyChange() {+ ModelEntity testModelEntity = TestUtil.getTestModelEntity();+ ModelVersionEntity testModelVersionEntity =+ TestUtil.getTestModelVersionEntity(+ testModelEntity.nameIdentifier(),+ 1,+ "s3://test/path",+ ImmutableMap.of(),+ "test model version",+ ImmutableList.of("alias1", "alias2"));++ EntityCache cache = new CaffeineEntityCache(new Config() {});+ cache.put(testModelEntity);+ Assertions.assertEquals(1, cache.size());+ Assertions.assertTrue(cache.contains(testModelEntity.nameIdentifier(), testModelEntity.type()));++ cache.put(testModelVersionEntity);+ Assertions.assertEquals(1, cache.size());+ Assertions.assertFalse(+ cache.contains(testModelEntity.nameIdentifier(), testModelEntity.type()));+ Assertions.assertTrue(+ cache.contains(testModelVersionEntity.nameIdentifier(), testModelVersionEntity.type()));+ }++ @Test+ void testPutSameRelationalEntities() {+ EntityCache cache = new CaffeineEntityCache(new Config() {});+ RoleEntity testRoleEntity = TestUtil.getTestRoleEntity();+ GroupEntity testGroupEntity =+ TestUtil.getTestGroupEntity(+ 20L, "group1", "test_metalake", ImmutableList.of(testRoleEntity.name()));++ cache.put(+ testRoleEntity.nameIdentifier(),+ Entity.EntityType.ROLE,+ SupportsRelationOperations.Type.ROLE_GROUP_REL,+ ImmutableList.of(testGroupEntity));+ cache.put(+ testRoleEntity.nameIdentifier(),+ Entity.EntityType.ROLE,+ SupportsRelationOperations.Type.ROLE_GROUP_REL,+ ImmutableList.of(testGroupEntity));++ Assertions.assertEquals(1, cache.size());+ Assertions.assertTrue(+ cache.contains(+ testRoleEntity.nameIdentifier(),+ testRoleEntity.type(),+ SupportsRelationOperations.Type.ROLE_GROUP_REL));+ Assertions.assertTrue(+ cache+ .getIfPresent(+ SupportsRelationOperations.Type.ROLE_GROUP_REL,+ testRoleEntity.nameIdentifier(),+ testRoleEntity.type())+ .isPresent());++ List<? extends Entity> entities =+ cache+ .getIfPresent(+ SupportsRelationOperations.Type.ROLE_GROUP_REL,+ testRoleEntity.nameIdentifier(),+ testRoleEntity.type())+ .get();+ Assertions.assertEquals(1, entities.size());+ Assertions.assertEquals(testGroupEntity, entities.get(0));+ }++ @Test+ void testPutRelationalEntitiesWithEmptyList() {+ EntityCache cache = new CaffeineEntityCache(new Config() {});+ RoleEntity testRoleEntity = TestUtil.getTestRoleEntity();++ Assertions.assertDoesNotThrow(+ () ->+ cache.put(+ testRoleEntity.nameIdentifier(),+ Entity.EntityType.ROLE,+ SupportsRelationOperations.Type.ROLE_GROUP_REL,+ ImmutableList.of()));+ Assertions.assertEquals(0, cache.size());+ }++ @Test+ void testPutRelationalEntitiesWithDifferentOrderButDeduplicated() {+ EntityCache cache = new CaffeineEntityCache(new Config() {});+ RoleEntity testRoleEntity = TestUtil.getTestRoleEntity();+ GroupEntity testGroupEntity1 =+ TestUtil.getTestGroupEntity(+ 20L, "group1", "test_metalake", ImmutableList.of(testRoleEntity.name()));+ GroupEntity testGroupEntity2 =+ TestUtil.getTestGroupEntity(+ 21L, "group1", "test_metalake", ImmutableList.of(testRoleEntity.name()));++ cache.put(+ testRoleEntity.nameIdentifier(),+ Entity.EntityType.ROLE,+ SupportsRelationOperations.Type.ROLE_GROUP_REL,+ ImmutableList.of(testGroupEntity1, testGroupEntity2));+ cache.put(+ testRoleEntity.nameIdentifier(),+ Entity.EntityType.ROLE,+ SupportsRelationOperations.Type.ROLE_GROUP_REL,+ ImmutableList.of(testGroupEntity2, testGroupEntity1));++ Assertions.assertEquals(1, cache.size());+ Assertions.assertTrue(+ cache+ .getIfPresent(+ SupportsRelationOperations.Type.ROLE_GROUP_REL,+ testRoleEntity.nameIdentifier(),+ testRoleEntity.type())+ .isPresent());++ List<? extends Entity> entities =+ cache+ .getIfPresent(+ SupportsRelationOperations.Type.ROLE_GROUP_REL,+ testRoleEntity.nameIdentifier(),+ testRoleEntity.type())+ .get();++ Assertions.assertEquals(+ Sets.newHashSet(testGroupEntity1, testGroupEntity2), Sets.newHashSet(entities));+ }++ @Test+ void testInvalidateRelationKeyAndRelatedEntities() {+ EntityCache cache = new CaffeineEntityCache(new Config() {});+ RoleEntity role = TestUtil.getTestRoleEntity();+ GroupEntity group = TestUtil.getTestGroupEntity();+ UserEntity user = TestUtil.getTestUserEntity();++ cache.put(+ role.nameIdentifier(),+ role.type(),+ SupportsRelationOperations.Type.ROLE_GROUP_REL,+ ImmutableList.of(group));+ cache.put(+ role.nameIdentifier(),+ role.type(),+ SupportsRelationOperations.Type.ROLE_USER_REL,+ ImmutableList.of(user));+ cache.put(role);++ cache.invalidate(role.nameIdentifier(), role.type());++ Assertions.assertFalse(+ cache.contains(+ role.nameIdentifier(), role.type(), SupportsRelationOperations.Type.ROLE_GROUP_REL));+ Assertions.assertFalse(+ cache.contains(+ role.nameIdentifier(), role.type(), SupportsRelationOperations.Type.ROLE_USER_REL));+ Assertions.assertFalse(cache.contains(role.nameIdentifier(), role.type()));+ }+@Testvoid testRemoveNonExistentEntity() {EntityCache cache = new CaffeineEntityCache(new Config() {});@@ -749,7 +954,7 @@ public class TestCaffeineEntityCache {null));}- private void initTestNameIdentifier() {+ private static void initTestNameIdentifier() {ident1 = NameIdentifier.of("metalake1", "catalog1", "schema1");ident2 = NameIdentifier.of("metalake2", "catalog2", "schema2");ident3 = NameIdentifier.of("metalake1", "catalog1", "schema1", "table1");@@ -768,7 +973,7 @@ public class TestCaffeineEntityCache {ident13 = NameIdentifierUtil.ofRole("metalake2", "role2");}- private void initTestEntities() {+ private static void initTestEntities() {entity1 =TestUtil.getTestSchemaEntity(1L, "schema1", Namespace.of("metalake1", "catalog1"), "test_schema1");