Copilot commented on code in PR #12867: URL: https://github.com/apache/gravitino/pull/12867#discussion_r4130553853
########## core/src/main/java/org/apache/gravitino/hook/SemanticModelHookDispatcher.java: ########## @@ -0,0 +1,126 @@ +/* + * 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.hook; + +import java.util.Map; +import java.util.function.Supplier; +import javax.annotation.Nullable; +import org.apache.gravitino.Entity; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.authorization.AuthorizationUtils; +import org.apache.gravitino.authorization.Owner; +import org.apache.gravitino.authorization.OwnerDispatcher; +import org.apache.gravitino.catalog.SemanticModelDispatcher; +import org.apache.gravitino.exceptions.IllegalSemanticModelException; +import org.apache.gravitino.exceptions.NoSuchSchemaException; +import org.apache.gravitino.exceptions.NoSuchSemanticModelException; +import org.apache.gravitino.exceptions.SemanticModelAlreadyExistsException; +import org.apache.gravitino.semantic.SemanticModel; +import org.apache.gravitino.semantic.SemanticModelChange; +import org.apache.gravitino.semantic.SemanticModelDefinition; +import org.apache.gravitino.utils.NameIdentifierUtil; +import org.apache.gravitino.utils.PrincipalUtils; + +/** + * {@code SemanticModelHookDispatcher} is a decorator for {@link SemanticModelDispatcher} that not + * only delegates Semantic Model operations to the underlying dispatcher but also executes some hook + * operations before or after the underlying operations. + */ +public class SemanticModelHookDispatcher implements SemanticModelDispatcher { + + private final SemanticModelDispatcher dispatcher; + private final Supplier<OwnerDispatcher> ownerDispatcher; + + /** + * Creates a Semantic Model hook dispatcher. + * + * @param dispatcher The underlying dispatcher. + * @param ownerDispatcher Supplies the owner dispatcher, or null when authorization is disabled. + */ + public SemanticModelHookDispatcher( + SemanticModelDispatcher dispatcher, Supplier<OwnerDispatcher> ownerDispatcher) { + this.dispatcher = dispatcher; + this.ownerDispatcher = ownerDispatcher; + } + + @Override + public NameIdentifier[] listSemanticModels(Namespace namespace) throws NoSuchSchemaException { + return dispatcher.listSemanticModels(namespace); + } + + @Override + public SemanticModel loadSemanticModel(NameIdentifier ident) throws NoSuchSemanticModelException { + return dispatcher.loadSemanticModel(ident); + } + + @Override + public boolean semanticModelExists(NameIdentifier ident) { + return dispatcher.semanticModelExists(ident); + } + + @Override + public SemanticModel createSemanticModel( + NameIdentifier ident, + @Nullable String comment, + SemanticModelDefinition definition, + Map<String, String> properties) + throws NoSuchSchemaException, SemanticModelAlreadyExistsException, + IllegalSemanticModelException { + SemanticModel semanticModel = + dispatcher.createSemanticModel(ident, comment, definition, properties); + + // Set the creator as the owner of the Semantic Model. + OwnerDispatcher ownerManager = ownerDispatcher.get(); + if (ownerManager != null) { + ownerManager.setOwner( + ident.namespace().level(0), + NameIdentifierUtil.toMetadataObject(ident, Entity.EntityType.SEMANTIC_MODEL), + PrincipalUtils.getCurrentUserName(), + Owner.Type.USER); + } + return semanticModel; + } + + @Override + public SemanticModel alterSemanticModel(NameIdentifier ident, SemanticModelChange... changes) + throws NoSuchSemanticModelException, SemanticModelAlreadyExistsException, + IllegalSemanticModelException { + SemanticModel model = dispatcher.alterSemanticModel(ident, changes); + for (SemanticModelChange change : changes) { + if (change instanceof SemanticModelChange.RenameSemanticModel) { + AuthorizationUtils.notifyEntityNameIdMappingChange(ident, Entity.EntityType.SEMANTIC_MODEL); + AuthorizationUtils.notifyEntityNameIdMappingChange( + NameIdentifier.of(ident.namespace(), model.name()), Entity.EntityType.SEMANTIC_MODEL); Review Comment: The rename/drop notifications use the normalized `ident`, but `JcasbinAuthorizationLookups` builds metadata-cache keys from the raw `MetadataObject.fullName()` before `MetadataIdConverter` normalizes the case-insensitive parent. A request such as `catalog.SCHEMA.SalesModel` can therefore leave a `...SCHEMA...SalesModel` entry behind when the hook invalidates `...schema...SalesModel`, allowing the old ID to be reused after rename/drop. Invalidate using the same canonical parent-case form used for lookup (or invalidate all equivalent parent-case keys), and add a regression covering mixed-case parent names. -- 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]
