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
Contributor

@olavloite olavloite commented Mar 19, 2021

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 thiagotnunes Mar 19, 2021
@olavloite olavloite requested a review from as a code owner Mar 19, 2021
@google-cla google-cla bot added the cla: yes label Mar 19, 2021
@product-auto-label product-auto-label bot added the api: spanner label Mar 19, 2021
@codecov
Copy link

@codecov 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

@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

@thiagotnunes thiagotnunes Mar 21, 2021

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
Contributor Author

@olavloite olavloite Mar 22, 2021

Choose a reason for hiding this comment

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

Done.

@olavloite
Copy link
Contributor Author

@olavloite olavloite commented Mar 22, 2021

@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 label Mar 22, 2021
@gcf-merge-on-green gcf-merge-on-green bot merged commit a95f6f8 into master Mar 22, 2021
18 checks passed
@gcf-merge-on-green gcf-merge-on-green bot deleted the retry-cancelled-on-first-statement branch Mar 22, 2021
@gcf-merge-on-green gcf-merge-on-green bot removed the automerge 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 cla: yes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants