[ 
https://issues.apache.org/jira/browse/HDFS-17813?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18063866#comment-18063866
 ] 

ASF GitHub Bot commented on HDFS-17813:
---------------------------------------

balodesecurity opened a new pull request, #8313:
URL: https://github.com/apache/hadoop/pull/8313

   ## 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:
   
   ```java
   // 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:
   
   ```java
   // 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.




> NameCache promote wrong name to cache map
> -----------------------------------------
>
>                 Key: HDFS-17813
>                 URL: https://issues.apache.org/jira/browse/HDFS-17813
>             Project: Hadoop HDFS
>          Issue Type: Bug
>          Components: namenode
>            Reporter: khazhen
>            Priority: Minor
>              Labels: pull-request-available
>
> The NameCache class is used to cache frequently used names in namenode, it 
> promotes a name used more than useThreshold to the cache, the promote logic:
> {code:java}
> K put(final K name) {
>   K internal = cache.get(name);
>   if (internal != null) {
>     lookups++;
>     return internal;
>   }
>   // Track the usage count only during initialization
>   if (!initialized) {
>     UseCount useCount = transientMap.get(name);
>     if (useCount != null) {
>       useCount.increment();
>       if (useCount.get() >= useThreshold) {
>         promote(name); // name got promoted
>       }
>       return useCount.value;
>     }
>     useCount = new UseCount(name);
>     transientMap.put(name, useCount);
>   }
>   return null;
> } {code}
> When promoting, the cache stores the `name` parameter from put() instead of 
> the existing useCount.value. This causes the returned value to change after a 
> name is promoted, resulting in memory duplication.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to