Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#6076] improve(CLI): Support model pre event to Gravitino server #6250

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
[#6067] improve(CLI): Support model pre event to Gravitino server
Support model pre event to Gravitino server.

[#6067] improve(CLI): Support model pre event to Gravitino server

add model version related pre event to Gravitino server.

[#6067] improve(CLI): Support model pre event to Gravitino server

add preEvent support for ModelEventDispatcher.

[#6067] improve(CLI): Support model pre event to Gravitino server

add test cases.
  • Loading branch information
Abyss-lord committed Jan 15, 2025
commit 6b6a1542ecf9999d7016a16086cdc75f964cc0ba
Original file line number Diff line number Diff line change
@@ -29,13 +29,21 @@
import org.apache.gravitino.exceptions.NoSuchModelVersionException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
import org.apache.gravitino.listener.api.event.DeleteModelEvent;
import org.apache.gravitino.listener.api.event.DeleteModelPreEvent;
import org.apache.gravitino.listener.api.event.DeleteModelVersionEvent;
import org.apache.gravitino.listener.api.event.DeleteModelVersionPreEvent;
import org.apache.gravitino.listener.api.event.GetModelEvent;
import org.apache.gravitino.listener.api.event.GetModelPreEvent;
import org.apache.gravitino.listener.api.event.GetModelVersionEvent;
import org.apache.gravitino.listener.api.event.GetModelVersionPreEvent;
import org.apache.gravitino.listener.api.event.LinkModelVersionEvent;
import org.apache.gravitino.listener.api.event.LinkModelVersionPreEvent;
import org.apache.gravitino.listener.api.event.ListModelEvent;
import org.apache.gravitino.listener.api.event.ListModelPreEvent;
import org.apache.gravitino.listener.api.event.ListModelVersionsEvent;
import org.apache.gravitino.listener.api.event.ListModelVersionsPreEvent;
import org.apache.gravitino.listener.api.event.RegisterModelEvent;
import org.apache.gravitino.listener.api.event.RegisterModelPreEvent;
import org.apache.gravitino.listener.api.info.ModelInfo;
import org.apache.gravitino.listener.api.info.ModelVersionInfo;
import org.apache.gravitino.model.Model;
@@ -83,7 +91,9 @@ public ModelEventDispatcher(EventBus eventBus, ModelDispatcher dispatcher) {
@Override
public Model registerModel(NameIdentifier ident, String comment, Map<String, String> properties)
throws NoSuchSchemaException, ModelAlreadyExistsException {
// TODO: preEvent
ModelInfo registerRequest = new ModelInfo(ident.name(), properties, comment, null);
eventBus.dispatchEvent(
new RegisterModelPreEvent(PrincipalUtils.getCurrentUserName(), ident, registerRequest));
try {
Model model = dispatcher.registerModel(ident, comment, properties);
ModelInfo modelInfo = new ModelInfo(model);
@@ -137,7 +147,7 @@ public Model registerModel(
*/
@Override
public Model getModel(NameIdentifier ident) throws NoSuchModelException {
// TODO: preEvent
eventBus.dispatchEvent(new GetModelPreEvent(PrincipalUtils.getCurrentUserName(), ident));
try {
Model model = dispatcher.getModel(ident);
ModelInfo modelInfo = new ModelInfo(model);
@@ -159,7 +169,7 @@ public Model getModel(NameIdentifier ident) throws NoSuchModelException {
*/
@Override
public boolean deleteModel(NameIdentifier ident) {
// TODO: preEvent
eventBus.dispatchEvent(new DeleteModelPreEvent(PrincipalUtils.getCurrentUserName(), ident));
try {
boolean isExists = dispatcher.deleteModel(ident);
eventBus.dispatchEvent(
@@ -180,7 +190,7 @@ public boolean deleteModel(NameIdentifier ident) {
*/
@Override
public NameIdentifier[] listModels(Namespace namespace) throws NoSuchSchemaException {
// TODO: preEvent
eventBus.dispatchEvent(new ListModelPreEvent(PrincipalUtils.getCurrentUserName(), namespace));
try {
NameIdentifier[] models = dispatcher.listModels(namespace);
eventBus.dispatchEvent(new ListModelEvent(PrincipalUtils.getCurrentUserName(), namespace));
@@ -216,14 +226,15 @@ public void linkModelVersion(
String comment,
Map<String, String> properties)
throws NoSuchModelException, ModelVersionAliasesAlreadyExistException {
// TODO: preEvent
Model model = dispatcher.getModel(ident);
ModelVersionInfo modelVersionInfo = new ModelVersionInfo(uri, aliases, comment, properties);
ModelInfo linkRequest = new ModelInfo(model, new ModelVersionInfo[] {modelVersionInfo});
eventBus.dispatchEvent(
new LinkModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident, linkRequest));
try {
Model model = dispatcher.getModel(ident);
ModelVersionInfo modelVersionInfo = new ModelVersionInfo(uri, aliases, comment, properties);
ModelInfo modelInfo = new ModelInfo(model, new ModelVersionInfo[] {modelVersionInfo});
dispatcher.linkModelVersion(ident, uri, aliases, comment, properties);
eventBus.dispatchEvent(
new LinkModelVersionEvent(PrincipalUtils.getCurrentUserName(), ident, modelInfo));
new LinkModelVersionEvent(PrincipalUtils.getCurrentUserName(), ident, linkRequest));
} catch (Exception e) {
// TODO: failureEvent
throw e;
@@ -241,7 +252,7 @@ public void linkModelVersion(
@Override
public ModelVersion getModelVersion(NameIdentifier ident, int version)
throws NoSuchModelVersionException {
// TODO: preEvent
eventBus.dispatchEvent(new GetModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident));
try {
ModelVersion modelVersion = dispatcher.getModelVersion(ident, version);
ModelInfo modelInfo = getModelInfo(ident, version);
@@ -265,7 +276,7 @@ public ModelVersion getModelVersion(NameIdentifier ident, int version)
@Override
public ModelVersion getModelVersion(NameIdentifier ident, String alias)
throws NoSuchModelVersionException {
// TODO: preEvent
eventBus.dispatchEvent(new GetModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident));
try {
ModelVersion modelVersion = dispatcher.getModelVersion(ident, alias);
ModelInfo modelInfo = getModelInfo(ident, alias);
@@ -288,7 +299,8 @@ public ModelVersion getModelVersion(NameIdentifier ident, String alias)
*/
@Override
public boolean deleteModelVersion(NameIdentifier ident, int version) {
// TODO: preEvent
eventBus.dispatchEvent(
new DeleteModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident));
try {
boolean isExists = dispatcher.deleteModelVersion(ident, version);
eventBus.dispatchEvent(
@@ -310,7 +322,8 @@ public boolean deleteModelVersion(NameIdentifier ident, int version) {
*/
@Override
public boolean deleteModelVersion(NameIdentifier ident, String alias) {
// TODO: preEvent
eventBus.dispatchEvent(
new DeleteModelVersionPreEvent(PrincipalUtils.getCurrentUserName(), ident));
try {
boolean isExists = dispatcher.deleteModelVersion(ident, alias);
eventBus.dispatchEvent(
@@ -331,11 +344,12 @@ public boolean deleteModelVersion(NameIdentifier ident, String alias) {
*/
@Override
public int[] listModelVersions(NameIdentifier ident) throws NoSuchModelException {
// TODO: preEvent
Model model = dispatcher.getModel(ident);
ModelInfo modelInfo = new ModelInfo(model);
eventBus.dispatchEvent(
new ListModelVersionsPreEvent(PrincipalUtils.getCurrentUserName(), ident, modelInfo));
try {
int[] versions = dispatcher.listModelVersions(ident);
Model model = dispatcher.getModel(ident);
ModelInfo modelInfo = new ModelInfo(model);
eventBus.dispatchEvent(
new ListModelVersionsEvent(PrincipalUtils.getCurrentUserName(), ident, modelInfo));
return versions;
@@ -383,15 +397,15 @@ public boolean modelVersionExists(NameIdentifier ident, String alias) {
}

private ModelInfo getModelInfo(NameIdentifier modelIdent, int version) {
Model model = getModel(modelIdent);
Model model = dispatcher.getModel(modelIdent);
ModelVersion modelVersion = dispatcher.getModelVersion(modelIdent, version);
ModelVersionInfo modelVersionInfo = new ModelVersionInfo(modelVersion);

return new ModelInfo(model, new ModelVersionInfo[] {modelVersionInfo});
}

private ModelInfo getModelInfo(NameIdentifier modelIdent, String alias) {
Model model = getModel(modelIdent);
Model model = dispatcher.getModel(modelIdent);
ModelVersion modelVersion = dispatcher.getModelVersion(modelIdent, alias);
ModelVersionInfo modelVersionInfo = new ModelVersionInfo(modelVersion);

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.NameIdentifier;
import org.apache.gravitino.annotation.DeveloperApi;

/** Represents an event triggered before deleting a model. */
@DeveloperApi
public class DeleteModelPreEvent extends ModelPreEvent {

/**
* Create a new {@link DeleteModelPreEvent} instance.
*
* @param user the user who triggered the event.
* @param identifier the identifier of the model being operated on.
*/
public DeleteModelPreEvent(String user, NameIdentifier identifier) {
super(user, identifier);
}

/**
* Returns the type of operation.
*
* @return the operation type.
*/
@Override
public OperationType operationType() {
return OperationType.DELETE_MODEL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.NameIdentifier;
import org.apache.gravitino.annotation.DeveloperApi;

/** Represents an event triggered before deleting a model version. */
@DeveloperApi
public class DeleteModelVersionPreEvent extends ModelPreEvent {

/**
* Create a new {@link DeleteModelVersionPreEvent} instance.
*
* @param user The username of the individual who initiated the model version linking.
* @param identifier The unique identifier of the model that was linked.
*/
public DeleteModelVersionPreEvent(String user, NameIdentifier identifier) {
super(user, identifier);
}

/**
* Returns the type of operation.
*
* @return the operation type.
*/
@Override
public OperationType operationType() {
return OperationType.DELETE_MODEL_VERSION;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.NameIdentifier;

/** Represents an event triggered before getting a model. */
public class GetModelPreEvent extends ModelPreEvent {
/**
* Create a new {@link GetModelPreEvent} instance.
*
* @param user the user who triggered the event.
* @param identifier the identifier of the model being operated on.
*/
public GetModelPreEvent(String user, NameIdentifier identifier) {
super(user, identifier);
}

/**
* Returns the type of operation.
*
* @return the operation type.
*/
@Override
public OperationType operationType() {
return OperationType.GET_MODEL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.NameIdentifier;
import org.apache.gravitino.annotation.DeveloperApi;

/** Represents an event triggered before getting the version of a model. */
@DeveloperApi
public class GetModelVersionPreEvent extends ModelPreEvent {

/**
* Create a new {@link GetModelVersionPreEvent} instance.
*
* @param user The username of the individual who initiated the model version linking.
* @param identifier The unique identifier of the model that was linked.
*/
public GetModelVersionPreEvent(String user, NameIdentifier identifier) {
super(user, identifier);
}

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