Skip to content

Fixes #35428: Stop cache-miss loads pinning virtual threads - #35429

Merged
yan-3005 merged 5 commits into
mainfrom
yan-3005/vthread-pinning-cache-loads
Oct 15, 2026
Merged

yan-3005 merged 5 commits into
mainfrom
yan-3005/vthread-pinning-cache-loads

Conversation

@yan-3005

@yan-3005 yan-3005 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Describe your changes:

Fixes #35428

Follow-up to #35374. An entity-cache miss in EntityRepository.find / findByName now loads outside Guava's lock.

Why: CACHE_WITH_ID.get(key) / CACHE_WITH_NAME.get(key) run the loader's Redis and database reads inside a Guava synchronized. On JDK 21 a virtual thread blocked there stays pinned to its carrier thread. In six merge-queue stalls of the Postgres + OpenSearch IT lane, all 4 carriers were pinned. Most of those pins came from Jdbi (fixed in #35374); 3 of the 24 were this Guava load.

Change: a small loadOutsideCacheLock helper in EntityRepository:

  1. read the write epoch;
  2. run the existing loader;
  3. put the result;
  4. re-check the epoch. If a write landed meanwhile, evict and throw LoaderRaceException, which find already handles by retrying through the bypass read.

Failures come back wrapped in ExecutionException, as LoadingCache.get throws them, so the existing catch blocks are unchanged. The loaders stay attached to the caches, so direct CACHE_WITH_*.get callers behave as before.

Trade-off: concurrent misses for the same entity may now each run the loader once, where Guava made them wait for one load. Redis L2 sits in front of the database, so the extra reads are usually cheap.

Type of change:

  • Bug fix

Tests:

Unit tests

EntityCacheVirtualThreadTest (5 tests):

  • a miss loaded on a virtual thread records no jdk.VirtualThreadPinned JFR event;
  • control: the old CACHE_WITH_ID.get does record one, so the first check can't pass vacuously;
  • a write during the load caches nothing;
  • a write between the load and the put evicts what was cached;
  • a failed load doesn't block the next miss.

The existing tests around the entity cache pass: Tests run: 165, Failures: 0, Errors: 0.

Backend integration tests

Run locally on MySQL + Elasticsearch, before the strip-down, against the same find / findByName load path:

  • Parallel lane: EntityCacheInvalidationIT, EntityCacheMemoryIT, RestoreHierarchyIT, EntityLifecycleStagesIT, EntityStatusWorkflowOwnershipIT, AuditLogResourceIT, BotResourceIT, DomainResourceIT. Tests run: 533, Failures: 0, Errors: 0, Skipped: 55.
  • Cache-tests lane (Redis): Tests run: 39, Failures: 0, Errors: 0, Skipped: 1.

The full IT suite (MySQL + ES, PostgreSQL + OS) is running in CI on this head.

Collate CI (maven-collate-ci) stays red until open-metadata/openmetadata-collate#7428 (Jdbi 3.49.5) merges. That failure is unrelated to this change.

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.
  • I have added tests (unit / integration / Playwright as applicable) and listed them above.
  • Decision check: adds docs/decisions/2026-10-14-cache-misses-that-do-io-load-outside-the-cache-lock.md.

🤖 Generated with Claude Code

On JDK 21 a virtual thread that blocks while holding a monitor keeps its
carrier thread. Guava LoadingCache.get, Caffeine get and
ConcurrentHashMap.compute all run the loader inside synchronized, so a
cache miss that reads Redis or the database pins the thread. In six
merge-queue stalls of the Postgres + OpenSearch IT lane all four carriers
of the runner were pinned; 3 of the 24 pinned threads were in the entity
cache's Guava load (the rest were Jdbi, fixed by #35374).

Add SingleFlight: runs a miss on the caller's thread with no monitor held,
at most one load per key, other callers waiting on a CompletableFuture.

- EntityRepository find/findByName load misses through SingleFlight and
  re-check the write epoch after the put, evicting if a write raced the
  load. Loaders stay attached so direct CACHE_WITH_* callers are unchanged.
- AuditLogRepository, OwnerResolver, RdfPropertyMapper: read, then put.
- EntityStatusWorkflows: SingleFlight plus an invalidation counter so a
  workflow change during a load is not hidden by what the load read.
- StageStatsTracker.flush: ReentrantLock instead of synchronized.
Copilot AI balanced review requested due to automatic review settings October 14, 2026 09:22
@yan-3005
yan-3005 requested a review from a team as a code owner October 14, 2026 09:22

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

