Repository navigation
Conversation
Change stream TVF queries return a sentinel read_timestamp that overflows veneer snapshots, requiring single-use ExecuteStreamingSql via GAPIC.
|
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 script and corresponding tests to stream initial partition records from a Spanner change stream TVF. The review feedback correctly identifies a potential session leak because the created Spanner session is never deleted. It is recommended to wrap the streaming SQL execution in a try...finally block to ensure the session is cleaned up, and to add a corresponding assertion in the unit tests.
| try: | ||
| session_req = spanner_v1.CreateSessionRequest( | ||
| database=database_name, | ||
| session=spanner_v1.Session(multiplexed=True), | ||
| ) | ||
| session = client.create_session(request=session_req) | ||
| sql_req = spanner_v1.ExecuteSqlRequest( | ||
| session=session.name, | ||
| sql=sql, | ||
| params=params, | ||
| param_types={"start_ts": ts_type, "end_ts": ts_type}, | ||
| transaction=selector, | ||
| ) | ||
| chunks = list(client.execute_streaming_sql(request=sql_req)) |
There was a problem hiding this comment.
The created session is not deleted or cleaned up after the streaming SQL query finishes. In Cloud Spanner, sessions are limited resources, and failing to delete them can lead to session leaks. Wrap the query execution in a try...finally block to ensure client.delete_session is called to clean up the session.
session_req = spanner_v1.CreateSessionRequest(
database=database_name,
session=spanner_v1.Session(multiplexed=True),
)
session = client.create_session(request=session_req)
try:
sql_req = spanner_v1.ExecuteSqlRequest(
session=session.name,
sql=sql,
params=params,
param_types={"start_ts": ts_type, "end_ts": ts_type},
transaction=selector,
)
chunks = list(client.execute_streaming_sql(request=sql_req))
finally:
try:
client.delete_session(name=session.name)
except Exception:
passThere was a problem hiding this comment.
Thanks for flagging session cleanup! Here the sample creates a multiplexed session (spanner_v1.Session(multiplexed=True)), whose lifecycle is managed automatically by Cloud Spanner (DeleteSession is not supported on multiplexed sessions). We added an inline comment clarifying this behavior.
| out, err = capsys.readouterr() | ||
| assert f"Resource ID: {DATABASE_NAME}" in out | ||
| assert "Change Stream: SingersAndAlbumsStream" in out | ||
| assert "Stream Chunks Received: 1" in out | ||
| assert err == "" |
There was a problem hiding this comment.
Add an assertion to verify that the created session is properly deleted after the streaming SQL query completes.
| out, err = capsys.readouterr() | |
| assert f"Resource ID: {DATABASE_NAME}" in out | |
| assert "Change Stream: SingersAndAlbumsStream" in out | |
| assert "Stream Chunks Received: 1" in out | |
| assert err == "" | |
| out, err = capsys.readouterr() | |
| assert f"Resource ID: {DATABASE_NAME}" in out | |
| assert "Change Stream: SingersAndAlbumsStream" in out | |
| assert "Stream Chunks Received: 1" in out | |
| assert err == "" | |
| client.delete_session.assert_called_once_with(name=SESSION_NAME) |
There was a problem hiding this comment.
Thanks! Since multiplexed sessions are managed automatically by Cloud Spanner and are not deleted via delete_session, we are skipping this mock assertion.
Documents that multiplexed sessions are Spanner-managed and omits project identifiers from stdout and stderr.
Prints only runtime resource and query results instead of hardcoded string literals.
Problem: reviewers of this sample set asked for stricter and clearer tests and for a single source of truth for dependencies. Mocks were created without a spec, so a renamed client argument would pass silently; the mocked wait timeout was raised from the API call instead of from the operation's result; live tests did not say which environment variables they need; and some tests carried leftover or repeated lines. Package dependencies were pinned in both pyproject.toml and requirements.txt. Solution: where applicable to the files below, patch client classes with autospec=True, raise the mocked TimeoutError from operation.result(), document the required environment variables above each live test and skip when one is missing, remove unused or duplicated test lines, and join string literals that the formatter had left split on one line. For package scaffolding, read dependencies from requirements.txt and update the README install command to match. Files updated: - spanner/change_streams/tests/read_change_stream_test.py
Description
Querying
READ_<ChangeStream>requires a single-use strong read overExecuteStreamingSql, and Spanner returns a sentinelread_timestampin change stream metadata that overflowsdatetimeconversion in high-leveldatabase.snapshot()calls.Adds
spanner_read_change_streamusingspanner_v1.SpannerClient.execute_streaming_sqlon a multiplexed session.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions