jerryshao commented on code in PR #5948: URL: https://github.com/apache/gravitino/pull/5948#discussion_r1897158323
########## server/src/main/java/org/apache/gravitino/server/web/rest/ModelOperations.java: ########## @@ -0,0 +1,410 @@ +/* + * 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.server.web.rest; + +import com.codahale.metrics.annotation.ResponseMetered; +import com.codahale.metrics.annotation.Timed; +import javax.inject.Inject; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.catalog.ModelDispatcher; +import org.apache.gravitino.dto.requests.ModelRegisterRequest; +import org.apache.gravitino.dto.requests.ModelVersionLinkRequest; +import org.apache.gravitino.dto.responses.BaseResponse; +import org.apache.gravitino.dto.responses.DropResponse; +import org.apache.gravitino.dto.responses.EntityListResponse; +import org.apache.gravitino.dto.responses.ModelResponse; +import org.apache.gravitino.dto.responses.ModelVersionListResponse; +import org.apache.gravitino.dto.responses.ModelVersionResponse; +import org.apache.gravitino.dto.util.DTOConverters; +import org.apache.gravitino.metrics.MetricNames; +import org.apache.gravitino.model.Model; +import org.apache.gravitino.model.ModelVersion; +import org.apache.gravitino.server.web.Utils; +import org.apache.gravitino.utils.NameIdentifierUtil; +import org.apache.gravitino.utils.NamespaceUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Path("metalakes/{metalake}/catalogs/{catalog}/schemas/{schema}/models") +public class ModelOperations { + + private static final Logger LOG = LoggerFactory.getLogger(ModelOperations.class); + + private final ModelDispatcher modelDispatcher; + + @Context private HttpServletRequest httpRequest; + + @Inject + public ModelOperations(ModelDispatcher modelDispatcher) { + this.modelDispatcher = modelDispatcher; + } + + @GET + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "list-model." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "list-model", absolute = true) + public Response listModels( + @PathParam("metalake") String metalake, + @PathParam("catalog") String catalog, + @PathParam("schema") String schema) { + LOG.info("Received list models request for schema: {}.{}.{}", metalake, catalog, schema); + Namespace modelNs = NamespaceUtil.ofModel(metalake, catalog, schema); + + try { + return Utils.doAs( + httpRequest, + () -> { + NameIdentifier[] modelIds = modelDispatcher.listModels(modelNs); + modelIds = modelIds == null ? new NameIdentifier[0] : modelIds; + LOG.info("List {} models under schema {}", modelIds.length, modelNs); + return Utils.ok(new EntityListResponse(modelIds)); + }); + + } catch (Exception e) { + return ExceptionHandlers.handleModelException(OperationType.LIST, "", schema, e); + } + } + + @GET + @Path("{model}") + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "get-model." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "get-model", absolute = true) + public Response getModel( + @PathParam("metalake") String metalake, + @PathParam("catalog") String catalog, + @PathParam("schema") String schema, + @PathParam("model") String model) { + LOG.info("Received get model request: {}.{}.{}.{}", metalake, catalog, schema, model); + NameIdentifier modelId = NameIdentifierUtil.ofModel(metalake, catalog, schema, model); + + try { + return Utils.doAs( + httpRequest, + () -> { + Model m = modelDispatcher.getModel(modelId); + LOG.info("Model got: {}", modelId); + return Utils.ok(new ModelResponse(DTOConverters.toDTO(m))); + }); + + } catch (Exception e) { + return ExceptionHandlers.handleModelException(OperationType.GET, model, schema, e); + } + } + + @POST + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "register-model." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "register-model", absolute = true) + public Response registerModel( + @PathParam("metalake") String metalake, + @PathParam("catalog") String catalog, + @PathParam("schema") String schema, + ModelRegisterRequest request) { + LOG.info( + "Received register model request: {}.{}.{}.{}", + metalake, + catalog, + schema, + request.getName()); + + try { + request.validate(); + NameIdentifier modelId = + NameIdentifierUtil.ofModel(metalake, catalog, schema, request.getName()); + + return Utils.doAs( + httpRequest, + () -> { + Model m = + modelDispatcher.registerModel( + modelId, request.getComment(), request.getProperties()); + LOG.info("Model registered: {}", modelId); + return Utils.ok(new ModelResponse(DTOConverters.toDTO(m))); + }); + + } catch (Exception e) { + return ExceptionHandlers.handleModelException( + OperationType.REGISTER, request.getName(), schema, e); + } + } + + @DELETE + @Path("{model}") + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "delete-model." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "delete-model", absolute = true) + public Response deleteModel( + @PathParam("metalake") String metalake, + @PathParam("catalog") String catalog, + @PathParam("schema") String schema, + @PathParam("model") String model) { + LOG.info("Received delete model request: {}.{}.{}.{}", metalake, catalog, schema, model); + NameIdentifier modelId = NameIdentifierUtil.ofModel(metalake, catalog, schema, model); + + try { + return Utils.doAs( + httpRequest, + () -> { + boolean deleted = modelDispatcher.deleteModel(modelId); + if (!deleted) { + LOG.warn("Failed to delete model {} under schema {}", model, schema); Review Comment: I see, I will fix it. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
