Skip to content

HADOOP-19918. Reduce lock contention in MutableQuantiles.add() - #8969

Open
Abhi0505-kinagi wants to merge 1 commit into
apache:trunkfrom
Abhi0505-kinagi:HADOOP-19918-quantiles-contention
Open

Abhi0505-kinagi wants to merge 1 commit into
apache:trunkfrom
Abhi0505-kinagi:HADOOP-19918-quantiles-contention

Conversation

@Abhi0505-kinagi

@Abhi0505-kinagi Abhi0505-kinagi commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

HADOOP-19918. Reduce lock contention in MutableQuantiles.add() and SampleQuantiles.

Description of PR

JIRA: https://issues.apache.org/jira/browse/HADOOP-19918

MutableQuantiles.add() was synchronized and called SampleQuantiles.insert(), which was also synchronized. Every 500th insert ran the sort, merge and compress under both monitors, so all callers of add() waited behind that batch work.

Changes

SampleQuantiles

  • Producers append to a preallocated long[500] active buffer under a short stateLock.
  • A full buffer is swapped with a second preallocated buffer and flushed under flushLock, outside stateLock. At most one flush runs at a time, and a producer waits only if both buffers are busy.
  • samples is an ArrayList. The batch merge is a single forward pass into a reused mergeBuffer (the two lists are swapped after each flush) instead of per-element ListIterator inserts.
  • snapshot() drains the partial buffer and queries using the count captured at that point. clear() waits for an in-flight flush. Waiting is done on stateLock without holding flushLock, so there is no busy-spin.

MutableQuantiles

  • add() takes the shared read lock of a new estimatorLock and inserts directly when the estimator is a SampleQuantiles. Other estimators keep the old behaviour (write lock plus monitor), since they are not assumed thread-safe.
  • Rollover and setEstimator() take the write lock, so rollover waits for in-flight add() calls. Rollover reads the count, snapshots, clears, and publishes previousCount and previousSnapshot together under the monitor.
  • snapshot(builder, all) stays plain synchronized and only reads the published pair.
  • Lock order is estimatorLock, then the MutableQuantiles monitor. Callers must not hold the monitor when calling add().
  • There are no public API changes. The package-private @VisibleForTesting hooks are getPreviousCount(), getPreviousSnapshot() and runRolloverForTesting().
flowchart LR
  P[add threads] -->|read lock| A[SampleQuantiles.insert]
  A -->|stateLock: append| B[active buffer]
  B -->|full: swap| C[flushLock: sort, merge, compress]
  R[rollover thread] -->|write lock: waits for add| S[count, snapshot, clear, publish]
  M[metrics poll] -->|synchronized| Q[read previousCount and previousSnapshot]
Loading

How was this patch tested?

Run on JDK 17, Maven 3.9:

mvn -pl hadoop-common-project/hadoop-common \
  -Dtest=TestSampleQuantiles,TestMutableMetrics \
  -Dsurefire.failIfNoSpecifiedTests=false test
# Tests run: 27, Failures: 0, Errors: 0  (TestMutableMetrics 19, TestSampleQuantiles 8)

mvn -pl hadoop-common-project/hadoop-common \
  -Dtest='org.apache.hadoop.metrics2.**' -Dsurefire.failIfNoSpecifiedTests=false test
# Tests run: 138, Failures: 0, Errors: 0, Skipped: 0

git diff --check   # clean

New tests:

  • TestMutableMetrics#testRolloverWaitsForInFlightInsert: a gated SampleQuantiles subclass 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 trunk 5838d522b75 as the baseline.

Workload Trunk Patch Result
Insert, 1 thread 1.870M ± 0.109M 1.929M ± 0.043M within noise
Insert, 4 threads 1.728M ± 0.139M 1.871M ± 0.059M within noise
Insert, 8 threads 1.620M ± 0.157M 1.801M ± 0.073M within noise
Insert, 16 threads 1.518M ± 0.064M 1.775M ± 0.063M +17%
Insert + snapshot, 4 threads 1.182M ± 0.093M 1.286M ± 0.039M within noise
Insert + snapshot, 8 threads 1.245M ± 0.084M 1.215M ± 0.079M within noise

JMH gc.alloc.rate.norm was 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

  • Rollover still holds the write lock while it snapshots and clears. This is deliberate: releasing it early would let a reader see previousCount and previousSnapshot from different windows.
  • compress() now calls ArrayList.remove, which is O(n) per removal where LinkedList was O(1). The benchmark above covers only one stream shape.
  • The duplicate-value merge and the delta edge conditions were rewritten for the batch merge. Quantile error-bound tests pass, but I have not added a differential test against trunk's iterator behaviour for equal values.

