Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 545
Add concurrency safety validation checks#3049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -22,14 +22,17 @@ | ||
| from pyiceberg.exceptions import ValidationException | ||
| from pyiceberg.io import FileIO | ||
| from pyiceberg.manifest import ManifestContent, ManifestEntry, ManifestEntryStatus, ManifestFile | ||
| from pyiceberg.manifest import DataFile, DataFileContent, ManifestContent, ManifestEntry, ManifestEntryStatus, ManifestFile | ||
| from pyiceberg.table import Table | ||
| from pyiceberg.table.snapshots import Operation, Snapshot, Summary | ||
| from pyiceberg.table.update.validate import ( | ||
| _added_data_files, | ||
| _added_delete_files, | ||
| _deleted_data_files, | ||
| _validate_added_data_files, | ||
| _validate_deleted_data_files, | ||
| _validate_no_new_delete_files, | ||
| _validate_no_new_deletes_for_data_files, | ||
| _validation_history, | ||
| ) | ||
| @@ -350,3 +353,153 @@ class DummyEntry: | ||
| data_filter=None, | ||
| parent_snapshot=oldest_snapshot, | ||
| ) | ||
| @pytest.mark.parametrize("operation", [Operation.APPEND, Operation.REPLACE]) | ||
| def test_added_delete_files_non_conflicting_count( | ||
| table_v2_with_extensive_snapshots_and_manifests: tuple[Table, dict[int, list[ManifestFile]]], | ||
| operation: Operation, | ||
| ) -> None: | ||
| table, mock_manifests = table_v2_with_extensive_snapshots_and_manifests | ||
| snapshot_history = 100 | ||
| snapshots = table.snapshots() | ||
| for i in range(1, snapshot_history + 1): | ||
| altered_snapshot = snapshots[-i] | ||
| altered_snapshot = altered_snapshot.model_copy(update={"summary": Summary(operation=operation)}) | ||
| snapshots[-i] = altered_snapshot | ||
| table.metadata = table.metadata.model_copy( | ||
| update={"snapshots": snapshots}, | ||
| ) | ||
| oldest_snapshot = table.snapshots()[-snapshot_history] | ||
| newest_snapshot = cast(Snapshot, table.current_snapshot()) | ||
| def mock_read_manifest_side_effect(self: Snapshot, io: FileIO) -> list[ManifestFile]: | ||
| """Mock the manifests method to use the snapshot_id for lookup.""" | ||
| snapshot_id = self.snapshot_id | ||
| if snapshot_id in mock_manifests: | ||
| return mock_manifests[snapshot_id] | ||
| return [] | ||
| def mock_fetch_manifest_entry(self: ManifestFile, io: FileIO, discard_deleted: bool = True) -> list[ManifestEntry]: | ||
| return [ | ||
| ManifestEntry.from_args( | ||
| status=ManifestEntryStatus.ADDED, snapshot_id=self.added_snapshot_id, sequence_number=self.sequence_number | ||
| ) | ||
| ] | ||
| with ( | ||
| patch("pyiceberg.table.snapshots.Snapshot.manifests", new=mock_read_manifest_side_effect), | ||
| patch("pyiceberg.manifest.ManifestFile.fetch_manifest_entry", new=mock_fetch_manifest_entry), | ||
| ): | ||
| dfi = _added_delete_files( | ||
| table=table, | ||
| starting_snapshot=newest_snapshot, | ||
| data_filter=None, | ||
| parent_snapshot=oldest_snapshot, | ||
| partition_set=None, | ||
| ) | ||
| assert dfi.is_empty() | ||
| assert len(dfi.referenced_delete_files()) == 0 | ||
| @pytest.mark.parametrize("operation", [Operation.DELETE, Operation.OVERWRITE]) | ||
| def test_added_delete_files_conflicting_count( | ||
| table_v2_with_extensive_snapshots_and_manifests: tuple[Table, dict[int, list[ManifestFile]]], | ||
| operation: Operation, | ||
| ) -> None: | ||
| table, mock_manifests = table_v2_with_extensive_snapshots_and_manifests | ||
| snapshot_history = 100 | ||
| snapshots = table.snapshots() | ||
| for i in range(1, snapshot_history + 1): | ||
| altered_snapshot = snapshots[-i] | ||
| altered_snapshot = altered_snapshot.model_copy(update={"summary": Summary(operation=operation)}) | ||
| snapshots[-i] = altered_snapshot | ||
| table.metadata = table.metadata.model_copy( | ||
| update={"snapshots": snapshots}, | ||
| ) | ||
| oldest_snapshot = table.snapshots()[-snapshot_history] | ||
| newest_snapshot = cast(Snapshot, table.current_snapshot()) | ||
| mock_delete_file = DataFile.from_args( | ||
| content=DataFileContent.POSITION_DELETES, | ||
| file_path="s3://dummy/path", | ||
| ) | ||
| mock_delete_file.spec_id = 0 | ||
| def mock_read_manifest_side_effect(self: Snapshot, io: FileIO) -> list[ManifestFile]: | ||
| """Mock the manifests method to use the snapshot_id for lookup.""" | ||
| snapshot_id = self.snapshot_id | ||
| if snapshot_id in mock_manifests: | ||
| return mock_manifests[snapshot_id] | ||
| return [] | ||
| def mock_fetch_manifest_entry(self: ManifestFile, io: FileIO, discard_deleted: bool = True) -> list[ManifestEntry]: | ||
| return [ | ||
| ManifestEntry.from_args( | ||
gabeiglio marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| status=ManifestEntryStatus.ADDED, | ||
| snapshot_id=self.added_snapshot_id, | ||
| sequence_number=self.min_sequence_number, | ||
| data_file=mock_delete_file, | ||
| ) | ||
| ] | ||
| with ( | ||
| patch("pyiceberg.table.snapshots.Snapshot.manifests", new=mock_read_manifest_side_effect), | ||
| patch("pyiceberg.manifest.ManifestFile.fetch_manifest_entry", new=mock_fetch_manifest_entry), | ||
| ): | ||
| dfi = _added_delete_files( | ||
| table=table, | ||
| starting_snapshot=newest_snapshot, | ||
| data_filter=None, | ||
| parent_snapshot=oldest_snapshot, | ||
| partition_set=None, | ||
| ) | ||
| assert not dfi.is_empty() | ||
| assert dfi.referenced_delete_files()[0] == mock_delete_file | ||
| def test_validate_no_new_delete_files_raises_on_conflict( | ||
| table_v2_with_extensive_snapshots_and_manifests: tuple[Table, dict[int, list[ManifestFile]]], | ||
| ) -> None: | ||
| table, _ = table_v2_with_extensive_snapshots_and_manifests | ||
| oldest_snapshot = table.snapshots()[0] | ||
| newest_snapshot = cast(Snapshot, table.current_snapshot()) | ||
| with patch("pyiceberg.table.update.validate.DeleteFileIndex.is_empty", return_value=False): | ||
| with pytest.raises(ValidationException): | ||
| _validate_no_new_delete_files( | ||
| table=table, | ||
| starting_snapshot=newest_snapshot, | ||
| data_filter=None, | ||
| partition_set=None, | ||
| parent_snapshot=oldest_snapshot, | ||
| ) | ||
| def test_validate_no_new_delete_files_for_data_files_raises_on_conflict( | ||
| table_v2_with_extensive_snapshots_and_manifests: tuple[Table, dict[int, list[ManifestFile]]], | ||
| ) -> None: | ||
| table, _ = table_v2_with_extensive_snapshots_and_manifests | ||
| oldest_snapshot = table.snapshots()[0] | ||
| newest_snapshot = cast(Snapshot, table.current_snapshot()) | ||
| mocked_data_file = DataFile.from_args() | ||
| with patch("pyiceberg.table.update.validate.DeleteFileIndex.for_data_file", return_value=[mocked_data_file]): | ||
| with pytest.raises(ValidationException): | ||
| _validate_no_new_deletes_for_data_files( | ||
| table=table, | ||
| starting_snapshot=newest_snapshot, | ||
| data_filter=None, | ||
| data_files={mocked_data_file}, | ||
| parent_snapshot=oldest_snapshot, | ||
| ) | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've verified this logic locally by mocking a concurrent conflict. The table.format_version < 2 guard correctly prevents unnecessary overhead for V1 tables, and the use of the DeleteFileIndex ensures we are only blocking commits when there is a real overlap in data files (avoiding 'lazy' global locks).