bharos commented on code in PR #13957:
URL: https://github.com/apache/gravitino/pull/13957#discussion_r4474061887


##########
core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/PolicyRoleRelBaseSQLProvider.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.storage.relational.mapper.provider.base;
+
+import static 
org.apache.gravitino.storage.relational.mapper.PolicyRoleRelMapper.POLICY_ROLE_RELATION_TABLE_NAME;
+
+import org.apache.gravitino.storage.relational.mapper.MetalakeMetaMapper;
+import org.apache.gravitino.storage.relational.mapper.PolicyMetaMapper;
+import org.apache.gravitino.storage.relational.mapper.provider.DatabaseTimeSQL;
+import org.apache.gravitino.storage.relational.po.PolicyRoleRelPO;
+import org.apache.ibatis.annotations.Param;
+
+/** Base SQL provider for policy-to-role references. */
+public class PolicyRoleRelBaseSQLProvider {
+
+  /** Returns SQL for listing the role references of one policy. */
+  public String listByPolicyId(@Param("policyId") Long policyId) {
+    return selectColumns()
+        + joins()
+        + " WHERE prr.policy_id = #{policyId}"
+        + activePredicates()
+        + " ORDER BY prr.role_name";
+  }
+
+  /** Returns SQL for listing the policies that reference one role name. */
+  public String listByRoleName(
+      @Param("metalakeName") String metalakeName, @Param("roleName") String 
roleName) {
+    return selectColumns()
+        + joins()
+        + " WHERE pm.metalake_id IN (SELECT mm.metalake_id FROM "
+        + MetalakeMetaMapper.TABLE_NAME
+        + " mm WHERE mm.metalake_name = #{metalakeName} AND mm.deleted_at = 0)"
+        + " AND prr.role_name = #{roleName}"
+        + activePredicates()
+        + " ORDER BY pm.policy_name";
+  }
+
+  /** Returns SQL for inserting one reference. */
+  public String insert(@Param("relation") PolicyRoleRelPO relation) {
+    return "INSERT INTO "
+        + POLICY_ROLE_RELATION_TABLE_NAME
+        + " (policy_id, role_name, current_version, last_version, deleted_at)"
+        + " VALUES (#{relation.policyId}, #{relation.roleName},"
+        + " #{relation.currentVersion}, #{relation.lastVersion}, 
#{relation.deletedAt})";
+  }
+
+  /** Returns SQL for soft-deleting one reference with a version CAS. */
+  public String softDeleteByIdAndVersion(@Param("relation") PolicyRoleRelPO 
relation) {
+    return "UPDATE "
+        + POLICY_ROLE_RELATION_TABLE_NAME
+        // Assign last_version first so MySQL computes both columns from the 
version observed
+        // before this update.
+        + " SET last_version = current_version + 1,"
+        + " current_version = current_version + 1,"
+        + " deleted_at = "
+        + deletedAtNowExpression()

Review Comment:
   Your #14285 added the policy soft-delete to the non-cascade path, so the 
policy row fences an in-flight update there now too. What was left was ordering 
- the role references were being cleared before the policy delete on that path. 
Moved them after it, matching the cascade path, and added a non-cascade 
regression test.



##########
core/src/main/java/org/apache/gravitino/storage/relational/service/MetalakeMetaService.java:
##########
@@ -401,6 +406,9 @@ public boolean deleteMetalake(NameIdentifier ident, boolean 
cascade) {
             () ->
                 SessionUtils.doWithoutCommit(
                     PolicyTagRelMapper.class, mapper -> 
mapper.softDeleteByMetalakeId(metalakeId)),
+            () ->
+                SessionUtils.doWithoutCommit(
+                    PolicyRoleRelMapper.class, mapper -> 
mapper.softDeleteByMetalakeId(metalakeId)),

Review Comment:
   Your #14285 added the policy soft-delete to the non-cascade path, so the 
policy row fences an in-flight update there now too. What was left was ordering 
- the role references were being cleared before the policy delete on that path. 
Moved them after it, matching the cascade path, and added a non-cascade 
regression test.



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