Skip to content

Commit c9c9a99

Browse files
Abyss-lordFourFriendsluoxin5diqiu50yuqi1129
authored
[#6076] improve(core): Support model pre event to Gravitino server (#6250)
### What changes were proposed in this pull request? Support model pre event to Gravitino server, based on #6129 , Both synchronize Dispatcher changes with each other https://docs.google.com/document/d/1_aVCd_tKiEebpzp9tg07Lzdk1j6EalNn8YOIdRn-3z4/edit?tab=t.0#heading=h.k85t4bueowc5 #### PreEvent | PreEvent | OperationType | ModelCatalog | | --- | --- | --- | | `RegisterModelPreEvent` | `REGISTER_MODEL` | `registerModel` | | `GetModelPreEvent` | `LOAD_MODEL` | `getModel` | | `DeleteModelEvent` | `Delete_MODEL` | `deleteModel` | | `ListModelPreEvent` | `LIST_MODEL` | `listModels` | | `LinkModelVersionPreEvent` | `LINK_MODEL_VERSION` | `linkModelVersion` | | `GetModelVersionPreEvent` | `GET_MODEL_VERSION` | `getModelVersion` | | `DeleteModelVersionPreEvent` | `Delete_MODEL_VERSION` | `deleteModelVersion` | | `ListModelVersionsPreEvent` | `LIST_MODEL_VERSIONS` | `listModelVersions` | #### ModelEventDispatcher ![image](https://github.com/user-attachments/assets/ad80c319-f17f-4c76-9b65-b42c023075cb) ### Why are the changes needed? Fix: #6076 ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? #### Model Event 1. `testRegisterModelEvent` 2. `testGetModelEvent` 3. `testDeleteExistsModelEvent` 4. `testDeleteNotExistsModelEvent` 5. `testListModelEvent` #### Model Version Event 1. `testLinkModelVersionEvent` 2. `testGetModelVersionEventViaVersion` 3. `testGetModelVersionEventViaAlias` 4. `testDeleteModelVersionEventViaVersion` 5. `testDeleteModelVersionEventViaAlias` 6. `testDeleteModelVersionEventViaVersionNotExists` 7. `testListModelVersionsEvent` --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: luoxin <34674439+FourFriends@users.noreply.github.com> Co-authored-by: luoxin5 <luoxin5@xiaomi.com> Co-authored-by: Yuhui <hui@datastrato.com> Co-authored-by: Qi Yu <yuqi@datastrato.com> Co-authored-by: YangJie <yangjie01@baidu.com> Co-authored-by: this-user <this-user@users.noreply.github.com> Co-authored-by: Qian Xia <lauraxiaqian@gmail.com> Co-authored-by: vitamin43 <104159582+vitamin43@users.noreply.github.com> Co-authored-by: roryqi <roryqi@apache.org> Co-authored-by: Pranay Kumar Karvi <pranaykarvi@gmail.com> Co-authored-by: AndreVale69 <57899285+AndreVale69@users.noreply.github.com> Co-authored-by: Abdullah Javed <65340910+JavedAbdullah@users.noreply.github.com> Co-authored-by: Brijesh Thummar <brijeshthummar02@gmail.com> Co-authored-by: Zhengke Zhou <madzhou1@gmail.com> Co-authored-by: Justin Mclean <justin@classsoftware.com> Co-authored-by: Jerry Shao <jerryshao@datastrato.com> Co-authored-by: Kang <zhoukangcn@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: FANNG <xiaojing@datastrato.com> Co-authored-by: Eric Chang <e850506@gmail.com> Co-authored-by: Tianhang <58762426+TEOTEO520@users.noreply.github.com> Co-authored-by: teo <litianhang@bilibili.com> Co-authored-by: teo <litianhang@bilibili.co>
1 parent 10252e7 commit c9c9a99

16 files changed

+1565
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
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;
21+
22+
import java.util.Map;
23+
import org.apache.gravitino.NameIdentifier;
24+
import org.apache.gravitino.Namespace;
25+
import org.apache.gravitino.catalog.ModelDispatcher;
26+
import org.apache.gravitino.exceptions.ModelAlreadyExistsException;
27+
import org.apache.gravitino.exceptions.ModelVersionAliasesAlreadyExistException;
28+
import org.apache.gravitino.exceptions.NoSuchModelException;
29+
import org.apache.gravitino.exceptions.NoSuchModelVersionException;
30+
import org.apache.gravitino.exceptions.NoSuchSchemaException;
31+
import org.apache.gravitino.listener.api.event.DeleteModelPreEvent;
32+
import org.apache.gravitino.listener.api.event.DeleteModelVersionPreEvent;
33+
import org.apache.gravitino.listener.api.event.GetModelPreEvent;
34+
import org.apache.gravitino.listener.api.event.GetModelVersionPreEvent;
35+
import org.apache.gravitino.listener.api.event.LinkModelVersionPreEvent;
36+
import org.apache.gravitino.listener.api.event.ListModelPreEvent;
37+
import org.apache.gravitino.listener.api.event.ListModelVersionPreEvent;
38+
import org.apache.gravitino.listener.api.event.RegisterAndLinkModelPreEvent;
39+
import org.apache.gravitino.listener.api.event.RegisterModelPreEvent;
40+
import org.apache.gravitino.listener.api.info.ModelInfo;
41+
import org.apache.gravitino.listener.api.info.ModelVersionInfo;
42+
import org.apache.gravitino.model.Model;
43+
import org.apache.gravitino.model.ModelVersion;
44+
import org.apache.gravitino.utils.PrincipalUtils;
45+
46+
/**
47+
* {@code ModelEventDispatcher} is a decorator for {@link ModelDispatcher} that not only delegates
48+
* model operations to the underlying catalog dispatcher but also dispatches corresponding events to
49+
* an {@link org.apache.gravitino.listener.EventBus} after each operation is completed. This allows
50+
* for event-driven workflows or monitoring of model operations.
51+
*/
52+
public class ModelEventDispatcher implements ModelDispatcher {
53+
private final EventBus eventBus;
54+
private final ModelDispatcher dispatcher;
55+
56+
/**
57+
* Constructs a {@link ModelEventDispatcher} with a specified EventBus and {@link
58+
* ModelDispatcher}.
59+
*
60+
* @param eventBus The EventBus to which events will be dispatched.
61+
* @param dispatcher The underlying {@link ModelDispatcher} that will perform the actual model
62+
* operations.
63+
*/
64+
public ModelEventDispatcher(EventBus eventBus, ModelDispatcher dispatcher) {
65+
this.eventBus = eventBus;
66+
this.dispatcher = dispatcher;
67+
}
68+
69+
/** {@inheritDoc} */
70+
@Override
71+
public Model registerModel(NameIdentifier ident, String comment, Map<String, String> properties)
72+
throws NoSuchSchemaException, ModelAlreadyExistsException {
73+
ModelInfo registerRequest = new ModelInfo(ident.name(), properties, comment);
74+
eventBus.dispatchEvent(
75+
new RegisterModelPreEvent(PrincipalUtils.getCurrentUserName(), ident, registerRequest));
76+
try {
77+
Model model = dispatcher.registerModel(ident, comment, properties);
78+
// TODO: ModelEvent
79+
return model;
80+
} catch (Exception e) {
81+
// TODO: failureEvent
82+
throw e;
83+
}
84+
}
85+
86+
/** {@inheritDoc} */
87+
@Override
88+
public Model registerModel(
89+
NameIdentifier ident,
90+
String uri,
91+
String[] aliases,
92+
String comment,
93+
Map<String, String> properties)
94+
throws NoSuchSchemaException, ModelAlreadyExistsException,
95+
ModelVersionAliasesAlreadyExistException {
96+
ModelInfo registerModelRequest = new ModelInfo(ident.name(), properties, comment);
97+
ModelVersionInfo linkModelVersionRequest =
98+
new ModelVersionInfo(uri, comment, properties, aliases);
99+
100+
RegisterAndLinkModelPreEvent registerAndLinkModelPreEvent =
101+
new RegisterAndLinkModelPreEvent(
102+
PrincipalUtils.getCurrentUserName(),
103+
ident,
104+
registerModelRequest,
105+
linkModelVersionRequest);
106+
eventBus.dispatchEvent(registerAndLinkModelPreEvent);
107+
try {
108+
// TODO: ModelEvent
109+
return dispatcher.registerModel(ident, uri, aliases, comment, properties);
110+
} catch (Exception e) {
111+
throw e;
112+
}
113+
}
114+
115+
/** {@inheritDoc} */
116+
@Override
117+
public Model getModel(NameIdentifier ident) throws NoSuchModelException {
118+
eventBus.dispatchEvent(new GetModelPreEvent(PrincipalUtils.getCurrentUserName(), ident));
119+
try {
120+
Model model = dispatcher.getModel(ident);
121+
// TODO: ModelEvent
122+
return model;
123+
} catch (Exception e) {
124+
// TODO: failureEvent
125+
throw e;
126+
}
127+
}
128+
129+
/** {@inheritDoc} */
130+
@Override
131+
public boolean deleteModel(NameIdentifier ident) {
132+
eventBus.dispatchEvent(new DeleteModelPreEvent(PrincipalUtils.getCurrentUserName(), ident));
133+
try {
134+
// TODO: ModelEvent
135+
return dispatcher.deleteModel(ident);
136+
} catch (Exception e) {
137+
// TODO: failureEvent
138+
throw e;
139+
}
140+
}
141+
142+
/** {@inheritDoc} */
143+
@Override
144+
public NameIdentifier[] listModels(Namespace namespace) throws NoSuchSchemaException {
145+
eventBus.dispatchEvent(new ListModelPreEvent(PrincipalUtils.getCurrentUserName(), namespace));
146+
try {
147+
NameIdentifier[] models = dispatcher.listModels(namespace);
148+
// TODO: ModelEvent
149+
return models;
150+
} catch (Exception e) {
151+
// TODO: failureEvent
152+
throw e;
153+
}
154+
}
155+
156+
/** {@inheritDoc} */
157+
@Override
158+
public void linkModelVersion(
159+
NameIdentifier ident,
160+
String uri,
161+
String[] aliases,
162+
String comment,
163+
Map<String, String> properties)
164+
throws NoSuchModelException, ModelVersionAliasesAlreadyExistException {
165+
ModelVersionInfo linkModelRequest = new ModelVersionInfo(uri, comment, properties, aliases);
166+
eventBus.dispatchEvent(
167+
new LinkModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident, linkModelRequest));
168+
try {
169+
dispatcher.linkModelVersion(ident, uri, aliases, comment, properties);
170+
// TODO: ModelEvent
171+
} catch (Exception e) {
172+
// TODO: failureEvent
173+
throw e;
174+
}
175+
}
176+
177+
/** {@inheritDoc} */
178+
@Override
179+
public ModelVersion getModelVersion(NameIdentifier ident, int version)
180+
throws NoSuchModelVersionException {
181+
eventBus.dispatchEvent(
182+
new GetModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident, null, version));
183+
try {
184+
// TODO: ModelEvent
185+
return dispatcher.getModelVersion(ident, version);
186+
} catch (Exception e) {
187+
// TODO: failureEvent
188+
throw e;
189+
}
190+
}
191+
192+
/** {@inheritDoc} */
193+
@Override
194+
public ModelVersion getModelVersion(NameIdentifier ident, String alias)
195+
throws NoSuchModelVersionException {
196+
eventBus.dispatchEvent(
197+
new GetModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident, alias, null));
198+
try {
199+
ModelVersion modelVersion = dispatcher.getModelVersion(ident, alias);
200+
// TODO: ModelEvent
201+
return modelVersion;
202+
} catch (Exception e) {
203+
// TODO: failureEvent
204+
throw e;
205+
}
206+
}
207+
208+
/** {@inheritDoc} */
209+
@Override
210+
public boolean deleteModelVersion(NameIdentifier ident, int version) {
211+
eventBus.dispatchEvent(
212+
new DeleteModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident, null, version));
213+
try {
214+
boolean isExists = dispatcher.deleteModelVersion(ident, version);
215+
// TODO: ModelEvent
216+
return isExists;
217+
} catch (Exception e) {
218+
// TODO: failureEvent
219+
throw e;
220+
}
221+
}
222+
223+
/** {@inheritDoc} */
224+
@Override
225+
public boolean deleteModelVersion(NameIdentifier ident, String alias) {
226+
eventBus.dispatchEvent(
227+
new DeleteModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident, alias, null));
228+
try {
229+
boolean isExists = dispatcher.deleteModelVersion(ident, alias);
230+
// TODO: ModelEvent
231+
return isExists;
232+
} catch (Exception e) {
233+
// TODO: failureEvent
234+
throw e;
235+
}
236+
}
237+
238+
/** {@inheritDoc} */
239+
@Override
240+
public int[] listModelVersions(NameIdentifier ident) throws NoSuchModelException {
241+
eventBus.dispatchEvent(
242+
new ListModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident));
243+
try {
244+
int[] versions = dispatcher.listModelVersions(ident);
245+
// TODO: ModelEvent
246+
return versions;
247+
} catch (Exception e) {
248+
// TODO: failureEvent
249+
throw e;
250+
}
251+
}
252+
253+
/** {@inheritDoc} */
254+
@Override
255+
public boolean modelExists(NameIdentifier ident) {
256+
return dispatcher.modelExists(ident);
257+
}
258+
259+
/** {@inheritDoc} */
260+
@Override
261+
public boolean modelVersionExists(NameIdentifier ident, int version) {
262+
return dispatcher.modelVersionExists(ident, version);
263+
}
264+
265+
/** {@inheritDoc} */
266+
@Override
267+
public boolean modelVersionExists(NameIdentifier ident, String alias) {
268+
return dispatcher.modelVersionExists(ident, alias);
269+
}
270+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
/** Represents an event triggered before deleting a model. */
26+
@DeveloperApi
27+
public class DeleteModelPreEvent extends ModelPreEvent {
28+
29+
/**
30+
* Create a new {@link DeleteModelPreEvent} instance.
31+
*
32+
* @param user the user who triggered the event.
33+
* @param identifier the identifier of the model being operated on.
34+
*/
35+
public DeleteModelPreEvent(String user, NameIdentifier identifier) {
36+
super(user, identifier);
37+
}
38+
39+
/**
40+
* Returns the type of operation.
41+
*
42+
* @return the operation type.
43+
*/
44+
@Override
45+
public OperationType operationType() {
46+
return OperationType.DELETE_MODEL;
47+
}
48+
}

0 commit comments

Comments
 (0)