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

[#5872] feat(client): Add model management Java client API #6003

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ static Catalog toCatalog(String metalake, CatalogDTO catalog, RESTClient client)
.withAudit((AuditDTO) catalog.auditInfo())
.withRestClient(client)
.build();
case MODEL:
return GenericModelCatalog.builder()
.withNamespace(namespace)
.withName(catalog.name())
.withType(catalog.type())
.withProvider(catalog.provider())
.withComment(catalog.comment())
.withProperties(catalog.properties())
.withAudit((AuditDTO) catalog.auditInfo())
.withRestClient(client)
.build();

default:
throw new UnsupportedOperationException("Unsupported catalog type: " + catalog.type());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@
import org.apache.gravitino.exceptions.MetalakeAlreadyExistsException;
import org.apache.gravitino.exceptions.MetalakeInUseException;
import org.apache.gravitino.exceptions.MetalakeNotInUseException;
import org.apache.gravitino.exceptions.ModelAlreadyExistsException;
import org.apache.gravitino.exceptions.ModelVersionAliasesAlreadyExistException;
import org.apache.gravitino.exceptions.NoSuchCatalogException;
import org.apache.gravitino.exceptions.NoSuchCredentialException;
import org.apache.gravitino.exceptions.NoSuchFilesetException;
import org.apache.gravitino.exceptions.NoSuchGroupException;
import org.apache.gravitino.exceptions.NoSuchMetadataObjectException;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
import org.apache.gravitino.exceptions.NoSuchModelException;
import org.apache.gravitino.exceptions.NoSuchModelVersionException;
import org.apache.gravitino.exceptions.NoSuchPartitionException;
import org.apache.gravitino.exceptions.NoSuchRoleException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
Expand Down Expand Up @@ -220,6 +224,15 @@ public static Consumer<ErrorResponse> ownerErrorHandler() {
return OwnerErrorHandler.INSTANCE;
}

/**
* Creates an error handler specific to Model operations.
*
* @return A Consumer representing the Model error handler.
*/
public static Consumer<ErrorResponse> modelErrorHandler() {
return ModelErrorHandler.INSTANCE;
}

private ErrorHandlers() {}

/**
Expand Down Expand Up @@ -987,6 +1000,69 @@ public void accept(ErrorResponse errorResponse) {
}
}

/** Error handler specific to Model operations. */
@SuppressWarnings("FormatStringAnnotation")
private static class ModelErrorHandler extends RestErrorHandler {

private static final ModelErrorHandler INSTANCE = new ModelErrorHandler();

@Override
public void accept(ErrorResponse errorResponse) {
String errorMsg = formatErrorMessage(errorResponse);

switch (errorResponse.getCode()) {
case ErrorConstants.ILLEGAL_ARGUMENTS_CODE:
throw new IllegalArgumentException(errorMsg);

case ErrorConstants.NOT_FOUND_CODE:
if (errorResponse.getType().equals(NoSuchSchemaException.class.getSimpleName())) {
throw new NoSuchSchemaException(errorMsg);
} else if (errorResponse.getType().equals(NoSuchModelException.class.getSimpleName())) {
throw new NoSuchModelException(errorMsg);
} else if (errorResponse
.getType()
.equals(NoSuchModelVersionException.class.getSimpleName())) {
throw new NoSuchModelVersionException(errorMsg);
} else {
throw new NotFoundException(errorMsg);
}

case ErrorConstants.ALREADY_EXISTS_CODE:
if (errorResponse.getType().equals(ModelAlreadyExistsException.class.getSimpleName())) {
throw new ModelAlreadyExistsException(errorMsg);
} else if (errorResponse
.getType()
.equals(ModelVersionAliasesAlreadyExistException.class.getSimpleName())) {
throw new ModelVersionAliasesAlreadyExistException(errorMsg);
} else {
throw new AlreadyExistsException(errorMsg);
}

case ErrorConstants.FORBIDDEN_CODE:
throw new ForbiddenException(errorMsg);

case ErrorConstants.INTERNAL_ERROR_CODE:
throw new RuntimeException(errorMsg);

case ErrorConstants.NOT_IN_USE_CODE:
if (errorResponse.getType().equals(CatalogNotInUseException.class.getSimpleName())) {
throw new CatalogNotInUseException(errorMsg);

} else if (errorResponse
.getType()
.equals(MetalakeNotInUseException.class.getSimpleName())) {
throw new MetalakeNotInUseException(errorMsg);

} else {
throw new NotInUseException(errorMsg);
}

default:
super.accept(errorResponse);
}
}
}

/** Generic error handler for REST requests. */
private static class RestErrorHandler extends ErrorHandler {
private static final ErrorHandler INSTANCE = new RestErrorHandler();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.client;

import java.util.Map;
import org.apache.gravitino.Audit;
import org.apache.gravitino.authorization.SupportsRoles;
import org.apache.gravitino.dto.model.ModelDTO;
import org.apache.gravitino.model.Model;
import org.apache.gravitino.tag.SupportsTags;

/** Represents a generic model. */
class GenericModel implements Model {

private final ModelDTO modelDTO;

GenericModel(ModelDTO modelDTO) {
this.modelDTO = modelDTO;
}

@Override
public Audit auditInfo() {
return modelDTO.auditInfo();
}

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

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

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

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

@Override
public SupportsTags supportsTags() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public SupportsRoles supportsRoles() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof GenericModel)) {
return false;
}

GenericModel that = (GenericModel) o;
return modelDTO.equals(that.modelDTO);
}

@Override
public int hashCode() {
return modelDTO.hashCode();
}
}
Loading
Loading