yuqi1129 commented on code in PR #10996:
URL: https://github.com/apache/gravitino/pull/10996#discussion_r3280817427
##########
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:
resolved.
##########
server-common/src/main/java/org/apache/gravitino/server/authorization/jcasbin/JcasbinAuthorizationCacheKeys.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.server.authorization.jcasbin;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.gravitino.MetadataObject;
+
+/** Cache key factory for JCasbin authorization caches. */
+final class JcasbinAuthorizationCacheKeys {
+
+ /** Unit Separator for internal cache keys. */
+ static final String SEPARATOR = "\u001F";
+
+ private JcasbinAuthorizationCacheKeys() {}
+
+ /**
+ * Builds a path-based key for the metadata id cache.
+ *
+ * <p>Container objects end with the internal separator so prefix
invalidation can remove both the
+ * container and entries under the same name path. Leaf objects include the
type suffix to avoid
+ * collisions between objects that share the same name path.
+ */
+ static String metadataObjectKey(String metalake, MetadataObject
metadataObject) {
+ if (metadataObject.type() == MetadataObject.Type.METALAKE) {
+ return metalake + SEPARATOR;
+ }
+
+ StringBuilder sb = new StringBuilder(metalake);
+ sb.append(SEPARATOR);
+ sb.append(String.join(SEPARATOR, metadataObject.fullName().split("\\.")));
+ if (isMetadataContainer(metadataObject.type())) {
+ sb.append(SEPARATOR);
+ } else {
+ sb.append(SEPARATOR);
+ sb.append(metadataObject.type().name());
+ }
+ return sb.toString();
+ }
+
+ static String userRoleKey(String metalake, String username) {
+ return "USER" + SEPARATOR + metalake + SEPARATOR + username;
Review Comment:
resolved.
--
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]