laserninja commented on code in PR #13533:
URL: https://github.com/apache/gravitino/pull/13533#discussion_r4365991109


##########
maintenance/optimizer/src/main/java/org/apache/gravitino/maintenance/optimizer/recommender/handler/ManifestRewriteStrategyHandler.java:
##########
@@ -0,0 +1,215 @@
+/*
+ * 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.handler;
+
+import static 
org.apache.gravitino.catalog.lakehouse.iceberg.IcebergConstants.CURRENT_SNAPSHOT_ID;
+
+import com.google.common.base.Preconditions;
+import java.math.BigInteger;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import javax.annotation.Nullable;
+import org.apache.gravitino.maintenance.optimizer.api.common.StatisticEntry;
+import 
org.apache.gravitino.maintenance.optimizer.api.recommender.StrategyEvaluation;
+import 
org.apache.gravitino.maintenance.optimizer.api.recommender.StrategyHandler;
+import 
org.apache.gravitino.maintenance.optimizer.api.recommender.StrategyHandlerContext;
+import 
org.apache.gravitino.maintenance.optimizer.common.IcebergManifestStatistics;
+import org.apache.gravitino.policy.IcebergRewriteManifestsContent;
+import org.apache.gravitino.policy.PolicyContents;
+import org.apache.gravitino.stats.StatisticValue;
+import org.apache.gravitino.stats.StatisticValues;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Evaluates a complete manifest measurement for exactly one resolved 
partition spec. */
+public final class ManifestRewriteStrategyHandler implements StrategyHandler {
+  private static final Logger LOG = 
LoggerFactory.getLogger(ManifestRewriteStrategyHandler.class);
+
+  private StrategyEvaluation evaluation = StrategyEvaluation.NO_EXECUTION;
+
+  @Override
+  public Set<DataRequirement> dataRequirements() {
+    return Set.of(DataRequirement.TABLE_METADATA, 
DataRequirement.TABLE_STATISTICS);
+  }
+
+  @Override
+  public String strategyType() {
+    return IcebergRewriteManifestsContent.STRATEGY_TYPE_VALUE;
+  }
+
+  @Override
+  public void initialize(StrategyHandlerContext context) {
+    evaluation = StrategyEvaluation.NO_EXECUTION;
+    Integer requestedSpec = requestedSpec(context.strategy().rules());
+    if (requestedSpec == null) {
+      LOG.warn(
+          "Skipping manifest rewrite policy {} for {}: set spec_id in the 
policy before running the recommender",
+          context.strategy().name(),
+          context.nameIdentifier());
+      return;
+    }
+    initialize(context, requestedSpec);
+  }
+
+  /**
+   * Evaluates statistics using the spec resolved at collection start. The 
default is never resolved
+   * again, even if partition evolution has occurred since collection.
+   *
+   * <p>This overload is for direct programmatic callers. The recommender and 
CLI call {@link
+   * #initialize(StrategyHandlerContext)} and require an explicit policy spec 
ID.
+   *
+   * @param context strategy, fresh table metadata, and one consistent 
table-statistics response
+   * @param resolvedSpecId spec selected before collection
+   */
+  public void initialize(StrategyHandlerContext context, int resolvedSpecId) {

Review Comment:
   Fixed in 8e5163930. Removed the two-argument initialize overload. Every 
caller now uses the single path requiring an explicit policy spec_id. The real 
Spark regression sets that target before collection and verifies the same spec 
is retained after partition evolution, recollection, evaluation, and rewriting.



##########
maintenance/optimizer/src/main/java/org/apache/gravitino/maintenance/optimizer/recommender/Recommender.java:
##########
@@ -371,7 +373,12 @@ private StrategyHandler createStrategyHandler(String 
strategyType) {
    * {@code COMPACTION}) to {@link StrategyHandler} implementations.
    */
   private String getStrategyHandlerClassName(String strategyType) {
-    return optimizerEnv.config().getStrategyHandlerClassName(strategyType);
+    String configured = 
optimizerEnv.config().getStrategyHandlerClassName(strategyType);
+    if (StringUtils.isBlank(configured)
+        && 
IcebergRewriteManifestsContent.STRATEGY_TYPE_VALUE.equals(strategyType)) {
+      return ManifestRewriteStrategyHandler.class.getName();

Review Comment:
   Fixed in 8e5163930. Metadata is now fetched after table/partition 
statistics. A regression advances the table snapshot during the statistics read 
and verifies that stale measurements do not submit a rewrite. The docs also 
make clear that this freshness check is not transactional with subsequent job 
submission.



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