Skip to content

Replace BatchSpanProcessor's blocking signal queue - #9068

Draft
friscoMad wants to merge 2 commits into
open-telemetry:mainfrom
friscoMad:codex/nonblocking-span-processor-wakeup
Draft

friscoMad wants to merge 2 commits into
open-telemetry:mainfrom
friscoMad:codex/nonblocking-span-processor-wakeup

Conversation

@friscoMad

@friscoMad friscoMad commented Nov 26, 2026 •

Copy link
Copy Markdown

Depends on #9070. Merge the benchmark fixes first. This PR contains only the span processor implementation and its worker concurrency tests.

Ending a sampled span on an event-loop thread can park inside BatchSpanProcessor.Worker.addSpan() while ArrayBlockingQueue.offer() acquires its ReentrantLock. This PR replaces that signal queue with LockSupport.unpark() while retaining DaemonThreadFactory and interrupt handling.

The worker publishes its batch threshold, rechecks queued work and pending flush/shutdown state, and then calls parkNanos(). Shutdown also wakes the worker when its completion callback runs after the flush has already completed. Related to #8240; this version adds deterministic wake-up/lifecycle tests and before/after benchmarks.

This removes the producer-side Java signal-queue lock. HotSpot's native unpark can still contend on a mutex. These measurements do not establish a wait-free span-end call.

Validation

  • Tests committed first: full-batch wake-up and prompt-shutdown tests fail on the original implementation. The late-shutdown-callback test failed before adding the final shutdown wake-up.
  • :sdk:trace:check passes using JDK 25, including 356 tests, formatting, static checks, and API comparison.
  • :sdk:trace:test :sdk:trace:testIncubating -PtestJavaVersion=11 passes: 326 regular tests and 30 incubating tests.
  • Seven worker tests cover the pre-wait race, full/partial batches, flush, shutdown, and an empty wake-up. The worker is package-private/extensible solely to control those interleavings without reflection.
  • The benchmark fixture, accounting tests, and measurement documentation are in Fix batch span processor benchmark accounting and latency measurement #9070. The performance results below were captured from the combined tree f31e798; splitting the PRs preserves those sources exactly.

Methodology, counters, and commands: benchmark README.

JMH results

Apple M4 Pro (14 cores, 48 GiB), macOS 26.7.1, Temurin 11.0.31+11, JMH 1.37. Each case uses three independent forks per variant, 3 × 2s warm-up and 5 × 2s measurement iterations, with the GC profiler. Variant order alternates baseline/patched, patched/baseline, baseline/patched. No builds or other benchmark runs ran concurrently with the measurements.

The identical harness and dependencies are used for both variants. Baseline BatchSpanProcessor classes are compiled from a92b644 with JDK 25 (--release 8) into a small override jar placed first on the fork classpath. The patched variant uses the main JMH jar. This holds the benchmark fixed without copying or modifying the checkout. The runtime implementation was held fixed while correcting the benchmarks.

All values below are median [minimum–maximum] across the three fork results, not confidence intervals. Rates use complete-iteration ledgers through the final drain, including JMH synchronization work around the timed loop. They are not the primary JMH attempt-throughput score. These results supersede the earlier exploratory tables made with the old accounting.

Saturation: successful no-op exporter, queue 2048, batch 512

Producers Exported M spans/s, baseline Exported M spans/s, patched Drop %, baseline Drop %, patched
5 3.18 [2.76–3.48] 7.51 [7.51–7.72] 64.19 [63.40–69.34] 0.57 [0.57–0.59]
10 2.68 [2.10–2.93] 3.41 [3.34–3.50] 54.97 [37.46–64.83] 3.34 [3.21–4.45]

Export rate increases in every paired fork, while attempt rate decreases; the baseline drops far more work. The improvement includes the publish/recheck race fix; this comparison does not isolate that fix from replacing the signaling primitive.

This is not a CPU-efficiency win under saturation. Median worker CPU rises from 36.8% to 91.2% of one core at five producers and 54.0% to 93.1% at ten. CPU per exported span rises from 116 to 121 ns and 257 to 273 ns respectively. Allocations per attempt also rise with the different accepted/dropped work mix, so these cases do not demonstrate an allocation improvement.

Sampled Span.end() latency: queue 2048, batch 512, target 5000 spans/s per producer

