flyrain commented on code in PR #3385:
URL: https://github.com/apache/polaris/pull/3385#discussion_r2831069436


##########
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcMetricsPersistenceProducer.java:
##########
@@ -0,0 +1,159 @@
+/*
+ * 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.context.RealmContext;
+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>When metrics tables are not available (metrics schema not bootstrapped), 
this producer returns
+ * {@link MetricsPersistence#NOOP} to avoid unnecessary database operations.
+ */
+@ApplicationScoped
+public class JdbcMetricsPersistenceProducer {
+
+  private static final Logger LOGGER =
+      LoggerFactory.getLogger(JdbcMetricsPersistenceProducer.class);
+
+  private final DatasourceOperations datasourceOperations;
+  private final boolean metricsSupported;
+
+  @SuppressWarnings("unused") // Required for CDI proxy
+  protected JdbcMetricsPersistenceProducer() {
+    this.datasourceOperations = null;
+    this.metricsSupported = false;
+  }
+
+  /**
+   * Creates the producer and determines metrics support at startup.
+   *
+   * <p>This constructor loads the metrics schema version once, avoiding 
database hits on every
+   * request. Metrics persistence is supported if the metrics_version table 
exists and contains a
+   * version >= 1.
+   *
+   * @param dataSource the datasource instance
+   * @param relationalJdbcConfiguration JDBC configuration
+   */
+  @Inject
+  public JdbcMetricsPersistenceProducer(
+      Instance<DataSource> dataSource, RelationalJdbcConfiguration 
relationalJdbcConfiguration) {
+    DatasourceOperations ops = null;
+    boolean supported = false;
+    try {
+      ops = new DatasourceOperations(dataSource.get(), 
relationalJdbcConfiguration);
+      // Check if metrics tables exist by querying the metrics_version table
+      supported = metricsTableExists(ops);
+      if (!supported) {
+        LOGGER.warn(
+            "Metrics tables not found. Metrics persistence operations will be 
no-ops. "
+                + "Run 'bootstrap-metrics' command to create metrics tables or 
set "
+                + "polaris.persistence.metrics.type=noop to disable metrics 
persistence.");
+      }
+    } catch (SQLException e) {
+      LOGGER.warn(
+          "Failed to initialize JdbcMetricsPersistenceProducer due to {}. "
+              + "Metrics persistence will be disabled.",
+          e.getMessage());
+    }
+    this.datasourceOperations = ops;
+    this.metricsSupported = supported;
+  }
+
+  /**
+   * Produces a {@link MetricsPersistence} instance for the current request.
+   *
+   * <p>If metrics tables are not available (determined at startup), this 
returns {@link
+   * MetricsPersistence#NOOP}. Otherwise, it creates a {@link 
JdbcMetricsPersistence} configured
+   * with the current realm.
+   *
+   * @param realmContext the realm context for the current request
+   * @return a MetricsPersistence implementation for JDBC, or NOOP if not 
supported
+   */
+  @Produces
+  @RequestScoped
+  @Identifier("relational-jdbc")
+  public MetricsPersistence metricsPersistence(RealmContext realmContext) {
+    if (!metricsSupported || datasourceOperations == null) {
+      return MetricsPersistence.NOOP;
+    }
+
+    String realmId = realmContext.getRealmIdentifier();
+    return new JdbcMetricsPersistence(datasourceOperations, realmId);
+  }
+
+  /**
+   * Produces a {@link MetricsSchemaBootstrap} instance for the JDBC backend.
+   *
+   * <p>This producer creates a {@link JdbcMetricsSchemaBootstrap} that can 
bootstrap the metrics
+   * schema tables independently from the entity schema.
+   *
+   * @return a MetricsSchemaBootstrap implementation for JDBC
+   */
+  @Produces
+  @ApplicationScoped
+  @Identifier("relational-jdbc")
+  public MetricsSchemaBootstrap metricsSchemaBootstrap() {
+    if (datasourceOperations == null) {
+      LOGGER.warn(
+          "DatasourceOperations not available. Returning NOOP 
MetricsSchemaBootstrap implementation.");
+      return MetricsSchemaBootstrap.NOOP;
+    }
+    return new JdbcMetricsSchemaBootstrap(datasourceOperations);
+  }
+
+  /**
+   * Checks if the metrics tables have been bootstrapped by querying the 
metrics_version table.
+   *
+   * @param datasourceOperations the datasource operations to use for the check
+   * @return true if the metrics_version table exists and contains data, false 
otherwise
+   */
+  static boolean metricsTableExists(DatasourceOperations datasourceOperations) 
{
+    PreparedQuery query = QueryGenerator.generateMetricsVersionQuery();
+    try {
+      List<MetricsSchemaVersion> versions =
+          datasourceOperations.executeSelect(query, new 
MetricsSchemaVersion());
+      return versions != null && !versions.isEmpty();

Review Comment:
   it currently checks only that metrics_version exists and has rows. It is 
worth validating the version value is within the supported range (for example 
>= 1 and <= LATEST_METRICS_SCHEMA_VERSION) so we do not treat corrupted or out 
of range versions as supported. 



-- 
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]

Reply via email to