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 @@ -25,6 +25,24 @@

_DEFAULT_HOST = "storage.googleapis.com"

# Detect a dead connection while an RPC or bidi stream is open. Without HTTP/2
# keepalive, a pending recv() on a bidi stream can block indefinitely when the
# path to the server silently drops traffic or the peer stops responding while
# its kernel still ACKs TCP keepalives. While an RPC is open, send a ping after
# 30 s without activity and drop the connection if it is not ACKed within 20 s,
# so the stream fails with a retryable error instead of hanging. On a stream
# with no outgoing data, gRPC limits these pings to one per minute.
# grpc.http2.ping_timeout_ms is set alongside keepalive_timeout_ms because
# grpc-python otherwise waits its 60 s default for the ping ACK.
# keepalive_permit_without_calls is intentionally left unset so channels with no
# open RPC send no pings (more frequent or idle pings can trigger GOAWAY
# too_many_pings from GCS frontends).
_KEEPALIVE_CHANNEL_OPTIONS: tuple[tuple[str, int], ...] = (
("grpc.keepalive_time_ms", 30000),
("grpc.keepalive_timeout_ms", 20000),
("grpc.http2.ping_timeout_ms", 20000),
)


def _validate_metadata(metadata):
"""Validates that metadata is a sequence of (key, value) pairs."""
Expand Down Expand Up @@ -133,12 +151,16 @@ def _create_async_grpc_client(
host = getattr(client_options, "api_endpoint", None) or _DEFAULT_HOST
quota_project_id = getattr(client_options, "quota_project_id", None)

channel_options = (
("grpc.primary_user_agent", primary_user_agent),
) + _KEEPALIVE_CHANNEL_OPTIONS

channel = transport_cls.create_channel(
host=host,
quota_project_id=quota_project_id,
attempt_direct_path=attempt_direct_path,
credentials=credentials,
options=(("grpc.primary_user_agent", primary_user_agent),),
options=channel_options,
)
transport = transport_cls(channel=channel)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,28 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import asyncio
from unittest import mock

import grpc
import pytest
from google.api_core import client_info as client_info_lib
from google.api_core import client_options
from google.api_core import client_options, exceptions
from google.auth import credentials as auth_credentials
from google.auth.credentials import AnonymousCredentials

from google.cloud import _storage_v2
from google.cloud.storage import __version__
from google.cloud.storage.asyncio import async_grpc_client
from google.cloud.storage.asyncio.async_read_object_stream import (
_AsyncReadObjectStream,
)

_EXPECTED_KEEPALIVE_OPTIONS = (
("grpc.keepalive_time_ms", 30000),
("grpc.keepalive_timeout_ms", 20000),
("grpc.http2.ping_timeout_ms", 20000),
)


def _make_credentials(spec=None):
Expand Down Expand Up @@ -50,7 +62,9 @@ def test_constructor_default_options(self, mock_async_storage_client):
agent_version = f"gcloud-python/{__version__}"
assert agent_version in client_info.user_agent
primary_user_agent = client_info.to_user_agent()
expected_options = (("grpc.primary_user_agent", primary_user_agent),)
expected_options = (
("grpc.primary_user_agent", primary_user_agent),
) + _EXPECTED_KEEPALIVE_OPTIONS

mock_transport_cls.create_channel.assert_called_once_with(
host="storage.googleapis.com",
Expand Down Expand Up @@ -81,7 +95,9 @@ def test_constructor_with_client_info(self, mock_async_storage_client):
agent_version = f"gcloud-python/{__version__}"
assert agent_version in client_info.user_agent
primary_user_agent = client_info.to_user_agent()
expected_options = (("grpc.primary_user_agent", primary_user_agent),)
expected_options = (
("grpc.primary_user_agent", primary_user_agent),
) + _EXPECTED_KEEPALIVE_OPTIONS

mock_transport_cls.create_channel.assert_called_once_with(
host="storage.googleapis.com",
Expand Down Expand Up @@ -112,7 +128,9 @@ def test_constructor_with_quota_project_and_endpoint(
kwargs = mock_async_storage_client.call_args.kwargs
client_info = kwargs["client_info"]
primary_user_agent = client_info.to_user_agent()
expected_options = (("grpc.primary_user_agent", primary_user_agent),)
expected_options = (
("grpc.primary_user_agent", primary_user_agent),
) + _EXPECTED_KEEPALIVE_OPTIONS

mock_transport_cls.create_channel.assert_called_once_with(
host="custom-endpoint.com",
Expand All @@ -137,7 +155,9 @@ def test_constructor_disables_directpath(self, mock_async_storage_client):
agent_version = f"gcloud-python/{__version__}"
assert agent_version in client_info.user_agent
primary_user_agent = client_info.to_user_agent()
expected_options = (("grpc.primary_user_agent", primary_user_agent),)
expected_options = (
("grpc.primary_user_agent", primary_user_agent),
) + _EXPECTED_KEEPALIVE_OPTIONS

mock_transport_cls.create_channel.assert_called_once_with(
host="storage.googleapis.com",
Expand Down Expand Up @@ -181,7 +201,9 @@ def test_grpc_client_property(self, mock_grpc_gapic_client):
assert "test-user-agent" in client_info.user_agent

primary_user_agent = client_info.to_user_agent()
expected_options = (("grpc.primary_user_agent", primary_user_agent),)
expected_options = (
("grpc.primary_user_agent", primary_user_agent),
) + _EXPECTED_KEEPALIVE_OPTIONS

mock_transport_cls.create_channel.assert_called_once_with(
host="storage.googleapis.com",
Expand Down Expand Up @@ -431,3 +453,128 @@ async def test_get_object_invalid_metadata(self, invalid_metadata, expected_exc)
client = async_grpc_client.AsyncGrpcClient(credentials=_make_credentials())
with pytest.raises(expected_exc):
await client.get_object("bucket", "object", metadata=invalid_metadata)

@mock.patch("google.cloud._storage_v2.StorageAsyncClient")
def test_channel_options_include_keepalive(
self,
mock_async_storage_client,
):
mock_transport_cls = mock.MagicMock()
mock_async_storage_client.get_transport_class.return_value = mock_transport_cls
mock_creds = _make_credentials()

async_grpc_client.AsyncGrpcClient(credentials=mock_creds)

raw_options = mock_transport_cls.create_channel.call_args.kwargs["options"]
# gRPC uses the first occurrence of a duplicated key; dict() keeps the last.
assert len({key for key, _ in raw_options}) == len(raw_options)
options = dict(raw_options)
assert options["grpc.keepalive_time_ms"] == 30000
assert options["grpc.keepalive_timeout_ms"] == 20000
assert options["grpc.http2.ping_timeout_ms"] == 20000
assert "grpc.keepalive_permit_without_calls" not in options

@pytest.mark.asyncio
async def test_keepalive_fails_blackholed_bidi_stream(self):
"""A real gRPC channel fails a silently dropped bidi stream instead of hanging.

gRPC ignores unknown channel option keys, so this catches misspelled
keepalive options that a mocked ``create_channel`` cannot.
"""

async def bidi_read(request_iterator, context):
await request_iterator.__anext__()
yield _storage_v2.BidiReadObjectResponse(
metadata=_storage_v2.Object(
bucket="projects/_/buckets/b", name="o", generation=1
)
)
await asyncio.Event().wait() # Keep the stream open.

server = grpc.aio.server()
server.add_generic_rpc_handlers(
[
grpc.method_handlers_generic_handler(
"google.storage.v2.Storage",
{
"BidiReadObject": grpc.stream_stream_rpc_method_handler(
bidi_read,
request_deserializer=_storage_v2.BidiReadObjectRequest.deserialize,
response_serializer=_storage_v2.BidiReadObjectResponse.serialize,
)
},
)
]
)
backend_port = server.add_insecure_port("127.0.0.1:0")
await server.start()

# TCP relay that keeps both sockets open but drops all bytes once
# `blackhole` is set, like a path that silently stops forwarding.
blackhole = asyncio.Event()
writers = []

async def pipe(reader, writer):
try:
while data := await reader.read(65536):
if not blackhole.is_set():
writer.write(data)
await writer.drain()
except OSError:
pass

async def on_connect(client_reader, client_writer):
backend_reader, backend_writer = await asyncio.open_connection(
"127.0.0.1", backend_port
)
writers.extend([client_writer, backend_writer])
await asyncio.gather(
pipe(client_reader, backend_writer),
pipe(backend_reader, client_writer),
)

relay = await asyncio.start_server(on_connect, "127.0.0.1", 0)
relay_port = relay.sockets[0].getsockname()[1]
fast_keepalive = (
("grpc.keepalive_time_ms", 100),
("grpc.keepalive_timeout_ms", 100),
("grpc.http2.ping_timeout_ms", 100),
)

def insecure_channel(target, credentials=None, **kwargs):
return grpc.aio.insecure_channel(target, options=kwargs.get("options"))

try:
with (
mock.patch.object(
async_grpc_client, "_KEEPALIVE_CHANNEL_OPTIONS", fast_keepalive
),
mock.patch(
"google.api_core.grpc_helpers_async.create_channel",
side_effect=insecure_channel,
),
):
client = async_grpc_client.AsyncGrpcClient(
credentials=_make_credentials(),
client_options=client_options.ClientOptions(
api_endpoint=f"127.0.0.1:{relay_port}"
),
attempt_direct_path=False,
)
try:
stream = _AsyncReadObjectStream(
client=client.grpc_client, bucket_name="b", object_name="o"
)
await stream.open()
blackhole.set()
# Usually fails in ~0.1 s. If a ping was ACKed just before the
# blackhole, gRPC waits 1 s before the next ping, so allow ~5x.
with pytest.raises(exceptions.ServiceUnavailable):
await asyncio.wait_for(stream.recv(), timeout=5)
finally:
await client.grpc_client.transport.close()
finally:
relay.close()
for writer in writers:
writer.close()
await server.stop(0)
Loading