|
| 1 | +"""UploadPart retry on transient backend errors. |
| 2 | +
|
| 3 | +Prod incident 2026-07-28: the Scylla backup of `main.companies` failed on 10 of |
| 4 | +13 racks, leaving ~2.4TiB un-uploaded. Every failure was a ~6.2GiB SSTable and |
| 5 | +every error was ``InternalError: The server did not respond in time.`` with |
| 6 | +``status code: 200`` -- RGW commits to a 200, streams it, then puts the failure |
| 7 | +in the body. |
| 8 | +
|
| 9 | +Three retry layers all miss that shape because each decides from the HTTP status |
| 10 | +line, which already read 200: |
| 11 | + * rclone's ``LowLevelRetries`` gates on status 429/500/503 (backend/s3/s3.go) |
| 12 | + * rclone's string fallback only matches transport phrases ("use of closed |
| 13 | + network connection", "unexpected EOF reading trailer"), not ``InternalError`` |
| 14 | + * botocore's ``max_attempts`` in S3Client sees the same 200 |
| 15 | +
|
| 16 | +#133 added this retry to UploadPartCopy and #138 to CompleteMultipartUpload, but |
| 17 | +UploadPart -- the operation a Scylla backup actually uses (all PUT, no COPY) -- |
| 18 | +was left bare. At 50MB rclone chunks a 6.2GiB file is ~762 internal PUTs, so one |
| 19 | +unretried transient failure loses the whole multi-GB file. |
| 20 | +""" |
| 21 | + |
| 22 | +from __future__ import annotations |
| 23 | + |
| 24 | +import pytest |
| 25 | +from botocore.exceptions import ClientError |
| 26 | + |
| 27 | +from s3proxy.handlers import base |
| 28 | +from s3proxy.handlers.multipart.upload_part import UploadPartMixin |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture(autouse=True) |
| 32 | +def _no_backoff(monkeypatch): |
| 33 | + monkeypatch.setattr(base, "SOURCE_READ_BACKOFF_SEC", 0.0) |
| 34 | + |
| 35 | + |
| 36 | +def _client_error(code: str, message: str | None = None) -> ClientError: |
| 37 | + return ClientError({"Error": {"Code": code, "Message": message or code}}, "UploadPart") |
| 38 | + |
| 39 | + |
| 40 | +class _FlakyUploadClient: |
| 41 | + """Backend whose first ``fail_times`` upload_part calls raise ``error_code``.""" |
| 42 | + |
| 43 | + def __init__(self, fail_times: int = 0, error_code: str = "InternalError"): |
| 44 | + self._fail_times = fail_times |
| 45 | + self._error_code = error_code |
| 46 | + self.attempts = 0 |
| 47 | + self.seen = [] |
| 48 | + |
| 49 | + async def upload_part(self, bucket, key, upload_id, part_number, body): |
| 50 | + self.attempts += 1 |
| 51 | + self.seen.append((part_number, bytes(body))) |
| 52 | + if self._fail_times > 0: |
| 53 | + self._fail_times -= 1 |
| 54 | + if self._error_code == "InternalError": |
| 55 | + raise _client_error("InternalError", "The server did not respond in time.") |
| 56 | + raise _client_error(self._error_code) |
| 57 | + return {"ETag": f'"etag-{part_number}"'} |
| 58 | + |
| 59 | + |
| 60 | +async def _upload(client, **kw): |
| 61 | + return await UploadPartMixin._upload_part_with_retry( |
| 62 | + UploadPartMixin.__new__(UploadPartMixin), |
| 63 | + client, |
| 64 | + "bucket", |
| 65 | + "key", |
| 66 | + "upload-id", |
| 67 | + kw.pop("internal_part_num", 7), |
| 68 | + kw.pop("ciphertext", b"ciphertext-bytes"), |
| 69 | + client_part_num=kw.pop("client_part_num", 1), |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.asyncio |
| 74 | +async def test_retries_late_200_internal_error(): |
| 75 | + """The exact prod shape: InternalError in a 200 body, then success.""" |
| 76 | + client = _FlakyUploadClient(fail_times=1) |
| 77 | + resp = await _upload(client) |
| 78 | + assert resp["ETag"] == '"etag-7"' |
| 79 | + assert client.attempts == 2 |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.asyncio |
| 83 | +async def test_retry_reuses_same_part_number_and_bytes(): |
| 84 | + """Re-PUT must be idempotent - same part number, same ciphertext.""" |
| 85 | + client = _FlakyUploadClient(fail_times=2) |
| 86 | + await _upload(client, internal_part_num=3, ciphertext=b"abc") |
| 87 | + assert client.attempts == 3 |
| 88 | + assert client.seen == [(3, b"abc")] * 3 |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.asyncio |
| 92 | +async def test_gives_up_after_attempt_cap(): |
| 93 | + """Exhausting attempts re-raises rather than looping forever.""" |
| 94 | + client = _FlakyUploadClient(fail_times=base.SOURCE_READ_ATTEMPTS) |
| 95 | + with pytest.raises(ClientError): |
| 96 | + await _upload(client) |
| 97 | + assert client.attempts == base.SOURCE_READ_ATTEMPTS |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.asyncio |
| 101 | +async def test_non_retryable_error_is_not_retried(): |
| 102 | + """A real client error must fail fast, not burn attempts.""" |
| 103 | + client = _FlakyUploadClient(fail_times=1, error_code="AccessDenied") |
| 104 | + with pytest.raises(ClientError): |
| 105 | + await _upload(client) |
| 106 | + assert client.attempts == 1 |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.asyncio |
| 110 | +async def test_success_path_makes_one_call(): |
| 111 | + client = _FlakyUploadClient() |
| 112 | + resp = await _upload(client) |
| 113 | + assert resp["ETag"] == '"etag-7"' |
| 114 | + assert client.attempts == 1 |
| 115 | + |
| 116 | + |
| 117 | +@pytest.mark.parametrize("code", ["InternalError", "SlowDown", "ServiceUnavailable"]) |
| 118 | +@pytest.mark.asyncio |
| 119 | +async def test_retryable_codes(code): |
| 120 | + client = _FlakyUploadClient(fail_times=1, error_code=code) |
| 121 | + await _upload(client) |
| 122 | + assert client.attempts == 2 |
0 commit comments