Copilot commented on code in PR #14381:
URL: https://github.com/apache/gravitino/pull/14381#discussion_r4414660753
##########
core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.java:
##########
@@ -359,6 +363,36 @@ private void invalidateDescendants(String keyPrefix) {
}
}
+ /**
+ * 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, so it is
kept as is.
+ */
+ private static String indexText(NameIdentifier ident) {
Review Comment:
The comment states the schema separator cannot be \".\" or the escape
character, but this code does not enforce that invariant. If
`HierarchicalSchemaUtil.schemaSeparator()` ever returns a value containing `.`
or `INDEX_ESCAPE`, descendant prefix scans may become incorrect or collide.
Consider either (a) asserting/validating the separator at startup (or in this
class) to guarantee the invariant, or (b) incorporating the schema separator
into the escaping strategy so correctness doesn’t depend on an external
constraint.
##########
core/src/test/java/org/apache/gravitino/cache/TestCaffeineEntityCacheInvalidation.java:
##########
@@ -125,6 +126,57 @@ void
testInvalidateDoesNotEvictSiblingsWithSharedNamePrefix() {
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 testInvalidateDottedLevelKeepsCollidingKeyIndexed() {
Review Comment:
The test name is misleading: after the fix, dotted levels should no longer
produce a colliding index key. Renaming to reflect the intent (e.g., that
removing one entry must not remove another entry’s index mapping) would make
the suite clearer and reduce confusion during future maintenance.
##########
core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.java:
##########
@@ -359,6 +363,36 @@ private void invalidateDescendants(String keyPrefix) {
}
}
+ /**
+ * 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, so it is
kept as is.
+ */
+ private static String indexText(NameIdentifier ident) {
+ List<String> levels = Lists.newArrayList(ident.namespace().levels());
+ levels.add(ident.name());
+ return levels.stream()
+ .map(
+ level ->
+ level
+ .replace(INDEX_ESCAPE, INDEX_ESCAPE + INDEX_ESCAPE)
+ .replace(NAME_LEVEL_BOUNDARY, INDEX_ESCAPE +
NAME_LEVEL_BOUNDARY))
+ .collect(Collectors.joining(NAME_LEVEL_BOUNDARY));
+ }
+
Review Comment:
This path allocates multiple intermediate objects (stream pipeline, lambdas,
and new strings per `replace`) each time an index key is generated. If cache
puts/invalidations are high-frequency, consider a more allocation-friendly
approach (e.g., manual loop with a `StringBuilder` and per-character escaping)
to reduce GC pressure. This is optional unless profiling shows this path is hot.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]