File tree 3 files changed +48
-2
lines changed
aws-xray-recorder-sdk-core/src
main/java/com/amazonaws/xray/entities
test/java/com/amazonaws/xray
3 files changed +48
-2
lines changed Original file line number Diff line number Diff line change 20
20
import com .fasterxml .jackson .annotation .JsonIgnore ;
21
21
import com .fasterxml .jackson .core .JsonProcessingException ;
22
22
import com .fasterxml .jackson .databind .node .ObjectNode ;
23
- import java .util .HashSet ;
23
+ import java .util .Collections ;
24
24
import java .util .Set ;
25
+ import java .util .concurrent .ConcurrentHashMap ;
25
26
import org .apache .commons .logging .Log ;
26
27
import org .apache .commons .logging .LogFactory ;
27
28
import org .checkerframework .checker .nullness .qual .Nullable ;
@@ -64,7 +65,7 @@ public SubsegmentImpl(AWSXRayRecorder creator,
64
65
super (creator , name );
65
66
this .parentSegment = parentSegment ;
66
67
parentSegment .incrementReferenceCount ();
67
- this .precursorIds = new HashSet <>();
68
+ this .precursorIds = Collections . newSetFromMap ( new ConcurrentHashMap <>() );
68
69
this .shouldPropagate = true ;
69
70
70
71
this .isSampled = samplingStrategyOverride == SamplingStrategyOverride .DISABLED ?
Original file line number Diff line number Diff line change 16
16
package com .amazonaws .xray ;
17
17
18
18
import com .amazonaws .xray .emitters .Emitter ;
19
+ import com .amazonaws .xray .entities .Segment ;
20
+ import com .amazonaws .xray .entities .SegmentImpl ;
21
+ import com .amazonaws .xray .entities .SubsegmentImpl ;
19
22
import com .amazonaws .xray .strategy .sampling .LocalizedSamplingStrategy ;
23
+ import java .util .Iterator ;
24
+ import java .util .Random ;
25
+ import java .util .concurrent .Callable ;
20
26
import java .util .concurrent .CountDownLatch ;
21
27
import java .util .concurrent .ExecutorService ;
22
28
import java .util .concurrent .Executors ;
@@ -64,4 +70,31 @@ void testManyThreads() throws InterruptedException {
64
70
}
65
71
}
66
72
73
+ @ Test
74
+ public void testPrecursorIdConcurrency () throws Exception {
75
+ Segment seg = new SegmentImpl (AWSXRay .getGlobalRecorder (), "test" );
76
+ SubsegmentImpl subseg = new SubsegmentImpl (AWSXRay .getGlobalRecorder (), "test" , seg );
77
+
78
+ final long startTime = System .currentTimeMillis ();
79
+
80
+ Thread thread1 = new Thread (() -> {
81
+ while (true ) {
82
+ subseg .addPrecursorId ("ID" + new Random ().nextInt ());
83
+ }
84
+ });
85
+ thread1 .start ();
86
+
87
+ Callable <Void > callable = (Callable ) () -> {
88
+ while (System .currentTimeMillis () - startTime < TimeUnit .SECONDS .toMillis (1 )) {
89
+ Iterator it = subseg .getPrecursorIds ().iterator ();
90
+ while (it .hasNext ()) {
91
+ it .next ();
92
+ }
93
+ }
94
+ return null ;
95
+ };
96
+ callable .call ();
97
+
98
+ thread1 .join ();
99
+ }
67
100
}
Original file line number Diff line number Diff line change @@ -76,6 +76,18 @@ void testUnknownClassSerialization() {
76
76
seg .serialize (); // Verify we don't crash here
77
77
}
78
78
79
+ @ Test
80
+ void testPrecursorIds () {
81
+ Segment seg = new SegmentImpl (AWSXRay .getGlobalRecorder (), "test" );
82
+ SubsegmentImpl subseg = new SubsegmentImpl (AWSXRay .getGlobalRecorder (), "test" , seg );
83
+ subseg .addPrecursorId ("myId1" );
84
+ subseg .addPrecursorId ("myId2" );
85
+ String serializedSubSeg = subseg .serialize ();
86
+
87
+ String expected = "\" precursor_ids\" :[\" myId1\" ,\" myId2\" ]" ;
88
+ assertThat (serializedSubSeg ).contains (expected );
89
+ }
90
+
79
91
static class EmptyBean {
80
92
String otherField = "cerealization" ;
81
93
}
You can’t perform that action at this time.
0 commit comments