Skip to content

Fixes #33741: lock the data product so a concurrent assets/add cannot orphan its edge (2.0) - #37107

Merged
yan-3005 merged 1 commit into
2.0from
fix/33741-data-product-delete-race-2.0
Nov 5, 2026
Merged

yan-3005 merged 1 commit into
2.0from
fix/33741-data-product-delete-race-2.0

Conversation

@yan-3005

@yan-3005 yan-3005 commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Describe your changes:

Fixes #33741

Backport of #33762 to 2.0. An assets/add that lands while the same data product is being hard-deleted leaves an entity_relationship HAS row whose data product no longer exists.

Why it happens on 2.0. The delete removes the data product's relationship rows first and its own row last, in one transaction, without locking anything. On the write side, DataProductRepository.bulkAssetsOperation checks the data product once with find(entityId, ALL), which can come from the cache, then calls addRelationship for each asset outside any transaction; its @Transaction annotation has no effect on a repository class. An add that lands after the delete's sweep inserts its edge, and nothing removes it.

What this changes (same design as #33762 on main, plus one 2.0-only change):

  • Writers lock first. Before an entity is linked to data products it does not already have, the write share-locks those rows (EntityDAO.lockExistingIds, FOR SHARE) in the same transaction as the edge insert, and fails with dataProduct … not found when a row is gone. Covers updateDataProducts (PUT/PATCH) and both storeDataProducts paths (create, bulk create).
  • 2.0 only: the assets/add path. bulkAssetsOperation now share-locks the data product and inserts each edge inside one flushInOneTransaction, because on 2.0 assets/add does not go through the asset's own PATCH as it does on main (Fixes #34689: record every Assets tab edit on the asset as its own PATCH #35026).
  • The delete locks first. A lockRowsForDelete hook runs first inside both cleanup transactions, a direct delete and a domain's bulk cascade. DataProductRepository takes the row locks there (EntityDAO.lockIdsForUpdate, SELECT … IN (…) ORDER BY id FOR UPDATE) before the sweep.

With both, the two requests serialize: either the add commits first and the delete's sweep removes its edge, or the delete commits first and the add fails with not-found.

Not covered here (same as main): input/output port edges have the same race; ghost rows that already exist are #33742.

Type of change:

  • Bug fix

High-level design:

N/A — backport of #33762. The lock ordering is documented in the Javadoc of EntityRepository#lockDataProducts and #lockRowsForDelete.

Tests:

Backend integration tests

  • Added openmetadata-integration-tests/.../it/tests/DataProductDeleteRaceIT.java (ported from Fixes #33741: lock the data product so a concurrent assets/add cannot orphan its edge #33762). Each test holds a row lock on a separate connection to pause one request at a known point, waits on the database's own view of in-flight statements, and asserts on the rows left in data_product_entity and entity_relationship.
    • 2.0 adaptations: the add-first case links the data product through a PATCH of the table (2.0's assets/add never writes the asset row, so holding that row cannot pause it), and on MySQL a waiting statement is detected through PROCESSLIST (a FOR SHARE blocked on a row sits in state statistics without an innodb_trx LOCK WAIT entry).
  • Fails on 2.0 without the fix: Postgres 3 of 3, MySQL 2 of 3 (No relationship row may outlive the deleted data product ==> expected: <0> but was: <1>).
  • With the fix, run locally against this branch:
Run Engine Result
DataProductDeleteRaceIT postgres-opensearch 3 run, 0 failures
DataProductDeleteRaceIT mysql-elasticsearch 3 run, 0 failures
DataProductDeleteRaceIT,DataProductResourceIT,DataProductDomainMigrationIT,DomainResourceIT,DomainBulkAssetsDryRunIT,DomainIsolationIT,MultiDomainHasDomainIT,BulkAssetChangeEventUserIT,EntityRelationshipCleanupIT,UnitOfWorkRollbackIT,OrphanedEntityReferenceIT mysql-elasticsearch 536 run, 0 failures, 0 errors, 46 skipped
DataProductDeleteRaceIT,DataProductResourceIT,DomainResourceIT postgres-opensearch 494 run, 0 failures, 0 errors, 46 skipped
OneTransactionFlushAtomicityIT cache-tests 9 run, 0 failures, 0 errors, 1 skipped

Playwright (UI) tests

DataProducts, Domains, DataProductAndSubdomains, DomainAdvanced, DataProductDomainMigration against a local server built from this branch: 102 of 103 pass. The one failure, DataProductAndSubdomains.spec.ts:353 ("Add assets to data product and verify count"), times out with the Add Assets modal's Save button still disabled, so it never sends assets/add; it fails the same way against plain 2.0 (81ce386e08), so it is pre-existing and unrelated to this change.

Manual testing performed

Covered by the race IT, which drives the real assets/add, table PATCH, data product delete and domain hard-delete endpoints against MySQL and Postgres.

UI screen recording:

Not applicable — backend-only fix.

Checklist:

  • I have read the CONTRIBUTING document.
  • My PR title is Fixes <issue-number>: <short explanation>
  • My PR is linked to a GitHub issue via Fixes #<issue-number> above.
  • I have commented on my code, particularly in hard-to-understand areas.
  • For JSON Schema changes: not applicable, no schema change.
  • For a UI feature or feature task: not applicable, backend-only.
  • I have added tests (unit / integration / Playwright as applicable) and listed them above.
  • Self-review: reviewed against REVIEW.md; same change as Fixes #33741: lock the data product so a concurrent assets/add cannot orphan its edge #33762, which passed its review threads.
  • Decision check: no docs/decisions/ record; the lock ordering is stated in the code's Javadoc.
  • I have added a test that covers the exact scenario we are fixing.

🤖 Generated with Claude Code

… orphan its edge (2.0)

Backport of #33762. A hard delete of a data product sweeps its
entity_relationship rows before deleting its own row, and writers only
checked that the data product exists with a read the cache can answer, so
an assets/add landing inside the delete left a HAS edge to a deleted data
product.

Writers now share-lock the data products an entity gains (lockExistingIds,
FOR SHARE) in the same transaction as the edge insert and fail with
not-found when the row is gone. On 2.0 the assets/add path is
DataProductRepository.bulkAssetsOperation, which inserted edges outside any
transaction; it now locks and inserts each edge inside flushInOneTransaction.
A lockRowsForDelete hook runs first in both cleanup transactions (direct
delete and domain cascade), and DataProductRepository locks its rows there
before the relationship sweep.

DataProductDeleteRaceIT holds row locks on a separate connection to fix the
interleaving of delete-first, add-first (through a table PATCH on 2.0) and
domain-cascade races.
@yan-3005
yan-3005 requested a review from a team as a code owner November 5, 2026 07:08
@yan-3005
yan-3005 requested review from sonika-shah and removed request for a team November 5, 2026 07:08
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

This PR cannot be merged until the following are addressed on its linked issue:

  • No GitHub issue is linked. Link an issue in the Development section of the PR (or add Fixes #12345 to the description). For a same-org cross-repo issue, add Fixes open-metadata/<repo>#123 to the description.

The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically.

Maintainers can bypass this check by adding the skip-pr-checks label.

@github-actions github-actions Bot added backend safe to test Add this label to run secure Github workflows on PRs labels Nov 5, 2026
@gitar-bot

gitar-bot Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Code Review ✅ Approved

🔴 High risk · The new locks serialize data-product linking with hard deletes; transaction or lock-order problems could block requests or leave orphaned relationship edges.

Backports the data product delete race fix to 2.0: writers share-lock the data product before adding edges, the assets/add path inserts edges in one transaction, and the delete takes row locks first so a concurrent assets/add can no longer orphan its relationship row. No issues found.

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

@yan-3005
yan-3005 merged commit 044f91b into 2.0 Nov 5, 2026
60 of 62 checks passed
@yan-3005
yan-3005 deleted the fix/33741-data-product-delete-race-2.0 branch November 5, 2026 08:56

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend safe to test Add this label to run secure Github workflows on PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants