Skip to content

Commit c12fb87

Browse files
committed
only update if shouldRetry is true, update to use clearCurrentAttempt helper method
1 parent dd81d25 commit c12fb87

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryRetryAlgorithm.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public static void setCurrentAttempt(int attempt) {
5757
currentAttempt.set(attempt);
5858
}
5959

60+
@InternalApi("internal to java-bigquery")
61+
public static void clearCurrentAttempt() {
62+
currentAttempt.remove();
63+
}
64+
6065
public BigQueryRetryAlgorithm(
6166
ResultRetryAlgorithm<ResponseT> resultAlgorithm,
6267
TimedRetryAlgorithm timedAlgorithm,
@@ -91,7 +96,9 @@ public boolean shouldRetry(
9196
&& shouldRetryBasedOnTiming(context, nextAttemptSettings);
9297

9398
// Store retry attempt count in thread-local storage for tracing
94-
setCurrentAttempt(attemptCount);
99+
if (shouldRetry) {
100+
setCurrentAttempt(attemptCount);
101+
}
95102

96103
if (LOG.isLoggable(Level.FINEST)) {
97104
LOG.log(

java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2189,7 +2189,7 @@ private <T> T executeWithSpan(Span span, SpanOperation<T> operation) throws IOEx
21892189
throw e;
21902190
} finally {
21912191
// Reset attempt count to 0 to avoid carrying over state across requests on the same thread
2192-
BigQueryRetryAlgorithm.setCurrentAttempt(0);
2192+
BigQueryRetryAlgorithm.clearCurrentAttempt();
21932193
span.end();
21942194
}
21952195
}

java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpcTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.util.HashMap;
6161
import java.util.List;
6262
import java.util.Map;
63+
import org.junit.jupiter.api.AfterEach;
6364
import org.junit.jupiter.api.BeforeEach;
6465
import org.junit.jupiter.api.Nested;
6566
import org.junit.jupiter.api.Test;
@@ -227,6 +228,14 @@ public void setUp() {
227228
rpc = createRpc(true);
228229
}
229230

231+
@AfterEach
232+
public void tearDown() {
233+
assertEquals(
234+
0,
235+
com.google.cloud.bigquery.BigQueryRetryAlgorithm.getCurrentAttempt(),
236+
"ThreadLocal should be 0 at the end of the request!");
237+
}
238+
230239
@Test
231240
public void testSpanEndOnError() {
232241
assertThrows(

0 commit comments

Comments
 (0)