roryqi commented on code in PR #10996:
URL: https://github.com/apache/gravitino/pull/10996#discussion_r3279576245


##########
server-common/src/main/java/org/apache/gravitino/server/authorization/jcasbin/JcasbinAuthorizer.java:
##########
@@ -756,47 +907,65 @@ private List<GroupEntity> resolveCurrentUserGroups(String 
metalake, EntityStore
     return entityStore.batchGet(groupIdents, Entity.EntityType.GROUP, 
GroupEntity.class);
   }
 
-  /**
-   * Adds a role mapping for the given user in both enforcers and 
asynchronously loads the role's
-   * policies if they are not already cached. When a role needs loading, the 
resulting {@link
-   * CompletableFuture} is appended to {@code loadRoleFutures} so the caller 
can join all futures
-   * after processing both direct and group-inherited roles.
-   */
-  private void addRoleForUserAndLoadPolicies(
-      Long userId,
-      String metalake,
-      Long roleId,
-      String roleName,
-      List<CompletableFuture<Void>> loadRoleFutures,
-      EntityStore entityStore,
-      AuthorizationRequestContext requestContext) {
-    allowEnforcer.addRoleForUser(String.valueOf(userId), 
String.valueOf(roleId));
-    denyEnforcer.addRoleForUser(String.valueOf(userId), 
String.valueOf(roleId));
-    if (loadedRoles.getIfPresent(roleId) != null) {
-      return;
-    }
-    CompletableFuture<Void> loadRoleFuture =
-        CompletableFuture.supplyAsync(
-                () -> {
-                  try {
-                    return entityStore.get(
-                        NameIdentifierUtil.ofRole(metalake, roleName),
-                        Entity.EntityType.ROLE,
-                        RoleEntity.class);
-                  } catch (Exception e) {
-                    throw new RuntimeException("Failed to load role: " + 
roleName, e);
-                  }
-                },
-                executor)
-            .thenAcceptAsync(
-                roleEntity -> {
-                  loadPolicyByRoleEntity(roleEntity, requestContext);
-                  loadedRoles.put(roleId, true);
-                },
-                executor);
-    loadRoleFutures.add(loadRoleFuture);
+  private void versionCheckAndLoadRoles(
+      String metalake, List<Long> roleIds, AuthorizationRequestContext 
requestContext) {
+    // Step 3: batch fetch (roleId, roleName, updated_at) for all role IDs — 1 
query
+    List<Long> uniqueRoleIds = 
roleIds.stream().distinct().collect(Collectors.toList());
+    List<RoleUpdatedAt> roleVersions =
+        SessionUtils.getWithoutCommit(
+            RoleMetaMapper.class, m -> m.batchGetRoleUpdatedAt(uniqueRoleIds));
+
+    for (RoleUpdatedAt rv : roleVersions) {
+      long roleId = rv.getRoleId();
+      long dbUpdatedAt = rv.getUpdatedAt();
+      Optional<Long> cachedUpdatedAt = loadedRoles.getIfPresent(roleId);
+
+      if (cachedUpdatedAt.isPresent() && cachedUpdatedAt.get() >= dbUpdatedAt) 
{
+        // Role policies are still current
+        continue;
+      }
+
+      // Load full role entity using roleName from the batch query (no extra 
DB scan)
+      RoleEntity roleEntity;

Review Comment:
   Could u batch get the roles?



-- 
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]

Reply via email to