Skip to content

Fixes 36026: Reconcile replayed RDF non-lineage and glossary relations against SQL - #38482

Merged
yan-3005 merged 4 commits into
open-metadata:mainfrom
iamkiayun:fix/36026-rdf-outbox-replay-reconcile
Nov 24, 2026
Merged

yan-3005 merged 4 commits into
open-metadata:mainfrom
iamkiayun:fix/36026-rdf-outbox-replay-reconcile

Conversation

@iamkiayun

@iamkiayun iamkiayun commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Describe your changes:

Fixes #36026

Brings replayed non-UPSTREAM relationships (RdfRepository.refreshRelationship) and typed glossary term relations (RdfRepository.refreshGlossaryTermRelation) onto the same replay-time SQL reconciliation contract as refreshLineage, so delayed outbox retries or blue-green rebuild replays never resurrect relationship edges that were deleted in SQL (for example via bulk updates or CSV imports that bypass RDF remove hooks).

Type of change:

  • Bug fix

High-level design:

Approach & why

  • SQL edge-existence lookups (CoreRelationshipDAOs.EntityRelationshipDAO):
    • Added exists(fromId, fromEntity, toId, toEntity, relation) (SELECT EXISTS (SELECT 1 FROM entity_relationship ... AND deleted = FALSE)) backed by the (fromId, toId, relation, relationType) primary key for O(1) non-lineage relationship verification on both MySQL and PostgreSQL.
    • Added findBetween(fromId, toId, entityType, relation) (UNION ALL of forward and non-self reverse primary-key lookups with deleted = FALSE) to fetch live glossary RELATED_TO rows between two terms in either direction.
  • Projection loader seams (RdfProjectionLoaders.java):
    • Added RelationshipExists and GlossaryTermRelationRows loader interfaces to RdfProjectionLoaders, plus pure hasGlossaryTermRelation / storedRelationType matching helpers.
    • Handles canonical UUID-ordered storage (fromId < toId with inverse relationType when authored from the larger UUID), symmetric/no-inverse types, legacy rows with blank relationType column (JSON fallback and "relatedTo" default), case-insensitive UUID and type matching, and self-relations.
  • Replay reconciliation (RdfRepository.java):
    • refreshRelationship: checks both SQL edge existence and endpoint liveness (!relationshipExists || !hasLiveEnds); if missing or soft-deleted, calls removeRelationship(relationship) instead of addRelationship(relationship).
    • refreshGlossaryTermRelation: resolves the relationship type (active ignore-case first, falling back to Include.ALL so a soft-deleted type whose last edge was deleted still cleans up cleanly without dead-lettering), checks type liveness, endpoint liveness, and SQL row match; if missing or non-live, removes only the directed semantic triple pair (<from> <pred> <to> and <to> <inversePred> <from>) via removeDirectedGlossaryTermRelation so a stale replay after flipping an asymmetric relation's direction (A broader B -> B broader A) never erases the live reversed edge.

Alternatives considered

  • No-op on missing SQL edge instead of deleting: Rejected because if a stale triple was already written before a hook-bypassing SQL deletion or soft delete, replaying ADD should converge the graph to match SQL (matching refreshLineage).
  • Calling removeGlossaryTermRelation (all 4 direction/predicate combinations) on stale glossary ADD: Rejected because it would wipe a live opposite-direction edge if the user flipped A broader B to B broader A before the old ADD replayed.

Tests:

Use cases covered

  • Replaying a captured non-lineage RelationshipChange (CONTAINS) after the SQL edge was deleted removes any stale triple and never resurrects it; replaying after an endpoint soft-delete removes the edge.
  • Replaying a captured GlossaryRelationChange (broader, etc.) after SQL deletion removes any stale triple and never resurrects it; replaying after a term soft-delete removes the edge.
  • Canonical reversed SQL storage (fromId > toId stored as (toId, fromId, inverseType)) projects both perspectives for every seeded and custom RelationshipType.
  • Flipping an asymmetric relation's direction (A broader B -> B broader A) and replaying the stale ADD(A, B, broader) removes stale A broader B / B narrower A while preserving live B broader A.
  • Deleting one relation type between two terms (broader) preserves other live types between the same pair (seeAlso).
  • Legacy empty relationType rows (JSON fallback / default "relatedTo") and soft-deleted RelationshipType cleanup work without errors.

