Skip to content

Commit 8128d8c

Browse files
committed
1 parent ba7424f commit 8128d8c

4 files changed

Lines changed: 24 additions & 25 deletions

File tree

modules/commons/src/main/java/org/apache/ignite/internal/thread/context/ScopedAttributeValueStack.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,12 @@ T peek() {
6666
}
6767

6868
/** */
69-
ThreadContextSnapshot exportTo(ThreadContextSnapshot snapshot) {
69+
ThreadContextSnapshot exportTopTo(ThreadContextSnapshot snapshot) {
7070
T val = peek();
7171

7272
return val == attr.initialValue() ? snapshot : snapshot.withAttribute(attr, val);
7373
}
7474

75-
/** */
76-
void restoreInitial(int scopeDepth) {
77-
if (isEmpty() || scopedVals.peek().value() == attr.initialValue())
78-
return;
79-
80-
scopedVals.push(new ScopedAttributeValue<>(scopeDepth, attr.initialValue()));
81-
}
82-
8375
/** */
8476
boolean isEmpty() {
8577
return F.isEmpty(scopedVals);

modules/commons/src/main/java/org/apache/ignite/internal/thread/context/ThreadContextAttributeRegistry.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class ThreadContextAttributeRegistry {
2929
private final List<ThreadContextAttribute<?>> attrs = new CopyOnWriteArrayList<>();
3030

3131
/**
32-
* Registers attribute with initial value set to {@code null}.
32+
* Registers new attribute with initial value set to {@code null}.
3333
*
3434
* @see #register(Object)
3535
*/
@@ -38,13 +38,13 @@ public <T> ThreadContextAttribute<T> register() {
3838
}
3939

4040
/**
41-
* Registers attribute with specified initial value. Initial value is returned by
41+
* Registers new attribute with specified initial value. Initial value is returned by
4242
* {@link ThreadContext#get(ThreadContextAttribute)} method if attribute value is not set explicitly.
4343
* Returned value represents a key used to access attribute value via {@link ThreadContext#get(ThreadContextAttribute)}
4444
* or {@link ThreadContext#withAttribute(ThreadContextAttribute, Object)} methods.
4545
*
4646
* @param initialVal Attribute initial value.
47-
* @return Registered attribute instance.
47+
* @return Thread Context Attribute instance.
4848
*/
4949
public synchronized <T> ThreadContextAttribute<T> register(T initialVal) {
5050
ThreadContextAttribute<T> attr = new ThreadContextAttribute<>(attrs.size(), initialVal);
@@ -56,6 +56,8 @@ public synchronized <T> ThreadContextAttribute<T> register(T initialVal) {
5656

5757
/** */
5858
<T> ThreadContextAttribute<T> attribute(int id) {
59+
assert id < attrs.size();
60+
5961
return (ThreadContextAttribute<T>)attrs.get(id);
6062
}
6163

modules/commons/src/main/java/org/apache/ignite/internal/thread/context/ThreadContextData.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ ThreadContextSnapshot createSnapshot() {
6262
ThreadContextSnapshot snapshot = ThreadContextSnapshot.emptySnapshot();
6363

6464
for (ScopedAttributeValueStack<?> attrVals : attrs)
65-
snapshot = attrVals.exportTo(snapshot);
65+
snapshot = attrVals.exportTopTo(snapshot);
6666

6767
return snapshot;
6868
}
@@ -73,13 +73,18 @@ void restoreSnapshot(ThreadContextSnapshot snapshot) {
7373
return;
7474

7575
for (int id = attrReg.size() - 1; id >= 0; id--) {
76-
if (!snapshot.isEmpty() && snapshot.attribute().id() == id) {
77-
put(snapshot.attribute(), snapshot.attributeValue());
76+
ThreadContextAttribute<Object> attr = attrReg.attribute(id);
77+
Object attrVal;
78+
79+
if (!snapshot.isEmpty() && snapshot.attributeId() == id) {
80+
attrVal = snapshot.attributeValue();
7881

7982
snapshot = snapshot.previous();
8083
}
8184
else
82-
attributeValues(id).restoreInitial(activeScopeDepth);
85+
attrVal = attr.initialValue();
86+
87+
put(attr, attrVal);
8388
}
8489
}
8590

@@ -107,13 +112,13 @@ private void clearActiveScopeData() {
107112
/** */
108113
private <T> ScopedAttributeValueStack<T> attributeValues(int id) {
109114
if (attrs.length <= id)
110-
fetchRegisteredAttibutes();
115+
fetchRegisteredAttributes();
111116

112117
return (ScopedAttributeValueStack<T>)attrs[id];
113118
}
114119

115120
/** */
116-
private void fetchRegisteredAttibutes() {
121+
private void fetchRegisteredAttributes() {
117122
ScopedAttributeValueStack<?>[] upd = new ScopedAttributeValueStack[attrReg.size()];
118123

119124
if (attrs.length != 0)

modules/commons/src/main/java/org/apache/ignite/internal/thread/context/ThreadContextSnapshot.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
/** */
2121
public class ThreadContextSnapshot {
2222
/** */
23-
private static final ThreadContextSnapshot EMPTY = new ThreadContextSnapshot(null, null, null);
23+
private static final ThreadContextSnapshot EMPTY = new ThreadContextSnapshot(-1, null, null);
2424

2525
/** */
26-
private final ThreadContextAttribute<?> attr;
26+
private final int attrId;
2727

2828
/** */
2929
private final Object attrVal;
@@ -32,17 +32,17 @@ public class ThreadContextSnapshot {
3232
private final ThreadContextSnapshot prev;
3333

3434
/** */
35-
private ThreadContextSnapshot(ThreadContextAttribute<?> attr, Object attrVal, ThreadContextSnapshot prev) {
36-
this.attr = attr;
35+
private ThreadContextSnapshot(int attrId, Object attrVal, ThreadContextSnapshot prev) {
36+
this.attrId = attrId;
3737
this.attrVal = attrVal;
3838
this.prev = prev;
3939
}
4040

4141
/** */
42-
<T> ThreadContextAttribute<T> attribute() {
42+
int attributeId() {
4343
assert !isEmpty();
4444

45-
return (ThreadContextAttribute<T>)attr;
45+
return attrId;
4646
}
4747

4848
/** */
@@ -66,7 +66,7 @@ boolean isEmpty() {
6666

6767
/** */
6868
<T> ThreadContextSnapshot withAttribute(ThreadContextAttribute<T> attr, T val) {
69-
return new ThreadContextSnapshot(attr, val, this);
69+
return new ThreadContextSnapshot(attr.id(), val, this);
7070
}
7171

7272
/** */

0 commit comments

Comments
 (0)