Producers Baseline p99, µs Patched p99, µs Baseline p99.9, µs Patched p99.9, µs
1 0.875 [0.667–2.416] 0.750 [0.584–0.792] 71.36 [69.50–106.71] 20.47 [12.94–20.50]
10 0.917 [0.375–1.166] 0.833 [0.542–1.000] 11.20 [3.30–14.49] 3.25 [2.96–4.12]

There are no dropped spans in any latency iteration, including warm-up. Normal-batch achieved rates are 4.94–4.99k/s at one producer and 49.38–49.91k/s at ten; skipped slots stay below 1.1%. P50 is around 125–167 ns, near the invocation/timer overhead floor. The p99.9 reductions are encouraging observations on this host, not bounds on event-loop blocking or evidence about Linux scheduling.

Frequent wake-ups: one producer, batch size 1

Target spans/s Baseline achieved spans/s Patched achieved spans/s Baseline p99.9, µs Patched p99.9, µs
5000 4877 [4456–4979] 4627 [4476–4978] 181.84 [55.29–267.63] 92.42 [41.82–159.14]
1000 990 [989–993] 993 [990–994] 351.29 [306.55–573.59] 257.86 [159.77–274.06]

Skipped arrival slots, as a percentage of attempted plus skipped arrivals:

  • 5000/s target: baseline 2.02 [0.37–9.85]%, patched 6.64 [0.39–9.37]%.
  • 1000/s target: baseline 0.83 [0.56–0.91]%, patched 0.64 [0.53–0.82]%.

The 5000/s case has unequal achieved loads and broad overlapping tail ranges. The 1000/s control was added after observing those missed arrivals. The lower-rate control achieves a closely matched load and still has substantial tails. These samples do not attribute the remaining pauses to a specific cause or establish that native mutex contention has disappeared.

Per-fork results
Case Round Variant Exported spans/s Drops % Skipped slots % Worker % of one core p99 µs p99.9 µs
normal/export_05Thread 1 baseline 2760519 69.336 0.000 33.209 — —
normal/export_10Thread 1 baseline 2932743 37.461 0.000 75.756 — —
normal/end_01Thread 1 baseline 4994 0.000 0.104 0.246 0.875 106.711
normal/end_10Thread 1 baseline 49781 0.000 0.306 0.496 1.166 14.488
normal/export_05Thread 1 patched 7505949 0.567 0.000 90.524 — —
normal/export_10Thread 1 patched 3340851 4.447 0.000 91.959 — —
normal/end_01Thread 1 patched 4956 0.000 0.801 0.290 0.750 20.473
normal/end_10Thread 1 patched 49828 0.000 0.183 0.367 0.542 3.248
normal/export_05Thread 2 baseline 3475042 63.399 0.000 37.958 — —
normal/export_10Thread 2 baseline 2682210 54.967 0.000 53.950 — —
normal/end_01Thread 2 baseline 4940 0.000 1.048 0.221 2.416 71.361
normal/end_10Thread 2 baseline 49376 0.000 0.883 0.478 0.917 11.200
normal/export_05Thread 2 patched 7509057 0.586 0.000 91.159 — —
normal/export_10Thread 2 patched 3495343 3.343 0.000 93.098 — —
normal/end_01Thread 2 patched 4993 0.000 0.106 0.229 0.584 12.941
normal/end_10Thread 2 patched 49908 0.000 0.097 0.355 0.833 2.956
normal/export_05Thread 3 baseline 3182183 64.188 0.000 36.826 — —
normal/export_10Thread 3 baseline 2099278 64.830 0.000 53.879 — —
normal/end_01Thread 3 baseline 4990 0.000 0.156 0.258 0.667 69.501
normal/end_10Thread 3 baseline 49861 0.000 0.182 0.412 0.375 3.298
normal/export_05Thread 3 patched 7719679 0.569 0.000 92.648 — —
normal/export_10Thread 3 patched 3413992 3.205 0.000 93.087 — —
normal/end_01Thread 3 patched 4981 0.000 0.314 0.252 0.792 20.500
normal/end_10Thread 3 patched 49892 0.000 0.090 0.396 1.000 4.120
wakeup/end_01Thread 1 baseline 4877 0.000 2.015 3.362 18.870 181.842
wakeup/end_01Thread 1 patched 4476 0.000 9.372 2.608 15.257 159.140
wakeup/end_01Thread 2 baseline 4979 0.000 0.369 2.205 4.120 55.286
wakeup/end_01Thread 2 patched 4627 0.000 6.636 2.258 7.040 92.424
wakeup/end_01Thread 3 baseline 4456 0.000 9.850 2.822 13.473 267.626
wakeup/end_01Thread 3 patched 4978 0.000 0.386 1.968 4.288 41.822
lowrate/end_01Thread 1 baseline 989 0.000 0.909 1.615 35.600 573.594
lowrate/end_01Thread 1 patched 993 0.000 0.639 1.130 30.218 159.767
lowrate/end_01Thread 2 baseline 990 0.000 0.829 1.387 36.269 306.550
lowrate/end_01Thread 2 patched 990 0.000 0.820 1.081 27.800 257.855
lowrate/end_01Thread 3 baseline 993 0.000 0.559 1.522 34.875 351.291
lowrate/end_01Thread 3 patched 994 0.000 0.529 1.366 35.213 274.061
Reproduce

