yuqi1129 commented on code in PR #12579: URL: https://github.com/apache/gravitino/pull/12579#discussion_r3843626282
########## core/src/main/java/org/apache/gravitino/storage/relational/service/PolicyTagRelService.java: ########## @@ -0,0 +1,367 @@ +/* + * 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.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.PolicyTagRelMapper; +import org.apache.gravitino.storage.relational.po.PolicyTagRelPO; +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 { + List<PolicyEntity> updatedPolicies = new ArrayList<>(); + try { + SessionUtils.doMultipleWithCommit( + () -> { + try { + updatedPolicies.addAll( + updateRelationsWithoutCommit(tagIdentifier, targetsToAdd, targetsToRemove)); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }); + } catch (UncheckedIOException e) { + throw e.getCause(); + } + return updatedPolicies; + } + + /** + * Soft-deletes all policy-to-tag relations for a deleted policy. + * + * @param metalake The metalake name. + * @param policyName The deleted policy name. + * @return The number of deleted relations. + */ + public int deleteRelationsForPolicy(String metalake, String policyName) { + return SessionUtils.doWithCommitAndFetchResult( + PolicyTagRelMapper.class, + mapper -> mapper.softDeleteByMetalakeAndPolicyName(metalake, policyName)); + } + + /** + * Soft-deletes all policy-to-tag relations for a deleted tag. + * + * @param metalake The metalake name. + * @param tagName The deleted tag name. + * @return The number of deleted relations. + */ + public int deleteRelationsForTag(String metalake, String tagName) { + return SessionUtils.doWithCommitAndFetchResult( + PolicyTagRelMapper.class, + mapper -> mapper.softDeleteByMetalakeAndTagName(metalake, tagName)); + } + + /** + * Soft-deletes all policy-to-tag relations for a deleted metalake. + * + * @param metalakeId The deleted metalake ID. + * @return The number of deleted relations. + */ + public int deleteRelationsForMetalake(long metalakeId) { + return SessionUtils.doWithCommitAndFetchResult( + PolicyTagRelMapper.class, mapper -> mapper.softDeleteByMetalakeId(metalakeId)); + } + + /** + * Physically deletes expired policy-to-tag relation rows. + * + * @param legacyTimeline The exclusive deletion timestamp upper bound. + * @param limit The maximum number of rows to delete. + * @return The number of deleted relations. + */ + public int deleteRelationsByLegacyTimeline(long legacyTimeline, int limit) { + return SessionUtils.doWithCommitAndFetchResult( + PolicyTagRelMapper.class, mapper -> mapper.deleteByLegacyTimeline(legacyTimeline, limit)); + } + + private List<PolicyEntity> updateRelationsWithoutCommit( + NameIdentifier tagIdentifier, + RelationEdgeTarget[] targetsToAdd, + RelationEdgeTarget[] targetsToRemove) + throws IOException { + long tagId = EntityIdService.getEntityId(tagIdentifier, Entity.EntityType.TAG); + String metalake = tagIdentifier.namespace().level(0); + RelationEdgeTarget[] targetsToRemoveOrEmpty = nullToEmpty(targetsToRemove); + RelationEdgeTarget[] targetsToAddOrEmpty = nullToEmpty(targetsToAdd); + + Map<String, Long> policyIdsToRemove = resolvePolicyIds(metalake, targetsToRemoveOrEmpty); + for (RelationEdgeTarget target : targetsToRemoveOrEmpty) { + long policyId = policyIdsToRemove.get(target.nameIdentifier().name()); + SessionUtils.doWithoutCommit( + PolicyTagRelMapper.class, mapper -> mapper.softDeleteByPair(policyId, tagId)); + } + + Map<String, Long> policyIdsToAdd = resolvePolicyIds(metalake, targetsToAddOrEmpty); + for (RelationEdgeTarget target : targetsToAddOrEmpty) { Review Comment: I'm working on the OCC refactor, and we need to use version and database lock mechanism to keep the method atomic. -- 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]
