roryqi commented on code in PR #12579: URL: https://github.com/apache/gravitino/pull/12579#discussion_r3870691322
########## 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 remains intentionally outside this PR based on the scope discussion. This PR keeps the existing Tag-row serialization and adds relation-row OCC only. Tag/Policy endpoint lifecycle fencing will be integrated with the entity OCC work in a follow-up; this PR does not increment or repurpose the current Policy current_version. -- 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]
