-
Notifications
You must be signed in to change notification settings - Fork 331
Fix VirtualThread support on multiple unmount/remount cycles #10931
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
Merged
+235
−22
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
...c/test/java/testdog/trace/instrumentation/java/lang/jdk21/VirtualThreadLifeCycleTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| package testdog.trace.instrumentation.java.lang.jdk21; | ||
|
|
||
| import static datadog.trace.agent.test.assertions.SpanMatcher.span; | ||
| import static datadog.trace.agent.test.assertions.TraceMatcher.trace; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import datadog.trace.agent.test.AbstractInstrumentationTest; | ||
| import datadog.trace.api.CorrelationIdentifier; | ||
| import datadog.trace.api.GlobalTracer; | ||
| import datadog.trace.api.Trace; | ||
| import java.time.Duration; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** Test context tracking through {@code VirtualThread} lifecycle - park/unpark (remount) cycles. */ | ||
| public class VirtualThreadLifeCycleTest extends AbstractInstrumentationTest { | ||
| private static final Duration TIMEOUT = Duration.ofSeconds(10); | ||
|
|
||
| @DisplayName("test context restored after virtual thread remounts") | ||
| @Test | ||
| void testContextRestoredAfterVirtualThreadRemount() { | ||
| int remountCount = 5; | ||
| String[] spanId = new String[1]; | ||
| String[] spanIdBeforeUnmount = new String[1]; | ||
| String[] spanIdsAfterRemount = new String[remountCount]; | ||
|
|
||
| new Runnable() { | ||
| @Override | ||
| @Trace(operationName = "parent") | ||
| public void run() { | ||
| spanId[0] = GlobalTracer.get().getSpanId(); | ||
|
|
||
| Thread thread = | ||
| Thread.startVirtualThread( | ||
| () -> { | ||
| spanIdBeforeUnmount[0] = GlobalTracer.get().getSpanId(); | ||
| for (int remount = 0; remount < remountCount; remount++) { | ||
| tryUnmount(); | ||
| spanIdsAfterRemount[remount] = GlobalTracer.get().getSpanId(); | ||
| } | ||
| }); | ||
| try { | ||
| thread.join(TIMEOUT); | ||
| } catch (InterruptedException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| }.run(); | ||
|
|
||
| assertEquals( | ||
| spanId[0], | ||
| spanIdBeforeUnmount[0], | ||
| "context should be inherited from the parent execution unit"); | ||
| for (int i = 0; i < remountCount; i++) { | ||
| assertEquals( | ||
| spanId[0], | ||
| spanIdsAfterRemount[i], | ||
| "context should be restored after virtual thread remounts"); | ||
| } | ||
|
|
||
| assertTraces(trace(span().root().operationName("parent"))); | ||
| } | ||
|
|
||
| @DisplayName("test context restored as implicit parent span after remount") | ||
| @Test | ||
| void testContextRestoredAsImplicitParentSpanAfterRemount() { | ||
| new Runnable() { | ||
| @Override | ||
| @Trace(operationName = "parent") | ||
| public void run() { | ||
| Thread thread = | ||
| Thread.startVirtualThread( | ||
| () -> { | ||
| tryUnmount(); | ||
| // Runnable to create child span, not async related | ||
| new Runnable() { | ||
| @Override | ||
| @Trace(operationName = "child") | ||
| public void run() {} | ||
| }.run(); | ||
| }); | ||
| try { | ||
| thread.join(TIMEOUT); | ||
| } catch (InterruptedException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| blockUntilChildSpansFinished(1); | ||
| } | ||
| }.run(); | ||
|
|
||
| assertTraces( | ||
| trace( | ||
| span().root().operationName("parent"), | ||
| span().childOfPrevious().operationName("child"))); | ||
| } | ||
|
|
||
| @DisplayName("test concurrent virtual threads with remount") | ||
| @Test | ||
| void testConcurrentVirtualThreadsWithRemount() { | ||
| int threadCount = 5; | ||
| String[] spanId = new String[1]; | ||
| String[] spanIdsAfterRemount = new String[threadCount]; | ||
|
|
||
| new Runnable() { | ||
| @Override | ||
| @Trace(operationName = "parent") | ||
| public void run() { | ||
| spanId[0] = CorrelationIdentifier.getSpanId(); | ||
|
|
||
| List<Thread> threads = new ArrayList<>(); | ||
| for (int i = 0; i < threadCount; i++) { | ||
| int index = i; | ||
| threads.add( | ||
| Thread.startVirtualThread( | ||
| () -> { | ||
| tryUnmount(); | ||
| spanIdsAfterRemount[index] = CorrelationIdentifier.getSpanId(); | ||
| })); | ||
| } | ||
|
|
||
| for (Thread thread : threads) { | ||
| try { | ||
| thread.join(TIMEOUT); | ||
| } catch (InterruptedException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } | ||
| }.run(); | ||
|
|
||
| for (int i = 0; i < threadCount; i++) { | ||
| assertEquals( | ||
| spanId[0], | ||
| spanIdsAfterRemount[i], | ||
| "context should be restored after virtual thread #" + i + "remounts"); | ||
| } | ||
|
|
||
| assertTraces(trace(span().root().operationName("parent"))); | ||
| } | ||
|
|
||
| @DisplayName("test no context virtual thread remount") | ||
| @Test | ||
| void testNoContextVirtualThreadRemount() throws InterruptedException { | ||
| AtomicReference<String> spanIdBeforeUnmount = new AtomicReference<>(); | ||
| AtomicReference<String> spanIdAfterRemount = new AtomicReference<>(); | ||
|
|
||
| Thread.startVirtualThread( | ||
| () -> { | ||
| spanIdBeforeUnmount.set(CorrelationIdentifier.getSpanId()); | ||
| tryUnmount(); | ||
| spanIdAfterRemount.set(CorrelationIdentifier.getSpanId()); | ||
| }) | ||
| .join(TIMEOUT); | ||
|
|
||
| assertEquals( | ||
| "0", spanIdBeforeUnmount.get(), "there should be no active context before unmount"); | ||
| assertEquals("0", spanIdAfterRemount.get(), "there should be no active context after remount"); | ||
| } | ||
|
|
||
| private static void tryUnmount() { | ||
| try { | ||
| // Multiple sleeps to expect triggering repeated park/unpark cycles. | ||
| // This is not guaranteed to work, but there is no API to force mount/unmount. | ||
| for (int i = 0; i < 5; i++) { | ||
| Thread.sleep(10); | ||
| } | ||
| } catch (InterruptedException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Why does the method
throws InterruptedException,when in the body it catches and wraps
InterruptedExceptionin aRuntimeExceptionThere 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.
Ahah, no, that was not AI generated. I took it from the original reproducer: https://github.com/DataDog/dd-trace-java/pull/10887/changes#diff-7981a7fa278247a76ac7f99feef1b2ae6fd4dc3dabd694140324fbb22083ea1dR145 but I clearly forgot to clean it up.
And as I duplicated the test cases, it propagated along all test methods. I will clean it up, thanks for catching it!