Apply the benchmark prerequisite #9070 before building these JMH classes. Build the combined tree with JDK 25, then use JDK 11 for the runner and forks:

./gradlew :sdk:trace:jmhJar --console=plain --max-workers=4
java -Djmh.blackhole.autoDetect=false -jar sdk/trace/build/libs/opentelemetry-sdk-trace-*-jmh.jar \
  '^io\.opentelemetry\.sdk\.trace\.export\.BatchSpanProcessor(MultiThreadBenchmark\.export_(05|10)Thread|SpanEndBenchmark\.end_(01|10)Thread)$' \
  -f 1 -wi 3 -w 2s -i 5 -r 2s -prof gc -foe true \
  -p targetSpansPerSecond=5000 -rf json -rff results.json

Repeat three times per variant in alternating order, using separate result files. For frequent wake-ups, select only BatchSpanProcessorSpanEndBenchmark.end_01Thread, add -p maxExportBatchSize=1, and run targets 5000 and 1000 separately. To reproduce the baseline, compile the original BatchSpanProcessor.java with javac --release 8 -proc:none -cp <jmh-jar>, package all resulting BatchSpanProcessor*.class files, and invoke org.openjdk.jmh.Main with that override jar first on the classpath instead of using -jar.

AI-assisted implementation, tests, and analysis with Codex (GPT-6), with additional Codex review. Kept as a draft for human review of the concurrency design and benchmark tradeoffs.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

CLA Missing ID

One or more co-authors of this pull request were not found. You must specify co-authors in commit message trailer via:

Co-authored-by: name <email>

Supported Co-authored-by: formats include:

  1. Anything <id+login@users.noreply.github.com> - it will locate your GitHub user by id part.
  2. Anything <login@users.noreply.github.com> - it will locate your GitHub user by login part.
  3. Anything <public-email> - it will locate your GitHub user by public-email part. Note that this email must be made public on Github.
  4. Anything <other-email> - it will locate your GitHub user by other-email part but only if that email was used before for any other CLA as a main commit author.
  5. login <any-valid-email> - it will locate your GitHub user by login part, note that login part must be at least 3 characters long.

Alternatively, if the co-author should not be included, remove the Co-authored-by: line from the commit message.

Please update your commit message(s) by doing git commit --amend and then git push [--force] and then request re-running CLA check via commenting on this pull request:

/easycla

@opentelemetry-pr-dashboard

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-11-26 11:42 UTC

Move out of draft to request review.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.75000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.57%. Comparing base (a92b644) to head (ca5fb1c).

Files with missing lines Patch % Lines
...telemetry/sdk/trace/export/BatchSpanProcessor.java 93.75% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #9068      +/-   ##
============================================
+ Coverage     90.56%   90.57%   +0.01%     
  Complexity    11220    11220              
============================================
  Files          1032     1032              
  Lines         30599    30600       +1     
  Branches       4010     4012       +2     
============================================
+ Hits          27711    27716       +5     
+ Misses         1913     1910       -3     
+ Partials        975      974       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

friscoMad and others added 2 commits November 26, 2026 13:45
Expose the worker within its package so tests can pause a supplied queue at the empty-check boundary without reflection.

Assisted-by: Codex (GPT-6)

Co-Authored-By: Codex (GPT-6) <noreply@openai.com>
Publish and recheck the batch threshold before parking the exporter. Wake it for flush and shutdown, including when flush finishes before shutdown callback registration.

Assisted-by: Codex (GPT-6)

Co-Authored-By: Codex (GPT-6) <noreply@openai.com>
@friscoMad
friscoMad force-pushed the codex/nonblocking-span-processor-wakeup branch from f31e798 to ca5fb1c Compare November 26, 2026 12:47

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant