kinolaev commented on code in PR #15727: URL: https://github.com/apache/iceberg/pull/15727#discussion_r4014489369
########## core/src/main/java/org/apache/iceberg/actions/RemoveDanglingDeleteFilesAction.java: ########## @@ -0,0 +1,192 @@ +/* + * 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.io.IOException; +import java.io.Serializable; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.BiFunction; +import org.apache.iceberg.DeleteFile; +import org.apache.iceberg.FileScanTask; +import org.apache.iceberg.ManifestFile; +import org.apache.iceberg.ManifestFiles; +import org.apache.iceberg.ManifestReader; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.RewriteFiles; +import org.apache.iceberg.Snapshot; +import org.apache.iceberg.SnapshotRef; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableScan; +import org.apache.iceberg.exceptions.RuntimeIOException; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Sets; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RemoveDanglingDeleteFilesAction + extends BaseSnapshotUpdateAction<RemoveDanglingDeleteFiles, RemoveDanglingDeleteFiles.Result> + implements RemoveDanglingDeleteFiles { + + private static final Logger LOG = LoggerFactory.getLogger(RemoveDanglingDeleteFilesAction.class); + private static final List<String> DELETE_COLUMNS = + ImmutableList.of( + "content", + "file_path", + "file_format", + "partition", + "record_count", + "file_size_in_bytes", + "key_metadata", + "split_offsets", + "equality_ids", + "sort_order_id", + "first_row_id", + "referenced_data_file", + "content_offset", + "content_size_in_bytes"); + private static final Result EMPTY_RESULT = + ImmutableRemoveDanglingDeleteFiles.Result.builder() + .removedDeleteFiles(ImmutableList.of()) + .build(); + + private final Table table; + private final BiFunction<Long, List<ManifestFile>, Collection<DeleteFile>> findDanglingDeletes; + private String branch = SnapshotRef.MAIN_BRANCH; + + public RemoveDanglingDeleteFilesAction(Table table) { + this.table = table; + this.findDanglingDeletes = this::findDanglingDeletes; + } + + public RemoveDanglingDeleteFilesAction( + Table table, + BiFunction<Long, List<ManifestFile>, Collection<DeleteFile>> findDanglingDeletes) { + this.table = table; + this.findDanglingDeletes = findDanglingDeletes; + } + + @Override + protected RemoveDanglingDeleteFilesAction self() { + return this; + } + + @Override + protected Table table() { + return table; + } + + public RemoveDanglingDeleteFilesAction toBranch(String targetBranch) { + Preconditions.checkArgument(targetBranch != null, "Invalid branch name: null"); + this.branch = targetBranch; + return this; + } + + @Override + public Result execute() { + Snapshot snapshot = table.snapshot(branch); + if (snapshot == null) { + Preconditions.checkArgument( + branch.equals(SnapshotRef.MAIN_BRANCH), + "Cannot remove dangling delete files from branch %s: branch does not exist", + branch); + return EMPTY_RESULT; + } + + List<ManifestFile> deleteManifests = snapshot.deleteManifests(table.io()); + if (deleteManifests.isEmpty()) { + return EMPTY_RESULT; + } + + Collection<DeleteFile> danglingDeletes = + findDanglingDeletes.apply(snapshot.snapshotId(), deleteManifests); + if (danglingDeletes.isEmpty()) { + return EMPTY_RESULT; + } + + RewriteFiles rewriteFiles = table.newRewrite().validateFromSnapshot(snapshot.snapshotId()); + for (DeleteFile deleteFile : danglingDeletes) { + LOG.debug("Removing dangling delete file {}", deleteFile.location()); + rewriteFiles.deleteFile(deleteFile); + } + + commit(rewriteFiles.toBranch(branch)); + + return ImmutableRemoveDanglingDeleteFiles.Result.builder() + .removedDeleteFiles(danglingDeletes) + .build(); + } + + /** + * Dangling delete files can be identified with following steps + * + * <ol> + * <li>Make a full scan and collect delete files from all file tasks. + * <li>Collect all delete file entries skipping files from the previous step. + * </ol> + */ + private List<DeleteFile> findDanglingDeletes( + long snapshotId, List<ManifestFile> deleteManifests) { + Set<DeleteFileKey> referencedKeys = Sets.newHashSet(); + TableScan scan = table.newScan().useSnapshot(snapshotId); + try (CloseableIterable<FileScanTask> tasks = scan.planFiles()) { + for (FileScanTask task : tasks) { + for (DeleteFile deleteFile : task.deletes()) { + referencedKeys.add(new DeleteFileKey(deleteFile)); + } + } + } catch (IOException e) { + throw new RuntimeIOException(e, "Failed to scan: %s", scan); + } + + List<DeleteFile> danglingDeletes = Lists.newArrayList(); + for (ManifestFile manifest : deleteManifests) { + try (ManifestReader<DeleteFile> reader = + readDeleteManifest(manifest, table.io(), table.specs())) { + for (DeleteFile deleteFile : reader) { + if (!referencedKeys.contains(new DeleteFileKey(deleteFile))) { + danglingDeletes.add(deleteFile); + } + } + } catch (IOException e) { + throw new RuntimeIOException(e, "Failed to read manifest: %s", manifest); + } + } + + return danglingDeletes; + } + + public static ManifestReader<DeleteFile> readDeleteManifest( + ManifestFile manifest, FileIO io, Map<Integer, PartitionSpec> specsById) { + return ManifestFiles.readDeleteManifest(manifest, io, specsById).select(DELETE_COLUMNS); + } + + public record DeleteFileKey(String location, Long contentOffset, Long contentSizeInBytes) Review Comment: Hi @manuzhang, thanks for catching this! Could you provide a few more details on how to reproduce the issue? Also, if you already have an idea of what Kryo doesn't support, that would be helpful - I haven't used Kryo before. upd: I found out that Kryo 4 that is used in Spark doesn't support records. Is that what's causing the problem? -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
