Repository navigation
Conversation
Concurrency tuning guides show configuring OPTIMISTIC or PESSIMISTIC read_lock_mode defaults on spanner.Client and overriding per transaction. Add a standalone spanner_read_lock_mode sample.
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new utility for configuring Spanner read lock modes, specifically demonstrating client-default OPTIMISTIC and per-transaction PESSIMISTIC locking. The reviewer identified a potential runtime error in the transaction logic where an empty result set could trigger an unhandled StopIteration exception, and suggested improving the robustness of the live integration test by ensuring the target row exists before execution.
| row = next( | ||
| iter( | ||
| transaction.execute_sql( | ||
| "SELECT AlbumTitle FROM Albums WHERE SingerId = 2 AND AlbumId = 1" | ||
| ) | ||
| ) | ||
| ) | ||
| previous_title = str(row[0]) | ||
| updated_rows = transaction.execute_update( | ||
| "UPDATE Albums SET AlbumTitle = 'A New Title' " | ||
| "WHERE SingerId = 2 AND AlbumId = 1" | ||
| ) |
There was a problem hiding this comment.
If the requested row does not exist in the Albums table, calling next(iter(...)) will raise a StopIteration exception, which is not caught by the try-except block and will cause the transaction to fail unexpectedly. Using next(iter(...), None) and checking for None provides a safer, more defensive approach.
results = transaction.execute_sql(
"SELECT AlbumTitle FROM Albums WHERE SingerId = 2 AND AlbumId = 1"
)
row = next(iter(results), None)
if row is not None:
previous_title = str(row[0])
updated_rows = transaction.execute_update(
"UPDATE Albums SET AlbumTitle = 'A New Title' "
"WHERE SingerId = 2 AND AlbumId = 1"
)There was a problem hiding this comment.
Good catch, done: the lookup uses next(iter(...), None) and raises a descriptive error if the row is missing, which the sample reports on stderr (ca58479).
| try: | ||
| read_lock_mode.run_transaction_with_read_lock_mode( | ||
| conftest.LIVE_PROJECT_ID, | ||
| conftest.LIVE_INSTANCE_ID, | ||
| conftest.LIVE_DATABASE_ID, | ||
| ) | ||
| out, err = capsys.readouterr() | ||
| assert err == "" | ||
| assert conftest.LIVE_PROJECT_ID not in out | ||
| assert "Updated Rows: 1" in out | ||
| finally: | ||
| with db.batch() as batch: | ||
| batch.update( | ||
| table="Albums", | ||
| columns=("SingerId", "AlbumId", "AlbumTitle"), | ||
| values=[(2, 1, "Green")], | ||
| ) |
There was a problem hiding this comment.
The live integration test expects to update exactly 1 row. However, if the target row does not exist in the database prior to running the test, the transaction will update 0 rows, causing the assertion to fail. Ensuring the row exists by performing an insert_or_update operation before running the transaction makes the test robust and self-contained.
# Ensure the target row exists before running the transaction
with db.batch() as batch:
batch.insert_or_update(
table="Albums",
columns=("SingerId", "AlbumId", "AlbumTitle"),
values=[(2, 1, "Green")],
)
try:
read_lock_mode.run_transaction_with_read_lock_mode(
conftest.LIVE_PROJECT_ID,
conftest.LIVE_INSTANCE_ID,
conftest.LIVE_DATABASE_ID,
)
out, err = capsys.readouterr()
assert err == ""
assert conftest.LIVE_PROJECT_ID not in out
assert "Updated Rows: 1" in out
finally:
with db.batch() as batch:
batch.insert_or_update(
table="Albums",
columns=("SingerId", "AlbumId", "AlbumTitle"),
values=[(2, 1, "Green")],
)There was a problem hiding this comment.
Done: the live test inserts-or-updates the row before running and restores it in finally (ca58479).
Problem: review of this sample identified correctness and robustness improvements in the sample and its tests. Solution: apply the accepted review suggestions and extend the tests to cover the updated behavior. Details are in the review thread replies. Files updated: - spanner/transactions/src/spanner_transactions/read_lock_mode.py - spanner/transactions/tests/read_lock_mode_test.py
Problem: multi-line SQL and DDL statements were written as adjacent string fragments. That style is easy to get wrong (a missing trailing space joins two SQL tokens) and is harder to read than the statement itself, and the samples in this set mixed it with triple-quoted SQL. Solution: write each multi-line statement as a single triple-quoted string, matching the layout already used by the other samples. Only whitespace between SQL tokens changes; the statements are otherwise identical. Files updated: - spanner/transactions/src/spanner_transactions/read_lock_mode.py
Description
Adds
spanner_read_lock_modedemonstrating client-levelOPTIMISTICand per-transactionPESSIMISTICReadLockModeconfiguration.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions