Skip to content

Commit

Permalink
[#5902] feat: Add tag failure event to Gravitino server (#5944)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Add tag failure event to Gravitino server

### Why are the changes needed?

Subtask: #5902

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

No.

### How was this patch tested?

Unit tests.
  • Loading branch information
cool9850311 authored Jan 7, 2025
1 parent 08573d1 commit 9823d77
Show file tree
Hide file tree
Showing 23 changed files with 1,178 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,22 @@
import java.util.Map;
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.AssociateTagsForMetadataObjectFailureEvent;
import org.apache.gravitino.listener.api.event.CreateTagFailureEvent;
import org.apache.gravitino.listener.api.event.DeleteTagFailureEvent;
import org.apache.gravitino.listener.api.event.GetTagFailureEvent;
import org.apache.gravitino.listener.api.event.GetTagForMetadataObjectFailureEvent;
import org.apache.gravitino.listener.api.event.ListMetadataObjectsForTagFailureEvent;
import org.apache.gravitino.listener.api.event.ListTagsFailureEvent;
import org.apache.gravitino.listener.api.event.ListTagsForMetadataObjectFailureEvent;
import org.apache.gravitino.listener.api.event.ListTagsInfoFailureEvent;
import org.apache.gravitino.listener.api.event.ListTagsInfoForMetadataObjectFailureEvent;
import org.apache.gravitino.listener.api.info.TagInfo;
import org.apache.gravitino.tag.Tag;
import org.apache.gravitino.tag.TagChange;
import org.apache.gravitino.tag.TagDispatcher;
import org.apache.gravitino.utils.PrincipalUtils;

/**
* {@code TagEventDispatcher} is a decorator for {@link TagDispatcher} that not only delegates tag
Expand All @@ -32,10 +45,7 @@
* of tag operations.
*/
public class TagEventDispatcher implements TagDispatcher {
@SuppressWarnings("unused")
private final EventBus eventBus;

@SuppressWarnings("unused")
private final TagDispatcher dispatcher;

public TagEventDispatcher(EventBus eventBus, TagDispatcher dispatcher) {
Expand All @@ -50,7 +60,8 @@ public String[] listTags(String metalake) {
// TODO: listTagsEvent
return dispatcher.listTags(metalake);
} catch (Exception e) {
// TODO: listTagFailureEvent
eventBus.dispatchEvent(
new ListTagsFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, e));
throw e;
}
}
Expand All @@ -62,7 +73,8 @@ public Tag[] listTagsInfo(String metalake) {
// TODO: listTagsInfoEvent
return dispatcher.listTagsInfo(metalake);
} catch (Exception e) {
// TODO: listTagsInfoFailureEvent
eventBus.dispatchEvent(
new ListTagsInfoFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, e));
throw e;
}
}
Expand All @@ -73,21 +85,24 @@ public Tag getTag(String metalake, String name) throws NoSuchTagException {
try {
// TODO: getTagEvent
return dispatcher.getTag(metalake, name);
} catch (NoSuchTagException e) {
// TODO: getTagFailureEvent
} catch (Exception e) {
eventBus.dispatchEvent(
new GetTagFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, name, e));
throw e;
}
}

