Skip to content

Commit 8795941

Browse files
committed
1 parent 8128d8c commit 8795941

7 files changed

Lines changed: 96 additions & 128 deletions

File tree

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.util.ArrayDeque;
2121
import java.util.Deque;
22-
import org.apache.ignite.internal.util.typedef.F;
2322

2423
/** */
2524
class ScopedAttributeValueStack<T> {
@@ -30,7 +29,7 @@ class ScopedAttributeValueStack<T> {
3029
private final ThreadContextAttribute<T> attr;
3130

3231
/** */
33-
private Deque<ScopedAttributeValue<T>> scopedVals;
32+
private final Deque<ScopedAttributeValue<T>> scopedVals = new ArrayDeque<>(DFLT_SCOPE_ATTR_VAL_CAPACITY);
3433

3534
/** */
3635
ScopedAttributeValueStack(ThreadContextAttribute<T> attr) {
@@ -49,9 +48,6 @@ boolean pop(int scopeDepth) {
4948

5049
/** */
5150
void push(int scopeDepth, T val) {
52-
if (scopedVals == null)
53-
scopedVals = new ArrayDeque<>(DFLT_SCOPE_ATTR_VAL_CAPACITY);
54-
5551
ScopedAttributeValue<T> scopedVal = scopedVals.peek();
5652

5753
if (scopedVal != null && scopedVal.scopeDepth == scopeDepth)
@@ -74,7 +70,12 @@ ThreadContextSnapshot exportTopTo(ThreadContextSnapshot snapshot) {
7470

7571
/** */
7672
boolean isEmpty() {
77-
return F.isEmpty(scopedVals);
73+
return scopedVals.isEmpty();
74+
}
75+
76+
/** */
77+
T initialValue() {
78+
return attr.initialValue();
7879
}
7980

8081
/** */

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,26 @@
1717

1818
package org.apache.ignite.internal.thread.context;
1919

20-
/** */
20+
import java.util.concurrent.atomic.AtomicInteger;
21+
22+
/**
23+
* Represents a key used to access or modify corresponding attribute values in {@link ThreadContext}.
24+
*
25+
* @see ThreadContext#get(ThreadContextAttribute)
26+
* @see ThreadContext#withAttribute(ThreadContextAttribute, Object)
27+
*/
2128
public class ThreadContextAttribute<T> {
29+
/** */
30+
private static final AtomicInteger ID_GEN = new AtomicInteger();
31+
2232
/** */
2333
private final int id;
2434

2535
/** */
2636
private final T initialVal;
2737

2838
/** */
29-
ThreadContextAttribute(int id, T initialVal) {
39+
private ThreadContextAttribute(int id, T initialVal) {
3040
this.id = id;
3141
this.initialVal = initialVal;
3242
}
@@ -40,4 +50,24 @@ int id() {
4050
public T initialValue() {
4151
return initialVal;
4252
}
53+
54+
/**
55+
* Creates attribute instance with initial value set to {@code null}.
56+
*
57+
* @see #newInstance(Object)
58+
*/
59+
public static <T> ThreadContextAttribute<T> newInstance() {
60+
return newInstance(null);
61+
}
62+
63+
/**
64+
* Creates attribute instance with specified initial value. Initial value is returned by
65+
* {@link ThreadContext#get(ThreadContextAttribute)} method if attribute value is not set for {@link Scope} explicitly.
66+
*
67+
* @param initialVal Attribute initial value.
68+
* @return Attribute instance.
69+
*/
70+
public static <T> ThreadContextAttribute<T> newInstance(T initialVal) {
71+
return new ThreadContextAttribute<>(ID_GEN.getAndIncrement(), initialVal);
72+
}
4373
}

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

Lines changed: 0 additions & 73 deletions
This file was deleted.

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

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ class ThreadContextData {
2222
/** */
2323
private static final ScopedAttributeValueStack<?>[] EMPTY = new ScopedAttributeValueStack[0];
2424

25-
/** */
26-
private final ThreadContextAttributeRegistry attrReg = ThreadContextAttributeRegistry.instance();
27-
2825
/** */
2926
private int activeScopeDepth;
3027

@@ -36,22 +33,25 @@ class ThreadContextData {
3633

3734
/** */
3835
<T> T get(ThreadContextAttribute<T> attr) {
39-
ScopedAttributeValueStack<T> attrVals = attributeValues(attr.id());
36+
if (attr.id() >= attrs.length)
37+
return attr.initialValue();
4038

41-
return attrVals.peek();
39+
ScopedAttributeValueStack<T> attrVals = (ScopedAttributeValueStack<T>)attrs[attr.id()];
40+
41+
return attrVals == null ? attr.initialValue() : attrVals.peek();
4242
}
4343

4444
/** */
4545
<T> void put(ThreadContextAttribute<T> attr, T val) {
46-
ScopedAttributeValueStack<T> attrVals = attributeValues(attr.id());
46+
if (attr.id() >= attrs.length)
47+
grow(attr.id() + 1);
4748

48-
if (attrVals.peek() == val)
49-
return;
49+
ScopedAttributeValueStack<T> attrVals = (ScopedAttributeValueStack<T>)attrs[attr.id()];
5050

51-
if (attrVals.isEmpty())
52-
++activeAttrsCnt;
51+
if (attrVals == null)
52+
attrs[attr.id()] = attrVals = new ScopedAttributeValueStack<>(attr);
5353

54-
attrVals.push(activeScopeDepth, val);
54+
push(attrVals, val);
5555
}
5656

5757
/** */
@@ -61,8 +61,12 @@ ThreadContextSnapshot createSnapshot() {
6161

6262
ThreadContextSnapshot snapshot = ThreadContextSnapshot.emptySnapshot();
6363

64-
for (ScopedAttributeValueStack<?> attrVals : attrs)
65-
snapshot = attrVals.exportTopTo(snapshot);
64+
for (int i = 0; i < attrs.length; i++) {
65+
ScopedAttributeValueStack<?> attrVals = attrs[i];
66+
67+
if (attrVals != null)
68+
snapshot = attrVals.exportTopTo(snapshot);
69+
}
6670

6771
return snapshot;
6872
}
@@ -72,19 +76,20 @@ void restoreSnapshot(ThreadContextSnapshot snapshot) {
7276
if (snapshot.isEmpty() && activeAttrsCnt == 0)
7377
return;
7478

75-
for (int id = attrReg.size() - 1; id >= 0; id--) {
76-
ThreadContextAttribute<Object> attr = attrReg.attribute(id);
77-
Object attrVal;
79+
int maxAttrId = snapshot.isEmpty() ? attrs.length - 1 : Math.max(snapshot.attribute().id(), attrs.length - 1);
7880

79-
if (!snapshot.isEmpty() && snapshot.attributeId() == id) {
80-
attrVal = snapshot.attributeValue();
81+
for (int attrId = maxAttrId; attrId >= 0; attrId--) {
82+
if (!snapshot.isEmpty() && snapshot.attribute().id() == attrId) {
83+
put(snapshot.attribute(), snapshot.attributeValue());
8184

8285
snapshot = snapshot.previous();
8386
}
84-
else
85-
attrVal = attr.initialValue();
87+
else {
88+
ScopedAttributeValueStack<Object> attrVals = (ScopedAttributeValueStack<Object>)attrs[attrId];
8689

87-
put(attr, attrVal);
90+
if (attrVals != null)
91+
push(attrVals, attrVals.initialValue());
92+
}
8893
}
8994
}
9095

@@ -102,31 +107,36 @@ void onScopeClosed() {
102107
}
103108

104109
/** */
105-
private void clearActiveScopeData() {
106-
for (ScopedAttributeValueStack<?> attrVals : attrs) {
107-
if (attrVals.pop(activeScopeDepth) && attrVals.isEmpty())
108-
--activeAttrsCnt;
109-
}
110+
private <T> void push(ScopedAttributeValueStack<T> attrVals, T val) {
111+
if (attrVals.peek() == val)
112+
return;
113+
114+
if (attrVals.isEmpty())
115+
++activeAttrsCnt;
116+
117+
attrVals.push(activeScopeDepth, val);
110118
}
111119

112120
/** */
113-
private <T> ScopedAttributeValueStack<T> attributeValues(int id) {
114-
if (attrs.length <= id)
115-
fetchRegisteredAttributes();
121+
private void clearActiveScopeData() {
122+
for (int i = 0; i < attrs.length; i++) {
123+
ScopedAttributeValueStack<?> attrVals = attrs[i];
116124

117-
return (ScopedAttributeValueStack<T>)attrs[id];
125+
if (attrVals == null)
126+
continue;
127+
128+
if (attrVals.pop(activeScopeDepth) && attrVals.isEmpty())
129+
--activeAttrsCnt;
130+
}
118131
}
119132

120133
/** */
121-
private void fetchRegisteredAttributes() {
122-
ScopedAttributeValueStack<?>[] upd = new ScopedAttributeValueStack[attrReg.size()];
134+
private void grow(int size) {
135+
ScopedAttributeValueStack<?>[] upd = new ScopedAttributeValueStack[size];
123136

124137
if (attrs.length != 0)
125138
System.arraycopy(attrs, 0, upd, 0, attrs.length);
126139

127-
for (int id = attrs.length; id < attrReg.size(); id++)
128-
upd[id] = new ScopedAttributeValueStack<>(attrReg.attribute(id));
129-
130140
attrs = upd;
131141
}
132142
}

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(-1, null, null);
23+
private static final ThreadContextSnapshot EMPTY = new ThreadContextSnapshot(null, null, null);
2424

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

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

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

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

45-
return attrId;
45+
return (ThreadContextAttribute<T>)attr;
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.id(), val, this);
69+
return new ThreadContextSnapshot(attr, val, this);
7070
}
7171

7272
/** */

modules/core/src/main/java/org/apache/ignite/internal/processors/security/IgniteSecurityProcessor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.apache.ignite.internal.thread.context.Scope;
3636
import org.apache.ignite.internal.thread.context.ThreadContext;
3737
import org.apache.ignite.internal.thread.context.ThreadContextAttribute;
38-
import org.apache.ignite.internal.thread.context.ThreadContextAttributeRegistry;
3938
import org.apache.ignite.internal.util.typedef.F;
4039
import org.apache.ignite.internal.util.typedef.internal.U;
4140
import org.apache.ignite.lang.IgniteFuture;
@@ -90,7 +89,7 @@ static boolean hasSandboxedNodes() {
9089
}
9190

9291
/** Thread context attribute that holds Security Context. */
93-
private static final ThreadContextAttribute<SecurityContext> SEC_CTX = ThreadContextAttributeRegistry.instance().register();
92+
private static final ThreadContextAttribute<SecurityContext> SEC_CTX = ThreadContextAttribute.newInstance();
9493

9594
/** Security processor. */
9695
private final GridSecurityProcessor secPrc;

modules/core/src/test/java/org/apache/ignite/internal/thread/context/ThreadContextAttributesTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public class ThreadContextAttributesTest extends GridCommonAbstractTest {
4242
private static final Integer DFLT_INT_VAL = 0;
4343

4444
/** */
45-
private static final ThreadContextAttribute<String> STR_ATTR = ThreadContextAttributeRegistry.instance().register();
45+
private static final ThreadContextAttribute<String> STR_ATTR = ThreadContextAttribute.newInstance();
4646

4747
/** */
48-
private static final ThreadContextAttribute<Integer> INT_ATTR = ThreadContextAttributeRegistry.instance().register(DFLT_INT_VAL);
48+
private static final ThreadContextAttribute<Integer> INT_ATTR = ThreadContextAttribute.newInstance(DFLT_INT_VAL);
4949

5050
/** */
5151
private ExecutorService poolToShutdownAfterTest;
@@ -151,8 +151,9 @@ public void testThreadContextSnapshot() {
151151
public void testRuntimeAttributeRegistration() {
152152
// Initializes the Context Data for the current thread to check the growth of the array containing attribute values.
153153
assertEquals(DFLT_INT_VAL, ThreadContext.get(INT_ATTR));
154+
assertEquals(DFLT_STR_VAL, ThreadContext.get(STR_ATTR));
154155

155-
ThreadContextAttribute<Object> attr = ThreadContextAttributeRegistry.instance().register();
156+
ThreadContextAttribute<Object> attr = ThreadContextAttribute.newInstance();
156157

157158
assertEquals(null, ThreadContext.get(attr));
158159

0 commit comments

Comments
 (0)