|
| 1 | +package datadog.trace.instrumentation.gax; |
| 2 | + |
| 3 | +import static datadog.trace.agent.test.assertions.SpanMatcher.span; |
| 4 | +import static datadog.trace.agent.test.assertions.TraceMatcher.SORT_BY_START_TIME; |
| 5 | +import static datadog.trace.agent.test.assertions.TraceMatcher.trace; |
| 6 | +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; |
| 7 | +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 10 | + |
| 11 | +import com.google.api.core.ApiClock; |
| 12 | +import com.google.api.core.NanoClock; |
| 13 | +import com.google.api.core.SettableApiFuture; |
| 14 | +import com.google.api.gax.retrying.BasicResultRetryAlgorithm; |
| 15 | +import com.google.api.gax.retrying.ExponentialRetryAlgorithm; |
| 16 | +import com.google.api.gax.retrying.RetryAlgorithm; |
| 17 | +import com.google.api.gax.retrying.RetrySettings; |
| 18 | +import com.google.api.gax.retrying.RetryingFuture; |
| 19 | +import com.google.api.gax.retrying.ScheduledRetryingExecutor; |
| 20 | +import datadog.trace.agent.test.AbstractInstrumentationTest; |
| 21 | +import datadog.trace.bootstrap.instrumentation.api.AgentScope; |
| 22 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
| 23 | +import java.util.concurrent.CountDownLatch; |
| 24 | +import java.util.concurrent.ExecutionException; |
| 25 | +import java.util.concurrent.Executors; |
| 26 | +import java.util.concurrent.ScheduledExecutorService; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import java.util.concurrent.atomic.AtomicInteger; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.threeten.bp.Duration; |
| 31 | + |
| 32 | +class GaxRetryContinuationTest extends AbstractInstrumentationTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + void supersededAttemptListenerDoesNotLeak() throws Exception { |
| 36 | + ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); |
| 37 | + try { |
| 38 | + ScheduledRetryingExecutor<String> executor = |
| 39 | + new ScheduledRetryingExecutor<>(retryAlgorithm(1), scheduler); |
| 40 | + RetryingFuture<String> retryingFuture = executor.createFuture(() -> "ok"); |
| 41 | + |
| 42 | + AgentSpan span = startSpan("gax", "publish"); |
| 43 | + try (AgentScope scope = activateSpan(span)) { |
| 44 | + // reserve the attempt slot with a placeholder that is never completed |
| 45 | + SettableApiFuture<String> placeholder = SettableApiFuture.create(); |
| 46 | + retryingFuture.setAttemptFuture(placeholder); |
| 47 | + // supersede it with the real attempt future -> must cancel the placeholder's continuation |
| 48 | + SettableApiFuture<String> attempt = SettableApiFuture.create(); |
| 49 | + retryingFuture.setAttemptFuture(attempt); |
| 50 | + // real attempt completes -> its listener runs -> resolves normally |
| 51 | + attempt.set("ok"); |
| 52 | + } |
| 53 | + span.finish(); |
| 54 | + // one trace that will be dropped if the placeholder's continuation is never cancelled |
| 55 | + assertTraces(trace(span().root().operationName("publish"))); |
| 56 | + } finally { |
| 57 | + scheduler.shutdownNow(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void singleAttemptSucceedsAndContextNotLeaked() throws Exception { |
| 63 | + ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); |
| 64 | + try { |
| 65 | + ScheduledRetryingExecutor<String> executor = |
| 66 | + new ScheduledRetryingExecutor<>(retryAlgorithm(3), scheduler); |
| 67 | + AgentSpan parent = startSpan("gax", "publish"); |
| 68 | + CountDownLatch allAttemptsDone = new CountDownLatch(1); |
| 69 | + RetryingFuture<String> future = |
| 70 | + executor.createFuture( |
| 71 | + () -> { |
| 72 | + AgentSpan attempt = startSpan("gax", "attempt"); |
| 73 | + try { |
| 74 | + return "ok"; |
| 75 | + } finally { |
| 76 | + attempt.finish(); |
| 77 | + allAttemptsDone.countDown(); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + try (AgentScope scope = activateSpan(parent)) { |
| 82 | + future.setAttemptFuture(executor.submit(future)); |
| 83 | + assertEquals("ok", future.get(5, TimeUnit.SECONDS)); |
| 84 | + } |
| 85 | + allAttemptsDone.await(5, TimeUnit.SECONDS); |
| 86 | + parent.finish(); |
| 87 | + assertTraces( |
| 88 | + trace( |
| 89 | + SORT_BY_START_TIME, |
| 90 | + span().root().operationName("publish"), |
| 91 | + span().childOf(parent.getSpanId()).operationName("attempt"))); |
| 92 | + } finally { |
| 93 | + scheduler.shutdownNow(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void retriedAttemptsSucceedAndContextNotLeaked() throws Exception { |
| 99 | + ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); |
| 100 | + try { |
| 101 | + ScheduledRetryingExecutor<String> executor = |
| 102 | + new ScheduledRetryingExecutor<>(retryAlgorithm(3), scheduler); |
| 103 | + AtomicInteger count = new AtomicInteger(0); |
| 104 | + AgentSpan parent = startSpan("gax", "publish"); |
| 105 | + CountDownLatch allAttemptsDone = new CountDownLatch(3); |
| 106 | + RetryingFuture<String> future = |
| 107 | + executor.createFuture( |
| 108 | + () -> { |
| 109 | + AgentSpan attempt = startSpan("gax", "attempt"); |
| 110 | + try { |
| 111 | + if (count.incrementAndGet() < 3) { |
| 112 | + throw new RuntimeException("transient"); |
| 113 | + } |
| 114 | + return "ok"; |
| 115 | + } finally { |
| 116 | + attempt.finish(); |
| 117 | + allAttemptsDone.countDown(); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + try (AgentScope scope = activateSpan(parent)) { |
| 122 | + future.setAttemptFuture(executor.submit(future)); |
| 123 | + assertEquals("ok", future.get(5, TimeUnit.SECONDS)); |
| 124 | + } |
| 125 | + allAttemptsDone.await(5, TimeUnit.SECONDS); |
| 126 | + parent.finish(); |
| 127 | + assertTraces( |
| 128 | + trace( |
| 129 | + SORT_BY_START_TIME, |
| 130 | + span().root().operationName("publish"), |
| 131 | + span().childOf(parent.getSpanId()).operationName("attempt"), |
| 132 | + span().childOf(parent.getSpanId()).operationName("attempt"), |
| 133 | + span().childOf(parent.getSpanId()).operationName("attempt"))); |
| 134 | + } finally { |
| 135 | + scheduler.shutdownNow(); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void exhaustedRetriesContextNotLeaked() throws Exception { |
| 141 | + ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); |
| 142 | + try { |
| 143 | + ScheduledRetryingExecutor<String> executor = |
| 144 | + new ScheduledRetryingExecutor<>(retryAlgorithm(3), scheduler); |
| 145 | + AgentSpan parent = startSpan("gax", "publish"); |
| 146 | + CountDownLatch allAttemptsDone = new CountDownLatch(3); |
| 147 | + RetryingFuture<String> future = |
| 148 | + executor.createFuture( |
| 149 | + () -> { |
| 150 | + AgentSpan attempt = startSpan("gax", "attempt"); |
| 151 | + try { |
| 152 | + throw new RuntimeException("always fails"); |
| 153 | + } finally { |
| 154 | + attempt.finish(); |
| 155 | + allAttemptsDone.countDown(); |
| 156 | + } |
| 157 | + }); |
| 158 | + |
| 159 | + try (AgentScope scope = activateSpan(parent)) { |
| 160 | + future.setAttemptFuture(executor.submit(future)); |
| 161 | + assertThrows(ExecutionException.class, () -> future.get(5, TimeUnit.SECONDS)); |
| 162 | + } |
| 163 | + allAttemptsDone.await(5, TimeUnit.SECONDS); |
| 164 | + parent.finish(); |
| 165 | + assertTraces( |
| 166 | + trace( |
| 167 | + SORT_BY_START_TIME, |
| 168 | + span().root().operationName("publish"), |
| 169 | + span().childOf(parent.getSpanId()).operationName("attempt"), |
| 170 | + span().childOf(parent.getSpanId()).operationName("attempt"), |
| 171 | + span().childOf(parent.getSpanId()).operationName("attempt"))); |
| 172 | + } finally { |
| 173 | + scheduler.shutdownNow(); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private static RetryAlgorithm<String> retryAlgorithm(int maxAttempts) { |
| 178 | + RetrySettings settings = |
| 179 | + RetrySettings.newBuilder() |
| 180 | + .setMaxAttempts(maxAttempts) |
| 181 | + .setInitialRetryDelay(Duration.ofMillis(1)) |
| 182 | + .setRetryDelayMultiplier(1.0) |
| 183 | + .setMaxRetryDelay(Duration.ofMillis(10)) |
| 184 | + .setInitialRpcTimeout(Duration.ofSeconds(5)) |
| 185 | + .setRpcTimeoutMultiplier(1.0) |
| 186 | + .setMaxRpcTimeout(Duration.ofSeconds(5)) |
| 187 | + .setTotalTimeout(Duration.ofSeconds(30)) |
| 188 | + .build(); |
| 189 | + ApiClock clock = NanoClock.getDefaultClock(); |
| 190 | + return new RetryAlgorithm<>( |
| 191 | + new BasicResultRetryAlgorithm<>(), new ExponentialRetryAlgorithm(settings, clock)); |
| 192 | + } |
| 193 | +} |
0 commit comments