FANNG1 commented on code in PR #4102: URL: https://github.com/apache/gravitino/pull/4102#discussion_r1677270915
########## core/src/main/java/com/datastrato/gravitino/listener/PartitionEventDispatcher.java: ########## @@ -0,0 +1,176 @@ +/* + * 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; + +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.ExistPartitionEvent; +import com.datastrato.gravitino.listener.api.event.ExistPartitionFailureEvent; +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.event.ListPartitionNamesEvent; +import com.datastrato.gravitino.listener.api.event.ListPartitionNamesFailureEvent; +import com.datastrato.gravitino.listener.api.event.PurgePartitionEvent; +import com.datastrato.gravitino.listener.api.event.PurgePartitionFailureEvent; +import com.datastrato.gravitino.listener.api.info.PartitionInfo; +import com.datastrato.gravitino.rel.partitions.Partition; +import com.datastrato.gravitino.utils.PrincipalUtils; + +/** + * {@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 newPartition = dispatcher.addPartition(ident, partition); + eventBus.dispatchEvent( + new AddPartitionEvent( + PrincipalUtils.getCurrentUserName(), ident, new PartitionInfo(newPartition))); + return newPartition; + } catch (Exception e) { + PartitionInfo addPartitionRequest = new PartitionInfo(partition); + eventBus.dispatchEvent( + new AddPartitionFailureEvent( + PrincipalUtils.getCurrentUserName(), ident, e, addPartitionRequest)); + throw e; + } + } + + @Override + public Partition getPartition(NameIdentifier ident, String partitionName) + throws NoSuchPartitionException { + try { + Partition partition = dispatcher.getPartition(ident, partitionName); + eventBus.dispatchEvent( + new GetPartitionEvent( + PrincipalUtils.getCurrentUserName(), ident, new PartitionInfo(partition))); + return partition; + } catch (Exception e) { + eventBus.dispatchEvent( + new GetPartitionFailureEvent( + PrincipalUtils.getCurrentUserName(), ident, e, partitionName)); + throw e; + } + } + + @Override + public boolean dropPartition(NameIdentifier ident, String partitionName) { + try { + boolean isExists = dispatcher.dropPartition(ident, partitionName); + eventBus.dispatchEvent( + new DropPartitionEvent( + PrincipalUtils.getCurrentUserName(), ident, isExists, partitionName)); + return isExists; + } catch (Exception e) { + eventBus.dispatchEvent( + new DropPartitionFailureEvent( + PrincipalUtils.getCurrentUserName(), ident, e, partitionName)); + throw e; + } + } + + @Override + public Partition[] listPartitions(NameIdentifier ident) { + try { + Partition[] listPartitions = dispatcher.listPartitions(ident); + eventBus.dispatchEvent(new ListPartitionEvent(PrincipalUtils.getCurrentUserName(), ident)); + return listPartitions; + } catch (Exception e) { + eventBus.dispatchEvent( + new ListPartitionFailureEvent(PrincipalUtils.getCurrentUserName(), ident, e)); + throw e; + } + } + + @Override + public String[] listPartitionNames(NameIdentifier ident) { + try { + String[] listPartitionNames = dispatcher.listPartitionNames(ident); + eventBus.dispatchEvent( + new ListPartitionNamesEvent(PrincipalUtils.getCurrentUserName(), ident)); + return listPartitionNames; + } catch (Exception e) { + eventBus.dispatchEvent( + new ListPartitionNamesFailureEvent(PrincipalUtils.getCurrentUserName(), ident, e)); + throw e; + } + } + + @Override + public boolean partitionExists(NameIdentifier ident, String partitionName) { + try { + boolean isExists = dispatcher.partitionExists(ident, partitionName); + eventBus.dispatchEvent( + new ExistPartitionEvent( Review Comment: Could you rename it to `PartitionExistsEvent`? -- 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]
