dimas-b commented on code in PR #3385: URL: https://github.com/apache/polaris/pull/3385#discussion_r2825121723
########## persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcMetricsPersistenceProducer.java: ########## @@ -0,0 +1,124 @@ +/* + * 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 javax.sql.DataSource; +import org.apache.polaris.core.config.BehaviorChangeConfiguration; +import org.apache.polaris.core.config.RealmConfig; +import org.apache.polaris.core.context.RealmContext; +import org.apache.polaris.core.persistence.metrics.MetricsPersistence; +import org.apache.polaris.core.persistence.metrics.MetricsSchemaBootstrap; +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. When metrics tables are not available (schema version < 4), the produced + * instance will report this via {@link JdbcMetricsPersistence#supportsMetricsPersistence()}. + */ +@ApplicationScoped +public class JdbcMetricsPersistenceProducer { + + private static final Logger LOGGER = + LoggerFactory.getLogger(JdbcMetricsPersistenceProducer.class); + + @Inject Instance<DataSource> dataSource; + + @Inject RelationalJdbcConfiguration relationalJdbcConfiguration; + + /** + * Produces a {@link MetricsPersistence} instance for the current request. + * + * <p>This method creates a new {@link JdbcMetricsPersistence} configured with the current realm + * and schema version. If the schema version is less than 4 (which includes metrics tables), the + * returned instance will be functional but all operations will be no-ops. + * + * @param realmContext the current realm context (request-scoped) + * @param realmConfig the realm configuration (request-scoped) + * @return a MetricsPersistence implementation for JDBC + */ + @Produces + @RequestScoped + @Identifier("relational-jdbc") + public MetricsPersistence metricsPersistence(RealmContext realmContext, RealmConfig realmConfig) { + try { + DatasourceOperations datasourceOperations = + new DatasourceOperations(dataSource.get(), relationalJdbcConfiguration); + + String realmId = realmContext.getRealmIdentifier(); + + int schemaVersion = + JdbcBasePersistenceImpl.loadSchemaVersion( + datasourceOperations, + realmConfig.getConfig(BehaviorChangeConfiguration.SCHEMA_VERSION_FALL_BACK_ON_DNE)); + + JdbcMetricsPersistence persistence = + new JdbcMetricsPersistence(datasourceOperations, realmId, schemaVersion); + + if (!persistence.supportsMetricsPersistence()) { + LOGGER.warn( Review Comment: nit: Should we also produce the NOOP impl. in this case? nit: perhaps `supportsMetricsPersistence` can be done here, simplifying the metrics persistence SPI 🤔 ########## runtime/service/src/main/java/org/apache/polaris/service/persistence/MetricsPersistenceFactory.java: ########## @@ -0,0 +1,62 @@ +/* + * 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.service.persistence; + +import io.smallrye.common.annotation.Identifier; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Any; +import jakarta.enterprise.inject.Instance; +import jakarta.inject.Inject; +import org.apache.polaris.core.persistence.metrics.MetricsPersistence; + +/** + * Factory for creating {@link MetricsPersistence} instances. + * + * <p>This factory is {@link ApplicationScoped} to resolve the configured {@link MetricsPersistence} + * implementation once at startup. The heavy CDI bean selection is performed once, and subsequent + * calls to {@link #create()} are lightweight operations that simply retrieve an instance from the + * already-selected implementation. + * + * <p>This pattern avoids resolving the bean selector on every request, improving performance for + * request-scoped {@link MetricsPersistence} beans. + */ +@ApplicationScoped +public class MetricsPersistenceFactory { + + private final Instance<MetricsPersistence> selectedImpl; + + @Inject + public MetricsPersistenceFactory( + MetricsPersistenceConfiguration config, + @Any Instance<MetricsPersistence> metricsPersistenceImpls) { + // Resolve the selector once at startup based on configuration + this.selectedImpl = metricsPersistenceImpls.select(Identifier.Literal.of(config.type())); + } + + /** + * Creates a {@link MetricsPersistence} instance from the configured implementation. + * + * <p>This is a lightweight operation since the implementation was already selected at startup. + * + * @return a MetricsPersistence instance + */ + public MetricsPersistence create() { + return selectedImpl.get(); Review Comment: Actually what `JdbcMetricsPersistenceProducer` does is probably sufficient and this class may not be needed. Sorry about the confusion. Please debug `JdbcMetricsPersistenceProducer` and check whether per-request call paths only use pre-discovered information. ########## persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcMetricsPersistenceProducer.java: ########## @@ -0,0 +1,124 @@ +/* + * 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 javax.sql.DataSource; +import org.apache.polaris.core.config.BehaviorChangeConfiguration; +import org.apache.polaris.core.config.RealmConfig; +import org.apache.polaris.core.context.RealmContext; +import org.apache.polaris.core.persistence.metrics.MetricsPersistence; +import org.apache.polaris.core.persistence.metrics.MetricsSchemaBootstrap; +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. When metrics tables are not available (schema version < 4), the produced + * instance will report this via {@link JdbcMetricsPersistence#supportsMetricsPersistence()}. + */ +@ApplicationScoped +public class JdbcMetricsPersistenceProducer { + + private static final Logger LOGGER = + LoggerFactory.getLogger(JdbcMetricsPersistenceProducer.class); + + @Inject Instance<DataSource> dataSource; + + @Inject RelationalJdbcConfiguration relationalJdbcConfiguration; + + /** + * Produces a {@link MetricsPersistence} instance for the current request. + * + * <p>This method creates a new {@link JdbcMetricsPersistence} configured with the current realm + * and schema version. If the schema version is less than 4 (which includes metrics tables), the + * returned instance will be functional but all operations will be no-ops. + * + * @param realmContext the current realm context (request-scoped) + * @param realmConfig the realm configuration (request-scoped) + * @return a MetricsPersistence implementation for JDBC + */ + @Produces + @RequestScoped + @Identifier("relational-jdbc") + public MetricsPersistence metricsPersistence(RealmContext realmContext, RealmConfig realmConfig) { + try { + DatasourceOperations datasourceOperations = + new DatasourceOperations(dataSource.get(), relationalJdbcConfiguration); + + String realmId = realmContext.getRealmIdentifier(); + + int schemaVersion = + JdbcBasePersistenceImpl.loadSchemaVersion( + datasourceOperations, + realmConfig.getConfig(BehaviorChangeConfiguration.SCHEMA_VERSION_FALL_BACK_ON_DNE)); Review Comment: fallback can happen here, if the the application-scoped code did not find the version. however, do we need a fallback for metrics at all? We have first-class version support from day 1. ########## persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcMetricsPersistenceProducer.java: ########## @@ -0,0 +1,124 @@ +/* + * 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 javax.sql.DataSource; +import org.apache.polaris.core.config.BehaviorChangeConfiguration; +import org.apache.polaris.core.config.RealmConfig; +import org.apache.polaris.core.context.RealmContext; +import org.apache.polaris.core.persistence.metrics.MetricsPersistence; +import org.apache.polaris.core.persistence.metrics.MetricsSchemaBootstrap; +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. When metrics tables are not available (schema version < 4), the produced + * instance will report this via {@link JdbcMetricsPersistence#supportsMetricsPersistence()}. + */ +@ApplicationScoped +public class JdbcMetricsPersistenceProducer { + + private static final Logger LOGGER = + LoggerFactory.getLogger(JdbcMetricsPersistenceProducer.class); + + @Inject Instance<DataSource> dataSource; + + @Inject RelationalJdbcConfiguration relationalJdbcConfiguration; + + /** + * Produces a {@link MetricsPersistence} instance for the current request. + * + * <p>This method creates a new {@link JdbcMetricsPersistence} configured with the current realm + * and schema version. If the schema version is less than 4 (which includes metrics tables), the + * returned instance will be functional but all operations will be no-ops. + * + * @param realmContext the current realm context (request-scoped) + * @param realmConfig the realm configuration (request-scoped) + * @return a MetricsPersistence implementation for JDBC + */ + @Produces + @RequestScoped + @Identifier("relational-jdbc") + public MetricsPersistence metricsPersistence(RealmContext realmContext, RealmConfig realmConfig) { + try { + DatasourceOperations datasourceOperations = + new DatasourceOperations(dataSource.get(), relationalJdbcConfiguration); + + String realmId = realmContext.getRealmIdentifier(); + + int schemaVersion = + JdbcBasePersistenceImpl.loadSchemaVersion( Review Comment: sorry, I was not clear in my earlier comments, this can probably be done in the constrictor of this class, so that we do not have to hit the database for schema version on every request. WDYT? -- 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]
