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


##########
core/src/main/java/org/apache/gravitino/storage/relational/service/PolicyMetaService.java:
##########
@@ -429,6 +450,66 @@ private void insertPolicyWithoutCommit(
     updatePolicyRootWithVersion(observedIdentifier, existingPolicyPO, 
replacementPolicyPO);
     insertPolicyVersionIfAllocated(existingPolicyPO, replacementPolicyPO);
     afterUpdate(storedPolicy);
+    return replacementPolicyPO.getPolicyId();
+  }
+
+  /**
+   * Brings the role references derived from a policy's content in line with 
that content.
+   *
+   * <p>The references are written only here and never by a caller, so {@code 
content} stays the one
+   * authority on which roles a policy names.
+   *
+   * <p>Only the difference is written: a reference the content still names 
keeps its row, so a
+   * policy edit that leaves {@code applicableRoles} alone writes no reference 
rows at all.
+   *
+   * <p>A reference the content drops is deleted rather than soft-deleted, so 
no edit leaves a
+   * tombstone behind. Removing a role, naming it again, and removing it once 
more therefore cannot
+   * collide on the table's uniqueness over (policy, role, deleted_at), which 
the two removals would
+   * otherwise share whenever they read the same millisecond from the database 
clock.
+   */
+  private void updateRoleRelationsWithoutCommit(long policyId, PolicyEntity 
policyEntity) {
+    List<PolicyRoleRelPO> desired = 
POConverters.initializePolicyRoleRelPOs(policyId, policyEntity);
+    List<PolicyRoleRelPO> current =
+        SessionUtils.getWithoutCommit(
+            PolicyRoleRelMapper.class, mapper -> 
mapper.listByPolicyId(policyId));
+
+    Set<String> desiredRoles =
+        
desired.stream().map(PolicyRoleRelPO::getRoleName).collect(Collectors.toSet());
+    Set<String> currentRoles =
+        
current.stream().map(PolicyRoleRelPO::getRoleName).collect(Collectors.toSet());
+
+    for (PolicyRoleRelPO superseded : current) {
+      if (desiredRoles.contains(superseded.getRoleName())) {
+        continue;
+      }
+      // A zero-row delete means the reference is already gone, which is the 
intended end state.
+      SessionUtils.doWithoutCommit(
+          PolicyRoleRelMapper.class, mapper -> 
mapper.deleteByIdAndVersion(superseded));
+    }
+
+    for (PolicyRoleRelPO added : desired) {
+      if (currentRoles.contains(added.getRoleName())) {
+        continue;
+      }
+      try {
+        SessionUtils.doWithoutCommit(PolicyRoleRelMapper.class, mapper -> 
mapper.insert(added));
+      } catch (RuntimeException re) {
+        // This table names roles exactly as the role table does, so a 
duplicate here is content
+        // naming one role twice under the backend's own equality: MySQL 
ignores trailing spaces,
+        // PostgreSQL does not, and each matches how that backend identifies a 
role. Report the
+        // role that collided instead of letting the caller's handler blame 
the policy name. The
+        // SQL failure is deliberately not chained: the handlers upstream look 
for a duplicate key
+        // through the cause chain and would rewrite this back into an 
already-existing policy.
+        if (ExceptionUtils.isDuplicateKey(re)) {
+          throw new IllegalArgumentException(

Review Comment:
   Done. It now logs the original exception with the role, policy and ID before 
converting it.



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