Skip to content

Commit 8cc094c

Browse files
authored
[#6079] fix(core): Support model failure event to Gravitino server (#6670)
### What changes were proposed in this pull request? Support model failure event to Gravitino server. ### Why are the changes needed? Fix: #6079 ### Does this PR introduce _any_ user-facing change? yes, add model failure event to Gravitino server, offering insights into what went wrong and why the operation failed. ### How was this patch tested? local test.
1 parent b237637 commit 8cc094c

15 files changed

+1123
-113
lines changed

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

+50-31
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,23 @@
2828
import org.apache.gravitino.exceptions.NoSuchModelException;
2929
import org.apache.gravitino.exceptions.NoSuchModelVersionException;
3030
import org.apache.gravitino.exceptions.NoSuchSchemaException;
31+
import org.apache.gravitino.listener.api.event.DeleteModelFailureEvent;
3132
import org.apache.gravitino.listener.api.event.DeleteModelPreEvent;
33+
import org.apache.gravitino.listener.api.event.DeleteModelVersionFailureEvent;
3234
import org.apache.gravitino.listener.api.event.DeleteModelVersionPreEvent;
35+
import org.apache.gravitino.listener.api.event.GetModelFailureEvent;
3336
import org.apache.gravitino.listener.api.event.GetModelPreEvent;
37+
import org.apache.gravitino.listener.api.event.GetModelVersionFailureEvent;
3438
import org.apache.gravitino.listener.api.event.GetModelVersionPreEvent;
39+
import org.apache.gravitino.listener.api.event.LinkModelVersionFailureEvent;
3540
import org.apache.gravitino.listener.api.event.LinkModelVersionPreEvent;
41+
import org.apache.gravitino.listener.api.event.ListModelFailureEvent;
3642
import org.apache.gravitino.listener.api.event.ListModelPreEvent;
43+
import org.apache.gravitino.listener.api.event.ListModelVersionFailureEvent;
3744
import org.apache.gravitino.listener.api.event.ListModelVersionPreEvent;
45+
import org.apache.gravitino.listener.api.event.RegisterAndLinkModelFailureEvent;
3846
import org.apache.gravitino.listener.api.event.RegisterAndLinkModelPreEvent;
47+
import org.apache.gravitino.listener.api.event.RegisterModelFailureEvent;
3948
import org.apache.gravitino.listener.api.event.RegisterModelPreEvent;
4049
import org.apache.gravitino.listener.api.info.ModelInfo;
4150
import org.apache.gravitino.listener.api.info.ModelVersionInfo;
@@ -70,15 +79,15 @@ public ModelEventDispatcher(EventBus eventBus, ModelDispatcher dispatcher) {
7079
@Override
7180
public Model registerModel(NameIdentifier ident, String comment, Map<String, String> properties)
7281
throws NoSuchSchemaException, ModelAlreadyExistsException {
82+
String user = PrincipalUtils.getCurrentUserName();
7383
ModelInfo registerRequest = new ModelInfo(ident.name(), properties, comment);
74-
eventBus.dispatchEvent(
75-
new RegisterModelPreEvent(PrincipalUtils.getCurrentUserName(), ident, registerRequest));
84+
eventBus.dispatchEvent(new RegisterModelPreEvent(user, ident, registerRequest));
7685
try {
7786
Model model = dispatcher.registerModel(ident, comment, properties);
7887
// TODO: ModelEvent
7988
return model;
8089
} catch (Exception e) {
81-
// TODO: failureEvent
90+
eventBus.dispatchEvent(new RegisterModelFailureEvent(user, ident, e, registerRequest));
8291
throw e;
8392
}
8493
}
@@ -96,59 +105,63 @@ public Model registerModel(
96105
ModelInfo registerModelRequest = new ModelInfo(ident.name(), properties, comment);
97106
ModelVersionInfo linkModelVersionRequest =
98107
new ModelVersionInfo(uri, comment, properties, aliases);
108+
String user = PrincipalUtils.getCurrentUserName();
99109

100110
RegisterAndLinkModelPreEvent registerAndLinkModelPreEvent =
101111
new RegisterAndLinkModelPreEvent(
102-
PrincipalUtils.getCurrentUserName(),
103-
ident,
104-
registerModelRequest,
105-
linkModelVersionRequest);
112+
user, ident, registerModelRequest, linkModelVersionRequest);
106113
eventBus.dispatchEvent(registerAndLinkModelPreEvent);
107114
try {
108115
// TODO: ModelEvent
109116
return dispatcher.registerModel(ident, uri, aliases, comment, properties);
110117
} catch (Exception e) {
118+
eventBus.dispatchEvent(
119+
new RegisterAndLinkModelFailureEvent(
120+
user, ident, e, registerModelRequest, linkModelVersionRequest));
111121
throw e;
112122
}
113123
}
114124

115125
/** {@inheritDoc} */
116126
@Override
117127
public Model getModel(NameIdentifier ident) throws NoSuchModelException {
118-
eventBus.dispatchEvent(new GetModelPreEvent(PrincipalUtils.getCurrentUserName(), ident));
128+
String user = PrincipalUtils.getCurrentUserName();
129+
eventBus.dispatchEvent(new GetModelPreEvent(user, ident));
119130
try {
120131
Model model = dispatcher.getModel(ident);
121132
// TODO: ModelEvent
122133
return model;
123134
} catch (Exception e) {
124-
// TODO: failureEvent
135+
eventBus.dispatchEvent(new GetModelFailureEvent(user, ident, e));
125136
throw e;
126137
}
127138
}
128139

129140
/** {@inheritDoc} */
130141
@Override
131142
public boolean deleteModel(NameIdentifier ident) {
132-
eventBus.dispatchEvent(new DeleteModelPreEvent(PrincipalUtils.getCurrentUserName(), ident));
143+
String user = PrincipalUtils.getCurrentUserName();
144+
eventBus.dispatchEvent(new DeleteModelPreEvent(user, ident));
133145
try {
134146
// TODO: ModelEvent
135147
return dispatcher.deleteModel(ident);
136148
} catch (Exception e) {
137-
// TODO: failureEvent
149+
eventBus.dispatchEvent(new DeleteModelFailureEvent(user, ident, e));
138150
throw e;
139151
}
140152
}
141153

142154
/** {@inheritDoc} */
143155
@Override
144156
public NameIdentifier[] listModels(Namespace namespace) throws NoSuchSchemaException {
145-
eventBus.dispatchEvent(new ListModelPreEvent(PrincipalUtils.getCurrentUserName(), namespace));
157+
String user = PrincipalUtils.getCurrentUserName();
158+
eventBus.dispatchEvent(new ListModelPreEvent(user, namespace));
146159
try {
147160
NameIdentifier[] models = dispatcher.listModels(namespace);
148161
// TODO: ModelEvent
149162
return models;
150163
} catch (Exception e) {
151-
// TODO: failureEvent
164+
eventBus.dispatchEvent(new ListModelFailureEvent(user, namespace, e));
152165
throw e;
153166
}
154167
}
@@ -163,13 +176,14 @@ public void linkModelVersion(
163176
Map<String, String> properties)
164177
throws NoSuchModelException, ModelVersionAliasesAlreadyExistException {
165178
ModelVersionInfo linkModelRequest = new ModelVersionInfo(uri, comment, properties, aliases);
166-
eventBus.dispatchEvent(
167-
new LinkModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident, linkModelRequest));
179+
String user = PrincipalUtils.getCurrentUserName();
180+
181+
eventBus.dispatchEvent(new LinkModelVersionPreEvent(user, ident, linkModelRequest));
168182
try {
169183
dispatcher.linkModelVersion(ident, uri, aliases, comment, properties);
170184
// TODO: ModelEvent
171185
} catch (Exception e) {
172-
// TODO: failureEvent
186+
eventBus.dispatchEvent(new LinkModelVersionFailureEvent(user, ident, e, linkModelRequest));
173187
throw e;
174188
}
175189
}
@@ -178,13 +192,14 @@ public void linkModelVersion(
178192
@Override
179193
public ModelVersion getModelVersion(NameIdentifier ident, int version)
180194
throws NoSuchModelVersionException {
181-
eventBus.dispatchEvent(
182-
new GetModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident, null, version));
195+
String user = PrincipalUtils.getCurrentUserName();
196+
197+
eventBus.dispatchEvent(new GetModelVersionPreEvent(user, ident, null, version));
183198
try {
184199
// TODO: ModelEvent
185200
return dispatcher.getModelVersion(ident, version);
186201
} catch (Exception e) {
187-
// TODO: failureEvent
202+
eventBus.dispatchEvent(new GetModelVersionFailureEvent(user, ident, e, null, version));
188203
throw e;
189204
}
190205
}
@@ -193,59 +208,63 @@ public ModelVersion getModelVersion(NameIdentifier ident, int version)
193208
@Override
194209
public ModelVersion getModelVersion(NameIdentifier ident, String alias)
195210
throws NoSuchModelVersionException {
196-
eventBus.dispatchEvent(
197-
new GetModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident, alias, null));
211+
String user = PrincipalUtils.getCurrentUserName();
212+
213+
eventBus.dispatchEvent(new GetModelVersionPreEvent(user, ident, alias, null));
198214
try {
199215
ModelVersion modelVersion = dispatcher.getModelVersion(ident, alias);
200216
// TODO: ModelEvent
201217
return modelVersion;
202218
} catch (Exception e) {
203-
// TODO: failureEvent
219+
eventBus.dispatchEvent(new GetModelVersionFailureEvent(user, ident, e, alias, null));
204220
throw e;
205221
}
206222
}
207223

