Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random failures regarding SegmentNotFoundException in multithreaded application #355

Open
NoMemoryError opened this issue Sep 28, 2022 · 1 comment

Comments

@NoMemoryError
Copy link

I am facing a problem in a multithreaded application where the dynamodb calls randomly fails to create a subsegment resulting in error message "Suppressing AWS X-Ray context missing exception (SegmentNotFoundException): Failed to begin subsegment named 'AmazonDynamoDBv2': segment cannot be found."

I am following the procedure in X-Ray documentation for passing segment context between threads as stated here - https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-multithreading.html and how its implemented in this example - https://docs.aws.amazon.com/xray/latest/devguide/scorekeep-workerthreads.html. In almost 70% cases the instrumentation is successful but the rest fails to find the segment when creating the subsegment.

In terms of implementation I am using custom fork join pool along with Java parallel stream.

forkJoinPool
              .submit(
                  () ->
                      filters.parallelStream()
                          .map(traceableFetcher)
                          .flatMap(Collection::stream)
                          .collect(Collectors.toList()))
              .get();

traceableFetcher gets passed the segment using awsXRayRecorder.getTraceEntity() and the implementation of the traceable class is the following

public class TraceableFunction<T, R> implements Function<T, R> {

  private final Function<T, R> target;

  private final Entity segment;

  @Getter private final MdcSnapshot mdcSnapshot;

  public TraceableFunction(Function<T, R> target, final Entity segment) {
    this.target = target;
    this.segment = segment;
    this.mdcSnapshot = new MdcSnapshot();
  }

  @Override
  public R apply(T t) {
    mdcSnapshot.populateMdc();
    var resultReference = new AtomicReference<R>();

    segment.run(() -> resultReference.set(target.apply(t)));

    mdcSnapshot.cleanMdc();
    return resultReference.get();
  }
}

It seems like in certain cases the ThreadLocalStorage doesn't have the segment or at least it has been cleared. Are you folks aware of any such issues or have any pointers? I am using com.amazonaws:aws-xray-recorder-sdk-core:2.9.1

@wangzlei
Copy link
Contributor

wangzlei commented Oct 1, 2022

|---parent segment---|
------|--- child subsegment ---|
That solution only woks if child subsegment is generated before parent segment close. If the sub thread starts later than main thread close, will get SegmentNotFoundException issue.

Please try to use SegmentContextExecutors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants