Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: retry cancelled error on first statement in transaction #999

Merged
merged 2 commits into from Mar 22, 2021

Conversation

olavloite
Copy link
Collaborator

If the first statement of a read/write transaction fails with a CANCELLED error and the error message is Read/query was cancelled due to the enclosing transaction being invalidated by a later transaction in the same session., then the transaction should be retried, as the error could be caused by a previous statement that was abandoned by the client but still executed by the backend. This could be the case if the statement timed out (on the client) or was cancelled.

Fixes #938

If the first statement of a read/write transaction fails with a CANCELLED error
and the error message is `Read/query was cancelled due to the enclosing transaction
being invalidated by a later transaction in the same session.`, then the transaction
should be retried, as the error could be caused by a previous statement that was
abandoned by the client but still executed by the backend. This could be the case
if the statement timed out (on the client) or was cancelled.

Fixes #938
@olavloite olavloite requested a review from a team as a code owner March 19, 2021 12:04
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Mar 19, 2021
@product-auto-label product-auto-label bot added the api: spanner Issues related to the googleapis/java-spanner API. label Mar 19, 2021
@codecov
Copy link

codecov bot commented Mar 19, 2021

Codecov Report

Merging #999 (3c5b5e1) into master (459a477) will increase coverage by 0.06%.
The diff coverage is 95.65%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #999      +/-   ##
============================================
+ Coverage     85.07%   85.13%   +0.06%     
- Complexity     2617     2621       +4     
============================================
  Files           154      154              
  Lines         14319    14324       +5     
  Branches       1331     1334       +3     
============================================
+ Hits          12182    12195      +13     
+ Misses         1569     1566       -3     
+ Partials        568      563       -5     
Impacted Files Coverage Δ Complexity Δ
...om/google/cloud/spanner/TransactionRunnerImpl.java 86.07% <95.00%> (+0.14%) 10.00 <0.00> (ø)
.../com/google/cloud/spanner/AbstractReadContext.java 86.54% <100.00%> (ø) 42.00 <1.00> (ø)
...va/com/google/cloud/spanner/AbstractResultSet.java 83.45% <100.00%> (ø) 28.00 <0.00> (ø)
...ain/java/com/google/cloud/spanner/SessionPool.java 89.32% <0.00%> (+0.38%) 73.00% <0.00%> (+1.00%)
.../google/cloud/spanner/SpannerExceptionFactory.java 87.96% <0.00%> (+2.77%) 48.00% <0.00%> (+2.00%)
.../google/cloud/spanner/AbstractLazyInitializer.java 100.00% <0.00%> (+7.14%) 5.00% <0.00%> (+1.00%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 459a477...3c5b5e1. Read the comment docs.

Copy link
Contributor

@thiagotnunes thiagotnunes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@olavloite without this fix, the user would just see a cancelled error and no retry would be made automatically, right?

SpannerException exceptionToThrow;
if (withBeginTransaction
&& e.getErrorCode() == ErrorCode.CANCELLED
&& e.getMessage().contains("invalidated by a later transaction")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could we extract a constant for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@olavloite
Copy link
Collaborator Author

@olavloite without this fix, the user would just see a cancelled error and no retry would be made automatically, right?

Correct. That is also exactly what happened in #938.

@olavloite olavloite added the automerge Merge the pull request once unit tests and other checks pass. label Mar 22, 2021
@gcf-merge-on-green gcf-merge-on-green bot merged commit a95f6f8 into master Mar 22, 2021
@gcf-merge-on-green gcf-merge-on-green bot deleted the retry-cancelled-on-first-statement branch March 22, 2021 09:34
@gcf-merge-on-green gcf-merge-on-green bot removed the automerge Merge the pull request once unit tests and other checks pass. label Mar 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the googleapis/java-spanner API. cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

spanner.connection.it.ITReadWriteAutocommitSpannerTest: test03_MultipleStatements_WithTimeouts failed
2 participants