|
58 | 58 | import com.google.cloud.spanner.spi.v1.GapicSpannerRpc; |
59 | 59 | import com.google.cloud.spanner.spi.v1.SpannerRpc; |
60 | 60 | import com.google.cloud.spanner.v1.SpannerSettings; |
| 61 | +import com.google.cloud.spanner.v1.stub.SpannerStub; |
61 | 62 | import com.google.cloud.spanner.v1.stub.SpannerStubSettings; |
62 | 63 | import com.google.common.annotations.VisibleForTesting; |
63 | 64 | import com.google.common.base.MoreObjects; |
|
70 | 71 | import com.google.spanner.v1.ExecuteSqlRequest; |
71 | 72 | import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions; |
72 | 73 | import com.google.spanner.v1.RequestOptions; |
| 74 | +import com.google.spanner.v1.RollbackRequest; |
73 | 75 | import com.google.spanner.v1.SpannerGrpc; |
74 | 76 | import com.google.spanner.v1.TransactionOptions; |
75 | 77 | import com.google.spanner.v1.TransactionOptions.IsolationLevel; |
@@ -204,6 +206,25 @@ public static GcpChannelPoolOptions createDefaultDynamicChannelPoolOptions() { |
204 | 206 | .build(); |
205 | 207 | } |
206 | 208 |
|
| 209 | + /** |
| 210 | + * Use the same {@link RetrySettings} for retrying an aborted transaction as for retrying a {@link |
| 211 | + * RollbackRequest}. The {@link RollbackRequest} automatically uses the default retry settings |
| 212 | + * defined for the {@link SpannerStub}. By referencing these settings, the retry settings for |
| 213 | + * retrying aborted transactions will also automatically be updated if the default retry settings |
| 214 | + * are updated. |
| 215 | + * |
| 216 | + * <p>A read/write transaction should not time out while retrying. The total timeout of the retry |
| 217 | + * settings is therefore set to 24 hours and there is no max attempts value. |
| 218 | + * |
| 219 | + * <p>These default {@link RetrySettings} are only used if no retry information is returned by the |
| 220 | + * {@link AbortedException}. |
| 221 | + */ |
| 222 | + public static final RetrySettings DEFAULT_TRANSACTION_RETRY_SETTINGS = |
| 223 | + SpannerStubSettings.newBuilder().rollbackSettings().getRetrySettings().toBuilder() |
| 224 | + .setTotalTimeoutDuration(Duration.ofHours(24L)) |
| 225 | + .setMaxAttempts(0) |
| 226 | + .build(); |
| 227 | + |
207 | 228 | private final TransportChannelProvider channelProvider; |
208 | 229 | private final ChannelEndpointCacheFactory channelEndpointCacheFactory; |
209 | 230 |
|
@@ -264,6 +285,7 @@ public static GcpChannelPoolOptions createDefaultDynamicChannelPoolOptions() { |
264 | 285 | private final boolean enableEndToEndTracing; |
265 | 286 | private final String monitoringHost; |
266 | 287 | private final TransactionOptions defaultTransactionOptions; |
| 288 | + private final RetrySettings defaultTransactionRetrySettings; |
267 | 289 | private final RequestOptions.ClientContext clientContext; |
268 | 290 |
|
269 | 291 | enum TracingFramework { |
@@ -934,6 +956,7 @@ protected SpannerOptions(Builder builder) { |
934 | 956 | enableEndToEndTracing = builder.enableEndToEndTracing; |
935 | 957 | monitoringHost = builder.monitoringHost; |
936 | 958 | defaultTransactionOptions = builder.defaultTransactionOptions; |
| 959 | + defaultTransactionRetrySettings = builder.defaultTransactionRetrySettings; |
937 | 960 | clientContext = builder.clientContext; |
938 | 961 | } |
939 | 962 |
|
@@ -1196,6 +1219,7 @@ public static class Builder |
1196 | 1219 | private String experimentalHost = null; |
1197 | 1220 | private boolean usePlainText = false; |
1198 | 1221 | private TransactionOptions defaultTransactionOptions = TransactionOptions.getDefaultInstance(); |
| 1222 | + private RetrySettings defaultTransactionRetrySettings = TransactionRetryHelper.DEFAULT_TRANSACTION_RETRY_SETTINGS; |
1199 | 1223 | private RequestOptions.ClientContext clientContext; |
1200 | 1224 |
|
1201 | 1225 | private static String createCustomClientLibToken(String token) { |
@@ -1302,6 +1326,7 @@ protected Builder() { |
1302 | 1326 | this.enableEndToEndTracing = options.enableEndToEndTracing; |
1303 | 1327 | this.monitoringHost = options.monitoringHost; |
1304 | 1328 | this.defaultTransactionOptions = options.defaultTransactionOptions; |
| 1329 | + this.defaultTransactionRetrySettings = options.defaultTransactionRetrySettings; |
1305 | 1330 | this.clientContext = options.clientContext; |
1306 | 1331 | } |
1307 | 1332 |
|
@@ -2055,6 +2080,13 @@ public Builder setDefaultTransactionOptions( |
2055 | 2080 | return this; |
2056 | 2081 | } |
2057 | 2082 |
|
| 2083 | + /** Sets the default {@link RetrySettings} for all read/write transactions that are executed using this client. These settings are used when the client automatically retries an aborted read/write transaction. The default is to retry for up to 24 hours without a limit for the maximum number of attempts. */ |
| 2084 | + public Builder setDefaultTransactionRetrySettings(RetrySettings retrySettings) { |
| 2085 | + Preconditions.checkNotNull(retrySettings, "RetrySettings cannot be null"); |
| 2086 | + this.defaultTransactionRetrySettings = retrySettings; |
| 2087 | + return this; |
| 2088 | + } |
| 2089 | + |
2058 | 2090 | /** Sets the default {@link RequestOptions.ClientContext} for all requests. */ |
2059 | 2091 | public Builder setDefaultClientContext(RequestOptions.ClientContext clientContext) { |
2060 | 2092 | this.clientContext = clientContext; |
@@ -2481,6 +2513,10 @@ public TransactionOptions getDefaultTransactionOptions() { |
2481 | 2513 | return defaultTransactionOptions; |
2482 | 2514 | } |
2483 | 2515 |
|
| 2516 | + public RetrySettings getDefaultTransactionRetrySettings() { |
| 2517 | + return this.defaultTransactionRetrySettings; |
| 2518 | + } |
| 2519 | + |
2484 | 2520 | @BetaApi |
2485 | 2521 | public boolean isUseVirtualThreads() { |
2486 | 2522 | return useVirtualThreads; |
|
0 commit comments