Skip to content

fix(table): reject concurrently added duplicate files in AddDataFiles and AddDataFilesByPath - #2678

Open
akritishakya wants to merge 9 commits into
apache:mainfrom
akritishakya:concurrent-adddatafilespath-adds-twice
Open

akritishakya wants to merge 9 commits into
apache:mainfrom
akritishakya:concurrent-adddatafilespath-adds-twice

Conversation

@akritishakya

@akritishakya akritishakya commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2629

Problem

By default, AddDataFiles and AddDataFilesByPath reject paths already in the table, but only against the snapshot the transaction was planned on. If another writer commits the same path first, a commit retry adds the file a second time and its rows are read twice. The same happens when Commit() is called again after a failed Commit() that left the transaction committable.

Fix

  • On retry, the snapshot rebuild calls the new checkDuplicatePaths (after checkRemovedFiles), which fails the commit with ErrDuplicateDataFiles, wrapped in ErrTransactionUnusable, if any of the paths being added is a live data file in the new parent.
  • It reads only the new parent's data manifests that were not in the planned parent; the planned parent's manifests were already read at staging.
  • It does not use snapshot history, so a concurrent rollback or snapshot expiry does not break it.
  • WithoutDuplicateCheck() and ignoreDuplicates=true also disable this check.
  • Docs updated in table.go, concurrent-writes.md and releases.md.

Tests

New file table/add_files_conflict_test.go covers:

  • a concurrent duplicate, with and without manifest merge, and on a second Commit() (rejected)
  • concurrent distinct files, rollback, snapshot expiry, and add-then-remove (committed, no duplicate rows)
  • the duplicate check disabled (committed)

The tests that expect ErrDuplicateDataFiles fail without this change.

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the fix is correct. Doing the check in rebuildFn rather than in a validator makes sense: fast/merge append don't register validators, and newConflictContext would fail a rollback with ErrCommitDiverged. Checking live entries on the fresh parent handles rollback, expiry and add-then-delete correctly. Merged with main, go test -race ./table/ -run 'AddDataFiles|Duplicate|Concurrent' passes, and the three rejection tests fail with snapshot_producers.go and transaction.go reverted to main.

  1. Retry cost. checkRemovedFiles deliberately returns early for append producers so a retry doesn't re-read the parent's manifests, but checkDuplicatePaths now reads every data manifest of the fresh parent on each retry. Manifest files never change once written, and every manifest in the planned parent was already scanned by the staging-time duplicate check. So the check could skip manifests whose path is in the planned parent's manifest list and read only those added by concurrent commits. That stays correct after a rollback (the manifest set only shrinks) and after merge-append rewrites (rewritten manifests get new paths). Either way, the comment in checkRemovedFiles about append producers skipping the scan should be updated.
  2. nit: none of the new tests enable commit.manifest-merge.enabled. appendSnapshotProducer can return mergeAppend(), so one concurrent-duplicate case with it enabled would cover that producer.
  3. nit: duplicateCheckPaths holds the caller's setToAdd / set map itself. Nothing changes it after staging, so this is safe, but maps.Clone would remove the coupling.
  4. Question: ReplaceDataFiles / ReplaceDataFilesWithDataFiles also take caller-supplied paths. Do they have the same retry gap, or is that handled elsewhere?

@akritishakya

Copy link
Copy Markdown
Contributor Author

@zeroshade Thanks for the review.

  • Updated. The staging-time duplicate check now also records the manifest paths of the planned parent, and checkDuplicatePaths skips those manifests on retry, so only manifests added by concurrent commits are read. The paths are recorded at staging rather than resolved on retry, since snapshot expiry can remove the planned parent along with its manifest list. I also updated the comment in checkRemovedFiles.
  • Added TestAddDataFilesByPath_ConcurrentDuplicateRejectedWithManifestMerge, which enables commit.manifest-merge.enabled. It fails without the fix.
  • Both functions now store maps.Clone of the set.
  • ReplaceDataFiles and ReplaceDataFilesByPath are handled elsewhere under the default serializable isolation: both use the overwrite producer, whose default validator rejects any data file added by a concurrent commit, so the retry fails instead of adding the file twice. With write.update.isolation-level=snapshot, that validator is skipped and they have the same gap.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Transaction: concurrent AddDataFiles / AddDataFilesByPath of the same file adds it twice on retry, duplicating rows

2 participants