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


##########
server-common/src/main/java/org/apache/gravitino/server/authorization/jcasbin/CachedGroupRoles.java:
##########
@@ -0,0 +1,37 @@
+/*
+ * 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 java.util.List;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * Cached snapshot of a group's role assignments. The {@code updatedAt} 
timestamp corresponds to the
+ * {@code group_meta.updated_at} column and is used as a version sentinel: if 
the DB value is newer,
+ * the cached role list is stale and must be reloaded.
+ */
+@Getter
+@AllArgsConstructor
+public class CachedGroupRoles {

Review Comment:
   Could u give a better name for this class? You cached group role relations 
instead of roles, don't you?



##########
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;
+  }
+
+  static String groupRoleKey(String metalake, String groupname) {
+    return "GROUP" + SEPARATOR + metalake + SEPARATOR + groupname;
+  }
+
+  @VisibleForTesting
+  static boolean isMetadataContainer(MetadataObject.Type type) {

Review Comment:
   Could u put this into MetadataObjectUtil? We should have a unified place to 
define this.



##########
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;
+  }
+
+  static String groupRoleKey(String metalake, String groupname) {
+    return "GROUP" + SEPARATOR + metalake + SEPARATOR + groupname;
+  }
+
+  @VisibleForTesting
+  static boolean isMetadataContainer(MetadataObject.Type type) {

Review Comment:
   Maybe we should have a better name. Because table can contain column.



##########
server-common/src/main/java/org/apache/gravitino/server/authorization/jcasbin/JcasbinAuthorizer.java:
##########
@@ -652,93 +694,202 @@ private boolean authorizeByJcasbin(
     }
   }
 
-  private static UserEntity getUserEntity(String username, String metalake) 
throws IOException {
+  // 
---------------------------------------------------------------------------
+  //  User info / ownership helpers
+  // 
---------------------------------------------------------------------------
+
+  /**
+   * Per-request {@link UserUpdatedAt} lookup. The underlying {@code 
user_meta} query is issued at
+   * most once per (metalake, username) within a single request.
+   */
+  private Optional<UserUpdatedAt> loadUserInfo(
+      String metalake, String username, AuthorizationRequestContext 
requestContext) {
+    String cacheKey = JcasbinAuthorizationCacheKeys.userRoleKey(metalake, 
username);
+    return requestContext.computeUserInfoIfAbsent(
+        cacheKey,
+        k ->
+            Optional.ofNullable(

Review Comment:
   Do we cache null value? Do we record the creation of metadata objects?



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