Do we have plan to support ScopedValue rather than ThreadLocal?

Offen
#8,282 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 3 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
25/100
Issue-Typ
Feature
Klarheit
Muss geklärt werden
Aktivitätsstatus
Ruhig
Tech-Stack
java
Bereich
backend

Rechercherichtung

Beginne mit der Überprüfung von io.opentelemetry.context.ThreadLocalContextStorage und des im Issue beschriebenen Verhaltens von Virtual Threads im JDK. Ermittle, ob ScopedValue mit den Anforderungen an den Context Storage kompatibel ist, und identifiziere die Benchmarks oder Tests, die erforderlich sind, um die Ansätze zu vergleichen. Als abgeschlossen gilt die Aufgabe erst mit einer dokumentierten Designentscheidung oder einem abgestimmten Implementierungsplan, nicht lediglich mit einer Codeänderung.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Feature Request

I saw the impl heavily relies on ThreadLocal like io.opentelemetry.context.ThreadLocalContextStorage.
While in our LnP test against JDK virtual thread, we found threadlocal could be the bottleneck.
Is there any plan to switch to ScopedValue?

Here's also some comments from AI:

The Performance Overhead Sources

  1. Thread.currentThread() lookup: Each get()/set() needs to get the current thread. With virtual threads, this involves:
    - Finding the carrier thread
    - Looking up which virtual thread is currently mounted
    - This has more indirection than with platform threads
  2. Mounting/unmounting cost: When a virtual thread blocks (IO), it's unmounted. On resumption, there's overhead. ThreadLocal access
    during this transition adds to the cost.
  3. Potential JVM internal contention: While ThreadLocalMap itself is largely lock-free, some JVM implementations have shown
    contention-like bottlenecks at scale due to:
    - Internal hash map operations
    - Memory barrier costs with millions of concurrent threads

The Reality Check

┌────────────────────────────────┬────────────────────────────────────────┐
│ Aspect │ Reality │
├────────────────────────────────┼────────────────────────────────────────┤
│ Traditional lock contention? │ No │
├────────────────────────────────┼────────────────────────────────────────┤
│ Per-operation overhead higher? │ Yes, marginally │
├────────────────────────────────┼────────────────────────────────────────┤
│ Scales to millions of threads? │ Debatable - depends on access patterns │
├────────────────────────────────┼────────────────────────────────────────┤
│ ScopedValue better? │ Yes, designed for virtual threads │
└────────────────────────────────┴────────────────────────────────────────┘

Recommendation

For new code with virtual threads, prefer ScopedValue (Java 21+) over ThreadLocal. It's semantics-matched to virtual thread model and
avoids some of the historical design choices in ThreadLocal that weren't made with millions of threads in mind.

If stuck with ThreadLocal, keep access patterns minimal and benchmark your specific use case.

Vorherrschende Sprache
Java
Sterne
2.5k
Forks
1.1k
Ø Merge
5 T. 11 Std.
Gemergte PRs (30 T.)
41

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus open-telemetry/opentelemetry-java

Alle Issues in open-telemetry/opentelemetry-java

Ähnliche Issues

Weitere Issues zu Java

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.