refactoring

This commit is contained in:
Матвей
2026-10-22 16:38:54 +03:00
parent 483d7eecb3
commit c84d112f0d
34 changed files with 791 additions and 84 deletions
+22 -13
View File
@@ -72,17 +72,18 @@ def main():
# A reference object must survive an apply sweep, even with zero grace.
protected = upload("protected.png")["json"]
message = client.message(alice, chat, "protected", attachments=[protected["id"]])["json"]
orphan = "originals/test-orphan-" + uuid.uuid4().hex
s3_code("put", orphan)
report = reconcile("--grace-seconds", "3600")
check(orphan in report["fresh_unreferenced"], "fresh orphan missing from dry-run")
check(not report["deleted"], "dry-run deleted an object")
report = reconcile("--apply", "--grace-seconds", "3600")
report = reconcile("--apply", "--grace-seconds", "3600", "--object-key", orphan)
check(orphan in report["fresh_unreferenced"] and not report["deleted"], "grace period ignored")
report = reconcile("--apply", "--grace-seconds", "0")
report = reconcile("--apply", "--grace-seconds", "0", "--object-key", orphan)
check(report["deleted"] == [orphan], "aged orphan was not deleted safely")
report = reconcile("--apply", "--grace-seconds", "0")
report = reconcile("--apply", "--grace-seconds", "0", "--object-key", orphan)
check(orphan not in report["deleted"] and orphan not in report["orphan_candidates"],
"second repair run was not idempotent")
check(client.call("GET", "/api/v1/attachments/" + protected["id"] + "/content", actor=bob)["bytes"] == image,
@@ -92,11 +93,14 @@ def main():
missing = upload("missing.png")["json"]
missing_key = "originals/" + missing["id"]
s3_code("delete", missing_key)
report = reconcile("--grace-seconds", "0")
expected = {"attachment_id": missing["id"], "object_key": missing_key}
check(expected in report["missing_objects"], "missing metadata/object pair not reported")
client.call("GET", "/api/v1/attachments/" + missing["id"] + "/content", 503, actor=bob)
s3_code("put", missing_key, image)
try:
report = reconcile("--apply", "--grace-seconds", "0", "--object-key", missing_key)
expected = {"attachment_id": missing["id"], "object_key": missing_key}
check(expected in report["missing_objects"], "missing metadata/object pair not reported")
client.call("GET", "/api/v1/attachments/" + missing["id"], actor=bob, schema="Attachment")
client.call("GET", "/api/v1/attachments/" + missing["id"] + "/content", 503, actor=bob)
finally:
s3_code("put", missing_key, image)
check(client.call("GET", "/api/v1/attachments/" + missing["id"] + "/content", actor=bob)["bytes"] == image,
"test object was not restored")
report = reconcile("--grace-seconds", "0")
@@ -107,6 +111,7 @@ def main():
# replica. The public load balancer may correctly select the healthy peer,
# which would not exercise this boundary.
environment = dict(__import__("os").environ, UPLOAD_FAULT_AFTER_S3_PUT="1")
before_fault = set(reconcile("--grace-seconds", "0")["orphan_candidates"])
try:
subprocess.run(["docker", "compose", "up", "-d", "--force-recreate", "--no-deps", "api-a"],
cwd=ROOT, env=environment, check=True, stdout=subprocess.DEVNULL)
@@ -128,14 +133,18 @@ else:
input=json.dumps({"chat_id": chat["id"], "token": alice["token"],
"image": base64.b64encode(image).decode()}))
report = reconcile("--grace-seconds", "0")
injected = [key for key in report["orphan_candidates"] if key.startswith("originals/")]
check(injected, "injected post-PUT failure did not create a detectable orphan")
report = reconcile("--apply", "--grace-seconds", "0")
injected = sorted(set(report["orphan_candidates"]) - before_fault)
check(len(injected) == 1, "fault must create exactly one new orphan; run without other writers")
report = reconcile("--apply", "--grace-seconds", "0", "--object-key", injected[0])
check(set(injected).issubset(report["deleted"]), "reconciler did not clean injected orphan")
print("PASS: PostgreSQL-side post-PUT/pre-DB failure is repaired without deleting references", flush=True)
history = client.call("GET", "/api/v1/chats/" + chat["id"] + "/messages", actor=bob, schema="MessagePage")["json"]
check(message in history["items"], "message reference was lost")
check(client.call("GET", "/api/v1/attachments/" + protected["id"] + "/content", actor=bob)["bytes"] == image,
"used original was lost")
print("PASS: injected pre-INSERT failure repaired; message and used original survive", flush=True)
finally:
environment["UPLOAD_FAULT_AFTER_S3_PUT"] = "0"
subprocess.run(["docker", "compose", "up", "-d", "--force-recreate", "--no-deps", "api-a"],
subprocess.run(["docker", "compose", "up", "-d", "--wait", "--force-recreate", "--no-deps", "api-a"],
cwd=ROOT, env=environment, check=True, stdout=subprocess.DEVNULL)