Skip to content

Commit

Permalink
fix: Only override built-in retry settings when the customer provides…
Browse files Browse the repository at this point in the history
… them. (#1588)

* Only overrride retry settings when the customer provides them.

* Pretty

* Add unit test

* Fix tests

* Revert change to encapsulation

* Refactor
  • Loading branch information
tom-andersen committed Mar 4, 2024
1 parent 2672d71 commit 103c37a
Show file tree
Hide file tree
Showing 2 changed files with 370 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@

package com.google.cloud.firestore.spi.v1;

import com.google.api.core.ApiFunction;
import com.google.api.gax.core.BackgroundResource;
import com.google.api.gax.core.GaxProperties;
import com.google.api.gax.grpc.GrpcCallContext;
import com.google.api.gax.grpc.GrpcTransportChannel;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.BidiStreamingCallable;
import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.HeaderProvider;
import com.google.api.gax.rpc.NoHeaderProvider;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.TransportChannel;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.cloud.NoCredentials;
import com.google.cloud.ServiceOptions;
Expand Down Expand Up @@ -126,17 +125,38 @@ public GrpcFirestoreRpc(final FirestoreOptions options) throws IOException {

clientContext = ClientContext.create(settingsBuilder.build());
}
ApiFunction<UnaryCallSettings.Builder<?, ?>, Void> retrySettingsSetter =
builder -> {
builder.setRetrySettings(options.getRetrySettings());
return null;
};

FirestoreStubSettings.Builder firestoreBuilder =
FirestoreStubSettings.newBuilder(clientContext)
.applyToAllUnaryMethods(retrySettingsSetter);
// Manually apply the retry settings to streaming methods
firestoreBuilder.runQuerySettings().setRetrySettings(options.getRetrySettings());
firestoreBuilder.batchGetDocumentsSettings().setRetrySettings(options.getRetrySettings());
FirestoreStubSettings.newBuilder(clientContext);
RetrySettings retrySettings = options.getRetrySettings();

// Override retry settings only if customer provides settings different from default.
if (retrySettings.equals(ServiceOptions.getDefaultRetrySettings())) {
// We are manually setting `setMaxAttempts(5)` to follow
// the `firestore_grpc_service_config.json` configuration.
// This code should be removed when following issue is fixed:
// https://github.com/googleapis/sdk-platform-java/issues/2306
firestoreBuilder.applyToAllUnaryMethods(
builder -> {
builder.retrySettings().setMaxAttempts(5);
return null;
});
// Manually apply the retry settings to streaming methods
firestoreBuilder.runQuerySettings().retrySettings().setMaxAttempts(5);
firestoreBuilder.runAggregationQuerySettings().retrySettings().setMaxAttempts(5);
firestoreBuilder.batchGetDocumentsSettings().retrySettings().setMaxAttempts(5);
} else {
firestoreBuilder.applyToAllUnaryMethods(
builder -> {
builder.setRetrySettings(retrySettings);
return null;
});
// Manually apply the retry settings to streaming methods
firestoreBuilder.runQuerySettings().setRetrySettings(retrySettings);
firestoreBuilder.runAggregationQuerySettings().setRetrySettings(retrySettings);
firestoreBuilder.batchGetDocumentsSettings().setRetrySettings(retrySettings);
}

firestoreStub = GrpcFirestoreStub.create(firestoreBuilder.build());
} catch (Exception e) {
throw new IOException(e);
Expand Down
Loading

0 comments on commit 103c37a

Please sign in to comment.