mchades commented on code in PR #10499:
URL: https://github.com/apache/gravitino/pull/10499#discussion_r2979786702


##########
core/src/main/java/org/apache/gravitino/listener/FunctionEventDispatcher.java:
##########
@@ -0,0 +1,189 @@
+/*
+ * 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.Arrays;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.Namespace;
+import org.apache.gravitino.catalog.FunctionDispatcher;
+import org.apache.gravitino.exceptions.FunctionAlreadyExistsException;
+import org.apache.gravitino.exceptions.NoSuchFunctionException;
+import org.apache.gravitino.exceptions.NoSuchSchemaException;
+import org.apache.gravitino.function.Function;
+import org.apache.gravitino.function.FunctionChange;
+import org.apache.gravitino.function.FunctionDefinition;
+import org.apache.gravitino.function.FunctionType;
+import org.apache.gravitino.listener.api.event.function.AlterFunctionEvent;
+import 
org.apache.gravitino.listener.api.event.function.AlterFunctionFailureEvent;
+import org.apache.gravitino.listener.api.event.function.AlterFunctionPreEvent;
+import org.apache.gravitino.listener.api.event.function.DropFunctionEvent;
+import 
org.apache.gravitino.listener.api.event.function.DropFunctionFailureEvent;
+import org.apache.gravitino.listener.api.event.function.DropFunctionPreEvent;
+import org.apache.gravitino.listener.api.event.function.GetFunctionEvent;
+import 
org.apache.gravitino.listener.api.event.function.GetFunctionFailureEvent;
+import org.apache.gravitino.listener.api.event.function.GetFunctionPreEvent;
+import org.apache.gravitino.listener.api.event.function.ListFunctionEvent;
+import 
org.apache.gravitino.listener.api.event.function.ListFunctionFailureEvent;
+import org.apache.gravitino.listener.api.event.function.ListFunctionInfosEvent;
+import org.apache.gravitino.listener.api.event.function.ListFunctionPreEvent;
+import org.apache.gravitino.listener.api.event.function.RegisterFunctionEvent;
+import 
org.apache.gravitino.listener.api.event.function.RegisterFunctionFailureEvent;
+import 
org.apache.gravitino.listener.api.event.function.RegisterFunctionPreEvent;
+import org.apache.gravitino.listener.api.info.FunctionInfo;
+import org.apache.gravitino.utils.PrincipalUtils;
+
+/**
+ * {@code FunctionEventDispatcher} is a decorator for {@link 
FunctionDispatcher} that not only
+ * delegates function operations to the underlying dispatcher but also 
dispatches corresponding
+ * events to an {@link EventBus} after each operation is completed.
+ */
+public class FunctionEventDispatcher implements FunctionDispatcher {
+
+  private final EventBus eventBus;
+  private final FunctionDispatcher dispatcher;
+
+  /**
+   * Constructs a FunctionEventDispatcher with a specified EventBus and 
FunctionDispatcher.
+   *
+   * @param eventBus The EventBus to which events will be dispatched.
+   * @param dispatcher The underlying {@link FunctionDispatcher} that will 
perform the actual
+   *     function operations.
+   */
+  public FunctionEventDispatcher(EventBus eventBus, FunctionDispatcher 
dispatcher) {
+    this.eventBus = eventBus;
+    this.dispatcher = dispatcher;
+  }
+
+  @Override
+  public NameIdentifier[] listFunctions(Namespace namespace) throws 
NoSuchSchemaException {
+    eventBus.dispatchEvent(
+        new ListFunctionPreEvent(PrincipalUtils.getCurrentUserName(), 
namespace));

Review Comment:
   `PrincipalUtils.getCurrentUserName()` is called multiple times within each 
method (for pre, success, and failure events). In `ModelEventDispatcher`, this 
is extracted into a local variable once at the beginning of each method:
   ```java
   String user = PrincipalUtils.getCurrentUserName();
   ```
   Consider following the same pattern here for consistency and clarity. This 
applies to all methods in this dispatcher.



##########
core/src/main/java/org/apache/gravitino/listener/api/event/function/AlterFunctionPreEvent.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.function;
+
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.annotation.DeveloperApi;
+import org.apache.gravitino.function.FunctionChange;
+import org.apache.gravitino.listener.api.event.OperationType;
+
+/** Represents an event triggered before altering a function. */
+@DeveloperApi
+public class AlterFunctionPreEvent extends FunctionPreEvent {
+  private final FunctionChange[] functionChanges;
+
+  public AlterFunctionPreEvent(
+      String user, NameIdentifier identifier, FunctionChange[] 
functionChanges) {
+    super(user, identifier);
+    this.functionChanges = functionChanges;

Review Comment:
   The `functionChanges` array is stored by reference without cloning here, 
while `AlterFunctionEvent` and `AlterFunctionFailureEvent` both use 
`functionChanges.clone()`. Since the pre-event is dispatched *before* the 
delegate call, a listener could potentially mutate the array that is later 
passed to `dispatcher.alterFunction(...)`.
   
   Consider cloning the array for defensive safety:
   ```java
   this.functionChanges = functionChanges.clone();
   ```



##########
core/src/main/java/org/apache/gravitino/listener/api/info/FunctionInfo.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.info;
+
+import java.util.Arrays;
+import javax.annotation.Nullable;
+import org.apache.gravitino.Audit;
+import org.apache.gravitino.annotation.DeveloperApi;
+import org.apache.gravitino.function.Function;
+import org.apache.gravitino.function.FunctionDefinition;
+import org.apache.gravitino.function.FunctionType;
+
+/**
+ * FunctionInfo exposes function information for event listener, it's supposed 
to be read only. Most
+ * of the fields are shallow copied internally not deep copies for performance.
+ */
+@DeveloperApi
+public final class FunctionInfo {
+  private final String name;
+  private final FunctionType functionType;
+  private final boolean deterministic;
+  @Nullable private final String comment;
+  private final FunctionDefinition[] definitions;
+  @Nullable private final Audit auditInfo;
+
+  /**
+   * Constructs a FunctionInfo object from a Function instance.
+   *
+   * @param function The source Function instance.
+   */
+  public FunctionInfo(Function function) {
+    this(
+        function.name(),
+        function.functionType(),
+        function.deterministic(),
+        function.comment(),
+        function.definitions(),
+        function.auditInfo());
+  }
+
+  /**
+   * Constructs a FunctionInfo object with specified details.
+   *
+   * @param name Name of the function.
+   * @param functionType The function type (SCALAR, AGGREGATE, or TABLE).
+   * @param deterministic Whether the function is deterministic.
+   * @param comment Optional comment about the function.
+   * @param definitions The function definitions.
+   * @param auditInfo Optional audit information.
+   */
+  public FunctionInfo(
+      String name,
+      FunctionType functionType,
+      boolean deterministic,
+      String comment,
+      FunctionDefinition[] definitions,
+      Audit auditInfo) {
+    this.name = name;
+    this.functionType = functionType;
+    this.deterministic = deterministic;
+    this.comment = comment;
+    this.definitions =
+        definitions == null
+            ? new FunctionDefinition[0]
+            : Arrays.copyOf(definitions, definitions.length);
+    this.auditInfo = auditInfo;
+  }
+
+  /**
+   * Returns the name of the function.
+   *
+   * @return Function name.
+   */
+  public String name() {
+    return name;
+  }
+
+  /**
+   * Returns the function type.
+   *
+   * @return The function type.
+   */
+  public FunctionType functionType() {
+    return functionType;
+  }
+
+  /**
+   * Returns whether the function is deterministic.
+   *
+   * @return True if the function is deterministic.
+   */
+  public boolean deterministic() {
+    return deterministic;
+  }
+
+  /**
+   * Returns the comment of the function.
+   *
+   * @return Function comment, or {@code null} if not available.
+   */
+  @Nullable
+  public String comment() {
+    return comment;
+  }
+
+  /**
+   * Returns the definitions of the function.
+   *
+   * @return Array of function definitions.
+   */
+  public FunctionDefinition[] definitions() {
+    return definitions;

Review Comment:
   The constructor defensively copies the `definitions` array via 
`Arrays.copyOf()`, but `definitions()` returns the internal array directly. 
This means callers can still mutate the stored array, weakening the read-only 
semantics documented in the class JavaDoc.
   
   Consider returning a copy:
   ```java
   public FunctionDefinition[] definitions() {
     return Arrays.copyOf(definitions, definitions.length);
   }
   ```
   The same applies to the `functionChanges()` getter in `AlterFunctionEvent`, 
`AlterFunctionFailureEvent`, and `AlterFunctionPreEvent`.



##########
core/src/main/java/org/apache/gravitino/listener/api/event/function/AlterFunctionEvent.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.function;
+
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.annotation.DeveloperApi;
+import org.apache.gravitino.function.FunctionChange;
+import org.apache.gravitino.listener.api.event.OperationType;
+import org.apache.gravitino.listener.api.info.FunctionInfo;
+
+/** Represents an event fired when a function is successfully altered. */
+@DeveloperApi
+public final class AlterFunctionEvent extends FunctionEvent {
+  private final FunctionInfo updatedFunctionInfo;
+  private final FunctionChange[] functionChanges;
+
+  /**
+   * Constructs an instance of {@code AlterFunctionEvent}.
+   *
+   * @param user The username of the individual responsible for initiating the 
function alteration.
+   * @param identifier The unique identifier of the altered function.
+   * @param functionChanges An array of {@link FunctionChange} objects 
representing the specific
+   *     changes applied to the function.
+   * @param updatedFunctionInfo The post-alteration state of the function.
+   */
+  public AlterFunctionEvent(
+      String user,
+      NameIdentifier identifier,
+      FunctionChange[] functionChanges,
+      FunctionInfo updatedFunctionInfo) {
+    super(user, identifier);
+    this.functionChanges = functionChanges.clone();

Review Comment:
   nit: `FunctionInfo` uses `Arrays.copyOf()` for defensive copying of arrays, 
while here `clone()` is used. Consider using `Arrays.copyOf(functionChanges, 
functionChanges.length)` for consistency within this PR. Same for 
`AlterFunctionFailureEvent`.



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