Repository navigation
HADOOP-19918. Reduce lock contention in MutableQuantiles.add() - #8969
Open
Abhi0505-kinagi wants to merge 1 commit into
Open
Abhi0505-kinagi wants to merge 1 commit into
Abhi0505-kinagi wants to merge 1 commit into
Conversation
Abhi0505-kinagi
force-pushed
the
HADOOP-19918-quantiles-contention
branch
from
November 11, 2026 15:01
5c25535 to
89a3c92
Compare
|
🎊 +1 overall
This message was automatically generated. |
|
🎊 +1 overall
This message was automatically generated. |
This branch has not been deployed
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.
HADOOP-19918. Reduce lock contention in MutableQuantiles.add() and SampleQuantiles.
Description of PR
JIRA: https://issues.apache.org/jira/browse/HADOOP-19918
MutableQuantiles.add()wassynchronizedand calledSampleQuantiles.insert(), which was alsosynchronized. Every 500th insert ran the sort, merge and compress under both monitors, so all callers ofadd()waited behind that batch work.Changes
SampleQuantileslong[500]active buffer under a shortstateLock.flushLock, outsidestateLock. At most one flush runs at a time, and a producer waits only if both buffers are busy.samplesis anArrayList. The batch merge is a single forward pass into a reusedmergeBuffer(the two lists are swapped after each flush) instead of per-elementListIteratorinserts.snapshot()drains the partial buffer and queries using the count captured at that point.clear()waits for an in-flight flush. Waiting is done onstateLockwithout holdingflushLock, so there is no busy-spin.MutableQuantilesadd()takes the shared read lock of a newestimatorLockand inserts directly when the estimator is aSampleQuantiles. Other estimators keep the old behaviour (write lock plus monitor), since they are not assumed thread-safe.setEstimator()take the write lock, so rollover waits for in-flightadd()calls. Rollover reads the count, snapshots, clears, and publishespreviousCountandpreviousSnapshottogether under the monitor.snapshot(builder, all)stays plainsynchronizedand only reads the published pair.estimatorLock, then theMutableQuantilesmonitor. Callers must not hold the monitor when callingadd().@VisibleForTestinghooks aregetPreviousCount(),getPreviousSnapshot()andrunRolloverForTesting().How was this patch tested?
Run on JDK 17, Maven 3.9:
New tests:
TestMutableMetrics#testRolloverWaitsForInFlightInsert: a gatedSampleQuantilessubclass holds an insert in flight, and rollover must block on the write lock. The value then appears exactly once.TestMutableMetrics#testRolloverConservesConcurrentInsertions: 4 producers x 1000 values against repeated rollovers. The total rolled count must equal 4000.TestSampleQuantiles:testConcurrentInsertAndSnapshot,testClearDuringConcurrentActivity,testFinalBatchDrainingAndRepeatedBufferReuse,testDuplicateValuesAcrossBatches.Performance (estimator-level microbenchmark)
This is a temporary JMH 1.20 harness calling
SampleQuantiles.insert()directly. It uses 1M deterministic random longs, 5 quantiles,-Xms2g -Xmx2g, 3 warmups, 8 measurements and 2 forks. Throughput is shown as ops/s ± JMH error, with trunk5838d522b75as the baseline.JMH
gc.alloc.rate.normwas about 60 B/op on trunk and about 36 to 39 B/op with the patch. I have not profiled why.REPLACE: add a matched trunk-vs-patch table for the real
MutableQuantiles.add()path (1/4/8/16 threads, with and without rollover), run with the same harness and at least 3 forks. Without it, this PR makes no measured claim about the IPC-handler contention itself.Notes for reviewers
previousCountandpreviousSnapshotfrom different windows.compress()now callsArrayList.remove, which is O(n) per removal whereLinkedListwas O(1). The benchmark above covers only one stream shape.