Skip to content

Commit

Permalink
Don't lock segment from subsegment (aws#306)
Browse files Browse the repository at this point in the history
* Don't lock segment from subsegment

* synchronized list
  • Loading branch information
Anuraag Agrawal authored Oct 7, 2021
1 parent 0fe77bb commit d5ccae2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.google.errorprone.annotations.concurrent.GuardedBy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -197,7 +198,7 @@ protected EntityImpl(AWSXRayRecorder creator, String name) {

this.creator = creator;
this.name = name;
this.subsegments = new ArrayList<>();
this.subsegments = Collections.synchronizedList(new ArrayList<>());
this.subsegmentsLock = new ReentrantLock();
this.cause = new Cause();
this.http = new HashMap<>();
Expand Down Expand Up @@ -531,25 +532,21 @@ public void setParentId(@Nullable String parentId) {

@Override
public List<Subsegment> getSubsegments() {
synchronized (lock) {
return subsegments;
}
return subsegments;
}

@JsonIgnore
@Override
public List<Subsegment> getSubsegmentsCopy() {
synchronized (lock) {
return new ArrayList<>(subsegments);
}
return new ArrayList<>(subsegments);
}

@Override
public void addSubsegment(Subsegment subsegment) {
synchronized (lock) {
checkAlreadyEmitted();
subsegments.add(subsegment);
}
subsegments.add(subsegment);
}

@Override
Expand Down Expand Up @@ -675,8 +672,8 @@ public void incrementReferenceCount() {
synchronized (lock) {
checkAlreadyEmitted();
referenceCount++;
totalSize.increment();
}
totalSize.increment();
}

@Override
Expand Down Expand Up @@ -706,9 +703,7 @@ public int getReferenceCount() {
*/
@Override
public LongAdder getTotalSize() {
synchronized (lock) {
return totalSize;
}
return totalSize;
}

/**
Expand Down Expand Up @@ -770,10 +765,8 @@ public String prettySerialize() {

@Override
public void removeSubsegment(Subsegment subsegment) {
synchronized (lock) {
subsegments.remove(subsegment);
getParentSegment().getTotalSize().decrement();
}
subsegments.remove(subsegment);
getParentSegment().getTotalSize().decrement();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public class SegmentImpl extends EntityImpl implements Segment {
protected Map<String, Object> service;

@JsonIgnore
@GuardedBy("lock")
private boolean sampled;
private volatile boolean sampled;

@SuppressWarnings({ "unused", "nullness" })
private SegmentImpl() {
Expand Down Expand Up @@ -88,17 +87,15 @@ public boolean isRecording() {

@Override
public boolean isSampled() {
synchronized (lock) {
return sampled;
}
return sampled;
}

@Override
public void setSampled(boolean sampled) {
synchronized (lock) {
checkAlreadyEmitted();
this.sampled = sampled;
}
this.sampled = sampled;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public int getMaxSegmentSize() {
*/
@Override
public boolean requiresStreaming(Segment segment) {
if (segment.isSampled() && null != segment.getTotalSize()) {
if (segment.isSampled()) {
return segment.getTotalSize().intValue() > maxSegmentSize;
}
return false;
Expand Down

0 comments on commit d5ccae2

Please sign in to comment.