@hadoop-yetus

Copy link
Copy Markdown

🎊 +1 overall

Vote Subsystem Runtime Logfile Comment
+0 🆗 reexec 0m 24s Docker mode activated.
_ Prechecks _
+1 💚 dupname 0m 0s No case conflicting files found.
+0 🆗 codespell 0m 0s codespell was not available.
+0 🆗 detsecrets 0m 0s detect-secrets was not available.
+1 💚 @author 0m 0s The patch does not contain any @author tags.
+1 💚 test4tests 0m 0s The patch appears to include 2 new or modified test files.
_ trunk Compile Tests _
+1 💚 mvninstall 29m 10s trunk passed
+1 💚 compile 9m 48s trunk passed with JDK Ubuntu-21.0.12.1+1-1-24.04.4-Ubuntu
+1 💚 compile 9m 46s trunk passed with JDK Ubuntu-17.0.20.1+1-1-24.04-Ubuntu
+1 💚 checkstyle 0m 52s trunk passed
+1 💚 mvnsite 1m 13s trunk passed
+1 💚 javadoc 0m 57s trunk passed with JDK Ubuntu-21.0.12.1+1-1-24.04.4-Ubuntu
+1 💚 javadoc 0m 55s trunk passed with JDK Ubuntu-17.0.20.1+1-1-24.04-Ubuntu
+1 💚 spotbugs 1m 54s trunk passed
+1 💚 shadedclient 17m 55s branch has no errors when building and testing our client artifacts.
_ Patch Compile Tests _
+1 💚 mvninstall 0m 48s the patch passed
+1 💚 compile 9m 14s the patch passed with JDK Ubuntu-21.0.12.1+1-1-24.04.4-Ubuntu
+1 💚 javac 9m 14s the patch passed
+1 💚 compile 9m 46s the patch passed with JDK Ubuntu-17.0.20.1+1-1-24.04-Ubuntu
+1 💚 javac 9m 46s the patch passed
+1 💚 blanks 0m 0s The patch has no blanks issues.
+1 💚 checkstyle 0m 53s the patch passed
+1 💚 mvnsite 1m 13s the patch passed
+1 💚 javadoc 0m 57s the patch passed with JDK Ubuntu-21.0.12.1+1-1-24.04.4-Ubuntu
+1 💚 javadoc 0m 55s the patch passed with JDK Ubuntu-17.0.20.1+1-1-24.04-Ubuntu
+1 💚 spotbugs 2m 2s the patch passed
+1 💚 shadedclient 18m 6s patch has no errors when building and testing our client artifacts.
_ Other Tests _
+1 💚 unit 18m 42s hadoop-common in the patch passed.
+1 💚 asflicense 0m 47s The patch does not generate ASF License warnings.
137m 34s
Subsystem Report/Notes
Docker ClientAPI=1.56 ServerAPI=1.56 base: https://ci-hadoop.apache.org/job/hadoop-multibranch/job/PR-8969/2/artifact/out/Dockerfile
GITHUB PR #8969
Optional Tests dupname asflicense compile javac javadoc mvninstall mvnsite unit shadedclient spotbugs checkstyle codespell detsecrets
uname Linux 53ac8bde4f82 5.15.0-190-generic #200-Ubuntu SMP Fri Aug 7 15:06:04 UTC 2026 x86_64 x86_64 x86_64 GNU/Linux
Build tool maven
Personality dev-support/bin/hadoop.sh
git revision trunk / 89a3c92
Default Java Ubuntu-17.0.20.1+1-1-24.04-Ubuntu
Multi-JDK versions /usr/lib/jvm/java-21-openjdk-amd64:Ubuntu-21.0.12.1+1-1-24.04.4-Ubuntu /usr/lib/jvm/java-17-openjdk-amd64:Ubuntu-17.0.20.1+1-1-24.04-Ubuntu
Test Results https://ci-hadoop.apache.org/job/hadoop-multibranch/job/PR-8969/2/testReport/
Max. process+thread count 1276 (vs. ulimit of 10000)
modules C: hadoop-common-project/hadoop-common U: hadoop-common-project/hadoop-common
Console output https://ci-hadoop.apache.org/job/hadoop-multibranch/job/PR-8969/2/console
versions git=2.43.0 maven=3.9.15 spotbugs=4.9.7
Powered by Apache Yetus 0.14.1 https://yetus.apache.org

This message was automatically generated.

@hadoop-yetus

Copy link
Copy Markdown

🎊 +1 overall

Vote Subsystem Runtime Logfile Comment
+0 🆗 reexec 0m 37s Docker mode activated.
_ Prechecks _
+1 💚 dupname 0m 0s No case conflicting files found.
+0 🆗 codespell 0m 1s codespell was not available.
+0 🆗 detsecrets 0m 1s detect-secrets was not available.
+1 💚 @author 0m 0s The patch does not contain any @author tags.
+1 💚 test4tests 0m 0s The patch appears to include 2 new or modified test files.
_ trunk Compile Tests _
+1 💚 mvninstall 44m 30s trunk passed
+1 💚 compile 16m 32s trunk passed with JDK Ubuntu-21.0.12.1+1-1-24.04.4-Ubuntu
+1 💚 compile 16m 56s trunk passed with JDK Ubuntu-17.0.20.1+1-1-24.04-Ubuntu
+1 💚 checkstyle 1m 33s trunk passed
+1 💚 mvnsite 2m 2s trunk passed
+1 💚 javadoc 1m 32s trunk passed with JDK Ubuntu-21.0.12.1+1-1-24.04.4-Ubuntu
+1 💚 javadoc 1m 30s trunk passed with JDK Ubuntu-17.0.20.1+1-1-24.04-Ubuntu
+1 💚 spotbugs 3m 11s trunk passed
+1 💚 shadedclient 31m 49s branch has no errors when building and testing our client artifacts.
_ Patch Compile Tests _
+1 💚 mvninstall 1m 15s the patch passed
+1 💚 compile 15m 56s the patch passed with JDK Ubuntu-21.0.12.1+1-1-24.04.4-Ubuntu
+1 💚 javac 15m 56s the patch passed
+1 💚 compile 16m 53s the patch passed with JDK Ubuntu-17.0.20.1+1-1-24.04-Ubuntu
+1 💚 javac 16m 53s the patch passed
+1 💚 blanks 0m 0s The patch has no blanks issues.
+1 💚 checkstyle 1m 25s the patch passed
+1 💚 mvnsite 2m 3s the patch passed
+1 💚 javadoc 1m 29s the patch passed with JDK Ubuntu-21.0.12.1+1-1-24.04.4-Ubuntu
+1 💚 javadoc 1m 28s the patch passed with JDK Ubuntu-17.0.20.1+1-1-24.04-Ubuntu
+1 💚 spotbugs 3m 25s the patch passed
+1 💚 shadedclient 31m 56s patch has no errors when building and testing our client artifacts.
_ Other Tests _
+1 💚 unit 24m 6s hadoop-common in the patch passed.
+1 💚 asflicense 1m 14s The patch does not generate ASF License warnings.
222m 51s
Subsystem Report/Notes
Docker ClientAPI=1.56 ServerAPI=1.56 base: https://ci-hadoop.apache.org/job/hadoop-multibranch/job/PR-8969/1/artifact/out/Dockerfile
GITHUB PR #8969
Optional Tests dupname asflicense compile javac javadoc mvninstall mvnsite unit shadedclient spotbugs checkstyle codespell detsecrets
uname Linux 1f394507e6ce 5.15.0-198-generic #208-Ubuntu SMP Fri Sep 4 10:22:38 UTC 2026 x86_64 x86_64 x86_64 GNU/Linux
Build tool maven
Personality dev-support/bin/hadoop.sh
git revision trunk / 5c25535
Default Java Ubuntu-17.0.20.1+1-1-24.04-Ubuntu
Multi-JDK versions /usr/lib/jvm/java-21-openjdk-amd64:Ubuntu-21.0.12.1+1-1-24.04.4-Ubuntu /usr/lib/jvm/java-17-openjdk-amd64:Ubuntu-17.0.20.1+1-1-24.04-Ubuntu
Test Results https://ci-hadoop.apache.org/job/hadoop-multibranch/job/PR-8969/1/testReport/
Max. process+thread count 1451 (vs. ulimit of 10000)
modules C: hadoop-common-project/hadoop-common U: hadoop-common-project/hadoop-common
Console output https://ci-hadoop.apache.org/job/hadoop-multibranch/job/PR-8969/1/console
versions git=2.43.0 maven=3.9.15 spotbugs=4.9.7
Powered by Apache Yetus 0.14.1 https://yetus.apache.org

This message was automatically generated.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants