|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.bigquery; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | + |
| 21 | +import com.google.api.gax.retrying.ResultRetryAlgorithm; |
| 22 | +import com.google.api.gax.retrying.ResultRetryAlgorithmWithContext; |
| 23 | +import com.google.api.gax.retrying.RetryAlgorithm; |
| 24 | +import com.google.api.gax.retrying.RetryingContext; |
| 25 | +import com.google.api.gax.retrying.TimedAttemptSettings; |
| 26 | +import com.google.api.gax.retrying.TimedRetryAlgorithm; |
| 27 | +import com.google.api.gax.retrying.TimedRetryAlgorithmWithContext; |
| 28 | +import java.util.Iterator; |
| 29 | +import java.util.concurrent.CancellationException; |
| 30 | + |
| 31 | +public class BigQueryRetryAlgorithm<ResponseT> extends RetryAlgorithm<ResponseT> { |
| 32 | + private final BigQueryRetryConfig bigQueryRetryConfig; |
| 33 | + private final ResultRetryAlgorithm<ResponseT> resultAlgorithm; |
| 34 | + private final TimedRetryAlgorithm timedAlgorithm; |
| 35 | + private final ResultRetryAlgorithmWithContext<ResponseT> resultAlgorithmWithContext; |
| 36 | + private final TimedRetryAlgorithmWithContext timedAlgorithmWithContext; |
| 37 | + |
| 38 | + public BigQueryRetryAlgorithm( |
| 39 | + ResultRetryAlgorithm<ResponseT> resultAlgorithm, |
| 40 | + TimedRetryAlgorithm timedAlgorithm, |
| 41 | + BigQueryRetryConfig bigQueryRetryConfig) { |
| 42 | + super(resultAlgorithm, timedAlgorithm); |
| 43 | + this.bigQueryRetryConfig = checkNotNull(bigQueryRetryConfig); |
| 44 | + this.resultAlgorithm = checkNotNull(resultAlgorithm); |
| 45 | + this.timedAlgorithm = checkNotNull(timedAlgorithm); |
| 46 | + this.resultAlgorithmWithContext = null; |
| 47 | + this.timedAlgorithmWithContext = null; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public boolean shouldRetry( |
| 52 | + RetryingContext context, |
| 53 | + Throwable previousThrowable, |
| 54 | + ResponseT previousResponse, |
| 55 | + TimedAttemptSettings nextAttemptSettings) |
| 56 | + throws CancellationException { |
| 57 | + // Implementing shouldRetryBasedOnBigQueryRetryConfig so that we can retry exceptions based on |
| 58 | + // the exception messages |
| 59 | + return (shouldRetryBasedOnResult(context, previousThrowable, previousResponse) |
| 60 | + || shouldRetryBasedOnBigQueryRetryConfig(previousThrowable, bigQueryRetryConfig)) |
| 61 | + && shouldRetryBasedOnTiming(context, nextAttemptSettings); |
| 62 | + } |
| 63 | + |
| 64 | + private boolean shouldRetryBasedOnBigQueryRetryConfig( |
| 65 | + Throwable previousThrowable, BigQueryRetryConfig bigQueryRetryConfig) { |
| 66 | + /* |
| 67 | + We are deciding if a given error should be retried on the basis of error message. |
| 68 | + Cannot rely on Error/Status code as for example error code 400 (which is not retriable) could be thrown due to rateLimitExceed, which is retriable |
| 69 | + */ |
| 70 | + String errorDesc; |
| 71 | + if (previousThrowable != null && (errorDesc = previousThrowable.getMessage()) != null) { |
| 72 | + for (Iterator<String> retriableMessages = |
| 73 | + bigQueryRetryConfig.getRetriableErrorMessages().iterator(); |
| 74 | + retriableMessages.hasNext(); ) { |
| 75 | + if (errorDesc.contains(retriableMessages.next())) { // Error message should be retried |
| 76 | + return true; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + /*Duplicating this method as it can not be inherited from the RetryAlgorithm due to the default access modifier*/ |
| 84 | + boolean shouldRetryBasedOnResult( |
| 85 | + RetryingContext context, Throwable previousThrowable, ResponseT previousResponse) { |
| 86 | + if (resultAlgorithmWithContext != null && context != null) { |
| 87 | + return resultAlgorithmWithContext.shouldRetry(context, previousThrowable, previousResponse); |
| 88 | + } |
| 89 | + return getResultAlgorithm().shouldRetry(previousThrowable, previousResponse); |
| 90 | + } |
| 91 | + |
| 92 | + /*Duplicating this method as it can not be inherited from the RetryAlgorithm due to the private access modifier*/ |
| 93 | + private boolean shouldRetryBasedOnTiming( |
| 94 | + RetryingContext context, TimedAttemptSettings nextAttemptSettings) { |
| 95 | + if (nextAttemptSettings == null) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + if (timedAlgorithmWithContext != null && context != null) { |
| 99 | + return timedAlgorithmWithContext.shouldRetry(context, nextAttemptSettings); |
| 100 | + } |
| 101 | + return getTimedAlgorithm().shouldRetry(nextAttemptSettings); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public TimedAttemptSettings createNextAttempt( |
| 106 | + RetryingContext context, |
| 107 | + Throwable previousThrowable, |
| 108 | + ResponseT previousResponse, |
| 109 | + TimedAttemptSettings previousSettings) { |
| 110 | + // a small optimization that avoids calling relatively heavy methods |
| 111 | + // like timedAlgorithm.createNextAttempt(), when it is not necessary. |
| 112 | + |
| 113 | + if (!((shouldRetryBasedOnResult(context, previousThrowable, previousResponse) |
| 114 | + || shouldRetryBasedOnBigQueryRetryConfig( |
| 115 | + previousThrowable, |
| 116 | + bigQueryRetryConfig)))) { // Calling shouldRetryBasedOnBigQueryRetryConfig to check if |
| 117 | + // the error message could be retried |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + TimedAttemptSettings newSettings = |
| 122 | + createNextAttemptBasedOnResult( |
| 123 | + context, previousThrowable, previousResponse, previousSettings); |
| 124 | + if (newSettings == null) { |
| 125 | + newSettings = createNextAttemptBasedOnTiming(context, previousSettings); |
| 126 | + } |
| 127 | + return newSettings; |
| 128 | + } |
| 129 | + |
| 130 | + /*Duplicating this method as it can not be inherited from the RetryAlgorithm due to the private access modifier*/ |
| 131 | + private TimedAttemptSettings createNextAttemptBasedOnResult( |
| 132 | + RetryingContext context, |
| 133 | + Throwable previousThrowable, |
| 134 | + ResponseT previousResponse, |
| 135 | + TimedAttemptSettings previousSettings) { |
| 136 | + if (resultAlgorithmWithContext != null && context != null) { |
| 137 | + return resultAlgorithmWithContext.createNextAttempt( |
| 138 | + context, previousThrowable, previousResponse, previousSettings); |
| 139 | + } |
| 140 | + return getResultAlgorithm() |
| 141 | + .createNextAttempt(previousThrowable, previousResponse, previousSettings); |
| 142 | + } |
| 143 | + |
| 144 | + /*Duplicating this method as it can not be inherited from the RetryAlgorithm due to the private access modifier*/ |
| 145 | + private TimedAttemptSettings createNextAttemptBasedOnTiming( |
| 146 | + RetryingContext context, TimedAttemptSettings previousSettings) { |
| 147 | + if (timedAlgorithmWithContext != null && context != null) { |
| 148 | + return timedAlgorithmWithContext.createNextAttempt(context, previousSettings); |
| 149 | + } |
| 150 | + return getTimedAlgorithm().createNextAttempt(previousSettings); |
| 151 | + } |
| 152 | +} |
0 commit comments