|
1 | 1 | package com.datadoghq.profiler.context; |
2 | 2 |
|
| 3 | +import java.nio.charset.StandardCharsets; |
3 | 4 | import java.util.Arrays; |
4 | 5 | import java.util.ArrayList; |
5 | 6 | import java.util.HashMap; |
@@ -239,6 +240,172 @@ public void testCrossSlotIsolation() throws Exception { |
239 | 240 | assertNull(readTag(contextSetter, "tag2")); |
240 | 241 | } |
241 | 242 |
|
| 243 | + @Test |
| 244 | + public void testReapplyByIdAndBytes() throws Exception { |
| 245 | + Assumptions.assumeTrue(!Platform.isJ9()); |
| 246 | + registerCurrentThreadForWallClockProfiling(); |
| 247 | + ContextSetter contextSetter = new ContextSetter(profiler, Arrays.asList("tag1", "tag2")); |
| 248 | + int slot = contextSetter.offsetOf("tag1"); |
| 249 | + String value = "app-managed"; |
| 250 | + |
| 251 | + // Set the attribute the normal way, then capture both the constant ID (sidecar) and the |
| 252 | + // UTF-8 bytes — exactly what dd-trace-java retains for the reapply hot path. |
| 253 | + assertTrue(contextSetter.setContextValue("tag1", value)); |
| 254 | + int[] ids = contextSetter.snapshotTags(); |
| 255 | + int savedId = ids[slot]; |
| 256 | + assertNotEquals(0, savedId); |
| 257 | + byte[][] bytes = new byte[ids.length][]; |
| 258 | + bytes[slot] = value.getBytes(StandardCharsets.UTF_8); |
| 259 | + |
| 260 | + // setContext (span activation) wipes both views. |
| 261 | + profiler.setContext(1L, 42L, 0L, 42L); |
| 262 | + assertNull(readTag(contextSetter, "tag1"), "attrs_data must be wiped by setContext"); |
| 263 | + assertEquals(0, contextSetter.snapshotTags()[slot], "sidecar must be wiped by setContext"); |
| 264 | + |
| 265 | + // Reapply by ID + bytes restores BOTH views. |
| 266 | + assertTrue(contextSetter.setContextValuesByIdAndBytes(ids, bytes)); |
| 267 | + assertEquals(value, readTag(contextSetter, "tag1"), "attrs_data must be restored"); |
| 268 | + assertEquals(savedId, contextSetter.snapshotTags()[slot], "sidecar must be restored"); |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + public void testReapplyByIdAndBytesRejectsBadArgs() throws Exception { |
| 273 | + Assumptions.assumeTrue(!Platform.isJ9()); |
| 274 | + registerCurrentThreadForWallClockProfiling(); |
| 275 | + ContextSetter contextSetter = new ContextSetter(profiler, Arrays.asList("tag1", "tag2")); |
| 276 | + int slot = contextSetter.offsetOf("tag1"); |
| 277 | + |
| 278 | + assertTrue(contextSetter.setContextValue("tag1", "v")); |
| 279 | + int id = contextSetter.snapshotTags()[slot]; |
| 280 | + assertNotEquals(0, id); |
| 281 | + |
| 282 | + // Null arrays and length mismatch. |
| 283 | + assertFalse(contextSetter.setContextValuesByIdAndBytes(null, new byte[1][])); |
| 284 | + assertFalse(contextSetter.setContextValuesByIdAndBytes(new int[1], null)); |
| 285 | + assertFalse(contextSetter.setContextValuesByIdAndBytes(new int[2], new byte[3][])); |
| 286 | + |
| 287 | + // A slot with constantId > 0 requires non-null bytes within the size limit. |
| 288 | + assertFalse(contextSetter.setContextValuesByIdAndBytes( |
| 289 | + new int[] {id, 0}, new byte[][] {null, null})); |
| 290 | + assertFalse(contextSetter.setContextValuesByIdAndBytes( |
| 291 | + new int[] {id, 0}, new byte[][] {new byte[256], null})); |
| 292 | + |
| 293 | + // 255 bytes is the boundary and must be accepted. |
| 294 | + byte[] ok255 = new byte[255]; |
| 295 | + Arrays.fill(ok255, (byte) 'x'); |
| 296 | + assertTrue(contextSetter.setContextValuesByIdAndBytes( |
| 297 | + new int[] {id, 0}, new byte[][] {ok255, null})); |
| 298 | + } |
| 299 | + |
| 300 | + @Test |
| 301 | + public void testReapplyByIdAndBytesReplacesExistingValue() throws Exception { |
| 302 | + Assumptions.assumeTrue(!Platform.isJ9()); |
| 303 | + registerCurrentThreadForWallClockProfiling(); |
| 304 | + ContextSetter contextSetter = new ContextSetter(profiler, Arrays.asList("tag1", "tag2")); |
| 305 | + int slot = contextSetter.offsetOf("tag1"); |
| 306 | + |
| 307 | + // Capture the ID + bytes for "first". |
| 308 | + assertTrue(contextSetter.setContextValue("tag1", "first")); |
| 309 | + int[] idsFirst = contextSetter.snapshotTags(); |
| 310 | + byte[][] bytesFirst = new byte[idsFirst.length][]; |
| 311 | + bytesFirst[slot] = "first".getBytes(StandardCharsets.UTF_8); |
| 312 | + |
| 313 | + // Overwrite the live slot with a different value. |
| 314 | + assertTrue(contextSetter.setContextValue("tag1", "second")); |
| 315 | + assertEquals("second", readTag(contextSetter, "tag1")); |
| 316 | + |
| 317 | + // Reapply "first" by ID + bytes over the live "second" — exercises the |
| 318 | + // compact-then-insert path in replaceOtepAttribute. |
| 319 | + assertTrue(contextSetter.setContextValuesByIdAndBytes(idsFirst, bytesFirst)); |
| 320 | + assertEquals("first", readTag(contextSetter, "tag1")); |
| 321 | + assertEquals(idsFirst[slot], contextSetter.snapshotTags()[slot]); |
| 322 | + } |
| 323 | + |
| 324 | + @Test |
| 325 | + public void testReapplyByIdAndBytesAfterClear() throws Exception { |
| 326 | + Assumptions.assumeTrue(!Platform.isJ9()); |
| 327 | + registerCurrentThreadForWallClockProfiling(); |
| 328 | + ContextSetter contextSetter = new ContextSetter(profiler, Arrays.asList("tag1", "tag2")); |
| 329 | + int slot = contextSetter.offsetOf("tag1"); |
| 330 | + |
| 331 | + assertTrue(contextSetter.setContextValue("tag1", "live")); |
| 332 | + int[] ids = contextSetter.snapshotTags(); |
| 333 | + byte[][] bytes = new byte[ids.length][]; |
| 334 | + bytes[slot] = "live".getBytes(StandardCharsets.UTF_8); |
| 335 | + |
| 336 | + assertTrue(contextSetter.clearContextValue("tag1")); |
| 337 | + assertNull(readTag(contextSetter, "tag1")); |
| 338 | + assertEquals(0, contextSetter.snapshotTags()[slot]); |
| 339 | + |
| 340 | + assertTrue(contextSetter.setContextValuesByIdAndBytes(ids, bytes)); |
| 341 | + assertEquals("live", readTag(contextSetter, "tag1")); |
| 342 | + assertEquals(ids[slot], contextSetter.snapshotTags()[slot]); |
| 343 | + } |
| 344 | + |
| 345 | + @Test |
| 346 | + public void testReapplyByIdAndBytesClearedRecord() throws Exception { |
| 347 | + // Verifies that setContextValuesByIdAndBytes never resurrects a cleared (span-less) record. |
| 348 | + // A cleared record has valid=0 and no trace/span context; re-publishing it would expose |
| 349 | + // attribute values with no associated trace, which is meaningless to the signal handler. |
| 350 | + Assumptions.assumeTrue(!Platform.isJ9()); |
| 351 | + registerCurrentThreadForWallClockProfiling(); |
| 352 | + ContextSetter contextSetter = new ContextSetter(profiler, Arrays.asList("tag1", "tag2")); |
| 353 | + int slot = contextSetter.offsetOf("tag1"); |
| 354 | + |
| 355 | + // Establish a live record with tag1 set. |
| 356 | + profiler.setContext(1L, 42L, 0L, 42L); |
| 357 | + assertTrue(contextSetter.setContextValue("tag1", "will-be-cleared")); |
| 358 | + int[] ids = contextSetter.snapshotTags(); |
| 359 | + byte[][] bytes = new byte[ids.length][]; |
| 360 | + bytes[slot] = "will-be-cleared".getBytes(StandardCharsets.UTF_8); |
| 361 | + |
| 362 | + // Drive valid=0 via the all-zero clear path (clearContext → put(0,0,0,0) → no attach()). |
| 363 | + profiler.clearContext(); |
| 364 | + // readContextAttribute respects valid=0 and returns null, confirming the record is dark. |
| 365 | + assertNull(readTag(contextSetter, "tag1")); |
| 366 | + |
| 367 | + // Reapply must return false and must not resurrect the cleared record. |
| 368 | + assertFalse(contextSetter.setContextValuesByIdAndBytes(ids, bytes), |
| 369 | + "setContextValuesByIdAndBytes must return false when the record is cleared (valid=0)"); |
| 370 | + assertNull(readTag(contextSetter, "tag1"), |
| 371 | + "cleared record must not be resurrected by setContextValuesByIdAndBytes"); |
| 372 | + } |
| 373 | + |
| 374 | + @Test |
| 375 | + public void testReapplyByIdAndBytesOverflowRollback() throws Exception { |
| 376 | + Assumptions.assumeTrue(!Platform.isJ9()); |
| 377 | + registerCurrentThreadForWallClockProfiling(); |
| 378 | + List<String> attrs = new ArrayList<>(); |
| 379 | + for (int i = 1; i <= 10; i++) { |
| 380 | + attrs.add("tag" + i); |
| 381 | + } |
| 382 | + ContextSetter contextSetter = new ContextSetter(profiler, attrs); |
| 383 | + |
| 384 | + // Register one 255-byte value to obtain a valid constant ID. |
| 385 | + char[] chars = new char[255]; |
| 386 | + Arrays.fill(chars, 'x'); |
| 387 | + String bigValue = new String(chars); |
| 388 | + assertTrue(contextSetter.setContextValue("tag1", bigValue)); |
| 389 | + int bigId = contextSetter.snapshotTags()[contextSetter.offsetOf("tag1")]; |
| 390 | + assertNotEquals(0, bigId); |
| 391 | + byte[] bigBytes = bigValue.getBytes(StandardCharsets.UTF_8); |
| 392 | + |
| 393 | + // Reapply the same 255-byte value to all 10 slots — attrs_data cannot hold them all. |
| 394 | + int[] ids = new int[10]; |
| 395 | + byte[][] bytes = new byte[10][]; |
| 396 | + Arrays.fill(ids, bigId); |
| 397 | + Arrays.fill(bytes, bigBytes); |
| 398 | + assertFalse(contextSetter.setContextValuesByIdAndBytes(ids, bytes), |
| 399 | + "10 x 255-byte values must overflow attrs_data"); |
| 400 | + |
| 401 | + // The last slot certainly overflowed: its sidecar must be zeroed and attrs_data empty. |
| 402 | + int lastSlot = contextSetter.offsetOf("tag10"); |
| 403 | + assertEquals(0, contextSetter.snapshotTags()[lastSlot], |
| 404 | + "overflowed slot's sidecar must be zeroed"); |
| 405 | + assertNull(readTag(contextSetter, "tag10"), |
| 406 | + "overflowed slot must read null — the entry never landed in attrs_data"); |
| 407 | + } |
| 408 | + |
242 | 409 | private void work(ContextSetter contextSetter, String contextAttribute, String contextValue) |
243 | 410 | throws InterruptedException { |
244 | 411 | assertTrue(contextSetter.setContextValue(contextAttribute, contextValue)); |
|
0 commit comments