Skip to content

Commit 9823d77

Browse files
authored
[#5902] feat: Add tag failure event to Gravitino server (#5944)
### 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.
1 parent 08573d1 commit 9823d77

23 files changed

+1178
-94
lines changed

core/src/main/java/org/apache/gravitino/listener/TagEventDispatcher.java

+48-15
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,22 @@
2121
import java.util.Map;
2222
import org.apache.gravitino.MetadataObject;
2323
import org.apache.gravitino.exceptions.NoSuchTagException;
24+
import org.apache.gravitino.listener.api.event.AlterTagFailureEvent;
25+
import org.apache.gravitino.listener.api.event.AssociateTagsForMetadataObjectFailureEvent;
26+
import org.apache.gravitino.listener.api.event.CreateTagFailureEvent;
27+
import org.apache.gravitino.listener.api.event.DeleteTagFailureEvent;
28+
import org.apache.gravitino.listener.api.event.GetTagFailureEvent;
29+
import org.apache.gravitino.listener.api.event.GetTagForMetadataObjectFailureEvent;
30+
import org.apache.gravitino.listener.api.event.ListMetadataObjectsForTagFailureEvent;
31+
import org.apache.gravitino.listener.api.event.ListTagsFailureEvent;
32+
import org.apache.gravitino.listener.api.event.ListTagsForMetadataObjectFailureEvent;
33+
import org.apache.gravitino.listener.api.event.ListTagsInfoFailureEvent;
34+
import org.apache.gravitino.listener.api.event.ListTagsInfoForMetadataObjectFailureEvent;
35+
import org.apache.gravitino.listener.api.info.TagInfo;
2436
import org.apache.gravitino.tag.Tag;
2537
import org.apache.gravitino.tag.TagChange;
2638
import org.apache.gravitino.tag.TagDispatcher;
39+
import org.apache.gravitino.utils.PrincipalUtils;
2740

2841
/**
2942
* {@code TagEventDispatcher} is a decorator for {@link TagDispatcher} that not only delegates tag
@@ -32,10 +45,7 @@
3245
* of tag operations.
3346
*/
3447
public class TagEventDispatcher implements TagDispatcher {
35-
@SuppressWarnings("unused")
3648
private final EventBus eventBus;
37-
38-
@SuppressWarnings("unused")
3949
private final TagDispatcher dispatcher;
4050

4151
public TagEventDispatcher(EventBus eventBus, TagDispatcher dispatcher) {
@@ -50,7 +60,8 @@ public String[] listTags(String metalake) {
5060
// TODO: listTagsEvent
5161
return dispatcher.listTags(metalake);
5262
} catch (Exception e) {
53-
// TODO: listTagFailureEvent
63+
eventBus.dispatchEvent(
64+
new ListTagsFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, e));
5465
throw e;
5566
}
5667
}
@@ -62,7 +73,8 @@ public Tag[] listTagsInfo(String metalake) {
6273
// TODO: listTagsInfoEvent
6374
return dispatcher.listTagsInfo(metalake);
6475
} catch (Exception e) {
65-
// TODO: listTagsInfoFailureEvent
76+
eventBus.dispatchEvent(
77+
new ListTagsInfoFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, e));
6678
throw e;
6779
}
6880
}
@@ -73,21 +85,24 @@ public Tag getTag(String metalake, String name) throws NoSuchTagException {
7385
try {
7486
// TODO: getTagEvent
7587
return dispatcher.getTag(metalake, name);
76-
} catch (NoSuchTagException e) {
77-
// TODO: getTagFailureEvent
88+
} catch (Exception e) {
89+
eventBus.dispatchEvent(
90+
new GetTagFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, name, e));
7891
throw e;
7992
}
8093
}
8194

