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


##########
core/src/test/java/org/apache/gravitino/listener/api/event/TestFunctionEvent.java:
##########
@@ -0,0 +1,305 @@
+/*
+ * 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;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+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.GravitinoRuntimeException;
+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.DummyEventListener;
+import org.apache.gravitino.listener.EventBus;
+import org.apache.gravitino.listener.FunctionEventDispatcher;
+import org.apache.gravitino.listener.api.info.FunctionInfo;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.TestInstance.Lifecycle;
+
+@TestInstance(Lifecycle.PER_CLASS)
+public class TestFunctionEvent {
+  private FunctionEventDispatcher dispatcher;
+  private FunctionEventDispatcher failureDispatcher;
+  private DummyEventListener dummyEventListener;
+  private Function function;
+
+  @BeforeAll
+  void init() {
+    this.function = mockFunction();
+    this.dummyEventListener = new DummyEventListener();
+    EventBus eventBus = new EventBus(Arrays.asList(dummyEventListener));
+    FunctionDispatcher functionDispatcher = mockFunctionDispatcher();
+    this.dispatcher = new FunctionEventDispatcher(eventBus, 
functionDispatcher);
+    FunctionDispatcher functionExceptionDispatcher = 
mockExceptionFunctionDispatcher();
+    this.failureDispatcher = new FunctionEventDispatcher(eventBus, 
functionExceptionDispatcher);
+  }
+
+  @Test
+  void testRegisterFunctionEvent() {
+    NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", 
"schema", "func");
+    dispatcher.registerFunction(
+        identifier,
+        function.comment(),
+        function.functionType(),
+        function.deterministic(),
+        function.definitions());
+
+    Event event = dummyEventListener.popPostEvent();
+    Assertions.assertEquals(identifier, event.identifier());
+    Assertions.assertEquals(RegisterFunctionEvent.class, event.getClass());
+    FunctionInfo functionInfo = ((RegisterFunctionEvent) 
event).registeredFunctionInfo();
+    checkFunctionInfo(functionInfo, function);
+    Assertions.assertEquals(OperationType.REGISTER_FUNCTION, 
event.operationType());
+    Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus());
+
+    PreEvent preEvent = dummyEventListener.popPreEvent();
+    Assertions.assertEquals(identifier, preEvent.identifier());
+    Assertions.assertEquals(RegisterFunctionPreEvent.class, 
preEvent.getClass());
+    FunctionInfo requestInfo = ((RegisterFunctionPreEvent) 
preEvent).registerFunctionRequest();
+    Assertions.assertEquals(function.functionType(), 
requestInfo.functionType());
+    Assertions.assertEquals(function.deterministic(), 
requestInfo.deterministic());
+    Assertions.assertEquals(OperationType.REGISTER_FUNCTION, 
preEvent.operationType());
+    Assertions.assertEquals(OperationStatus.UNPROCESSED, 
preEvent.operationStatus());
+  }
+
+  @Test
+  void testGetFunctionEvent() {
+    NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", 
"schema", "func");
+    dispatcher.getFunction(identifier);
+
+    Event event = dummyEventListener.popPostEvent();
+    Assertions.assertEquals(identifier, event.identifier());
+    Assertions.assertEquals(GetFunctionEvent.class, event.getClass());
+    FunctionInfo functionInfo = ((GetFunctionEvent) event).functionInfo();
+    checkFunctionInfo(functionInfo, function);
+    Assertions.assertEquals(OperationType.GET_FUNCTION, event.operationType());
+    Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus());
+
+    PreEvent preEvent = dummyEventListener.popPreEvent();
+    Assertions.assertEquals(identifier, preEvent.identifier());
+    Assertions.assertEquals(GetFunctionPreEvent.class, preEvent.getClass());
+    Assertions.assertEquals(OperationType.GET_FUNCTION, 
preEvent.operationType());
+    Assertions.assertEquals(OperationStatus.UNPROCESSED, 
preEvent.operationStatus());
+  }
+
+  @Test
+  void testAlterFunctionEvent() {
+    NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", 
"schema", "func");
+    FunctionChange change = FunctionChange.updateComment("new comment");
+    dispatcher.alterFunction(identifier, change);
+
+    Event event = dummyEventListener.popPostEvent();
+    Assertions.assertEquals(identifier, event.identifier());
+    Assertions.assertEquals(AlterFunctionEvent.class, event.getClass());
+    FunctionInfo functionInfo = ((AlterFunctionEvent) 
event).updatedFunctionInfo();
+    checkFunctionInfo(functionInfo, function);
+    Assertions.assertEquals(1, ((AlterFunctionEvent) 
event).functionChanges().length);
+    Assertions.assertEquals(change, ((AlterFunctionEvent) 
event).functionChanges()[0]);
+    Assertions.assertEquals(OperationType.ALTER_FUNCTION, 
event.operationType());
+    Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus());
+
+    PreEvent preEvent = dummyEventListener.popPreEvent();
+    Assertions.assertEquals(identifier, preEvent.identifier());
+    Assertions.assertEquals(AlterFunctionPreEvent.class, preEvent.getClass());
+    Assertions.assertEquals(1, ((AlterFunctionPreEvent) 
preEvent).functionChanges().length);
+    Assertions.assertEquals(change, ((AlterFunctionPreEvent) 
preEvent).functionChanges()[0]);
+    Assertions.assertEquals(OperationType.ALTER_FUNCTION, 
preEvent.operationType());
+    Assertions.assertEquals(OperationStatus.UNPROCESSED, 
preEvent.operationStatus());
+  }
+
+  @Test
+  void testDropFunctionEvent() {
+    NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", 
"schema", "func");
+    dispatcher.dropFunction(identifier);
+
+    Event event = dummyEventListener.popPostEvent();
+    Assertions.assertEquals(identifier, event.identifier());
+    Assertions.assertEquals(DropFunctionEvent.class, event.getClass());
+    Assertions.assertEquals(true, ((DropFunctionEvent) event).isExists());
+    Assertions.assertEquals(OperationType.DROP_FUNCTION, 
event.operationType());
+    Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus());
+
+    PreEvent preEvent = dummyEventListener.popPreEvent();
+    Assertions.assertEquals(identifier, preEvent.identifier());
+    Assertions.assertEquals(DropFunctionPreEvent.class, preEvent.getClass());
+    Assertions.assertEquals(OperationType.DROP_FUNCTION, 
preEvent.operationType());
+    Assertions.assertEquals(OperationStatus.UNPROCESSED, 
preEvent.operationStatus());
+  }
+
+  @Test
+  void testListFunctionEvent() {
+    Namespace namespace = Namespace.of("metalake", "catalog", "schema");
+    dispatcher.listFunctions(namespace);
+
+    Event event = dummyEventListener.popPostEvent();
+    Assertions.assertEquals(namespace.toString(), 
event.identifier().toString());
+    Assertions.assertEquals(ListFunctionEvent.class, event.getClass());
+    Assertions.assertEquals(namespace, ((ListFunctionEvent) 
event).namespace());
+    Assertions.assertEquals(OperationType.LIST_FUNCTION, 
event.operationType());
+    Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus());
+
+    PreEvent preEvent = dummyEventListener.popPreEvent();
+    Assertions.assertEquals(namespace.toString(), 
preEvent.identifier().toString());
+    Assertions.assertEquals(ListFunctionPreEvent.class, preEvent.getClass());
+    Assertions.assertEquals(namespace, ((ListFunctionPreEvent) 
preEvent).namespace());
+    Assertions.assertEquals(OperationType.LIST_FUNCTION, 
preEvent.operationType());
+    Assertions.assertEquals(OperationStatus.UNPROCESSED, 
preEvent.operationStatus());
+  }

Review Comment:
   Tests cover `listFunctions(...)` event dispatching, but there is no coverage 
for `FunctionEventDispatcher.listFunctionInfos(...)` which is also 
event-wrapped in this PR. Add a unit test that calls `listFunctionInfos(...)` 
and asserts the expected pre/post/failure event types and 
`operationType`/`operationStatus` (and that it is distinguishable from 
`listFunctions` if you split the operation types/events).



##########
core/src/main/java/org/apache/gravitino/listener/FunctionEventDispatcher.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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 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.AlterFunctionEvent;
+import org.apache.gravitino.listener.api.event.AlterFunctionFailureEvent;
+import org.apache.gravitino.listener.api.event.AlterFunctionPreEvent;
+import org.apache.gravitino.listener.api.event.DropFunctionEvent;
+import org.apache.gravitino.listener.api.event.DropFunctionFailureEvent;
+import org.apache.gravitino.listener.api.event.DropFunctionPreEvent;
+import org.apache.gravitino.listener.api.event.GetFunctionEvent;
+import org.apache.gravitino.listener.api.event.GetFunctionFailureEvent;
+import org.apache.gravitino.listener.api.event.GetFunctionPreEvent;
+import org.apache.gravitino.listener.api.event.ListFunctionEvent;
+import org.apache.gravitino.listener.api.event.ListFunctionFailureEvent;
+import org.apache.gravitino.listener.api.event.ListFunctionPreEvent;
+import org.apache.gravitino.listener.api.event.RegisterFunctionEvent;
+import org.apache.gravitino.listener.api.event.RegisterFunctionFailureEvent;
+import org.apache.gravitino.listener.api.event.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));
+    try {
+      NameIdentifier[] nameIdentifiers = dispatcher.listFunctions(namespace);
+      eventBus.dispatchEvent(new 
ListFunctionEvent(PrincipalUtils.getCurrentUserName(), namespace));
+      return nameIdentifiers;
+    } catch (Exception e) {
+      eventBus.dispatchEvent(
+          new ListFunctionFailureEvent(PrincipalUtils.getCurrentUserName(), 
namespace, e));
+      throw e;
+    }
+  }
+
+  @Override
+  public Function[] listFunctionInfos(Namespace namespace) throws 
NoSuchSchemaException {
+    eventBus.dispatchEvent(
+        new ListFunctionPreEvent(PrincipalUtils.getCurrentUserName(), 
namespace));
+    try {
+      Function[] functions = dispatcher.listFunctionInfos(namespace);
+      eventBus.dispatchEvent(new 
ListFunctionEvent(PrincipalUtils.getCurrentUserName(), namespace));
+      return functions;
+    } catch (Exception e) {
+      eventBus.dispatchEvent(
+          new ListFunctionFailureEvent(PrincipalUtils.getCurrentUserName(), 
namespace, e));
+      throw e;
+    }

Review Comment:
   `listFunctionInfos()` currently dispatches the same ListFunction* 
events/`OperationType.LIST_FUNCTION` as `listFunctions()`. Since the two APIs 
have different semantics (identifiers-only vs returning full `Function[]`), 
listeners cannot distinguish which operation occurred. Consider introducing a 
dedicated operation type and event classes for `listFunctionInfos` (similar to 
`LIST_POLICY` vs `LIST_POLICY_INFO`, or `LIST_MODEL_VERSIONS` vs 
`LIST_MODEL_VERSION_INFOS`) and dispatch those from this method.



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