roryqi commented on code in PR #14326: URL: https://github.com/apache/gravitino/pull/14326#discussion_r4509190423
########## core/src/main/java/org/apache/gravitino/policy/HiddenPolicyTag.java: ########## @@ -0,0 +1,74 @@ +/* + * 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.policy; + +import java.util.Collections; +import java.util.Locale; +import javax.annotation.Nullable; +import org.apache.gravitino.meta.PolicyEntity; +import org.apache.gravitino.meta.TagEntity; +import org.apache.gravitino.utils.NamespaceUtil; + +/** Naming and visibility checks for server-managed policy tags. */ +public final class HiddenPolicyTag { + + private static final String PREFIX = "gravitino_hidden_"; + private static final String POLICY_ID_PROPERTY = "gravitino.internal.policy-id"; + + private HiddenPolicyTag() {} + + /** + * Builds the server-managed tag for a policy using Gravitino's tag entity model. + * + * @param policy The policy that owns the hidden tag. + * @param tagId The generated tag entity ID. + * @return An unassigned, value-less tag with the policy's namespace and audit information. + */ + public static TagEntity create(PolicyEntity policy, long tagId) { + return TagEntity.builder() + .withId(tagId) + .withName(name(policy.id())) + .withNamespace(NamespaceUtil.ofTag(policy.namespace().level(0))) + .withComment("Server-managed policy binding") + .withProperties(Collections.singletonMap(POLICY_ID_PROPERTY, Long.toString(policy.id()))) + .withAllowedValues(new String[0]) + .withAuditInfo(policy.auditInfo()) + .build(); + } + + /** + * Returns a stable tag name that survives policy renames. + * + * @param policyId The policy entity ID. + * @return The reserved tag name. + */ + public static String name(long policyId) { + return PREFIX + policyId; + } + + /** + * Returns whether a tag name belongs to the reserved internal namespace. + * + * @param name The tag name. + * @return Whether the name is reserved. + */ + public static boolean isReservedName(@Nullable String name) { + return name != null && name.toLowerCase(Locale.ROOT).startsWith(PREFIX); Review Comment: Covered by the [earlier migration-preflight reply](https://github.com/apache/gravitino/pull/14326#discussion_r4509093394). The offline migration tool (#14375) can detect existing user tags in this namespace before upgraded servers start, allowing collisions to be resolved during migration. We will keep the current prefix convention. ########## core/src/main/java/org/apache/gravitino/policy/PolicyManager.java: ########## @@ -73,6 +78,54 @@ public PolicyManager(IdGenerator idGenerator, EntityStore entityStore) { this.objectPolicyResolver = new ObjectPolicyResolver(entityStore); } + /** + * Applies a policy to an object through its hidden tag using the entity store. + * + * <p>The hidden tag is created with the policy. This method loads that tag and submits an + * idempotent object assignment. The relation operation validates the object endpoint and owns the + * transaction. + * + * <p>This internal method is not exposed through the policy dispatcher or client APIs. + * + * @param metalake The policy and object's metalake. + * @param policyName The policy name. + * @param object The metadata object to associate. + * @param auditInfo The audit information to preserve on newly created relations. + * @return The policy's hidden tag ID. + * @throws IOException If an entity or relation cannot be persisted. + * @throws NoSuchEntityException If the policy, hidden tag or target object does not exist. + * @throws IllegalArgumentException If the object type does not support tag assignments. + */ + public long addPolicyToObject( + String metalake, String policyName, MetadataObject object, Audit auditInfo) + throws IOException { + Preconditions.checkNotNull(auditInfo, "Policy binding audit information is required"); + Preconditions.checkArgument( + SUPPORTED_METADATA_OBJECT_TYPES_FOR_TAGS.contains(object.type()), + "Unsupported policy binding object type: %s", + object.type()); + checkMetalake(NameIdentifier.of(metalake), entityStore); + + NameIdentifier policyIdent = NameIdentifierUtil.ofPolicy(metalake, policyName); + PolicyEntity policy = + entityStore.get(policyIdent, Entity.EntityType.POLICY, PolicyEntity.class); + NameIdentifier tagIdent = NameIdentifierUtil.ofTag(metalake, HiddenPolicyTag.name(policy.id())); + TagEntity tag = entityStore.get(tagIdent, Entity.EntityType.TAG, TagEntity.class); Review Comment: Please see the [existing reply on initialization](https://github.com/apache/gravitino/pull/14326#discussion_r4509093278). Detection and initialization of pre-existing policies belong to the migration step in #14375. This internal helper expects the hidden tag and policy-tag relation to have been prepared before assigning the policy to an object. ########## core/src/main/java/org/apache/gravitino/storage/relational/service/PolicyMetaService.java: ########## @@ -434,6 +450,32 @@ private void insertPolicyVersionIfAllocated(PolicyPO oldPolicyPO, PolicyPO newPo mapper -> mapper.insertPolicyVersion(newPolicyPO.getPolicyVersionPO())); } + private void createHiddenTagWithoutCommit(PolicyEntity policy, PolicyPO policyPO) { Review Comment: I checked the existing storage semantics: H2, MySQL and PostgreSQL all use a tag-name unique key over `(metalake_id, tag_name, deleted_at)`. Active rows have `deleted_at=0`, so a soft-deleted row does not conflict with a new active row solely because their names match. Overwriting an active policy preserves its existing hidden tag; overwrite with a deleted policy ID is rejected, and recreation through `PolicyManager` allocates a new policy ID and therefore a new hidden-tag name. This PR reuses `TagMetaService` for deletion. Changes to the generic soft-delete/recreate mechanism are outside this PR's scope; any separate storage-layer issue should be tracked independently. ########## core/src/main/java/org/apache/gravitino/storage/relational/service/PolicyMetaService.java: ########## @@ -129,6 +140,8 @@ public void insertPolicy(PolicyEntity policyEntity, boolean overwritten) throws SessionUtils.doMultipleWithCommit( () -> lockMetalakeForPolicyCreate(metalakePO), () -> insertPolicyWithoutCommit(policyEntity, policyPO, overwritten)); + } catch (UncheckedIOException e) { + throw e.getCause(); Review Comment: `insertPolicy` explicitly declares `throws IOException`; its callers handle or declare that checked exception. `SessionUtils.doMultipleWithCommit` rolls back before control reaches the catch that unwraps `UncheckedIOException`. For `overwritten=true`, an existing policy is updated without creating a hidden tag. When no policy exists, the creation branch uses the same hidden-tag helper inside that outer transaction and exception handler. The [new regression test](https://github.com/apache/gravitino/pull/14326#discussion_r4509093553) covers an `IOException` injected after a real tag insert, checking exact exception propagation and rollback. Name conflicts are separate from I/O failures. Migration preflight can detect pre-existing namespace collisions, and we consider the remaining collision risk low under the reserved-name convention. No additional collision-specific exception branch is needed here. -- 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]
