Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#7673] feat(model): Add event when getting URI of model version (#7674)

XiaoZ20f76e90e7c2
diff --git a/core/src/main/java/org/apache/gravitino/catalog/ModelNormalizeDispatcher.java b/core/src/main/java/org/apache/gravitino/catalog/ModelNormalizeDispatcher.java
index 5a8d3267d4..73c4935a03 100644
--- a/core/src/main/java/org/apache/gravitino/catalog/ModelNormalizeDispatcher.java
+++ b/core/src/main/java/org/apache/gravitino/catalog/ModelNormalizeDispatcher.java
@@ -31,6 +31,7 @@ import org.apache.gravitino.exceptions.ModelAlreadyExistsException;
import org.apache.gravitino.exceptions.ModelVersionAliasesAlreadyExistException;
import org.apache.gravitino.exceptions.NoSuchModelException;
import org.apache.gravitino.exceptions.NoSuchModelVersionException;
+import org.apache.gravitino.exceptions.NoSuchModelVersionURINameException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
import org.apache.gravitino.model.Model;
import org.apache.gravitino.model.ModelChange;
@@ -125,6 +126,18 @@ public class ModelNormalizeDispatcher implements ModelDispatcher {
dispatcher.linkModelVersion(normalizeCaseSensitive(ident), uri, aliases, comment, properties);
}
+ @Override
+ public String getModelVersionUri(NameIdentifier ident, int version, String uriName)
+ throws NoSuchModelVersionException, NoSuchModelVersionURINameException {
+ return dispatcher.getModelVersionUri(normalizeCaseSensitive(ident), version, uriName);
+ }
+
+ @Override
+ public String getModelVersionUri(NameIdentifier ident, String alias, String uriName)
+ throws NoSuchModelVersionException, NoSuchModelVersionURINameException {
+ return dispatcher.getModelVersionUri(normalizeCaseSensitive(ident), alias, uriName);
+ }
+
@Override
public boolean deleteModelVersion(NameIdentifier ident, int version) {
return dispatcher.deleteModelVersion(normalizeCaseSensitive(ident), version);
diff --git a/core/src/main/java/org/apache/gravitino/catalog/ModelOperationDispatcher.java b/core/src/main/java/org/apache/gravitino/catalog/ModelOperationDispatcher.java
index b2de2b20c1..b44a25b75a 100644
--- a/core/src/main/java/org/apache/gravitino/catalog/ModelOperationDispatcher.java
+++ b/core/src/main/java/org/apache/gravitino/catalog/ModelOperationDispatcher.java
@@ -35,6 +35,7 @@ import org.apache.gravitino.exceptions.ModelAlreadyExistsException;
import org.apache.gravitino.exceptions.ModelVersionAliasesAlreadyExistException;
import org.apache.gravitino.exceptions.NoSuchModelException;
import org.apache.gravitino.exceptions.NoSuchModelVersionException;
+import org.apache.gravitino.exceptions.NoSuchModelVersionURINameException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
import org.apache.gravitino.lock.LockType;
import org.apache.gravitino.lock.TreeLockUtils;
@@ -211,6 +212,34 @@ public class ModelOperationDispatcher extends OperationDispatcher implements Mod
ModelVersionAliasesAlreadyExistException.class));
}
+ @Override
+ public String getModelVersionUri(NameIdentifier ident, int version, String uriName)
+ throws NoSuchModelVersionException, NoSuchModelVersionURINameException {
+ return TreeLockUtils.doWithTreeLock(
+ ident,
+ LockType.READ,
+ () ->
+ doWithCatalog(
+ getCatalogIdentifier(ident),
+ c -> c.doWithModelOps(m -> m.getModelVersionUri(ident, version, uriName)),
+ NoSuchModelVersionException.class,
+ NoSuchModelVersionURINameException.class));
+ }
+
+ @Override
+ public String getModelVersionUri(NameIdentifier ident, String alias, String uriName)
+ throws NoSuchModelVersionException, NoSuchModelVersionURINameException {
+ return TreeLockUtils.doWithTreeLock(
+ ident,
+ LockType.READ,
+ () ->
+ doWithCatalog(
+ getCatalogIdentifier(ident),
+ c -> c.doWithModelOps(m -> m.getModelVersionUri(ident, alias, uriName)),
+ NoSuchModelVersionException.class,
+ NoSuchModelVersionURINameException.class));
+ }
+
@Override
public boolean deleteModelVersion(NameIdentifier ident, int version) {
return TreeLockUtils.doWithTreeLock(
diff --git a/core/src/main/java/org/apache/gravitino/hook/ModelHookDispatcher.java b/core/src/main/java/org/apache/gravitino/hook/ModelHookDispatcher.java
index 560caa7522..eb18f704ee 100644
--- a/core/src/main/java/org/apache/gravitino/hook/ModelHookDispatcher.java
+++ b/core/src/main/java/org/apache/gravitino/hook/ModelHookDispatcher.java
@@ -32,6 +32,7 @@ import org.apache.gravitino.exceptions.ModelAlreadyExistsException;
import org.apache.gravitino.exceptions.ModelVersionAliasesAlreadyExistException;
import org.apache.gravitino.exceptions.NoSuchModelException;
import org.apache.gravitino.exceptions.NoSuchModelVersionException;
+import org.apache.gravitino.exceptions.NoSuchModelVersionURINameException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
import org.apache.gravitino.model.Model;
import org.apache.gravitino.model.ModelChange;
@@ -122,6 +123,18 @@ public class ModelHookDispatcher implements ModelDispatcher {
dispatcher.linkModelVersion(ident, uri, aliases, comment, properties);
}
+ @Override
+ public String getModelVersionUri(NameIdentifier ident, String alias, String uriName)
+ throws NoSuchModelVersionException, NoSuchModelVersionURINameException {
+ return dispatcher.getModelVersionUri(ident, alias, uriName);
+ }
+
+ @Override
+ public String getModelVersionUri(NameIdentifier ident, int version, String uriName)
+ throws NoSuchModelVersionException, NoSuchModelVersionURINameException {
+ return dispatcher.getModelVersionUri(ident, version, uriName);
+ }
+
@Override
public boolean deleteModelVersion(NameIdentifier ident, int version) {
return dispatcher.deleteModelVersion(ident, version);
diff --git a/core/src/main/java/org/apache/gravitino/listener/ModelEventDispatcher.java b/core/src/main/java/org/apache/gravitino/listener/ModelEventDispatcher.java
index 3569d66e11..bd51d4680d 100644
--- a/core/src/main/java/org/apache/gravitino/listener/ModelEventDispatcher.java
+++ b/core/src/main/java/org/apache/gravitino/listener/ModelEventDispatcher.java
@@ -28,6 +28,7 @@ import org.apache.gravitino.exceptions.ModelAlreadyExistsException;
import org.apache.gravitino.exceptions.ModelVersionAliasesAlreadyExistException;
import org.apache.gravitino.exceptions.NoSuchModelException;
import org.apache.gravitino.exceptions.NoSuchModelVersionException;
+import org.apache.gravitino.exceptions.NoSuchModelVersionURINameException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
import org.apache.gravitino.listener.api.event.AlterModelEvent;
import org.apache.gravitino.listener.api.event.AlterModelFailureEvent;
@@ -47,6 +48,9 @@ import org.apache.gravitino.listener.api.event.GetModelPreEvent;
import org.apache.gravitino.listener.api.event.GetModelVersionEvent;
import org.apache.gravitino.listener.api.event.GetModelVersionFailureEvent;
import org.apache.gravitino.listener.api.event.GetModelVersionPreEvent;
+import org.apache.gravitino.listener.api.event.GetModelVersionUriEvent;
+import org.apache.gravitino.listener.api.event.GetModelVersionUriFailureEvent;
+import org.apache.gravitino.listener.api.event.GetModelVersionUriPreEvent;
import org.apache.gravitino.listener.api.event.LinkModelVersionEvent;
import org.apache.gravitino.listener.api.event.LinkModelVersionFailureEvent;
import org.apache.gravitino.listener.api.event.LinkModelVersionPreEvent;
@@ -254,6 +258,40 @@ public class ModelEventDispatcher implements ModelDispatcher {
}
}
+ @Override
+ public String getModelVersionUri(NameIdentifier ident, String alias, String uriName)
+ throws NoSuchModelVersionException, NoSuchModelVersionURINameException {
+ String user = PrincipalUtils.getCurrentUserName();
+
+ eventBus.dispatchEvent(new GetModelVersionUriPreEvent(user, ident, alias, null, uriName));
+ try {
+ String uri = dispatcher.getModelVersionUri(ident, alias, uriName);
+ eventBus.dispatchEvent(new GetModelVersionUriEvent(user, ident, alias, null, uriName, uri));
+ return uri;
+ } catch (Exception e) {
+ eventBus.dispatchEvent(
+ new GetModelVersionUriFailureEvent(user, ident, e, alias, null, uriName));
+ throw e;
+ }
+ }
+
+ @Override
+ public String getModelVersionUri(NameIdentifier ident, int version, String uriName)
+ throws NoSuchModelVersionException, NoSuchModelVersionURINameException {
+ String user = PrincipalUtils.getCurrentUserName();
+
+ eventBus.dispatchEvent(new GetModelVersionUriPreEvent(user, ident, null, version, uriName));
+ try {
+ String uri = dispatcher.getModelVersionUri(ident, version, uriName);
+ eventBus.dispatchEvent(new GetModelVersionUriEvent(user, ident, null, version, uriName, uri));
+ return uri;
+ } catch (Exception e) {
+ eventBus.dispatchEvent(
+ new GetModelVersionUriFailureEvent(user, ident, e, null, version, uriName));
+ throw e;
+ }
+ }
+
/** {@inheritDoc} */
@Override
public boolean deleteModelVersion(NameIdentifier ident, int version) {
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/GetModelVersionUriEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/GetModelVersionUriEvent.java
new file mode 100644
index 0000000000..ccd86904ba
--- /dev/null
+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/GetModelVersionUriEvent.java
@@ -0,0 +1,107 @@
+/*
+ * 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 java.util.Optional;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.annotation.DeveloperApi;
+
+/** Represents an event triggered after the URI of a model version is successfully retrieved. */
+@DeveloperApi
+public class GetModelVersionUriEvent extends ModelEvent {
+ private final Optional<String> alias;
+ private final Optional<Integer> version;
+ private final String uriName;
+ private final String uri;
+
+ /**
+ * Create a new {@link GetModelVersionUriEvent} instance, containing the details of the event.
+ * Only one of alias or version are valid.
+ *
+ * @param user The username of the individual who initiated the model version URI to get.
+ * @param identifier The unique identifier of the model that was getting the version URI.
+ * @param alias The alias of the model version to get.
+ * @param version The version of the model version to get.
+ * @param uriName The URI name of the model version to get.
+ * @param uri The URI of the model version.
+ */
+ public GetModelVersionUriEvent(
+ String user,
+ NameIdentifier identifier,
+ String alias,
+ Integer version,
+ String uriName,
+ String uri) {
+ super(user, identifier);
+
+ this.alias = Optional.ofNullable(alias);
+ this.version = Optional.ofNullable(version);
+ this.uriName = uriName;
+ this.uri = uri;
+ }
+
+ /**
+ * Returns the alias of the model version to be accessed.
+ *
+ * @return A {@link Optional} instance containing the alias if it was provided, or an empty {@link
+ * Optional} otherwise.
+ */
+ public Optional<String> alias() {
+ return alias;
+ }
+
+ /**
+ * Returns the version number of the model version to be accessed.
+ *
+ * @return A {@link Optional} instance containing the version if it was provided, or an empty
+ * {@link Optional} otherwise.
+ */
+ public Optional<Integer> version() {
+ return version;
+ }
+
+ /**
+ * Returns the URI name of the model version to be accessed.
+ *
+ * @return The name of the URI.
+ */
+ public String uriName() {
+ return uriName;
+ }
+
+ /**
+ * Returns the URI of the model version to be accessed.
+ *
+ * @return The URI of the model version.
+ */
+ public String uri() {
+ return uri;
+ }
+
+ /**
+ * Returns the type of operation.
+ *
+ * @return the operation type.
+ */
+ @Override
+ public OperationType operationType() {
+ return OperationType.GET_MODEL_VERSION_URI;
+ }
+}
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/GetModelVersionUriFailureEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/GetModelVersionUriFailureEvent.java
new file mode 100644
index 0000000000..78a281afac
--- /dev/null
+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/GetModelVersionUriFailureEvent.java
@@ -0,0 +1,100 @@
+/*
+ * 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 java.util.Optional;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.annotation.DeveloperApi;
+
+/**
+ * Represents an event that is generated when an attempt to get URI of a model version fails due to
+ * an exception.
+ */
+@DeveloperApi
+public class GetModelVersionUriFailureEvent extends ModelFailureEvent {
+ private final Optional<String> alias;
+ private final Optional<Integer> version;
+ private final String uriName;
+
+ /**
+ * Construct a new {@link GetModelVersionUriFailureEvent} instance, capturing detailed information
+ * about the failed attempt to get URI of a model version. only one of alias or version are valid.
+ *
+ * @param user The user associated with the failed model operation.
+ * @param identifier The identifier of the model that was involved in the failed operation.
+ * @param exception The exception that was thrown during the get model version operation, offering
+ * insights into what went wrong and why the operation failed.
+ * @param alias The alias of the model version to get.
+ * @param version The version of the model version to get.
+ * @param uriName The URI name of the model version to get.
+ */
+ public GetModelVersionUriFailureEvent(
+ String user,
+ NameIdentifier identifier,
+ Exception exception,
+ String alias,
+ Integer version,
+ String uriName) {
+ super(user, identifier, exception);
+
+ this.alias = Optional.ofNullable(alias);
+ this.version = Optional.ofNullable(version);
+ this.uriName = uriName;
+ }
+
+ /**
+ * Returns the alias of the model version to be accessed.
+ *
+ * @return A {@link Optional} instance containing the alias if it was provided, or an empty {@link
+ * Optional} otherwise.
+ */
+ public Optional<String> alias() {
+ return alias;
+ }
+
+ /**
+ * Returns the version of the model version to be accessed.
+ *
+ * @return A {@link Optional} instance containing the version if it was provided, or an empty
+ * {@link Optional} otherwise.
+ */
+ public Optional<Integer> version() {
+ return version;
+ }
+
+ /**
+ * Returns the URI name of the model version to be accessed.
+ *
+ * @return The name of the URI.
+ */
+ public String uriName() {
+ return uriName;
+ }
+
+ /**
+ * Returns the type of operation.
+ *
+ * @return the operation type.
+ */
+ @Override
+ public OperationType operationType() {
+ return OperationType.GET_MODEL_VERSION_URI;
+ }
+}
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/GetModelVersionUriPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/GetModelVersionUriPreEvent.java
new file mode 100644
index 0000000000..dbf72ba4e1
--- /dev/null
+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/GetModelVersionUriPreEvent.java
@@ -0,0 +1,90 @@
+/*
+ * 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 java.util.Optional;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.annotation.DeveloperApi;
+
+/** Represents an event triggered before getting the URI of a model version. */
+@DeveloperApi
+public class GetModelVersionUriPreEvent extends ModelPreEvent {
+ private final Optional<String> alias;
+ private final Optional<Integer> version;
+ private final String uriName;
+
+ /**
+ * Create a new {@link GetModelVersionUriPreEvent} instance with alias, version and uriName
+ * arguments. Only one of alias or version are valid.
+ *
+ * @param user The username of the individual who initiated the model version URI to get.
+ * @param identifier The unique identifier of the model that was getting the version URI.
+ * @param alias The alias of the model version to get.
+ * @param version The version of the model version to get.
+ * @param uriName The URI name of the model version to get.
+ */
+ public GetModelVersionUriPreEvent(
+ String user, NameIdentifier identifier, String alias, Integer version, String uriName) {
+ super(user, identifier);
+
+ this.alias = Optional.ofNullable(alias);
+ this.version = Optional.ofNullable(version);
+ this.uriName = uriName;
+ }
+
+ /**
+ * Returns the alias of the model version to be accessed.
+ *
+ * @return A {@link Optional} instance containing the alias if it was provided, or an empty {@link
+ * Optional} otherwise.
+ */
+ public Optional<String> alias() {
+ return alias;
+ }
+
+ /**
+ * Returns the version number of the model version to be accessed.
+ *
+ * @return A {@link Optional} instance containing the version if it was provided, or an empty
+ * {@link Optional} otherwise.
+ */
+ public Optional<Integer> version() {
+ return version;
+ }
+
+ /**
+ * Returns the URI name of the model version to be accessed.
+ *
+ * @return The name of the URI.
+ */
+ public String uriName() {
+ return uriName;
+ }
+
+ /**
+ * Returns the type of operation.
+ *
+ * @return the operation type.
+ */
+ @Override
+ public OperationType operationType() {
+ return OperationType.GET_MODEL_VERSION_URI;
+ }
+}
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/OperationType.java b/core/src/main/java/org/apache/gravitino/listener/api/event/OperationType.java
index d217abeb37..3c7b1439b1 100644
--- a/core/src/main/java/org/apache/gravitino/listener/api/event/OperationType.java
+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/OperationType.java
@@ -111,6 +111,7 @@ public enum OperationType {
LINK_MODEL_VERSION,
DELETE_MODEL_VERSION,
GET_MODEL_VERSION,
+ GET_MODEL_VERSION_URI,
LIST_MODEL_VERSIONS,
LIST_MODEL_VERSION_INFOS,
REGISTER_AND_LINK_MODEL_VERSION,
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/RegisterAndLinkModelEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/RegisterAndLinkModelEvent.java
index 80abd1bdca..7fc260dd5a 100644
--- a/core/src/main/java/org/apache/gravitino/listener/api/event/RegisterAndLinkModelEvent.java
+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/RegisterAndLinkModelEvent.java
@@ -19,8 +19,11 @@
package org.apache.gravitino.listener.api.event;
+import com.google.common.collect.ImmutableMap;
+import java.util.Map;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.listener.api.info.ModelInfo;
+import org.apache.gravitino.model.ModelVersion;
/**
* Represents an event that is generated after a model is successfully registered and a model
@@ -28,7 +31,7 @@ import org.apache.gravitino.listener.api.info.ModelInfo;
*/
public class RegisterAndLinkModelEvent extends ModelEvent {
private final ModelInfo registeredModelInfo;
- private final String uri;
+ private final Map<String, String> uris;
/**
* Constructs a new instance of {@link RegisterAndLinkModelEvent}, capturing the user, identifier,
@@ -38,14 +41,35 @@ public class RegisterAndLinkModelEvent extends ModelEvent {
* @param identifier The identifier of the Model involved in the operation. This encapsulates some
* information.
* @param registeredModelInfo The final state of the model post-creation.
- * @param uri The uri of the linked model version.
+ * @param uri The URI of the linked model version.
*/
public RegisterAndLinkModelEvent(
String user, NameIdentifier identifier, ModelInfo registeredModelInfo, String uri) {
super(user, identifier);
this.registeredModelInfo = registeredModelInfo;
- this.uri = uri;
+ this.uris = ImmutableMap.of(ModelVersion.URI_NAME_UNKNOWN, uri);
+ }
+
+ /**
+ * Constructs a new instance of {@link RegisterAndLinkModelEvent}, capturing the user, identifier,
+ * registered model information, and linked model version information.
+ *
+ * @param user The user responsible for triggering the model operation.
+ * @param identifier The identifier of the Model involved in the operation. This encapsulates some
+ * information.
+ * @param registeredModelInfo The final state of the model post-creation.
+ * @param uris The URIs of the linked model version.
+ */
+ public RegisterAndLinkModelEvent(
+ String user,
+ NameIdentifier identifier,
+ ModelInfo registeredModelInfo,
+ Map<String, String> uris) {
+ super(user, identifier);
+
+ this.registeredModelInfo = registeredModelInfo;
+ this.uris = uris;
}
/**
@@ -58,12 +82,21 @@ public class RegisterAndLinkModelEvent extends ModelEvent {
}
/**
- * Retrieves the uri of the linked model version.
+ * Retrieves the unknown URI of the linked model version.
*
- * @return the uri of the linked model version
+ * @return the unknown URI of the linked model version
*/
public String uri() {
- return uri;
+ return uris.get(ModelVersion.URI_NAME_UNKNOWN);
+ }
+
+ /**
+ * Retrieves the URIs of the linked model version.
+ *
+ * @return the URIs of the linked model version
+ */
+ public Map<String, String> uris() {
+ return uris;
}
/**
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/info/ModelVersionInfo.java b/core/src/main/java/org/apache/gravitino/listener/api/info/ModelVersionInfo.java
index 0b690bd160..bfcccb9caa 100644
--- a/core/src/main/java/org/apache/gravitino/listener/api/info/ModelVersionInfo.java
+++ b/core/src/main/java/org/apache/gravitino/listener/api/info/ModelVersionInfo.java
@@ -20,6 +20,7 @@
package org.apache.gravitino.listener.api.info;
import com.google.common.collect.ImmutableMap;
+import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import org.apache.gravitino.Audit;
@@ -32,7 +33,7 @@ import org.apache.gravitino.model.ModelVersion;
*/
@DeveloperApi
public class ModelVersionInfo {
- private final String uri;
+ private final Map<String, String> uris;
private final Map<String, String> properties;
private final Optional<String[]> aliases;
@@ -46,7 +47,7 @@ public class ModelVersionInfo {
*/
public ModelVersionInfo(ModelVersion modelVersion) {
this(
- modelVersion.uri(),
+ ImmutableMap.of(ModelVersion.URI_NAME_UNKNOWN, modelVersion.uri()),
modelVersion.comment(),
modelVersion.properties(),
modelVersion.aliases(),
@@ -68,7 +69,7 @@ public class ModelVersionInfo {
}
/**
- * Constructs a {@link ModelVersionInfo} instance based on given uri, comment, properties,
+ * Constructs a {@link ModelVersionInfo} instance based on given URI, comment, properties,
* aliases, and audit information.
*
* @param uri The URI of the model version.
@@ -83,8 +84,31 @@ public class ModelVersionInfo {
Map<String, String> properties,
String[] aliases,
Audit auditInfo) {
- this.uri = uri;
+ this.uris =
+ uri == null ? Collections.emptyMap() : ImmutableMap.of(ModelVersion.URI_NAME_UNKNOWN, uri);
+ this.properties = properties == null ? ImmutableMap.of() : ImmutableMap.copyOf(properties);
+ this.comment = Optional.ofNullable(comment);
+ this.audit = Optional.ofNullable(auditInfo);
+ this.aliases = Optional.ofNullable(aliases);
+ }
+ /**
+ * Constructs a {@link ModelVersionInfo} instance based on given URIs, comment, properties,
+ * aliases, and audit information.
+ *
+ * @param uris The URIs of the model version.
+ * @param comment The comment of the model version.
+ * @param properties The properties of the model version.
+ * @param aliases The aliases of the model version.
+ * @param auditInfo The audit information of the model version.
+ */
+ public ModelVersionInfo(
+ Map<String, String> uris,
+ String comment,
+ Map<String, String> properties,
+ String[] aliases,
+ Audit auditInfo) {
+ this.uris = uris;
this.properties = properties == null ? ImmutableMap.of() : ImmutableMap.copyOf(properties);
this.comment = Optional.ofNullable(comment);
this.audit = Optional.ofNullable(auditInfo);
@@ -92,12 +116,21 @@ public class ModelVersionInfo {
}
/**
- * Returns the URI of the model version.
+ * Returns the unknown URI of the model version.
*
- * @return the URI of the model version.
+ * @return the unknown URI of the model version.
*/
public String uri() {
- return uri;
+ return uris.get(ModelVersion.URI_NAME_UNKNOWN);
+ }
+
+ /**
+ * Returns the URIs of the model version.
+ *
+ * @return the URIs of the model version.
+ */
+ public Map<String, String> uris() {
+ return uris;
}
/**
diff --git a/core/src/test/java/org/apache/gravitino/listener/api/event/TestModelEvent.java b/core/src/test/java/org/apache/gravitino/listener/api/event/TestModelEvent.java
index 8435cbf5dc..3b4f7b4766 100644
--- a/core/src/test/java/org/apache/gravitino/listener/api/event/TestModelEvent.java
+++ b/core/src/test/java/org/apache/gravitino/listener/api/event/TestModelEvent.java
@@ -238,7 +238,8 @@ public class TestModelEvent {
ModelVersionInfo linkModelVersionRequest =
registerAndLinkModelPreEvent.linkModelVersionRequest();
- Assertions.assertEquals(firstModelVersion.uri(), linkModelVersionRequest.uri());
+ Assertions.assertEquals(
+ firstModelVersion.uri(), linkModelVersionRequest.uris().get(ModelVersion.URI_NAME_UNKNOWN));
Assertions.assertEquals("commentA", linkModelVersionRequest.comment().orElse(null));
checkArray(firstModelVersion.aliases(), linkModelVersionRequest.aliases().orElse(null));
checkProperties(firstModelVersion.properties(), linkModelVersionRequest.properties());
@@ -258,8 +259,9 @@ public class TestModelEvent {
checkModelInfo(modelInfo, modelA);
// validate post-event model uri info
- String versionUri = registerAndLinkModelEvent.uri();
- Assertions.assertEquals(firstModelVersion.uri(), versionUri);
+ Map<String, String> versionUris = registerAndLinkModelEvent.uris();
+ Assertions.assertEquals(
+ firstModelVersion.uri(), versionUris.get(ModelVersion.URI_NAME_UNKNOWN));
}
@Test
@@ -292,7 +294,8 @@ public class TestModelEvent {
registerAndLinkModelFailureEvent.linkModelVersionRequest();
checkModelInfo(registerModelRequest, modelA);
- Assertions.assertEquals(firstModelVersion.uri(), linkModelVersionRequest.uri());
+ Assertions.assertEquals(
+ firstModelVersion.uri(), linkModelVersionRequest.uris().get(ModelVersion.URI_NAME_UNKNOWN));
Assertions.assertEquals("commentA", linkModelVersionRequest.comment().orElse(null));
checkArray(firstModelVersion.aliases(), linkModelVersionRequest.aliases().orElse(null));
checkProperties(firstModelVersion.properties(), linkModelVersionRequest.properties());
@@ -625,6 +628,182 @@ public class TestModelEvent {
Assertions.assertTrue(deleteModelVersionEvent.isExists());
}
+ @Test
+ void testGetModelVersionUriFailureEventViaVersion() {
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class,
+ () -> failureDispatcher.getModelVersionUri(existingIdentA, 1, "n1"));
+
+ Event event = dummyEventListener.popPostEvent();
+
+ Assertions.assertEquals(GetModelVersionUriFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class,
+ ((GetModelVersionUriFailureEvent) event).exception().getClass());
+ Assertions.assertEquals(OperationType.GET_MODEL_VERSION_URI, event.operationType());
+ Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus());
+
+ GetModelVersionUriFailureEvent getModelVersionUriFailureEvent =
+ (GetModelVersionUriFailureEvent) event;
+ Assertions.assertEquals(existingIdentA, getModelVersionUriFailureEvent.identifier());
+ Assertions.assertTrue(getModelVersionUriFailureEvent.version().isPresent());
+ Assertions.assertEquals(1, getModelVersionUriFailureEvent.version().get());
+ Assertions.assertFalse(getModelVersionUriFailureEvent.alias().isPresent());
+ Assertions.assertEquals("n1", getModelVersionUriFailureEvent.uriName());
+ }
+
+ @Test
+ void testGetModelVersionUriFailureEventViaAlias() {
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class,
+ () -> failureDispatcher.getModelVersionUri(existingIdentA, "alias", "n1"));
+
+ Event event = dummyEventListener.popPostEvent();
+
+ Assertions.assertEquals(GetModelVersionUriFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class,
+ ((GetModelVersionUriFailureEvent) event).exception().getClass());
+ Assertions.assertEquals(OperationType.GET_MODEL_VERSION_URI, event.operationType());
+ Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus());
+
+ GetModelVersionUriFailureEvent getModelVersionUriFailureEvent =
+ (GetModelVersionUriFailureEvent) event;
+ Assertions.assertEquals(existingIdentA, getModelVersionUriFailureEvent.identifier());
+ Assertions.assertTrue(getModelVersionUriFailureEvent.alias().isPresent());
+ Assertions.assertEquals("alias", getModelVersionUriFailureEvent.alias().get());
+ Assertions.assertFalse(getModelVersionUriFailureEvent.version().isPresent());
+ Assertions.assertEquals("n1", getModelVersionUriFailureEvent.uriName());
+ }
+
+ @Test
+ void testGetModelVersionUriEventViaVersion() {
+ dispatcher.getModelVersionUri(existingIdentA, 1, "n1");
+
+ // validate pre-event
+ PreEvent preEvent = dummyEventListener.popPreEvent();
+ Assertions.assertEquals(GetModelVersionUriPreEvent.class, preEvent.getClass());
+ Assertions.assertEquals(OperationType.GET_MODEL_VERSION_URI, preEvent.operationType());
+ Assertions.assertEquals(OperationStatus.UNPROCESSED, preEvent.operationStatus());
+
+ GetModelVersionUriPreEvent getModelVersionUriPreEvent = (GetModelVersionUriPreEvent) preEvent;
+ Assertions.assertEquals(existingIdentA, getModelVersionUriPreEvent.identifier());
+ Assertions.assertTrue(getModelVersionUriPreEvent.version().isPresent());
+ Assertions.assertEquals(1, getModelVersionUriPreEvent.version().get());
+ Assertions.assertFalse(getModelVersionUriPreEvent.alias().isPresent());
+ Assertions.assertEquals("n1", getModelVersionUriPreEvent.uriName());
+
+ // validate post-event
+ Event postEvent = dummyEventListener.popPostEvent();
+ Assertions.assertEquals(GetModelVersionUriEvent.class, postEvent.getClass());
+ Assertions.assertEquals(OperationType.GET_MODEL_VERSION_URI, postEvent.operationType());
+ Assertions.assertEquals(OperationStatus.SUCCESS, postEvent.operationStatus());
+
+ GetModelVersionUriEvent getModelVersionUriEvent = (GetModelVersionUriEvent) postEvent;
+ Assertions.assertEquals(existingIdentA, getModelVersionUriEvent.identifier());
+ Assertions.assertTrue(getModelVersionUriEvent.version().isPresent());
+ Assertions.assertEquals(1, getModelVersionUriEvent.version().get());
+ Assertions.assertFalse(getModelVersionUriEvent.alias().isPresent());
+ Assertions.assertEquals("n1", getModelVersionUriEvent.uriName());
+ Assertions.assertEquals("u1", getModelVersionUriEvent.uri());
+ }
+
+ @Test
+ void testGetModelVersionUriEventWithoutUriNameViaVersion() {
+ dispatcher.getModelVersionUri(existingIdentA, 1, null);
+
+ // validate pre-event
+ PreEvent preEvent = dummyEventListener.popPreEvent();
+ Assertions.assertEquals(GetModelVersionUriPreEvent.class, preEvent.getClass());
+ Assertions.assertEquals(OperationType.GET_MODEL_VERSION_URI, preEvent.operationType());
+ Assertions.assertEquals(OperationStatus.UNPROCESSED, preEvent.operationStatus());
+
+ GetModelVersionUriPreEvent getModelVersionUriPreEvent = (GetModelVersionUriPreEvent) preEvent;
+ Assertions.assertEquals(existingIdentA, getModelVersionUriPreEvent.identifier());
+ Assertions.assertTrue(getModelVersionUriPreEvent.version().isPresent());
+ Assertions.assertEquals(1, getModelVersionUriPreEvent.version().get());
+ Assertions.assertFalse(getModelVersionUriPreEvent.alias().isPresent());
+ Assertions.assertNull(getModelVersionUriPreEvent.uriName());
+
+ // validate post-event
+ Event postEvent = dummyEventListener.popPostEvent();
+ Assertions.assertEquals(GetModelVersionUriEvent.class, postEvent.getClass());
+ Assertions.assertEquals(OperationType.GET_MODEL_VERSION_URI, postEvent.operationType());
+ Assertions.assertEquals(OperationStatus.SUCCESS, postEvent.operationStatus());
+
+ GetModelVersionUriEvent getModelVersionUriEvent = (GetModelVersionUriEvent) postEvent;
+ Assertions.assertEquals(existingIdentA, getModelVersionUriEvent.identifier());
+ Assertions.assertTrue(getModelVersionUriEvent.version().isPresent());
+ Assertions.assertEquals(1, getModelVersionUriEvent.version().get());
+ Assertions.assertFalse(getModelVersionUriEvent.alias().isPresent());
+ Assertions.assertNull(getModelVersionUriEvent.uriName());
+ Assertions.assertEquals("u1", getModelVersionUriEvent.uri());
+ }
+
+ @Test
+ void testGetModelVersionUriEventViaAlias() {
+ dispatcher.getModelVersionUri(existingIdentA, "alias", "n1");
+
+ // validate pre-event
+ PreEvent preEvent = dummyEventListener.popPreEvent();
+ Assertions.assertEquals(GetModelVersionUriPreEvent.class, preEvent.getClass());
+ Assertions.assertEquals(OperationType.GET_MODEL_VERSION_URI, preEvent.operationType());
+ Assertions.assertEquals(OperationStatus.UNPROCESSED, preEvent.operationStatus());
+
+ GetModelVersionUriPreEvent getModelVersionUriPreEvent = (GetModelVersionUriPreEvent) preEvent;
+ Assertions.assertEquals(existingIdentA, getModelVersionUriPreEvent.identifier());
+ Assertions.assertTrue(getModelVersionUriPreEvent.alias().isPresent());
+ Assertions.assertEquals("alias", getModelVersionUriPreEvent.alias().get());
+ Assertions.assertFalse(getModelVersionUriPreEvent.version().isPresent());
+ Assertions.assertEquals("n1", getModelVersionUriPreEvent.uriName());
+
+ // validate post-event
+ Event postEvent = dummyEventListener.popPostEvent();
+ Assertions.assertEquals(GetModelVersionUriEvent.class, postEvent.getClass());
+ Assertions.assertEquals(OperationType.GET_MODEL_VERSION_URI, postEvent.operationType());
+ Assertions.assertEquals(OperationStatus.SUCCESS, postEvent.operationStatus());
+
+ GetModelVersionUriEvent getModelVersionUriEvent = (GetModelVersionUriEvent) postEvent;
+ Assertions.assertEquals(existingIdentA, getModelVersionUriEvent.identifier());
+ Assertions.assertTrue(getModelVersionUriEvent.alias().isPresent());
+ Assertions.assertEquals("alias", getModelVersionUriEvent.alias().get());
+ Assertions.assertFalse(getModelVersionUriEvent.version().isPresent());
+ Assertions.assertEquals("n1", getModelVersionUriEvent.uriName());
+ Assertions.assertEquals("u1", getModelVersionUriEvent.uri());
+ }
+
+ @Test
+ void testGetModelVersionUriEventWithoutUriNameViaAlias() {
+ dispatcher.getModelVersionUri(existingIdentA, "alias", null);
+
+ // validate pre-event
+ PreEvent preEvent = dummyEventListener.popPreEvent();
+ Assertions.assertEquals(GetModelVersionUriPreEvent.class, preEvent.getClass());
+ Assertions.assertEquals(OperationType.GET_MODEL_VERSION_URI, preEvent.operationType());
+ Assertions.assertEquals(OperationStatus.UNPROCESSED, preEvent.operationStatus());
+
+ GetModelVersionUriPreEvent getModelVersionUriPreEvent = (GetModelVersionUriPreEvent) preEvent;
+ Assertions.assertEquals(existingIdentA, getModelVersionUriPreEvent.identifier());
+ Assertions.assertTrue(getModelVersionUriPreEvent.alias().isPresent());
+ Assertions.assertEquals("alias", getModelVersionUriPreEvent.alias().get());
+ Assertions.assertFalse(getModelVersionUriPreEvent.version().isPresent());
+ Assertions.assertNull(getModelVersionUriPreEvent.uriName());
+
+ // validate post-event
+ Event postEvent = dummyEventListener.popPostEvent();
+ Assertions.assertEquals(GetModelVersionUriEvent.class, postEvent.getClass());
+ Assertions.assertEquals(OperationType.GET_MODEL_VERSION_URI, postEvent.operationType());
+ Assertions.assertEquals(OperationStatus.SUCCESS, postEvent.operationStatus());
+
+ GetModelVersionUriEvent getModelVersionUriEvent = (GetModelVersionUriEvent) postEvent;
+ Assertions.assertEquals(existingIdentA, getModelVersionUriEvent.identifier());
+ Assertions.assertTrue(getModelVersionUriEvent.alias().isPresent());
+ Assertions.assertEquals("alias", getModelVersionUriEvent.alias().get());
+ Assertions.assertFalse(getModelVersionUriEvent.version().isPresent());
+ Assertions.assertNull(getModelVersionUriEvent.uriName());
+ Assertions.assertEquals("u1", getModelVersionUriEvent.uri());
+ }
+
@Test
void testDeleteModelVersionFailureEventViaVersion() {
Assertions.assertThrowsExactly(
@@ -887,7 +1066,8 @@ public class TestModelEvent {
ModelVersionInfo modelVersionInfo = alterModelVersionEvent.alteredModelVersionInfo();
// validate ModelVersionInfo
- Assertions.assertEquals("uriA", modelVersionInfo.uri());
+ Assertions.assertEquals(
+ ImmutableMap.of(ModelVersion.URI_NAME_UNKNOWN, "uriA"), modelVersionInfo.uris());
Assertions.assertTrue(modelVersionInfo.aliases().isPresent());
Assertions.assertArrayEquals(
new String[] {"aliasProduction"}, modelVersionInfo.aliases().get());
@@ -913,7 +1093,8 @@ public class TestModelEvent {
ModelVersionInfo modelVersionInfo = alterModelVersionEvent.alteredModelVersionInfo();
// validate ModelVersionInfo
- Assertions.assertEquals("uriB", modelVersionInfo.uri());
+ Assertions.assertEquals(
+ ImmutableMap.of(ModelVersion.URI_NAME_UNKNOWN, "uriB"), modelVersionInfo.uris());
Assertions.assertTrue(modelVersionInfo.aliases().isPresent());
Assertions.assertArrayEquals(new String[] {"aliasTest"}, modelVersionInfo.aliases().get());
Assertions.assertTrue(modelVersionInfo.comment().isPresent());
@@ -1016,6 +1197,10 @@ public class TestModelEvent {
when(dispatcher.alterModelVersion(existingIdentB, "aliasTest", modelUpdateCommentChange))
.thenReturn(otherAlterCommentModelVersion);
+ when(dispatcher.getModelVersionUri(existingIdentA, 1, "n1")).thenReturn("u1");
+ when(dispatcher.getModelVersionUri(existingIdentA, 1, null)).thenReturn("u1");
+ when(dispatcher.getModelVersionUri(existingIdentA, "alias", "n1")).thenReturn("u1");
+ when(dispatcher.getModelVersionUri(existingIdentA, "alias", null)).thenReturn("u1");
return dispatcher;
}
@@ -1097,7 +1282,8 @@ public class TestModelEvent {
private void checkModelVersionInfo(ModelVersionInfo modelVersionInfo, ModelVersion modelVersion) {
// check normal fields
- Assertions.assertEquals(modelVersion.uri(), modelVersionInfo.uri());
+ Assertions.assertEquals(
+ modelVersion.uri(), modelVersionInfo.uris().get(ModelVersion.URI_NAME_UNKNOWN));
Assertions.assertEquals(modelVersion.comment(), modelVersionInfo.comment().orElse(null));
// check aliases