stevenzwu commented on code in PR #12493:
URL: https://github.com/apache/iceberg/pull/12493#discussion_r2014975086


##########
core/src/main/java/org/apache/iceberg/actions/SizeBasedFileRewritePlanner.java:
##########
@@ -0,0 +1,361 @@
+/*
+ * 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.iceberg.actions;
+
+import java.math.RoundingMode;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.iceberg.ContentFile;
+import org.apache.iceberg.ContentScanTask;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.math.LongMath;
+import org.apache.iceberg.util.BinPacking;
+import org.apache.iceberg.util.PropertyUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A file rewrite planner that determines which files to rewrite based on 
their size.
+ *
+ * <p>If files are smaller than the {@link #MIN_FILE_SIZE_BYTES} threshold or 
larger than the {@link
+ * #MAX_FILE_SIZE_BYTES} threshold, they are considered targets for being 
rewritten.
+ *
+ * <p>Once selected, files are grouped based on the {@link BinPacking 
bin-packing algorithm} into
+ * groups of no more than {@link #MAX_FILE_GROUP_SIZE_BYTES}. Groups will be 
actually rewritten if
+ * they contain more than {@link #MIN_INPUT_FILES} or if they would produce at 
least one file of
+ * {@link #TARGET_FILE_SIZE_BYTES}.
+ *
+ * <p>Note that implementations may add extra conditions for selecting files 
or filtering groups.
+ */
+public abstract class SizeBasedFileRewritePlanner<
+        I,
+        T extends ContentScanTask<F>,
+        F extends ContentFile<F>,
+        G extends RewriteGroupBase<I, T, F>>
+    implements FileRewritePlanner<I, T, F, G> {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SizeBasedFileRewritePlanner.class);
+
+  /** The target output file size that this file rewriter will attempt to 
generate. */
+  public static final String TARGET_FILE_SIZE_BYTES = "target-file-size-bytes";
+
+  /**
+   * Controls which files will be considered for rewriting. Files with sizes 
under this threshold
+   * will be considered for rewriting regardless of any other criteria.
+   *
+   * <p>Defaults to 75% of the target file size.
+   */
+  public static final String MIN_FILE_SIZE_BYTES = "min-file-size-bytes";
+
+  public static final double MIN_FILE_SIZE_DEFAULT_RATIO = 0.75;
+
+  /**
+   * Controls which files will be considered for rewriting. Files with sizes 
above this threshold
+   * will be considered for rewriting regardless of any other criteria.
+   *
+   * <p>Defaults to 180% of the target file size.
+   */
+  public static final String MAX_FILE_SIZE_BYTES = "max-file-size-bytes";
+
+  public static final double MAX_FILE_SIZE_DEFAULT_RATIO = 1.80;
+
+  /**
+   * Any file group exceeding this number of files will be rewritten 
regardless of other criteria.
+   * This config ensures file groups that contain many files are compacted 
even if the total size of
+   * that group is less than the target file size. This can also be thought of 
as the maximum number
+   * of wrongly sized files that could remain in a partition after rewriting.
+   */
+  public static final String MIN_INPUT_FILES = "min-input-files";
+
+  public static final int MIN_INPUT_FILES_DEFAULT = 5;
+
+  /** Overrides other options and forces rewriting of all provided files. */
+  public static final String REWRITE_ALL = "rewrite-all";
+
+  public static final boolean REWRITE_ALL_DEFAULT = false;
+
+  /**
+   * This option controls the largest amount of data that should be rewritten 
in a single file
+   * group. It helps with breaking down the rewriting of very large partitions 
which may not be
+   * rewritable otherwise due to the resource constraints of the cluster. For 
example, a sort-based
+   * rewrite may not scale to TB-sized partitions, and those partitions need 
to be worked on in
+   * small subsections to avoid exhaustion of resources.
+   */
+  public static final String MAX_FILE_GROUP_SIZE_BYTES = 
"max-file-group-size-bytes";
+
+  public static final long MAX_FILE_GROUP_SIZE_BYTES_DEFAULT = 100L * 1024 * 
1024 * 1024; // 100 GB
+
+  private static final long SPLIT_OVERHEAD = 5L * 1024;
+
+  private final Table table;
+  private long targetFileSize;
+  private long minFileSize;
+  private long maxFileSize;
+  private int minInputFiles;
+  private boolean rewriteAll;
+  private long maxGroupSize;
+  private int outputSpecId;
+
+  protected SizeBasedFileRewritePlanner(Table table) {
+    this.table = table;
+  }
+
+  /** Expected target file size before configuration. */
+  protected abstract long defaultTargetFileSize();
+
+  /** Additional filter for tasks before grouping. */
+  protected abstract Iterable<T> filterFiles(Iterable<T> tasks);
+
+  /** Additional filter for groups. */
+  protected abstract Iterable<List<T>> filterFileGroups(List<List<T>> groups);
+
+  @Override
+  public Set<String> validOptions() {
+    return ImmutableSet.of(
+        TARGET_FILE_SIZE_BYTES,
+        MIN_FILE_SIZE_BYTES,
+        MAX_FILE_SIZE_BYTES,
+        MIN_INPUT_FILES,
+        REWRITE_ALL,
+        MAX_FILE_GROUP_SIZE_BYTES);
+  }
+
+  @Override
+  public void init(Map<String, String> options) {
+    Map<String, Long> sizeThresholds = sizeThresholds(options);
+    this.targetFileSize = sizeThresholds.get(TARGET_FILE_SIZE_BYTES);
+    this.minFileSize = sizeThresholds.get(MIN_FILE_SIZE_BYTES);
+    this.maxFileSize = sizeThresholds.get(MAX_FILE_SIZE_BYTES);
+    this.minInputFiles = minInputFiles(options);
+    this.rewriteAll = rewriteAll(options);
+    this.maxGroupSize = maxGroupSize(options);
+    this.outputSpecId = outputSpecId(options);
+
+    if (rewriteAll) {
+      LOG.info("Configured to rewrite all provided files in table {}", 
table.name());
+    }
+  }
+
+  protected Table table() {
+    return table;
+  }
+
+  protected boolean wronglySized(T task) {

Review Comment:
   I know the method name is copied from existing code. I don't know if we are 
entertaining maybe some renaming to make it easier to read.
   
   e.g., this one can be called `outsideDesiredFileSizeRange`.



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to