Skip to content

Commit ee6548c

Browse files
feat: make administrative request retries optional (#2476)
* feat: make administrative request retries optional Make retries of administrative requests that fail because the administrative request limit has been exceeded optional. This allows users to apply custom retries or other custom handling for these errors. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent a49c872 commit ee6548c

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -993,13 +993,26 @@ public Builder setAutoThrottleAdministrativeRequests() {
993993
return this;
994994
}
995995

996+
/**
997+
* Disables automatic retries of administrative requests that fail if the <a
998+
* href="https://cloud.google.com/spanner/quotas#administrative_limits">https://cloud.google.com/spanner/quotas#administrative_limits</a>
999+
* have been exceeded. You should disable these retries if you intend to handle these errors in
1000+
* your application.
1001+
*/
1002+
public Builder disableAdministrativeRequestRetries() {
1003+
this.retryAdministrativeRequestsSettings =
1004+
this.retryAdministrativeRequestsSettings.toBuilder().setMaxAttempts(1).build();
1005+
return this;
1006+
}
1007+
9961008
/**
9971009
* Sets the retry settings for retrying administrative requests when the quote of administrative
9981010
* requests per minute has been exceeded.
9991011
*/
10001012
Builder setRetryAdministrativeRequestsSettings(
10011013
RetrySettings retryAdministrativeRequestsSettings) {
1002-
this.retryAdministrativeRequestsSettings = retryAdministrativeRequestsSettings;
1014+
this.retryAdministrativeRequestsSettings =
1015+
Preconditions.checkNotNull(retryAdministrativeRequestsSettings);
10031016
return this;
10041017
}
10051018

google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseAdminClientTest.java

+27
Original file line numberDiff line numberDiff line change
@@ -935,4 +935,31 @@ public void testRetryOperationOnAdminMethodQuotaPerMinutePerProjectExceeded() {
935935
assertEquals(DB_ID, database.getId().getDatabase());
936936
assertEquals(2, mockDatabaseAdmin.countRequestsOfType(GetDatabaseRequest.class));
937937
}
938+
939+
@Test
940+
public void testRetriesDisabledForOperationOnAdminMethodQuotaPerMinutePerProjectExceeded() {
941+
ErrorInfo info =
942+
ErrorInfo.newBuilder()
943+
.putMetadata("quota_limit", "AdminMethodQuotaPerMinutePerProject")
944+
.build();
945+
Metadata.Key<ErrorInfo> key =
946+
Metadata.Key.of(
947+
info.getDescriptorForType().getFullName() + Metadata.BINARY_HEADER_SUFFIX,
948+
ProtoLiteUtils.metadataMarshaller(info));
949+
Metadata trailers = new Metadata();
950+
trailers.put(key, info);
951+
mockDatabaseAdmin.addException(
952+
Status.RESOURCE_EXHAUSTED.withDescription("foo").asRuntimeException(trailers));
953+
mockDatabaseAdmin.clearRequests();
954+
955+
Spanner spannerWithoutRetries =
956+
spanner.getOptions().toBuilder().disableAdministrativeRequestRetries().build().getService();
957+
AdminRequestsPerMinuteExceededException exception =
958+
assertThrows(
959+
AdminRequestsPerMinuteExceededException.class,
960+
() -> spannerWithoutRetries.getDatabaseAdminClient().getDatabase(INSTANCE_ID, DB_ID));
961+
assertEquals(ErrorCode.RESOURCE_EXHAUSTED, exception.getErrorCode());
962+
// There should be only one request on the server, as the request was not retried.
963+
assertEquals(1, mockDatabaseAdmin.countRequestsOfType(GetDatabaseRequest.class));
964+
}
938965
}

0 commit comments

Comments
 (0)