feat: implement AsyncMultiRangeDownloader with multiplexed bidi-gRPC stream support#16528
feat: implement AsyncMultiRangeDownloader with multiplexed bidi-gRPC stream support#16528zhixiangli wants to merge 3 commits intogoogleapis:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a _StreamMultiplexer to handle concurrent download tasks over a single bidirectional gRPC stream, replacing the previous locking mechanism in AsyncMultiRangeDownloader. The multiplexer routes responses to per-task asyncio queues based on read_id, allowing for better resource utilization. The lock parameter in download_ranges is now deprecated. Feedback focuses on improving the multiplexer's reliability and performance, specifically by using asyncio.gather to prevent head-of-line blocking during response broadcasting, ensuring the background receive loop terminates when no tasks are active, and adding error logging for observability.
| for queue in queues_to_notify: | ||
| await queue.put(response) |
There was a problem hiding this comment.
Broadcasting responses to multiple queues sequentially using await queue.put(response) can lead to head-of-line blocking. If one consumer is slow or its queue is full, it will delay delivery to all other concurrent tasks. Using asyncio.gather allows all put operations to be initiated concurrently, improving throughput and reducing the impact of a single slow consumer.
Furthermore, if a task is cancelled, its queue will no longer be drained. If the _recv_loop is blocked on await queue.put for that specific queue, the entire multiplexer will hang until the stream is reopened or closed. This sequential broadcast pattern is also used on lines 112-113 and 126-127 and should be updated there as well.
| for queue in queues_to_notify: | |
| await queue.put(response) | |
| if queues_to_notify: | |
| await asyncio.gather(*[queue.put(response) for queue in queues_to_notify]) |
packages/google-cloud-storage/google/cloud/storage/asyncio/_stream_multiplexer.py
Show resolved
Hide resolved
packages/google-cloud-storage/google/cloud/storage/asyncio/_stream_multiplexer.py
Show resolved
Hide resolved
13d5d08 to
774d691
Compare
acfab40 to
aed8682
Compare
This PR implements
AsyncMultiRangeDownloaderwith a new_StreamMultiplexer, enabling multiple concurrent range downloads to share a single bidirectional gRPC stream.Before vs. After
How it works
The system uses a background
_StreamMultiplexerto manage the shared bidirectional stream:BidiReadObjectRequest) directly to the shared stream.read_idin each response to route data to the correct task-specificasyncio.Queue._StreamErrorand automatically retry using the new stream generation.Key Changes:
_StreamMultiplexer: Background receiver loop for routing responses.AsyncMultiRangeDownloaderIntegration: Full support for concurrentdownload_rangescalls.