|
15 | 15 | */ |
16 | 16 | package com.google.cloud.bigquery; |
17 | 17 |
|
| 18 | +import com.google.api.client.http.HttpResponseException; |
18 | 19 | import com.google.api.core.ApiClock; |
19 | 20 | import com.google.api.gax.retrying.DirectRetryingExecutor; |
20 | 21 | import com.google.api.gax.retrying.ExponentialRetryAlgorithm; |
|
23 | 24 | import com.google.api.gax.retrying.RetrySettings; |
24 | 25 | import com.google.api.gax.retrying.RetryingExecutor; |
25 | 26 | import com.google.api.gax.retrying.RetryingFuture; |
| 27 | +import com.google.api.gax.retrying.TimedAttemptSettings; |
26 | 28 | import com.google.api.gax.retrying.TimedRetryAlgorithm; |
27 | 29 | import com.google.cloud.RetryHelper; |
28 | 30 | import io.opentelemetry.api.trace.Span; |
@@ -69,6 +71,9 @@ public static <V> V runWithRetries( |
69 | 71 | // implementation does not use response at all, so ignoring its type is ok. |
70 | 72 | @SuppressWarnings("unchecked") |
71 | 73 | ResultRetryAlgorithm<V> algorithm = (ResultRetryAlgorithm<V>) resultRetryAlgorithm; |
| 74 | + if (algorithm == BigQueryBaseService.DEFAULT_BIGQUERY_EXCEPTION_HANDLER) { |
| 75 | + algorithm = wrapDefaultAlgorithm(algorithm); |
| 76 | + } |
72 | 77 | return run( |
73 | 78 | callable, |
74 | 79 | new ExponentialRetryAlgorithm(retrySettings, clock), |
@@ -119,6 +124,29 @@ private static <V> V run( |
119 | 124 | return retryingFuture.get(); |
120 | 125 | } |
121 | 126 |
|
| 127 | + private static <V> ResultRetryAlgorithm<V> wrapDefaultAlgorithm( |
| 128 | + ResultRetryAlgorithm<V> defaultAlgorithm) { |
| 129 | + return new ResultRetryAlgorithm<V>() { |
| 130 | + @Override |
| 131 | + public TimedAttemptSettings createNextAttempt( |
| 132 | + Throwable previousThrowable, V previousResponse, TimedAttemptSettings previousSettings) { |
| 133 | + return defaultAlgorithm.createNextAttempt( |
| 134 | + previousThrowable, previousResponse, previousSettings); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public boolean shouldRetry(Throwable previousThrowable, V previousResponse) { |
| 139 | + if (previousThrowable instanceof HttpResponseException) { |
| 140 | + int statusCode = ((HttpResponseException) previousThrowable).getStatusCode(); |
| 141 | + if (statusCode == 500 || statusCode == 502 || statusCode == 503 || statusCode == 504) { |
| 142 | + return true; |
| 143 | + } |
| 144 | + } |
| 145 | + return defaultAlgorithm.shouldRetry(previousThrowable, previousResponse); |
| 146 | + } |
| 147 | + }; |
| 148 | + } |
| 149 | + |
122 | 150 | public static class BigQueryRetryHelperException extends RuntimeException { |
123 | 151 |
|
124 | 152 | private static final long serialVersionUID = -8519852520090965314L; |
|
0 commit comments