You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gravitino.apache.org by "yuqi1129 (via GitHub)" <gi...@apache.org> on 2026/06/01 03:44:37 UTC

[PR] [#11297] fix(cache): Propagate role invalidation to METADATA_OBJECT_ROLE_REL cache [gravitino]

yuqi1129 opened a new pull request, #11310:
URL: https://github.com/apache/gravitino/pull/11310

   ### What changes were proposed in this pull request?
   
   Extend `ROLE_SECURABLE_OBJECT_REVERSE_RULE` in `ReverseIndexRules` with one additional reverse-index entry: when a `RoleEntity` is stored as part of a **relation cache entry** (`key.relationType() != null`, e.g. `schemaIdent:SCHEMA:METADATA_OBJECT_ROLE_REL`), also register `reverseIndex[roleIdent:ROLE] → [schemaIdent:SCHEMA:METADATA_OBJECT_ROLE_REL]`.
   
   This lets the existing BFS in `CaffeineEntityCache.invalidateEntities` find and evict the stale relation cache whenever `cache.invalidate(roleIdent, ROLE)` is called (e.g. after `revokePrivilegesFromRole`).
   
   Add `TestCaffeineEntityCacheInvalidation` with 6 unit tests covering:
   - Core revoke scenario (issue #11297)
   - Role also separately cached before revoke
   - Multiple roles sharing the same schema
   - Role with securable objects on multiple schemas
   - Grant path when role was previously in the relation cache
   - Reverse-index cleanup / no memory leak
   
   ### Why are the changes needed?
   
   After `revokePrivilegesFromRole` removes the last privilege from a role's securable object, `RelationalEntityStore.update()` calls `cache.invalidate(roleIdent, ROLE)`. The BFS searched the reverse index by the role's identifier prefix (`metalake.system.role.<name>`) but `METADATA_OBJECT_ROLE_REL` was keyed by the schema identifier (`metalake.catalog.schema:SCHEMA`), so the BFS found nothing and the stale entry persisted. A subsequent call to `listBindingRoleNames()` served the cached (now-revoked) role from the stale entry.
   
   Fix: #11297
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. This is an internal cache-invalidation fix; the external API and DB layer are unchanged.
   
   ### How was this patch tested?
   
   New unit tests in `TestCaffeineEntityCacheInvalidation` exercise the exact cache state described in the bug report (grant → listBindingRoleNames → revoke → listBindingRoleNames must miss cache). All existing cache tests continue to pass:
   
   ```
   ./gradlew :core:test --tests "org.apache.gravitino.cache.*" -PskipITs
   ```


-- 
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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#11297] fix(cache): Propagate role invalidation to METADATA_OBJECT_ROLE_REL cache [gravitino]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #11310:
URL: https://github.com/apache/gravitino/pull/11310#discussion_r3332053385


##########
core/src/test/java/org/apache/gravitino/storage/TestEntityStorageRelationCache.java:
##########
@@ -1468,6 +1468,148 @@ void testFunctionOwnerRelationCacheInvalidation(String type, boolean enableCache
     }
   }
 
+  /**
+   * Reproduces GitHub issue #11297: after {@code revokePrivilegesFromRole} removes the last
+   * privilege from a role's securable schema, {@code
+   * listEntitiesByRelation(METADATA_OBJECT_ROLE_REL)} must not return the stale role from cache.
+   *
+   * <p>Flow: create schema + role → list binding roles (populates cache) → update role to remove
+   * securable object (simulates revoke) → list binding roles again → must reflect the change.
+   */
+  @ParameterizedTest
+  @MethodSource("storageProvider")
+  void testRevokePrivilegeInvalidatesMetadataObjectRoleRelCache(String type, boolean enableCache)
+      throws Exception {
+    Config config = Mockito.mock(Config.class);
+    Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(enableCache);
+    init(type, config);

Review Comment:
   `init(type, config)` stubs `Configs.CACHE_ENABLED` to `true` unconditionally (see `AbstractEntityStorageTest.init`), which overrides the earlier `thenReturn(enableCache)` and makes the `enableCache` parameter ineffective in this test. Re-stub `CACHE_ENABLED` after `init` so the parameterized `enableCache=false` case actually runs without cache and the conditional assertions match the real store behavior.



-- 
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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#11297] fix(cache): Propagate role invalidation to METADATA_OBJECT_ROLE_REL cache [gravitino]

Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 merged PR #11310:
URL: https://github.com/apache/gravitino/pull/11310


-- 
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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#11297] fix(cache): Propagate role invalidation to METADATA_OBJECT_ROLE_REL cache [gravitino]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #11310:
URL: https://github.com/apache/gravitino/pull/11310#discussion_r3331551717


##########
core/src/test/java/org/apache/gravitino/cache/TestCaffeineEntityCacheInvalidation.java:
##########
@@ -0,0 +1,376 @@
+/*
+ * 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.Lists;
+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.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.meta.RoleEntity;
+import org.apache.gravitino.storage.RandomIdGenerator;
+import org.apache.gravitino.utils.NameIdentifierUtil;
+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.
+ */
+public class TestCaffeineEntityCacheInvalidation {
+
+  private CaffeineEntityCache cache;
+  private AuditInfo auditInfo;
+
+  @BeforeEach
+  void setUp() {
+    Config config = new Config(false) {};
+    cache = new CaffeineEntityCache(config);
+    auditInfo = AuditInfo.builder().withCreator("test").withCreateTime(Instant.now()).build();
+  }
+
+  /**
+   * 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();
+  }
+
+  /**
+   * 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() {

Review Comment:
   Test method name has a typo/inconsistent camel-casing (`Propagatesto`). Renaming improves readability and makes it easier to locate the test by intent (propagates-to).



-- 
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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#11297] fix(cache): Propagate role invalidation to METADATA_OBJECT_ROLE_REL cache [gravitino]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #11310:
URL: https://github.com/apache/gravitino/pull/11310#issuecomment-4589788468

   <!-- coverage-report -->
   ### Code Coverage Report
   |Overall Project|66.73% **`+0.02%`**|:green_circle:|
   |:-|:-|:-:|
   |Files changed|87.14%|:green_circle:|
   <br>
   
   |Module|Coverage||
   |:-|:-|:-:|
   |aliyun|1.72%|:red_circle:|
   |api|46.82%|:green_circle:|
   |authorization-common|85.96%|:green_circle:|
   |aws|3.66%|:red_circle:|
   |azure|2.47%|:red_circle:|
   |catalog-common|10.04%|:red_circle:|
   |catalog-fileset|80.33%|:green_circle:|
   |catalog-glue|66.08%|:green_circle:|
   |catalog-hive|79.55%|:green_circle:|
   |catalog-jdbc-clickhouse|80.02%|:green_circle:|
   |catalog-jdbc-common|45.31%|:green_circle:|
   |catalog-jdbc-doris|80.28%|:green_circle:|
   |catalog-jdbc-hologres|54.03%|:green_circle:|
   |catalog-jdbc-mysql|79.23%|:green_circle:|
   |catalog-jdbc-oceanbase|78.38%|:green_circle:|
   |catalog-jdbc-postgresql|82.29%|:green_circle:|
   |catalog-jdbc-starrocks|78.51%|:green_circle:|
   |catalog-kafka|77.01%|:green_circle:|
   |catalog-lakehouse-generic|44.89%|:green_circle:|
   |catalog-lakehouse-hudi|79.1%|:green_circle:|
   |catalog-lakehouse-iceberg|85.73%|:green_circle:|
   |catalog-lakehouse-paimon|79.29%|:green_circle:|
   |catalog-model|77.72%|:green_circle:|
   |cli|44.51%|:green_circle:|
   |client-java|77.91%|:green_circle:|
   |common|49.99%|:green_circle:|
   |core|82.44% **`+0.01%`**|:green_circle:|
   |filesystem-hadoop3|76.97%|:green_circle:|
   |flink|0.0%|:red_circle:|
   |flink-common|41.2%|:green_circle:|
   |flink-runtime|0.0%|:red_circle:|
   |gcp|14.12%|:red_circle:|
   |hadoop-common|10.39%|:red_circle:|
   |hive-metastore-common|53.26%|:green_circle:|
   |iceberg-common|56.71%|:green_circle:|
   |iceberg-rest-server|72.01%|:green_circle:|
   |idp-basic|85.99%|:green_circle:|
   |integration-test-common|0.0%|:red_circle:|
   |jobs|66.17%|:green_circle:|
   |lance-common|20.83%|:red_circle:|
   |lance-rest-server|60.27%|:green_circle:|
   |lineage|53.02%|:green_circle:|
   |optimizer|82.87%|:green_circle:|
   |optimizer-api|21.95%|:red_circle:|
   |server|85.73%|:green_circle:|
   |server-common|73.66%|:green_circle:|
   |spark|32.79%|:red_circle:|
   |spark-common|39.75%|:red_circle:|
   |trino-connector|39.44%|:red_circle:|
   
   <details>
   <summary>Files</summary>
   
   |Module|File|Coverage||
   |:-|:-|:-|:-:|
   |core|[ReverseIndexRules.java](https://github.com/yuqi1129/gravitino/blob/13ab05c614a57277070fb5cbca8d2427c7d0d685/core/src/main/java/org/apache/gravitino/cache/ReverseIndexRules.java)|87.14%|:green_circle:|
   
   </details>


-- 
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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#11297] fix(cache): Propagate role invalidation to METADATA_OBJECT_ROLE_REL cache [gravitino]

Posted by "yuqi1129 (via GitHub)" <gi...@apache.org>.
yuqi1129 commented on code in PR #11310:
URL: https://github.com/apache/gravitino/pull/11310#discussion_r3332072116


##########
core/src/test/java/org/apache/gravitino/storage/TestEntityStorageRelationCache.java:
##########
@@ -1468,6 +1468,148 @@ void testFunctionOwnerRelationCacheInvalidation(String type, boolean enableCache
     }
   }
 
+  /**
+   * Reproduces GitHub issue #11297: after {@code revokePrivilegesFromRole} removes the last
+   * privilege from a role's securable schema, {@code
+   * listEntitiesByRelation(METADATA_OBJECT_ROLE_REL)} must not return the stale role from cache.
+   *
+   * <p>Flow: create schema + role → list binding roles (populates cache) → update role to remove
+   * securable object (simulates revoke) → list binding roles again → must reflect the change.
+   */
+  @ParameterizedTest
+  @MethodSource("storageProvider")
+  void testRevokePrivilegeInvalidatesMetadataObjectRoleRelCache(String type, boolean enableCache)
+      throws Exception {
+    Config config = Mockito.mock(Config.class);
+    Mockito.when(config.get(Configs.CACHE_ENABLED)).thenReturn(enableCache);
+    init(type, config);

Review Comment:
   No need to change.



-- 
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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org