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

Commit 78097e3

Browse files
committed
Officially inject the first set of x-goog-spanner-request-id values into header metadata
1 parent 4f1da67 commit 78097e3

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 = (
@@ -467,3 +468,7 @@ def value(self):
467468

468469
def increment(self, value=1):
469470
return self.__iadd__(value)
471+
472+
473+
def _metadata_with_request_id(*args, **kwargs):
474+
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
AtomicInt,
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
@@ -694,17 +695,18 @@ def execute_partitioned_dml(
694695
_metadata_with_leader_aware_routing(self._route_to_leader_enabled)
695696
)
696697

697-
nth_request = self._next_nth_request()
698+
nth_request = getattr(self, "_next_nth_request", 0)
698699
attempt = AtomicInt(1) # It'll be incremented inside _restart_on_unavailable
699700

700701
def execute_pdml():
701702
with SessionCheckout(self._pool) as session:
702-
channel_id = session._channel_id
703-
metadata = with_request_id(
704-
self._client._nth_client_id, nth_request, attempt.value, metadata
703+
channel_id = getattr(session, "_channel_id", 0)
704+
client_id = getattr(self, "_nth_client_id", 0)
705+
all_metadata = _metadata_with_request_id(
706+
client_id, channel_id, nth_request, attempt.value, metadata
705707
)
706708
txn = api.begin_transaction(
707-
session=session.name, options=txn_options, metadata=metadata
709+
session=session.name, options=txn_options, metadata=all_metadata
708710
)
709711

710712
txn_selector = TransactionSelector(id=txn.id)
@@ -737,9 +739,14 @@ def execute_pdml():
737739

738740
return _retry_on_aborted(execute_pdml, DEFAULT_RETRY_BACKOFF)()
739741

742+
@property
740743
def _next_nth_request(self):
741744
return self._instance._client._next_nth_request
742745

746+
@property
747+
def _nth_client_id(self):
748+
return self._instance._client._nth_client_id
749+
743750
def session(self, labels=None, database_role=None):
744751
"""Factory to create a session for this database.
745752

google/cloud/spanner_v1/pool.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def __init__(self, labels=None, database_role=None):
4848
labels = {}
4949
self._labels = labels
5050
self._database_role = database_role
51-
self.__lock = threading.lock()
51+
self.__lock = threading.Lock()
5252
self._session_id_to_channel_id = dict()
5353

5454
@property
@@ -129,10 +129,12 @@ def _new_session(self):
129129
labels=self.labels, database_role=self.database_role
130130
)
131131

132-
with self.__lock:
133-
channel_id = len(self._session_id_to_channel_id) + 1
134-
self._session_id_to_channel_id[session._session.id] = channel_id
135-
session._channel_id = channel_id
132+
session_id = getattr(session, "_session_id", None)
133+
if session_id:
134+
with self.__lock:
135+
channel_id = len(self._session_id_to_channel_id) + 1
136+
self._session_id_to_channel_id[session._session_id] = channel_id
137+
session._channel_id = channel_id
136138

137139
return session
138140

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)