-
Notifications
You must be signed in to change notification settings - Fork 344
Fix lookup-then-read race in GenerationalUtf8Cache.getUtf8 #11917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
| import java.nio.charset.StandardCharsets; | ||
| import javax.annotation.concurrent.ThreadSafe; | ||
|
|
||
| /** | ||
| * 2-level generational cache of UTF8 values - primarily intended to be used for tag values | ||
|
|
@@ -62,6 +63,7 @@ | |
| * calling ValueUtf8Cache#reclibrate will adjust promotion thresholds to | ||
| * provide better cache utilization. | ||
| */ | ||
| @ThreadSafe | ||
| @SuppressFBWarnings( | ||
| value = "IS2_INCONSISTENT_SYNC", | ||
| justification = | ||
|
|
@@ -219,32 +221,42 @@ public final byte[] getUtf8(String value, long accessTimeMs) { | |
| CacheEntry[] tenuredEntries = this.tenuredEntries; | ||
| int matchingTenuredIndex = lookupEntryIndex(tenuredEntries, MAX_TENURED_PROBES, adjHash, value); | ||
| if (matchingTenuredIndex != -1) { | ||
| // The slot can be mutated concurrently between the lookup and this read: nulled (recalibrate | ||
| // purge / eviction) or reassigned to a *different* value. CacheEntry identity is immutable | ||
| // (adjHash/value/valueUtf8 are final), so re-validate the loaded reference against the | ||
| // request; anything but a match means the slot moved out from under us, so fall through and | ||
| // treat it as a miss rather than NPE'ing (null) or returning another value's bytes | ||
| // (reassigned). | ||
| CacheEntry tenuredEntry = tenuredEntries[matchingTenuredIndex]; | ||
| if (tenuredEntry != null && tenuredEntry.matches(adjHash, value)) { | ||
| tenuredEntry.hit(accessTimeMs); | ||
|
|
||
| tenuredEntry.hit(accessTimeMs); | ||
|
|
||
| this.tenuredHits += 1; | ||
| return tenuredEntry.utf8(); | ||
| this.tenuredHits += 1; | ||
| return tenuredEntry.utf8(); | ||
| } | ||
| } | ||
|
|
||
| CacheEntry[] edenEntries = this.edenEntries; | ||
| int matchingEdenIndex = lookupEntryIndex(edenEntries, MAX_EDEN_PROBES, adjHash, value); | ||
| if (matchingEdenIndex != -1) { | ||
| // Same lookup-then-read race as tenured, plus concurrent promotion nulls the slot (line | ||
| // below); re-validate the loaded reference and treat null-or-mismatch as a miss. | ||
| CacheEntry edenEntry = edenEntries[matchingEdenIndex]; | ||
| if (edenEntry != null && edenEntry.matches(adjHash, value)) { | ||
| double hits = edenEntry.hit(accessTimeMs); | ||
| if (hits > this.promotionThreshold) { | ||
| // mark promoted first - to avoid racy insertions | ||
| this.promotions += 1; | ||
|
|
||
| double hits = edenEntry.hit(accessTimeMs); | ||
| if (hits > this.promotionThreshold) { | ||
| // mark promoted first - to avoid racy insertions | ||
| this.promotions += 1; | ||
| boolean evicted = lruInsert(this.tenuredEntries, MAX_TENURED_PROBES, edenEntry); | ||
| if (evicted) this.tenuredEvictions += 1; | ||
|
|
||
| boolean evicted = lruInsert(this.tenuredEntries, MAX_TENURED_PROBES, edenEntry); | ||
| if (evicted) this.tenuredEvictions += 1; | ||
| edenEntries[matchingEdenIndex] = null; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks also fragile to me and lack of a cas that would solve the case if also another thread possibly replaces that slot with a different new entry?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code is deliberately designed not to use CAS to avoid the overhead. But that does put the burden on the caller to double-check that the Entry matches the expectations -- which is admittedly, how the bug happened in the first place. |
||
| } | ||
|
|
||
| edenEntries[matchingEdenIndex] = null; | ||
| this.edenHits += 1; | ||
| return edenEntry.utf8(); | ||
| } | ||
|
|
||
| this.edenHits += 1; | ||
| return edenEntry.utf8(); | ||
| } | ||
|
|
||
| boolean wasMarked = Caching.mark(this.edenMarkers, adjHash); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package datadog.trace.common.writer.ddagent; | ||
|
|
||
| import static datadog.trace.common.writer.ddagent.Utf8Workload.NUM_LOOKUPS; | ||
| import static datadog.trace.common.writer.ddagent.Utf8Workload.nextTag; | ||
| import static datadog.trace.common.writer.ddagent.Utf8Workload.nextValue; | ||
|
|
||
| import datadog.communication.serialization.GenerationalUtf8Cache; | ||
| import datadog.communication.serialization.SimpleUtf8Cache; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Group; | ||
| import org.openjdk.jmh.annotations.GroupThreads; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
|
|
||
| /** | ||
| * Multi-threaded companion to {@link Utf8Benchmark}, exercising the caches' intended thread-safety | ||
| * contract against the shared caches. | ||
| * | ||
| * <p>The point of this variant is to illustrate how {@code recalibrate()} is driven under | ||
| * concurrency. Unlike the single-threaded case — where the lone serializer thread recalibrates | ||
| * inline — you would <em>not</em> have every worker recalibrate (that needlessly churns the shared | ||
| * cache and widens the lookup-then-read race window). Instead a single dedicated thread drives | ||
| * {@code recalibrate()} while the remaining threads perform lookups. JMH's {@code @Group} / | ||
| * {@code @GroupThreads} expresses exactly that: 7 lookup threads + 1 recalibrate thread on the same | ||
| * cache. | ||
| * | ||
| * <p>The recalibrate thread runs continuously, which is deliberately more aggressive than a real | ||
| * periodic cadence — it maximizes contention so this doubles as a concurrency guardrail. | ||
| */ | ||
| @BenchmarkMode(Mode.Throughput) | ||
| @State(Scope.Group) | ||
| public class Utf8ConcurrentBenchmark { | ||
| static final GenerationalUtf8Cache VALUE_CACHE = new GenerationalUtf8Cache(64, 128); | ||
| static final SimpleUtf8Cache SIMPLE_VALUE_CACHE = new SimpleUtf8Cache(128); | ||
|
|
||
| @Benchmark | ||
| @Group("generational") | ||
| @GroupThreads(7) | ||
| public void generational_lookup(Blackhole bh) { | ||
| for (int i = 0; i < NUM_LOOKUPS; ++i) { | ||
| String tag = nextTag(); | ||
| bh.consume(VALUE_CACHE.getUtf8(nextValue(tag))); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Group("generational") | ||
| @GroupThreads(1) | ||
| public void generational_recalibrate() { | ||
| VALUE_CACHE.recalibrate(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Group("simple") | ||
| @GroupThreads(7) | ||
| public void simple_lookup(Blackhole bh) { | ||
| for (int i = 0; i < NUM_LOOKUPS; ++i) { | ||
| String tag = nextTag(); | ||
| bh.consume(SIMPLE_VALUE_CACHE.getUtf8(nextValue(tag))); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Group("simple") | ||
| @GroupThreads(1) | ||
| public void simple_recalibrate() { | ||
| SIMPLE_VALUE_CACHE.recalibrate(); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: use
{on theifThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not my preference, but I can if you wish.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was pretty sure we had somewhere written to prefer using brackets on if but I cannot find anymore so perhaps I imagined it 🤣 do as you prefer!