Repository navigation
perf(db): let SnapshotRoot cache refills share a read lock - #7104
Open
halibobo1205 wants to merge 1 commit into
Open
halibobo1205 wants to merge 1 commit into
halibobo1205 wants to merge 1 commit into
Conversation
halibobo1205
requested review from
317787106,
bladehan1,
waynercheung and
xxo1shine
November 12, 2026 03:12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Follow-up to #7078. Replaces the
SnapshotRootcache monitor with aReentrantReadWriteLock.Why are these changes required?
#7078 puts every cache-miss refill of a store behind one monitor. Before it, Guava's segments let those refills run in parallel. A refill only has to exclude the generation advance, not other refills.
Local microbenchmark, not node-level: JDK 17 on a 10-core Apple Silicon machine, Guava 33.6.0 with the account / storage-row cache spec (
maximumSize=20000, expireAfterAccess=30s, concurrencyLevel=CPUS), a cold-key workload with approximately 99% cache misses, the DB read is a busy-wait, and the writer advances the generation every 50 ms. In production, caught-up block processing normally refreshes each store once per block, roughly every 3 seconds; catch-up and startup have different write patterns.This PR has been tested by:
SnapshotRootCacheRaceTest(22 cases, rerun 5 times), theorg.tron.core.db2package,TronCacheTest,AccountAssetStoreTest,BandwidthProcessorTest.Follow up
None.
Extra details
Test changes:
staleReadWaitingOnLockIsSkippedholds the write lock and waits until the reader is queued on it (hasQueuedThread). A thread waiting on ajava.util.concurrentlock isWAITING, notBLOCKED.putWaitsForInFlightRefill/mergeListWaitsForInFlightRefilldetect the queued writer the same way.concurrentRefillsDoNotBlockEachOther: a refill completes and is cached while another refill holds the read lock.assertUnlockedchecks that no read or write hold is left after a refill.park()now fails if it is never resumed, so a missed interleaving cannot pass silently.Alternatives considered
ReentrantLockwithtryLock, skipping a refill that finds the lock taken. Refills do not wait for the SnapshotRoot cache lock, but colliding refills are dropped (51–99% in the same benchmark). Dropped refills can cause additional DB reads if those keys are accessed again before being cached. Its higher raw throughput comes from skipping the cache put, which matters less as DB latency grows. Its writer p99 at 10 threads was also higher (0.7–4.2 ms).readLock().tryLock()without a timeout. It bypasses the reader admission policy and can barge past a queued writer, so a steady stream of refills could keep the flush thread waiting.readLock().tryLock(0, TimeUnit.NANOSECONDS). With the default nonfair lock it follows the same reader admission policy aslock(), which is not strictly fair. It needsInterruptedExceptionhandling, and its writer wait was the longest of all variants (p50 1–39 ms). A refill that captured the generation before the writer's advance fails the recheck after waiting anyway, so skipping it would not change what that refill caches.