fix(migrator): retry failed writes serially
CI / test (push) Canceled after 0s

Pass-B writes (issue body rewrite + each comment) ran only at the
caller's --concurrency, so a transient target 5xx either aborted the
run or silently lost the write. A self-hosted Gitea throws bursts of
500s under concurrent writes, losing comments and leaving bodies with
stale cross-refs.

Failed writes are now deferred and replayed once serially (concurrency
1) after the concurrent wave -- serial load removes the backpressure
causing the 500s, so nearly all recover. Writes still failing serially
are logged and skipped; a genuinely-down target still aborts via
CircuitOpenError.

Adds two phase tests: persistent-fail (no abort) and transient (recover).
This commit is contained in:
AlexKucera committed 2026-08-13 16:10:10 +02:00
1 parent 8449a2bd81
commit e6fc0c7fb9
4 files changed
+167 -12

No files matched your search

+99
View File
@@ -347,6 +347,105 @@ def test_migrate_issues_and_prs_attaches_mapped_labels_and_pr_milestone() -> Non
assert pr_ms_patches
def test_migrate_issues_and_prs_retries_failed_writes_serially_without_aborting() -> None:
# The target persistently 500s on the body-rewrite PATCH and on the "second"
# comment. Each is deferred in the concurrent pass, retried once serially,
# and — still failing — reported as a warning. The run completes; the other
# comments land; the issue is mapped. Nothing aborts on a per-write ApiError.
src = _src(
{
"/repos/o/r/issues": [{"number": 1, "title": "t", "body": "see #2", "state": "open"}],
"/repos/o/r/pulls": [],
"/repos/o/r/issues/1/comments": [
{"id": 11, "body": "first"},
{"id": 12, "body": "second"},
{"id": 13, "body": "third"},
],
}
)
recorded: list[dict] = []
def tgt_h(req: httpx.Request) -> httpx.Response:
method, path = req.method, _wire_path(req)
body = json.loads(req.read() or "null")
recorded.append({"method": method, "path": path, "body": body})
if method == "POST" and path == "/repos/o/r/issues":
return httpx.Response(201, json={"number": 101})
if method == "PATCH" and path == "/repos/o/r/issues/101":
return httpx.Response(500, json={"message": "persistent"})
if method == "POST" and path == "/repos/o/r/issues/101/comments":
if isinstance(body, dict) and body.get("body") == "second":
return httpx.Response(500, json={"message": "persistent"})
return httpx.Response(201, json={"id": 1})
return httpx.Response(200, json={})
tgt = _client(tgt_h, base="https://git.babylondreams.de")
m = Migration(src, tgt, "o", "r")
migrate_issues_and_prs(m, {}, {}, concurrency=1) # must not raise
# "second" attempted twice (concurrent pass + serial retry), still lost;
# "first"/"third" landed once each.
comment_posts = _find(recorded, "POST", "/repos/o/r/issues/101/comments")
assert [c["body"]["body"] for c in comment_posts] == ["first", "second", "third", "second"]
# body rewrite attempted in both passes; persistent 500, never recovered
assert len(_find(recorded, "PATCH", "/repos/o/r/issues/101")) == 2
assert m.numbers.get(1) == 101
def test_migrate_issues_and_prs_recovers_deferred_writes_on_serial_replay() -> None:
# The target 500s only on the FIRST attempt of the body rewrite and of the
# "second" comment; the serial replay succeeds. Final state: the deferred
# writes are recovered (each attempted twice: fail then ok).
src = _src(
{
"/repos/o/r/issues": [{"number": 1, "title": "t", "body": "see #2", "state": "open"}],
"/repos/o/r/pulls": [],
"/repos/o/r/issues/1/comments": [
{"id": 11, "body": "first"},
{"id": 12, "body": "second"},
],
}
)
recorded: list[dict] = []
seen: dict[str, int] = {"patch": 0, "second": 0}
def tgt_h(req: httpx.Request) -> httpx.Response:
method, path = req.method, _wire_path(req)
body = json.loads(req.read() or "null")
recorded.append({"method": method, "path": path, "body": body})
if method == "POST" and path == "/repos/o/r/issues":
return httpx.Response(201, json={"number": 101})
if method == "PATCH" and path == "/repos/o/r/issues/101":
seen["patch"] += 1
if seen["patch"] == 1:
return httpx.Response(500, json={"message": "transient"})
return httpx.Response(200, json={})
if method == "POST" and path == "/repos/o/r/issues/101/comments":
if isinstance(body, dict) and body.get("body") == "second":
seen["second"] += 1
if seen["second"] == 1:
return httpx.Response(500, json={"message": "transient"})
return httpx.Response(201, json={"id": 1})
return httpx.Response(200, json={})
tgt = _client(tgt_h, base="https://git.babylondreams.de")
m = Migration(src, tgt, "o", "r")
migrate_issues_and_prs(m, {}, {}, concurrency=2) # must not raise
# body rewrite recovered on the serial replay (attempted twice: fail then ok)
assert len(_find(recorded, "PATCH", "/repos/o/r/issues/101")) == 2
# "second" recovered: posted twice (fail in pass 1, ok in pass 2)
second_posts = [
r
for r in _find(recorded, "POST", "/repos/o/r/issues/101/comments")
if r["body"].get("body") == "second"
]
assert len(second_posts) == 2
assert m.numbers.get(1) == 101
# -- releases (+ assets) ------------------------------------------------
def test_migrate_releases_recreates_rewrites_body_and_reuploads_assets() -> None:
src = _src(