FANNG1 commented on code in PR #4102: URL: https://github.com/apache/gravitino/pull/4102#discussion_r1669499677
########## core/src/main/java/com/datastrato/gravitino/listener/PartitionEventDispatcher.java: ########## @@ -0,0 +1,136 @@ +/* + * 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; + +import com.datastrato.gravitino.NameIdentifier; +import com.datastrato.gravitino.catalog.PartitionDispatcher; +import com.datastrato.gravitino.exceptions.NoSuchPartitionException; +import com.datastrato.gravitino.exceptions.PartitionAlreadyExistsException; +import com.datastrato.gravitino.listener.api.event.AddPartitionEvent; +import com.datastrato.gravitino.listener.api.event.AddPartitionFailureEvent; +import com.datastrato.gravitino.listener.api.event.DropPartitionEvent; +import com.datastrato.gravitino.listener.api.event.DropPartitionFailureEvent; +import com.datastrato.gravitino.listener.api.event.GetPartitionEvent; +import com.datastrato.gravitino.listener.api.event.GetPartitionFailureEvent; +import com.datastrato.gravitino.listener.api.event.ListPartitionEvent; +import com.datastrato.gravitino.listener.api.event.ListPartitionFailureEvent; +import com.datastrato.gravitino.listener.api.info.PartitionInfo; +import com.datastrato.gravitino.rel.partitions.Partition; +import com.datastrato.gravitino.utils.PrincipalUtils; +import com.google.common.collect.Maps; + +/** + * {@code PartitionEventDispatcher} is a decorator for {@link PartitionDispatcher} that not only + * delegates partition operations to the underlying partition dispatcher but also dispatches + * corresponding events to an {@link com.datastrato.gravitino.listener.EventBus} after each + * operation is completed. This allows for event-driven workflows or monitoring of partition + * operations. + */ +public class PartitionEventDispatcher implements PartitionDispatcher { + private final EventBus eventBus; + private final PartitionDispatcher dispatcher; + + /** + * Constructs a PartitionEventDispatcher with a specified EventBus and PartitionDispatcher. + * + * @param eventBus The EventBus to which events will be dispatched. + * @param dispatcher The underlying {@link PartitionDispatcher} that will perform the actual + * partition operations. + */ + public PartitionEventDispatcher(EventBus eventBus, PartitionDispatcher dispatcher) { + this.eventBus = eventBus; + this.dispatcher = dispatcher; + } + + @Override + public Partition addPartition(NameIdentifier ident, Partition partition) + throws NoSuchPartitionException, PartitionAlreadyExistsException { + try { + Partition new_partition = dispatcher.addPartition(ident, partition); + eventBus.dispatchEvent( + new AddPartitionEvent( + PrincipalUtils.getCurrentUserName(), ident, new PartitionInfo(new_partition))); + return partition; + } catch (Exception e) { + PartitionInfo addPartitionRequest = new PartitionInfo(ident.name(), Maps.newHashMap()); Review Comment: create partition failed, we'd better place `partition` in `PartitionInfo` -- 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]
