Repository navigation
Fix computeIfAbsent resetting expireAfterWrite for existing entries - #8828
Open
Sanjays2402 wants to merge 1 commit into
Open
Sanjays2402 wants to merge 1 commit into
Sanjays2402 wants to merge 1 commit into
Conversation
agammann
reviewed
Nov 5, 2026
agammann
left a comment
There was a problem hiding this comment.
Re-adding e at the tail of writeQueue while retaining its old write time breaks the queue's timestamp order. expireEntries() only removes expired entries from the head, so an older entry can sit behind a newer, unexpired one. For example, with concurrencyLevel(1) and expireAfterWrite(10 minutes): put A at t=0, B at t=5, then computeIfAbsent(A, ...) at t=6. At t=11, cleanUp() sees B at the head and leaves expired A counted in size(). Could this preserve A's write-queue position, and add a two-entry FakeTicker test for cleanup order?
This branch has not been deployed
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.
Fixes #3700
Calling
cache.asMap().computeIfAbsent()for a key that is already presentreset the entry's write timestamp, so with
expireAfterWritethe entryeffectively never expired as long as
computeIfAbsentkept being called.That contradicts the
Mapcontract forcomputeIfAbsent, which leaves anexisting mapping untouched, and it made an
expireAfterWritecache behavelike an
expireAfterAccessone for those keys.The cause is in
Segment.compute: when the remapping function returns thevery same value that is already stored, the code restored the old value
reference but still recorded a write, which updates the write timestamp.
This change records a read instead, which refreshes the access time but
leaves the write time alone, and re-adds the entry to the write queue so
it can still expire. The same branch covers
compute,computeIfPresentand
mergereturning the unchanged value, which get the same treatment.Repro, before the fix (a manual
Ticker,expireAfterWriteof 10minutes):
putat t=0,computeIfAbsenton the existing key at t=9,then
getIfPresentat t=11 returned the value. After the fix it returnsnull. A second check confirms
computeIfAbsentstill refreshes theaccess time for
expireAfterAccesscaches.Tests: added
testComputeIfAbsentDoesNotRefreshWriteTimeandtestComputeIfAbsentStillRefreshesAccessTimetoLocalCacheMapComputeTest.Ran
LocalCacheMapComputeTest,CacheExpirationTest,LocalCacheTestand
CacheEvictionTest: 10,370 tests, all passing.