findepi commented on code in PR #17754:
URL: https://github.com/apache/iceberg/pull/17754#discussion_r4707153384
##########
api/src/main/java/org/apache/iceberg/RowDelta.java:
##########
@@ -41,6 +41,10 @@ public interface RowDelta extends SnapshotUpdate<RowDelta> {
/**
* Add a {@link DeleteFile} to the table.
*
+ * <p>A deletion vector added concurrently for the same data file may be
merged into this
+ * operation's deletion vector instead of failing the commit; see {@link
+ * #validateNoConflictingDeleteFiles()} for the conditions.
Review Comment:
Is this note important here? or just sloppetti? we didn't have _any_
conflict resolution comments here yet
##########
core/src/main/java/org/apache/iceberg/MergingSnapshotProducer.java:
##########
@@ -902,15 +923,43 @@ protected void validateAddedDVs(
filterManifestsByPartition(base, conflictDetectionFilter,
newDeleteManifests),
ManifestFile::hasAddedFiles);
+ List<DeleteFile> concurrentDVs =
Collections.synchronizedList(Lists.newArrayList());
+
+ // only DVs added by delete operations can be merged; a DV added by an
operation that also
+ // adds data files marks rows that were rewritten into those files, so
merging it would let
+ // rows deleted by this operation survive in the rewritten files
+ Set<Long> mergeableSnapshotIds;
+ if (canMergeConcurrentDVs) {
+ mergeableSnapshotIds =
+ newSnapshotIds.stream()
+ .filter(id ->
DataOperations.DELETE.equals(base.snapshot(id).operation()))
Review Comment:
This check is per snapshot, but one transaction can produce several. T1
creates snapshot S1 (APPEND A' with a copy of row r from A) and S2 (DELETE: DV
on A at r's position), then commits them both at once (`commitTransaction()`).
Concurrently we delete r in A. S2 counts as mergeable, so our delete lands on A
while r survives in A'.
I am not sure this can be solved today, maybe we need some changes in
transaction API so that snapshots from one transaction can be identified?
##########
core/src/main/java/org/apache/iceberg/BaseRowDelta.java:
##########
@@ -168,9 +168,16 @@ protected void validate(TableMetadata base, Snapshot
parent) {
}
validateNoConflictingFileAndPositionDeletes();
-
- validateAddedDVs(base, startingSnapshotId, conflictDetectionFilter,
parent);
}
+
+ // concurrent DVs can be merged only when this operation does not add or
remove data files and
+ // does not require concurrent delete files to fail validation
+ boolean canMergeConcurrentDVs =
+ !validateNewDeleteFiles && !addsDataFiles() && !deletesDataFiles();
Review Comment:
There is an assumption being made: "it's safe to run deletions concurrently,
but not when they add, as eg in MERGE statement"
However, what if the two competing operations are both "find duplicate
records and delete one of them"? From commit perspective, this is a pure delete
operation, but obviously they do logically conflict. When run serially, one
copy of record always stays, when concurrently and DVs merged, a record can be
gone.
##########
core/src/main/java/org/apache/iceberg/MergingSnapshotProducer.java:
##########
@@ -919,19 +968,109 @@ private void validateAddedDVs(
for (ManifestEntry<DeleteFile> entry : entries) {
DeleteFile file = entry.file();
- if (newSnapshotIds.contains(entry.snapshotId()) &&
ContentFileUtil.isDV(file)) {
+ if (newSnapshotIds.contains(entry.snapshotId())
+ && ContentFileUtil.isDV(file)
+ && dvsByReferencedFile.containsKey(file.referencedDataFile())) {
ValidationException.check(
- !dvsByReferencedFile.containsKey(file.referencedDataFile()),
+ mergeableSnapshotIds.contains(entry.snapshotId()),
"Found concurrently added DV for %s: %s",
file.referencedDataFile(),
ContentFileUtil.dvDesc(file));
+ concurrentDVs.add(file.copy());
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
+ /**
+ * Merges concurrently added DVs into this operation's DVs.
Review Comment:
Javadoc on private members (also `undoConcurrentDVMerge`,
`MergedConcurrentDV`, and package-private `ManifestFilterManager.dropDelete`)
describes internals. Drop it, or keep only a caller obligation if there is one.
##########
core/src/test/java/org/apache/iceberg/TestRowDelta.java:
##########
@@ -1957,7 +1958,356 @@ public void testConcurrentMergeRewriteSameRemoveRows() {
}
@TestTemplate
- public void testConcurrentDVsForSameDataFile() {
+ public void testConcurrentDVsForSameDataFileAreMerged() throws IOException {
Review Comment:
New test methods: package-private and without the `test` prefix, please
(applies to all added tests).
for this one, i would simply keep the old name
##########
core/src/main/java/org/apache/iceberg/MergingSnapshotProducer.java:
##########
@@ -871,8 +874,26 @@ protected void validateAddedDVs(
Long startingSnapshotId,
Expression conflictDetectionFilter,
Snapshot parent) {
Review Comment:
This overload has no callers left (`BaseRowDelta` was the only one) and the
class is package-private, so there is no compatibility reason to keep it.
Remove it?
##########
core/src/main/java/org/apache/iceberg/MergingSnapshotProducer.java:
##########
@@ -871,8 +874,26 @@ protected void validateAddedDVs(
Long startingSnapshotId,
Expression conflictDetectionFilter,
Snapshot parent) {
- // skip if there is no current table state or this operation doesn't add
new DVs
- if (parent == null || dvsByReferencedFile.isEmpty()) {
+ validateAddedDVs(base, startingSnapshotId, conflictDetectionFilter,
parent, false);
+ }
+
+ // validates there are no concurrently added DVs for referenced data files;
when
Review Comment:
This comment, and the ones at L894, L1368 and in `BaseRowDelta` (L178),
describe retry/undo mechanics that would need rewriting in any
behavior-preserving refactor. Trim them to what the code does not say, or drop
them.
##########
core/src/test/java/org/apache/iceberg/TestRowDelta.java:
##########
@@ -1957,7 +1958,356 @@ public void testConcurrentMergeRewriteSameRemoveRows() {
}
@TestTemplate
- public void testConcurrentDVsForSameDataFile() {
+ public void testConcurrentDVsForSameDataFileAreMerged() throws IOException {
+ assumeThat(formatVersion).isGreaterThanOrEqualTo(3);
+
+ DataFile dataFile = newDataFile("data_bucket=0");
+ commit(table, table.newRowDelta().addRows(dataFile), branch);
+
+ OutputFileFactory fileFactory =
+ OutputFileFactory.builderFor(table, 1,
1).format(FileFormat.PUFFIN).build();
+
+ DeleteFile deleteFile1 = dvWithPositions(dataFile, fileFactory, 0, 2);
+ RowDelta rowDelta1 = table.newRowDelta().addDeletes(deleteFile1);
+
+ DeleteFile deleteFile2 = dvWithPositions(dataFile, fileFactory, 2, 4);
+ RowDelta rowDelta2 = table.newRowDelta().addDeletes(deleteFile2);
+
+ commit(table, rowDelta1, branch);
+ commit(table, rowDelta2, branch);
+
+ SnapshotChanges changes =
+ SnapshotChanges.builderFor(table).snapshot(latestSnapshot(table,
branch)).build();
+
+ // the concurrent DV must be replaced with a DV that merges both sets of
positions
+ DeleteFile mergedDV = Iterables.getOnlyElement(changes.addedDeleteFiles());
+ assertDVHasDeletedPositions(mergedDV, LongStream.range(0,
4).boxed()::iterator);
+ assertThat(changes.removedDeleteFiles())
+ .extracting(ContentFile::location)
+ .containsExactly(deleteFile1.location());
+ }
+
+ @TestTemplate
+ public void testConcurrentDVsMergedWhenReplacingExistingDV() throws
IOException {
+ assumeThat(formatVersion).isGreaterThanOrEqualTo(3);
+
+ DataFile dataFile = newDataFile("data_bucket=0");
+ commit(table, table.newRowDelta().addRows(dataFile), branch);
+
+ OutputFileFactory fileFactory =
+ OutputFileFactory.builderFor(table, 1,
1).format(FileFormat.PUFFIN).build();
+
+ DeleteFile existingDV = dvWithPositions(dataFile, fileFactory, 0, 2);
+ commit(table, table.newRowDelta().addDeletes(existingDV), branch);
+
+ Snapshot base = latestSnapshot(table, branch);
+
+ // both writers merge the existing DV into their new DV and replace it
+ DeleteFile deleteFile1 = dvWithPositions(dataFile, fileFactory, 0, 4);
+ RowDelta rowDelta1 =
+ table
+ .newRowDelta()
+ .addDeletes(deleteFile1)
+ .removeDeletes(existingDV)
+ .validateFromSnapshot(base.snapshotId())
+ .validateDataFilesExist(ImmutableList.of(dataFile.location()))
+ .validateDeletedFiles();
+
+ List<PositionDelete<?>> deletes = Lists.newArrayList();
+ for (long pos : new long[] {0, 1, 4, 5}) {
+ deletes.add(PositionDelete.create().set(dataFile.location(), pos));
+ }
+ DeleteFile deleteFile2 =
+ writeDV(table, deletes, dataFile.specId(), dataFile.partition(),
fileFactory);
+ RowDelta rowDelta2 =
+ table
+ .newRowDelta()
+ .addDeletes(deleteFile2)
+ .removeDeletes(existingDV)
+ .validateFromSnapshot(base.snapshotId())
+ .validateDataFilesExist(ImmutableList.of(dataFile.location()))
+ .validateDeletedFiles();
+
+ commit(table, rowDelta1, branch);
+ commit(table, rowDelta2, branch);
+
+ SnapshotChanges changes =
+ SnapshotChanges.builderFor(table).snapshot(latestSnapshot(table,
branch)).build();
+
+ // the stale removal of the existing DV must be dropped and the concurrent
DV replaced
+ DeleteFile mergedDV = Iterables.getOnlyElement(changes.addedDeleteFiles());
+ assertDVHasDeletedPositions(mergedDV, LongStream.range(0,
6).boxed()::iterator);
+ assertThat(changes.removedDeleteFiles())
+ .extracting(ContentFile::location)
+ .containsExactly(deleteFile1.location());
+ }
+
+ @TestTemplate
+ public void testConcurrentDVMergeWithCommitRetry() throws IOException {
+ assumeThat(formatVersion).isGreaterThanOrEqualTo(3);
+
+ DataFile dataFile = newDataFile("data_bucket=0");
+ commit(table, table.newRowDelta().addRows(dataFile), branch);
+
+ OutputFileFactory fileFactory =
+ OutputFileFactory.builderFor(table, 1,
1).format(FileFormat.PUFFIN).build();
+
+ DeleteFile deleteFile1 = dvWithPositions(dataFile, fileFactory, 0, 2);
+ RowDelta rowDelta1 = table.newRowDelta().addDeletes(deleteFile1);
+
+ DeleteFile deleteFile2 = dvWithPositions(dataFile, fileFactory, 2, 4);
+ RowDelta rowDelta2 = table.newRowDelta().addDeletes(deleteFile2);
+
+ commit(table, rowDelta1, branch);
+
+ // the merge must be idempotent across commit attempts
+ table.ops().failCommits(1);
+ commit(table, rowDelta2, branch);
+
+ SnapshotChanges changes =
+ SnapshotChanges.builderFor(table).snapshot(latestSnapshot(table,
branch)).build();
+
+ DeleteFile mergedDV = Iterables.getOnlyElement(changes.addedDeleteFiles());
+ assertDVHasDeletedPositions(mergedDV, LongStream.range(0,
4).boxed()::iterator);
+ assertThat(changes.removedDeleteFiles())
+ .extracting(ContentFile::location)
+ .containsExactly(deleteFile1.location());
+ }
+
+ @TestTemplate
+ public void testConcurrentDVsForMultipleDataFilesAreMerged() throws
IOException {
+ assumeThat(formatVersion).isGreaterThanOrEqualTo(3);
+
+ DataFile dataFileA = newDataFile("data_bucket=0");
+ DataFile dataFileB = newDataFile("data_bucket=1");
+ commit(table, table.newRowDelta().addRows(dataFileA).addRows(dataFileB),
branch);
+
+ OutputFileFactory fileFactory =
+ OutputFileFactory.builderFor(table, 1,
1).format(FileFormat.PUFFIN).build();
+
+ DeleteFile dvA1 = dvWithPositions(dataFileA, fileFactory, 0, 2);
+ DeleteFile dvB1 = dvWithPositions(dataFileB, fileFactory, 0, 3);
+ RowDelta rowDelta1 = table.newRowDelta().addDeletes(dvA1).addDeletes(dvB1);
+
+ DeleteFile dvA2 = dvWithPositions(dataFileA, fileFactory, 2, 4);
+ DeleteFile dvB2 = dvWithPositions(dataFileB, fileFactory, 3, 6);
+ RowDelta rowDelta2 = table.newRowDelta().addDeletes(dvA2).addDeletes(dvB2);
+
+ commit(table, rowDelta1, branch);
+ commit(table, rowDelta2, branch);
+
+ SnapshotChanges changes =
+ SnapshotChanges.builderFor(table).snapshot(latestSnapshot(table,
branch)).build();
+
+ assertThat(changes.addedDeleteFiles()).hasSize(2);
+ for (DeleteFile mergedDV : changes.addedDeleteFiles()) {
+ if (mergedDV.referencedDataFile().equals(dataFileA.location())) {
+ assertDVHasDeletedPositions(mergedDV, LongStream.range(0,
4).boxed()::iterator);
+ } else {
+
assertThat(mergedDV.referencedDataFile()).isEqualTo(dataFileB.location());
+ assertDVHasDeletedPositions(mergedDV, LongStream.range(0,
6).boxed()::iterator);
+ }
+ }
+
+ assertThat(changes.removedDeleteFiles())
+ .extracting(ContentFile::location)
+ .containsExactlyInAnyOrder(dvA1.location(), dvB1.location());
+ }
+
+ @TestTemplate
+ public void testConcurrentDVReplacedBetweenCommitAttempts() throws
IOException {
+ assumeThat(formatVersion).isGreaterThanOrEqualTo(3);
+
+ DataFile dataFile = newDataFile("data_bucket=0");
+ commit(table, table.newRowDelta().addRows(dataFile), branch);
+
+ OutputFileFactory fileFactory =
+ OutputFileFactory.builderFor(table, 1,
1).format(FileFormat.PUFFIN).build();
+
+ DeleteFile deleteFile1 = dvWithPositions(dataFile, fileFactory, 0, 2);
+ commit(table, table.newRowDelta().addDeletes(deleteFile1), branch);
+
+ DeleteFile deleteFile2 = dvWithPositions(dataFile, fileFactory, 2, 4);
+ RowDelta rowDelta = table.newRowDelta().addDeletes(deleteFile2);
+
+ // the first attempt merges the first concurrent DV
+ apply(rowDelta, branch);
+
+ // a newer concurrent commit replaces the first DV before the next attempt
+ DeleteFile deleteFile3 = dvWithPositions(dataFile, fileFactory, 0, 3);
+ commit(
+ table,
+ table
+ .newRowDelta()
+ .addDeletes(deleteFile3)
+ .removeDeletes(deleteFile1)
+ .validateFromSnapshot(latestSnapshot(table, branch).snapshotId()),
+ branch);
+
+ Snapshot finalSnapshot = commit(table, rowDelta, branch);
+
+ SnapshotChanges changes =
SnapshotChanges.builderFor(table).snapshot(finalSnapshot).build();
+ DeleteFile mergedDV = Iterables.getOnlyElement(changes.addedDeleteFiles());
+ assertDVHasDeletedPositions(mergedDV, LongStream.range(0,
4).boxed()::iterator);
+ assertThat(changes.removedDeleteFiles())
+ .extracting(ContentFile::location)
+ .containsExactly(deleteFile3.location());
+
+ // the merged DV file written by the first attempt must be deleted
+ List<String> mergedDVFiles = mergedDVFileNames();
+ assertThat(mergedDVFiles).hasSize(1);
+
assertThat(mergedDV.location()).endsWith(Iterables.getOnlyElement(mergedDVFiles));
+ }
+
+ @TestTemplate
+ public void testConcurrentDVRolledBackBetweenCommitAttempts() throws
IOException {
+ assumeThat(formatVersion).isGreaterThanOrEqualTo(3);
+
+ DataFile dataFile = newDataFile("data_bucket=0");
+ commit(table, table.newRowDelta().addRows(dataFile), branch);
+
+ long snapshotBeforeConcurrentDV = latestSnapshot(table,
branch).snapshotId();
+
+ OutputFileFactory fileFactory =
+ OutputFileFactory.builderFor(table, 1,
1).format(FileFormat.PUFFIN).build();
+
+ DeleteFile deleteFile1 = dvWithPositions(dataFile, fileFactory, 0, 2);
+ commit(table, table.newRowDelta().addDeletes(deleteFile1), branch);
+
+ DeleteFile deleteFile2 = dvWithPositions(dataFile, fileFactory, 2, 4);
+ RowDelta rowDelta = table.newRowDelta().addDeletes(deleteFile2);
+
+ // the first attempt merges the concurrent DV
+ apply(rowDelta, branch);
+
+ // the concurrent DV is rolled back before the next attempt
+ table.manageSnapshots().replaceBranch(branch,
snapshotBeforeConcurrentDV).commit();
+
+ Snapshot finalSnapshot = commit(table, rowDelta, branch);
+
+ SnapshotChanges changes =
SnapshotChanges.builderFor(table).snapshot(finalSnapshot).build();
+ DeleteFile dv = Iterables.getOnlyElement(changes.addedDeleteFiles());
+ PositionDeleteIndex index = DVUtil.readDV(dv, table.io());
+ assertThat(index.isDeleted(0L)).as("position 0 was rolled back").isFalse();
+ assertThat(index.isDeleted(1L)).as("position 1 was rolled back").isFalse();
+ assertThat(index.isDeleted(2L)).isTrue();
+ assertThat(index.isDeleted(3L)).isTrue();
+ }
+
+ @TestTemplate
+ public void testConcurrentDVRolledBackRestoresPendingDVRemoval() throws
IOException {
+ assumeThat(formatVersion).isGreaterThanOrEqualTo(3);
+
+ DataFile dataFile = newDataFile("data_bucket=0");
+ commit(table, table.newRowDelta().addRows(dataFile), branch);
+
+ OutputFileFactory fileFactory =
+ OutputFileFactory.builderFor(table, 1,
1).format(FileFormat.PUFFIN).build();
+
+ DeleteFile existingDV = dvWithPositions(dataFile, fileFactory, 0, 2);
+ commit(table, table.newRowDelta().addDeletes(existingDV), branch);
+
+ long snapshotBeforeConcurrentDV = latestSnapshot(table,
branch).snapshotId();
+
+ DeleteFile newDV = dvWithPositions(dataFile, fileFactory, 0, 4);
+ RowDelta rowDelta =
+ table
+ .newRowDelta()
+ .addDeletes(newDV)
+ .removeDeletes(existingDV)
+ .validateDeletedFiles()
+ .validateFromSnapshot(snapshotBeforeConcurrentDV);
+
+ DeleteFile concurrentDV = dvWithPositions(dataFile, fileFactory, 0, 6);
+ commit(table,
table.newRowDelta().addDeletes(concurrentDV).removeDeletes(existingDV), branch);
+
+ // the first attempt merges the concurrent DV and drops the removal it
already performed
+ apply(rowDelta, branch);
+
+ // the concurrent DV is rolled back before the next attempt, restoring the
existing DV
+ table.manageSnapshots().replaceBranch(branch,
snapshotBeforeConcurrentDV).commit();
+
+ Snapshot finalSnapshot = commit(table, rowDelta, branch);
+
+ SnapshotChanges changes =
SnapshotChanges.builderFor(table).snapshot(finalSnapshot).build();
+ DeleteFile dv = Iterables.getOnlyElement(changes.addedDeleteFiles());
+ PositionDeleteIndex index = DVUtil.readDV(dv, table.io());
+ assertDVHasDeletedPositions(dv, LongStream.range(0, 4).boxed()::iterator);
+ assertThat(index.isDeleted(4L)).as("position 4 was rolled back").isFalse();
+ assertThat(index.isDeleted(5L)).as("position 5 was rolled back").isFalse();
+ assertThat(changes.removedDeleteFiles())
+ .extracting(ContentFile::location)
+ .containsExactly(existingDV.location());
+ }
+
+ @TestTemplate
+ public void testConcurrentDVMergeUndoneWhenBranchRemoved() throws
IOException {
+ assumeThat(formatVersion).isGreaterThanOrEqualTo(3);
+ assumeThat(branch).isNotEqualTo(SnapshotRef.MAIN_BRANCH);
+
+ DataFile dataFile = newDataFile("data_bucket=0");
+ commit(table, table.newRowDelta().addRows(dataFile), branch);
+
+ OutputFileFactory fileFactory =
+ OutputFileFactory.builderFor(table, 1,
1).format(FileFormat.PUFFIN).build();
+
+ DeleteFile deleteFile1 = dvWithPositions(dataFile, fileFactory, 0, 2);
+ commit(table, table.newRowDelta().addDeletes(deleteFile1), branch);
+
+ DeleteFile deleteFile2 = dvWithPositions(dataFile, fileFactory, 2, 4);
+ RowDelta rowDelta = table.newRowDelta().addDeletes(deleteFile2);
+
+ // the first attempt merges the concurrent DV
+ apply(rowDelta, branch);
+
+ table.manageSnapshots().removeBranch(branch).commit();
+
+ Snapshot finalSnapshot = commit(table, rowDelta, branch);
+
+ SnapshotChanges changes =
SnapshotChanges.builderFor(table).snapshot(finalSnapshot).build();
+ DeleteFile dv = Iterables.getOnlyElement(changes.addedDeleteFiles());
+ PositionDeleteIndex index = DVUtil.readDV(dv, table.io());
+ assertThat(index.isDeleted(0L)).as("position 0 is no longer in the
table").isFalse();
+ assertThat(index.isDeleted(1L)).as("position 1 is no longer in the
table").isFalse();
+ assertThat(index.isDeleted(2L)).isTrue();
+ assertThat(index.isDeleted(3L)).isTrue();
+ }
+
+ @TestTemplate
+ public void testConcurrentDVMergeCleanupOnCommitFailure() throws IOException
{
+ assumeThat(formatVersion).isGreaterThanOrEqualTo(3);
+
+ DataFile dataFile = newDataFile("data_bucket=0");
+ commit(table, table.newRowDelta().addRows(dataFile), branch);
+
+ OutputFileFactory fileFactory =
+ OutputFileFactory.builderFor(table, 1,
1).format(FileFormat.PUFFIN).build();
+
+ DeleteFile deleteFile1 = dvWithPositions(dataFile, fileFactory, 0, 2);
+ commit(table, table.newRowDelta().addDeletes(deleteFile1), branch);
+
+ DeleteFile deleteFile2 = dvWithPositions(dataFile, fileFactory, 2, 4);
+ RowDelta rowDelta = table.newRowDelta().addDeletes(deleteFile2);
+
+ table.ops().failCommits(5);
+ assertThatThrownBy(() -> commit(table, rowDelta, branch))
+ .isInstanceOf(CommitFailedException.class)
+ .hasMessage("Injected failure");
+
+ assertThat(mergedDVFileNames()).isEmpty();
+ }
+
+ private List<String> mergedDVFileNames() throws IOException {
+ try (Stream<java.nio.file.Path> files =
java.nio.file.Files.walk(tableDir.toPath())) {
Review Comment:
Import `java.nio.file.Path` / `java.nio.file.Files` instead of using fully
qualified names.
##########
core/src/main/java/org/apache/iceberg/BaseRowDelta.java:
##########
@@ -168,9 +168,16 @@ protected void validate(TableMetadata base, Snapshot
parent) {
}
validateNoConflictingFileAndPositionDeletes();
-
- validateAddedDVs(base, startingSnapshotId, conflictDetectionFilter,
parent);
}
+
+ // concurrent DVs can be merged only when this operation does not add or
remove data files and
+ // does not require concurrent delete files to fail validation
+ boolean canMergeConcurrentDVs =
Review Comment:
This turns a commit failure into a silent merge for every delete-only
RowDelta, with no way to opt out. New behavior should default to off; can it be
gated behind an explicit option?
--
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]