FANNG1 commented on code in PR #4102: URL: https://github.com/apache/gravitino/pull/4102#discussion_r1673275867
########## core/src/main/java/com/datastrato/gravitino/listener/api/info/PartitionInfo.java: ########## @@ -0,0 +1,74 @@ +/* + * 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 com.datastrato.gravitino.listener.api.info; + +import com.datastrato.gravitino.annotation.DeveloperApi; +import com.datastrato.gravitino.rel.partitions.Partition; +import com.google.common.collect.ImmutableMap; +import java.util.Map; + +/** + * Provides access to metadata about a Partition instance, designed for use by event listeners. This + * class encapsulates the essential attributes of a Partition, including its name and properties + * information. + */ +@DeveloperApi +public final class PartitionInfo { + private final String name; + private final Map<String, String> properties; + + /** + * Constructs PartitionInfo from an existing Partition object. + * + * @param partition The Partition instance to extract information from. + */ + public PartitionInfo(Partition partition) { Review Comment: Partition could be `ListPartition` `RangePartition` and `IdentityPartition`, seems we should create corresponding ParititionInfo to represent different partitions. ########## core/src/test/java/com/datastrato/gravitino/listener/api/event/TestPartitionEvent.java: ########## @@ -0,0 +1,235 @@ +/* + * 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 com.datastrato.gravitino.listener.api.event; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.datastrato.gravitino.NameIdentifier; +import com.datastrato.gravitino.catalog.PartitionDispatcher; +import com.datastrato.gravitino.exceptions.GravitinoRuntimeException; +import com.datastrato.gravitino.exceptions.NoSuchPartitionException; +import com.datastrato.gravitino.listener.DummyEventListener; +import com.datastrato.gravitino.listener.EventBus; +import com.datastrato.gravitino.listener.PartitionEventDispatcher; +import com.datastrato.gravitino.listener.api.info.PartitionInfo; +import com.datastrato.gravitino.rel.partitions.Partition; +import com.google.common.collect.ImmutableMap; +import java.util.Arrays; +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 TestPartitionEvent { + private PartitionDispatcher dispatcher; + private PartitionDispatcher failureDispatcher; + private DummyEventListener dummyEventListener; + private Partition partition; + + @BeforeAll + void init() { + this.partition = mockPartition(); + this.dummyEventListener = new DummyEventListener(); + EventBus eventBus = new EventBus(Arrays.asList(dummyEventListener)); + PartitionDispatcher partitionDispatcher = mockPartitionDispatcher(); + this.dispatcher = new PartitionEventDispatcher(eventBus, partitionDispatcher); + PartitionDispatcher partitionExceptionDispatcher = mockExceptionPartitionDispatcher(); + this.failureDispatcher = new PartitionEventDispatcher(eventBus, partitionExceptionDispatcher); + } + + @Test + void testAddPartitionEvent() { + NameIdentifier identifier = NameIdentifier.of("metalake", partition.name()); + dispatcher.addPartition(identifier, partition); + Event event = dummyEventListener.popEvent(); + Assertions.assertEquals(identifier, event.identifier()); + Assertions.assertEquals(AddPartitionEvent.class, event.getClass()); + PartitionInfo partitionInfo = ((AddPartitionEvent) event).addPartitionRequest(); + checkPartitionInfo(partitionInfo, partition); + } + + @Test + void testDropPartitionEvent() { + NameIdentifier identifier = NameIdentifier.of("metalake", partition.name()); + dispatcher.dropPartition(identifier, partition.name()); + Event event = dummyEventListener.popEvent(); + Assertions.assertEquals(identifier, event.identifier()); + Assertions.assertEquals(DropPartitionEvent.class, event.getClass()); + Assertions.assertEquals(true, ((DropPartitionEvent) event).isExists()); + } + + @Test + void testExistPartitionEvent() { + NameIdentifier identifier = NameIdentifier.of("metalake", partition.name()); + dispatcher.partitionExists(identifier, partition.name()); + Event event = dummyEventListener.popEvent(); + Assertions.assertEquals(identifier, event.identifier()); + Assertions.assertEquals(ExistPartitionEvent.class, event.getClass()); + Assertions.assertEquals(true, ((ExistPartitionEvent) event).isExists()); + } + + @Test + void testListPartitionEvent() { + NameIdentifier identifier = NameIdentifier.of("metalake", partition.name()); + dispatcher.listPartitions(identifier); + Event event = dummyEventListener.popEvent(); + Assertions.assertEquals(identifier, event.identifier()); + Assertions.assertEquals(ListPartitionEvent.class, event.getClass()); + Assertions.assertEquals(identifier, ((ListPartitionEvent) event).identifier()); + } + + @Test + void testListPartitionNamesEvent() { + NameIdentifier identifier = NameIdentifier.of("metalake", partition.name()); + dispatcher.listPartitionNames(identifier); + Event event = dummyEventListener.popEvent(); + Assertions.assertEquals(identifier, event.identifier()); + Assertions.assertEquals(ListPartitionNamesEvent.class, event.getClass()); + Assertions.assertEquals(identifier, ((ListPartitionEvent) event).identifier()); + } + + @Test + void testPurgePartitionEvent() { + NameIdentifier identifier = NameIdentifier.of("metalake", partition.name()); + dispatcher.purgePartition(identifier, partition.name()); + Event event = dummyEventListener.popEvent(); + Assertions.assertEquals(identifier, event.identifier()); + Assertions.assertEquals(PurgePartitionEvent.class, event.getClass()); + Assertions.assertEquals(identifier, ((PurgePartitionEvent) event).identifier()); + } + + @Test + void testAddPartitionFailureEvent() { + NameIdentifier identifier = NameIdentifier.of("metalake", partition.name()); + Assertions.assertThrowsExactly( + GravitinoRuntimeException.class, + () -> failureDispatcher.addPartition(identifier, partition)); + Event event = dummyEventListener.popEvent(); + Assertions.assertEquals(AddPartitionFailureEvent.class, event.getClass()); + Assertions.assertEquals( + GravitinoRuntimeException.class, ((AddPartitionFailureEvent) event).exception().getClass()); + checkPartitionInfo(((AddPartitionFailureEvent) event).addPartitionRequest(), partition); + Assertions.assertEquals(identifier, event.identifier()); + } + + @Test + void testDropPartitionFailureEvent() { + NameIdentifier identifier = NameIdentifier.of("metalake", "partition"); + Assertions.assertThrowsExactly( + GravitinoRuntimeException.class, + () -> failureDispatcher.dropPartition(identifier, partition.name())); + Event event = dummyEventListener.popEvent(); + Assertions.assertEquals(DropPartitionFailureEvent.class, event.getClass()); + Assertions.assertEquals( + GravitinoRuntimeException.class, + ((DropPartitionFailureEvent) event).exception().getClass()); + Assertions.assertEquals(identifier, event.identifier()); + } + + @Test + void testExistPartitionFailureEvent() { + NameIdentifier identifier = NameIdentifier.of("metalake", "partition"); + Assertions.assertThrowsExactly( + GravitinoRuntimeException.class, + () -> failureDispatcher.partitionExists(identifier, partition.name())); + Event event = dummyEventListener.popEvent(); + Assertions.assertEquals(ExistPartitionFailureEvent.class, event.getClass()); + Assertions.assertEquals( + GravitinoRuntimeException.class, + ((ExistPartitionFailureEvent) event).exception().getClass()); + Assertions.assertEquals(identifier, event.identifier()); + } + + @Test + void testListPartitionFailureEvent() { + NameIdentifier identifier = NameIdentifier.of("metalake", "partition"); + Assertions.assertThrowsExactly( + GravitinoRuntimeException.class, () -> failureDispatcher.listPartitions(identifier)); + Event event = dummyEventListener.popEvent(); + Assertions.assertEquals(ListPartitionFailureEvent.class, event.getClass()); + Assertions.assertEquals( + GravitinoRuntimeException.class, + ((ListPartitionFailureEvent) event).exception().getClass()); + Assertions.assertEquals(identifier, ((ListPartitionFailureEvent) event).identifier()); + } + + @Test + void testListPartitionNamesFailureEvent() { + NameIdentifier identifier = NameIdentifier.of("metalake", "partition"); + Assertions.assertThrowsExactly( + GravitinoRuntimeException.class, () -> failureDispatcher.listPartitionNames(identifier)); + Event event = dummyEventListener.popEvent(); + Assertions.assertEquals(ListPartitionNamesFailureEvent.class, event.getClass()); + Assertions.assertEquals( + GravitinoRuntimeException.class, + ((ListPartitionNamesFailureEvent) event).exception().getClass()); + Assertions.assertEquals(identifier, ((ListPartitionNamesFailureEvent) event).identifier()); + } + + @Test + void testPurgePartitionFailureEvent() { + NameIdentifier identifier = NameIdentifier.of("metalake", "partition"); + Assertions.assertThrowsExactly( + GravitinoRuntimeException.class, + () -> failureDispatcher.purgePartition(identifier, partition.name())); + Event event = dummyEventListener.popEvent(); + Assertions.assertEquals(PurgePartitionFailureEvent.class, event.getClass()); + Assertions.assertEquals( + GravitinoRuntimeException.class, + ((PurgePartitionFailureEvent) event).exception().getClass()); + Assertions.assertEquals(identifier, event.identifier()); + } + + private void checkPartitionInfo(PartitionInfo partitionInfo, Partition partition) { + Assertions.assertEquals(partition.name(), partitionInfo.name()); + Assertions.assertEquals(partition.properties(), partitionInfo.properties()); + } + + private Partition mockPartition() { + Partition partition = mock(Partition.class); + when(partition.properties()).thenReturn(ImmutableMap.of("a", "b")); + when(partition.name()).thenReturn("partition"); + return partition; + } + + private PartitionDispatcher mockPartitionDispatcher() { + PartitionDispatcher dispatcher = mock(PartitionDispatcher.class); + when(dispatcher.addPartition(any(NameIdentifier.class), any(Partition.class))) + .thenReturn(partition); + when(dispatcher.getPartition(any(NameIdentifier.class), any(String.class))) + .thenReturn(partition); + when(dispatcher.listPartitionNames(any(NameIdentifier.class))).thenReturn(null); + when(dispatcher.listPartitions(any(NameIdentifier.class))).thenReturn(null); + return dispatcher; + } + + private PartitionDispatcher mockExceptionPartitionDispatcher() { + PartitionDispatcher dispatcher = + mock( + PartitionDispatcher.class, + invocation -> { + throw new NoSuchPartitionException("Exception for all methods"); Review Comment: You throws `NoSuchPartitionException` , but checks `GravitinoRuntimeException`, please fix it with `GravitinoRuntimeException` ```java Assertions.assertThrowsExactly( GravitinoRuntimeException.class, () -> failureDispatcher.addPartition(identifier, partition)); ``` ########## core/src/test/java/com/datastrato/gravitino/listener/api/event/TestPartitionEvent.java: ########## @@ -0,0 +1,235 @@ +/* + * 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 com.datastrato.gravitino.listener.api.event; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.datastrato.gravitino.NameIdentifier; +import com.datastrato.gravitino.catalog.PartitionDispatcher; +import com.datastrato.gravitino.exceptions.GravitinoRuntimeException; +import com.datastrato.gravitino.exceptions.NoSuchPartitionException; +import com.datastrato.gravitino.listener.DummyEventListener; +import com.datastrato.gravitino.listener.EventBus; +import com.datastrato.gravitino.listener.PartitionEventDispatcher; +import com.datastrato.gravitino.listener.api.info.PartitionInfo; +import com.datastrato.gravitino.rel.partitions.Partition; +import com.google.common.collect.ImmutableMap; +import java.util.Arrays; +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 TestPartitionEvent { Review Comment: please try to fix the UT in local environment, -- 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]
