stevenzwu commented on code in PR #19129: URL: https://github.com/apache/iceberg/pull/19129#discussion_r4815244265
########## core/src/main/java/org/apache/iceberg/ManifestListProducer.java: ########## @@ -0,0 +1,704 @@ +/* + * 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 static org.apache.iceberg.TableProperties.MANIFEST_MIN_MERGE_COUNT; +import static org.apache.iceberg.TableProperties.MANIFEST_MIN_MERGE_COUNT_DEFAULT; +import static org.apache.iceberg.TableProperties.MANIFEST_TARGET_SIZE_BYTES; +import static org.apache.iceberg.TableProperties.MANIFEST_TARGET_SIZE_BYTES_DEFAULT; +import static org.apache.iceberg.TableProperties.SNAPSHOT_ID_INHERITANCE_ENABLED; +import static org.apache.iceberg.TableProperties.SNAPSHOT_ID_INHERITANCE_ENABLED_DEFAULT; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Consumer; +import java.util.stream.Collectors; +import org.apache.iceberg.encryption.EncryptedOutputFile; +import org.apache.iceberg.expressions.Expression; +import org.apache.iceberg.io.InputFile; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.base.Predicate; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.Iterables; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.relocated.com.google.common.collect.Sets; +import org.apache.iceberg.relocated.com.google.common.collect.Streams; +import org.apache.iceberg.util.ContentFileUtil; +import org.apache.iceberg.util.DataFileSet; +import org.apache.iceberg.util.DeleteFileSet; +import org.apache.iceberg.util.SnapshotUtil; +import org.apache.iceberg.util.ThreadPools; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +class ManifestListProducer implements ProducerActions { + private static final Logger LOG = LoggerFactory.getLogger(ManifestListProducer.class); + + private final String tableName; + private final TableOperations ops; + private final long snapshotId; + private final int formatVersion; + private final boolean canInheritSnapshotId; + private final ManifestWriteManager writeManager; + private final ManifestMergeManager<DataFile> mergeManager; + private final ManifestFilterManager<DataFile> filterManager; + private final ManifestMergeManager<DeleteFile> deleteMergeManager; + private final ManifestFilterManager<DeleteFile> deleteFilterManager; + private final AtomicInteger dvMergeAttempt = new AtomicInteger(0); + private Consumer<String> deleteFunc; + private ExecutorService workerPool = ThreadPools.getWorkerPool(); + + // update data + private final Map<Integer, DataFileSet> newDataFilesBySpec = Maps.newHashMap(); + private final List<DeleteFile> v2Deletes = Lists.newArrayList(); + private final Map<String, List<DeleteFile>> dvsByReferencedFile = Maps.newLinkedHashMap(); + private final List<ManifestFile> appendManifests = Lists.newArrayList(); + private final List<ManifestFile> rewrittenAppendManifests = Lists.newArrayList(); + + // cache new data manifests after writing + private final List<ManifestFile> cachedNewDataManifests = Lists.newLinkedList(); + private boolean hasNewDataFiles = false; + + // cache new manifests for delete files + private final List<ManifestFile> cachedNewDeleteManifests = Lists.newLinkedList(); + private boolean hasNewDeleteFiles = false; + + // operation results, set by apply + private List<DeleteFile> addedDeleteFiles = Lists.newArrayList(); + private int duplicateDeleteCount = 0; + private List<DataFile> deletedDataFiles = null; + private List<DataFile> missingDataFiles = null; + private List<String> missingDataLocations = null; + private List<DeleteFile> missingDeleteFiles = null; + private List<DeleteFile> deletedDeleteFiles = null; + + ManifestListProducer(String tableName, TableOperations ops, long snapshotId, int formatVersion) { + this.tableName = tableName; + this.ops = ops; + this.deleteFunc = ops.io()::deleteFile; Review Comment: nit: `ops.io()::deleteFile` still evaluates `ops.io()` here at construction. A `path -> ops.io().deleteFile(path)` lambda would call it on each delete. ########## core/src/main/java/org/apache/iceberg/MergingSnapshotProducer.java: ########## @@ -1093,41 +1022,26 @@ protected Map<String, String> summary() { return summaryBuilder.build(); } - // guard buffered deletes against concurrent format upgrade - private void validateDeleteFilesForVersion(int currentFormatVersion) { - for (DeleteFile file : v2Deletes) { - validateDeleteFileForVersion(file, currentFormatVersion); - } - } - @Override protected NewSnapshotMetadata writeRootManifest( TableMetadata base, Snapshot parent, long sequenceNumber) { - validateDeleteFilesForVersion(base.formatVersion()); - - if (addsDeleteFiles()) { - throw new UnsupportedOperationException("Cannot add new delete files in v4"); - } else if (filterManager.containsDeletes()) { - throw new UnsupportedOperationException("Cannot remove data files in v4"); - } else if (deleteFilterManager.containsDeletes()) { - throw new UnsupportedOperationException("Cannot remove delete files in v4"); - } else if (!appendManifests.isEmpty()) { - throw new UnsupportedOperationException("Cannot append manifest files in v4"); - } - - if (rootManifestProducer == null || hasNewDataFiles) { - this.rootManifestProducer = new RootManifestProducer(ops(), snapshotId()); - for (DataFile file : Iterables.concat(newDataFilesBySpec.values())) { - rootManifestProducer.appendFile(file); - } + ValidationException.check( + base.formatVersion() < 4, Review Comment: This fails every v4 commit, since `writeRootManifest` is only called for v4+ bases. The case to catch is an operation created at v3, where `rootManifestProducer` is null. Should this check `rootManifestProducer != null` instead? ########## core/src/main/java/org/apache/iceberg/MergingSnapshotProducer.java: ########## @@ -1138,74 +1046,71 @@ protected NewSnapshotMetadata writeRootManifest( } @Override - public List<ManifestFile> apply(TableMetadata base, Snapshot snapshot) { - validateDeleteFilesForVersion(base.formatVersion()); - // filter any existing manifests - List<ManifestFile> filtered = - filterManager.filterManifests( - SnapshotUtil.schemaFor(base, targetBranch()), - snapshot != null ? snapshot.dataManifests(ops().io()) : null); - long minDataSequenceNumber = - filtered.stream() - .map(ManifestFile::minSequenceNumber) - .filter( - seq -> - seq - != ManifestWriter - .UNASSIGNED_SEQ) // filter out unassigned in rewritten manifests - .reduce(base.lastSequenceNumber(), Math::min); - deleteFilterManager.dropDeleteFilesOlderThan(minDataSequenceNumber); - - // retrieve the data files to be deleted from the DataFileFilterManager and pass it to the - // DeleteFileFilterManager so that it can potentially remove orphaned DVs - Set<DataFile> filesToBeDeleted = filterManager.filesToBeDeleted(); - deleteFilterManager.removeDanglingDeletesFor(filesToBeDeleted); - - List<ManifestFile> filteredDeletes = - deleteFilterManager.filterManifests( - SnapshotUtil.schemaFor(base, targetBranch()), - snapshot != null ? snapshot.deleteManifests(ops().io()) : null); - - // only keep manifests that have live data files or that were written by this commit - Predicate<ManifestFile> shouldKeep = - manifest -> - manifest.hasAddedFiles() - || manifest.hasExistingFiles() - || manifest.snapshotId() == snapshotId(); - Iterable<ManifestFile> unmergedManifests = - Iterables.filter(Iterables.concat(prepareNewDataManifests(), filtered), shouldKeep); - Iterable<ManifestFile> unmergedDeleteManifests = - Iterables.filter(Iterables.concat(prepareDeleteManifests(), filteredDeletes), shouldKeep); - - // update the snapshot summary + public List<ManifestFile> apply(TableMetadata base, Snapshot parent) { + ValidationException.check( + base.formatVersion() < 4, + "Cannot commit to table concurrently upgraded to v%s", + base.formatVersion()); + + List<ManifestFile> manifests = manifestListProducer.apply(base, parent, targetBranch()); + + List<DataFile> deletedDataFiles = manifestListProducer.deletedDataFiles(); + List<DeleteFile> deletedDeleteFiles = manifestListProducer.deletedDeleteFiles(); + if (failAnyDelete) { Review Comment: Makes sense. Then the fail-flag handling in `ManifestFilterManager` is dead: `failAnyDelete` and `failMissingDeletePaths` are never set, so `DeleteException`, `validateRequiredDeletes`, and the now-unused `buildSummary` can be removed. -- 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]
