-
Notifications
You must be signed in to change notification settings - Fork 338
Make reactor-core / reactive-streams context propagation Context-first #11578
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
3
commits into
master
Choose a base branch
from
andrea.marziali/redesign-reactor
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
62 changes: 62 additions & 0 deletions
62
...java/datadog/trace/instrumentation/reactivestreams/ReactiveStreamsContextPropagation.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,62 @@ | ||
| package datadog.trace.instrumentation.reactivestreams; | ||
|
|
||
| import datadog.context.Context; | ||
| import datadog.context.ContextScope; | ||
| import datadog.trace.bootstrap.ContextStore; | ||
| import org.reactivestreams.Publisher; | ||
| import org.reactivestreams.Subscriber; | ||
|
|
||
| public final class ReactiveStreamsContextPropagation { | ||
|
|
||
| private ReactiveStreamsContextPropagation() {} | ||
|
|
||
| public static ContextScope captureOnSubscribe( | ||
| final Publisher<?> publisher, | ||
| final Subscriber<?> subscriber, | ||
| final ContextStore<Publisher, Context> publisherContexts, | ||
| final ContextStore<Subscriber, Context> subscriberContexts) { | ||
| // Don't consume the publisher context until we've verified the subscriber is non-null. For | ||
| // subscribe(null), Reactive Streams mandates an NPE after this advice returns. Consuming the | ||
| // context earlier would incorrectly discard it. | ||
| if (subscriber == null) { | ||
| return null; | ||
| } | ||
|
|
||
| final Context contextFromPublisher = publisherContexts.remove(publisher); | ||
| final Context activeContext = Context.current(); | ||
| final Context context = contextFromPublisher != null ? contextFromPublisher : activeContext; | ||
| if (context == Context.root()) { | ||
| return null; | ||
| } | ||
|
|
||
| final Context subscriberContext = subscriberContexts.putIfAbsent(subscriber, context); | ||
| // A context captured on the publisher (cross-thread propagation) must win even when the | ||
| // current thread already carries a non-root active context. | ||
| return attachIfRequired(subscriberContext, activeContext, true); | ||
| } | ||
|
|
||
| public static ContextScope activateOnSignal( | ||
| final Subscriber<?> subscriber, final ContextStore<Subscriber, Context> subscriberContexts) { | ||
| final Context activeContext = Context.current(); | ||
| if (activeContext != Context.root()) { | ||
| return null; | ||
| } | ||
| return attachIfRequired(subscriberContexts.get(subscriber), activeContext, false); | ||
| } | ||
|
|
||
| public static ContextScope activateOnComplete( | ||
| final Subscriber<?> subscriber, final ContextStore<Subscriber, Context> subscriberContexts) { | ||
| return attachIfRequired(subscriberContexts.get(subscriber), Context.current(), true); | ||
| } | ||
|
|
||
| private static ContextScope attachIfRequired( | ||
| final Context context, final Context activeContext, final boolean allowReplacingActive) { | ||
| if (context == null || context == activeContext || context == Context.root()) { | ||
| return null; | ||
| } | ||
| if (!allowReplacingActive && activeContext != Context.root()) { | ||
| return null; | ||
| } | ||
| return context.attach(); | ||
| } | ||
| } | ||
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
158 changes: 158 additions & 0 deletions
158
.../datadog/trace/instrumentation/reactivestreams/ReactiveStreamsContextPropagationTest.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,158 @@ | ||
| package datadog.trace.instrumentation.reactivestreams; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
|
|
||
| import datadog.context.Context; | ||
| import datadog.context.ContextKey; | ||
| import datadog.context.ContextScope; | ||
| import datadog.trace.bootstrap.ContextStore; | ||
| import java.util.IdentityHashMap; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.reactivestreams.Publisher; | ||
| import org.reactivestreams.Subscriber; | ||
| import org.reactivestreams.Subscription; | ||
|
|
||
| class ReactiveStreamsContextPropagationTest { | ||
|
|
||
| private static final ContextKey<String> KEY = ContextKey.named("reactive-streams-test"); | ||
|
|
||
| @Test | ||
| void publisherCapturedContextOverridesActiveContext() { | ||
| final Publisher<Object> publisher = subscriber -> {}; | ||
| final Subscriber<Object> subscriber = new NoopSubscriber(); | ||
| final ContextStore<Publisher, Context> publisherContexts = new MapContextStore<>(); | ||
| final ContextStore<Subscriber, Context> subscriberContexts = new MapContextStore<>(); | ||
|
|
||
| // A context was captured on the publisher (e.g. at assembly / cross-thread subscribe). | ||
| final Context captured = Context.root().with(KEY, "captured"); | ||
| publisherContexts.put(publisher, captured); | ||
|
|
||
| // The current thread already carries a different, non-root active context. | ||
| final Context active = Context.root().with(KEY, "active"); | ||
| try (ContextScope activeScope = active.attach()) { | ||
| assertSame(active, Context.current()); | ||
|
|
||
| final ContextScope scope = | ||
| ReactiveStreamsContextPropagation.captureOnSubscribe( | ||
| publisher, subscriber, publisherContexts, subscriberContexts); | ||
| try { | ||
| // The captured context must win over the ambient active one | ||
| assertNotNull(scope, "captured context should be attached over the active context"); | ||
| assertSame(captured, Context.current()); | ||
| } finally { | ||
| if (scope != null) { | ||
| scope.close(); | ||
| } | ||
| } | ||
|
|
||
| // Closing the scope restores the previously active context. | ||
| assertSame(active, Context.current()); | ||
| } | ||
|
|
||
| // The captured context is remembered for the subscriber, and removed from the publisher store. | ||
| assertSame(captured, subscriberContexts.get(subscriber)); | ||
| assertNull(publisherContexts.get(publisher)); | ||
| } | ||
|
|
||
| @Test | ||
| void signalActivationIsSkippedWhenAnotherContextIsActive() { | ||
| final Subscriber<Object> subscriber = new NoopSubscriber(); | ||
| final ContextStore<Subscriber, Context> subscriberContexts = new MapContextStore<>(); | ||
| subscriberContexts.put(subscriber, Context.root().with(KEY, "stored")); | ||
|
|
||
| final Context active = Context.root().with(KEY, "active"); | ||
| try (ContextScope activeScope = active.attach()) { | ||
| final ContextScope scope = | ||
| ReactiveStreamsContextPropagation.activateOnSignal(subscriber, subscriberContexts); | ||
| assertNull(scope, "must not override an already-active non-root context on a signal"); | ||
| assertSame(active, Context.current()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void signalActivationAttachesStoredContextWhenIdle() { | ||
| final Subscriber<Object> subscriber = new NoopSubscriber(); | ||
| final ContextStore<Subscriber, Context> subscriberContexts = new MapContextStore<>(); | ||
| final Context stored = Context.root().with(KEY, "stored"); | ||
| subscriberContexts.put(subscriber, stored); | ||
|
|
||
| final ContextScope scope = | ||
| ReactiveStreamsContextPropagation.activateOnSignal(subscriber, subscriberContexts); | ||
| try { | ||
| assertNotNull(scope); | ||
| assertSame(stored, Context.current()); | ||
| } finally { | ||
| if (scope != null) { | ||
| scope.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static final class NoopSubscriber implements Subscriber<Object> { | ||
| @Override | ||
| public void onSubscribe(final Subscription subscription) {} | ||
|
|
||
| @Override | ||
| public void onNext(final Object value) {} | ||
|
|
||
| @Override | ||
| public void onError(final Throwable throwable) {} | ||
|
|
||
| @Override | ||
| public void onComplete() {} | ||
| } | ||
|
|
||
| private static final class MapContextStore<K, C> implements ContextStore<K, C> { | ||
| private final Map<K, C> map = new IdentityHashMap<>(); | ||
|
|
||
| @Override | ||
| public C get(final K key) { | ||
| return map.get(key); | ||
| } | ||
|
|
||
| @Override | ||
| public void put(final K key, final C context) { | ||
| map.put(key, context); | ||
| } | ||
|
|
||
| @Override | ||
| public C putIfAbsent(final K key, final C context) { | ||
| final C existing = map.get(key); | ||
| if (existing != null) { | ||
| return existing; | ||
| } | ||
| map.put(key, context); | ||
| return context; | ||
| } | ||
|
|
||
| @Override | ||
| public C putIfAbsent(final K key, final Factory<C> contextFactory) { | ||
| final C existing = map.get(key); | ||
| if (existing != null) { | ||
| return existing; | ||
| } | ||
| final C created = contextFactory.create(); | ||
| map.put(key, created); | ||
| return created; | ||
| } | ||
|
|
||
| @Override | ||
| public C computeIfAbsent(final K key, final KeyAwareFactory<? super K, C> contextFactory) { | ||
| final C existing = map.get(key); | ||
| if (existing != null) { | ||
| return existing; | ||
| } | ||
| final C created = contextFactory.create(key); | ||
| map.put(key, created); | ||
| return created; | ||
| } | ||
|
|
||
| @Override | ||
| public C remove(final K key) { | ||
| return map.remove(key); | ||
| } | ||
| } | ||
| } |
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
55 changes: 0 additions & 55 deletions
55
...-core-3.1/src/main/java/datadog/trace/instrumentation/reactor/core/ContextSpanHelper.java
This file was deleted.
Oops, something went wrong.
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.