Repository navigation
fix(storage): replay unflushed tail after reconnect in AsyncAppendableObjectWriter - #19102
Yonghui-Lee wants to merge 6 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.
…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.
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to replay unpersisted tail bytes in AsyncAppendableObjectWriter upon stream reconnection, utilizing a new _ChainedReader class to read across multiple byte buffers without copying. The review feedback identifies a critical protocol desynchronization issue in _replay_tail_from where recv() should be called for both flush and state_lookup requests to prevent leaving unread responses on the stream. Additionally, a defensive programming improvement is suggested in _trim_tail to store self._tail_end() in a local variable to avoid potential bugs from future refactoring.
ee02b6c to
f7ff5ac
Compare
…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.
4789b32 to
6097940
Compare
zhixiangli
left a comment
There was a problem hiding this comment.
Tested on a real Zonal (RAPID) bucket with AsyncAppendableObjectWriter and gcsfs (ZonalFile.write/commit, put_file, and pipe_file with finalize_on_close=False and True) — all recover cleanly with matching SHA256, and the bytearray simplification is much cleaner than _ChainedReader.
Left a few inline comments below; please also sync the PR description with 609794089ac before squash-merging so the changelog doesn't mention _ChainedReader or REPLAY_UNPERSISTED_TAIL.
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.
…eObjectWriter append() only flushes every flush_interval bytes, and the counter carries across calls. When a call does not end on a flush boundary, its trailing bytes are sent but not flushed. On Zonal buckets the server can drop them if the stream breaks shortly afterwards. The writer no longer held those bytes, so a reconnect during the next append() raised DataLoss. Keep the bytes sent since the last server-confirmed persisted offset in a bytearray and replay them after a reconnect. - After each append(), add the unconfirmed part of data to the tail and drop bytes the server confirmed (flush responses, flush(), state_lookup()). The tail normally stays below flush_interval and is capped at 2 * flush_interval. - The normal append() path is unchanged. Only when a reconnect reports persisted_size before the start of data does append() swap in io.BytesIO(tail + data) and move _WriteState.initial_offset to the tail start, so reset_to_persisted() rewinds into the tail. - close() and finalize() resend the lost tail bytes with append() after a reconnect, then close, instead of raising DataLoss. - DataLoss remains when the server reports persisted_size below the tail start. Fixes googleapis#19101 # Conflicts: # packages/google-cloud-storage/google/cloud/storage/asyncio/async_appendable_object_writer.py # packages/google-cloud-storage/tests/unit/asyncio/test_async_appendable_object_writer.py
…licy Address review feedback on PR googleapis#19102. - If append() fails after a retry reopened the stream (retries exhausted, a non-retryable error, or cancellation), self.offset could be left at the server's persisted size while the tail still held earlier bytes, so a later close(finalize_on_close=True) finalized a truncated object. append() now restores self.offset to the end of the acknowledged bytes, resets the flush counter, cancels the stream, and sets write_obj_stream to None. The next append(), flush(), simple_flush(), state_lookup(), close() or finalize() reconnects and replays the tail. If the server persisted part of the failed append, it raises DataLoss and closes the writer instead. - close() and finalize() clear the tail in finally, so a failed replay doesn't leave it behind. - The reopen and replay inside close() and finalize() use the caller's retry policy, limited to the time left before its deadline. - Tests: the in-memory server now handles finish_write and persists on a graceful close, so the close replay test asserts the returned sizes. Replay workloads use (byte, length) segments with readable ids, and the trim test no longer says "segments".
6097940 to
508f3d1
Compare
|
@zhixiangli thanks for testing it on a real bucket. The PR description is now synced with the |
Note
Stacked on #19092. The first two commits belong to #19092. Only the last commit (
fix(storage): replay unflushed tail after reconnect in AsyncAppendableObjectWriter) is new here. I'll rebase once #19092 merges.Fixes #19101
Problem
append()only requests persistence everyflush_intervalbytes, and the counter carries across calls. When a call does not end on a flush boundary, its trailing bytes are sent but not flushed. On Zonal buckets the server can drop them if the stream breaks shortly afterwards, for example during the nextappend(). The writer no longer held those bytes, so after #19092 such a reconnect raisesDataLoss. Unaligned appends are the common case (gcsfsZonalFile.write(),put_filewith 50 MiB chunks and the 16 MiB default interval).Changes
bytearrayplus its starting offset. After eachappend(), the part ofdatathe server hasn't confirmed is added, and bytes the server confirms are dropped (flush responses,flush(),state_lookup()). The tail normally stays belowflush_intervaland is capped at2 * flush_interval.append()still reads fromio.BytesIO(data). Only when a reconnect reports apersisted_sizebefore the start ofdatadoes it swap inio.BytesIO(tail + data)and move_WriteState.initial_offsetto the tail start. The existingreset_to_persisted()then rewinds into the tail.close()andfinalize()replay: after a reconnect, they resend the lost tail bytes withappend(), then close, instead of raisingDataLoss. Closing persists them.DataLosssafety net: still raised when the server reports apersisted_sizebelow the tail start.Testing
Unit tests with the in-memory server (which drops unflushed bytes on reopen) cover the three real-GCS workloads from the #19092 review (2 x 5 MiB reset at 6 MiB, 10 x 1 MiB reset at 5 MiB, 100 x 64 KiB), caller buffer reuse, the tail cap, flush and state-lookup trimming, close/finalize replay, and the
DataLosspath.Real Zonal bucket (
asia-southeast1-a). One abrupt stream break per case (the gRPC call is cancelled, not closed gracefully), then the object is read back and compared by size and SHA-256:mainDataLossDataLossDataLossDataLossclose()RetryErrorDataLossfinalize()RetryErrorDataLossOverhead without faults. Same bucket, two interleaved rounds:
flush_intervalbytes. For 50 MiB appends with the 16 MiB default, that's the 2 MiB left after each append.