GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||
GeezerVison is showing github.com — Add AsyncDrainWriter to eliminate PrintWriter lock contention by gnodet · Pull Request #12654 · apache/maven Conversation
Contributor
SummaryDuring parallel model building ( Since Fix
JFR Results (4383-module diamond project,
|
| Source | Before | After |
|---|---|---|
| PrintWriter contention | 1,470ms | 0ms |
| PhasingExecutor contention | 546ms | 49ms |
| Metric | Before | After | Δ |
|---|---|---|---|
| Median | 14.738s | 14.253s | -485ms (-3.3%) |
| Average | 14.546s | 14.242s | -304ms (-2.1%) |
| Range (variance) | 2.70s | 1.33s | Halved |
All 8,766 module log lines verified present — zero messages lost.
maven-core unit tests passmaven-cli unit tests pass🤖 Generated with Claude Code
Sorry, something went wrong.
There was an error while loading. Please reload this page.
During parallel model building, multiple PhasingExecutor threads log concurrently via SLF4J → ProjectBuildLogAppender → SimpleBuildEventListener → PrintWriter.println(). Since PrintWriter.println() is synchronized, this creates significant contention (1,470ms blocked time on a 4383-module project with -T1C). AsyncDrainWriter wraps the writer Consumer with a lock-free ConcurrentLinkedQueue and a non-blocking tryLock() drain pattern: - Producers enqueue messages (CAS, no blocking) - At most one thread drains the queue to the underlying PrintWriter - Other threads return immediately after enqueue - close() performs a final blocking drain to ensure no messages are lost JFR results on 4383-module diamond project (validate -T1C): - PrintWriter contention: 1,470ms → 0ms (eliminated) - PhasingExecutor contention: 546ms → 49ms (side effect) - Median wall time: -485ms (-3.3%) - Run-to-run variance halved (range 2.70s → 1.33s) Co-Authored-By: Claude Opus 4.6 <[email protected]>
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Clean, well-motivated performance optimization with a correct lock-free drain algorithm. The ConcurrentLinkedQueue + tryLock drain + single re-check pattern is well-established for reducing contention on a shared output sink.
The 1,470ms-to-0ms PrintWriter contention elimination is a compelling result backed by thorough JFR profiling data.
One suggestion: consider adding unit tests for AsyncDrainWriter — a concurrent component should have at least basic tests for (1) single-threaded message ordering, (2) multi-threaded stress test verifying no messages are lost, and (3) close() draining remaining buffered messages.
Also, if delegate.accept(m) throws during drain(), subsequent queued messages are silently dropped. In practice this is unlikely since PrintWriter.println() swallows IOExceptions, but a try/catch in drain() would make the component more robust.
Note: Cannot submit as APPROVE because the PR author matches the review account.
This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.
Claude Code on behalf of gnodet
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Could we ditch the PrintWriter completely?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
And go where ? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Could you just use an OutputStreamWriter instead? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Whatever class we use is irrelevant. We need to ensure the lines are written one after the other, and that we don't have a mix of lines, whether we write on a file or on a terminal. This requires synchronisation at some point on a single resource (the terminal or file). The point of this PR is to offload the synchronisation in a separate thread, so that we don't block all maven threads while writing. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Review: Add AsyncDrainWriter to eliminate PrintWriter lock contention
Well-designed lock-free drain pattern that correctly eliminates PrintWriter contention. Thread safety is sound, close ordering is correct (LIFO: stop sink → drain buffered → close writer), and CI is fully green. The JFR profiling data is compelling — eliminating 1,470ms of contention on a large parallel build is a meaningful win.
Confirmed findings:
[medium] No unit tests for AsyncDrainWriter — This class has a non-trivial concurrent algorithm (the tryLock/re-check pattern) that would benefit from automated tests: (a) single-threaded ordering verification, (b) multi-threaded stress test for no-message-loss under contention, and (c) a test that close() drains all remaining buffered messages. While the manual validation (8,766/8,766 log lines verified) is thorough, automated tests would guard against future regressions.
[low] Exception in drain() loses the polled message — In drain(), if delegate.accept(m) throws, the message already poll()-ed from the queue is lost. Remaining messages stay queued for the next drain cycle. Not a practical concern for PrintWriter.println() (which swallows IOExceptions), but since AsyncDrainWriter accepts any Consumer<String>, a try/catch around the accept in drain() that continues the loop would make this more robust as a general-purpose utility.
[low] Bounded message stranding window — After the re-check drain completes and releases the lock, there is no further re-check. A message enqueued in the narrow window between the last poll() returning null and unlock() can sit in the queue until the next accept() call or close(). The class Javadoc already says "picked up by the ongoing or next drain cycle," which implicitly acknowledges eventual delivery. Consider making this more explicit for future maintainers.
This review was generated by an AI agent (Claude Code) and may contain inaccuracies. Please verify all suggestions before applying.
Claude Code on behalf of Guillaume Nodet
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Review — sound concurrency design, missing tests
The lock-free drain pattern is well-designed: ConcurrentLinkedQueue + ReentrantLock.tryLock() ensures at most one thread drains at a time, and the post-unlock re-check prevents messages from being stranded by the enqueue-during-drain race. Thread safety is sound — all fields are final, ConcurrentLinkedQueue and ReentrantLock provide proper happens-before guarantees.
Close ordering via context.closeables LIFO is correct: AsyncDrainWriter closes first (draining buffered messages), then the underlying PrintWriter.
🟡 No unit tests (medium) — A concurrency-critical class like this would benefit from tests verifying: (1) concurrent producers don't lose messages (N threads × M messages), and (2) close() drains remaining messages. This guards against future regressions.
Minor observations (non-blocking):
@since 4.0.0 should be @since 4.1.0 (project version is 4.1.0-SNAPSHOT, milestone is 4.1.0)delegate.accept() throws during drain(), the polled message is lost and remaining messages are not drained. Practically near-impossible since PrintWriter swallows IOExceptions, but a try-catch in drain() would add resilienceThis review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.
Claude Code on behalf of Guillaume Nodet
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
#12892) During parallel model building, multiple PhasingExecutor threads log concurrently via SLF4J → ProjectBuildLogAppender → SimpleBuildEventListener → PrintWriter.println(). Since PrintWriter.println() is synchronized, this creates significant contention (1,470ms blocked time on a 4383-module project with -T1C). AsyncDrainWriter wraps the writer Consumer with a lock-free ConcurrentLinkedQueue and a non-blocking tryLock() drain pattern: - Producers enqueue messages (CAS, no blocking) - At most one thread drains the queue to the underlying PrintWriter - Other threads return immediately after enqueue - close() performs a final blocking drain to ensure no messages are lost JFR results on 4383-module diamond project (validate -T1C): - PrintWriter contention: 1,470ms → 0ms (eliminated) - PhasingExecutor contention: 546ms → 49ms (side effect) - Median wall time: -485ms (-3.3%) - Run-to-run variance halved (range 2.70s → 1.33s) Co-authored-by: Claude Opus 4.6 <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.