hdygxsj commented on code in PR #8102:
URL: https://github.com/apache/gravitino/pull/8102#discussion_r2336169590


##########
server-common/src/main/java/org/apache/gravitino/server/authorization/RequestAuthorizationCache.java:
##########
@@ -0,0 +1,250 @@
+/*
+ * 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;
+
+import java.security.Principal;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import org.apache.gravitino.MetadataObject;
+import org.apache.gravitino.utils.PrincipalUtils;
+
+/**
+ * Used to avoid duplicate authorization checks for the same metadata within 
the same thread.
+ *
+ * <p>The time complexity of Jcasbin's enforce method is O(n). Further caching 
of authorization
+ * results can reduce the time spent on authorization.
+ */
+public class RequestAuthorizationCache {
+
+  private RequestAuthorizationCache() {}
+
+  /** Used to cache the results of metadata authorization. */
+  private static final ThreadLocal<Map<AuthorizationContext, Boolean>> 
allowAuthorizerThreadCache =
+      new ThreadLocal<>();
+
+  /** Used to cache the results of metadata authorization. */
+  private static final ThreadLocal<Map<AuthorizationContext, Boolean>> 
denyAuthorizerThreadCache =
+      new ThreadLocal<>();
+
+  /** Used to determine whether the role has already been loaded. */
+  private static final ThreadLocal<BooleanHolder> hasLoadedRoleThreadLocal = 
new ThreadLocal<>();
+
+  /**
+   * Wrap the authorization method with this method to prevent threadlocal 
leakage.
+   *
+   * @param supplier authorization method
+   * @return authorization result
+   * @param <T> authorization method
+   */
+  public static <T> T executeWithThreadCache(Supplier<T> supplier) {
+    start();
+    T result = threadLocalTransmitWrapper(supplier).get();
+    end();
+    return result;
+  }
+
+  /**
+   * Used to wrap a Supplier to ensure that the ThreadLocal context can be 
correctly passed across
+   * threads, enabling pruning effects when concurrently obtaining 
authorization results.
+   *
+   * @param supplier supplier
+   * @return wrapped supplier
+   */
+  public static <T> Supplier<T> threadLocalTransmitWrapper(Supplier<T> 
supplier) {
+    Map<AuthorizationContext, Boolean> tempAllowAuthorizer = 
allowAuthorizerThreadCache.get();
+    Map<AuthorizationContext, Boolean> tempDenyAuthorizer = 
denyAuthorizerThreadCache.get();
+    BooleanHolder tempRoleLoadedThread = hasLoadedRoleThreadLocal.get();
+    Principal currentPrincipal = PrincipalUtils.getCurrentPrincipal();
+
+    return () -> {
+      allowAuthorizerThreadCache.set(tempAllowAuthorizer);
+      denyAuthorizerThreadCache.set(tempDenyAuthorizer);
+      hasLoadedRoleThreadLocal.set(tempRoleLoadedThread);
+      try {
+        return PrincipalUtils.doAs(currentPrincipal, supplier::get);
+      } catch (Exception e) {
+        throw new RuntimeException(e);
+      } finally {
+        allowAuthorizerThreadCache.remove();
+        denyAuthorizerThreadCache.remove();
+        hasLoadedRoleThreadLocal.remove();
+      }
+    };
+  }
+
+  public static void loadRole(Runnable runnable) {
+    BooleanHolder hasLoadedRole = hasLoadedRoleThreadLocal.get();
+    if (hasLoadedRole == null) {
+      runnable.run();
+      return;
+    }
+    if (hasLoadedRole.isBool()) {
+      return;
+    }
+    runnable.run();
+    hasLoadedRole.setBool(true);
+  }
+
+  /**
+   * check allow
+   *
+   * @param username username
+   * @param metalake metalake
+   * @param metadataObject metadata object
+   * @param privilege privilege
+   * @param authorizer authorizer
+   * @return authorization result
+   */
+  public static boolean authorizeAllow(
+      String username,
+      String metalake,
+      MetadataObject metadataObject,
+      String privilege,
+      Function<AuthorizationContext, Boolean> authorizer) {
+    AuthorizationContext context =
+        new AuthorizationContext(username, metalake, metadataObject, 
privilege);
+    Map<AuthorizationContext, Boolean> authorizationContextBooleanMap =
+        allowAuthorizerThreadCache.get();
+    if (authorizationContextBooleanMap == null) {
+      return authorizer.apply(context);
+    }
+    return authorizationContextBooleanMap.computeIfAbsent(context, authorizer);
+  }
+
+  /**
+   * check deny
+   *
+   * @param username username
+   * @param metalake metalake
+   * @param metadataObject metadata object
+   * @param privilege privilege
+   * @param authorizer authorizer
+   * @return authorization result
+   */
+  public static boolean authorizeDeny(
+      String username,
+      String metalake,
+      MetadataObject metadataObject,
+      String privilege,
+      Function<AuthorizationContext, Boolean> authorizer) {
+    AuthorizationContext context =
+        new AuthorizationContext(username, metalake, metadataObject, 
privilege);
+    Map<AuthorizationContext, Boolean> authorizationContextBooleanMap =
+        denyAuthorizerThreadCache.get();
+    if (authorizationContextBooleanMap == null) {
+      return authorizer.apply(context);
+    }
+    return authorizationContextBooleanMap.computeIfAbsent(context, authorizer);
+  }
+
+  private static void start() {
+    allowAuthorizerThreadCache.set(new ConcurrentHashMap<>());
+    denyAuthorizerThreadCache.set(new ConcurrentHashMap<>());
+    hasLoadedRoleThreadLocal.set(new BooleanHolder());
+  }
+
+  private static void end() {

Review Comment:
   fixed



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