From 8d755800ec1211989b7a262d40b9697a11007433 Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Thu, 16 Jun 2022 16:09:05 -0400 Subject: [PATCH] add request deadline for ping and warm --- .../data/v2/stub/BigtableChannelPrimer.java | 9 ++---- .../data/v2/stub/EnhancedBigtableStub.java | 4 +-- .../v2/stub/EnhancedBigtableStubSettings.java | 31 +++++++++++++++++++ .../v2/BigtableDataClientFactoryTest.java | 10 ++---- .../v2/stub/BigtableChannelPrimerTest.java | 2 +- .../EnhancedBigtableStubSettingsTest.java | 11 +++++++ 6 files changed, 50 insertions(+), 17 deletions(-) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/BigtableChannelPrimer.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/BigtableChannelPrimer.java index 2a68e1aadc..2c22c9aa90 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/BigtableChannelPrimer.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/BigtableChannelPrimer.java @@ -22,15 +22,13 @@ import com.google.api.gax.grpc.GrpcTransportChannel; import com.google.api.gax.rpc.FixedTransportChannelProvider; import com.google.auth.Credentials; -import com.google.bigtable.v2.InstanceName; import com.google.bigtable.v2.PingAndWarmRequest; +import com.google.cloud.bigtable.data.v2.internal.NameUtil; import com.google.common.base.Preconditions; -import com.google.protobuf.ByteString; import io.grpc.ManagedChannel; import java.io.IOException; import java.util.concurrent.ExecutionException; import java.util.logging.Logger; -import org.threeten.bp.Duration; /** * A channel warmer that ensures that a Bigtable channel is ready to be used before being added to @@ -43,9 +41,6 @@ class BigtableChannelPrimer implements ChannelPrimer { private static Logger LOG = Logger.getLogger(BigtableChannelPrimer.class.toString()); - static ByteString PRIMING_ROW_KEY = ByteString.copyFromUtf8("nonexistent-priming-row"); - private static Duration PRIME_REQUEST_TIMEOUT = Duration.ofSeconds(30); - private final EnhancedBigtableStubSettings settingsTemplate; static BigtableChannelPrimer create( @@ -94,7 +89,7 @@ private void sendPrimeRequests(ManagedChannel managedChannel) throws IOException PingAndWarmRequest request = PingAndWarmRequest.newBuilder() .setName( - InstanceName.format( + NameUtil.formatInstanceName( primingSettings.getProjectId(), primingSettings.getInstanceId())) .setAppProfileId(primingSettings.getAppProfileId()) .build(); diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java index f5db1c5949..a6813d280f 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java @@ -816,8 +816,8 @@ private UnaryCallable createPingAndWarm @Override public Map extract(PingAndWarmRequest request) { return ImmutableMap.of( - "name", request.getName(), - "app_profile_id", request.getAppProfileId()); + "name", request.getName(), + "app_profile_id", request.getAppProfileId()); } }) .build(), diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettings.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettings.java index cc1422b263..8a13907e14 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettings.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettings.java @@ -33,6 +33,7 @@ import com.google.api.gax.rpc.TransportChannelProvider; import com.google.api.gax.rpc.UnaryCallSettings; import com.google.auth.Credentials; +import com.google.bigtable.v2.PingAndWarmRequest; import com.google.cloud.bigtable.Version; import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation; import com.google.cloud.bigtable.data.v2.models.KeyOffset; @@ -94,6 +95,8 @@ public class EnhancedBigtableStubSettings extends StubSettings IDEMPOTENT_RETRY_CODES = ImmutableSet.of(Code.DEADLINE_EXCEEDED, Code.UNAVAILABLE); + private static Duration PRIME_REQUEST_TIMEOUT = Duration.ofSeconds(30); + // Copy of default retrying settings in the yaml private static final RetrySettings IDEMPOTENT_RETRY_SETTINGS = RetrySettings.newBuilder() @@ -173,6 +176,7 @@ public class EnhancedBigtableStubSettings extends StubSettings checkAndMutateRowSettings; private final UnaryCallSettings readModifyWriteRowSettings; + private final UnaryCallSettings pingAndWarmSettings; private EnhancedBigtableStubSettings(Builder builder) { super(builder); @@ -208,6 +212,7 @@ private EnhancedBigtableStubSettings(Builder builder) { bulkReadRowsSettings = builder.bulkReadRowsSettings.build(); checkAndMutateRowSettings = builder.checkAndMutateRowSettings.build(); readModifyWriteRowSettings = builder.readModifyWriteRowSettings.build(); + pingAndWarmSettings = builder.pingAndWarmSettings.build(); } /** Create a new builder. */ @@ -494,6 +499,15 @@ public UnaryCallSettings readModifyWriteRowSettings() { return readModifyWriteRowSettings; } + /** + * Returns the object with the settings used for calls to PingAndWarm. + * + *

By default the retries are disabled for PingAndWarm and deadline is set to 30 seconds. + */ + UnaryCallSettings pingAndWarmSettings() { + return pingAndWarmSettings; + } + /** Returns a builder containing all the values of this settings class. */ public Builder toBuilder() { return new Builder(this); @@ -518,6 +532,7 @@ public static class Builder extends StubSettings.Builder checkAndMutateRowSettings; private final UnaryCallSettings.Builder readModifyWriteRowSettings; + private final UnaryCallSettings.Builder pingAndWarmSettings; /** * Initializes a new Builder with sane defaults for all settings. @@ -629,6 +644,15 @@ private Builder() { readModifyWriteRowSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); copyRetrySettings(baseDefaults.readModifyWriteRowSettings(), readModifyWriteRowSettings); + + pingAndWarmSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); + pingAndWarmSettings.setRetrySettings( + RetrySettings.newBuilder() + .setMaxAttempts(1) + .setInitialRpcTimeout(PRIME_REQUEST_TIMEOUT) + .setMaxRpcTimeout(PRIME_REQUEST_TIMEOUT) + .setTotalTimeout(PRIME_REQUEST_TIMEOUT) + .build()); } private Builder(EnhancedBigtableStubSettings settings) { @@ -649,6 +673,7 @@ private Builder(EnhancedBigtableStubSettings settings) { bulkReadRowsSettings = settings.bulkReadRowsSettings.toBuilder(); checkAndMutateRowSettings = settings.checkAndMutateRowSettings.toBuilder(); readModifyWriteRowSettings = settings.readModifyWriteRowSettings.toBuilder(); + pingAndWarmSettings = settings.pingAndWarmSettings.toBuilder(); } // @@ -817,6 +842,11 @@ public UnaryCallSettings.Builder readModifyWriteRowSett return readModifyWriteRowSettings; } + /** Returns the builder with the settings used for calls to PingAndWarm. */ + public UnaryCallSettings.Builder pingAndWarmSettings() { + return pingAndWarmSettings; + } + @SuppressWarnings("unchecked") public EnhancedBigtableStubSettings build() { Preconditions.checkState(projectId != null, "Project id must be set"); @@ -864,6 +894,7 @@ public String toString() { .add("bulkReadRowsSettings", bulkReadRowsSettings) .add("checkAndMutateRowSettings", checkAndMutateRowSettings) .add("readModifyWriteRowSettings", readModifyWriteRowSettings) + .add("pingAndWarmSettings", pingAndWarmSettings) .add("parent", super.toString()) .toString(); } diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/BigtableDataClientFactoryTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/BigtableDataClientFactoryTest.java index 241a115e57..ebda860851 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/BigtableDataClientFactoryTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/BigtableDataClientFactoryTest.java @@ -345,13 +345,9 @@ public void mutateRow( @Override public void pingAndWarm( PingAndWarmRequest request, StreamObserver responseObserver) { - try { - pingAndWarmRequests.add(request); - responseObserver.onNext(pingAndWarmCallback.apply(request)); - responseObserver.onCompleted(); - } catch (RuntimeException e) { - responseObserver.onError(e); - } + pingAndWarmRequests.add(request); + responseObserver.onNext(pingAndWarmCallback.apply(request)); + responseObserver.onCompleted(); } } diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/BigtableChannelPrimerTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/BigtableChannelPrimerTest.java index 4586d6f21a..e1f22bebbd 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/BigtableChannelPrimerTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/BigtableChannelPrimerTest.java @@ -21,9 +21,9 @@ import com.google.auth.oauth2.AccessToken; import com.google.auth.oauth2.OAuth2Credentials; import com.google.bigtable.v2.BigtableGrpc.BigtableImplBase; -import com.google.cloud.bigtable.data.v2.FakeServiceBuilder; import com.google.bigtable.v2.PingAndWarmRequest; import com.google.bigtable.v2.PingAndWarmResponse; +import com.google.cloud.bigtable.data.v2.FakeServiceBuilder; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; import io.grpc.Metadata; diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettingsTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettingsTest.java index c4e5ea2e40..d5a22884b6 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettingsTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettingsTest.java @@ -28,6 +28,7 @@ import com.google.api.gax.rpc.UnaryCallSettings; import com.google.api.gax.rpc.WatchdogProvider; import com.google.auth.Credentials; +import com.google.bigtable.v2.PingAndWarmRequest; import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation; import com.google.cloud.bigtable.data.v2.models.KeyOffset; import com.google.cloud.bigtable.data.v2.models.Query; @@ -645,6 +646,15 @@ public void checkAndMutateRowSettingsAreSane() { assertThat(builder.getRetryableCodes()).isEmpty(); } + @Test + public void pingAndWarmRetriesAreDisabled() { + UnaryCallSettings.Builder builder = + EnhancedBigtableStubSettings.newBuilder().pingAndWarmSettings(); + + assertThat(builder.getRetrySettings().getMaxAttempts()).isAtMost(1); + assertThat(builder.getRetrySettings().getInitialRpcTimeout()).isAtMost(Duration.ofSeconds(30)); + } + private void verifyRetrySettingAreSane(Set retryCodes, RetrySettings retrySettings) { assertThat(retryCodes).containsAtLeast(Code.DEADLINE_EXCEEDED, Code.UNAVAILABLE); @@ -701,6 +711,7 @@ public void isRefreshingChannelFalseValueTest() { "bulkReadRowsSettings", "checkAndMutateRowSettings", "readModifyWriteRowSettings", + "pingAndWarmSettings", }; @Test