DUE TO SPAM, SIGN-UP IS DISABLED. Goto Selfserve wiki signup and request an account.
DUE TO SPAM, SIGN-UP IS DISABLED. Goto Selfserve wiki signup and request an account.
| ID | IEP-143 | ||||||||
| Author | |||||||||
| Sponsor | |||||||||
| Created |
| ||||||||
| Status |
|
| Table of Contents |
|---|
...
Examples of context propagation problems:
This IEP is intended to provide a robust mechanism for propagating arbitrary operation context that preserves the convenience of the ThreadLocal approach while minimizing the risk of operation context loss in a multithreaded environment.
...
Example of ThreadLocal storage API:
| Code Block | ||||
|---|---|---|---|---|
| ||||
public class Context { /** * Retrieves value associated with specified attribute by accessing Context bound to the thread this method is * called from. If no value is explicitly associated with specified attribute, {@link ContextAttribute#initialValue()} * is returned. * * @param attr Context Attribute. * @return Context Attribute Value. */ @Nullable public static <T> T get(ContextAttribute<T> attr); /** * Updates the value of the specified attribute for the Context bound to the thread this method is called from. * * @param attr Context Attribute. * @return Scope instance that, when closed, undoes the applied update. It is crucial to undo all applied Context * updates to free up thread-bound resources and avoid memory leaks, so it is highly encouraged to use a * try-with-resource block to close the returned Scope. Note, updates must be undone in the same order and in the * same thread they were applied. */ public static <T> Scope set(ContextAttribute<T> attr, T val); /** * Creates Snapshot of all attributes and their corresponding values stored in the Context bound to the thread this * method is called from. * * @return Context Snapshot. */ public static ContextSnapshot createSnapshot(); /** * Restores values of all attributes for Context bound to the thread this method is called from. * * @param snp Context Snapshot. * @return Scope instance that, when closed, undoes the applied operation. It is crucial to undo all applied Context * updates to free up thread-bound resources and avoid memory leaks, so it is highly encouraged to use a * try-with-resource block to close the returned Scope. Note, updates must be undone in the same order and in the * same thread they were applied. */ public static Scope restoreSnapshot(ContextSnapshot snp); } |
| Code Block | ||
|---|---|---|
| ||
/** */ private static final ContextAttribute<String> ATTR = ContextAttribute.newInstance("initial-value"); /** */ public static void main(String[] args) { ContextSnapshot snapshot; try (Scope scope = Context.set(ATTR, "test")) { String attrVal = Context.get(ATTR); } ContextSnapshot snapshot = Context.createSnapshot(); try (Scope scope = Context.restoreSnapshot(snapshot)) { String attrVal = Context.get(ATTR); } } |
// Describe project risks, such as API or binary compatibility issues, major protocol changes, etcThe proposed improvement allows to largely eliminate the problem of losing the context of an operation in a multi-threaded environment, but, unfortunately, does not solve this problem completely.
External libraries may provide the ability to submit code for multithreaded execution, but obviously do not support context propagation. Like JDK mechanism - java.util.stream.BaseStream#parallel. Misuse of such mechanisms can still result in loss of operation context and should be treated with caution.
Ubiquitous support for context capture and recovery may impact performance and GC. Although preliminary tests did not reveal any noticeable impact.
// Links to discussions on the devlist, if applicable.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ScopedValue.html
https://github.com/open-telemetry/opentelemetry-java/
...
tree/main/context/src/main/java/io/opentelemetry/context
https://projectreactor.io/docs/core/release/reference/advanched-contextPropagation.html
https://github.com/micrometer-metrics/context-propagation
https://logback.qos.ch/manual/mdc.html
| Jira | ||||||
|---|---|---|---|---|---|---|
|
// Links or report with relevant JIRA tickets.