|
| 1 | +/* |
| 2 | + * Copyright 2024 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.admin.v2.stub; |
| 17 | + |
| 18 | +import com.google.api.core.ApiAsyncFunction; |
| 19 | +import com.google.api.core.ApiFunction; |
| 20 | +import com.google.api.core.ApiFuture; |
| 21 | +import com.google.api.core.ApiFutures; |
| 22 | +import com.google.api.gax.retrying.ExponentialPollAlgorithm; |
| 23 | +import com.google.api.gax.retrying.NonCancellableFuture; |
| 24 | +import com.google.api.gax.retrying.ResultRetryAlgorithm; |
| 25 | +import com.google.api.gax.retrying.RetryAlgorithm; |
| 26 | +import com.google.api.gax.retrying.RetrySettings; |
| 27 | +import com.google.api.gax.retrying.RetryingExecutor; |
| 28 | +import com.google.api.gax.retrying.RetryingFuture; |
| 29 | +import com.google.api.gax.retrying.ScheduledRetryingExecutor; |
| 30 | +import com.google.api.gax.retrying.TimedAttemptSettings; |
| 31 | +import com.google.api.gax.rpc.ApiCallContext; |
| 32 | +import com.google.api.gax.rpc.ClientContext; |
| 33 | +import com.google.api.gax.rpc.UnaryCallable; |
| 34 | +import com.google.bigtable.admin.v2.CheckConsistencyRequest; |
| 35 | +import com.google.bigtable.admin.v2.CheckConsistencyResponse; |
| 36 | +import com.google.bigtable.admin.v2.GenerateConsistencyTokenRequest; |
| 37 | +import com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse; |
| 38 | +import com.google.cloud.bigtable.admin.v2.models.ConsistencyRequest; |
| 39 | +import com.google.cloud.bigtable.data.v2.internal.TableAdminRequestContext; |
| 40 | +import com.google.common.annotations.VisibleForTesting; |
| 41 | +import com.google.common.util.concurrent.MoreExecutors; |
| 42 | +import java.util.concurrent.Callable; |
| 43 | +import java.util.concurrent.CancellationException; |
| 44 | + |
| 45 | +/** |
| 46 | + * Callable that waits until either replication or Data Boost has caught up to the point it was |
| 47 | + * called. |
| 48 | + * |
| 49 | + * <p>This callable wraps GenerateConsistencyToken and CheckConsistency RPCs. It will generate a |
| 50 | + * token then poll until isConsistent is true. |
| 51 | + */ |
| 52 | +class AwaitConsistencyCallable extends UnaryCallable<ConsistencyRequest, Void> { |
| 53 | + private final UnaryCallable<GenerateConsistencyTokenRequest, GenerateConsistencyTokenResponse> |
| 54 | + generateCallable; |
| 55 | + private final UnaryCallable<CheckConsistencyRequest, CheckConsistencyResponse> checkCallable; |
| 56 | + private final RetryingExecutor<CheckConsistencyResponse> executor; |
| 57 | + |
| 58 | + private final TableAdminRequestContext requestContext; |
| 59 | + |
| 60 | + static AwaitConsistencyCallable create( |
| 61 | + UnaryCallable<GenerateConsistencyTokenRequest, GenerateConsistencyTokenResponse> |
| 62 | + generateCallable, |
| 63 | + UnaryCallable<CheckConsistencyRequest, CheckConsistencyResponse> checkCallable, |
| 64 | + ClientContext clientContext, |
| 65 | + RetrySettings pollingSettings, |
| 66 | + TableAdminRequestContext requestContext) { |
| 67 | + |
| 68 | + RetryAlgorithm<CheckConsistencyResponse> retryAlgorithm = |
| 69 | + new RetryAlgorithm<>( |
| 70 | + new PollResultAlgorithm(), |
| 71 | + new ExponentialPollAlgorithm(pollingSettings, clientContext.getClock())); |
| 72 | + |
| 73 | + RetryingExecutor<CheckConsistencyResponse> retryingExecutor = |
| 74 | + new ScheduledRetryingExecutor<>(retryAlgorithm, clientContext.getExecutor()); |
| 75 | + |
| 76 | + return new AwaitConsistencyCallable( |
| 77 | + generateCallable, checkCallable, retryingExecutor, requestContext); |
| 78 | + } |
| 79 | + |
| 80 | + @VisibleForTesting |
| 81 | + AwaitConsistencyCallable( |
| 82 | + UnaryCallable<GenerateConsistencyTokenRequest, GenerateConsistencyTokenResponse> |
| 83 | + generateCallable, |
| 84 | + UnaryCallable<CheckConsistencyRequest, CheckConsistencyResponse> checkCallable, |
| 85 | + RetryingExecutor<CheckConsistencyResponse> executor, |
| 86 | + TableAdminRequestContext requestContext) { |
| 87 | + this.generateCallable = generateCallable; |
| 88 | + this.checkCallable = checkCallable; |
| 89 | + this.executor = executor; |
| 90 | + this.requestContext = requestContext; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public ApiFuture<Void> futureCall( |
| 95 | + final ConsistencyRequest consistencyRequest, final ApiCallContext apiCallContext) { |
| 96 | + ApiFuture<GenerateConsistencyTokenResponse> tokenFuture = |
| 97 | + generateToken(consistencyRequest.toGenerateTokenProto(requestContext), apiCallContext); |
| 98 | + |
| 99 | + return ApiFutures.transformAsync( |
| 100 | + tokenFuture, |
| 101 | + new ApiAsyncFunction<GenerateConsistencyTokenResponse, Void>() { |
| 102 | + @Override |
| 103 | + public ApiFuture<Void> apply(GenerateConsistencyTokenResponse input) { |
| 104 | + CheckConsistencyRequest request = |
| 105 | + consistencyRequest.toCheckConsistencyProto( |
| 106 | + requestContext, input.getConsistencyToken()); |
| 107 | + return pollToken(request, apiCallContext); |
| 108 | + } |
| 109 | + }, |
| 110 | + MoreExecutors.directExecutor()); |
| 111 | + } |
| 112 | + |
| 113 | + private ApiFuture<GenerateConsistencyTokenResponse> generateToken( |
| 114 | + GenerateConsistencyTokenRequest generateRequest, ApiCallContext context) { |
| 115 | + return generateCallable.futureCall(generateRequest, context); |
| 116 | + } |
| 117 | + |
| 118 | + private ApiFuture<Void> pollToken(CheckConsistencyRequest request, ApiCallContext context) { |
| 119 | + AttemptCallable<CheckConsistencyRequest, CheckConsistencyResponse> attemptCallable = |
| 120 | + new AttemptCallable<>(checkCallable, request, context); |
| 121 | + RetryingFuture<CheckConsistencyResponse> retryingFuture = |
| 122 | + executor.createFuture(attemptCallable); |
| 123 | + attemptCallable.setExternalFuture(retryingFuture); |
| 124 | + attemptCallable.call(); |
| 125 | + |
| 126 | + return ApiFutures.transform( |
| 127 | + retryingFuture, |
| 128 | + new ApiFunction<CheckConsistencyResponse, Void>() { |
| 129 | + @Override |
| 130 | + public Void apply(CheckConsistencyResponse input) { |
| 131 | + return null; |
| 132 | + } |
| 133 | + }, |
| 134 | + MoreExecutors.directExecutor()); |
| 135 | + } |
| 136 | + |
| 137 | + /** A callable representing an attempt to make an RPC call. */ |
| 138 | + private static class AttemptCallable<RequestT, ResponseT> implements Callable<ResponseT> { |
| 139 | + private final UnaryCallable<RequestT, ResponseT> callable; |
| 140 | + private final RequestT request; |
| 141 | + |
| 142 | + private volatile RetryingFuture<ResponseT> externalFuture; |
| 143 | + private volatile ApiCallContext callContext; |
| 144 | + |
| 145 | + AttemptCallable( |
| 146 | + UnaryCallable<RequestT, ResponseT> callable, RequestT request, ApiCallContext callContext) { |
| 147 | + this.callable = callable; |
| 148 | + this.request = request; |
| 149 | + this.callContext = callContext; |
| 150 | + } |
| 151 | + |
| 152 | + void setExternalFuture(RetryingFuture<ResponseT> externalFuture) { |
| 153 | + this.externalFuture = externalFuture; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public ResponseT call() { |
| 158 | + try { |
| 159 | + // NOTE: unlike gax's AttemptCallable, this ignores rpc timeouts |
| 160 | + externalFuture.setAttemptFuture(new NonCancellableFuture<ResponseT>()); |
| 161 | + if (externalFuture.isDone()) { |
| 162 | + return null; |
| 163 | + } |
| 164 | + ApiFuture<ResponseT> internalFuture = callable.futureCall(request, callContext); |
| 165 | + externalFuture.setAttemptFuture(internalFuture); |
| 166 | + } catch (Throwable e) { |
| 167 | + externalFuture.setAttemptFuture(ApiFutures.<ResponseT>immediateFailedFuture(e)); |
| 168 | + } |
| 169 | + |
| 170 | + return null; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * A polling algorithm for waiting for a consistent {@link CheckConsistencyResponse}. Please note |
| 176 | + * that this class doesn't handle retryable errors and expects the underlying callable chain to |
| 177 | + * handle this. |
| 178 | + */ |
| 179 | + private static class PollResultAlgorithm |
| 180 | + implements ResultRetryAlgorithm<CheckConsistencyResponse> { |
| 181 | + @Override |
| 182 | + public TimedAttemptSettings createNextAttempt( |
| 183 | + Throwable prevThrowable, |
| 184 | + CheckConsistencyResponse prevResponse, |
| 185 | + TimedAttemptSettings prevSettings) { |
| 186 | + return null; |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public boolean shouldRetry(Throwable prevThrowable, CheckConsistencyResponse prevResponse) |
| 191 | + throws CancellationException { |
| 192 | + return prevResponse != null && !prevResponse.getConsistent(); |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments