HDFS-17813. NameCache promotes wrong object reference on threshold crossing#8313
Open
balodesecurity wants to merge 1 commit intoapache:trunkfrom
Open
HDFS-17813. NameCache promotes wrong object reference on threshold crossing#8313balodesecurity wants to merge 1 commit intoapache:trunkfrom
balodesecurity wants to merge 1 commit intoapache:trunkfrom
Conversation
…ossing. In NameCache.promote(), cache.put(name, name) stored the caller's argument as the cached value instead of useCount.value (the original object captured when the entry was first inserted into the transient map). This caused two distinct heap objects to exist for the same logical name, defeating the purpose of the cache (memory deduplication / object identity reuse). Fix: pass UseCount to promote() and store useCount.value as the cache value. Added testPromotionPreservesObjectIdentity to TestNameCache to assert that the object returned after promotion is the same reference (assertSame) as the one originally inserted.
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.
Problem
In
NameCache.put(), when a name's use count crosses the promotion threshold, the cache was populated with the caller'snameargument rather than the originaluseCount.valuethat was stored in the transient map:This means after promotion the cache holds a different object reference than what was stored before promotion. Any caller that held a reference to the pre-promotion value now has a stale reference, and two distinct
Stringobjects exist for what should be a single cached identity — wasting memory and breaking reference equality.Fix
Pass
useCountinto thepromote()helper and storeuseCount.valueinstead:This preserves the original object reference across the promotion boundary.
Testing
TestNameCache#testPromotionPreservesObjectIdentity: creates aNameCachewith threshold 2, inserts a non-internedString, triggers promotion, and usesassertSameto verify the returned reference is identical before and after promotion.