|
| 1 | +import datadog.trace.agent.test.AgentTestRunner |
| 2 | +import datadog.trace.core.DDSpan |
| 3 | +import io.netty.channel.DefaultEventLoopGroup |
| 4 | +import io.netty.channel.ThreadPerChannelEventLoop |
| 5 | +import io.netty.channel.nio.NioEventLoopGroup |
| 6 | +import io.netty.channel.oio.OioEventLoopGroup |
| 7 | +import io.netty.util.concurrent.DefaultEventExecutor |
| 8 | +import org.apache.tomcat.util.threads.TaskQueue |
| 9 | +import spock.lang.Shared |
| 10 | + |
| 11 | +import java.util.concurrent.ExecutorService |
| 12 | +import java.util.concurrent.Executors |
| 13 | +import java.util.concurrent.ForkJoinPool |
| 14 | +import java.util.concurrent.ThreadFactory |
| 15 | +import java.util.concurrent.TimeUnit |
| 16 | +import java.util.concurrent.atomic.AtomicInteger |
| 17 | + |
| 18 | +import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace |
| 19 | + |
| 20 | +class CrossedContextTest extends AgentTestRunner { |
| 21 | + |
| 22 | + @Shared |
| 23 | + def executeRunnable = { e, c -> e.execute((Runnable) c) } |
| 24 | + @Shared |
| 25 | + def submitRunnable = { e, c -> e.submit((Runnable) c) } |
| 26 | + |
| 27 | + // this pool is not what's being tested |
| 28 | + @Shared |
| 29 | + ExecutorService pool = Executors.newFixedThreadPool(40, new ThreadFactory() { |
| 30 | + AtomicInteger i = new AtomicInteger() |
| 31 | + |
| 32 | + @Override |
| 33 | + Thread newThread(Runnable r) { |
| 34 | + Thread t = new Thread(r) |
| 35 | + t.setName("parent-" + i.getAndIncrement()) |
| 36 | + return t |
| 37 | + } |
| 38 | + }) |
| 39 | + |
| 40 | + def cleanupSpec() { |
| 41 | + pool.shutdownNow() |
| 42 | + } |
| 43 | + |
| 44 | + def "concurrent #action are traced with correct lineage in #executor"() { |
| 45 | + when: |
| 46 | + def sut = executor |
| 47 | + def fn = function |
| 48 | + int taskCount = 200 |
| 49 | + for (int i = 0; i < taskCount; ++i) { |
| 50 | + pool.execute({ |
| 51 | + String threadName = Thread.currentThread().getName() |
| 52 | + runUnderTrace(threadName) { |
| 53 | + fn(sut, new Descendant(threadName)) |
| 54 | + } |
| 55 | + }) |
| 56 | + } |
| 57 | + |
| 58 | + TEST_WRITER.waitForTraces(taskCount) |
| 59 | + then: |
| 60 | + for (List<DDSpan> trace : TEST_WRITER) { |
| 61 | + assert trace.size() == 2 |
| 62 | + DDSpan parent = trace.find({ it.isRootSpan() }) |
| 63 | + assert null != parent |
| 64 | + DDSpan child = trace.find({ it.getParentId() == parent.getSpanId() }) |
| 65 | + assert null != child |
| 66 | + assert child.getOperationName().toString().startsWith(parent.getOperationName().toString()) |
| 67 | + } |
| 68 | + |
| 69 | + cleanup: |
| 70 | + executor.shutdownNow() |
| 71 | + |
| 72 | + where: |
| 73 | + executor | action | function |
| 74 | + new ForkJoinPool() | "submission" | submitRunnable |
| 75 | + new ForkJoinPool(10) | "submission" | submitRunnable |
| 76 | + Executors.newSingleThreadExecutor() | "submission" | submitRunnable |
| 77 | + Executors.newSingleThreadScheduledExecutor() | "submission" | submitRunnable |
| 78 | + Executors.newFixedThreadPool(10) | "submission" | submitRunnable |
| 79 | + Executors.newScheduledThreadPool(10) | "submission" | submitRunnable |
| 80 | + Executors.newCachedThreadPool() | "submission" | submitRunnable |
| 81 | + new DefaultEventLoopGroup(10) | "submission" | submitRunnable |
| 82 | + new DefaultEventLoopGroup(1).next() | "submission" | submitRunnable |
| 83 | + // TODO - flaky - seems to be relying on PendingTrace flush |
| 84 | + // new UnorderedThreadPoolEventExecutor(10) | "submission" | submitRunnable |
| 85 | + new NioEventLoopGroup(10) | "submission" | submitRunnable |
| 86 | + new DefaultEventExecutor() | "submission" | submitRunnable |
| 87 | + new org.apache.tomcat.util.threads.ThreadPoolExecutor(1, 1, 5, TimeUnit.SECONDS, new TaskQueue()) | "submission" | submitRunnable |
| 88 | + new ForkJoinPool() | "execution" | executeRunnable |
| 89 | + new ForkJoinPool(10) | "execution" | executeRunnable |
| 90 | + Executors.newSingleThreadExecutor() | "execution" | executeRunnable |
| 91 | + Executors.newSingleThreadScheduledExecutor() | "execution" | executeRunnable |
| 92 | + Executors.newFixedThreadPool(10) | "execution" | executeRunnable |
| 93 | + Executors.newScheduledThreadPool(10) | "execution" | executeRunnable |
| 94 | + Executors.newCachedThreadPool() | "execution" | executeRunnable |
| 95 | + new DefaultEventLoopGroup(10) | "execution" | executeRunnable |
| 96 | + new DefaultEventLoopGroup(1).next() | "execution" | executeRunnable |
| 97 | + // TODO - flaky - seems to be relying on PendingTrace flush |
| 98 | + // new UnorderedThreadPoolEventExecutor(10) | "execution" | executeRunnable |
| 99 | + new NioEventLoopGroup(10) | "execution" | executeRunnable |
| 100 | + new DefaultEventExecutor() | "execution" | executeRunnable |
| 101 | + new org.apache.tomcat.util.threads.ThreadPoolExecutor(1, 1, 5, TimeUnit.SECONDS, new TaskQueue()) | "execution" | executeRunnable |
| 102 | + } |
| 103 | + |
| 104 | + def "netty event loop internal executions in #executor are traced with correct lineage" () { |
| 105 | + setup: |
| 106 | + ExecutorService pool = executor |
| 107 | + when: |
| 108 | + |
| 109 | + int taskCount = 200 |
| 110 | + for (int i = 0; i < taskCount; ++i) { |
| 111 | + pool.execute({ |
| 112 | + String threadName = Thread.currentThread().getName() |
| 113 | + runUnderTrace(threadName) { |
| 114 | + pool.execute(new Descendant(threadName)) |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | + |
| 119 | + TEST_WRITER.waitForTraces(taskCount) |
| 120 | + then: |
| 121 | + for (List<DDSpan> trace : TEST_WRITER) { |
| 122 | + assert trace.size() == 2 |
| 123 | + DDSpan parent = trace.find({ it.isRootSpan() }) |
| 124 | + assert null != parent |
| 125 | + DDSpan child = trace.find({ it.getParentId() == parent.getSpanId() }) |
| 126 | + assert null != child |
| 127 | + assert child.getOperationName().toString().startsWith(parent.getOperationName().toString()) |
| 128 | + } |
| 129 | + |
| 130 | + where: |
| 131 | + executor << [ |
| 132 | + new ThreadPerChannelEventLoop(new OioEventLoopGroup()), |
| 133 | + new DefaultEventExecutor(), |
| 134 | + new NioEventLoopGroup(10), |
| 135 | + new DefaultEventLoopGroup(10) |
| 136 | + // flaky |
| 137 | + // new UnorderedThreadPoolEventExecutor(10) |
| 138 | + ] |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments