obelix74 commented on code in PR #3385: URL: https://github.com/apache/polaris/pull/3385#discussion_r2843737525
########## persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcMetricsPersistenceProducer.java: ########## @@ -0,0 +1,197 @@ +/* + * 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.polaris.persistence.relational.jdbc; + +import io.smallrye.common.annotation.Identifier; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.context.RequestScoped; +import jakarta.enterprise.inject.Instance; +import jakarta.enterprise.inject.Produces; +import jakarta.inject.Inject; +import java.sql.SQLException; +import java.util.List; +import javax.sql.DataSource; +import org.apache.polaris.core.auth.PolarisPrincipal; +import org.apache.polaris.core.context.RealmContext; +import org.apache.polaris.core.context.RequestIdSupplier; +import org.apache.polaris.core.persistence.metrics.MetricsPersistence; +import org.apache.polaris.core.persistence.metrics.MetricsSchemaBootstrap; +import org.apache.polaris.persistence.relational.jdbc.QueryGenerator.PreparedQuery; +import org.apache.polaris.persistence.relational.jdbc.models.MetricsSchemaVersion; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * CDI producer for {@link MetricsPersistence} in the JDBC persistence backend. + * + * <p>This producer creates {@link JdbcMetricsPersistence} instances when the JDBC persistence + * backend is in use. The schema version is loaded once at startup (application-scoped) to avoid + * hitting the database on every request. + * + * <p>This producer fails fast at startup if metrics tables are not available. Users must either + * bootstrap the metrics schema using the 'bootstrap-metrics' command or set {@code + * polaris.persistence.metrics.type=noop} to disable metrics persistence. + */ +@ApplicationScoped +public class JdbcMetricsPersistenceProducer { + + private static final Logger LOGGER = + LoggerFactory.getLogger(JdbcMetricsPersistenceProducer.class); + + private final DatasourceOperations datasourceOperations; Review Comment: I assume you are asking about quarkus datasource? The `JdbcMetricsPersistenceProducer` serves several purposes beyond just providing a datasource: 1. Startup validation: Checks if metrics tables exist and validates the schema version is in the supported range [1, LATEST] - this happens once at startup rather than on every request 2. Fail-fast behavior: Throws IllegalStateException at startup if metrics tables aren't bootstrapped, giving clear error messages to operators 3. Multiple bean production: Produces both MetricsPersistence (request-scoped) and MetricsSchemaBootstrap (application-scoped) beans 4. Context handling: Safely handles missing `PolarisPrincipal` and `RequestIdSupplier` in non-request contexts (e.g., Admin CLI) That said, if you think this could be simplified using named datasources, I'm open to refactoring. Could you elaborate on what you have in mind? Would we still maintain the startup validation and fail-fast behavior?" Also, can it be done in a separate PR? We have to separate out the metastore data source and the metrics data source out anyway. -- 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]
