Skip to content

fix(storage): seek relative to initial_offset when retrying multi-call append - #19092

Open
Yonghui-Lee wants to merge 4 commits into
googleapis:mainfrom
Yonghui-Lee:fix/storage-aaow-multi-append-offset
Open

Yonghui-Lee wants to merge 4 commits into
googleapis:mainfrom
Yonghui-Lee:fix/storage-aaow-multi-append-offset

Conversation

@Yonghui-Lee

Copy link
Copy Markdown

Summary

Fixes silent data truncation and shifted-byte corruption in AsyncAppendableObjectWriter.append() (and append_from_file()) when a transient gRPC error or redirect triggers stream recovery on a second or subsequent append() call.

AsyncAppendableObjectWriter.append(data) wraps only the current call's payload in io.BytesIO(data), whereas persisted_size tracks the total persisted size of the object across the stream. Previously, recovery sought user_buffer to persisted_size instead of persisted_size - initial_offset, seeking past EOF and dropping unpersisted bytes when persisted_size >= len(data) or skipping initial_offset bytes inside data when 0 < initial_offset <= persisted_size < len(data).

Changes

  • Add initial_offset (defaulting to 0) to _WriteState so each append() call records the object byte offset corresponding to position 0 of user_buffer and initializes bytes_sent at initial_offset.

  • Add _WriteState.reset_to_persisted() to rewind user_buffer to persisted_offset - initial_offset (clamped at 0 before stream reopen refreshes persisted_size) and reset bytes_sent and bytes_since_last_flush in one place.

  • Update both _WriteResumptionStrategy.recover_state_on_failure() and AsyncAppendableObjectWriter.append() stream reopen handling to call write_state.reset_to_persisted() instead of duplicating raw seek(persisted_size) logic.

  • Pass initial_offset=self.offset or 0 when constructing _WriteState in AsyncAppendableObjectWriter.append() and reuse a single _WriteResumptionStrategy instance across request generation and retry management.

  • Raise google.api_core.exceptions.DataLoss after stream reopen if the server's refreshed persisted_size is smaller than initial_offset, failing fast when unpersisted bytes from an earlier append() call were lost on the server.

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea

  • Ensure the tests and linter pass

  • Code coverage does not decrease (if any source code was changed)

  • Appropriate docs were updated (if necessary)

Fixes #19091 🦕

…l append

`AsyncAppendableObjectWriter.append(data)` wraps only the current call's
payload in `io.BytesIO(data)` starting at `initial_offset = self.offset or 0`,
while `persisted_size` tracks the global object size persisted on the server.

When a stream retry or redirect occurred on a second or later `append()` call,
both `_WriteResumptionStrategy.recover_state_on_failure` and the stream
reopen handler sought `user_buffer` to `persisted_size` instead of
`persisted_size - initial_offset`. That caused silent truncation when
`persisted_size >= len(data)` and shifted-byte corruption when
`0 < initial_offset <= persisted_size < len(data)`.

Track `initial_offset` on `_WriteState`, centralize buffer and counter
rewinding in `_WriteState.reset_to_persisted()`, and raise
`google.api_core.exceptions.DataLoss` if reconnecting reveals that unpersisted
bytes from an earlier `append()` call were lost on the server.
@Yonghui-Lee
Yonghui-Lee requested review from a team as code owners November 16, 2026 03:48

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces tracking of an initial_offset in _WriteState to correctly handle retries and buffer rewinding during resume operations in AsyncAppendableObjectWriter. It adds a reset_to_persisted() helper to safely rewind the user buffer relative to the initial offset, raises a DataLoss exception if unpersisted bytes from a prior append are lost during a reconnect, and updates associated unit tests to verify these behaviors. I have no feedback to provide.

@zhixiangli zhixiangli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related, existing behavior: close() and finalize() raise InternalServerError("Unrecoverable data loss during reconnect…") on an offset mismatch (L811, L904). _is_write_retryable treats that error as retryable, so they retry until the deadline.

On a real zonal bucket, I appended 18 MiB (2 MiB of it unflushed) and then injected a reset during close(). With the default retry policy, close() reopened the stream for 119 s before raising RetryError, and the object was left at 16 MiB. With a 12 s policy, it made 14 reopens.

Since this PR raises DataLoss for the same condition in append(), raising DataLoss here too would fail fast and keep the two consistent. The tail replay from the first comment would let these paths recover.

…match

Address review feedback on the multi-call append retry fix.

- close() and finalize() raised InternalServerError when a reconnect
  reported fewer bytes than were sent. _is_write_retryable treats that
  as retryable, so they reopened the stream until the retry deadline.
  Raise DataLoss instead, matching append().
- Correct the append() docstring. Bytes after the last flush_interval
  boundary are only durable after flush() or close().
- Add an unaligned multi-call append retry case (5 MiB + 5 MiB with a
  4 MiB flush interval) and assert persisted_size and
  bytes_appended_since_last_flush. Add recovery boundary cases to the
  resumption strategy tests.
@Yonghui-Lee

Copy link
Copy Markdown
Author

@zhixiangli re the close() and finalize() offset mismatch: done in f88de78. Both now raise DataLoss instead of the retryable InternalServerError, so they fail fast instead of reopening the stream until the deadline. Recovery for these paths will come with the tail replay follow-up (#19101).

…riter

close() returns persisted_size, which is only refreshed by flush
responses. Closing persists bytes appended after the last flush, but
the returned value can be up to flush_interval bytes smaller than the
object. Document this, and point to flush() before close() or
writer.offset for the exact size.
Address review feedback on PR googleapis#19092.

- append() reopens the stream before checking for lost bytes. On
  DataLoss it now closes that stream and marks the writer closed, so the
  stream isn't leaked and a later close(finalize_on_close=True) raises
  instead of finalizing the truncated object.
- close() closes a stream reopened by a retry that then failed, instead
  of only clearing _is_stream_open.
- Tests: build the multi-call retry payloads from (byte, length)
  segments inside the test with readable ids, so the junit XML shrinks
  from about 27 MB to 22 KB, and vary the bytes in the unaligned case so
  a wrong slice is caught. Use a fast retry policy in the close
  fail-fast test.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(storage): AsyncAppendableObjectWriter.append silently truncates or shifts bytes on retry during multi-call appends

2 participants