Skip to content

Commit f598279

Browse files
fix: add retry logging for BigQueryRetryAlgorithm.java (#1506)
* fix: add retry logging for BigQueryRetryAlgorithm.java * Added shouldRetry and Error Message to Logging * Added shouldRetry and Error Message to Logging * update based on comments - to add in retried method (enclosingMethod) * set log level to FINEST Co-authored-by: Prashant Mishra <[email protected]>
1 parent 8e21a12 commit f598279

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
import com.google.api.gax.retrying.TimedRetryAlgorithmWithContext;
2828
import java.util.Iterator;
2929
import java.util.concurrent.CancellationException;
30+
import java.util.logging.Level;
31+
import java.util.logging.Logger;
3032
import java.util.regex.Pattern;
33+
import org.threeten.bp.Duration;
3134

3235
public class BigQueryRetryAlgorithm<ResponseT> extends RetryAlgorithm<ResponseT> {
3336
private final BigQueryRetryConfig bigQueryRetryConfig;
@@ -36,6 +39,8 @@ public class BigQueryRetryAlgorithm<ResponseT> extends RetryAlgorithm<ResponseT>
3639
private final ResultRetryAlgorithmWithContext<ResponseT> resultAlgorithmWithContext;
3740
private final TimedRetryAlgorithmWithContext timedAlgorithmWithContext;
3841

42+
private static final Logger LOG = Logger.getLogger(BigQueryRetryAlgorithm.class.getName());
43+
3944
public BigQueryRetryAlgorithm(
4045
ResultRetryAlgorithm<ResponseT> resultAlgorithm,
4146
TimedRetryAlgorithm timedAlgorithm,
@@ -55,11 +60,32 @@ public boolean shouldRetry(
5560
ResponseT previousResponse,
5661
TimedAttemptSettings nextAttemptSettings)
5762
throws CancellationException {
63+
// Log retry info
64+
int attemptCount = nextAttemptSettings == null ? 0 : nextAttemptSettings.getAttemptCount();
65+
Duration retryDelay =
66+
nextAttemptSettings == null ? Duration.ZERO : nextAttemptSettings.getRetryDelay();
67+
String errorMessage = previousThrowable != null ? previousThrowable.getMessage() : "";
68+
5869
// Implementing shouldRetryBasedOnBigQueryRetryConfig so that we can retry exceptions based on
5970
// the exception messages
60-
return (shouldRetryBasedOnResult(context, previousThrowable, previousResponse)
61-
|| shouldRetryBasedOnBigQueryRetryConfig(previousThrowable, bigQueryRetryConfig))
62-
&& shouldRetryBasedOnTiming(context, nextAttemptSettings);
71+
boolean shouldRetry =
72+
(shouldRetryBasedOnResult(context, previousThrowable, previousResponse)
73+
|| shouldRetryBasedOnBigQueryRetryConfig(previousThrowable, bigQueryRetryConfig))
74+
&& shouldRetryBasedOnTiming(context, nextAttemptSettings);
75+
76+
if (LOG.isLoggable(Level.FINEST)) {
77+
LOG.log(
78+
Level.FINEST,
79+
"Retrying with:\n{0}\n{1}\n{2}\n{3}\n{4}",
80+
new Object[] {
81+
"BigQuery attemptCount: " + attemptCount,
82+
"BigQuery delay: " + retryDelay,
83+
"BigQuery retriableException: " + previousThrowable,
84+
"BigQuery shouldRetry: " + shouldRetry,
85+
"BigQuery previousThrowable.getMessage: " + errorMessage
86+
});
87+
}
88+
return shouldRetry;
6389
}
6490

6591
private boolean shouldRetryBasedOnBigQueryRetryConfig(

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,24 @@
1616
package com.google.cloud.bigquery;
1717

1818
import com.google.api.core.ApiClock;
19-
import com.google.api.gax.retrying.*;
19+
import com.google.api.gax.retrying.DirectRetryingExecutor;
20+
import com.google.api.gax.retrying.ExponentialRetryAlgorithm;
21+
import com.google.api.gax.retrying.ResultRetryAlgorithm;
22+
import com.google.api.gax.retrying.RetryAlgorithm;
23+
import com.google.api.gax.retrying.RetrySettings;
24+
import com.google.api.gax.retrying.RetryingExecutor;
25+
import com.google.api.gax.retrying.RetryingFuture;
26+
import com.google.api.gax.retrying.TimedRetryAlgorithm;
2027
import com.google.cloud.RetryHelper;
2128
import java.util.concurrent.Callable;
2229
import java.util.concurrent.ExecutionException;
30+
import java.util.logging.Level;
31+
import java.util.logging.Logger;
2332

2433
public class BigQueryRetryHelper extends RetryHelper {
2534

35+
private static final Logger LOG = Logger.getLogger(BigQueryRetryHelper.class.getName());
36+
2637
public static <V> V runWithRetries(
2738
Callable<V> callable,
2839
RetrySettings retrySettings,
@@ -60,6 +71,16 @@ private static <V> V run(
6071
// BigQueryRetryAlgorithm retries considering bigQueryRetryConfig
6172
RetryingExecutor<V> executor = new DirectRetryingExecutor<>(retryAlgorithm);
6273

74+
// Log retry info
75+
if (LOG.isLoggable(Level.FINEST)) {
76+
LOG.log(
77+
Level.FINEST,
78+
"Retrying with:\n{0}",
79+
new Object[] {
80+
"BigQuery retried method: " + callable.getClass().getEnclosingMethod().getName(),
81+
});
82+
}
83+
6384
RetryingFuture<V> retryingFuture = executor.createFuture(callable);
6485
executor.submit(retryingFuture);
6586
return retryingFuture.get();

0 commit comments

Comments
 (0)