Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 3f1bf52

Browse files
committed
Officially inject the first set of x-goog-spanner-request-id values into header metadata
1 parent 8024fca commit 3f1bf52

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

google/cloud/spanner_v1/_helpers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from google.cloud.spanner_v1 import TypeCode
3232
from google.cloud.spanner_v1 import ExecuteSqlRequest
3333
from google.cloud.spanner_v1 import JsonObject
34+
from google.cloud.spanner_v1.request_id_header import with_request_id
3435

3536
# Validation error messages
3637
NUMERIC_MAX_SCALE_ERR_MSG = (
@@ -564,3 +565,7 @@ def __radd__(self, n):
564565
Defines the result of invoking: value = addable + AtomicCounter
565566
"""
566567
return self.__add__(n)
568+
569+
570+
def _metadata_with_request_id(*args, **kwargs):
571+
return with_request_id(*args, **kwargs)

google/cloud/spanner_v1/client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import grpc
2727
import os
2828
import warnings
29-
import threading
3029

3130
from google.api_core.gapic_v1 import client_info
3231
from google.auth.credentials import AnonymousCredentials

google/cloud/spanner_v1/database.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
AtomicCounter,
5454
_metadata_with_prefix,
5555
_metadata_with_leader_aware_routing,
56+
_metadata_with_request_id,
5657
)
5758
from google.cloud.spanner_v1.batch import Batch
5859
from google.cloud.spanner_v1.batch import MutationGroups
@@ -698,17 +699,18 @@ def execute_partitioned_dml(
698699
_metadata_with_leader_aware_routing(self._route_to_leader_enabled)
699700
)
700701

701-
nth_request = self._next_nth_request()
702+
nth_request = getattr(self, "_next_nth_request", 0)
702703
attempt = AtomicCounter(1) # It'll be incremented inside _restart_on_unavailable
703704

704705
def execute_pdml():
705706
with SessionCheckout(self._pool) as session:
706-
channel_id = session._channel_id
707-
metadata = with_request_id(
708-
self._client._nth_client_id, nth_request, attempt.value, metadata
707+
channel_id = getattr(session, "_channel_id", 0)
708+
client_id = getattr(self, "_nth_client_id", 0)
709+
all_metadata = _metadata_with_request_id(
710+
client_id, channel_id, nth_request, attempt.value, metadata
709711
)
710712
txn = api.begin_transaction(
711-
session=session.name, options=txn_options, metadata=metadata
713+
session=session.name, options=txn_options, metadata=all_metadata
712714
)
713715

714716
txn_selector = TransactionSelector(id=txn.id)
@@ -741,9 +743,14 @@ def execute_pdml():
741743

742744
return _retry_on_aborted(execute_pdml, DEFAULT_RETRY_BACKOFF)()
743745

746+
@property
744747
def _next_nth_request(self):
745748
return self._instance._client._next_nth_request
746749

750+
@property
751+
def _nth_client_id(self):
752+
return self._instance._client._nth_client_id
753+
747754
def session(self, labels=None, database_role=None):
748755
"""Factory to create a session for this database.
749756

google/cloud/spanner_v1/pool.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(self, labels=None, database_role=None):
5353
labels = {}
5454
self._labels = labels
5555
self._database_role = database_role
56-
self.__lock = threading.lock()
56+
self.__lock = threading.Lock()
5757
self._session_id_to_channel_id = dict()
5858

5959
@property
@@ -134,10 +134,12 @@ def _new_session(self):
134134
labels=self.labels, database_role=self.database_role
135135
)
136136

137-
with self.__lock:
138-
channel_id = len(self._session_id_to_channel_id) + 1
139-
self._session_id_to_channel_id[session._session.id] = channel_id
140-
session._channel_id = channel_id
137+
session_id = getattr(session, "_session_id", None)
138+
if session_id:
139+
with self.__lock:
140+
channel_id = len(self._session_id_to_channel_id) + 1
141+
self._session_id_to_channel_id[session._session_id] = channel_id
142+
session._channel_id = channel_id
141143

142144
return session
143145

google/cloud/spanner_v1/request_id_header.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def generate_rand_uint64():
3636
REQ_RAND_PROCESS_ID = generate_rand_uint64()
3737

3838

39-
def with_request_id(client_id, nth_request, attempt, other_metadata=[]):
39+
def with_request_id(client_id, channel_id, nth_request, attempt, other_metadata=[]):
4040
req_id = f"{REQ_ID_VERSION}.{REQ_RAND_PROCESS_ID}.{client_id}.{channel_id}.{nth_request}.{attempt}"
4141
other_metadata.append((REQ_ID_HEADER_KEY, req_id))
4242
return other_metadata

0 commit comments

Comments
 (0)