Skip to content

fix(ingestion): cache Query and User lookups during usage ingestion - #32254

Merged
mohittilala merged 18 commits into
open-metadata:mainfrom
OmRajput17:fix-usage-caching-32210
Sep 26, 2026
Merged

mohittilala merged 18 commits into
open-metadata:mainfrom
OmRajput17:fix-usage-caching-32210

Conversation

@OmRajput17

@OmRajput17 OmRajput17 commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #32210

Summary

  • Wrap _get_or_create_query with a SkipNoneLRUCache keyed by query hash, so repeated tables referencing the same SQL skip the redundant GET/PUT. A miss is never cached, since another table in the same run may create the Query moments later.
  • Add get_cached_user_reference, backed by an LRUCache keyed by username, shared between the staging get_by_name(User) call in table_usage.py and the lifecycle get_entity_reference(User) call in metadata_usage.py. Misses are cached too, since a user absent from OpenMetadata at the start of a run won't appear mid-run.

Both use the existing metadata.utils.lru_cache utilities as suggested in the issue, rather than adding a new dependency.

Note on a deliberate tradeoff

The Query cache's key function excludes self, so the cache is shared process-wide across all OMetaQueryMixin/OpenMetadata instances rather than per-instance (unlike the existing __get_query_by_hash, which uses plain @lru_cache and is per-instance since self is part of the key). In practice a usage run typically uses a single OpenMetadata client, so this should be a non-issue, but flagging it explicitly in case a different approach is preferred.

Tests

  • Added tests/unit/metadata/ingestion/ometa/test_usage_caching.py covering: Query cache hit on repeat lookup, miss-not-cached behavior for Queries, User cache hit and miss-cached behavior, and independent caching per name.
  • All existing tests in the touched files still pass: test_metadata_usage.py (21 passed), test_user_mixin.py (included above), test_life_cycle_query_mixin.py (4 passed).

RetriggerConfidence Score: 4/5

The cache implementation appears functionally safe, but the repository’s explicit testing requirement must be satisfied before merging.

Findings

  1. P2 Tests Verify Mock Wiring ▶
Summary

This PR reduces redundant OpenMetadata API calls during usage ingestion by adding bounded, client-scoped caches for Query entities and User references.

  • Caches successful Query lookup and creation results while leaving misses uncached.
  • Caches User-reference hits and misses across staging and lifecycle processing.
  • Scopes both caches to individual OpenMetadata clients and limits each to 1,000 entries.
  • Adds tests for hits, misses, service separation, and user-reference conversion.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart LR
  U[Usage record] --> Q{Query cached?}
  Q -->|Yes| QE[Reuse Query entity]
  Q -->|No| QL[Lookup or create Query]
  QL -->|Found or created| QC[Cache per client]
  QL -->|Missing| QN[Return None without caching]
  U --> R{User reference cached?}
  R -->|Yes| UR[Reuse reference or cached miss]
  R -->|No| UL[Lookup User reference]
  UL --> UC[Cache result per client]
Loading

Reviews (4) · Last reviewed commit: "Merge branch 'main' into fix-usage-cachi..."

…ap _get_or_create_query with a SkipNoneLRUCache keyed by query hash sorepeated tables referencing the same SQL skip the redundant GET/PUT, whilenever caching a miss (another table may have just created the Query).Add get_cached_user_reference, backed by an LRUCache keyed by username,shared between the staging get_by_name(User) call and the lifecycleget_entity_reference(User) call, caching misses too since a user absentat the start of a run will not appear mid-run.Fixes open-metadata#32210
@OmRajput17
OmRajput17 requested a review from a team as a code owner August 29, 2026 05:22
@github-actions

github-actions Bot commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

✅ PR checks passed

The linked issue has a description and all required Shipping project fields set. Thanks!

@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@OmRajput17

Copy link
Copy Markdown
Contributor Author

Hi maintainers, the PR checklist is blocked on the linked issue (#32210) being added to the Shipping project, which I don't have permission to do myself. Could someone add it when you get a chance? Happy to push a follow-up commit to re-trigger the check once that's done.

…cached User EntityReference in staging, fixing a bug where a model object was passed to CreateQueryRequest(users=...) instead of a string.- Scope Query and User reference caches per-OpenMetadata-client-instance instead of at the class level, so multiple clients/catalogs in the same process never share cached data.- Reduce cache sizes from 4096 to 1000 to match repo convention.
@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

Cover failed query lookup and creation retries, and align the cache changes with current Python formatting.
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@IceS2 IceS2 added the safe to test Add this label to run secure Github workflows on PRs label Sep 4, 2026
@github-actions github-actions Bot removed the safe to test Add this label to run secure Github workflows on PRs label Sep 4, 2026
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@IceS2 IceS2 added the safe to test Add this label to run secure Github workflows on PRs label Sep 4, 2026
@github-actions github-actions Bot removed the safe to test Add this label to run secure Github workflows on PRs label Sep 4, 2026
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@IceS2 IceS2 added the safe to test Add this label to run secure Github workflows on PRs label Sep 4, 2026
@github-actions github-actions Bot removed the safe to test Add this label to run secure Github workflows on PRs label Sep 6, 2026
@github-actions

github-actions Bot commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@github-actions github-actions Bot removed the safe to test Add this label to run secure Github workflows on PRs label Sep 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@ulixius9 ulixius9 added the safe to test Add this label to run secure Github workflows on PRs label Sep 25, 2026
@mohittilala
mohittilala added this pull request to the merge queue Sep 25, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🚦 Removed from the merge queue — failed_checks (2026-09-25T23:48:44Z)

Blocked the queue: playwright-summary

@ShaileshParmar11
ShaileshParmar11 added this pull request to the merge queue Sep 26, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🚦 Removed from the merge queue — failed_checks (2026-09-26T04:42:59Z)

Blocked the queue: playwright-summary

@github-actions github-actions Bot removed the safe to test Add this label to run secure Github workflows on PRs label Sep 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@ShaileshParmar11 ShaileshParmar11 added the safe to test Add this label to run secure Github workflows on PRs label Sep 26, 2026
@gitar-bot

gitar-bot Bot commented Sep 26, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 2 closed / 2 findings

🟡 Medium risk · Usage ingestion caches Query and User lookups, altering repeated entity resolution.

Adds LRU caching for Query and User lookups during usage ingestion to eliminate redundant API calls. The implementation correctly scopes caches to individual OpenMetadata clients, handles cache misses appropriately for Queries, and fixes the EntityReference.fullyQualifiedName issue and class-level cache persistence that were blocking this change.

✅ 2 closed
✅ Bug: Missing .root on EntityReference.fullyQualifiedName in _get_user_entity

📄 ingestion/src/metadata/ingestion/stage/table_usage.py:102-104
get_cached_user_reference returns an EntityReference, whose fullyQualifiedName is a FullyQualifiedEntityName RootModel (accessed via .root), not a plain str — confirmed by table_ref.fullyQualifiedName.root in test_table_entity.py and model_str(entity_reference.fullyQualifiedName) in metadata_rest.py. The old code correctly used user.fullyQualifiedName.root, but the new code returns [user_reference.fullyQualifiedName], i.e. a list containing the model object instead of the FQN string. This value is fed into CreateQueryRequest(users=...), which expects a list of strings, so every query with a matched user will either raise a pydantic ValidationError (dropping the record via the try/except in _handle_table_usage) or persist a malformed user reference. Restore .root (or wrap with model_str).

✅ Bug: Class-level caches persist across ingestion runs, leaking stale data

📄 ingestion/src/metadata/ingestion/ometa/mixins/user_mixin.py:78 📄 ingestion/src/metadata/ingestion/ometa/mixins/query_mixin.py:54
_query_cache and _user_reference_cache are defined as class attributes, so they are shared across all mixin/OpenMetadata instances and, more importantly, persist for the lifetime of the process rather than a single ingestion run. In a long-lived process that runs multiple usage pipelines (e.g. a scheduler/worker), a cached Query or a cached User miss from one run leaks into subsequent runs: a user or query created between runs will never be picked up because the negative result stays cached. Consider making the caches per-instance (instance attribute set in __init__) or explicitly clearing them at the start/end of each run via the existing LRUCache.clear().

Review coverage

📋 Rules No rules evaluated

🧪 Functional validation Not enabled · Set up

🤖 Auto-approval Not enabled · Set up

Options

Display: compact → Counting what did not apply, without listing it.

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

@sonarqubecloud

Copy link
Copy Markdown

@mohittilala
mohittilala added this pull request to the merge queue Sep 26, 2026
Merged via the queue into open-metadata:main with commit 1d71430 Sep 26, 2026
182 of 192 checks passed
@OmRajput17
OmRajput17 deleted the fix-usage-caching-32210 branch October 8, 2026 05:24

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

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.

Usage ingestion looks up the same Query and user once per table

5 participants