8295
@Override
8396
public Tag createTag(
8497
String metalake, String name, String comment, Map<String, String> properties) {
98+
TagInfo tagInfo = new TagInfo(name, comment, properties);
8599
// TODO: createTagPreEvent
86100
try {
87101
// TODO: createTagEvent
88102
return dispatcher.createTag(metalake, name, comment, properties);
89103
} catch (Exception e) {
90-
// TODO: createTagFailureEvent
104+
eventBus.dispatchEvent(
105+
new CreateTagFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, tagInfo, e));
91106
throw e;
92107
}
93108
}
@@ -99,7 +114,9 @@ public Tag alterTag(String metalake, String name, TagChange... changes) {
99114
// TODO: alterTagEvent
100115
return dispatcher.alterTag(metalake, name, changes);
101116
} catch (Exception e) {
102-
// TODO: alterTagFailureEvent
117+
eventBus.dispatchEvent(
118+
new AlterTagFailureEvent(
119+
PrincipalUtils.getCurrentUserName(), metalake, name, changes, e));
103120
throw e;
104121
}
105122
}
@@ -111,7 +128,8 @@ public boolean deleteTag(String metalake, String name) {
111128
// TODO: deleteTagEvent
112129
return dispatcher.deleteTag(metalake, name);
113130
} catch (Exception e) {
114-
// TODO: deleteTagFailureEvent
131+
eventBus.dispatchEvent(
132+
new DeleteTagFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, name, e));
115133
throw e;
116134
}
117135
}
@@ -123,7 +141,9 @@ public MetadataObject[] listMetadataObjectsForTag(String metalake, String name)
123141
// TODO: listMetadataObjectsForTagEvent
124142
return dispatcher.listMetadataObjectsForTag(metalake, name);
125143
} catch (Exception e) {
126-
// TODO: listMetadataObjectsForTagFailureEvent
144+
eventBus.dispatchEvent(
145+
new ListMetadataObjectsForTagFailureEvent(
146+
PrincipalUtils.getCurrentUserName(), metalake, name, e));
127147
throw e;
128148
}
129149
}
@@ -135,7 +155,9 @@ public String[] listTagsForMetadataObject(String metalake, MetadataObject metada
135155
// TODO: listTagsForMetadataObjectEvent
136156
return dispatcher.listTagsForMetadataObject(metalake, metadataObject);
137157
} catch (Exception e) {
138-
// TODO: listTagsForMetadataObjectFailureEvent
158+
eventBus.dispatchEvent(
159+
new ListTagsForMetadataObjectFailureEvent(
160+
PrincipalUtils.getCurrentUserName(), metalake, metadataObject, e));
139161
throw e;
140162
}
141163
}
@@ -147,7 +169,9 @@ public Tag[] listTagsInfoForMetadataObject(String metalake, MetadataObject metad
147169
// TODO: listTagsInfoForMetadataObjectEvent
148170
return dispatcher.listTagsInfoForMetadataObject(metalake, metadataObject);
149171
} catch (Exception e) {
150-
// TODO: listTagsInfoForMetadataObjectFailureEvent
172+
eventBus.dispatchEvent(
173+
new ListTagsInfoForMetadataObjectFailureEvent(
174+
PrincipalUtils.getCurrentUserName(), metalake, metadataObject, e));
151175
throw e;
152176
}
153177
}
@@ -161,7 +185,14 @@ public String[] associateTagsForMetadataObject(
161185
return dispatcher.associateTagsForMetadataObject(
162186
metalake, metadataObject, tagsToAdd, tagsToRemove);
163187
} catch (Exception e) {
164-
// TODO: associateTagsForMetadataObjectFailureEvent
188+
eventBus.dispatchEvent(
189+
new AssociateTagsForMetadataObjectFailureEvent(
190+
PrincipalUtils.getCurrentUserName(),
191+
metalake,
192+
metadataObject,
193+
tagsToAdd,
194+
tagsToRemove,
195+
e));
165196
throw e;
166197
}
167198
}
@@ -173,7 +204,9 @@ public Tag getTagForMetadataObject(String metalake, MetadataObject metadataObjec
173204
// TODO: getTagForMetadataObjectEvent
174205
return dispatcher.getTagForMetadataObject(metalake, metadataObject, name);
175206
} catch (Exception e) {
176-
// TODO: getTagForMetadataObjectFailureEvent
207+
eventBus.dispatchEvent(
208+
new GetTagForMetadataObjectFailureEvent(
209+
PrincipalUtils.getCurrentUserName(), metalake, metadataObject, name, e));
177210
throw e;
178211
}
179212
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.gravitino.listener.api.event;
21+
22+
import org.apache.gravitino.annotation.DeveloperApi;
23+
import org.apache.gravitino.tag.TagChange;
24+
import org.apache.gravitino.utils.NameIdentifierUtil;
25+
26+
/**
27+
* Represents an event triggered when an attempt to alter a tag in the database fails due to an
28+
* exception.
29+
*/
30+
@DeveloperApi
31+
public class AlterTagFailureEvent extends TagFailureEvent {
32+
private final TagChange[] changes;
33+
34+
/**
35+
* Constructs a new AlterTagFailureEvent.
36+
*
37+
* @param user the user who attempted to alter the tag
38+
* @param metalake the metalake identifier
39+
* @param name the name of the tag
40+
* @param changes the changes attempted to be made to the tag
41+
* @param exception the exception that caused the failure
42+
*/
43+
public AlterTagFailureEvent(
44+
String user, String metalake, String name, TagChange[] changes, Exception exception) {
45+
super(user, NameIdentifierUtil.ofTag(metalake, name), exception);
46+
this.changes = changes;
47+
}
48+
49+
/**
50+
* Returns the changes attempted to be made to the tag.
51+
*
52+
* @return the changes attempted to be made to the tag
53+
*/
54+
public TagChange[] changes() {
55+
return changes;
56+
}
57+
58+
/**
59+
* Returns the type of operation.
60+
*
61+
* @return the operation type.
62+
*/
63+
@Override
64+
public OperationType operationType() {
65+
return OperationType.ALTER_TAG;
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.gravitino.listener.api.event;
20+
21+
import org.apache.gravitino.MetadataObject;
22+
import org.apache.gravitino.annotation.DeveloperApi;
23+
import org.apache.gravitino.utils.MetadataObjectUtil;
24+
25+
/**
26+
* Represents an event triggered when an attempt to associate tags for a metadata object fails due
27+
* to an exception.
28+
*/
29+
@DeveloperApi
30+
public class AssociateTagsForMetadataObjectFailureEvent extends TagFailureEvent {
31+
private final String[] tagsToAdd;
32+
private final String[] tagsToRemove;
33+
34+
/**
35+
* Constructs a new {@code AssociateTagsForMetadataObjectFailureEvent} instance.
36+
*
37+
* @param user The user who initiated the operation.
38+
* @param metalake The metalake name where the metadata object resides.
39+
* @param metadataObject The metadata object for which tags are being associated.
40+
* @param tagsToAdd The tags to add.
41+
* @param tagsToRemove The tags to remove.
42+
* @param exception The exception encountered during the operation, providing insights into the
43+
* reasons behind the failure.
44+
*/
45+
public AssociateTagsForMetadataObjectFailureEvent(
46+
String user,
47+
String metalake,
48+
MetadataObject metadataObject,
49+
String[] tagsToAdd,
50+
String[] tagsToRemove,
51+
Exception exception) {
52+
super(user, MetadataObjectUtil.toEntityIdent(metalake, metadataObject), exception);
53+
this.tagsToAdd = tagsToAdd;
54+
this.tagsToRemove = tagsToRemove;
55+
}
56+
57+
/**
58+
* Returns the tags to add.
59+
*
60+
* @return The tags to add.
61+
*/
62+
public String[] tagsToAdd() {
63+
return tagsToAdd;
64+
}
65+
66+
/**
67+
* Returns the tags to remove.
68+
*
69+
* @return The tags to remove.
70+
*/
71+
public String[] tagsToRemove() {
72+
return tagsToRemove;
73+
}
74+
75+
/**
76+
* Returns the type of operation.
77+
*
78+
* @return the operation type.
79+
*/
80+
@Override
81+
public OperationType operationType() {
82+
return OperationType.ASSOCIATE_TAGS_FOR_METADATA_OBJECT;
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.gravitino.listener.api.event;
21+
22+
import org.apache.gravitino.annotation.DeveloperApi;
23+
import org.apache.gravitino.listener.api.info.TagInfo;
24+
import org.apache.gravitino.utils.NameIdentifierUtil;
25+
26+
/**
27+
* Represents an event triggered when an attempt to create a tag in the database fails due to an
28+
* exception.
29+
*/
30+
@DeveloperApi
31+
public class CreateTagFailureEvent extends TagFailureEvent {
32+
private final TagInfo tagInfo;
33+
/**
34+
* Constructs a new {@code CreateTagFailureEvent} instance.
35+
*
36+
* @param user The user who initiated the tag creation operation.
37+
* @param metalake The metalake name where the tag resides.
38+
* @param tagInfo The information about the tag to be created.
39+
* @param exception The exception encountered during the tag creation operation, providing
40+
* insights into the reasons behind the operation's failure.
41+
*/
42+
public CreateTagFailureEvent(String user, String metalake, TagInfo tagInfo, Exception exception) {
43+
super(user, NameIdentifierUtil.ofTag(metalake, tagInfo.name()), exception);
44+
this.tagInfo = tagInfo;
45+
}
46+
47+
/**
48+
* Returns the information about the tag.
49+
*
50+
* @return the tag information
51+
*/
52+
public TagInfo tagInfo() {
53+
return tagInfo;
54+
}
55+
56+
/**
57+
* Returns the type of operation.
58+
*
59+
* @return the operation type.
60+
*/
61+
@Override
62+
public OperationType operationType() {
63+
return OperationType.CREATE_TAG;
64+
}
65+
}

0 commit comments

Comments
 (0)