yuqi1129 commented on code in PR #12579: URL: https://github.com/apache/gravitino/pull/12579#discussion_r3863408257
########## core/src/main/java/org/apache/gravitino/storage/relational/po/PolicyTagRelPO.java: ########## @@ -0,0 +1,145 @@ +/* + * 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.po; + +import com.google.common.base.Objects; +import com.google.common.base.Preconditions; +import javax.annotation.Nullable; +import lombok.Getter; + +/** Persistent object for a policy-to-tag relation row. */ +@Getter +public class PolicyTagRelPO { + private Long policyId; Review Comment: Blocking for relation-level OCC: the table has an immutable primary key `id`, but this PO drops it, so `softDeleteByPair` can only compare `(policy_id, tag_id, current_version)`. That is ABA-unsafe: T1 reads relation row A at version 1; T2 deletes A and re-adds the pair as row B, again at version 1; T1 can then delete B with the stale observation of A. Please map the relation row ID and make update/delete CAS use `id + observed current_version + deleted_at = 0`. Any in-place selector update must also advance the relation version. ########## core/src/main/java/org/apache/gravitino/storage/relational/service/PolicyTagRelService.java: ########## @@ -0,0 +1,365 @@ +/* + * 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.service; + +import com.google.common.base.Preconditions; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.gravitino.Entity; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.RelationEdgeTarget; +import org.apache.gravitino.RelationalEntity; +import org.apache.gravitino.SupportsRelationOperations; +import org.apache.gravitino.exceptions.NoSuchEntityException; +import org.apache.gravitino.exceptions.OptimisticLockException; +import org.apache.gravitino.json.JsonUtils; +import org.apache.gravitino.meta.AuditInfo; +import org.apache.gravitino.meta.PolicyEntity; +import org.apache.gravitino.meta.TagEntity; +import org.apache.gravitino.storage.relational.mapper.PolicyMetaMapper; +import org.apache.gravitino.storage.relational.mapper.PolicyTagRelMapper; +import org.apache.gravitino.storage.relational.mapper.TagMetaMapper; +import org.apache.gravitino.storage.relational.po.PolicyPO; +import org.apache.gravitino.storage.relational.po.PolicyTagRelPO; +import org.apache.gravitino.storage.relational.po.TagPO; +import org.apache.gravitino.storage.relational.utils.SessionUtils; +import org.apache.gravitino.utils.NameIdentifierUtil; +import org.apache.gravitino.utils.PrincipalUtils; + +/** JDBC metadata service for policy-to-tag relations. */ +public class PolicyTagRelService { + + private static final PolicyTagRelService INSTANCE = new PolicyTagRelService(); + + /** + * @return The singleton service instance. + */ + public static PolicyTagRelService getInstance() { + return INSTANCE; + } + + private PolicyTagRelService() {} + + /** + * Lists policy-to-tag relation edges from policy or tag anchors. + * + * @param anchors The policy or tag identifiers to query. + * @param anchorType The entity type of every anchor. + * @return The relation edges, including selector JSON as the relation value. + */ + public List<RelationalEntity<?>> listRelations( + List<NameIdentifier> anchors, Entity.EntityType anchorType) { + if (anchors == null || anchors.isEmpty()) { + return Collections.emptyList(); + } + Preconditions.checkArgument( + anchorType == Entity.EntityType.TAG || anchorType == Entity.EntityType.POLICY, + "Policy-to-tag relations do not support anchor type %s", + anchorType); + validateSameMetalake(anchors); + + String metalake = anchors.get(0).namespace().level(0); + List<String> anchorNames = + anchors.stream().map(NameIdentifier::name).distinct().collect(Collectors.toList()); + List<PolicyTagRelPO> relations = + SessionUtils.getWithoutCommit( + PolicyTagRelMapper.class, + mapper -> + anchorType == Entity.EntityType.TAG + ? mapper.listByTagNames(metalake, anchorNames) + : mapper.listByPolicyNames(metalake, anchorNames)); + if (relations.isEmpty()) { + return Collections.emptyList(); + } + + return anchorType == Entity.EntityType.TAG + ? policyTargets(metalake, relations) + : tagTargets(metalake, relations); + } + + /** + * Creates, replaces, or removes policy-to-tag relations for one tag. + * + * <p>An add for an existing pair replaces its selector. Repeating the same selector and removing + * a missing pair are idempotent no-ops. + * + * @param tagIdentifier The source tag identifier. + * @param targetsToAdd Policy targets to create or replace. + * @param targetsToRemove Policy targets to remove. + * @return All active policy targets for the tag after the update. + * @throws IOException If selector audit information cannot be serialized. + */ + public List<PolicyEntity> updateRelations( + NameIdentifier tagIdentifier, + RelationEdgeTarget[] targetsToAdd, + RelationEdgeTarget[] targetsToRemove) + throws IOException { + NameIdentifierUtil.checkTag(tagIdentifier); + String metalake = tagIdentifier.namespace().level(0); + RelationEdgeTarget[] targetsToAddOrEmpty = nullToEmpty(targetsToAdd); + RelationEdgeTarget[] targetsToRemoveOrEmpty = nullToEmpty(targetsToRemove); + validatePolicyTargets(metalake, targetsToAddOrEmpty); + validatePolicyTargets(metalake, targetsToRemoveOrEmpty); + + List<PolicyEntity> updatedPolicies = new ArrayList<>(); + try { + SessionUtils.doMultipleWithCommit( + () -> { + try { + updatedPolicies.addAll( + updateRelationsWithoutCommit( + tagIdentifier, targetsToAddOrEmpty, targetsToRemoveOrEmpty)); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }); + } catch (UncheckedIOException e) { + throw e.getCause(); + } + return updatedPolicies; + } + + private List<PolicyEntity> updateRelationsWithoutCommit( + NameIdentifier tagIdentifier, + RelationEdgeTarget[] targetsToAdd, + RelationEdgeTarget[] targetsToRemove) + throws IOException { + String metalake = tagIdentifier.namespace().level(0); + TagPO tagPO = lockTag(tagIdentifier); Review Comment: This currently gets its safety from a pessimistic Tag row lock, not from relation data OCC, and it does not fence the Policy endpoint. With the entity OCC work, the relation mutation must participate in the same database protocol as both endpoint lifecycles: validate/fence the live Tag and every affected Policy inside this transaction, then apply relation-row CAS writes; entity delete must win its root CAS before cleaning relations. A zero-row endpoint or relation CAS must roll back the whole operation. Please reuse the metadata OCC token introduced by the tag/policy OCC work rather than blindly incrementing the current Policy `current_version`, because that column currently also selects `policy_version_info.version`. ########## core/src/main/java/org/apache/gravitino/storage/relational/service/PolicyTagRelService.java: ########## @@ -0,0 +1,365 @@ +/* + * 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.service; + +import com.google.common.base.Preconditions; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.gravitino.Entity; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.RelationEdgeTarget; +import org.apache.gravitino.RelationalEntity; +import org.apache.gravitino.SupportsRelationOperations; +import org.apache.gravitino.exceptions.NoSuchEntityException; +import org.apache.gravitino.exceptions.OptimisticLockException; +import org.apache.gravitino.json.JsonUtils; +import org.apache.gravitino.meta.AuditInfo; +import org.apache.gravitino.meta.PolicyEntity; +import org.apache.gravitino.meta.TagEntity; +import org.apache.gravitino.storage.relational.mapper.PolicyMetaMapper; +import org.apache.gravitino.storage.relational.mapper.PolicyTagRelMapper; +import org.apache.gravitino.storage.relational.mapper.TagMetaMapper; +import org.apache.gravitino.storage.relational.po.PolicyPO; +import org.apache.gravitino.storage.relational.po.PolicyTagRelPO; +import org.apache.gravitino.storage.relational.po.TagPO; +import org.apache.gravitino.storage.relational.utils.SessionUtils; +import org.apache.gravitino.utils.NameIdentifierUtil; +import org.apache.gravitino.utils.PrincipalUtils; + +/** JDBC metadata service for policy-to-tag relations. */ +public class PolicyTagRelService { + + private static final PolicyTagRelService INSTANCE = new PolicyTagRelService(); + + /** + * @return The singleton service instance. + */ + public static PolicyTagRelService getInstance() { + return INSTANCE; + } + + private PolicyTagRelService() {} + + /** + * Lists policy-to-tag relation edges from policy or tag anchors. + * + * @param anchors The policy or tag identifiers to query. + * @param anchorType The entity type of every anchor. + * @return The relation edges, including selector JSON as the relation value. + */ + public List<RelationalEntity<?>> listRelations( + List<NameIdentifier> anchors, Entity.EntityType anchorType) { + if (anchors == null || anchors.isEmpty()) { + return Collections.emptyList(); + } + Preconditions.checkArgument( + anchorType == Entity.EntityType.TAG || anchorType == Entity.EntityType.POLICY, + "Policy-to-tag relations do not support anchor type %s", + anchorType); + validateSameMetalake(anchors); + + String metalake = anchors.get(0).namespace().level(0); + List<String> anchorNames = + anchors.stream().map(NameIdentifier::name).distinct().collect(Collectors.toList()); + List<PolicyTagRelPO> relations = + SessionUtils.getWithoutCommit( + PolicyTagRelMapper.class, + mapper -> + anchorType == Entity.EntityType.TAG + ? mapper.listByTagNames(metalake, anchorNames) + : mapper.listByPolicyNames(metalake, anchorNames)); + if (relations.isEmpty()) { + return Collections.emptyList(); + } + + return anchorType == Entity.EntityType.TAG + ? policyTargets(metalake, relations) + : tagTargets(metalake, relations); + } + + /** + * Creates, replaces, or removes policy-to-tag relations for one tag. + * + * <p>An add for an existing pair replaces its selector. Repeating the same selector and removing + * a missing pair are idempotent no-ops. + * + * @param tagIdentifier The source tag identifier. + * @param targetsToAdd Policy targets to create or replace. + * @param targetsToRemove Policy targets to remove. + * @return All active policy targets for the tag after the update. + * @throws IOException If selector audit information cannot be serialized. + */ + public List<PolicyEntity> updateRelations( + NameIdentifier tagIdentifier, + RelationEdgeTarget[] targetsToAdd, + RelationEdgeTarget[] targetsToRemove) + throws IOException { + NameIdentifierUtil.checkTag(tagIdentifier); + String metalake = tagIdentifier.namespace().level(0); + RelationEdgeTarget[] targetsToAddOrEmpty = nullToEmpty(targetsToAdd); + RelationEdgeTarget[] targetsToRemoveOrEmpty = nullToEmpty(targetsToRemove); + validatePolicyTargets(metalake, targetsToAddOrEmpty); + validatePolicyTargets(metalake, targetsToRemoveOrEmpty); + + List<PolicyEntity> updatedPolicies = new ArrayList<>(); + try { + SessionUtils.doMultipleWithCommit( + () -> { + try { + updatedPolicies.addAll( + updateRelationsWithoutCommit( + tagIdentifier, targetsToAddOrEmpty, targetsToRemoveOrEmpty)); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }); + } catch (UncheckedIOException e) { + throw e.getCause(); + } + return updatedPolicies; + } + + private List<PolicyEntity> updateRelationsWithoutCommit( + NameIdentifier tagIdentifier, + RelationEdgeTarget[] targetsToAdd, + RelationEdgeTarget[] targetsToRemove) + throws IOException { + String metalake = tagIdentifier.namespace().level(0); + TagPO tagPO = lockTag(tagIdentifier); + long tagId = tagPO.getTagId(); + Map<String, Long> policyIds = resolvePolicyIds(metalake, targetsToAdd, targetsToRemove); + + for (RelationEdgeTarget target : targetsToRemove) { + long policyId = policyIds.get(target.nameIdentifier().name()); + PolicyTagRelPO existing = + SessionUtils.getWithoutCommit( + PolicyTagRelMapper.class, mapper -> mapper.getByPolicyIdAndTagId(policyId, tagId)); + if (existing != null) { + int deleted = + SessionUtils.getWithoutCommit( + PolicyTagRelMapper.class, mapper -> mapper.softDeleteByPair(existing)); + if (deleted != 1) { + throw relationConflict(tagIdentifier); + } + } + } + + for (RelationEdgeTarget target : targetsToAdd) { + long policyId = policyIds.get(target.nameIdentifier().name()); + insertIfAbsent(policyId, tagId, target.relationValue().orElse(null)); + } + + return listRelations(Collections.singletonList(tagIdentifier), Entity.EntityType.TAG).stream() + .map(relation -> (PolicyEntity) relation.targetEntity()) + .collect(Collectors.toList()); + } + + private static List<RelationalEntity<?>> policyTargets( + String metalake, List<PolicyTagRelPO> relations) { + Set<String> policyNames = + relations.stream() + .map(PolicyTagRelPO::getPolicyName) + .collect(Collectors.toCollection(LinkedHashSet::new)); + List<NameIdentifier> policyIdentifiers = + policyNames.stream() + .map(name -> NameIdentifierUtil.ofPolicy(metalake, name)) + .collect(Collectors.toList()); + Map<String, PolicyEntity> policies = + PolicyMetaService.getInstance().batchGetPolicyByIdentifier(policyIdentifiers).stream() + .collect(Collectors.toMap(PolicyEntity::name, policy -> policy)); + + List<RelationalEntity<?>> result = new ArrayList<>(); + for (PolicyTagRelPO relation : relations) { + PolicyEntity policy = policies.get(relation.getPolicyName()); + if (policy != null) { + result.add( + new RelationalEntity<>( + SupportsRelationOperations.Type.POLICY_TAG_REL, + NameIdentifierUtil.ofTag(metalake, relation.getTagName()), + Entity.EntityType.TAG, + policy, + relation.getSelector())); + } + } + return result; + } + + private static List<RelationalEntity<?>> tagTargets( + String metalake, List<PolicyTagRelPO> relations) { + Set<String> tagNames = + relations.stream() + .map(PolicyTagRelPO::getTagName) + .collect(Collectors.toCollection(LinkedHashSet::new)); + List<NameIdentifier> tagIdentifiers = + tagNames.stream() + .map(name -> NameIdentifierUtil.ofTag(metalake, name)) + .collect(Collectors.toList()); + Map<String, TagEntity> tags = + TagMetaService.getInstance().batchGetTagByIdentifier(tagIdentifiers).stream() + .collect(Collectors.toMap(TagEntity::name, tag -> tag)); + + List<RelationalEntity<?>> result = new ArrayList<>(); + for (PolicyTagRelPO relation : relations) { + TagEntity tag = tags.get(relation.getTagName()); + if (tag != null) { + result.add( + new RelationalEntity<>( + SupportsRelationOperations.Type.POLICY_TAG_REL, + NameIdentifierUtil.ofPolicy(metalake, relation.getPolicyName()), + Entity.EntityType.POLICY, + tag, + relation.getSelector())); + } + } + return result; + } + + private static void insertIfAbsent(long policyId, long tagId, String selector) + throws IOException { + PolicyTagRelPO existing = Review Comment: Once the Tag lock is replaced by OCC, this read-before-insert path is racy: two transactions can both observe no active row and race into the unique key, exposing a raw duplicate-key failure instead of an idempotent result or an OCC conflict. Please use an atomic insert-if-absent operation with an affected-row result and re-read the winning row. The existing-row case must also define selector semantics: the Javadoc says that add replaces the selector, while this method silently keeps the old selector. Prefer a version-CAS selector update; otherwise document remove-plus-add and reject or report a conflicting selector instead of silently succeeding. ########## core/src/test/java/org/apache/gravitino/storage/relational/service/TestPolicyTagRelService.java: ########## @@ -0,0 +1,529 @@ +/* + * 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.service; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import org.apache.gravitino.Entity; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.RelationEdgeTarget; +import org.apache.gravitino.RelationUpdate; +import org.apache.gravitino.RelationalEntity; +import org.apache.gravitino.SupportsRelationOperations; +import org.apache.gravitino.exceptions.NoSuchEntityException; +import org.apache.gravitino.meta.BaseMetalake; +import org.apache.gravitino.meta.PolicyEntity; +import org.apache.gravitino.meta.TagEntity; +import org.apache.gravitino.storage.RandomIdGenerator; +import org.apache.gravitino.storage.relational.TestJDBCBackend; +import org.apache.gravitino.storage.relational.mapper.PolicyTagRelMapper; +import org.apache.gravitino.storage.relational.po.PolicyTagRelPO; +import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper; +import org.apache.gravitino.storage.relational.utils.SessionUtils; +import org.apache.gravitino.utils.NameIdentifierUtil; +import org.apache.gravitino.utils.NamespaceUtil; +import org.apache.ibatis.session.SqlSession; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.TestTemplate; + +/** Tests policy-to-tag relation persistence and lifecycle behavior. */ +public class TestPolicyTagRelService extends TestJDBCBackend { + + private static final String METALAKE = "policy_tag_relation_metalake"; + private static final String FINANCE_SELECTOR = "{\"type\":\"TAG_VALUE\",\"value\":\"finance\"}"; + private static final String RISK_SELECTOR = "{\"type\":\"TAG_VALUE\",\"value\":\"risk\"}"; + + @TestTemplate + public void testSelectorCreateReplaceBidirectionalReadAndIdempotentDelete() throws IOException { + createAndInsertMakeLake(METALAKE); + TagEntity tag = + TagEntity.builder() + .withId(RandomIdGenerator.INSTANCE.nextId()) + .withName("domain") + .withNamespace(NamespaceUtil.ofTag(METALAKE)) + .withProperties(Collections.emptyMap()) + .withAuditInfo(AUDIT_INFO) + .build(); + PolicyEntity policy = + createPolicy( + RandomIdGenerator.INSTANCE.nextId(), + NamespaceUtil.ofPolicy(METALAKE), + "retention", + AUDIT_INFO); + backend.insert(tag, false); + backend.insert(policy, false); + + RelationEdgeTarget financeTarget = + RelationEdgeTarget.of(policy.nameIdentifier(), Entity.EntityType.POLICY, FINANCE_SELECTOR); + RelationUpdate financeUpdate = + RelationUpdate.of( + SupportsRelationOperations.Type.POLICY_TAG_REL, + tag.nameIdentifier(), + Entity.EntityType.TAG, + new RelationEdgeTarget[] {financeTarget}, + new RelationEdgeTarget[0]); + backend.updateEntityRelations(financeUpdate); + backend.updateEntityRelations(financeUpdate); + + List<RelationalEntity<?>> byTag = + backend.batchListEntitiesByRelation( + SupportsRelationOperations.Type.POLICY_TAG_REL, + Collections.singletonList(tag.nameIdentifier()), + Entity.EntityType.TAG); + Assertions.assertEquals(1, byTag.size()); + Assertions.assertEquals(SupportsRelationOperations.Type.POLICY_TAG_REL, byTag.get(0).type()); + Assertions.assertEquals(tag.nameIdentifier(), byTag.get(0).source()); + Assertions.assertEquals(Entity.EntityType.TAG, byTag.get(0).sourceType()); + Assertions.assertEquals(policy, byTag.get(0).targetEntity()); + Assertions.assertEquals(FINANCE_SELECTOR, byTag.get(0).relationValue().orElse(null)); + Assertions.assertEquals( + policy, + backend.getEntityByRelation( + SupportsRelationOperations.Type.POLICY_TAG_REL, + tag.nameIdentifier(), + Entity.EntityType.TAG, + policy.nameIdentifier())); + + RelationEdgeTarget riskTarget = + RelationEdgeTarget.of(policy.nameIdentifier(), Entity.EntityType.POLICY, RISK_SELECTOR); + backend.updateEntityRelations( + RelationUpdate.of( + SupportsRelationOperations.Type.POLICY_TAG_REL, + tag.nameIdentifier(), + Entity.EntityType.TAG, + new RelationEdgeTarget[] {riskTarget}, + new RelationEdgeTarget[0])); + byTag = + backend.batchListEntitiesByRelation( + SupportsRelationOperations.Type.POLICY_TAG_REL, + Collections.singletonList(tag.nameIdentifier()), + Entity.EntityType.TAG); + Assertions.assertEquals(1, byTag.size()); + Assertions.assertEquals(FINANCE_SELECTOR, byTag.get(0).relationValue().orElse(null)); + + backend.updateEntityRelations( + RelationUpdate.of( + SupportsRelationOperations.Type.POLICY_TAG_REL, + tag.nameIdentifier(), + Entity.EntityType.TAG, + new RelationEdgeTarget[] {riskTarget}, + new RelationEdgeTarget[] {financeTarget})); + List<RelationalEntity<?>> byPolicy = + backend.batchListEntitiesByRelation( + SupportsRelationOperations.Type.POLICY_TAG_REL, + Collections.singletonList(policy.nameIdentifier()), + Entity.EntityType.POLICY); + Assertions.assertEquals(1, byPolicy.size()); + Assertions.assertEquals(policy.nameIdentifier(), byPolicy.get(0).source()); + Assertions.assertEquals(Entity.EntityType.POLICY, byPolicy.get(0).sourceType()); + Assertions.assertEquals(tag, byPolicy.get(0).targetEntity()); + Assertions.assertEquals(RISK_SELECTOR, byPolicy.get(0).relationValue().orElse(null)); + + RelationUpdate removeUpdate = + RelationUpdate.of( + SupportsRelationOperations.Type.POLICY_TAG_REL, + tag.nameIdentifier(), + Entity.EntityType.TAG, + new RelationEdgeTarget[0], + new RelationEdgeTarget[] {riskTarget}); + backend.updateEntityRelations(removeUpdate); + backend.updateEntityRelations(removeUpdate); + Assertions.assertTrue( + backend + .batchListEntitiesByRelation( + SupportsRelationOperations.Type.POLICY_TAG_REL, + Collections.singletonList(NameIdentifierUtil.ofTag(METALAKE, tag.name())), + Entity.EntityType.TAG) + .isEmpty()); + } + + @TestTemplate + public void testBatchListRelationsByMultipleAnchors() throws IOException { + createAndInsertMakeLake(METALAKE); + TagEntity firstTag = createAssociation(METALAKE, "domain_a", "retention_a"); + TagEntity secondTag = createAssociation(METALAKE, "domain_b", "retention_b"); + String otherMetalake = METALAKE + "_other"; + createAndInsertMakeLake(otherMetalake); + createAssociation(otherMetalake, "domain_a", "retention_a"); + + List<RelationalEntity<?>> byTags = + backend.batchListEntitiesByRelation( + SupportsRelationOperations.Type.POLICY_TAG_REL, + Arrays.asList( + firstTag.nameIdentifier(), + secondTag.nameIdentifier(), + NameIdentifierUtil.ofTag(METALAKE, "missing_tag")), + Entity.EntityType.TAG); + Assertions.assertEquals(2, byTags.size()); + Assertions.assertEquals( + Set.of("domain_a", "domain_b"), + byTags.stream().map(relation -> relation.source().name()).collect(Collectors.toSet())); + + List<RelationalEntity<?>> byPolicies = + backend.batchListEntitiesByRelation( + SupportsRelationOperations.Type.POLICY_TAG_REL, + Arrays.asList( + NameIdentifierUtil.ofPolicy(METALAKE, "retention_a"), + NameIdentifierUtil.ofPolicy(METALAKE, "retention_b"), + NameIdentifierUtil.ofPolicy(METALAKE, "missing_policy")), + Entity.EntityType.POLICY); + Assertions.assertEquals(2, byPolicies.size()); + Assertions.assertEquals( + Set.of("retention_a", "retention_b"), + byPolicies.stream().map(relation -> relation.source().name()).collect(Collectors.toSet())); + } + + @TestTemplate + public void testUpdateRelationsRollsBackOnFailure() throws IOException { + createAndInsertMakeLake(METALAKE); + TagEntity tag = createAssociation(METALAKE, "domain", "retention"); + RelationEdgeTarget existingTarget = + RelationEdgeTarget.of( + NameIdentifierUtil.ofPolicy(METALAKE, "retention"), Entity.EntityType.POLICY, null); + RelationEdgeTarget missingTarget = + RelationEdgeTarget.of( + NameIdentifierUtil.ofPolicy(METALAKE, "missing_policy"), + Entity.EntityType.POLICY, + null); + + Assertions.assertThrows( + NoSuchEntityException.class, + () -> + backend.updateEntityRelations( + RelationUpdate.of( + SupportsRelationOperations.Type.POLICY_TAG_REL, + tag.nameIdentifier(), + Entity.EntityType.TAG, + new RelationEdgeTarget[] {missingTarget}, + new RelationEdgeTarget[] {existingTarget}))); + + List<RelationalEntity<?>> relations = + backend.batchListEntitiesByRelation( + SupportsRelationOperations.Type.POLICY_TAG_REL, + Collections.singletonList(tag.nameIdentifier()), + Entity.EntityType.TAG); + Assertions.assertEquals(1, relations.size()); + Assertions.assertEquals("retention", relations.get(0).targetEntity().name()); + } + + @TestTemplate + public void testEntityDeletesCascadePolicyTagRelations() throws IOException { + createAndInsertMakeLake(METALAKE); + TagEntity policyDeletedTag = + createAssociation(METALAKE, "policy_deleted_tag", "deleted_policy"); + backend.delete( + NameIdentifierUtil.ofPolicy(METALAKE, "deleted_policy"), Entity.EntityType.POLICY, false); + Assertions.assertTrue( + backend + .batchListEntitiesByRelation( + SupportsRelationOperations.Type.POLICY_TAG_REL, + Collections.singletonList(policyDeletedTag.nameIdentifier()), + Entity.EntityType.TAG) + .isEmpty()); + + TagEntity deletedTag = createAssociation(METALAKE, "deleted_tag", "surviving_policy"); + backend.delete(deletedTag.nameIdentifier(), Entity.EntityType.TAG, false); + Assertions.assertTrue( + backend + .batchListEntitiesByRelation( + SupportsRelationOperations.Type.POLICY_TAG_REL, + Collections.singletonList( + NameIdentifierUtil.ofPolicy(METALAKE, "surviving_policy")), + Entity.EntityType.POLICY) + .isEmpty()); + } + + @TestTemplate + public void testMetalakeCascadeDoesNotDeleteRelationsFromOtherMetalakes() throws IOException { + BaseMetalake deletedMetalake = createAndInsertMakeLake(METALAKE); + createAssociation(METALAKE, "deleted_domain", "deleted_retention"); + String survivingMetalake = METALAKE + "_surviving"; + createAndInsertMakeLake(survivingMetalake); + TagEntity survivingTag = + createAssociation(survivingMetalake, "surviving_domain", "surviving_retention"); + + backend.delete(deletedMetalake.nameIdentifier(), Entity.EntityType.METALAKE, true); + + List<RelationalEntity<?>> survivingRelations = + backend.batchListEntitiesByRelation( + SupportsRelationOperations.Type.POLICY_TAG_REL, + Collections.singletonList(survivingTag.nameIdentifier()), + Entity.EntityType.TAG); + Assertions.assertEquals(1, survivingRelations.size()); + Assertions.assertEquals("surviving_retention", survivingRelations.get(0).targetEntity().name()); + } + + @TestTemplate + public void testConcurrentIdenticalAddsAreIdempotent() throws Exception { + createAndInsertMakeLake(METALAKE); + RelationEndpoints endpoints = createEndpoints(METALAKE, "domain", "retention"); + RelationUpdate add = relationUpdate(endpoints, null, true); + + runConcurrently(() -> updateRelationsUnchecked(add), () -> updateRelationsUnchecked(add)); Review Comment: This barrier only starts both calls before they contend on the same Tag `FOR UPDATE` lock, so the test serializes the operations and does not exercise database OCC or the read/insert race. Please add deterministic barriers at the actual race boundaries and cover at least: (1) both sessions observe an absent pair before add/add; (2) stale row A cannot delete re-added row B (ABA); (3) relation add versus Tag delete and versus Policy delete, asserting the raw relation table has no active orphan rather than relying on joins that hide deleted endpoints; (4) selector update/update and update/delete; and (5) a multi-target conflict rolls back every relation and endpoint-version change. Run these through independent database sessions on H2, MySQL, and PostgreSQL. -- 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]