208224
/** {@inheritDoc} */
209225
@Override
210226
public boolean deleteModelVersion(NameIdentifier ident, int version) {
211-
eventBus.dispatchEvent(
212-
new DeleteModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident, null, version));
227+
String user = PrincipalUtils.getCurrentUserName();
228+
229+
eventBus.dispatchEvent(new DeleteModelVersionPreEvent(user, ident, null, version));
213230
try {
214231
boolean isExists = dispatcher.deleteModelVersion(ident, version);
215232
// TODO: ModelEvent
216233
return isExists;
217234
} catch (Exception e) {
218-
// TODO: failureEvent
235+
eventBus.dispatchEvent(new DeleteModelVersionFailureEvent(user, ident, e, null, version));
219236
throw e;
220237
}
221238
}
222239

223240
/** {@inheritDoc} */
224241
@Override
225242
public boolean deleteModelVersion(NameIdentifier ident, String alias) {
226-
eventBus.dispatchEvent(
227-
new DeleteModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident, alias, null));
243+
String user = PrincipalUtils.getCurrentUserName();
244+
245+
eventBus.dispatchEvent(new DeleteModelVersionPreEvent(user, ident, alias, null));
228246
try {
229247
boolean isExists = dispatcher.deleteModelVersion(ident, alias);
230248
// TODO: ModelEvent
231249
return isExists;
232250
} catch (Exception e) {
233-
// TODO: failureEvent
251+
eventBus.dispatchEvent(new DeleteModelVersionFailureEvent(user, ident, e, alias, null));
234252
throw e;
235253
}
236254
}
237255

