Skip to content

feat(google-cloud-ndb): add x-goog-is-transaction routing header - #18847

Open
nikhilchowdary wants to merge 1 commit into
googleapis:mainfrom
nikhilchowdary:feat/ndb-is-transaction-header
Open

nikhilchowdary wants to merge 1 commit into
googleapis:mainfrom
nikhilchowdary:feat/ndb-is-transaction-header

Conversation

@nikhilchowdary

@nikhilchowdary nikhilchowdary commented Oct 25, 2026 •

Copy link
Copy Markdown

Add x-goog-is-transaction routing header in Python ndb SDK to differentiate transactional requests and non-transactional requests from each other. Having a routing header for transactions helps in routing grpc requests at GFE.

This commit attaches the x-goog-is-transaction routing header to all RPCs

Set to true for transactional RPCs
-- beginTransaction
-- rollback
-- transactional commit
-- transactional lookup
-- transactional runQuery
-- transactional runAggregationQuery.

Set to false for non transactional RPCs
-- allocateIds
-- reserveIds
-- non transactional commit
-- non transactional lookup
-- non transactional runQuery
-- non transactional runAggregationQuery.

cc: @MarkDuckworth

@nikhilchowdary
nikhilchowdary requested a review from a team as a code owner October 25, 2026 04:41

@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 adds transaction detection to Datastore RPC requests by appending an x-goog-is-transaction metadata header. It introduces helper functions to determine if a request is transactional and updates various Datastore API calls and unit tests accordingly. The review feedback suggests optimizing the metadata generation in _add_routing_info by using itertools.chain to avoid copying overhead from multiple tuple concatenations.

Comment on lines +1191 to +1202
result = tuple(metadata)
if header_params:
return tuple(metadata) + (
result = result + (
gapic_v1.routing_header.to_grpc_metadata(header_params),
)

return tuple(metadata)
if is_transaction is None:
is_transaction = _is_transaction_request(request)

return result + (
("x-goog-is-transaction", "true" if is_transaction else "false"),
)

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.

medium

In performance-critical paths like Datastore RPC metadata generation, multiple tuple concatenations (e.g., result = result + (...)) introduce unnecessary copying overhead. Using itertools.chain to combine the metadata collections and converting to a tuple once at the end is much more efficient.

Suggested change
result = tuple(metadata)
if header_params:
return tuple(metadata) + (
result = result + (
gapic_v1.routing_header.to_grpc_metadata(header_params),
)
return tuple(metadata)
if is_transaction is None:
is_transaction = _is_transaction_request(request)
return result + (
("x-goog-is-transaction", "true" if is_transaction else "false"),
)
additional = []
if header_params:
additional.append(
gapic_v1.routing_header.to_grpc_metadata(header_params)
)
if is_transaction is None:
is_transaction = _is_transaction_request(request)
additional.append(
("x-goog-is-transaction", "true" if is_transaction else "false")
)
import itertools
return tuple(itertools.chain(metadata, additional))
References
  1. In performance-critical paths, avoid combining collections by converting them to tuples and concatenating them (e.g., tuple(a) + tuple(b)), as this introduces copying overhead. Instead, use lazy iterators like itertools.chain to iterate over multiple collections efficiently.

Attach the `x-goog-is-transaction` routing metadata header in `_add_routing_info`:
- Set to `true` for transactional RPCs (`begin_transaction`, `rollback`, and transactional `commit`, `lookup`, `run_query`, `run_aggregation_query`).
- Set to `false` for non-transactional RPCs (`allocate_ids`, `reserve_ids`, and non-transactional `commit`, `lookup`, `run_query`, `run_aggregation_query`).
@nikhilchowdary
nikhilchowdary force-pushed the feat/ndb-is-transaction-header branch from eac13bf to 42e0414 Compare October 25, 2026 06:04

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant