GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: support aborted transactions internal retry by IlyaFaer · Pull Request #544 · googleapis/python-spanner-django

This repository was archived by the owner on Jun 8, 2026. It is now read-only.

Conversation

@IlyaFaer

@IlyaFaer IlyaFaer commented Oct 23, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Implement aborted transactions retry mechanism.

While executing SQL statements in !autocommit mode, connection must remember every executed statement. In case the transaction aborted, all of these statements should be re-executed. Doing this, connection also must calculate checksum of every statement results, so that we could check if the retried transaction got the same results that the original one got. In case the checksums are not equal there is no way to continue transaction due to underlying data being changed during retry.

Closes #539

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
IlyaFaer
feat: support aborted transactions internal retry
50580b4
@IlyaFaer IlyaFaer added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. api: spanner Issues related to the googleapis/python-spanner-django API. labels Oct 23, 2020
@google-cla google-cla Bot added the cla: yes This human has signed the Contributor License Agreement. label Oct 23, 2020
IlyaFaer
IlyaFaer commented Oct 23, 2020
View reviewed changes
Comment thread google/cloud/spanner_dbapi/checksum.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/connection.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/connection.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/cursor.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/cursor.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@IlyaFaer

IlyaFaer commented Oct 23, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Author

@c24t, @olavloite, I've pushed a part of transaction retry mechanism implementation, PTAL.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

olavloite
olavloite reviewed Oct 23, 2020
View reviewed changes
Comment thread google/cloud/spanner_dbapi/cursor.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/checksum.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/checksum.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/cursor.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/cursor.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/cursor.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread tests/spanner_dbapi/test_checksum.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/checksum.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread tests/spanner_dbapi/test_checksum.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

IlyaFaer
add a transaction retry method
481db2a
@IlyaFaer

IlyaFaer commented Oct 26, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Author

Pushed the second step of the implementation. Added actual retrying of aborted transaction. Requires to add more unit tests yet, for Cursor - to check if fetch* methods are retrying transactions correctly.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

IlyaFaer added 3 commits October 27, 2020 12:58
IlyaFaer
add retrying all the statements up to failed one
313e16d
IlyaFaer
Merge branch 'master' into aborted_transactions_retry
3a7537c
IlyaFaer
resolve conflicts
4edf6c1
IlyaFaer
IlyaFaer commented Oct 27, 2020
View reviewed changes
Comment thread tests/spanner_dbapi/test_connect.py
client_mock.assert_called_once_with(
project=PROJECT,
credentials=CREDENTIALS,
client_info=CLIENT_INFO,

@IlyaFaer IlyaFaer Oct 27, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This test is actually broken by another PR, but nox doesn't see it and doesn't run it. These tests were not moved to unit directory.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
IlyaFaer
move unit tests
f87129a
IlyaFaer
IlyaFaer commented Oct 27, 2020
View reviewed changes
Comment thread tests/spanner_dbapi/test_connection.py
from google.cloud.spanner_dbapi.checksum import ResultsChecksum
from google.cloud.spanner_dbapi.connection import AUTOCOMMIT_MODE_WARNING
from google.cloud.spanner_v1.database import Database
from google.cloud.spanner_v1.instance import Instance

@IlyaFaer IlyaFaer Oct 27, 2020

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Why these tests are still here? They were copied into unit directory, so I suppose they should be erased from this directory?!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@c24t c24t Oct 28, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Good question. Looks like this (and test_connect) weren't moved in #532?

https://github.com/q-logic/python-spanner-django/blob/41abaebb6f2e0b1cf16704aa1e394acc5a47e68b/tests/spanner_dbapi/test_connection.py

The test files weren't exactly copied, #532 changed them and added some new tests. E.g. the version on master now doesn't include test_transaction_autocommit_warnings.

@mf2199 can you confirm that you meant to change/remove these tests before removing tests/spanner_dbapi in this PR?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
IlyaFaer
fix tests
49e17be
IlyaFaer
IlyaFaer commented Oct 27, 2020
View reviewed changes
Comment thread google/cloud/spanner_dbapi/cursor.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

IlyaFaer
IlyaFaer commented Oct 28, 2020
View reviewed changes
Comment thread google/cloud/spanner_dbapi/cursor.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

IlyaFaer added 4 commits October 28, 2020 10:58
IlyaFaer
small fixes
a8158b3
IlyaFaer
fix lint and unit tests
ccf5385
IlyaFaer
fix lint and unit tests
a537628
IlyaFaer
fix imports
0b1a641
@IlyaFaer

IlyaFaer commented Oct 28, 2020

Copy link
Copy Markdown
Author

@olavloite, am I understood correctly, that commit() can also be aborted? And in this case we should retry the whole transaction?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@olavloite

olavloite commented Oct 28, 2020

Copy link
Copy Markdown
Contributor

@olavloite, am I understood correctly, that commit() can also be aborted? And in this case we should retry the whole transaction?

@IlyaFaer Yes, that is correct.

IlyaFaer reacted with rocket emoji
All reactions
  • 🚀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

IlyaFaer
add aborted commit retry
0e2ca3e
olavloite
olavloite reviewed Oct 28, 2020
View reviewed changes
Comment thread google/cloud/spanner_dbapi/connection.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

c24t
c24t reviewed Oct 28, 2020
View reviewed changes

@c24t c24t left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Besides cleaning up the tests, I wonder if this PR is catching Aborted errors in the right place. Since we're only calling execute_sql and not the streaming variant, I'd expect to see errors immediately instead of when we iterate over the results in the cursor. Am I missing something here?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/unit/spanner_dbapi/test_cursor.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/cursor.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/connection.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/connection.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

IlyaFaer added 2 commits October 29, 2020 12:00
IlyaFaer
add retry inside retry, cleanup tests
a4ffab5
IlyaFaer
lint fix
fc890c2
olavloite
olavloite reviewed Oct 29, 2020
View reviewed changes
Comment thread google/cloud/spanner_dbapi/cursor.py
return
except Aborted:
self.connection.retry_transaction()
return self.fetchone()

@olavloite olavloite Oct 29, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I think this will be a problem. Assuming that this is using the ExecuteStreamingSql RPC, then each next() call could potentially mean that a new RPC is executed. So for the sake of simplicity, assume in the example below that each call to next() executes the ExecuteStreamingSql RPC.

Assume the following situation:

  1. The table Singers contains the following singers (last names): Allison, Morrison, Pieterson
  2. The application executes the query SELECT LastName FROM Singers ORDER BY LastName in transaction 1.
  3. The client application calls fetchone() which returns 'Allison'.
  4. Some other transaction executes `DELETE FROM Singers WHERE LastName='Pieterson'.
  5. The first transaction is aborted by the backend. A retry is executed and the retry logic checks that the checksum of the retried result set is equal to the original attempt, which it is as the first record is still 'Allison'.
  6. The client application calls fetchone() again. This should return 'Morrison', but as it needs to call ExecuteStreamingSql it will (probably) use the transaction id of the original transaction (unless that transaction id has somehow been replaced in the underlying iterator). If it does use the old transaction id, the RPC will fail with yet another Aborted error, and that will repeat itself until the transaction retry limit has been reached.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@IlyaFaer IlyaFaer Oct 30, 2020

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Seems to me we can just drop the _transaction property, so that Connection will initiate a new one on the next execute() call.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@olavloite olavloite Nov 12, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@IlyaFaer @c24t

Sorry for reopening this, and this comment should not be considered blocking for merging this PR, but I think we need to look into this once more. Only dropping the _transaction property will in this case not be enough for the following reason:

  1. When executeSql is called, a streaming iterator is returned to the application.
  2. That streaming iterator is linked with the transaction that was active at that moment, and a reference to that transaction is also held in the iterator.
  3. If a transaction is aborted and the client application has consumed only parts of a streaming iterator, that iterator is no longer valid (at least: it will also throw an exception if it needs to receive more data from the server).

The JDBC driver client solves the above problem by wrapping all streaming iterators before returning these to the client application. That makes it possible for the JDBC driver to replace the underlying streaming iterator with a new one when a transaction has been aborted and successfully retried.

We should add that to the Python DBApi as well, but we could do that in a separate PR to prevent this PR from becoming even bigger than it already is.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@IlyaFaer IlyaFaer Nov 13, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@c24t, @olavloite, hm-m. I think we're protected from errors here, because our connection API doesn't actually give streaming result objects to a user.

Here is where we're getting a streaming iterator:

python-spanner-django/google/cloud/spanner_dbapi/cursor.py

Lines 167 to 170 in 196c449

self._result_set = transaction.execute_sql(
sql, params, param_types=get_param_types(params)
)
self._itr = PeekIterator(self._result_set)

So, iterator is held in the protected property _itr, and users will be streaming it with Cursor.fetch*() methods, without actual access to the iterator itself:

python-spanner-django/google/cloud/spanner_dbapi/cursor.py

Lines 204 to 212 in 196c449

def fetchone(self):
"""Fetch the next row of a query result set, returning a single
sequence, or None when no more data is available."""
self._raise_if_closed()
try:
return next(self)
except StopIteration:
return None

Where next(self) is calling next(self._itr) here:

python-spanner-django/google/cloud/spanner_dbapi/cursor.py

Lines 293 to 296 in 196c449

def __next__(self):
if self._itr is None:
raise ProgrammingError("no results to return")
return next(self._itr)

Thus, if a transaction failed, the connection will drop the transaction, checkout a new one, re-run all the statements, each of which will replace _itr with a new streamed iterator. So, all the iterators are processed internally, and will be replaced on a retry, as I see.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
IlyaFaer added 3 commits October 30, 2020 12:35
IlyaFaer
use new transaction on retry
d204836
IlyaFaer
fix imports
9ea2a01
IlyaFaer
add retrying limit
7e70d86
@IlyaFaer
IlyaFaer marked this pull request as ready for review November 4, 2020 07:25
@IlyaFaer
IlyaFaer requested a review from a team November 4, 2020 07:25
olavloite
olavloite reviewed Nov 4, 2020
View reviewed changes

@olavloite olavloite left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks for the updates. I think this is coming very to close to what we need, but I have a couple of small questions.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread google/cloud/spanner_dbapi/exceptions.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/connection.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/connection.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

IlyaFaer and others added 4 commits November 5, 2020 10:59
IlyaFaer
Merge branch 'master' into aborted_transactions_retry
870e170
IlyaFaer
rename the exception, fix remembering retried statements
450b91b
IlyaFaer
erase excess while cycle - retry_transaction() is already using its own
578eaa2
@c24t
Merge branch 'master' into aborted_transactions_retry
59d597a
c24t
c24t reviewed Nov 12, 2020
View reviewed changes

@c24t c24t left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

No more substantial comments from me, but note that this whole PR will have to move to python-spanner now that googleapis/python-spanner#160 is in.

@olavloite, does this address all your comments?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@olavloite

olavloite commented Nov 12, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

No more substantial comments from me, but note that this whole PR will have to move to python-spanner now that googleapis/python-spanner#160 is in.

@olavloite, does this address all your comments?

Yes, my comments have been addressed. I do think that it's important that we get the (integration) tests running soon, as there could still be corner cases that we haven't thought of yet that only occur incidentally.

I also have a separate concern regarding streaming iterators, but that should not block the merging of this PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

c24t
c24t approved these changes Nov 17, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

View reviewed changes
@c24t c24t mentioned this pull request Nov 17, 2020
feat(dbapi): add aborted transactions retry support googleapis/python-spanner#168
Merged
@c24t

c24t commented Nov 17, 2020

Copy link
Copy Markdown
Contributor

This PR won't be merged here, see googleapis/python-spanner#168.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

c24t pushed a commit to googleapis/python-spanner that referenced this pull request Nov 23, 2020
Ilya Gurov
feat(dbapi): add aborted transactions retry support (#168)
d59d502
Fixes #34.

See googleapis/python-spanner-django#544.
@IlyaFaer

IlyaFaer commented Nov 30, 2020

Copy link
Copy Markdown
Author

Merged into the original Spanner client repo

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@IlyaFaer IlyaFaer closed this Nov 30, 2020
@IlyaFaer
IlyaFaer deleted the aborted_transactions_retry branch November 30, 2020 07:03
gcf-merge-on-green Bot pushed a commit to googleapis/python-spanner that referenced this pull request Nov 30, 2020
@release-please
chore: release 2.1.0 (#173)
cf87cdf
🤖 I have created a release \*beep\* \*boop\* 
---
## [2.1.0](https://www.github.com/googleapis/python-spanner/compare/v2.0.0...v2.1.0) (2020-11-24)


### Features

* **dbapi:** add aborted transactions retry support ([#168](https://www.github.com/googleapis/python-spanner/issues/168)) ([d59d502](https://www.github.com/googleapis/python-spanner/commit/d59d502590f618c8b13920ae05ab11add78315b5)), closes [#34](https://www.github.com/googleapis/python-spanner/issues/34) [googleapis/python-spanner-django#544](https://www.github.com/googleapis/python-spanner-django/issues/544)
* remove adding a dummy WHERE clause into UPDATE and DELETE statements ([#169](https://www.github.com/googleapis/python-spanner/issues/169)) ([7f4d478](https://www.github.com/googleapis/python-spanner/commit/7f4d478fd9812c965cdb185c52aa9a8c9e599bed))


### Bug Fixes

* Add sqlparse dependency ([#171](https://www.github.com/googleapis/python-spanner/issues/171)) ([e801a2e](https://www.github.com/googleapis/python-spanner/commit/e801a2e014fcff66a69cb9da83abedb218cda2ab))


### Reverts

* Revert "test: unskip list_backup_operations sample test (#170)" (#174) ([6053f4a](https://www.github.com/googleapis/python-spanner/commit/6053f4ab0fc647a9cfc181e16c246141483c2397)), closes [#170](https://www.github.com/googleapis/python-spanner/issues/170) [#174](https://www.github.com/googleapis/python-spanner/issues/174)
---


This PR was generated with [Release Please](https://github.com/googleapis/release-please).
parthea pushed a commit to googleapis/google-cloud-python that referenced this pull request Mar 9, 2026
Ilya Gurov
feat(dbapi): add aborted transactions retry support (#168)
b3ff75a
Fixes #34.

See googleapis/python-spanner-django#544.
parthea pushed a commit to googleapis/google-cloud-python that referenced this pull request Mar 9, 2026
@release-please
chore: release 2.1.0 (#173)
ec8a8d7
🤖 I have created a release \*beep\* \*boop\* 
---
## [2.1.0](https://www.github.com/googleapis/python-spanner/compare/v2.0.0...v2.1.0) (2020-11-24)


### Features

* **dbapi:** add aborted transactions retry support ([#168](https://www.github.com/googleapis/python-spanner/issues/168)) ([d59d502](https://www.github.com/googleapis/python-spanner/commit/d59d502590f618c8b13920ae05ab11add78315b5)), closes [#34](https://www.github.com/googleapis/python-spanner/issues/34) [googleapis/python-spanner-django#544](https://www.github.com/googleapis/python-spanner-django/issues/544)
* remove adding a dummy WHERE clause into UPDATE and DELETE statements ([#169](https://www.github.com/googleapis/python-spanner/issues/169)) ([7f4d478](https://www.github.com/googleapis/python-spanner/commit/7f4d478fd9812c965cdb185c52aa9a8c9e599bed))


### Bug Fixes

* Add sqlparse dependency ([#171](https://www.github.com/googleapis/python-spanner/issues/171)) ([e801a2e](https://www.github.com/googleapis/python-spanner/commit/e801a2e014fcff66a69cb9da83abedb218cda2ab))


### Reverts

* Revert "test: unskip list_backup_operations sample test (#170)" (#174) ([6053f4a](https://www.github.com/googleapis/python-spanner/commit/6053f4ab0fc647a9cfc181e16c246141483c2397)), closes [#170](https://www.github.com/googleapis/python-spanner/issues/170) [#174](https://www.github.com/googleapis/python-spanner/issues/174)
---


This PR was generated with [Release Please](https://github.com/googleapis/release-please).
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

2 more reviewers
@olavloite olavloite olavloite left review comments
@c24t c24t c24t approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

api: spanner Issues related to the googleapis/python-spanner-django API. cla: yes This human has signed the Contributor License Agreement. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Transaction retry

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@IlyaFaer @olavloite @c24t
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.