mchades commented on code in PR #14143: URL: https://github.com/apache/gravitino/pull/14143#discussion_r4398376868
########## server/src/main/java/org/apache/gravitino/server/web/rest/SemanticModelMetricOperations.java: ########## @@ -0,0 +1,156 @@ +/* + * 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 java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import javax.inject.Inject; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import org.apache.gravitino.Entity; +import org.apache.gravitino.MetadataObject; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.catalog.SemanticModelDispatcher; +import org.apache.gravitino.dto.responses.EntityListResponse; +import org.apache.gravitino.dto.responses.MetricInfoListResponse; +import org.apache.gravitino.dto.semantic.SemanticModelMetricDTO; +import org.apache.gravitino.metrics.MetricNames; +import org.apache.gravitino.semantic.SemanticModelMetric; +import org.apache.gravitino.server.authorization.MetadataAuthzHelper; +import org.apache.gravitino.server.authorization.annotations.AuthorizationExpression; +import org.apache.gravitino.server.authorization.annotations.AuthorizationMetadata; +import org.apache.gravitino.server.authorization.expression.AuthorizationExpressionConstants; +import org.apache.gravitino.server.web.Utils; +import org.apache.gravitino.utils.NamespaceUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** REST discovery of metrics contained in the Semantic Models of a schema. */ +@Path("metalakes/{metalake}/catalogs/{catalog}/schemas/{schema}/semantic-model-metrics") +public class SemanticModelMetricOperations { + private static final Logger LOG = LoggerFactory.getLogger(SemanticModelMetricOperations.class); + + private final SemanticModelDispatcher dispatcher; + @Context private HttpServletRequest httpRequest; + + /** + * Creates schema-level metric discovery operations. + * + * @param dispatcher The Semantic Model dispatcher. + */ + @Inject + public SemanticModelMetricOperations(SemanticModelDispatcher dispatcher) { + this.dispatcher = dispatcher; + } + + /** + * Lists metric identifiers or complete definitions visible to the caller. + * + * @param metalake The metalake name. + * @param catalog The catalog name. + * @param schema The schema name. + * @param details Whether to return complete metric definitions. + * @return An identifier list or metric info list response. + */ + @GET + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "list-semantic-model-metrics." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "list-semantic-model-metrics", absolute = true) + @AuthorizationExpression( + expression = AuthorizationExpressionConstants.LOAD_SCHEMA_AUTHORIZATION_EXPRESSION, + accessMetadataType = MetadataObject.Type.SCHEMA) + public Response listMetrics( + @PathParam("metalake") @AuthorizationMetadata(type = Entity.EntityType.METALAKE) + String metalake, + @PathParam("catalog") @AuthorizationMetadata(type = Entity.EntityType.CATALOG) String catalog, + @PathParam("schema") @AuthorizationMetadata(type = Entity.EntityType.SCHEMA) String schema, + @QueryParam("details") @DefaultValue("false") boolean details) { + LOG.info( + "Received list Semantic Model metrics request for {}.{}.{}, details: {}", + metalake, + catalog, + schema, + details); + try { + return Utils.doAs( + httpRequest, + () -> { + Namespace namespace = NamespaceUtil.ofSemanticModel(metalake, catalog, schema); + if (!details) { + NameIdentifier[] identifiers = dispatcher.listMetrics(namespace); + identifiers = identifiers == null ? new NameIdentifier[0] : identifiers; + Set<NameIdentifier> visibleModels = + visibleModels( + metalake, + Arrays.stream(identifiers) + .map(ident -> NameIdentifier.of(ident.namespace().levels())) + .toArray(NameIdentifier[]::new)); + NameIdentifier[] visible = + Arrays.stream(identifiers) + .filter( + ident -> + visibleModels.contains(NameIdentifier.of(ident.namespace().levels()))) + .toArray(NameIdentifier[]::new); + return Utils.ok(new EntityListResponse(visible)); + } + + SemanticModelMetric[] metrics = dispatcher.listMetricInfos(namespace); + metrics = metrics == null ? new SemanticModelMetric[0] : metrics; + Set<NameIdentifier> visibleModels = + visibleModels( + metalake, + Arrays.stream(metrics) + .map(info -> NameIdentifier.of(info.namespace().levels())) + .toArray(NameIdentifier[]::new)); + SemanticModelMetricDTO[] visible = + Arrays.stream(metrics) + .filter( + info -> + visibleModels.contains(NameIdentifier.of(info.namespace().levels()))) + .map(SemanticModelMetricDTO::fromSemanticModelMetric) + .toArray(SemanticModelMetricDTO[]::new); + return Utils.ok(new MetricInfoListResponse(visible)); + }); + } catch (Exception e) { + return ExceptionHandlers.handleSemanticModelException(OperationType.LIST, "", schema, e); Review Comment: Follow-up: #13107 discusses the shared error-message policy and explicitly lists REST `ExceptionHandlers` for scope triage. Keeping this PR consistent with the existing listing APIs. ########## core/src/main/java/org/apache/gravitino/storage/relational/service/SemanticModelMetricService.java: ########## @@ -0,0 +1,136 @@ +/* + * 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.storage.relational.service; + +import static org.apache.gravitino.metrics.source.MetricsSource.GRAVITINO_RELATIONAL_STORE_METRIC_NAME; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.google.common.base.Preconditions; +import java.util.ArrayList; +import java.util.List; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.dto.semantic.MetricDTO; +import org.apache.gravitino.exceptions.NoSuchEntityException; +import org.apache.gravitino.json.JsonUtils; +import org.apache.gravitino.metrics.Monitored; +import org.apache.gravitino.semantic.ImmutableSemanticModelMetric; +import org.apache.gravitino.semantic.SemanticModelMetric; +import org.apache.gravitino.storage.relational.mapper.SemanticModelMetricMapper; +import org.apache.gravitino.storage.relational.po.SemanticModelMetricListingPO; +import org.apache.gravitino.storage.relational.utils.SessionUtils; +import org.apache.gravitino.utils.HierarchicalSchemaUtil; +import org.apache.gravitino.utils.NamespaceUtil; + +/** Reads schema-level metrics from committed relational state without loading complete models. */ +public class SemanticModelMetricService { + private static final SemanticModelMetricService INSTANCE = new SemanticModelMetricService(); + + private SemanticModelMetricService() {} + + /** Returns the metric discovery service. */ + public static SemanticModelMetricService getInstance() { + return INSTANCE; + } + + /** + * Lists live metric identifiers without reading definition snapshots. + * + * @param namespace The full schema namespace. + * @return Metric identifiers with the containing model as the last namespace level. + * @throws NoSuchEntityException If the schema or an ancestor does not exist. + */ + @Monitored( + metricsSource = GRAVITINO_RELATIONAL_STORE_METRIC_NAME, + baseMetricName = "listSemanticModelMetrics") + public List<NameIdentifier> listMetrics(Namespace namespace) { + return read(namespace, false).stream() + .filter(po -> po.getMetricName() != null) + .map( + po -> + NameIdentifier.of( + metricNamespace(namespace, po.getSemanticModelName()), po.getMetricName())) + .toList(); + } + + /** + * Lists complete metrics from current snapshots, decoding every array before returning. + * + * @param namespace The full schema namespace. + * @return Decoded metrics and their containing namespaces. + * @throws NoSuchEntityException If the schema or an ancestor does not exist. + * @throws IllegalStateException If a current snapshot is missing or metrics cannot be decoded. + */ + @Monitored( + metricsSource = GRAVITINO_RELATIONAL_STORE_METRIC_NAME, + baseMetricName = "listSemanticModelMetricInfos") + public List<SemanticModelMetric> listMetricInfos(Namespace namespace) { + List<SemanticModelMetric> results = new ArrayList<>(); + for (SemanticModelMetricListingPO po : read(namespace, true)) { + if (po.getSemanticModelId() == null) { + continue; + } + Preconditions.checkState( + po.getVersion() != null, + "Missing current snapshot for Semantic Model %s", + po.getSemanticModelId()); + if (po.getMetrics() == null) { + continue; + } + Namespace metricNamespace = metricNamespace(namespace, po.getSemanticModelName()); + try { + MetricDTO[] metrics = + JsonUtils.anyFieldMapper().readValue(po.getMetrics(), MetricDTO[].class); + Preconditions.checkArgument(metrics != null, "metrics JSON must be an array"); + for (MetricDTO metric : metrics) { + Preconditions.checkArgument(metric != null, "metrics must not contain null"); + results.add(new ImmutableSemanticModelMetric(metricNamespace, metric.toMetric())); + } + } catch (JsonProcessingException | IllegalArgumentException e) { + throw new IllegalStateException( + "Failed to decode metrics for Semantic Model " Review Comment: Keeping the shared error handling consistent with Table and View listing. #13107 discusses the error-message policy and explicitly lists REST `ExceptionHandlers` for scope triage; the follow-up is linked in the original thread. ########## core/src/main/java/org/apache/gravitino/EntityStore.java: ########## @@ -30,10 +30,37 @@ import org.apache.gravitino.Entity.EntityType; import org.apache.gravitino.exceptions.NoSuchEntityException; import org.apache.gravitino.meta.SchemaEntity; +import org.apache.gravitino.semantic.SemanticModelMetric; import org.apache.gravitino.utils.Executable; public interface EntityStore extends Closeable { + /** + * Reads metric identifiers in a schema without loading complete Semantic Models. + * + * @param namespace The full schema namespace. + * @return Metric identifiers from one consistent statement or read snapshot. + * @throws NoSuchEntityException If the schema does not exist. + * @throws IOException If the read fails. + */ + default List<NameIdentifier> listSemanticModelMetrics(Namespace namespace) throws IOException { Review Comment: Documented `UnsupportedOperationException` on all four methods in 48c510bf4c. The REST handler already maps it to HTTP 501, so no error-handling change was needed. -- 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]
