Repository navigation
OAK-12450: Fix concurrency and memory-retention issues in ThreadSpecificSegmentBufferWriterPool - #3205
Open
lweitzendorf wants to merge 1 commit into
Open
OAK-12450: Fix concurrency and memory-retention issues in ThreadSpecificSegmentBufferWriterPool#3205lweitzendorf wants to merge 1 commit into
lweitzendorf wants to merge 1 commit into
Conversation
Hardens `ThreadSpecificSegmentBufferWriterPool` in `oak-segment-tar`. Review surfaced two concurrency defects plus a memory-retention issue in the read/write-lock based pool used on the segment write path. ### Changes - **Read-lock leak (correctness).** `execute()` obtained the per-thread writer *before* the `try`, so an exception while creating the writer (e.g. inside `computeIfAbsent` / `newWriter`) leaked the read lock and would block a later `flush()` (write lock) forever. The writer is now fetched inside the `try/finally`. - **`writerId` data race (correctness).** `writerId` was a plain `short` mutated with `++`. In the thread-specific pool, `newWriter` runs inside `computeIfAbsent`'s mapping function, which `ConcurrentHashMap` can invoke concurrently for distinct keys — an unsynchronized read-modify-write that can lose updates or produce duplicate writer ids. Now an `AtomicInteger` with a bounded `getAndUpdate`. - **Dead-thread pinning (memory).** The pool was keyed by the `Thread` object, holding it (and its `ThreadLocal`s) alive until the next `flush()`. Now keyed by thread id, so dead threads are collectable immediately. Thread ids are unique among live threads, so concurrent `execute()` calls never share a writer; a recycled id simply reuses a dead thread's writer, which is safe. ### Not included Avoiding the per-call key/lambda allocation in `getWriter` is deferred pending profiling; the allocation is small relative to the lock acquire and the write operation itself. ### Testing `mvn test -pl oak-segment-tar -Dtest='SegmentBufferWriterPoolTest,SegmentBufferWriterPoolMonitorTest,SingleSegmentBufferWriterPoolTest'` — 19 tests pass. Module compiles clean under `--release 17`.
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.
https://issues.apache.org/jira/browse/OAK-12450
Summary
Fixes three issues in
ThreadSpecificSegmentBufferWriterPool:execute()obtained the per-thread writer before enteringtry/finally. If creating the writer threw, the read lock was never released and a laterflush()blocked forever. The writer is now obtained insidetry/finally.writerId:writerIdwas a plainshortincremented insidecomputeIfAbsent's mapping function, which can run concurrently for different keys. It is now anAtomicIntegerupdated with a boundedgetAndUpdate.Thread, retaining terminated threads and theirThreadLocals until the nextflush(). It is now keyed by thread ID. IDs are unique among live threads, so concurrentexecute()calls never share a writer; a recycled ID simply reuses a dead thread's writer.Tests
New cases in
SegmentBufferWriterPoolTest.