Unit tests

  • I added unit tests for the new/changed logic (reproducing and verifying the exact #36026 failure scenarios).
  • Files updated:
    • openmetadata-service/src/test/java/org/openmetadata/service/rdf/RdfLiveWriteTest.java
    • openmetadata-service/src/test/java/org/openmetadata/service/rdf/RdfGlossaryTermRelationTest.java
    • openmetadata-service/src/test/java/org/openmetadata/service/rdf/RdfEntityDeleteLineageTest.java
    • openmetadata-service/src/test/java/org/openmetadata/service/rdf/RdfQualityEvidenceTest.java
  • Verified locally with JDK 21:
    • Full org.openmetadata.service.rdf.* unit suite: 1,816 run, 0 failures, 0 errors, 0 skipped
    • Focused affected unit classes re-run after final refinement (4 classes): 149 run, 0 failures, 0 errors, 0 skipped
    • mvn -B spotless:check, scripts/harness/check_decision_records.py, .github/scripts/plan_local_java_tests.py --base upstream/main --check-branch: all clean
  • Coverage %: Not measured with JaCoCo locally.

Backend integration tests

  • I added an integration test in openmetadata-integration-tests/src/test/java/org/openmetadata/it/tests/RdfLiveProjectionIT.java (replayedRelationshipAndGlossaryRelationAdditionsDoNotResurrectDeletedEdges) exercising real SQL exists / findBetween queries, canonical reversed glossary storage, SQL deletion, stale replay, and direction-flip preservation against Fuseki.
  • Local status: compile-verified via mvn -B test-compile -pl openmetadata-integration-tests; live-container execution is pending (Docker daemon unavailable locally; ready for CI safe to test / merge queue).

Ingestion integration tests

  • Not applicable (no ingestion changes).

Playwright (UI) tests

  • Not applicable (no UI changes).

Manual testing performed

  • Not run against a local Docker stack (Docker daemon was unavailable locally).

UI screen recording:

Not applicable — backend RDF live-write replay reconciliation only (no UI changes).

Checklist:

  • I have read the CONTRIBUTING document.
  • My PR title is Fixes 36026: Reconcile replayed RDF non-lineage and glossary relations against SQL
  • My PR is linked to a GitHub issue via Fixes #36026 above.
  • I have commented on my code, particularly in hard-to-understand areas.
  • For JSON Schema changes: Not applicable (no JSON Schema changes).
  • For a UI feature or feature task: Not applicable (backend-only bug fix).
  • I have added tests (unit + integration) and listed them above, including regression tests covering the exact #36026 bug.
  • Self-review: Reviewed final diff against REVIEW.md; added null-payload guards in refreshRelationship/refreshGlossaryTermRelation, case-insensitive UUID matching in hasGlossaryTermRelation, and direction-flip + guarded cleanup in RdfLiveProjectionIT.
  • Decision check: Aligns with the existing replay-reconciliation contract (refreshLineage and ADR:2026-10-26-the-rdf-graph-projects-the-live-catalog-only); no new cross-component contract or policy introduced.

RetriggerView in GreptileConfidence Score: 4/5

Fix reverse matching for directed table relations before merging.

Findings

  1. P1 Deleted table references return ▶

Summary

The PR reconciles replayed RDF relationships against current SQL rows and live endpoints.

  • Ordinary relationship replays follow the current SQL edge and live endpoints.
  • Glossary relation replays match live typed edges and remove only stale directions.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Replay relationship addition] --> B{Forward SQL row exists?}
  B -- Yes --> E{Both endpoints live?}
  B -- No --> C{Reverse matching allowed?}
  C -- No --> D[Remove RDF edge]
  C -- Yes --> F{Reverse SQL row exists?}
  F -- No --> D
  F -- Yes --> E
  E -- No --> D
  E -- Yes --> G[Add RDF edge]
Loading

Reviews (2) · Last reviewed commit: "Accept reversed canonical SQL row for bi..." · Reviewed by Greptile

…ssary relations against SQL

- Add primary-key-backed exists and bidirectional findBetween lookups to
  CoreRelationshipDAOs.EntityRelationshipDAO.
- Wire RelationshipExists and GlossaryTermRelationRows loaders into
  RdfProjectionLoaders with canonical UUID-ordered / inverse / legacy JSON
  glossary term relation matching.
- Reconcile RdfRepository.refreshRelationship and refreshGlossaryTermRelation
  against current SQL state on replay, removing stale triples instead of
  resurrecting deleted edges while preserving live opposite-direction asymmetric
  glossary relations.
- Add unit tests in RdfLiveWriteTest and RdfGlossaryTermRelationTest and an
  integration test in RdfLiveProjectionIT.
@iamkiayun
iamkiayun requested a review from a team as a code owner November 23, 2026 11:44
@github-actions

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

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

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.

@yan-3005

Copy link
Copy Markdown
Contributor

