Commit b9d1b57
Fix lookup-then-read race in GenerationalUtf8Cache.getUtf8 (#11917)
Fix lookup-then-read race in GenerationalUtf8Cache.getUtf8
getUtf8() looks up a matching slot via lookupEntryIndex() and then reads
that slot from the array in a second step. Between the two, another thread
can mutate the slot: recalibrate()/eviction can null it, and promotion nulls
the eden slot after promoting into tenured. The array read therefore returned
either null (-> NPE on the following hit()) or, worse, a *different* value's
entry whose bytes were then returned as if they were the requested value's --
silent payload corruption.
This never manifests today because trace serialization runs on a single
thread (TraceProcessingWorker), but the cache is built to allow concurrent
access, so the race is a latent bug against that contract.
CacheEntry identity is immutable (adjHash/value/valueUtf8 are final), so the
fix re-validates the loaded reference against the request
(entry != null && entry.matches(adjHash, value)) on both the tenured and eden
read paths; a null-or-mismatched slot is treated as a miss. The residual races
on hit()'s score/lastUsedMs writes are benign -- they only nudge LRU/eviction
bookkeeping and never affect returned bytes.
Adds a concurrent regression test that fails (NPE / wrong bytes) without the
fix and passes with it.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Mark Utf8 caches @threadsafe and add multi-threaded cache benchmarks
Follow-on cleanup to the getUtf8 race fix:
- Mark both `GenerationalUtf8Cache` and `SimpleUtf8Cache` `@ThreadSafe`
(`javax.annotation.concurrent.ThreadSafe`, the annotation already used across
the codebase), making the intended concurrency contract explicit.
(`SimpleUtf8Cache` was already correct — its lookup returns a validated entry
reference rather than re-reading the slot by index.)
- Split the UTF8 cache benchmarks into a single-threaded `Utf8Benchmark`
and a multi-threaded `Utf8ConcurrentBenchmark`, sharing `Utf8Workload`.
The single-threaded variant reflects how the caches are driven today
(serialization is single-threaded) and drives recalibrate inline; the
concurrent variant uses @group to model the intended concurrent drive
pattern (a dedicated recalibrate thread + worker lookup threads on the
shared cache) and doubles as a concurrency guardrail. A @threads>1
benchmark like this would have hit the NPE and surfaced the race sooner.
Measured cost of the matches() re-validation (single-thread, -f3, with vs
without fix, `simple` as unchanged control): allocation flat (-0.03%) and
throughput within run-to-run noise (changed benchmark moved less than the
untouched control), i.e. no measurable cost.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Merge branch 'master' into dougqh/utf8-cache-concurrency-fix
Co-authored-by: devflow.devflow-routing-intake <devflow.devflow-routing-intake@kubernetes.us1.ddbuild.io>1 parent 58f636d commit b9d1b57
6 files changed
Lines changed: 255 additions & 73 deletions
File tree
- communication/src
- main/java/datadog/communication/serialization
- test/java/datadog/communication/serialization
- dd-trace-core/src/jmh/java/datadog/trace/common/writer/ddagent
Lines changed: 26 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
| 66 | + | |
65 | 67 | | |
66 | 68 | | |
67 | 69 | | |
| |||
219 | 221 | | |
220 | 222 | | |
221 | 223 | | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
222 | 230 | | |
| 231 | + | |
| 232 | + | |
223 | 233 | | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
228 | 237 | | |
229 | 238 | | |
230 | 239 | | |
231 | 240 | | |
232 | 241 | | |
| 242 | + | |
| 243 | + | |
233 | 244 | | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
234 | 250 | | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
| 251 | + | |
| 252 | + | |
239 | 253 | | |
240 | | - | |
241 | | - | |
| 254 | + | |
| 255 | + | |
242 | 256 | | |
243 | | - | |
| 257 | + | |
| 258 | + | |
244 | 259 | | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | 260 | | |
249 | 261 | | |
250 | 262 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
| 45 | + | |
44 | 46 | | |
45 | 47 | | |
46 | 48 | | |
| |||
Lines changed: 86 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
| 12 | + | |
11 | 13 | | |
| 14 | + | |
| 15 | + | |
12 | 16 | | |
13 | 17 | | |
14 | 18 | | |
| |||
175 | 179 | | |
176 | 180 | | |
177 | 181 | | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
178 | 264 | | |
179 | 265 | | |
180 | 266 | | |
| |||
Lines changed: 15 additions & 59 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
3 | 7 | | |
4 | 8 | | |
5 | 9 | | |
6 | | - | |
7 | 10 | | |
8 | 11 | | |
9 | 12 | | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
13 | | - | |
14 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
15 | 21 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
21 | 26 | | |
22 | 27 | | |
23 | 28 | | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | 29 | | |
74 | 30 | | |
75 | 31 | | |
| |||
109 | 65 | | |
110 | 66 | | |
111 | 67 | | |
112 | | - | |
| 68 | + | |
113 | 69 | | |
114 | 70 | | |
115 | 71 | | |
| |||
125 | 81 | | |
126 | 82 | | |
127 | 83 | | |
128 | | - | |
| 84 | + | |
129 | 85 | | |
130 | 86 | | |
131 | 87 | | |
| |||
Lines changed: 72 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
0 commit comments