github-actions Bot commented Oct 14, 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 github-actions Bot added backend safe to test Add this label to run secure Github workflows on PRs labels Oct 14, 2026
Only the entity cache's Guava load showed up pinned in the stall dumps.
Load the miss with a plain read, put and epoch re-check in EntityRepository
instead of a new SingleFlight class, and revert the preventive changes to
the audit bot cache, OwnerResolver, RdfPropertyMapper, EntityStatusWorkflows
and StageStatsTracker, plus the decision record.
Copilot AI balanced review requested due to automatic review settings October 14, 2026 10:38

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI balanced review requested due to automatic review settings October 14, 2026 10:53

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@gitar-bot

gitar-bot Bot commented Oct 14, 2026

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

🟡 Medium risk · Entity-cache misses now load outside Guava locks, allowing duplicate Redis/database reads that could increase backend load during concurrent misses.

Stops cache-miss entity loads in EntityRepository.find and findByName from pinning virtual threads by running the loader outside Guava's lock, with an epoch re-check that evicts and retries on concurrent writes. The previously open finding about the missing load-outside-cache-lock rule was resolved after the ADR was removed. No open issues found.

✅ 1 closed
✅ Quality: Load-outside-cache-lock rule no longer recorded after ADR deletion

📄 openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java:397-411 📄 openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java:1988-1995 📄 openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java:2642-2649
This commit deletes docs/decisions/2026-10-14-cache-misses-that-do-io-load-outside-the-cache-lock.md along with SingleFlight. The rule it recorded still holds in loadOutsideCacheLock, and no record in docs/decisions/ states it now. The description gives the rule in prose: a miss "loads outside Guava's lock", the epoch is re-checked after the put, and the trade-off is that "concurrent misses for the same entity may now each run the loader once". The repo squash-merges, so none of this reaches git log. A later change, such as the planned Caffeine migration, could go back to CACHE_WITH_*.get and bring the pinning back with nothing in the tree to warn against it.

The record should say: Code that can run on a virtual thread does no blocking I/O inside a cache loader, compute function or synchronized block. An entity-cache miss reads the epoch, loads, puts, then re-checks the epoch and evicts if a write landed. Concurrent misses for one key may each run the loader. Fix: restore the ADR with the SingleFlight bullet removed and the duplicate-load trade-off stated. Its Guard should list only EntityCacheVirtualThreadTest. Optionally, cite it as ADR:2026-10-14-... in the loadOutsideCacheLock Javadoc.

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 added this pull request to the merge queue Oct 14, 2026
Merged via the queue into main with commit 5834684 Oct 15, 2026
79 checks passed
@yan-3005
yan-3005 deleted the yan-3005/vthread-pinning-cache-loads branch October 15, 2026 01:04
harshach added a commit that referenced this pull request Oct 15, 2026
Main's #35429 moved find/findByName L1 misses out of the cache lock
(loadOutsideCacheLock: read epoch, load, put, re-check epoch) because a
loading get, Guava's or Caffeine's, pins virtual threads
(ADR:2026-10-14-cache-misses-that-do-io-load-outside-the-cache-lock).
That replaces this branch's Guava-to-Caffeine L1 switch, so keep main's
find/findByName, L1 builders, COUNT_CACHE and loaders, and drop the
Caffeine-only test.

Keep the L1-after-Redis eviction order in invalidateCachesAfterStore, and
bump the write epochs again just before the L1 eviction there and in
evictCommittedWrites. Without a loading get, an eviction no longer waits
for a load in flight: a miss that read its epoch after the first bump and
fetched the stale Redis copy before the delete could put it after the L1
eviction and still pass its re-check. EntityCacheLoadRaceTest covers both
paths and fails without the second bump.
manerow added a commit that referenced this pull request Nov 23, 2026
… (#38520)

* Fixes #35373: Stop Jdbi pinning virtual threads and the test suite summary 404 on deleted test cases (#35374)

* Build Jdbi SQL object handlers on first call, not on every attach (#35469)

* Fixes #35428: Stop cache-miss loads pinning virtual threads (#35429)

---------

Co-authored-by: Mohit Yadav <[email protected]>
Co-authored-by: Ram Narayan Balaji <[email protected]>
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.

Cache-miss loads still pin virtual threads after the Jdbi upgrade

3 participants