Git Product home page Git Product logo

Comments (1)

gauravrmazra avatar gauravrmazra commented on May 25, 2024

I will explain the whole problem. I was evaluating the Spring cloud sleuth for tracing. There was no direct support for executors but there were two classes TraceCallable and TraceRunnable in Sleuth which supports tracing. I wrote one ExecutorService to wrap Runnable and Callable to their respective Traceable implementations. I created one instance of that ExecutorService(FixedThreadPool with 3 threads as its size. The size here doesn't matter).
The app I created is Spring Boot app which accepts request from UI(browser), sends it to Service layer to cater that request(Service layer uses Executor). I was sending multiple requests to test the feasibility of support for Executors. Then I found the weird Htrace error. Earlier I thought the issue might be because in worker Thread, span is not removed from their thread local variable. This is still true. But when I wrote unit test case to support my hypothesis, I found everything is working fine. The code for unit test case is :

@Test
public void test_whenTraceContextOfWorkerThreadIsNotClosed_thenException() {

    final AtomicInteger counter = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(TOTAL_THREADS);
    TraceScope scope = this.trace.startSpan("PARENT");

    for (int i = 0; i < TOTAL_THREADS; i++) {
        traceableExecutorService.execute(new MyRunnable(counter, latch));
    }

    try {
        latch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    scope.close();

    verify(this.publisher, times(NUM_SPANS)).publishEvent(isA(SpanAcquiredEvent.class));
    verify(publisher, times(NUM_SPANS)).publishEvent(isA(SpanReleasedEvent.class));

    ArgumentCaptor<ApplicationEvent> captor = ArgumentCaptor
            .forClass(ApplicationEvent.class);
    verify(publisher, atLeast(NUM_SPANS)).publishEvent(captor.capture());

    List<Span> spans = new ArrayList<>();
    for (ApplicationEvent event : captor.getAllValues()) {
        if (event instanceof SpanReleasedEvent) {
            spans.add(((SpanReleasedEvent) event).getSpan());
        }
    }

    assertThat("spans was wrong size", spans.size(), is(NUM_SPANS));
}

After giving it a think, I found that in close() of TraceScope class, last line is TraceContextHolder.setCurrentSpan(this.savedSpan);

When I printed on console this saved span, it was the parent span when these TraceCallable or TraceRunnable were created.

Now the scenerio is, In web-based application and when we are sharing that executorService and when multiple requests are coming in TraceFilter starts or continue the span. For two different requests, the parent span will be different and if in the executorService same thread tries to execute task, it will throws weird Htrace error. I hope I explained the problem in detail.

from spring-cloud-sleuth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.