World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#14380] fix(core): Escape dotted levels in the entity cache prefix index (#14381)
Honggeun Ji0ad9daf4fe6b
diff --git a/core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.java b/core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.javaindex 8ac859458d..d4ba1846c9 100644--- a/core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.java+++ b/core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.java@@ -100,6 +100,9 @@ public class CaffeineEntityCache extends BaseEntityCache {*/private static final String NAME_LEVEL_BOUNDARY = ".";+ /** Escapes {@link #NAME_LEVEL_BOUNDARY} and itself inside a level of an index key. */+ private static final char INDEX_ESCAPE = '\u0001';+/** Segmented locking for better concurrency */private final SegmentedLock segmentedLock;@@ -255,7 +258,7 @@ public class CaffeineEntityCache extends BaseEntityCache {// notification removes its index entry. This is not a synchronous admission check.// The quiet lookup does not schedule maintenance or acquire Caffeine's eviction lock.if (cacheData.policy().getIfPresentQuietly(entityCacheKey) != null) {- cacheIndex.put(entityCacheKey.toString(), entityCacheKey);+ cacheIndex.put(indexKey(entityCacheKey), entityCacheKey);}}});@@ -305,7 +308,7 @@ public class CaffeineEntityCache extends BaseEntityCache {protected void invalidateExpiredItem(EntityCacheKey key) {synchronized (indexLock) {if (cacheData.policy().getIfPresentQuietly(key) == null) {- cacheIndex.remove(key.toString());+ cacheIndex.remove(indexKey(key));}}}@@ -338,7 +341,7 @@ public class CaffeineEntityCache extends BaseEntityCache {cacheData.invalidate(key);removeIndexEntryIfAbsent(key);- String identifier = key.identifier().toString();+ String identifier = indexText(key.identifier());invalidateDescendants(identifier + NAME_LEVEL_BOUNDARY);if (key.entityType() == Entity.EntityType.SCHEMA) {invalidateDescendants(identifier + HierarchicalSchemaUtil.schemaSeparator());@@ -359,6 +362,48 @@ public class CaffeineEntityCache extends BaseEntityCache {}}+ /**+ * Returns the prefix-index key of a cache key: the escaped identifier text, {@code ":"}, and the+ * entity type.+ */+ private static String indexKey(EntityCacheKey key) {+ return indexText(key.identifier()) + ":" + key.entityType();+ }++ /**+ * Joins the levels of an identifier into prefix-index text.+ *+ * <p>{@link NameIdentifier#toString()} is not one-to-one when a level contains {@code "."}: table+ * {@code a.b} in schema {@code sch} and table {@code b} in schema {@code sch.a} both render as+ * {@code sch.a.b}. Escaping {@code "."} and the escape character inside each level makes the text+ * unique, while every real level boundary stays a bare {@code "."}, so both prefix scans in+ * {@link #invalidateHierarchy(EntityCacheKey)} still match exactly the descendants. The schema+ * separator can be neither {@code "."} nor the escape character (see {@link+ * Configs#SCHEMA_SEPARATOR}), so it is kept as is.+ */+ private static String indexText(NameIdentifier ident) {+ StringBuilder text = new StringBuilder();+ for (String level : ident.namespace().levels()) {+ appendEscaped(text, level).append(NAME_LEVEL_BOUNDARY);+ }+ return appendEscaped(text, ident.name()).toString();+ }++ /**+ * Appends a level to prefix-index text, prefixing {@code "."} and the escape character with the+ * escape character.+ */+ private static StringBuilder appendEscaped(StringBuilder text, String level) {+ for (int i = 0; i < level.length(); i++) {+ char c = level.charAt(i);+ if (c == INDEX_ESCAPE || c == NAME_LEVEL_BOUNDARY.charAt(0)) {+ text.append(INDEX_ESCAPE);+ }+ text.append(c);+ }+ return text;+ }+/*** Returns a new instance of Caffeine cache builder.*diff --git a/core/src/test/java/org/apache/gravitino/cache/TestCaffeineEntityCacheInvalidation.java b/core/src/test/java/org/apache/gravitino/cache/TestCaffeineEntityCacheInvalidation.javaindex ce277811ca..4890c5bc16 100644--- a/core/src/test/java/org/apache/gravitino/cache/TestCaffeineEntityCacheInvalidation.java+++ b/core/src/test/java/org/apache/gravitino/cache/TestCaffeineEntityCacheInvalidation.java@@ -26,6 +26,7 @@ import org.apache.gravitino.Config;import org.apache.gravitino.Configs;import org.apache.gravitino.Entity;import org.apache.gravitino.GravitinoEnv;+import org.apache.gravitino.NameIdentifier;import org.apache.gravitino.Namespace;import org.apache.gravitino.meta.CatalogEntity;import org.apache.gravitino.meta.GroupEntity;@@ -125,6 +126,57 @@ public class TestCaffeineEntityCacheInvalidation {Assertions.assertTrue(cache.contains(catalog10.nameIdentifier(), Entity.EntityType.CATALOG));}+ @Test+ void testInvalidateWithDottedLevelsDoesNotCollide() {+ // Both render as "lake.cat.sch.a.b".+ TableEntity t1 = TestUtil.getTestTableEntity(1L, "a.b", Namespace.of("lake", "cat", "sch"));+ TableEntity t2 = TestUtil.getTestTableEntity(2L, "b", Namespace.of("lake", "cat", "sch.a"));+ Assertions.assertNotEquals(t1.nameIdentifier(), t2.nameIdentifier());+ Assertions.assertEquals(t1.nameIdentifier().toString(), t2.nameIdentifier().toString());++ cache.put(t1);+ cache.put(t2);+ Assertions.assertEquals(2, cache.size());++ cache.invalidate(NameIdentifier.of("lake", "cat", "sch"), Entity.EntityType.SCHEMA);++ Assertions.assertFalse(+ cache.contains(t1.nameIdentifier(), Entity.EntityType.TABLE),+ "T1 (table 'a.b' in schema 'sch') must be invalidated with its schema");+ Assertions.assertTrue(+ cache.contains(t2.nameIdentifier(), Entity.EntityType.TABLE),+ "T2 (table 'b' in schema 'sch.a') must survive invalidation of schema 'sch'");+ }++ @Test+ void testInvalidateDottedLevelEntryKeepsOtherEntryIndexed() {+ TableEntity t1 = TestUtil.getTestTableEntity(1L, "a.b", Namespace.of("lake", "cat", "sch"));+ TableEntity t2 = TestUtil.getTestTableEntity(2L, "b", Namespace.of("lake", "cat", "sch.a"));+ cache.put(t1);+ cache.put(t2);++ // Removing T1 must not drop the index entry of T2.+ cache.invalidate(t1.nameIdentifier(), Entity.EntityType.TABLE);+ Assertions.assertTrue(cache.contains(t2.nameIdentifier(), Entity.EntityType.TABLE));++ cache.invalidate(NameIdentifier.of("lake", "cat", "sch.a"), Entity.EntityType.SCHEMA);+ Assertions.assertFalse(cache.contains(t2.nameIdentifier(), Entity.EntityType.TABLE));+ }++ @Test+ void testInvalidateWithEscapeCharacterInLevelsDoesNotCollide() {+ // Escaping only '.' would render both as "lake.cat.s<ESC>.b<ESC>.c", where ESC is U+0001.+ TableEntity t1 = TestUtil.getTestTableEntity(1L, "b.c", Namespace.of("lake", "cat", "s\u0001"));+ TableEntity t2 = TestUtil.getTestTableEntity(2L, "c", Namespace.of("lake", "cat", "s.b\u0001"));+ cache.put(t1);+ cache.put(t2);+ Assertions.assertEquals(2, cache.size());++ cache.invalidate(NameIdentifier.of("lake", "cat", "s\u0001"), Entity.EntityType.SCHEMA);+ Assertions.assertFalse(cache.contains(t1.nameIdentifier(), Entity.EntityType.TABLE));+ Assertions.assertTrue(cache.contains(t2.nameIdentifier(), Entity.EntityType.TABLE));+ }+@Testvoid testRoleUserGroupAreNotCached() {RoleEntity role = TestUtil.getTestRoleEntity();