Skip to content

Commit

Permalink
[#5817] core(feat): Add server-side REST APIs for model management (#…
Browse files Browse the repository at this point in the history
…5948)

### What changes were proposed in this pull request?

This PR adds the server-side REST endpoint for model management.

### Why are the changes needed?

This is a part of model management for Gravitino.

Fix: #5817 

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Add UTs for this PR.
  • Loading branch information
jerryshao authored Dec 26, 2024
1 parent 082bbdc commit a68e5e2
Show file tree
Hide file tree
Showing 25 changed files with 3,006 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ Model registerModel(NameIdentifier ident, String comment, Map<String, String> pr
*
* @param ident The name identifier of the model.
* @param uri The model artifact URI.
* @param aliases The aliases of the model version. The alias are optional and can be empty.
* @param aliases The aliases of the model version. The aliases should be unique in this model,
* otherwise the {@link ModelVersionAliasesAlreadyExistException} will be thrown. The aliases
* are optional and can be empty. Also, be aware that the alias cannot be a number or a number
* string.
* @param comment The comment of the model. The comment is optional and can be null.
* @param properties The properties of the model. The properties are optional and can be null or
* empty.
Expand Down Expand Up @@ -198,7 +201,8 @@ default boolean modelVersionExists(NameIdentifier ident, String alias) {
* @param uri The URI of the model version artifact.
* @param aliases The aliases of the model version. The aliases should be unique in this model,
* otherwise the {@link ModelVersionAliasesAlreadyExistException} will be thrown. The aliases
* are optional and can be empty.
* are optional and can be empty. Also, be aware that the alias cannot be a number or a number
* string.
* @param comment The comment of the model version. The comment is optional and can be null.
* @param properties The properties of the model version. The properties are optional and can be
* null or empty.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.apache.gravitino.catalog.model;

import java.util.Map;
import org.apache.gravitino.CatalogProvider;
import org.apache.gravitino.EntityStore;
import org.apache.gravitino.GravitinoEnv;
import org.apache.gravitino.connector.BaseCatalog;
Expand All @@ -40,7 +39,7 @@ public class ModelCatalogImpl extends BaseCatalog<ModelCatalogImpl> {

@Override
public String shortName() {
return CatalogProvider.shortNameForManagedCatalog(super.type());
return "model";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
# specific language governing permissions and limitations
# under the License.
#
org.apache.gravitino.catalog.model.ModelCatalog
org.apache.gravitino.catalog.model.ModelCatalogImpl
163 changes: 163 additions & 0 deletions common/src/main/java/org/apache/gravitino/dto/model/ModelDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* 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.dto.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import java.util.Map;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.gravitino.dto.AuditDTO;
import org.apache.gravitino.model.Model;

/** Represents a model DTO (Data Transfer Object). */
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@EqualsAndHashCode
public class ModelDTO implements Model {

@JsonProperty("name")
private String name;

@JsonProperty("comment")
private String comment;

@JsonProperty("properties")
private Map<String, String> properties;

@JsonProperty("latestVersion")
private int latestVersion;

@JsonProperty("audit")
private AuditDTO audit;

@Override
public String name() {
return name;
}

@Override
public String comment() {
return comment;
}

@Override
public Map<String, String> properties() {
return properties;
}

@Override
public int latestVersion() {
return latestVersion;
}

@Override
public AuditDTO auditInfo() {
return audit;
}

/**
* Creates a new builder for constructing a Model DTO.
*
* @return The builder.
*/
public static Builder builder() {
return new Builder();
}

/** Builder for constructing a Model DTO. */
public static class Builder {
private String name;
private String comment;
private Map<String, String> properties;
private int latestVersion;
private AuditDTO audit;

/**
* Sets the name of the model.
*
* @param name The name of the model.
* @return The builder.
*/
public Builder withName(String name) {
this.name = name;
return this;
}

/**
* Sets the comment associated with the model.
*
* @param comment The comment associated with the model.
* @return The builder.
*/
public Builder withComment(String comment) {
this.comment = comment;
return this;
}

/**
* Sets the properties associated with the model.
*
* @param properties The properties associated with the model.
* @return The builder.
*/
public Builder withProperties(Map<String, String> properties) {
this.properties = properties;
return this;
}

/**
* Sets the latest version of the model.
*
* @param latestVersion The latest version of the model.
* @return The builder.
*/
public Builder withLatestVersion(int latestVersion) {
this.latestVersion = latestVersion;
return this;
}

/**
* Sets the audit information associated with the model.
*
* @param audit The audit information associated with the model.
* @return The builder.
*/
public Builder withAudit(AuditDTO audit) {
this.audit = audit;
return this;
}

/**
* Builds the model DTO.
*
* @return The model DTO.
*/
public ModelDTO build() {
Preconditions.checkArgument(StringUtils.isNotBlank(name), "name cannot be null or empty");
Preconditions.checkArgument(latestVersion >= 0, "latestVersion cannot be negative");
Preconditions.checkArgument(audit != null, "audit cannot be null");

return new ModelDTO(name, comment, properties, latestVersion, audit);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* 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.dto.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import java.util.Map;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.gravitino.Audit;
import org.apache.gravitino.dto.AuditDTO;
import org.apache.gravitino.model.ModelVersion;

/** Represents a model version DTO (Data Transfer Object). */
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@EqualsAndHashCode
public class ModelVersionDTO implements ModelVersion {

@JsonProperty("version")
private int version;

@JsonProperty("comment")
private String comment;

@JsonProperty("aliases")
private String[] aliases;

@JsonProperty("uri")
private String uri;

@JsonProperty("properties")
private Map<String, String> properties;

@JsonProperty("audit")
private AuditDTO audit;

@Override
public Audit auditInfo() {
return audit;
}

@Override
public int version() {
return version;
}

@Override
public String comment() {
return comment;
}

@Override
public String[] aliases() {
return aliases;
}

@Override
public String uri() {
return uri;
}

@Override
public Map<String, String> properties() {
return properties;
}

/**
* Creates a new builder for constructing a Model Version DTO.
*
* @return The builder.
*/
public static Builder builder() {
return new Builder();
}

/** Builder for constructing a Model Version DTO. */
public static class Builder {
private int version;
private String comment;
private String[] aliases;
private String uri;
private Map<String, String> properties;
private AuditDTO audit;

/**
* Sets the version number of the model version.
*
* @param version The version number.
* @return The builder.
*/
public Builder withVersion(int version) {
this.version = version;
return this;
}

/**
* Sets the comment of the model version.
*
* @param comment The comment.
* @return The builder.
*/
public Builder withComment(String comment) {
this.comment = comment;
return this;
}

/**
* Sets the aliases of the model version.
*
* @param aliases The aliases.
* @return The builder.
*/
public Builder withAliases(String[] aliases) {
this.aliases = aliases;
return this;
}

/**
* Sets the URI of the model version.
*
* @param uri The URI.
* @return The builder.
*/
public Builder withUri(String uri) {
this.uri = uri;
return this;
}

/**
* Sets the properties of the model version.
*
* @param properties The properties.
* @return The builder.
*/
public Builder withProperties(Map<String, String> properties) {
this.properties = properties;
return this;
}

/**
* Sets the audit information of the model version.
*
* @param audit The audit information.
* @return The builder.
*/
public Builder withAudit(AuditDTO audit) {
this.audit = audit;
return this;
}

/**
* Builds the Model Version DTO.
*
* @return The Model Version DTO.
*/
public ModelVersionDTO build() {
Preconditions.checkArgument(version >= 0, "Version must be non-negative");
Preconditions.checkArgument(StringUtils.isNotBlank(uri), "URI cannot be null or empty");
Preconditions.checkArgument(audit != null, "Audit cannot be null");

return new ModelVersionDTO(version, comment, aliases, uri, properties, audit);
}
}
}
Loading

0 comments on commit a68e5e2

Please sign in to comment.