@Override
public Tag createTag(
String metalake, String name, String comment, Map<String, String> properties) {
TagInfo tagInfo = new TagInfo(name, comment, properties);
// TODO: createTagPreEvent
try {
// TODO: createTagEvent
return dispatcher.createTag(metalake, name, comment, properties);
} catch (Exception e) {
// TODO: createTagFailureEvent
eventBus.dispatchEvent(
new CreateTagFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, tagInfo, e));
throw e;
}
}
Expand All @@ -99,7 +114,9 @@ public Tag alterTag(String metalake, String name, TagChange... changes) {
// TODO: alterTagEvent
return dispatcher.alterTag(metalake, name, changes);
} catch (Exception e) {
// TODO: alterTagFailureEvent
eventBus.dispatchEvent(
new AlterTagFailureEvent(
PrincipalUtils.getCurrentUserName(), metalake, name, changes, e));
throw e;
}
}
Expand All @@ -111,7 +128,8 @@ public boolean deleteTag(String metalake, String name) {
// TODO: deleteTagEvent
return dispatcher.deleteTag(metalake, name);
} catch (Exception e) {
// TODO: deleteTagFailureEvent
eventBus.dispatchEvent(
new DeleteTagFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, name, e));
throw e;
}
}
Expand All @@ -123,7 +141,9 @@ public MetadataObject[] listMetadataObjectsForTag(String metalake, String name)
// TODO: listMetadataObjectsForTagEvent
return dispatcher.listMetadataObjectsForTag(metalake, name);
} catch (Exception e) {
// TODO: listMetadataObjectsForTagFailureEvent
eventBus.dispatchEvent(
new ListMetadataObjectsForTagFailureEvent(
PrincipalUtils.getCurrentUserName(), metalake, name, e));
throw e;
}
}
Expand All @@ -135,7 +155,9 @@ public String[] listTagsForMetadataObject(String metalake, MetadataObject metada
// TODO: listTagsForMetadataObjectEvent
return dispatcher.listTagsForMetadataObject(metalake, metadataObject);
} catch (Exception e) {
// TODO: listTagsForMetadataObjectFailureEvent
eventBus.dispatchEvent(
new ListTagsForMetadataObjectFailureEvent(
PrincipalUtils.getCurrentUserName(), metalake, metadataObject, e));
throw e;
}
}
Expand All @@ -147,7 +169,9 @@ public Tag[] listTagsInfoForMetadataObject(String metalake, MetadataObject metad
// TODO: listTagsInfoForMetadataObjectEvent
return dispatcher.listTagsInfoForMetadataObject(metalake, metadataObject);
} catch (Exception e) {
// TODO: listTagsInfoForMetadataObjectFailureEvent
eventBus.dispatchEvent(
new ListTagsInfoForMetadataObjectFailureEvent(
PrincipalUtils.getCurrentUserName(), metalake, metadataObject, e));
throw e;
}
}
Expand All @@ -161,7 +185,14 @@ public String[] associateTagsForMetadataObject(
return dispatcher.associateTagsForMetadataObject(
metalake, metadataObject, tagsToAdd, tagsToRemove);
} catch (Exception e) {
// TODO: associateTagsForMetadataObjectFailureEvent
eventBus.dispatchEvent(
new AssociateTagsForMetadataObjectFailureEvent(
PrincipalUtils.getCurrentUserName(),
metalake,
metadataObject,
tagsToAdd,
tagsToRemove,
e));
throw e;
}
}
Expand All @@ -173,7 +204,9 @@ public Tag getTagForMetadataObject(String metalake, MetadataObject metadataObjec
// TODO: getTagForMetadataObjectEvent
return dispatcher.getTagForMetadataObject(metalake, metadataObject, name);
} catch (Exception e) {
// TODO: getTagForMetadataObjectFailureEvent
eventBus.dispatchEvent(
new GetTagForMetadataObjectFailureEvent(
PrincipalUtils.getCurrentUserName(), metalake, metadataObject, name, e));
throw e;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 when an attempt to alter a tag in the database fails due to an
* exception.
*/
@DeveloperApi
public class AlterTagFailureEvent extends TagFailureEvent {
private final TagChange[] changes;

/**
* Constructs a new AlterTagFailureEvent.
*
* @param user the user who attempted to alter the tag
* @param metalake the metalake identifier
* @param name the name of the tag
* @param changes the changes attempted to be made to the tag
* @param exception the exception that caused the failure
*/
public AlterTagFailureEvent(
String user, String metalake, String name, TagChange[] changes, Exception exception) {
super(user, NameIdentifierUtil.ofTag(metalake, name), exception);
this.changes = changes;
}

/**
* Returns the changes attempted to be made to the tag.
*
* @return the changes attempted to be made to the tag
*/
public TagChange[] changes() {
return changes;
}

/**
* Returns the type of operation.
*
* @return the operation type.
*/
@Override
public OperationType operationType() {
return OperationType.ALTER_TAG;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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 when an attempt to associate tags for a metadata object fails due
* to an exception.
*/
@DeveloperApi
public class AssociateTagsForMetadataObjectFailureEvent extends TagFailureEvent {
private final String[] tagsToAdd;
private final String[] tagsToRemove;

/**
* Constructs a new {@code AssociateTagsForMetadataObjectFailureEvent} instance.
*
* @param user The user who initiated the operation.
* @param metalake The metalake name where the metadata object resides.
* @param metadataObject The metadata object for which tags are being associated.
* @param tagsToAdd The tags to add.
* @param tagsToRemove The tags to remove.
* @param exception The exception encountered during the operation, providing insights into the
* reasons behind the failure.
*/
public AssociateTagsForMetadataObjectFailureEvent(
String user,
String metalake,
MetadataObject metadataObject,
String[] tagsToAdd,
String[] tagsToRemove,
Exception exception) {
super(user, MetadataObjectUtil.toEntityIdent(metalake, metadataObject), exception);
this.tagsToAdd = tagsToAdd;
this.tagsToRemove = tagsToRemove;
}

/**
* Returns the tags to add.
*
* @return The tags to add.
*/
public String[] tagsToAdd() {
return tagsToAdd;
}

/**
* Returns the tags to remove.
*
* @return The tags to remove.
*/
public String[] tagsToRemove() {
return tagsToRemove;
}

/**
* Returns the type of operation.
*
* @return the operation type.
*/
@Override
public OperationType operationType() {
return OperationType.ASSOCIATE_TAGS_FOR_METADATA_OBJECT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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 when an attempt to create a tag in the database fails due to an
* exception.
*/
@DeveloperApi
public class CreateTagFailureEvent extends TagFailureEvent {
private final TagInfo tagInfo;
/**
* Constructs a new {@code CreateTagFailureEvent} 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.
* @param exception The exception encountered during the tag creation operation, providing
* insights into the reasons behind the operation's failure.
*/
public CreateTagFailureEvent(String user, String metalake, TagInfo tagInfo, Exception exception) {
super(user, NameIdentifierUtil.ofTag(metalake, tagInfo.name()), exception);
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 9823d77

Please sign in to comment.