Repository navigation
fix(storage): seek relative to initial_offset when retrying multi-call append - #19092
Yonghui-Lee wants to merge 4 commits into
Conversation
…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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
|
@zhixiangli re the |
…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.
Summary
Fixes silent data truncation and shifted-byte corruption in
AsyncAppendableObjectWriter.append()(andappend_from_file()) when a transient gRPC error or redirect triggers stream recovery on a second or subsequentappend()call.AsyncAppendableObjectWriter.append(data)wraps only the current call's payload inio.BytesIO(data), whereaspersisted_sizetracks the total persisted size of the object across the stream. Previously, recovery soughtuser_buffertopersisted_sizeinstead ofpersisted_size - initial_offset, seeking past EOF and dropping unpersisted bytes whenpersisted_size >= len(data)or skippinginitial_offsetbytes insidedatawhen0 < initial_offset <= persisted_size < len(data).Changes
Add
initial_offset(defaulting to0) to_WriteStateso eachappend()call records the object byte offset corresponding to position0ofuser_bufferand initializesbytes_sentatinitial_offset.Add
_WriteState.reset_to_persisted()to rewinduser_buffertopersisted_offset - initial_offset(clamped at0before stream reopen refreshespersisted_size) and resetbytes_sentandbytes_since_last_flushin one place.Update both
_WriteResumptionStrategy.recover_state_on_failure()andAsyncAppendableObjectWriter.append()stream reopen handling to callwrite_state.reset_to_persisted()instead of duplicating rawseek(persisted_size)logic.Pass
initial_offset=self.offset or 0when constructing_WriteStateinAsyncAppendableObjectWriter.append()and reuse a single_WriteResumptionStrategyinstance across request generation and retry management.Raise
google.api_core.exceptions.DataLossafter stream reopen if the server's refreshedpersisted_sizeis smaller thaninitial_offset, failing fast when unpersisted bytes from an earlierappend()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 🦕