aokolnychyi commented on a change in pull request #894: Spark: Implement an 
action to remove orphan files
URL: https://github.com/apache/incubator-iceberg/pull/894#discussion_r406573683
 
 

 ##########
 File path: spark/src/main/java/org/apache/iceberg/RemoveOrphanFilesAction.java
 ##########
 @@ -0,0 +1,239 @@
+/*
+ * 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;
+
+import com.google.common.collect.Lists;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.function.Consumer;
+import java.util.function.Predicate;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileStatus;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.iceberg.exceptions.RuntimeIOException;
+import org.apache.iceberg.hadoop.HiddenPathFilter;
+import org.apache.iceberg.util.Tasks;
+import org.apache.spark.api.java.JavaRDD;
+import org.apache.spark.api.java.JavaSparkContext;
+import org.apache.spark.api.java.function.FlatMapFunction;
+import org.apache.spark.broadcast.Broadcast;
+import org.apache.spark.sql.Column;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Encoders;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.SparkSession;
+import org.apache.spark.util.SerializableConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import parquet.Preconditions;
+
+public class RemoveOrphanFilesAction implements Action<List<String>> {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(RemoveOrphanFilesAction.class);
+
+  private final SparkSession spark;
+  private final JavaSparkContext sparkContext;
+  private final SerializableConfiguration hadoopConf;
+  private final int partitionDiscoveryParallelism;
+  private final Table table;
+  private final TableOperations ops;
+
+  private String location = null;
+  private Long olderThanTimestamp = null;
+  private Consumer<String> deleteFunc = new Consumer<String>() {
+    @Override
+    public void accept(String file) {
+      table.io().deleteFile(file);
+    }
+  };
+
+  RemoveOrphanFilesAction(SparkSession spark, Table table) {
+    this.spark = spark;
+    this.sparkContext = new JavaSparkContext(spark.sparkContext());
+    this.hadoopConf = new 
SerializableConfiguration(spark.sessionState().newHadoopConf());
+    this.partitionDiscoveryParallelism = 
spark.sessionState().conf().parallelPartitionDiscoveryParallelism();
+    this.table = table;
+    this.ops = ((HasTableOperations) table).operations();
+    this.location = table.location();
+  }
+
+  public RemoveOrphanFilesAction location(String newLocation) {
+    this.location = newLocation;
+    return this;
+  }
+
+  public RemoveOrphanFilesAction olderThan(long newOlderThanTimestamp) {
+    this.olderThanTimestamp = newOlderThanTimestamp;
+    return this;
+  }
+
+  public RemoveOrphanFilesAction deleteWith(Consumer<String> newDeleteFunc) {
+    this.deleteFunc = newDeleteFunc;
+    return this;
+  }
+
+  @Override
+  public List<String> execute() {
+    Preconditions.checkArgument(olderThanTimestamp != null, 
"olderThanTimestamp must be set");
+
+    Dataset<Row> validDataFileDF = buildValidDataFileDF();
+    Dataset<Row> validMetadataFileDF = buildValidMetadataFileDF();
+    Dataset<Row> validFileDF = validDataFileDF.union(validMetadataFileDF);
+    Dataset<Row> actualFileDF = buildActualFileDF();
+
+    Column joinCond = 
validFileDF.col("file_path").equalTo(actualFileDF.col("file_path"));
+    List<String> orphanFiles = actualFileDF.join(validFileDF, joinCond, 
"leftanti")
+        .as(Encoders.STRING())
+        .collectAsList();
+
+    Tasks.foreach(orphanFiles)
+        .noRetry()
+        .suppressFailureWhenFinished()
+        .onFailure((file, exc) -> LOG.warn("Failed to delete file: {}", file, 
exc))
+        .run(deleteFunc::accept);
+
+    return orphanFiles;
+  }
+
+  private Dataset<Row> buildValidDataFileDF() {
+    String allDataFilesMetadataTable = 
metadataTableName(MetadataTableType.ALL_DATA_FILES);
+    return spark.read().format("iceberg")
+        .load(allDataFilesMetadataTable)
+        .select("file_path");
+  }
+
+  private Dataset<Row> buildValidMetadataFileDF() {
+    String allManifestsMetadataTable = 
metadataTableName(MetadataTableType.ALL_MANIFESTS);
+    Dataset<Row> manifestDF = spark.read().format("iceberg")
+        .load(allManifestsMetadataTable)
+        .selectExpr("path as file_path");
+
+    List<String> otherMetadataFiles = Lists.newArrayList();
+
+    for (Snapshot snapshot : table.snapshots()) {
+      String manifestListLocation = snapshot.manifestListLocation();
+      if (manifestListLocation != null) {
+        otherMetadataFiles.add(manifestListLocation);
+      }
+    }
+
+    otherMetadataFiles.add(ops.metadataFileLocation("version-hint.text"));
+
+    TableMetadata metadata = ops.current();
+    otherMetadataFiles.add(metadata.file().location());
+    for (TableMetadata.MetadataLogEntry previousMetadataFile : 
metadata.previousFiles()) {
+      otherMetadataFiles.add(previousMetadataFile.file());
+    }
+
+    Dataset<Row> otherMetadataFileDF = spark
+        .createDataset(otherMetadataFiles, Encoders.STRING())
+        .toDF("file_path");
+
+    return manifestDF.union(otherMetadataFileDF);
+  }
+
+  private Dataset<Row> buildActualFileDF() {
+    List<String> topLevelDirs = Lists.newArrayList();
+    List<String> matchingTopLevelFiles = Lists.newArrayList();
+
+    try {
+      Path path = new Path(location);
 
 Review comment:
   There is one more problem: the initial number of locations might be pretty 
small. It seems beneficial to list, for example, 3 levels on the driver and 
then parallelize. The number of top-level partitions might be small. At the 
same time, we should avoid listing too much on the driver if the data is 
written to the root table location. That's why I modified the listing logic so 
that we list 3 levels by default by don't list locations that have more than 10 
sub-locations. The latter ones will be listed in a distributed manner. This 
should cover cases with a lot of top-level and leaf partitions.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

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

Reply via email to