Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
from google.cloud.spanner_admin_database_v1 import DatabaseDialect
from google.cloud.spanner_v1._async.database import Database
from google.cloud.spanner_v1.database import SPANNER_DATA_SCOPE
from google.cloud.spanner_v1.services.spanner.transports import (
SpannerGrpcTransport,
SpannerTransport,
)
from google.cloud.spanner_v1.services.spanner.transports import SpannerTransport

if CrossSync.is_async:
from google.cloud.spanner_v1._async.testing.interceptors import (
Expand All @@ -38,8 +35,14 @@
from google.cloud.spanner_v1.services.spanner.async_client import (
SpannerAsyncClient as SpannerClient,
)
from google.cloud.spanner_v1.services.spanner.transports.grpc_asyncio import (
SpannerGrpcAsyncIOTransport as SpannerGrpcTransport,
)
else:
from google.cloud.spanner_v1.services.spanner.client import SpannerClient
from google.cloud.spanner_v1.services.spanner.transports.grpc import (
SpannerGrpcTransport,
)
from google.cloud.spanner_v1.testing.interceptors import (
MethodAbortInterceptor,
MethodCountInterceptor,
Expand All @@ -49,6 +52,28 @@
__CROSS_SYNC_OUTPUT__ = "google.cloud.spanner_v1.testing.database_test"


if CrossSync.is_async:

def _attach_async_interceptors(channel, interceptors):
"""Attach multi-interface async interceptors to all matching gRPC channel lists.
grpc.aio.Channel.__init__ uses an if/elif chain on interceptor base classes,
so an interceptor implementing both UnaryUnaryClientInterceptor and
UnaryStreamClientInterceptor would otherwise only be attached to unary-unary.
"""
for interceptor in interceptors:
if isinstance(interceptor, grpc.aio.UnaryUnaryClientInterceptor):
channel._unary_unary_interceptors.append(interceptor)
if isinstance(interceptor, grpc.aio.UnaryStreamClientInterceptor):
channel._unary_stream_interceptors.append(interceptor)
if isinstance(interceptor, grpc.aio.StreamUnaryClientInterceptor):
channel._stream_unary_interceptors.append(interceptor)
if isinstance(interceptor, grpc.aio.StreamStreamClientInterceptor):
channel._stream_stream_interceptors.append(interceptor)
Comment thread
olavloite marked this conversation as resolved.
Resolved
Comment thread
olavloite marked this conversation as resolved.
Resolved
Comment thread
olavloite marked this conversation as resolved.
Resolved
Comment thread
olavloite marked this conversation as resolved.
Resolved

Comment thread
olavloite marked this conversation as resolved.
Resolved

# TODO: Remove TestDatabase and enable_interceptors_in_tests after migrating
# mockserver, system, and unit tests to direct test transports.
class TestDatabase(Database):
"""Representation of a Cloud Spanner Database. This class is only used for
system testing as there is no support for interceptors in grpc client
Expand All @@ -68,6 +93,7 @@ def __init__(
database_dialect=DatabaseDialect.DATABASE_DIALECT_UNSPECIFIED,
database_role=None,
enable_drop_protection=False,
proto_descriptors=None,
):
super().__init__(
database_id,
Expand All @@ -79,6 +105,7 @@ def __init__(
database_dialect,
database_role,
enable_drop_protection,
proto_descriptors,
)

self._method_count_interceptor = MethodCountInterceptor()
Expand All @@ -95,21 +122,19 @@ def spanner_api(self):
client = self._instance._client
client_info = client._client_info
client_options = client._client_options
if (
not hasattr(self, "_x_goog_request_id_interceptor")
or self._x_goog_request_id_interceptor is None
):
self._x_goog_request_id_interceptor = XGoogRequestIDHeaderInterceptor()
if self._x_goog_request_id_interceptor not in self._interceptors:
self._interceptors.append(self._x_goog_request_id_interceptor)
if self._instance.emulator_host is not None:
if CrossSync.is_async:
self._x_goog_request_id_interceptor = (
XGoogRequestIDHeaderInterceptor()
)
self._interceptors.append(self._x_goog_request_id_interceptor)
channel = grpc.aio.insecure_channel(
self._instance.emulator_host, interceptors=self._interceptors
)
channel = grpc.aio.insecure_channel(self._instance.emulator_host)
_attach_async_interceptors(channel, self._interceptors)
Comment thread
olavloite marked this conversation as resolved.
Resolved
else:
channel = grpc.insecure_channel(self._instance.emulator_host)
self._x_goog_request_id_interceptor = (
XGoogRequestIDHeaderInterceptor()
)
self._interceptors.append(self._x_goog_request_id_interceptor)
channel = grpc.intercept_channel(channel, *self._interceptors)

transport = SpannerGrpcTransport(channel=channel)
Expand All @@ -119,9 +144,6 @@ def spanner_api(self):
)
return self._spanner_api
if getattr(client, "instance_type", None) == "omni":
self._x_goog_request_id_interceptor = XGoogRequestIDHeaderInterceptor()
self._interceptors.append(self._x_goog_request_id_interceptor)

from google.cloud.spanner_v1._async._helpers import (
_create_spanner_omni_transport as _create_spanner_omni_transport_async,
)
Expand All @@ -137,9 +159,12 @@ def spanner_api(self):
client._ca_certificate,
client._client_certificate,
client._client_key,
self._interceptors,
None,
credentials=client.credentials,
)
_attach_async_interceptors(
transport.grpc_channel, self._interceptors
)
Comment thread
olavloite marked this conversation as resolved.
Resolved
else:
transport = _create_spanner_omni_transport_sync(
SpannerGrpcTransport,
Expand Down Expand Up @@ -181,8 +206,8 @@ def _create_spanner_client_for_tests(self, client_options, credentials):
default_scopes=SpannerTransport.AUTH_SCOPES,
scopes=client_options.scopes,
default_host=SpannerTransport.DEFAULT_HOST,
interceptors=self._interceptors,
)
_attach_async_interceptors(channel, self._interceptors)
Comment thread
olavloite marked this conversation as resolved.
Resolved
else:
channel = grpc_helpers.create_channel(
api_endpoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import inspect
import threading
from collections import defaultdict

Expand All @@ -20,37 +21,83 @@

from google.cloud.spanner_v1.request_id_header import parse_request_id

X_GOOG_REQUEST_ID = "x-goog-spanner-request-id"


def _normalize_method_name(method) -> str:
"""Decode gRPC method names from bytes to str when needed."""
if isinstance(method, bytes):
return method.decode("utf-8")
return method


def _extract_request_id(client_call_details) -> str:
"""Extract and validate the x-goog-spanner-request-id metadata header."""
metadata = client_call_details.metadata or ()
x_goog_request_id = None
for key, value in metadata:
decoded_key = key.decode("utf-8") if isinstance(key, bytes) else key
if decoded_key == X_GOOG_REQUEST_ID:
x_goog_request_id = (
value.decode("utf-8") if isinstance(value, bytes) else value
)
break

if not x_goog_request_id:
method = _normalize_method_name(client_call_details.method)
raise ValueError(f"Missing {X_GOOG_REQUEST_ID} header in {method}")

class MethodCountAsyncInterceptor(grpc.aio.UnaryUnaryClientInterceptor):
return x_goog_request_id


class MethodCountAsyncInterceptor(
grpc.aio.UnaryUnaryClientInterceptor,
grpc.aio.UnaryStreamClientInterceptor,
):
def __init__(self):
self._counts = defaultdict(int)

async def intercept_unary_unary(self, continuation, client_call_details, request):
self._counts[client_call_details.method] += 1
self._counts[_normalize_method_name(client_call_details.method)] += 1
return await continuation(client_call_details, request)

async def intercept_unary_stream(self, continuation, client_call_details, request):
self._counts[_normalize_method_name(client_call_details.method)] += 1
return await continuation(client_call_details, request)

def reset(self):
self._counts = defaultdict(int)


class MethodAbortAsyncInterceptor(grpc.aio.UnaryUnaryClientInterceptor):
class MethodAbortAsyncInterceptor(
grpc.aio.UnaryUnaryClientInterceptor,
grpc.aio.UnaryStreamClientInterceptor,
):
def __init__(self):
self._method_to_abort = None
self._count = 0
self._max_raise_count = 1
self._connection = None

async def intercept_unary_unary(self, continuation, client_call_details, request):
if (
self._count < self._max_raise_count
and client_call_details.method == self._method_to_abort
):
async def _maybe_raise_aborted(self, client_call_details):
method = _normalize_method_name(client_call_details.method)
if self._count < self._max_raise_count and method == self._method_to_abort:
self._count += 1
if self._connection is not None:
# Note: This assumes the connection rollback is sync or handled elsewhere
# For async connection, we might need a different approach if rollback is async
self._connection._transaction.rollback()
if (
self._connection is not None
and self._connection._transaction is not None
):
rollback_result = self._connection._transaction.rollback()
if inspect.isawaitable(rollback_result):
await rollback_result
Comment thread
olavloite marked this conversation as resolved.
Resolved
raise Aborted("Thrown from Async ClientInterceptor for testing")

async def intercept_unary_unary(self, continuation, client_call_details, request):
await self._maybe_raise_aborted(client_call_details)
return await continuation(client_call_details, request)

async def intercept_unary_stream(self, continuation, client_call_details, request):
await self._maybe_raise_aborted(client_call_details)
return await continuation(client_call_details, request)

def set_method_to_abort(self, method_to_abort, connection=None, max_raise_count=1):
Expand All @@ -62,36 +109,31 @@ def set_method_to_abort(self, method_to_abort, connection=None, max_raise_count=
def reset(self):
self._method_to_abort = None
self._count = 0
self._max_raise_count = 1
self._connection = None


X_GOOG_REQUEST_ID = "x-goog-spanner-request-id"


class XGoogRequestIDHeaderAsyncInterceptor(grpc.aio.UnaryUnaryClientInterceptor):
class XGoogRequestIDHeaderAsyncInterceptor(
grpc.aio.UnaryUnaryClientInterceptor,
grpc.aio.UnaryStreamClientInterceptor,
):
def __init__(self):
self._unary_req_segments = []
self._stream_req_segments = []
self.__lock = threading.Lock()

async def intercept_unary_unary(self, continuation, client_call_details, request):
metadata = client_call_details.metadata
x_goog_request_id = None
for key, value in metadata:
if key == X_GOOG_REQUEST_ID:
x_goog_request_id = value
break

if not x_goog_request_id:
raise Exception(
f"Missing {X_GOOG_REQUEST_ID} header in {client_call_details.method}"
)

def _record_request_id(self, client_call_details, target_segments):
x_goog_request_id = _extract_request_id(client_call_details)
method = _normalize_method_name(client_call_details.method)
with self.__lock:
self._unary_req_segments.append(
(client_call_details.method, parse_request_id(x_goog_request_id))
)
target_segments.append((method, parse_request_id(x_goog_request_id)))

async def intercept_unary_unary(self, continuation, client_call_details, request):
self._record_request_id(client_call_details, self._unary_req_segments)
return await continuation(client_call_details, request)

async def intercept_unary_stream(self, continuation, client_call_details, request):
self._record_request_id(client_call_details, self._stream_req_segments)
return await continuation(client_call_details, request)

@property
Expand All @@ -103,5 +145,6 @@ def stream_request_ids(self):
return self._stream_req_segments

def reset(self):
self._stream_req_segments.clear()
self._unary_req_segments.clear()
with self.__lock:
self._stream_req_segments.clear()
self._unary_req_segments.clear()
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
from google.cloud.spanner_admin_database_v1 import DatabaseDialect
from google.cloud.spanner_v1.database import SPANNER_DATA_SCOPE, Database
from google.cloud.spanner_v1.services.spanner.client import SpannerClient
from google.cloud.spanner_v1.services.spanner.transports import (
from google.cloud.spanner_v1.services.spanner.transports import SpannerTransport
from google.cloud.spanner_v1.services.spanner.transports.grpc import (
SpannerGrpcTransport,
SpannerTransport,
)
from google.cloud.spanner_v1.testing.interceptors import (
MethodAbortInterceptor,
Expand Down Expand Up @@ -52,6 +52,7 @@ def __init__(
database_dialect=DatabaseDialect.DATABASE_DIALECT_UNSPECIFIED,
database_role=None,
enable_drop_protection=False,
proto_descriptors=None,
):
super().__init__(
database_id,
Expand All @@ -63,6 +64,7 @@ def __init__(
database_dialect,
database_role,
enable_drop_protection,
proto_descriptors,
)
self._method_count_interceptor = MethodCountInterceptor()
self._method_abort_interceptor = MethodAbortInterceptor()
Expand All @@ -78,19 +80,22 @@ def spanner_api(self):
client = self._instance._client
client_info = client._client_info
client_options = client._client_options
if self._instance.emulator_host is not None:
channel = grpc.insecure_channel(self._instance.emulator_host)
if (
not hasattr(self, "_x_goog_request_id_interceptor")
or self._x_goog_request_id_interceptor is None
):
self._x_goog_request_id_interceptor = XGoogRequestIDHeaderInterceptor()
if self._x_goog_request_id_interceptor not in self._interceptors:
self._interceptors.append(self._x_goog_request_id_interceptor)
if self._instance.emulator_host is not None:
channel = grpc.insecure_channel(self._instance.emulator_host)
channel = grpc.intercept_channel(channel, *self._interceptors)
transport = SpannerGrpcTransport(channel=channel)
self._spanner_api = SpannerClient(
client_info=client_info, transport=transport
)
return self._spanner_api
if getattr(client, "instance_type", None) == "omni":
self._x_goog_request_id_interceptor = XGoogRequestIDHeaderInterceptor()
self._interceptors.append(self._x_goog_request_id_interceptor)
from google.cloud.spanner_v1._helpers import (
_create_spanner_omni_transport as _create_spanner_omni_transport_sync,
)
Expand Down
Loading
Loading