Copilot commented on code in PR #13458:
URL: https://github.com/apache/gravitino/pull/13458#discussion_r4231639999


##########
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;
+    }
+  }

Review Comment:
   `ListSemanticModelEvent.resultCount()` can become `-1` when the underlying 
dispatcher returns `null`. A negative count is likely to violate listener 
expectations for `ListEvent` (count should be >= 0) and can break downstream 
audit/metrics. Prefer normalizing `null` to an empty array (and count `0`), or 
at minimum emit `0` while leaving the return value unchanged.



##########
core/src/test/java/org/apache/gravitino/audit/TestOperation.java:
##########
@@ -348,6 +381,19 @@ public void testAlterOperation() {
         new AlterViewFailureEvent(USER, viewIdentifier, new Exception(), new 
ViewChange[] {});
     Assertions.assertEquals(
         AuditLog.Operation.ALTER_VIEW, 
AuditLog.Operation.fromEvent(alterViewFailureEvent));
+
+    Event alterSemanticModelEvent =
+        new AlterSemanticModelEvent(
+            USER, semanticModelIdentifier, new SemanticModelChange[] {}, 
semanticModelInfo);
+    Assertions.assertEquals(
+        AuditLog.Operation.ALTER_SEMANTIC_MODEL,
+        AuditLog.Operation.fromEvent(alterSemanticModelEvent));
+    Event alterSemanticModelFailureEvent =
+        new AlterSemanticModelFailureEvent(
+            USER, semanticModelIdentifier, new Exception(), new 
SemanticModelChange[] {});

Review Comment:
   These tests construct alter events with an empty `SemanticModelChange[]`, 
but the production alter path appears to validate that at least one change is 
required (and may fail before success events are produced). Using a non-empty 
change array here would better reflect realistic events and reduce the chance 
of future confusion if event constructors later enforce invariants.



##########
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;
+    }

Review Comment:
   `PrincipalUtils.getCurrentUserName()` is called multiple times within a 
single operation, which can lead to inconsistencies if the principal context 
changes mid-call and does redundant work. Capture the username once at method 
entry (e.g., `String user = ...`) and reuse it for pre/success/failure events.



##########
core/src/test/java/org/apache/gravitino/audit/TestOperation.java:
##########
@@ -348,6 +381,19 @@ public void testAlterOperation() {
         new AlterViewFailureEvent(USER, viewIdentifier, new Exception(), new 
ViewChange[] {});
     Assertions.assertEquals(
         AuditLog.Operation.ALTER_VIEW, 
AuditLog.Operation.fromEvent(alterViewFailureEvent));
+
+    Event alterSemanticModelEvent =
+        new AlterSemanticModelEvent(
+            USER, semanticModelIdentifier, new SemanticModelChange[] {}, 
semanticModelInfo);
+    Assertions.assertEquals(
+        AuditLog.Operation.ALTER_SEMANTIC_MODEL,
+        AuditLog.Operation.fromEvent(alterSemanticModelEvent));
+    Event alterSemanticModelFailureEvent =
+        new AlterSemanticModelFailureEvent(
+            USER, semanticModelIdentifier, new Exception(), new 
SemanticModelChange[] {});

Review Comment:
   These tests construct alter events with an empty `SemanticModelChange[]`, 
but the production alter path appears to validate that at least one change is 
required (and may fail before success events are produced). Using a non-empty 
change array here would better reflect realistic events and reduce the chance 
of future confusion if event constructors later enforce invariants.



##########
core/src/main/java/org/apache/gravitino/listener/api/event/semantic/CreateSemanticModelPreEvent.java:
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.api.event.semantic;
+
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.annotation.DeveloperApi;
+import org.apache.gravitino.listener.api.event.OperationType;
+import org.apache.gravitino.listener.api.info.SemanticModelInfo;
+
+/** Pre-event before creating a Semantic Model. */
+@DeveloperApi
+public class CreateSemanticModelPreEvent extends SemanticModelPreEvent {

Review Comment:
   Several newly added pre-event classes are non-`final` while the 
corresponding success/failure event classes are `final` (e.g., 
`CreateSemanticModelEvent`, `CreateSemanticModelFailureEvent`). If these events 
are not intended for extension (typical for event payload types), mark 
pre-event classes `final` as well to avoid API surface for subclassing and 
reduce compatibility constraints.



-- 
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]

Reply via email to