FANNG1 commented on code in PR #10106:
URL: https://github.com/apache/gravitino/pull/10106#discussion_r2889664519


##########
maintenance/optimizer-api/src/main/java/org/apache/gravitino/maintenance/optimizer/common/util/StatisticValueSerdeUtils.java:
##########
@@ -0,0 +1,50 @@
+/*
+ * 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.maintenance.optimizer.common.util;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.google.common.base.Preconditions;
+import org.apache.gravitino.json.JsonUtils;
+import org.apache.gravitino.stats.StatisticValue;
+
+/** Shared serde helpers for {@link StatisticValue}. */
+public final class StatisticValueSerdeUtils {
+
+  private StatisticValueSerdeUtils() {}
+
+  public static String toString(StatisticValue<?> value) {

Review Comment:
   Fixed in 8bf02e1b0544bec56dace0dd0e2492a001765151. Added JavaDoc (including 
`@param`, `@return`, and `@throws`) for both `toString` and `fromString`.



##########
maintenance/jobs/src/main/java/org/apache/gravitino/maintenance/jobs/iceberg/IcebergUpdateStatsAndMetricsJob.java:
##########
@@ -0,0 +1,607 @@
+/*
+ * 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.maintenance.jobs.iceberg;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.job.JobTemplateProvider;
+import org.apache.gravitino.job.SparkJobTemplate;
+import org.apache.gravitino.maintenance.jobs.BuiltInJob;
+import org.apache.gravitino.maintenance.optimizer.api.common.MetricPoint;
+import org.apache.gravitino.maintenance.optimizer.api.common.PartitionEntry;
+import org.apache.gravitino.maintenance.optimizer.api.common.PartitionPath;
+import org.apache.gravitino.maintenance.optimizer.api.common.StatisticEntry;
+import org.apache.gravitino.maintenance.optimizer.api.updater.MetricsUpdater;
+import 
org.apache.gravitino.maintenance.optimizer.api.updater.StatisticsUpdater;
+import org.apache.gravitino.maintenance.optimizer.common.OptimizerEnv;
+import org.apache.gravitino.maintenance.optimizer.common.PartitionEntryImpl;
+import org.apache.gravitino.maintenance.optimizer.common.StatisticEntryImpl;
+import org.apache.gravitino.maintenance.optimizer.common.conf.OptimizerConfig;
+import 
org.apache.gravitino.maintenance.optimizer.common.util.IcebergSparkConfigUtils;
+import org.apache.gravitino.maintenance.optimizer.common.util.ProviderUtils;
+import org.apache.gravitino.stats.StatisticValues;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.SparkSession;
+import org.apache.spark.sql.types.StructField;
+import org.apache.spark.sql.types.StructType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Built-in job for computing Iceberg table file statistics and persisting 
them to Gravitino. */
+public class IcebergUpdateStatsAndMetricsJob implements BuiltInJob {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(IcebergUpdateStatsAndMetricsJob.class);
+
+  private static final String NAME =
+      JobTemplateProvider.BUILTIN_NAME_PREFIX + "iceberg-update-stats";
+  private static final String VERSION = "v1";
+  private static final String DEFAULT_STATISTICS_UPDATER = 
"gravitino-statistics-updater";
+  private static final String DEFAULT_METRICS_UPDATER = 
"gravitino-metrics-updater";
+  private static final long DEFAULT_TARGET_FILE_SIZE_BYTES = 128L * 1024 * 
1024;
+  private static final long SMALL_FILE_THRESHOLD_BYTES = 32L * 1024 * 1024;
+  private static final String DEFAULT_UPDATE_MODE = UpdateMode.ALL.modeName;
+  private static final String CUSTOM_STAT_PREFIX = "custom-";
+
+  @Override
+  public SparkJobTemplate jobTemplate() {
+    return SparkJobTemplate.builder()
+        .withName(NAME)
+        .withComment(
+            "Built-in Iceberg update stats job template for computing datafile 
MSE and file metrics")
+        
.withExecutable(resolveExecutable(IcebergUpdateStatsAndMetricsJob.class))
+        .withClassName(IcebergUpdateStatsAndMetricsJob.class.getName())
+        .withArguments(buildArguments())
+        .withConfigs(buildSparkConfigs())
+        .withCustomFields(
+            Collections.singletonMap(JobTemplateProvider.PROPERTY_VERSION_KEY, 
VERSION))
+        .build();
+  }
+
+  /** Main entry point. */
+  public static void main(String[] args) {
+    Map<String, String> argMap = parseArguments(args);
+    String catalogName = argMap.get("catalog");
+    String tableIdentifier = argMap.get("table");
+    UpdateMode updateMode = parseUpdateMode(argMap.get("update-mode"));
+
+    if (catalogName == null || tableIdentifier == null) {
+      System.err.println("Error: --catalog and --table are required 
arguments");
+      printUsage();
+      System.exit(1);
+    }
+
+    Map<String, String> updaterOptions = 
parseJsonOptions(argMap.get("updater-options"));
+    String sparkConfJson = argMap.get("spark-conf");
+
+    SparkSession.Builder sparkBuilder =
+        SparkSession.builder().appName("Gravitino Built-in Iceberg Update 
Stats");
+
+    if (sparkConfJson != null && !sparkConfJson.isEmpty()) {
+      Map<String, String> customConfigs = 
parseCustomSparkConfigs(sparkConfJson);
+      for (Map.Entry<String, String> entry : customConfigs.entrySet()) {
+        sparkBuilder.config(entry.getKey(), entry.getValue());
+      }
+    }
+
+    SparkSession spark = sparkBuilder.getOrCreate();
+    StatisticsUpdater statisticsUpdater = null;
+    MetricsUpdater metricsUpdater = null;
+    try {
+      Map<String, String> optimizerProperties = 
buildOptimizerProperties(updaterOptions);
+      if (updateMode.updateStats) {
+        String statisticsUpdaterName =
+            updaterOptions.getOrDefault("statistics_updater", 
DEFAULT_STATISTICS_UPDATER).trim();
+        statisticsUpdater =
+            createStatisticsUpdater(
+                statisticsUpdaterName, 
requireGravitinoConfig(optimizerProperties));
+      }
+      if (updateMode.updateMetrics) {
+        String metricsUpdaterName =
+            updaterOptions.getOrDefault("metrics_updater", 
DEFAULT_METRICS_UPDATER).trim();
+        metricsUpdater = createMetricsUpdater(metricsUpdaterName, 
optimizerProperties);
+      }
+
+      updateStatistics(
+          spark, statisticsUpdater, metricsUpdater, updateMode, catalogName, 
tableIdentifier);
+    } catch (Exception e) {
+      LOG.error("Failed to update Iceberg statistics/metrics", e);
+      System.exit(1);
+    } finally {
+      if (statisticsUpdater != null) {
+        try {
+          statisticsUpdater.close();
+        } catch (Exception e) {
+          LOG.warn("Failed to close statistics updater", e);
+        }
+      }
+      if (metricsUpdater != null) {
+        try {
+          metricsUpdater.close();
+        } catch (Exception e) {
+          LOG.warn("Failed to close metrics updater", e);
+        }
+      }
+      spark.stop();
+    }
+  }
+
+  static void updateStatistics(

Review Comment:
   Fixed in 8bf02e1b0544bec56dace0dd0e2492a001765151. Added 
`@VisibleForTesting` to the package-private static helpers that are 
package-visible primarily for test access.



##########
maintenance/optimizer/src/test/java/org/apache/gravitino/maintenance/optimizer/recommender/job/TestBuiltinIcebergUpdateStatsJob.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.maintenance.optimizer.recommender.job;
+
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.stream.Collectors;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.Catalog;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.client.GravitinoAdminClient;
+import org.apache.gravitino.client.GravitinoClient;
+import org.apache.gravitino.client.GravitinoMetalake;
+import org.apache.gravitino.exceptions.NoSuchMetalakeException;
+import org.apache.gravitino.job.JobHandle;
+import org.apache.gravitino.rel.Table;
+import org.apache.gravitino.stats.Statistic;
+import org.apache.spark.sql.SparkSession;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+
+/** Environment IT for triggering built-in Iceberg update stats job through 
the client API. */
+@EnabledIfEnvironmentVariable(named = "GRAVITINO_ENV_IT", matches = "true")
+public class TestBuiltinIcebergUpdateStatsJob {
+
+  private static final String SERVER_URI = "http://localhost:8090";;
+  private static final String ICEBERG_REST_URI = 
"http://localhost:9001/iceberg";;
+  private static final String JOB_TEMPLATE_NAME = 
"builtin-iceberg-update-stats";
+  private static final String SPARK_CATALOG_NAME = "rest_catalog";
+  private static final String WAREHOUSE_LOCATION = "";

Review Comment:
   Fixed in 8bf02e1b0544bec56dace0dd0e2492a001765151. The test now derives 
warehouse from `GRAVITINO_TEST_WAREHOUSE` with a safe fallback and adds an 
explicit assumption guard to avoid confusing runtime failures.



##########
clients/client-java/build.gradle.kts:
##########
@@ -16,12 +16,27 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+import org.gradle.api.attributes.java.TargetJvmVersion
+import org.gradle.api.tasks.compile.JavaCompile
+

Review Comment:
   Fixed in 8bf02e1b0544bec56dace0dd0e2492a001765151. Added an explicit comment 
in `clients/client-java/build.gradle.kts` explaining why `compileTestJava` uses 
release 17 while the main artifact remains Java 8-compatible.



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