238256
/** {@inheritDoc} */
239257
@Override
240258
public int[] listModelVersions(NameIdentifier ident) throws NoSuchModelException {
241-
eventBus.dispatchEvent(
242-
new ListModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident));
259+
String user = PrincipalUtils.getCurrentUserName();
260+
261+
eventBus.dispatchEvent(new ListModelVersionPreEvent(user, ident));
243262
try {
244263
int[] versions = dispatcher.listModelVersions(ident);
245264
// TODO: ModelEvent
246265
return versions;
247266
} catch (Exception e) {
248-
// TODO: failureEvent
267+
eventBus.dispatchEvent(new ListModelVersionFailureEvent(user, ident, e));
249268
throw e;
250269
}
251270
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.NameIdentifier;
23+
import org.apache.gravitino.annotation.DeveloperApi;
24+
25+
/**
26+
* Represents an event that is generated when an attempt to drop a model from the schema fails due
27+
* to an exception.
28+
*/
29+
@DeveloperApi
30+
public class DeleteModelFailureEvent extends ModelFailureEvent {
31+
/**
32+
* Construct a new {@link DeleteModelFailureEvent} instance, capturing detailed information about
33+
* the failed attempt to drop a model.
34+
*
35+
* @param user The user who initiated the drop model operation.
36+
* @param identifier The identifier of the model that the operation attempted to drop.
37+
* @param exception The exception that was thrown during the drop model operation, offering
38+
* insights into what went wrong and why the operation failed.
39+
*/
40+
public DeleteModelFailureEvent(String user, NameIdentifier identifier, Exception exception) {
41+
super(user, identifier, exception);
42+
}
43+
44+
/**
45+
* Returns the type of operation.
46+
*
47+
* @return the operation type.
48+
*/
49+
@Override
50+
public OperationType operationType() {
51+
return OperationType.DELETE_MODEL;
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 java.util.Optional;
23+
import org.apache.gravitino.NameIdentifier;
24+
import org.apache.gravitino.annotation.DeveloperApi;
25+
26+
/**
27+
* Represents an event that is generated when an attempt to delete a model version from a model
28+
* fails due to an exception.
29+
*/
30+
@DeveloperApi
31+
public class DeleteModelVersionFailureEvent extends ModelFailureEvent {
32+
private final Optional<String> alias;
33+
private final Optional<Integer> version;
34+
35+
/**
36+
* Constructs a new instance of {@link DeleteModelVersionFailureEvent} instance, capturing
37+
* detailed information about the failed attempt to delete a model version. only one of alias or
38+
* version are valid.
39+
*
40+
* @param user The user who initiated the delete model version operation.
41+
* @param identifier The identifier of the model that the operation attempted to delete a version
42+
* from.
43+
* @param exception The exception that was thrown during the delete model version operation,
44+
* offering insights into what went wrong and why the operation failed.
45+
* @param alias The alias of the model version to be deleted.
46+
* @param version The version of the model version to be deleted.
47+
*/
48+
public DeleteModelVersionFailureEvent(
49+
String user, NameIdentifier identifier, Exception exception, String alias, Integer version) {
50+
super(user, identifier, exception);
51+
52+
this.alias = Optional.ofNullable(alias);
53+
this.version = Optional.ofNullable(version);
54+
}
55+
56+
/**
57+
* Returns the alias of the model version to be deleted.
58+
*
59+
* @return A {@link Optional} instance containing the alias if it was provided, or an empty {@link
60+
* Optional} otherwise.
61+
*/
62+
public Optional<String> alias() {
63+
return alias;
64+
}
65+
66+
/**
67+
* Returns the version of the model version to be deleted.
68+
*
69+
* @return A {@link Optional} instance containing the version if it was provided, or an empty
70+
* {@link Optional} otherwise.
71+
*/
72+
public Optional<Integer> version() {
73+
return version;
74+
}
75+
76+
/**
77+
* Returns the type of operation.
78+
*
79+
* @return the operation type.
80+
*/
81+
@Override
82+
public OperationType operationType() {
83+
return OperationType.DELETE_MODEL_VERSION;
84+
}
85+
}

0 commit comments

Comments
 (0)