Skip to content

HDFS-17813. NameCache promotes wrong object reference on threshold crossing#8313

Open
balodesecurity wants to merge 1 commit intoapache:trunkfrom
balodesecurity:HDFS-17813
Open

HDFS-17813. NameCache promotes wrong object reference on threshold crossing#8313
balodesecurity wants to merge 1 commit intoapache:trunkfrom
balodesecurity:HDFS-17813

Conversation

@balodesecurity
Copy link

Problem

In NameCache.put(), when a name's use count crosses the promotion threshold, the cache was populated with the caller's name argument rather than the original useCount.value that was stored in the transient map:

// Before (buggy)
cache.put(name, name);

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 String objects exist for what should be a single cached identity — wasting memory and breaking reference equality.

Fix

Pass useCount into the promote() helper and store useCount.value instead:

// After
cache.put(name, useCount.value);

This preserves the original object reference across the promotion boundary.

Testing

  • Added TestNameCache#testPromotionPreservesObjectIdentity: creates a NameCache with threshold 2, inserts a non-interned String, triggers promotion, and uses assertSame to verify the returned reference is identical before and after promotion.
  • Test passes locally.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants