Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle retry info so client respect the delay server sets #2026

Merged
merged 14 commits into from
Dec 19, 2023
Prev Previous commit
Next Next commit
feat: handle retry info so client respect the delay server sets
  • Loading branch information
mutianf committed Dec 18, 2023
commit bec1f11766cacfeaab380ede2efc2258363ecc5a
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.bigtable.v2.MutateRowsResponse.Entry;
import com.google.cloud.bigtable.data.v2.models.MutateRowsException;
import com.google.cloud.bigtable.data.v2.models.MutateRowsException.FailedMutation;
import com.google.cloud.bigtable.gaxx.retrying.ApiResultRetryAlgorithm;
import com.google.cloud.bigtable.gaxx.retrying.NonCancellableFuture;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -235,7 +236,8 @@ private void handleAttemptError(Throwable rpcError) {
FailedMutation failedMutation = FailedMutation.create(origIndex, entryError);
allFailures.add(failedMutation);

if (!failedMutation.getError().isRetryable()) {
if (ApiResultRetryAlgorithm.extractRetryDelay(failedMutation.getError()) == null
&& !failedMutation.getError().isRetryable()) {
permanentFailures.add(failedMutation);
} else {
// Schedule the mutation entry for the next RPC by adding it to the request builder and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,46 @@
import com.google.api.core.InternalApi;
import com.google.api.gax.retrying.BasicResultRetryAlgorithm;
import com.google.api.gax.retrying.RetryingContext;
import com.google.api.gax.retrying.TimedAttemptSettings;
import com.google.api.gax.rpc.ApiException;
import com.google.protobuf.util.Durations;
import com.google.rpc.RetryInfo;
import io.grpc.Metadata;
import io.grpc.Status;
import io.grpc.protobuf.ProtoUtils;
import org.threeten.bp.Duration;

/** For internal use, public for technical reasons. */
/**
* For internal use, public for technical reasons. This retry algorithm checks the metadata of an
* exception for additional error details. If the metadata has a RetryInfo field, use the retry
* delay to set the wait time between attempts.
*/
@InternalApi
public class ApiResultRetryAlgorithm<ResponseT> extends BasicResultRetryAlgorithm<ResponseT> {

private static final Metadata.Key<RetryInfo> KEY_RETRY_INFO =
ProtoUtils.keyForProto(RetryInfo.getDefaultInstance());
igorbernstein2 marked this conversation as resolved.
Show resolved Hide resolved

@Override
public TimedAttemptSettings createNextAttempt(
Throwable prevThrowable, ResponseT prevResponse, TimedAttemptSettings prevSettings) {
Duration retryDelay = extractRetryDelay(prevThrowable);
if (retryDelay != null) {
return prevSettings
.toBuilder()
.setRandomizedRetryDelay(retryDelay)
.setAttemptCount(prevSettings.getAttemptCount() + 1)
igorbernstein2 marked this conversation as resolved.
Show resolved Hide resolved
.build();
}
return null;
}

/** Returns true if previousThrowable is an {@link ApiException} that is retryable. */
@Override
public boolean shouldRetry(Throwable previousThrowable, ResponseT previousResponse) {
return (previousThrowable instanceof ApiException)
&& ((ApiException) previousThrowable).isRetryable();
return (extractRetryDelay(previousThrowable) != null)
|| (previousThrowable instanceof ApiException
&& ((ApiException) previousThrowable).isRetryable());
mutianf marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -43,11 +72,30 @@ public boolean shouldRetry(
if (context.getRetryableCodes() != null) {
// Ignore the isRetryable() value of the throwable if the RetryingContext has a specific list
// of codes that should be retried.
return (previousThrowable instanceof ApiException)
&& context
.getRetryableCodes()
.contains(((ApiException) previousThrowable).getStatusCode().getCode());
return extractRetryDelay(previousThrowable) != null
|| ((previousThrowable instanceof ApiException)
&& context
.getRetryableCodes()
.contains(((ApiException) previousThrowable).getStatusCode().getCode()));
}
return shouldRetry(previousThrowable, previousResponse);
}

public static Duration extractRetryDelay(Throwable throwable) {
mutianf marked this conversation as resolved.
Show resolved Hide resolved
if (throwable == null) {
return null;
}
Metadata trailers = Status.trailersFromThrowable(throwable);
if (trailers == null) {
return null;
}
RetryInfo retryInfo = trailers.get(KEY_RETRY_INFO);
if (retryInfo == null) {
return null;
}
if (!retryInfo.hasRetryDelay()) {
return null;
}
return Duration.ofMillis(Durations.toMillis(retryInfo.getRetryDelay()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> retrying(
}

RetryAlgorithm<ResponseT> retryAlgorithm =
new RetryAlgorithm<>(
new UnaryRetryAlgorithm<>(
new ApiResultRetryAlgorithm<ResponseT>(),
new ExponentialRetryAlgorithm(settings.getRetrySettings(), clientContext.getClock()));
ScheduledRetryingExecutor<ResponseT> retryingExecutor =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.gaxx.retrying;

import com.google.api.core.InternalApi;
import com.google.api.gax.retrying.ResultRetryAlgorithmWithContext;
import com.google.api.gax.retrying.RetryAlgorithm;
import com.google.api.gax.retrying.RetryingContext;
import com.google.api.gax.retrying.TimedAttemptSettings;
import com.google.api.gax.retrying.TimedRetryAlgorithmWithContext;
import java.util.concurrent.CancellationException;

/**
* Retry algorithm for unary calls. It'll use the result from result algorithm first and only fall
* back to timedAlgorithm if resultAlgorithm#shouldRetry is false.
*/
@InternalApi
public class UnaryRetryAlgorithm<ResponseT> extends RetryAlgorithm<ResponseT> {

public UnaryRetryAlgorithm(
ResultRetryAlgorithmWithContext<ResponseT> resultAlgorithm,
TimedRetryAlgorithmWithContext timedAlgorithm) {
super(resultAlgorithm, timedAlgorithm);
}

@Override
public boolean shouldRetry(
RetryingContext context,
Throwable previousThrowable,
ResponseT previousResponse,
TimedAttemptSettings nextAttemptSettings)
throws CancellationException {
if (getResultAlgorithm().shouldRetry(previousThrowable, previousResponse)) {
return true;
}
return super.shouldRetry(context, previousThrowable, previousResponse, nextAttemptSettings);
}
}
Loading