IGNITE-26608 Added a unified mechanism for propagating Context Attributes#12429
IGNITE-26608 Added a unified mechanism for propagating Context Attributes#12429petrov-mg wants to merge 6 commits intoapache:masterfrom
Conversation
| * @return Security context holder. | ||
| * @return Thread Context Scope. | ||
| * | ||
| * @see #withContext(Scope, SecurityContext |
There was a problem hiding this comment.
| * @see #withContext(Scope, SecurityContext | |
| * @see #withContext(Scope, SecurityContext) |
9bf30e8 to
324206b
Compare
da14337 to
8795941
Compare
|
|
||
| /** */ | ||
| <T> T get(ThreadContextAttribute<T> attr) { | ||
| if (attr.id() >= attrs.length) |
There was a problem hiding this comment.
Shoulud we throw assert instead? Is it correct to access to the attribute we actually didn't set?
Could it lead to an error, when user forget to set the var and received default value instead?
There was a problem hiding this comment.
No, we shouldn't. The initial value of an attribute literally means - the value returned by the ThreadContext#get method if the attribute's value is not explicitly set.
It can be used to skip explicitly setting attributes for some internal operations/workers and still get some value for attribute.
| } | ||
| public interface Scope extends AutoCloseable { | ||
| /** Binds attribute with specified value to the current scope. */ | ||
| public <T> Scope withAttribute(ThreadContextAttribute<T> attr, T val); |
There was a problem hiding this comment.
Broken incapsulation. ThreadContextScope inherits Scope, then ThreadContextAttribute must be part of ThreadContextScrope.
There was a problem hiding this comment.
It’s more of a naming problem than an “encapsulation.” Anyway, I’ve updated the PR and removed the Scope interface entirely — on second thought, it’s redundant for the current patch. So now only ThreadContextScope remains.
da9cc26 to
a5347c7
Compare
modules/commons/src/main/java/org/apache/ignite/internal/thread/context/Scope.java
Show resolved
Hide resolved
modules/commons/src/main/java/org/apache/ignite/internal/thread/context/ScopedContext.java
Outdated
Show resolved
Hide resolved
modules/commons/src/main/java/org/apache/ignite/internal/thread/context/ContextAttribute.java
Show resolved
Hide resolved
| /** */ | ||
| private ContextAttribute(byte id, T initVal) { | ||
| this.id = id; | ||
| this.bitmask = 1 << id; |
There was a problem hiding this comment.
Looks that storing ID is enough. The bitmask is a parameter of ContextDataChain only.
There was a problem hiding this comment.
Nope. The bitmask is used in many places to check for the presence of an attribute value. Therefore, it's better to calculate it once and store it in the attribute instance.
modules/commons/src/main/java/org/apache/ignite/internal/thread/context/ContextAttribute.java
Outdated
Show resolved
Hide resolved
| } | ||
|
|
||
| /** */ | ||
| public <T> ScopedContext with(ContextAttribute<T> attr, T val) { |
There was a problem hiding this comment.
Looks weird, an object that stores an attribute value accepts another attribute (name + value). AFAIU, it's a responsibility of Scope to store all attributes within single scope.
There was a problem hiding this comment.
it's a responsibility of Scope to store all attributes within single scope
Nope. See comments above.
I changed class naming a bit so it may be less confusing now.
Looks weird, an object that stores an attribute value accepts another attribute (name + value)
Also keep in mind that the current patch is based on intrusive lists and it is unlikely to be possible to avoid the "weirdness" you mentioned.
0930d07 to
fcbcc57
Compare
fcbcc57 to
df78984
Compare
|
31be217 to
98f14f7
Compare
modules/commons/src/main/java/org/apache/ignite/internal/thread/context/Context.java
Outdated
Show resolved
Hide resolved
modules/commons/src/main/java/org/apache/ignite/internal/thread/context/Context.java
Outdated
Show resolved
Hide resolved
modules/commons/src/main/java/org/apache/ignite/internal/thread/context/Context.java
Outdated
Show resolved
Hide resolved
| * try-with-resource block to close the returned Scope. Note, updates must be undone in the same order they were applied. | ||
| */ | ||
| public static <T> Scope set(ContextAttribute<T> attr, T val) { | ||
| if (get(attr) == val) |
There was a problem hiding this comment.
WDYT, can we move this optimization into applyUpdates? Currently, there are 2 calls of INSTANCE.get(), that the most expensive part.
|
|
||
| /** */ | ||
| <T> ContextUpdater set(ContextAttribute<T> attr, T val) { | ||
| if (get(attr) == val) |
There was a problem hiding this comment.
Same here, can we check it later, within Context?
modules/commons/src/main/java/org/apache/ignite/internal/thread/context/Context.java
Show resolved
Hide resolved
modules/commons/src/main/java/org/apache/ignite/internal/thread/context/Context.java
Show resolved
Hide resolved
| "already used: " + id); | ||
|
|
||
| extPools[id] = ctx.security().enabled() ? new SecurityAwareIoPool(ctx.security(), ex) : ex; | ||
| extPools[id] = ctx.security().enabled() ? ContextAwareIoPool.wrap(ex) : ex; |
There was a problem hiding this comment.
Here and below: looks like we should not check security anymore and use ContextAwareIoPool everywhere?
There was a problem hiding this comment.
We will definitely do this, but in a separate ticket.
...s/src/main/java/org/apache/ignite/internal/thread/context/function/ContextAwareCallable.java
Show resolved
Hide resolved
| } | ||
|
|
||
| /** Updates the current context with the specified attributes and their corresponding values. */ | ||
| private Scope applyAttributeUpdates(AttributeValueHolder<?>... atrVals) { |
There was a problem hiding this comment.
Misprint, atrVals -> attrVals
modules/commons/src/main/java/org/apache/ignite/internal/thread/context/Context.java
Show resolved
Hide resolved
modules/commons/src/main/java/org/apache/ignite/internal/thread/context/Context.java
Show resolved
Hide resolved
|
|
||
| changeState(prevSnp, newSnp); | ||
|
|
||
| return () -> INSTANCE.get().changeState(newSnp, prevSnp); |
There was a problem hiding this comment.
It looks it should work with () -> changeState(newSnp, prevSnp). Because we close Scope within the same thread that restores snapshot.
4244313 to
9eeac8d
Compare
9eeac8d to
9b8f71f
Compare
9b8f71f to
5df3c84
Compare
|



Thank you for submitting the pull request to the Apache Ignite.
In order to streamline the review of the contribution
we ask you to ensure the following steps have been taken:
The Contribution Checklist
The description explains WHAT and WHY was made instead of HOW.
The following pattern must be used:
IGNITE-XXXX Change summarywhereXXXX- number of JIRA issue.(see the Maintainers list)
the
green visaattached to the JIRA ticket (see TC.Bot: Check PR)Notes
If you need any help, please email dev@ignite.apache.org or ask anу advice on http://asf.slack.com #ignite channel.