yuqi1129 commented on code in PR #14143: URL: https://github.com/apache/gravitino/pull/14143#discussion_r4397870570
########## 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: Nit: the two failures here identify the model differently: line 91 uses the internal model id, while this one uses the full identifier, which a caller who can't see that model gets back in the 500 body (as Copilot noted). Keeping the `listFunctionInfos` behavior is fine for this PR, but could you file the follow-up issue for sanitizing internal error details and link it in that thread, so it isn't lost? While here, consider using the same identification in both messages. ########## common/src/main/java/org/apache/gravitino/dto/responses/MetricInfoListResponse.java: ########## @@ -0,0 +1,66 @@ +/* + * 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.responses; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.base.Preconditions; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.apache.gravitino.dto.semantic.SemanticModelMetricDTO; + +/** Response containing complete metrics discovered in a schema. */ +@EqualsAndHashCode(callSuper = true) +@ToString +public class MetricInfoListResponse extends BaseResponse { + + @JsonProperty("metrics") + private SemanticModelMetricDTO[] metrics; + + private MetricInfoListResponse() { + super(); + } + + /** + * Creates a response containing metric definitions and their namespaces. + * + * @param metrics The metric discovery results. + */ + public MetricInfoListResponse(SemanticModelMetricDTO[] metrics) { + super(0); + this.metrics = metrics == null ? null : metrics.clone(); + } + + /** + * Returns the metric discovery results. + * + * @return A copy of the result array. + */ + public SemanticModelMetricDTO[] getMetrics() { + return metrics == null ? null : metrics.clone(); + } + + @Override + public void validate() throws IllegalArgumentException { + super.validate(); + Preconditions.checkArgument(metrics != null, "metrics must not be null"); + for (SemanticModelMetricDTO metric : metrics) { + Preconditions.checkArgument(metric != null, "metrics must not contain null"); Review Comment: Nit: `validate()` only rejects null elements. A deserialized `SemanticModelMetricDTO` can still have a null `namespace` or `metric` (the private constructor allows it), and the Java client would then hit an NPE in `namespace()`/`metric()` instead of a validation error. Could you also check those two fields here, or add a `validate()` to the DTO, before the client support lands? ########## 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: Nit: the default implementations throw `UnsupportedOperationException`, but the Javadoc only lists `NoSuchEntityException` and `IOException`. `ManagedSemanticModelOperations` doesn't catch it, so a store without these methods surfaces an undocumented 500. Please add `@throws UnsupportedOperationException` here, on `listSemanticModelMetricInfos`, and on the two methods in `RelationalBackend`. -- 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]
