Copilot commented on code in PR #10138:
URL: https://github.com/apache/gravitino/pull/10138#discussion_r2875922742


##########
maintenance/optimizer/src/main/java/org/apache/gravitino/maintenance/optimizer/updater/Updater.java:
##########
@@ -102,140 +94,216 @@ public void update(
       List<NameIdentifier> nameIdentifiers,
       UpdateType updateType) {
     StatisticsCalculator calculator = 
getStatisticsCalculator(statisticsCalculatorName);
-    List<TableMetricWriteRequest> tableMetricWriteRequests = new ArrayList<>();
-    List<JobMetricWriteRequest> jobMetricWriteRequests = new ArrayList<>();
+
+    if (UpdateType.STATISTICS.equals(updateType)) {
+      updateStatisticsForIdentifiers(statisticsCalculatorName, 
nameIdentifiers, calculator);
+      return;
+    }
+
+    updateMetricsForIdentifiers(statisticsCalculatorName, nameIdentifiers, 
calculator);
+  }
+
+  /**
+   * Updates statistics or metrics for all identifiers returned by the 
calculator.
+   *
+   * @param statisticsCalculatorName The provider name of the statistics 
calculator.
+   * @param updateType The target update type: statistics or metrics.
+   */
+  public void updateAll(String statisticsCalculatorName, UpdateType 
updateType) {
+    StatisticsCalculator calculator = 
getStatisticsCalculator(statisticsCalculatorName);
+
+    if (UpdateType.STATISTICS.equals(updateType)) {
+      updateAllStatistics(statisticsCalculatorName, calculator);
+      return;
+    }
+
+    updateAllMetrics(statisticsCalculatorName, calculator);
+  }
+
+  @VisibleForTesting
+  public MetricsUpdater getMetricsUpdater() {
+    return metricsUpdater;
+  }
+
+  @Override
+  public void close() throws Exception {
+    closeableGroup.close();
+  }
+
+  private void updateStatisticsForIdentifiers(
+      String statisticsCalculatorName,
+      List<NameIdentifier> nameIdentifiers,
+      StatisticsCalculator calculator) {
+    long tableRecords = 0;
+    long partitionRecords = 0;
+
+    for (NameIdentifier nameIdentifier : nameIdentifiers) {
+      if (!(calculator instanceof SupportsCalculateTableStatistics)) {
+        continue;
+      }
+      SupportsCalculateTableStatistics supportTableStatistics =
+          (SupportsCalculateTableStatistics) calculator;
+      TableAndPartitionStatistics bundle =
+          supportTableStatistics.calculateTableStatistics(nameIdentifier);
+      List<StatisticEntry<?>> statistics = bundle != null ? 
bundle.tableStatistics() : List.of();
+      Map<PartitionPath, List<StatisticEntry<?>>> partitionStatistics =
+          bundle != null ? bundle.partitionStatistics() : Map.of();
+
+      tableRecords += countStatistics(statistics);
+      partitionRecords += countPartitionStatistics(partitionStatistics);
+      LOG.info(
+          "Updating table statistics: calculator={}, identifier={}",
+          statisticsCalculatorName,
+          nameIdentifier);
+
+      updateTableStatistics(statistics, nameIdentifier);
+      updatePartitionStatistics(partitionStatistics, nameIdentifier);
+    }
+
+    System.out.println(
+        String.format(
+            "SUMMARY: %s totalRecords=%d tableRecords=%d partitionRecords=%d 
jobRecords=%d",
+            UpdateType.STATISTICS.name().toLowerCase(Locale.ROOT),
+            tableRecords + partitionRecords,
+            tableRecords,
+            partitionRecords,
+            0L));
+  }
+
+  private void updateMetricsForIdentifiers(
+      String statisticsCalculatorName,
+      List<NameIdentifier> nameIdentifiers,
+      StatisticsCalculator calculator) {
+    boolean hasTableMetricsCalculator = calculator instanceof 
SupportsCalculateTableMetrics;
+    boolean hasJobMetricsCalculator = calculator instanceof 
SupportsCalculateJobMetrics;
+    if (!hasTableMetricsCalculator && !hasJobMetricsCalculator) {
+      throw new IllegalArgumentException(
+          String.format(
+              "Statistics calculator '%s' does not implement metric 
interfaces. "
+                  + "Expected SupportsCalculateTableMetrics and/or 
SupportsCalculateJobMetrics.",
+              statisticsCalculatorName));
+    }
+
     long tableRecords = 0;
     long partitionRecords = 0;
     long jobRecords = 0;
+
     for (NameIdentifier nameIdentifier : nameIdentifiers) {
-      if (calculator instanceof SupportsCalculateTableStatistics) {
-        SupportsCalculateTableStatistics supportTableStatistics =
-            ((SupportsCalculateTableStatistics) calculator);
-        TableAndPartitionStatistics bundle =
-            supportTableStatistics.calculateTableStatistics(nameIdentifier);
-        List<StatisticEntry<?>> statistics = bundle != null ? 
bundle.tableStatistics() : List.of();
-        Map<PartitionPath, List<StatisticEntry<?>>> partitionStatistics =
-            bundle != null ? bundle.partitionStatistics() : Map.of();
-        tableRecords += countStatistics(statistics);
-        partitionRecords += countPartitionStatistics(partitionStatistics);
+      if (hasTableMetricsCalculator) {
+        List<MetricPoint> metrics =
+            ((SupportsCalculateTableMetrics) 
calculator).calculateTableMetrics(nameIdentifier);
+        tableRecords += countMetricsByScope(metrics, MetricPoint.Scope.TABLE);
+        partitionRecords += countMetricsByScope(metrics, 
MetricPoint.Scope.PARTITION);
         LOG.info(
-            "Updating table statistics/metrics: calculator={}, updateType={}, 
identifier={}",
+            "Updating table/partition metrics: calculator={}, identifier={}, 
count={}",
             statisticsCalculatorName,
-            updateType,
-            nameIdentifier);
-        if (UpdateType.STATISTICS.equals(updateType)) {
-          updateTableStatistics(statistics, nameIdentifier);
-          updatePartitionStatistics(partitionStatistics, nameIdentifier);
-        } else {
-          tableMetricWriteRequests.addAll(collectTableMetrics(statistics, 
nameIdentifier));
-          tableMetricWriteRequests.addAll(
-              collectPartitionMetrics(partitionStatistics, nameIdentifier));
-        }
+            nameIdentifier,
+            metrics == null ? 0 : metrics.size());
+        updateMetrics(metrics);
       }
-      if (calculator instanceof SupportsCalculateJobStatistics
-          && UpdateType.METRICS.equals(updateType)) {
-        SupportsCalculateJobStatistics supportJobStatistics =
-            ((SupportsCalculateJobStatistics) calculator);
-        List<StatisticEntry<?>> statistics =
-            supportJobStatistics.calculateJobStatistics(nameIdentifier);
-        jobRecords += countStatistics(statistics);
+
+      if (hasJobMetricsCalculator) {
+        List<MetricPoint> metrics =
+            ((SupportsCalculateJobMetrics) 
calculator).calculateJobMetrics(nameIdentifier);
+        jobRecords += countMetricsByScope(metrics, MetricPoint.Scope.JOB);
         LOG.info(
-            "Updating job metrics: calculator={}, identifier={}",
+            "Updating job metrics: calculator={}, identifier={}, count={}",
             statisticsCalculatorName,
-            nameIdentifier);
-        jobMetricWriteRequests.addAll(collectJobMetrics(statistics, 
nameIdentifier));
+            nameIdentifier,
+            metrics == null ? 0 : metrics.size());
+        updateMetrics(metrics);
       }

Review Comment:
   updateMetrics(metrics) is invoked inside the identifier loop for both 
table/partition and job metrics. This can result in many small persistence 
calls (and DB batches) when updating a large set of identifiers. Consider 
collecting MetricPoint instances across identifiers (and across scopes if 
possible) and calling metricsUpdater.updateMetrics(...) once per update 
invocation (or at least once per scope) to reduce overhead.



##########
maintenance/optimizer/src/main/java/org/apache/gravitino/maintenance/optimizer/updater/Updater.java:
##########
@@ -102,140 +94,216 @@ public void update(
       List<NameIdentifier> nameIdentifiers,
       UpdateType updateType) {
     StatisticsCalculator calculator = 
getStatisticsCalculator(statisticsCalculatorName);
-    List<TableMetricWriteRequest> tableMetricWriteRequests = new ArrayList<>();
-    List<JobMetricWriteRequest> jobMetricWriteRequests = new ArrayList<>();
+
+    if (UpdateType.STATISTICS.equals(updateType)) {
+      updateStatisticsForIdentifiers(statisticsCalculatorName, 
nameIdentifiers, calculator);
+      return;
+    }
+
+    updateMetricsForIdentifiers(statisticsCalculatorName, nameIdentifiers, 
calculator);
+  }
+
+  /**
+   * Updates statistics or metrics for all identifiers returned by the 
calculator.
+   *
+   * @param statisticsCalculatorName The provider name of the statistics 
calculator.
+   * @param updateType The target update type: statistics or metrics.
+   */
+  public void updateAll(String statisticsCalculatorName, UpdateType 
updateType) {
+    StatisticsCalculator calculator = 
getStatisticsCalculator(statisticsCalculatorName);
+
+    if (UpdateType.STATISTICS.equals(updateType)) {
+      updateAllStatistics(statisticsCalculatorName, calculator);
+      return;
+    }
+
+    updateAllMetrics(statisticsCalculatorName, calculator);
+  }
+
+  @VisibleForTesting
+  public MetricsUpdater getMetricsUpdater() {
+    return metricsUpdater;
+  }
+
+  @Override
+  public void close() throws Exception {
+    closeableGroup.close();
+  }
+
+  private void updateStatisticsForIdentifiers(
+      String statisticsCalculatorName,
+      List<NameIdentifier> nameIdentifiers,
+      StatisticsCalculator calculator) {
+    long tableRecords = 0;
+    long partitionRecords = 0;
+
+    for (NameIdentifier nameIdentifier : nameIdentifiers) {
+      if (!(calculator instanceof SupportsCalculateTableStatistics)) {
+        continue;
+      }
+      SupportsCalculateTableStatistics supportTableStatistics =
+          (SupportsCalculateTableStatistics) calculator;
+      TableAndPartitionStatistics bundle =
+          supportTableStatistics.calculateTableStatistics(nameIdentifier);
+      List<StatisticEntry<?>> statistics = bundle != null ? 
bundle.tableStatistics() : List.of();
+      Map<PartitionPath, List<StatisticEntry<?>>> partitionStatistics =
+          bundle != null ? bundle.partitionStatistics() : Map.of();
+
+      tableRecords += countStatistics(statistics);
+      partitionRecords += countPartitionStatistics(partitionStatistics);
+      LOG.info(
+          "Updating table statistics: calculator={}, identifier={}",
+          statisticsCalculatorName,
+          nameIdentifier);
+
+      updateTableStatistics(statistics, nameIdentifier);
+      updatePartitionStatistics(partitionStatistics, nameIdentifier);
+    }
+
+    System.out.println(
+        String.format(
+            "SUMMARY: %s totalRecords=%d tableRecords=%d partitionRecords=%d 
jobRecords=%d",
+            UpdateType.STATISTICS.name().toLowerCase(Locale.ROOT),
+            tableRecords + partitionRecords,
+            tableRecords,
+            partitionRecords,
+            0L));

Review Comment:
   This code prints summary information using System.out.println. For 
consistency with the rest of the module and to support production 
logging/redirects, prefer using the existing SLF4J logger (LOG.info/LOG.debug) 
instead of writing directly to stdout.



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