Skip to content

Commit

Permalink
add request deadline for ping and warm
Browse files Browse the repository at this point in the history
  • Loading branch information
mutianf committed Jun 16, 2022
1 parent adeedda commit 8d75580
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,8 @@ private UnaryCallable<PingAndWarmRequest, PingAndWarmResponse> createPingAndWarm
@Override
public Map<String, String> extract(PingAndWarmRequest request) {
return ImmutableMap.of(
"name", request.getName(),
"app_profile_id", request.getAppProfileId());
"name", request.getName(),
"app_profile_id", request.getAppProfileId());
}
})
.build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -94,6 +95,8 @@ public class EnhancedBigtableStubSettings extends StubSettings<EnhancedBigtableS
private static final Set<Code> 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()
Expand Down Expand Up @@ -173,6 +176,7 @@ public class EnhancedBigtableStubSettings extends StubSettings<EnhancedBigtableS
private final BigtableBulkReadRowsCallSettings bulkReadRowsSettings;
private final UnaryCallSettings<ConditionalRowMutation, Boolean> checkAndMutateRowSettings;
private final UnaryCallSettings<ReadModifyWriteRow, Row> readModifyWriteRowSettings;
private final UnaryCallSettings<PingAndWarmRequest, Void> pingAndWarmSettings;

private EnhancedBigtableStubSettings(Builder builder) {
super(builder);
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -494,6 +499,15 @@ public UnaryCallSettings<ReadModifyWriteRow, Row> readModifyWriteRowSettings() {
return readModifyWriteRowSettings;
}

/**
* Returns the object with the settings used for calls to PingAndWarm.
*
* <p>By default the retries are disabled for PingAndWarm and deadline is set to 30 seconds.
*/
UnaryCallSettings<PingAndWarmRequest, Void> pingAndWarmSettings() {
return pingAndWarmSettings;
}

/** Returns a builder containing all the values of this settings class. */
public Builder toBuilder() {
return new Builder(this);
Expand All @@ -518,6 +532,7 @@ public static class Builder extends StubSettings.Builder<EnhancedBigtableStubSet
private final UnaryCallSettings.Builder<ConditionalRowMutation, Boolean>
checkAndMutateRowSettings;
private final UnaryCallSettings.Builder<ReadModifyWriteRow, Row> readModifyWriteRowSettings;
private final UnaryCallSettings.Builder<PingAndWarmRequest, Void> pingAndWarmSettings;

/**
* Initializes a new Builder with sane defaults for all settings.
Expand Down Expand Up @@ -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) {
Expand All @@ -649,6 +673,7 @@ private Builder(EnhancedBigtableStubSettings settings) {
bulkReadRowsSettings = settings.bulkReadRowsSettings.toBuilder();
checkAndMutateRowSettings = settings.checkAndMutateRowSettings.toBuilder();
readModifyWriteRowSettings = settings.readModifyWriteRowSettings.toBuilder();
pingAndWarmSettings = settings.pingAndWarmSettings.toBuilder();
}
// <editor-fold desc="Private Helpers">

Expand Down Expand Up @@ -817,6 +842,11 @@ public UnaryCallSettings.Builder<ReadModifyWriteRow, Row> readModifyWriteRowSett
return readModifyWriteRowSettings;
}

/** Returns the builder with the settings used for calls to PingAndWarm. */
public UnaryCallSettings.Builder<PingAndWarmRequest, Void> pingAndWarmSettings() {
return pingAndWarmSettings;
}

@SuppressWarnings("unchecked")
public EnhancedBigtableStubSettings build() {
Preconditions.checkState(projectId != null, "Project id must be set");
Expand Down Expand Up @@ -864,6 +894,7 @@ public String toString() {
.add("bulkReadRowsSettings", bulkReadRowsSettings)
.add("checkAndMutateRowSettings", checkAndMutateRowSettings)
.add("readModifyWriteRowSettings", readModifyWriteRowSettings)
.add("pingAndWarmSettings", pingAndWarmSettings)
.add("parent", super.toString())
.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,9 @@ public void mutateRow(
@Override
public void pingAndWarm(
PingAndWarmRequest request, StreamObserver<PingAndWarmResponse> 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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -645,6 +646,15 @@ public void checkAndMutateRowSettingsAreSane() {
assertThat(builder.getRetryableCodes()).isEmpty();
}

@Test
public void pingAndWarmRetriesAreDisabled() {
UnaryCallSettings.Builder<PingAndWarmRequest, Void> builder =
EnhancedBigtableStubSettings.newBuilder().pingAndWarmSettings();

assertThat(builder.getRetrySettings().getMaxAttempts()).isAtMost(1);
assertThat(builder.getRetrySettings().getInitialRpcTimeout()).isAtMost(Duration.ofSeconds(30));
}

private void verifyRetrySettingAreSane(Set<Code> retryCodes, RetrySettings retrySettings) {
assertThat(retryCodes).containsAtLeast(Code.DEADLINE_EXCEEDED, Code.UNAVAILABLE);

Expand Down Expand Up @@ -701,6 +711,7 @@ public void isRefreshingChannelFalseValueTest() {
"bulkReadRowsSettings",
"checkAndMutateRowSettings",
"readModifyWriteRowSettings",
"pingAndWarmSettings",
};

@Test
Expand Down

0 comments on commit 8d75580

Please sign in to comment.