-
Notifications
You must be signed in to change notification settings - Fork 340
Provide ability to capture continuations using the Context API #11663
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
mcculls
wants to merge
11
commits into
master
Choose a base branch
from
mcculls/context-continuations
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
11 commits
Select commit
Hold shift + click to select a range
ba13f41
Provide ability to capture continuations using the Context API
mcculls 235d988
Clean up use of EmptyContext.INSTANCE
mcculls a2d6b5e
Apply @ParametersAreNonnullByDefault to test listeners
mcculls 0a3d354
Explain choice of rehash value
mcculls f453621
Extend context/scope tests
mcculls fc11418
Introduce ContextHolder to hold/update current context without the ov…
mcculls 1d73e24
Replace weak-ref NoopContextScope cache with simple allocation
mcculls c3c6acb
Greatly reduce allocations by treating existing context containers as…
mcculls df32a37
Add microbenchmarks to exercise Context API for both old and new impl…
mcculls e00a5f0
Refactor no-op scope approach
mcculls f8d0935
Allow contexts to wrap themselves as scopes without attaching.
mcculls 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
79 changes: 79 additions & 0 deletions
79
components/context/src/main/java/datadog/context/ContextContinuation.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,79 @@ | ||
| package datadog.context; | ||
|
|
||
| /** | ||
| * Captures a context attached to one execution unit so it can be resumed in another. | ||
| * | ||
| * <p>To propagate context to a single background task: | ||
| * | ||
| * <pre>{@code | ||
| * ContextContinuation continuation = Context.current().capture(); | ||
| * executor.execute(() -> { | ||
| * try (ContextScope scope = continuation.resume()) { | ||
| * // ... Context.current() here returns the captured context | ||
| * } | ||
| * // context implicitly released from continuation when resumed scope closes | ||
| * }); | ||
| * }</pre> | ||
| * | ||
| * <p>If a continuation is never resumed (e.g. a task is cancelled before it runs), you must release | ||
| * it explicitly to avoid resource leaks: | ||
| * | ||
| * <pre>{@code | ||
| * ContextContinuation continuation = Context.current().capture(); | ||
| * Future<?> future = executor.submit(() -> { | ||
| * try (ContextScope scope = continuation.resume()) { | ||
| * // ... | ||
| * } | ||
| * }); | ||
| * // ... | ||
| * if (future.cancel(false)) { | ||
| * continuation.release(); // task will never resume, so release manually | ||
| * } | ||
| * }</pre> | ||
| * | ||
| * <p>When the same context is resumed concurrently across multiple threads, call {@link #hold()} | ||
| * immediately after capture to prevent the first {@link #resume()} from releasing the context: | ||
| * | ||
| * <pre>{@code | ||
| * ContextContinuation continuation = Context.current().capture().hold(); | ||
| * for (int i = 0; i < N; i++) { | ||
| * executor.execute(() -> { | ||
| * try (ContextScope scope = continuation.resume()) { | ||
| * // ... | ||
| * } | ||
| * }); | ||
| * } | ||
| * // ... | ||
| * continuation.release(); // remember to release the hold once all tasks are resumed/done | ||
| * }</pre> | ||
| */ | ||
| public interface ContextContinuation { | ||
|
mcculls marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Optional builder method to stop {@link #resume()} from implicitly releasing the captured | ||
| * context. This is useful when multiple threads may concurrently resume the context. You must | ||
| * then explicitly {@link #release() release} the context once all threads are resumed/done. | ||
| * | ||
| * @return this continuation, but with implicit release-after-resume turned off. | ||
| */ | ||
| ContextContinuation hold(); | ||
|
mcculls marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Returns the context captured by this continuation. | ||
| * | ||
| * @return the captured context. | ||
| */ | ||
| Context context(); | ||
|
|
||
| /** | ||
| * Resumes the context captured by this continuation by attaching it to the current execution | ||
| * unit. Implicitly {@link #release() releases} the captured context at the end of the resumed | ||
| * scope, unless {@link #hold()} was called when creating the continuation. | ||
| * | ||
| * @return a scope to be closed when the resumed context is invalid. | ||
| */ | ||
| ContextScope resume(); | ||
|
|
||
| /** Explicitly releases the context captured by this continuation. */ | ||
| void release(); | ||
| } | ||
33 changes: 33 additions & 0 deletions
33
components/context/src/main/java/datadog/context/ContextListener.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,33 @@ | ||
| package datadog.context; | ||
|
|
||
| /** Listener of context events. */ | ||
| public interface ContextListener { | ||
|
|
||
| /** | ||
| * Notifies that the given context has been attached to the current execution unit. | ||
| * | ||
| * @param context the attached context. | ||
| */ | ||
| default void onAttach(Context context) {} | ||
|
|
||
| /** | ||
| * Notifies that the given context has been detached from the current execution unit. | ||
| * | ||
| * @param context the detached context. | ||
| */ | ||
| default void onDetach(Context context) {} | ||
|
|
||
| /** | ||
| * Notifies that the given context has been captured by a continuation. | ||
| * | ||
| * @param context the captured context. | ||
| */ | ||
| default void onCapture(Context context) {} | ||
|
|
||
| /** | ||
| * Notifies that the given context has been released from a continuation. | ||
| * | ||
| * @param context the released context. | ||
| */ | ||
| default void onRelease(Context context) {} | ||
|
mcculls marked this conversation as resolved.
|
||
| } | ||
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
3 changes: 0 additions & 3 deletions
3
components/context/src/main/java/datadog/context/ImplicitContextKeyed.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
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
31 changes: 31 additions & 0 deletions
31
components/context/src/main/java/datadog/context/NoopContextContinuation.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,31 @@ | ||
| package datadog.context; | ||
|
|
||
| /** {@link ContextContinuation} that has no effect on execution units. */ | ||
| final class NoopContextContinuation implements ContextContinuation, ContextScope { | ||
| private final Context context; | ||
|
|
||
| NoopContextContinuation(Context context) { | ||
| this.context = context; | ||
| } | ||
|
|
||
| @Override | ||
| public ContextContinuation hold() { | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public Context context() { | ||
| return context; | ||
| } | ||
|
|
||
| @Override | ||
| public ContextScope resume() { | ||
| return this; // acts as no-op scope, avoiding allocation | ||
| } | ||
|
|
||
| @Override | ||
| public void release() {} | ||
|
|
||
| @Override | ||
| public void close() {} | ||
| } |
18 changes: 18 additions & 0 deletions
18
components/context/src/main/java/datadog/context/NoopContextScope.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,18 @@ | ||
| package datadog.context; | ||
|
|
||
| /** {@link ContextScope} that has no effect on execution units. */ | ||
| final class NoopContextScope implements ContextScope { | ||
| private final Context context; | ||
|
|
||
| NoopContextScope(Context context) { | ||
| this.context = context; | ||
| } | ||
|
|
||
| @Override | ||
| public Context context() { | ||
| return context; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() {} | ||
| } |
17 changes: 17 additions & 0 deletions
17
components/context/src/main/java/datadog/context/SelfScopedContext.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,17 @@ | ||
| package datadog.context; | ||
|
|
||
| /** Context that acts as its own unattached scope. */ | ||
| interface SelfScopedContext extends Context, ContextScope { | ||
| @Override | ||
| default ContextScope asScope() { | ||
| return this; // acts as no-op scope, avoiding allocation | ||
| } | ||
|
|
||
| @Override | ||
| default Context context() { | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| default void close() {} | ||
| } |
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
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.
I'm concerned by the requirement to explicitly release.
There are places where that's hard to do.
I also think it complicates handling of dispatch loops. For dispatch loops, I rather guard the loop with ignoring incoming context - rather than stopping it on the originating end.
I say that because dispatch loops are often set-up lazily with a varied set of construction points, so defending against all of them seems messier.
I think if we could come up with a system without this requirement; it would clean-up instrumentation considerably. Right now, I feel like we're taking a step backwards.
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.
TBH that decision is best left up to the consumer of the capture and release events.
The code here as it stands won't cause any resource leak per se (the continuation is not kept in any internal cache) - what it will do is delay calling the release event until the conditions have been met.
Now the tracer, as a consumer of that event, can decide to forgo waiting on the release event - if it determines that there is either no need to hold back the trace, or that continuing to wait will cause resource issues. It is better placed to make that decision because it knows how many traces are pending, etc.
This avoids overcomplicating the context layer while still allowing downstream consumers to decide how to handle long-running situations. The alternative would be to try and use some kind of weak reference, which would introduce other issues around liveliness.
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.
I think it would be better to complicate the context layer rather than complicating the numerous instrumentations. But mostly, I just wish we explored this as a group before the decision was made.
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.
No, this is about the consumer of the events - i.e. the tracer core - not instrumentations.
Instrumentations are what trigger these events, which is why I want to keep the context layer simple - rather than second guessing when to bypass the "rules" and send additional events to the tracer core.
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.
I'm not proposing that the context layer second guess when to bypass the rules.
But what I'm saying is that a rule that assumes that the user code is well-behaved is problematic, I don't regard that as a bypass. For me, it is just about maintaining our requirement that we "do no harm" even when the user code is misbehaving.
So I'm just asking for the system to be resilient to misuse, that doesn't necessarily mean doing some fix-up -- just not allowing for runaway memory consumption.