Skip to content

feat(spanner): Add Spanner Queue RECEIVE and Ack sample - #14869

Open
iennae wants to merge 2 commits into
feat/spanner-queue-send-messagefrom
feat/spanner-queue-receive-messages
Open

iennae wants to merge 2 commits into
feat/spanner-queue-send-messagefrom
feat/spanner-queue-receive-messages

Conversation

@iennae

@iennae iennae commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Description

Consuming messages from a Cloud Spanner Queue requires streaming from RECEIVE_<Queue> under a strong snapshot, verifying SpannerLeaseExpirationTimestamp, and acknowledging processed keys via batch.ack.

Adds spanner_queue_receive_messages.

Note: Stacked on #14856 (feat/spanner-queue-send-message) so this PR shows only this sample's diff. It will merge into main after #14856.

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended. Unit and live integration tests pass on Python 3.13.

Compliance & Style


Post-Approval Actions

  • Please merge this PR for me once it is approved

Consuming leased messages from RECEIVE_<Queue> and acknowledging active leases via batch.ack lacked a runnable Python worker sample.
@iennae
iennae requested review from a team as code owners November 17, 2026 23:48
@product-auto-label product-auto-label Bot added api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples. labels Nov 17, 2026
@snippet-bot

snippet-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces functionality to receive and acknowledge messages from a Cloud Spanner queue, specifically querying the RECEIVE_UserTasks table-valued function, validating lease expirations, and acknowledging valid messages. It also includes comprehensive unit and integration tests. The review feedback recommends optimizing the message acknowledgement loop by wrapping it in a single database.batch() context manager to batch all acknowledgements into a single transaction rather than committing them individually.

Comment on lines +58 to +61
for key in valid_keys:
with database.batch() as batch:
batch.ack(queue=queue_name, key=key, ignore_not_found=True)
acked_count += 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Creating a new database.batch() context manager inside the loop commits each acknowledgement individually, resulting in multiple network roundtrips. Instead, you should wrap the loop inside a single database.batch() block to batch all acknowledgements into a single transaction/commit. Additionally, checking if valid_keys is not empty before entering the batch context prevents an unnecessary empty commit.

Suggested change
for key in valid_keys:
with database.batch() as batch:
batch.ack(queue=queue_name, key=key, ignore_not_found=True)
acked_count += 1
if valid_keys:
with database.batch() as batch:
for key in valid_keys:
batch.ack(queue=queue_name, key=key, ignore_not_found=True)
acked_count += 1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! This comment refers to an earlier revision of this file; the code here has since been reworked in later commits, so this thread no longer applies to the current diff.

…roject_id

Commits all valid message acknowledgements in a single batch Commit RPC and omits project identifiers from stdout and stderr.
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 Spanner API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants