GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(experimental): add write resumption strategy by Pulkit0110 · Pull Request #1663 · googleapis/python-storage

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

feat(experimental): add write resumption strategy #1663

Merged
Pulkit0110 merged 5 commits into googleapis:main from Pulkit0110:bidi-writes-retry
Jan 2, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

feat(experimental): add write resumption strategy #1663

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
5 commits
Select commit Hold shift + click to select a range
cc7fb6f
integrate retry logic with the MRD
Pulkit0110 Dec 8, 2025
f1f8d65
feat(experimental): add write resumption strategy
Pulkit0110 Dec 15, 2025
9a086e4
address gemini bot comments
Pulkit0110 Dec 29, 2025
7abfae0
addressing comments:
Pulkit0110 Dec 30, 2025
a3f6254
resolve comments
Pulkit0110 Jan 2, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

146 changes: 146 additions & 0 deletions 146 google/cloud/storage/_experimental/asyncio/retry/writes_resumption_strategy.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Dict, IO, Iterable, Optional, Union

import google_crc32c
from google.cloud._storage_v2.types import storage as storage_type
from google.cloud._storage_v2.types.storage import BidiWriteObjectRedirectedError
from google.cloud.storage._experimental.asyncio.retry.base_strategy import (
_BaseResumptionStrategy,
)


class _WriteState:
"""A helper class to track the state of a single upload operation.
:type spec: :class:`google.cloud.storage_v2.types.AppendObjectSpec`
:param spec: The specification for the object to write.
:type chunk_size: int
:param chunk_size: The size of chunks to write to the server.
:type user_buffer: IO[bytes]
:param user_buffer: The data source.
"""

def __init__(
self,
spec: Union[storage_type.AppendObjectSpec, storage_type.WriteObjectSpec],
chunk_size: int,
user_buffer: IO[bytes],
):
self.spec = spec
self.chunk_size = chunk_size
self.user_buffer = user_buffer
self.persisted_size: int = 0
self.bytes_sent: int = 0
self.write_handle: Union[bytes, storage_type.BidiWriteHandle, None] = None
self.routing_token: Optional[str] = None
self.is_finalized: bool = False


class _WriteResumptionStrategy(_BaseResumptionStrategy):
"""The concrete resumption strategy for bidi writes."""

def generate_requests(
self, state: Dict[str, Any]
) -> Iterable[storage_type.BidiWriteObjectRequest]:
"""Generates BidiWriteObjectRequests to resume or continue the upload.
For Appendable Objects, every stream opening should send an
AppendObjectSpec. If resuming, the `write_handle` is added to that spec.
This method is not applicable for `open` methods.
"""
Comment thread
Pulkit0110 marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

write_state: _WriteState = state["write_state"]

initial_request = storage_type.BidiWriteObjectRequest()

# Determine if we need to send WriteObjectSpec or AppendObjectSpec
Comment thread
Pulkit0110 marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

if isinstance(write_state.spec, storage_type.WriteObjectSpec):
initial_request.write_object_spec = write_state.spec
else:
if write_state.write_handle:
write_state.spec.write_handle = write_state.write_handle

if write_state.routing_token:
write_state.spec.routing_token = write_state.routing_token
initial_request.append_object_spec = write_state.spec

yield initial_request
Comment thread
Pulkit0110 marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


# The buffer should already be seeked to the correct position (persisted_size)
# by the `recover_state_on_failure` method before this is called.
while not write_state.is_finalized:
chunk = write_state.user_buffer.read(write_state.chunk_size)

# End of File detection
if not chunk:
return

checksummed_data = storage_type.ChecksummedData(content=chunk)
checksum = google_crc32c.Checksum(chunk)
checksummed_data.crc32c = int.from_bytes(checksum.digest(), "big")

request = storage_type.BidiWriteObjectRequest(
write_offset=write_state.bytes_sent,
checksummed_data=checksummed_data,
)
write_state.bytes_sent += len(chunk)

yield request

def update_state_from_response(
self, response: storage_type.BidiWriteObjectResponse, state: Dict[str, Any]
) -> None:
"""Processes a server response and updates the write state."""
write_state: _WriteState = state["write_state"]

if response.persisted_size:
write_state.persisted_size = response.persisted_size

if response.write_handle:
write_state.write_handle = response.write_handle

if response.resource:
write_state.persisted_size = response.resource.size
if response.resource.finalize_time:
write_state.is_finalized = True

async def recover_state_on_failure(
Comment thread
Pulkit0110 marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

self, error: Exception, state: Dict[str, Any]
) -> None:
"""
Handles errors, specifically BidiWriteObjectRedirectedError, and rewinds state.
This method rewinds the user buffer and internal byte tracking to the
last confirmed 'persisted_size' from the server.
"""
write_state: _WriteState = state["write_state"]
cause = getattr(error, "cause", error)

# Extract routing token and potentially a new write handle for redirection.
if isinstance(cause, BidiWriteObjectRedirectedError):
if cause.routing_token:
write_state.routing_token = cause.routing_token

redirect_handle = getattr(cause, "write_handle", None)
if redirect_handle:
Comment thread
chandra-siri marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

write_state.write_handle = redirect_handle

# We must assume any data sent beyond 'persisted_size' was lost.
Comment thread
chandra-siri marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

# Reset the user buffer to the last known good byte confirmed by the server.
write_state.user_buffer.seek(write_state.persisted_size)
write_state.bytes_sent = write_state.persisted_size
16 changes: 12 additions & 4 deletions 16 tests/unit/asyncio/retry/test_reads_resumption_strategy.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
from google.cloud._storage_v2.types.storage import BidiReadObjectRedirectedError

_READ_ID = 1
LOGGER_NAME = "google.cloud.storage._experimental.asyncio.retry.reads_resumption_strategy"
LOGGER_NAME = (
"google.cloud.storage._experimental.asyncio.retry.reads_resumption_strategy"
)


class TestDownloadState(unittest.TestCase):
Expand Down Expand Up @@ -309,7 +311,9 @@ def test_update_state_missing_read_range_logs_warning(self):
with self.assertLogs(LOGGER_NAME, level="WARNING") as cm:
self.strategy.update_state_from_response(response, self.state)

self.assertTrue(any("missing read_range field" in output for output in cm.output))
self.assertTrue(
any("missing read_range field" in output for output in cm.output)
)

def test_update_state_unknown_id_logs_warning(self):
"""Verify we log a warning and continue when read_id is unknown."""
Expand All @@ -320,8 +324,12 @@ def test_update_state_unknown_id_logs_warning(self):
with self.assertLogs(LOGGER_NAME, level="WARNING") as cm:
self.strategy.update_state_from_response(response, self.state)

self.assertTrue(any(f"unknown or stale read_id {unknown_id}" in output for output in cm.output))

self.assertTrue(
any(
f"unknown or stale read_id {unknown_id}" in output
for output in cm.output
)
)

# --- Recovery Tests ---

Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.