Abyss-lord commented on code in PR #6250: URL: https://github.com/apache/gravitino/pull/6250#discussion_r1968698645
########## core/src/main/java/org/apache/gravitino/listener/ModelEventDispatcher.java: ########## @@ -0,0 +1,377 @@ +/* + * 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.listener; + +import java.util.Map; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.catalog.ModelDispatcher; +import org.apache.gravitino.exceptions.ModelAlreadyExistsException; +import org.apache.gravitino.exceptions.ModelVersionAliasesAlreadyExistException; +import org.apache.gravitino.exceptions.NoSuchModelException; +import org.apache.gravitino.exceptions.NoSuchModelVersionException; +import org.apache.gravitino.exceptions.NoSuchSchemaException; +import org.apache.gravitino.listener.api.event.DeleteModelPreEvent; +import org.apache.gravitino.listener.api.event.DeleteModelVersionPreEvent; +import org.apache.gravitino.listener.api.event.GetModelPreEvent; +import org.apache.gravitino.listener.api.event.GetModelVersionPreEvent; +import org.apache.gravitino.listener.api.event.LinkModelVersionPreEvent; +import org.apache.gravitino.listener.api.event.ListModelPreEvent; +import org.apache.gravitino.listener.api.event.ListModelVersionsPreEvent; +import org.apache.gravitino.listener.api.event.RegisterModelPreEvent; +import org.apache.gravitino.listener.api.info.ModelInfo; +import org.apache.gravitino.listener.api.info.ModelVersionInfo; +import org.apache.gravitino.model.Model; +import org.apache.gravitino.model.ModelCatalog; +import org.apache.gravitino.model.ModelVersion; +import org.apache.gravitino.utils.PrincipalUtils; + +/** + * {@code ModelEventDispatcher} is a decorator for {@link ModelDispatcher} that not only delegates + * model operations to the underlying catalog dispatcher but also dispatches corresponding events to + * an {@link org.apache.gravitino.listener.EventBus} after each operation is completed. This allows + * for event-driven workflows or monitoring of model operations. + */ +public class ModelEventDispatcher implements ModelDispatcher { + private final EventBus eventBus; + private final ModelDispatcher dispatcher; + + /** + * Constructs a {@link ModelEventDispatcher} with a specified EventBus and {@link + * ModelDispatcher}. + * + * @param eventBus The EventBus to which events will be dispatched. + * @param dispatcher The underlying {@link ModelDispatcher} that will perform the actual model + * operations. + */ + public ModelEventDispatcher(EventBus eventBus, ModelDispatcher dispatcher) { + this.eventBus = eventBus; + this.dispatcher = dispatcher; + } + + /** + * Register a model in the catalog if the model is not existed, otherwise the {@link + * ModelAlreadyExistsException} will be thrown. The {@link Model} object will be created when the + * model is registered, users can call {@link ModelCatalog#linkModelVersion(NameIdentifier, + * String, String[], String, Map)} to link the model version to the registered {@link Model}. + * + * @param ident The name identifier of the model. + * @param comment The comment of the model. The comment is optional and can be null. + * @param properties The properties of the model. The properties are optional and can be null or + * empty. + * @return The registered model object. + * @throws NoSuchSchemaException If the schema does not exist. + * @throws ModelAlreadyExistsException If the model already registered. + */ + @Override + public Model registerModel(NameIdentifier ident, String comment, Map<String, String> properties) + throws NoSuchSchemaException, ModelAlreadyExistsException { + ModelInfo registerRequest = new ModelInfo(ident.name(), properties, comment, null); + eventBus.dispatchEvent( + new RegisterModelPreEvent(PrincipalUtils.getCurrentUserName(), ident, registerRequest)); + try { + Model model = dispatcher.registerModel(ident, comment, properties); + // TODO: ModelEvent + return model; + } catch (Exception e) { + // TODO: failureEvent + throw e; + } + } + + /** + * Register a model in the catalog if the model is not existed, otherwise the {@link + * ModelAlreadyExistsException} will be thrown. The {@link Model} object will be created when the + * model is registered, in the meantime, the model version (version 0) will also be created and + * linked to the registered model. + * + * @param ident The name identifier of the model. + * @param uri The model artifact URI. + * @param aliases The aliases of the model version. The aliases should be unique in this model, + * otherwise the {@link ModelVersionAliasesAlreadyExistException} will be thrown. The aliases + * are optional and can be empty. Also, be aware that the alias cannot be a number or a number + * string. + * @param comment The comment of the model. The comment is optional and can be null. + * @param properties The properties of the model. The properties are optional and can be null or + * empty. + * @return The registered model object. + * @throws NoSuchSchemaException If the schema does not exist when register a model. + * @throws ModelAlreadyExistsException If the model already registered. + * @throws ModelVersionAliasesAlreadyExistException If the aliases already exist in the model. + */ + @Override + public Model registerModel( + NameIdentifier ident, + String uri, + String[] aliases, + String comment, + Map<String, String> properties) + throws NoSuchSchemaException, ModelAlreadyExistsException, + ModelVersionAliasesAlreadyExistException { + return dispatcher.registerModel(ident, uri, aliases, comment, properties); Review Comment: @FANNG1 we can't get the version and alias information without loading the Model. -- 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: commits-unsubscr...@gravitino.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org