Skip to content

Commit

Permalink
[#5900] feat(tag): support tag pre-event to Gravitino server (#5980)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Implemented pre-event handling for all tag-related operations,
including:

- listTags
- listTagsInfo
- getTag
- createTag
- alterTag
- deleteTag
- listMetadataObjectsForTag
- listTagsForMetadataObject
- listTagsInfoForMetadataObject
- associateTagsForMetadataObject
- getTagForMetadataObject

### Why are the changes needed?
Fix: #5900

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
add UT
  • Loading branch information
amazingLychee authored Feb 9, 2025
1 parent e50e00e commit a84fd18
Show file tree
Hide file tree
Showing 15 changed files with 868 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,27 @@
import org.apache.gravitino.MetadataObject;
import org.apache.gravitino.exceptions.NoSuchTagException;
import org.apache.gravitino.listener.api.event.AlterTagFailureEvent;
import org.apache.gravitino.listener.api.event.AlterTagPreEvent;
import org.apache.gravitino.listener.api.event.AssociateTagsForMetadataObjectFailureEvent;
import org.apache.gravitino.listener.api.event.AssociateTagsForMetadataObjectPreEvent;
import org.apache.gravitino.listener.api.event.CreateTagFailureEvent;
import org.apache.gravitino.listener.api.event.CreateTagPreEvent;
import org.apache.gravitino.listener.api.event.DeleteTagFailureEvent;
import org.apache.gravitino.listener.api.event.DeleteTagPreEvent;
import org.apache.gravitino.listener.api.event.GetTagFailureEvent;
import org.apache.gravitino.listener.api.event.GetTagForMetadataObjectFailureEvent;
import org.apache.gravitino.listener.api.event.GetTagForMetadataObjectPreEvent;
import org.apache.gravitino.listener.api.event.GetTagPreEvent;
import org.apache.gravitino.listener.api.event.ListMetadataObjectsForTagFailureEvent;
import org.apache.gravitino.listener.api.event.ListMetadataObjectsForTagPreEvent;
import org.apache.gravitino.listener.api.event.ListTagsFailureEvent;
import org.apache.gravitino.listener.api.event.ListTagsForMetadataObjectFailureEvent;
import org.apache.gravitino.listener.api.event.ListTagsForMetadataObjectPreEvent;
import org.apache.gravitino.listener.api.event.ListTagsInfoFailureEvent;
import org.apache.gravitino.listener.api.event.ListTagsInfoForMetadataObjectFailureEvent;
import org.apache.gravitino.listener.api.event.ListTagsInfoForMetadataObjectPreEvent;
import org.apache.gravitino.listener.api.event.ListTagsInfoPreEvent;
import org.apache.gravitino.listener.api.event.ListTagsPreEvent;
import org.apache.gravitino.listener.api.info.TagInfo;
import org.apache.gravitino.tag.Tag;
import org.apache.gravitino.tag.TagChange;
Expand All @@ -55,7 +66,7 @@ public TagEventDispatcher(EventBus eventBus, TagDispatcher dispatcher) {

@Override
public String[] listTags(String metalake) {
// TODO: listTagsPreEvent
eventBus.dispatchEvent(new ListTagsPreEvent(PrincipalUtils.getCurrentUserName(), metalake));
try {
// TODO: listTagsEvent
return dispatcher.listTags(metalake);
Expand All @@ -68,7 +79,7 @@ public String[] listTags(String metalake) {

@Override
public Tag[] listTagsInfo(String metalake) {
// TODO: listTagsInfoPreEvent
eventBus.dispatchEvent(new ListTagsInfoPreEvent(PrincipalUtils.getCurrentUserName(), metalake));
try {
// TODO: listTagsInfoEvent
return dispatcher.listTagsInfo(metalake);
Expand All @@ -81,7 +92,7 @@ public Tag[] listTagsInfo(String metalake) {

@Override
public Tag getTag(String metalake, String name) throws NoSuchTagException {
// TODO: getTagPreEvent
eventBus.dispatchEvent(new GetTagPreEvent(PrincipalUtils.getCurrentUserName(), metalake, name));
try {
// TODO: getTagEvent
return dispatcher.getTag(metalake, name);
Expand All @@ -97,6 +108,9 @@ public Tag createTag(
String metalake, String name, String comment, Map<String, String> properties) {
TagInfo tagInfo = new TagInfo(name, comment, properties);
// TODO: createTagPreEvent

eventBus.dispatchEvent(
new CreateTagPreEvent(PrincipalUtils.getCurrentUserName(), metalake, tagInfo));
try {
// TODO: createTagEvent
return dispatcher.createTag(metalake, name, comment, properties);
Expand All @@ -109,7 +123,10 @@ public Tag createTag(

@Override
public Tag alterTag(String metalake, String name, TagChange... changes) {
// TODO: alterTagPreEvent
AlterTagPreEvent preEvent =
new AlterTagPreEvent(PrincipalUtils.getCurrentUserName(), metalake, name, changes);

eventBus.dispatchEvent(preEvent);
try {
// TODO: alterTagEvent
return dispatcher.alterTag(metalake, name, changes);
Expand All @@ -123,7 +140,10 @@ public Tag alterTag(String metalake, String name, TagChange... changes) {

@Override
public boolean deleteTag(String metalake, String name) {
// TODO: deleteTagPreEvent
DeleteTagPreEvent preEvent =
new DeleteTagPreEvent(PrincipalUtils.getCurrentUserName(), metalake, name);

eventBus.dispatchEvent(preEvent);
try {
// TODO: deleteTagEvent
return dispatcher.deleteTag(metalake, name);
Expand All @@ -136,7 +156,8 @@ public boolean deleteTag(String metalake, String name) {

@Override
public MetadataObject[] listMetadataObjectsForTag(String metalake, String name) {
// TODO: listMetadataObjectsForTagPreEvent
eventBus.dispatchEvent(
new ListMetadataObjectsForTagPreEvent(PrincipalUtils.getCurrentUserName(), metalake, name));
try {
// TODO: listMetadataObjectsForTagEvent
return dispatcher.listMetadataObjectsForTag(metalake, name);
Expand All @@ -150,7 +171,10 @@ public MetadataObject[] listMetadataObjectsForTag(String metalake, String name)

@Override
public String[] listTagsForMetadataObject(String metalake, MetadataObject metadataObject) {
// TODO: listTagsForMetadataObjectPreEvent
eventBus.dispatchEvent(
new ListTagsForMetadataObjectPreEvent(
PrincipalUtils.getCurrentUserName(), metalake, metadataObject));

try {
// TODO: listTagsForMetadataObjectEvent
return dispatcher.listTagsForMetadataObject(metalake, metadataObject);
Expand All @@ -164,7 +188,9 @@ public String[] listTagsForMetadataObject(String metalake, MetadataObject metada

@Override
public Tag[] listTagsInfoForMetadataObject(String metalake, MetadataObject metadataObject) {
// TODO: listTagsInfoForMetadataObjectPreEvent
eventBus.dispatchEvent(
new ListTagsInfoForMetadataObjectPreEvent(
PrincipalUtils.getCurrentUserName(), metalake, metadataObject));
try {
// TODO: listTagsInfoForMetadataObjectEvent
return dispatcher.listTagsInfoForMetadataObject(metalake, metadataObject);
Expand All @@ -179,7 +205,14 @@ public Tag[] listTagsInfoForMetadataObject(String metalake, MetadataObject metad
@Override
public String[] associateTagsForMetadataObject(
String metalake, MetadataObject metadataObject, String[] tagsToAdd, String[] tagsToRemove) {
// TODO: associateTagsForMetadataObjectPreEvent
eventBus.dispatchEvent(
new AssociateTagsForMetadataObjectPreEvent(
PrincipalUtils.getCurrentUserName(),
metalake,
metadataObject,
tagsToAdd,
tagsToRemove));

try {
// TODO: associateTagsForMetadataObjectEvent
return dispatcher.associateTagsForMetadataObject(
Expand All @@ -199,7 +232,9 @@ public String[] associateTagsForMetadataObject(

@Override
public Tag getTagForMetadataObject(String metalake, MetadataObject metadataObject, String name) {
// TODO: getTagForMetadataObjectPreEvent
eventBus.dispatchEvent(
new GetTagForMetadataObjectPreEvent(
PrincipalUtils.getCurrentUserName(), metalake, metadataObject, name));
try {
// TODO: getTagForMetadataObjectEvent
return dispatcher.getTagForMetadataObject(metalake, metadataObject, name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 org.apache.gravitino.annotation.DeveloperApi;
import org.apache.gravitino.tag.TagChange;
import org.apache.gravitino.utils.NameIdentifierUtil;

/** Represents an event triggered before altering a tag. */
@DeveloperApi
public class AlterTagPreEvent extends TagPreEvent {

private final TagChange[] changes;

/**
* Constructs a new AlterTagPreEvent instance.
*
* @param user The user responsible for the operation.
* @param metalake The namespace of the tag.
* @param name The name of the tag being altered.
* @param changes The changes being applied to the tag.
*/
public AlterTagPreEvent(String user, String metalake, String name, TagChange[] changes) {
super(user, NameIdentifierUtil.ofTag(metalake, name));
this.changes = changes;
}

/**
* Returns the changes being applied to the tag.
*
* @return An array of {@link TagChange}.
*/
public TagChange[] changes() {
return changes;
}

/**
* Returns the operation type for this event.
*
* @return The operation type {@link OperationType#ALTER_TAG}.
*/
@Override
public OperationType operationType() {
return OperationType.ALTER_TAG;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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 org.apache.gravitino.MetadataObject;
import org.apache.gravitino.annotation.DeveloperApi;
import org.apache.gravitino.utils.MetadataObjectUtil;

/** Represents an event triggered before associating tags with a specific metadata object. */
@DeveloperApi
public class AssociateTagsForMetadataObjectPreEvent extends TagPreEvent {
private final String[] tagsToAdd;
private final String[] tagsToRemove;

/**
* Constructs the pre-event with user, metalake, metadata object, tags to add, and tags to remove.
*
* @param user The user initiating the operation.
* @param metalake The metalake environment name.
* @param metadataObject The metadata object for which tags will be associated.
* @param tagsToAdd Tags to be added to the metadata object.
* @param tagsToRemove Tags to be removed from the metadata object.
*/
public AssociateTagsForMetadataObjectPreEvent(
String user,
String metalake,
MetadataObject metadataObject,
String[] tagsToAdd,
String[] tagsToRemove) {
super(user, MetadataObjectUtil.toEntityIdent(metalake, metadataObject));
this.tagsToAdd = tagsToAdd;
this.tagsToRemove = tagsToRemove;
}

/**
* Returns the tags to be added.
*
* @return Array of tag names to be added.
*/
public String[] tagsToAdd() {
return tagsToAdd;
}

/**
* Returns the tags to be removed.
*
* @return Array of tag names to be removed.
*/
public String[] tagsToRemove() {
return tagsToRemove;
}

/**
* Returns the operation type for this event.
*
* @return The operation type, which is ASSOCIATE_TAGS_FOR_METADATA_OBJECT.
*/
@Override
public OperationType operationType() {
return OperationType.ASSOCIATE_TAGS_FOR_METADATA_OBJECT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 org.apache.gravitino.annotation.DeveloperApi;
import org.apache.gravitino.listener.api.info.TagInfo;
import org.apache.gravitino.utils.NameIdentifierUtil;

/** Represents an event triggered before the creation of a tag. */
@DeveloperApi
public class CreateTagPreEvent extends TagPreEvent {
private final TagInfo tagInfo;

/**
* Constructs a new {@code CreateTagPreEvent} instance.
*
* @param user The user who initiated the tag creation operation.
* @param metalake The metalake name where the tag resides.
* @param tagInfo The information about the tag to be created.
*/
public CreateTagPreEvent(String user, String metalake, TagInfo tagInfo) {
super(user, NameIdentifierUtil.ofTag(metalake, tagInfo.name()));
this.tagInfo = tagInfo;
}

/**
* Returns the information about the tag.
*
* @return the tag information
*/
public TagInfo tagInfo() {
return tagInfo;
}

/**
* Returns the type of operation.
*
* @return the operation type.
*/
@Override
public OperationType operationType() {
return OperationType.CREATE_TAG;
}
}
Loading

0 comments on commit a84fd18

Please sign in to comment.