-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add option for retrying DML as PDML (#3480)
* feat: add option for retrying DML as PDML Adds an option to the Connection API for automatically retrying DML statements as Partitioned DML, if the DML statement fails due to exceeding the Spanner mutation limit. The retry as Partitiond DML fails if the DML statement is not suitable for Partitioned DML. The option can be enabled with the `fallback_to_partitioned_dml` connection variable. This can be set with a SQL statement like this: ``` SET FALLBACK_TO_PARTITIONED_DML = TRUE; UPDATE my_table SET active=true WHERE true; ``` The property can also be set in the connection URL and by calling the method `Connection#setFallbackToPartitionedDml(boolean)`. This option can also be used in the Spanner JDBC driver and PGAdapter, once those libraries include a version of the Spanner client that includes this change. * refactor: include the option in autocommit_dml_mode
- Loading branch information
Showing
18 changed files
with
1,580 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...ner/src/main/java/com/google/cloud/spanner/TransactionMutationLimitExceededException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.spanner; | ||
|
||
import static com.google.cloud.spanner.SpannerExceptionFactory.extractErrorDetails; | ||
|
||
import com.google.api.gax.rpc.ApiException; | ||
import com.google.api.gax.rpc.ErrorDetails; | ||
import javax.annotation.Nullable; | ||
|
||
/** Exception thrown by Spanner when the transaction mutation limit has been exceeded. */ | ||
public class TransactionMutationLimitExceededException extends SpannerException { | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */ | ||
TransactionMutationLimitExceededException( | ||
DoNotConstructDirectly token, | ||
ErrorCode errorCode, | ||
String message, | ||
Throwable cause, | ||
@Nullable ApiException apiException) { | ||
super(token, errorCode, /*retryable = */ false, message, cause, apiException); | ||
} | ||
|
||
static boolean isTransactionMutationLimitException(Throwable cause) { | ||
if (cause == null | ||
|| cause.getMessage() == null | ||
|| !cause.getMessage().contains("The transaction contains too many mutations.")) { | ||
return false; | ||
} | ||
// Spanner includes a hint that points to the Spanner limits documentation page when the error | ||
// was that the transaction mutation limit was exceeded. We use that here to identify the error, | ||
// as there is no other specific metadata in the error that identifies it (other than the error | ||
// message). | ||
ErrorDetails errorDetails = extractErrorDetails(cause); | ||
if (errorDetails != null && errorDetails.getHelp() != null) { | ||
return errorDetails.getHelp().getLinksCount() == 1 | ||
&& errorDetails | ||
.getHelp() | ||
.getLinks(0) | ||
.getDescription() | ||
.equals("Cloud Spanner limits documentation.") | ||
&& errorDetails | ||
.getHelp() | ||
.getLinks(0) | ||
.getUrl() | ||
.equals("https://cloud.google.com/spanner/docs/limits"); | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.