HDDS-15357. Fix MappedBufferManager WeakReference races#10351
Draft
smengcl wants to merge 1 commit into
Draft
Conversation
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 changes were proposed in this pull request?
MappedBufferManagercaches mapped buffers asWeakReference<ByteBuffer>and tracks in-flight quota via aSemaphore. Two races on master:computeIfAbsentGC race: The cache-hit branch readsrefer.get()once for a null check and a second time at the return. Between the two reads, theWeakReferenceis the only handle to the buffer. GC may clear it, the second read returnsnull, andChunkUtils.readDatathen dereferences a nullIOException(throw null).Cleanup-loop races in
getQuota: The async loop walksmappedBuffers.keySet()and re-looks up each key. A concurrentremove()makes the lookup returnnulland the chained.get()NPEs. A concurrentput()that replaces theWeakReferencemakes the unconditionalmappedBuffers.remove(key)drop someone else's fresh entry.pis incremented for it, andreleaseQuota(p)over-releases the semaphore.Fix
computeIfAbsentresolvesrefer.get()once into a localfinal ByteBuffer cachedand returns that. The stack-local strong reference keeps the buffer reachable until the method returns.ConcurrentHashMap.remove(key, value)so a concurrentput()that replaced theWeakReferencereturnsfalsefrom the conditional remove and is not counted as freed quota.What is the link to the Apache JIRA
https://issues.apache.org/jira/browse/HDDS-15357
How was this patch tested?