@iamkiayun thanks for this. The glossary-relation reconciliation looks correct. One blocker on the generic path:

RdfProjectionLoaders.relationshipExists checks only (fromId -> toId). But EntityRepository.addRelationship(..., bidirectional = true) stores a single row with the smaller UUID first, then queues RDF adds in both directions ("Also add the reverse relationship to RDF"). Today that is metric relatedMetrics (RELATED_TO, MetricRepository line 662). With this PR, the non-canonical add misses exists and goes to removeRelationship, so A om:relatedTo B is dropped whenever A.id > B.id even though the SQL relationship exists. main projects both.

Please accept the reversed row only for the bidirectional case (same-type RELATED_TO). Don't check both directions for every relation: for directed ones like CONTAINS/OWNS, a reverse row is a different fact and accepting it would keep a stale edge alive.

Tests: the generic exists fake in RdfLiveWriteTest (rel -> relationshipExists) ignores direction, so it can't catch this. Please make it direction-aware and add a case with a reversed stored row for a bidirectional relation. Also, RdfLiveProjectionIT hasn't run anywhere yet (IT lanes are skipped on PRs), so we'll need a green run of it before merge.

@yan-3005 yan-3005 added the To release Will cherry-pick this PR into the release branch label Nov 23, 2026

@yan-3005 yan-3005 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Blocking on the bidirectional RELATED_TO case: the one-direction exists check drops A om:relatedTo B whenever A.id > B.id. Details and suggested fix in #38482 (comment)

Copy link
Copy Markdown
Contributor Author

@yan-3005 Thanks for pointing out how the canonical SQL row is stored. ddf9ca60 limits reverse-row matching to metric-to-metric RELATED_TO. The direction-aware regression tests verify that the remaining reverse row cannot keep a deleted table or knowledge-page link alive.

I reran the RDF unit suite (1,819 passed) and RdfLiveProjectionIT (5/5 passed locally with MySQL, Elasticsearch, and Fuseki).

@yan-3005

Copy link
Copy Markdown
Contributor

@iamkiayun thanks, ddf9ca6 resolves my blocker. Limiting the reverse lookup to metric→metric RELATED_TO matches the only bidirectional addRelationship(..., true) caller (MetricRepository), and the direction-aware tests cover it. Greptile's table-constraint thread is outdated by the same change.

One more thing: please merge the latest main into the branch. The red Compatibility checks (Collate build) isn't caused by your code; it fails on PR branches based on main from before ~16:00 UTC Nov 23 and passes on fresher ones. Once that's green I'll approve. RdfLiveProjectionIT will also need a run before merge since PR CI skips ITs.

@gitar-bot

gitar-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 🟡 Medium risk 2 closed / 2 findings

Replay reconciliation removes stale RDF relationship triples against SQL and live endpoints, which could change projected catalog relations after delayed outbox events.

Reconciles replayed RDF non-lineage and glossary relations against SQL so delayed outbox retries and blue-green replays no longer resurrect deleted edges. Replay now accepts reversed canonical UUID-ordered rows for bidirectional relations and stops matching directed knowledge-page rows as same-type RELATED_TO. No open issues remain.

✅ 2 closed
✅ Bug: Replay removes live bidirectional edges stored in canonical UUID order

📄 openmetadata-service/src/main/java/org/openmetadata/service/rdf/RdfRepository.java:678-680 📄 openmetadata-service/src/main/java/org/openmetadata/service/rdf/RdfProjectionLoaders.java:152-166 📄 openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/CoreRelationshipDAOs.java:1894-1904
For a bidirectional relationship, EntityRepository.addRelationship(..., bidirectional=true) writes a single SQL row with fromId < toId. It still queues two RDF adds: one in the authored direction and one reversed. refreshRelationship now runs relationshipDAO().exists(fromId, fromEntity, toId, toEntity, relation), which matches one direction only. For each pair, the add whose direction is not the stored one finds no row and goes to removeRelationship. Every live bidirectional edge, such as metric RELATED_TO from MetricRepository.storeRelationships (line 662), loses one of its directions in the graph on the first live apply, not just on a stale replay.

The PR already handles this canonical ordering for glossary relations (findBetween with its UNION) but not for the generic path. The fix is to check both storage directions for symmetric relationships, or simply always check both directions as findBetween does. A unit test with a reversed SQL row would catch this.

✅ Edge Case: Same-type RELATED_TO heuristic also matches directed knowledge-page rows

📄 openmetadata-service/src/main/java/org/openmetadata/service/rdf/RdfProjectionLoaders.java:177-190
isBidirectional counts every same-type RELATED_TO as bidirectional. Only Metric.relatedMetrics actually writes it that way (MetricRepository.java:662,1234 with bidirectional=true). KnowledgePageRepository stores related articles as directed KNOWLEDGE_PAGE -> KNOWLEDGE_PAGE RELATED_TO rows (KnowledgePageRepository.java:679-684,950-956, no bidirectional flag).

Here is how a deleted edge comes back. Article A lists B (row B->A) and B lists A (row A->B). A user then removes A from B's related articles, which deletes row A->B. If an earlier ADD for A->B is replayed, the primary lookup misses, but the reversed lookup finds the unrelated B->A row. relationshipExists returns true, so the stale A->B triple is re-added instead of removed. That brings back the resurrection this PR is meant to stop.

Fix: restrict the reversed-row fallback to the entity types that really store canonical bidirectional rows, e.g. an explicit Set.of(Entity.METRIC). Or carry a bidirectional flag on the captured change.

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 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verified at 7bd9f99 (MySQL + Elasticsearch, rdf lane with Fuseki): RdfLiveProjectionIT 5/5 with 0 skipped, including replayedRelationshipAndGlossaryRelationAdditionsDoNotResurrectDeletedEdges and bidirectionalMetricRelatedToProjectsBothDirectionsAndReconcilesOnReplay; RDF unit tests 152/0. Bidirectional blocker resolved. Thanks @iamkiayun.

@yan-3005
yan-3005 merged commit 0c6f3b0 into open-metadata:main Nov 24, 2026
50 of 51 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🤖 Backport to 2.0 is under way. It is built from openmetadata-collate, which holds the credentials needed to open a PR here, and it will comment again with the result — nothing is needed from you unless it reports otherwise.

@github-actions

Copy link
Copy Markdown
Contributor

🤖 Backport to 2.1 is under way. It is built from openmetadata-collate, which holds the credentials needed to open a PR here, and it will comment again with the result — nothing is needed from you unless it reports otherwise.

@gh-bot-collate

Copy link
Copy Markdown

🤖 Not backported to 2.0. Claude looked at the conflict and declined to resolve it:

Backport aborted: PR #38482 → 2.0

Reason

PR #38482 ("Reconcile replayed RDF non-lineage and glossary relations
against SQL") patches a live-mutation replay/reconciliation mechanism in
RdfRepository (refreshRelationship, refreshGlossaryTermRelation,
RdfProjectionLoaders, RdfEntityLiveness) that does not exist on
2.0 at all
.

Checked on origin/2.0:

In short, #38482 is an incremental fix on top of a live-replay
architecture that was never ported to 2.0. Resolving the conflicts here
would mean inventing RdfProjectionLoaders, RdfEntityLiveness, the
decomposed CoreRelationshipDAOs, and the whole replay-on-SQL-state
concept from scratch on this branch — effectively backporting #38419 (and
likely its own prerequisites) first. That is well outside the scope of a
single-PR backport and risks producing RDF replay behavior on 2.0 that was
never reviewed as such.

Recommendation

If RDF replay reconciliation is wanted on 2.0, backport PR #38419 (and
confirm no further prerequisite RDF PRs are missing) first, then retry
this backport of #38482 on top of it.

State left behind

The repository is left mid-git cherry-pick (started from
0c6f3b0d0cf775c312979ffbda93044195f85439) with the original conflicts
untouched, as instructed — no files were staged, resolved, or committed.

resolver run

@gh-bot-collate

Copy link
Copy Markdown

🤖 Backport to 2.1 failed. The conflict could not be resolved, so this change is not on that branch. Resolver run.

yan-3005 added a commit that referenced this pull request Nov 24, 2026
…lations against SQL (#38482) (#38581)

Before projecting a replayed RDF relationship add (generic relationships and
glossary-term relations), check the SQL row still exists; if it does not,
remove the triple instead of resurrecting it. The reverse-row lookup applies
only to metric -> metric RELATED_TO.

Adapted for 2.1 (main's #38419 is not on this branch):
- RdfProjectionLoaders: keep 2.1's 6-component record (no BoundAssets or
  table-key loaders); add RelationshipExists and GlossaryTermRelationRows.
- RdfLiveWriteTest, RdfGlossaryTermRelationTest, RdfEntityDeleteLineageTest:
  construct the 6-arg loaders; keep 2.1's existing tests and add #38482's
  cases.
- RdfQualityEvidenceTest: dropped, the file does not exist on 2.1.

(cherry picked from commit 0c6f3b0)

Co-authored-by: Kia Yun <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

To release Will cherry-pick this PR into the release branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Detail Bug] RDF/SPARQL: Outbox replay can resurrect deleted non-lineage and glossary relationship edges

2 participants