mchades commented on code in PR #13458: URL: https://github.com/apache/gravitino/pull/13458#discussion_r4158001283
########## core/src/main/java/org/apache/gravitino/listener/SemanticModelEventDispatcher.java: ########## @@ -0,0 +1,180 @@ +/* + * 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 javax.annotation.Nullable; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.catalog.SemanticModelDispatcher; +import org.apache.gravitino.catalog.SemanticModelNormalizeDispatcher; +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.listener.api.event.semantic.AlterSemanticModelEvent; +import org.apache.gravitino.listener.api.event.semantic.AlterSemanticModelFailureEvent; +import org.apache.gravitino.listener.api.event.semantic.AlterSemanticModelPreEvent; +import org.apache.gravitino.listener.api.event.semantic.CreateSemanticModelEvent; +import org.apache.gravitino.listener.api.event.semantic.CreateSemanticModelFailureEvent; +import org.apache.gravitino.listener.api.event.semantic.CreateSemanticModelPreEvent; +import org.apache.gravitino.listener.api.event.semantic.DropSemanticModelEvent; +import org.apache.gravitino.listener.api.event.semantic.DropSemanticModelFailureEvent; +import org.apache.gravitino.listener.api.event.semantic.DropSemanticModelPreEvent; +import org.apache.gravitino.listener.api.event.semantic.ListSemanticModelEvent; +import org.apache.gravitino.listener.api.event.semantic.ListSemanticModelFailureEvent; +import org.apache.gravitino.listener.api.event.semantic.ListSemanticModelPreEvent; +import org.apache.gravitino.listener.api.event.semantic.LoadSemanticModelEvent; +import org.apache.gravitino.listener.api.event.semantic.LoadSemanticModelFailureEvent; +import org.apache.gravitino.listener.api.event.semantic.LoadSemanticModelPreEvent; +import org.apache.gravitino.listener.api.info.SemanticModelInfo; +import org.apache.gravitino.semantic.SemanticModel; +import org.apache.gravitino.semantic.SemanticModelChange; +import org.apache.gravitino.semantic.SemanticModelDefinition; +import org.apache.gravitino.utils.PrincipalUtils; + +/** + * Decorates a {@link SemanticModelDispatcher} to dispatch pre/post/failure Semantic Model events to + * an {@link EventBus}, mirroring {@link ViewEventDispatcher}. + */ +public class SemanticModelEventDispatcher implements SemanticModelDispatcher { + + private final EventBus eventBus; + private final SemanticModelDispatcher dispatcher; + + /** + * Constructs a {@code SemanticModelEventDispatcher}. + * + * @param eventBus Event bus for listener plugins. + * @param dispatcher Underlying dispatcher, for example a {@link + * SemanticModelNormalizeDispatcher}. + */ + public SemanticModelEventDispatcher(EventBus eventBus, SemanticModelDispatcher dispatcher) { + this.eventBus = eventBus; + this.dispatcher = dispatcher; + } + + @Override + public NameIdentifier[] listSemanticModels(Namespace namespace) throws NoSuchSchemaException { + eventBus.dispatchEvent( + new ListSemanticModelPreEvent(PrincipalUtils.getCurrentUserName(), namespace)); + try { + NameIdentifier[] identifiers = dispatcher.listSemanticModels(namespace); + eventBus.dispatchEvent( + new ListSemanticModelEvent( + PrincipalUtils.getCurrentUserName(), + namespace, + identifiers != null ? identifiers.length : -1)); + return identifiers; + } catch (Exception e) { + eventBus.dispatchEvent( + new ListSemanticModelFailureEvent(PrincipalUtils.getCurrentUserName(), namespace, e)); + throw e; + } + } + + @Override + public SemanticModel loadSemanticModel(NameIdentifier ident) throws NoSuchSemanticModelException { + eventBus.dispatchEvent( + new LoadSemanticModelPreEvent(PrincipalUtils.getCurrentUserName(), ident)); + try { + SemanticModel semanticModel = dispatcher.loadSemanticModel(ident); + eventBus.dispatchEvent( + new LoadSemanticModelEvent( + PrincipalUtils.getCurrentUserName(), ident, new SemanticModelInfo(semanticModel))); + return semanticModel; + } catch (Exception e) { + eventBus.dispatchEvent( + new LoadSemanticModelFailureEvent(PrincipalUtils.getCurrentUserName(), ident, e)); + throw e; + } + } + + @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 { + SemanticModelInfo createRequest = + new SemanticModelInfo(ident.name(), comment, definition, properties, null); + eventBus.dispatchEvent( + new CreateSemanticModelPreEvent(PrincipalUtils.getCurrentUserName(), ident, createRequest)); + try { + SemanticModel semanticModel = + dispatcher.createSemanticModel(ident, comment, definition, properties); + eventBus.dispatchEvent( + new CreateSemanticModelEvent( + PrincipalUtils.getCurrentUserName(), ident, new SemanticModelInfo(semanticModel))); + return semanticModel; + } catch (Exception e) { + eventBus.dispatchEvent( + new CreateSemanticModelFailureEvent( + PrincipalUtils.getCurrentUserName(), ident, e, createRequest)); + throw e; + } + } + + @Override + public SemanticModel alterSemanticModel(NameIdentifier ident, SemanticModelChange... changes) + throws NoSuchSemanticModelException, SemanticModelAlreadyExistsException, + IllegalSemanticModelException { + eventBus.dispatchEvent( + new AlterSemanticModelPreEvent(PrincipalUtils.getCurrentUserName(), ident, changes)); + try { Review Comment: [P2] Preserve validation and failure events for null changes `alterSemanticModel(ident, (SemanticModelChange[]) null)` previously reached the normalizer's `IllegalArgumentException("At least one change is required")`. Now `AlterSemanticModelPreEvent` calls `changes.clone()` before this `try`, so the same call throws `NullPointerException` and emits no failure event. Please make the pre/failure payloads tolerate this input and let validation run inside the failure-dispatch path; `AlterSemanticModelFailureEvent` also clones unconditionally. Add a regression test through the event dispatcher. -- 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]
