Repository navigation
Clear Reactor Netty request context when pooled connections are released #12763
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
Open
amarziali
wants to merge
1
commit into
master
Choose a base branch
from
andrea.marziali/nett-test-race
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
12 changes: 11 additions & 1 deletion
12
...1.0/src/main/java/datadog/trace/instrumentation/reactor/netty/AfterConstructorAdvice.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 |
|---|---|---|
| @@ -1,14 +1,24 @@ | ||
| package datadog.trace.instrumentation.reactor.netty; | ||
|
|
||
| import datadog.context.Context; | ||
| import datadog.trace.bootstrap.ContextStore; | ||
| import datadog.trace.bootstrap.InstrumentationContext; | ||
| import net.bytebuddy.asm.Advice; | ||
| import reactor.netty.http.client.HttpClient; | ||
| import reactor.netty.http.client.HttpClientRequest; | ||
|
|
||
| public class AfterConstructorAdvice { | ||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit( | ||
| @Advice.Thrown Throwable throwable, @Advice.Return(readOnly = false) HttpClient client) { | ||
| if (null == throwable) { | ||
| client = client.mapConnect(new CaptureConnectSpan()).doOnRequest(new TransferConnectSpan()); | ||
| ContextStore<HttpClientRequest, Context> requestContexts = | ||
| InstrumentationContext.get(HttpClientRequest.class, Context.class); | ||
| client = | ||
| client | ||
| .mapConnect(new CaptureConnectSpan()) | ||
| .doOnRequest(new TransferConnectSpan(requestContexts)) | ||
| .doOnDisconnected(new ClearRequestContext(requestContexts)); | ||
| } | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
...ty-1.0/src/main/java/datadog/trace/instrumentation/reactor/netty/ClearRequestContext.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,48 @@ | ||
| package datadog.trace.instrumentation.reactor.netty; | ||
|
|
||
| import static datadog.trace.instrumentation.netty41.AttributeKeys.CLIENT_PARENT_ATTRIBUTE_KEY; | ||
| import static datadog.trace.instrumentation.netty41.AttributeKeys.CONTEXT_ATTRIBUTE_KEY; | ||
|
|
||
| import datadog.context.Context; | ||
| import datadog.trace.bootstrap.ContextStore; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import io.netty.channel.Channel; | ||
| import io.netty.util.Attribute; | ||
| import java.util.function.Consumer; | ||
| import reactor.netty.Connection; | ||
| import reactor.netty.channel.ChannelOperations; | ||
| import reactor.netty.http.client.HttpClientRequest; | ||
|
|
||
| /** Clears request context when a connection is released or disconnected. */ | ||
| public class ClearRequestContext implements Consumer<Connection> { | ||
| private final ContextStore<HttpClientRequest, Context> requestContexts; | ||
|
|
||
| public ClearRequestContext(ContextStore<HttpClientRequest, Context> requestContexts) { | ||
| this.requestContexts = requestContexts; | ||
| } | ||
|
|
||
| @Override | ||
| public void accept(Connection connection) { | ||
| if (!(connection instanceof HttpClientRequest)) { | ||
| return; | ||
| } | ||
| Context requestContext = requestContexts.remove((HttpClientRequest) connection); | ||
| Channel channel = connection.channel(); | ||
| ChannelOperations<?, ?> current = ChannelOperations.get(channel); | ||
| if (current != null && current != connection) { | ||
| return; // The pooled channel already belongs to another request. | ||
| } | ||
| AgentSpan parent = AgentSpan.fromContext(requestContext); | ||
|
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. IIUC there may not always be an AgentSpan when context is captured, e.g. with only baggage? Should we handle saved contexts without spans too? |
||
| if (parent == null) { | ||
| return; | ||
| } | ||
| Attribute<Context> attribute = channel.attr(CONTEXT_ATTRIBUTE_KEY); | ||
| Context stored = attribute.get(); | ||
| // Leave a live client span for Netty's error/close handler to finish. | ||
| if (stored != null | ||
| && AgentSpan.fromContext(stored) == parent | ||
| && attribute.compareAndSet(stored, null)) { | ||
| channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).compareAndSet(parent, null); | ||
| } | ||
| } | ||
| } | ||
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
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
83 changes: 83 additions & 0 deletions
83
....0/src/test/java/datadog/trace/instrumentation/reactor/netty/ClearRequestContextTest.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,83 @@ | ||
| package datadog.trace.instrumentation.reactor.netty; | ||
|
|
||
| import static datadog.trace.instrumentation.netty41.AttributeKeys.CLIENT_PARENT_ATTRIBUTE_KEY; | ||
| import static datadog.trace.instrumentation.netty41.AttributeKeys.CONTEXT_ATTRIBUTE_KEY; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.CALLS_REAL_METHODS; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.mockito.Mockito.withSettings; | ||
|
|
||
| import datadog.context.Context; | ||
| import datadog.trace.bootstrap.ContextStore; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import io.netty.channel.Channel; | ||
| import io.netty.util.DefaultAttributeMap; | ||
| import org.junit.jupiter.api.Test; | ||
| import reactor.netty.Connection; | ||
| import reactor.netty.channel.ChannelOperations; | ||
| import reactor.netty.http.client.HttpClientRequest; | ||
|
|
||
| class ClearRequestContextTest { | ||
| @Test | ||
| void clearsReleasedRequestContext() { | ||
| Fixture fixture = new Fixture(); | ||
|
|
||
| fixture.cleanup.accept(fixture.connection); | ||
|
|
||
| assertNull(fixture.channel.attr(CONTEXT_ATTRIBUTE_KEY).get()); | ||
| assertNull(fixture.channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); | ||
| } | ||
|
|
||
| @Test | ||
| void leavesUnfinishedClientSpanForNettyCleanup() { | ||
| Fixture fixture = new Fixture(); | ||
| Context clientContext = Context.root().with(mock(AgentSpan.class, CALLS_REAL_METHODS)); | ||
| fixture.channel.attr(CONTEXT_ATTRIBUTE_KEY).set(clientContext); | ||
|
|
||
| fixture.cleanup.accept(fixture.connection); | ||
|
|
||
| assertSame(clientContext, fixture.channel.attr(CONTEXT_ATTRIBUTE_KEY).get()); | ||
| assertSame(fixture.parent, fixture.channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); | ||
| } | ||
|
|
||
| @Test | ||
| void preservesReusedChannelEvenWhenNextRequestHasTheSameParent() { | ||
| Fixture fixture = new Fixture(); | ||
| ChannelOperations<?, ?> nextRequest = mock(ChannelOperations.class); | ||
| when(nextRequest.as(ChannelOperations.class)).thenReturn(nextRequest); | ||
| Connection.from(fixture.channel).rebind(nextRequest); | ||
|
|
||
| fixture.cleanup.accept(fixture.connection); | ||
|
|
||
| assertSame(fixture.context, fixture.channel.attr(CONTEXT_ATTRIBUTE_KEY).get()); | ||
| assertSame(fixture.parent, fixture.channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).get()); | ||
| } | ||
|
|
||
| private static class Fixture { | ||
| final AgentSpan parent = mock(AgentSpan.class, CALLS_REAL_METHODS); | ||
| final Context context = Context.root().with(parent); | ||
| final Channel channel = mock(Channel.class); | ||
| final HttpClientRequest request = | ||
| mock(HttpClientRequest.class, withSettings().extraInterfaces(Connection.class)); | ||
| final Connection connection = (Connection) request; | ||
| final ClearRequestContext cleanup; | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| Fixture() { | ||
| DefaultAttributeMap attributes = new DefaultAttributeMap(); | ||
| when(channel.hasAttr(any())) | ||
| .thenAnswer(invocation -> attributes.hasAttr(invocation.getArgument(0))); | ||
| when(channel.attr(any())) | ||
| .thenAnswer(invocation -> attributes.attr(invocation.getArgument(0))); | ||
| when(connection.channel()).thenReturn(channel); | ||
| channel.attr(CONTEXT_ATTRIBUTE_KEY).set(context); | ||
| channel.attr(CLIENT_PARENT_ATTRIBUTE_KEY).set(parent); | ||
| ContextStore<HttpClientRequest, Context> contexts = mock(ContextStore.class); | ||
| when(contexts.remove(request)).thenReturn(context); | ||
| cleanup = new ClearRequestContext(contexts); | ||
| } | ||
| } | ||
| } |
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.
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.
Codex recommends to arrange cleanup after Netty finishes the span - I think the suggestion makes sense from the description below:
This section "removes the saved request context before checking whether the client span is still live. With an outgoing request-body failure, the disconnect callback runs while that span is live and skips clearing the attributes. Netty subsequently finishes the client span and restores the parent context. Later cleanup callbacks cannot remove it because the saved context is gone or the connection is no longer an
HttpClientRequest. Subsequent channel events can still reactivate that parent."