|
| 1 | +/* |
| 2 | + * Copyright 2023 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 | + * https://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 | +package com.google.cloud.bigtable.gaxx.retrying; |
| 17 | + |
| 18 | +import com.google.api.core.InternalApi; |
| 19 | +import com.google.api.gax.retrying.ExponentialRetryAlgorithm; |
| 20 | +import com.google.api.gax.retrying.RetryAlgorithm; |
| 21 | +import com.google.api.gax.retrying.RetrySettings; |
| 22 | +import com.google.api.gax.retrying.ScheduledRetryingExecutor; |
| 23 | +import com.google.api.gax.retrying.StreamingRetryAlgorithm; |
| 24 | +import com.google.api.gax.rpc.ClientContext; |
| 25 | +import com.google.api.gax.rpc.ServerStreamingCallSettings; |
| 26 | +import com.google.api.gax.rpc.ServerStreamingCallable; |
| 27 | +import com.google.api.gax.rpc.StatusCode; |
| 28 | +import com.google.api.gax.rpc.UnaryCallSettings; |
| 29 | +import com.google.api.gax.rpc.UnaryCallable; |
| 30 | +import java.util.Collection; |
| 31 | + |
| 32 | +// TODO: remove this once ApiResultRetryAlgorithm is added to gax. |
| 33 | +/** |
| 34 | + * Class with utility methods to create callable objects using provided settings. |
| 35 | + * |
| 36 | + * <p>The callable objects wrap a given direct callable with features like retry and exception |
| 37 | + * translation. |
| 38 | + */ |
| 39 | +@InternalApi |
| 40 | +public class Callables { |
| 41 | + |
| 42 | + private Callables() {} |
| 43 | + |
| 44 | + public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> retrying( |
| 45 | + UnaryCallable<RequestT, ResponseT> innerCallable, |
| 46 | + UnaryCallSettings<?, ?> callSettings, |
| 47 | + ClientContext clientContext) { |
| 48 | + |
| 49 | + UnaryCallSettings<?, ?> settings = callSettings; |
| 50 | + |
| 51 | + if (areRetriesDisabled(settings.getRetryableCodes(), settings.getRetrySettings())) { |
| 52 | + // When retries are disabled, the total timeout can be treated as the rpc timeout. |
| 53 | + settings = |
| 54 | + settings |
| 55 | + .toBuilder() |
| 56 | + .setSimpleTimeoutNoRetries(settings.getRetrySettings().getTotalTimeout()) |
| 57 | + .build(); |
| 58 | + } |
| 59 | + |
| 60 | + RetryAlgorithm<ResponseT> retryAlgorithm = |
| 61 | + new RetryAlgorithm<>( |
| 62 | + new ApiResultRetryAlgorithm<ResponseT>(), |
| 63 | + new ExponentialRetryAlgorithm(settings.getRetrySettings(), clientContext.getClock())); |
| 64 | + ScheduledRetryingExecutor<ResponseT> retryingExecutor = |
| 65 | + new ScheduledRetryingExecutor<>(retryAlgorithm, clientContext.getExecutor()); |
| 66 | + return new RetryingCallable<>( |
| 67 | + clientContext.getDefaultCallContext(), innerCallable, retryingExecutor); |
| 68 | + } |
| 69 | + |
| 70 | + public static <RequestT, ResponseT> ServerStreamingCallable<RequestT, ResponseT> retrying( |
| 71 | + ServerStreamingCallable<RequestT, ResponseT> innerCallable, |
| 72 | + ServerStreamingCallSettings<RequestT, ResponseT> callSettings, |
| 73 | + ClientContext clientContext) { |
| 74 | + |
| 75 | + ServerStreamingCallSettings<RequestT, ResponseT> settings = callSettings; |
| 76 | + if (areRetriesDisabled(settings.getRetryableCodes(), settings.getRetrySettings())) { |
| 77 | + // When retries are disabled, the total timeout can be treated as the rpc timeout. |
| 78 | + settings = |
| 79 | + settings |
| 80 | + .toBuilder() |
| 81 | + .setSimpleTimeoutNoRetries(settings.getRetrySettings().getTotalTimeout()) |
| 82 | + .build(); |
| 83 | + } |
| 84 | + |
| 85 | + StreamingRetryAlgorithm<Void> retryAlgorithm = |
| 86 | + new StreamingRetryAlgorithm<>( |
| 87 | + new ApiResultRetryAlgorithm<Void>(), |
| 88 | + new ExponentialRetryAlgorithm(settings.getRetrySettings(), clientContext.getClock())); |
| 89 | + |
| 90 | + ScheduledRetryingExecutor<Void> retryingExecutor = |
| 91 | + new ScheduledRetryingExecutor<>(retryAlgorithm, clientContext.getExecutor()); |
| 92 | + |
| 93 | + return new RetryingServerStreamingCallable<>( |
| 94 | + innerCallable, retryingExecutor, settings.getResumptionStrategy()); |
| 95 | + } |
| 96 | + |
| 97 | + private static boolean areRetriesDisabled( |
| 98 | + Collection<StatusCode.Code> retryableCodes, RetrySettings retrySettings) { |
| 99 | + return retrySettings.getMaxAttempts() == 1 |
| 100 | + || retryableCodes.isEmpty() |
| 101 | + || (retrySettings.getMaxAttempts() == 0 && retrySettings.getTotalTimeout().isZero()); |
| 102 | + } |
| 103 | +} |
0 commit comments