136 lines
4.9 KiB
Python
136 lines
4.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Prove recovery across DB-commit/publish and result-commit/ack crashes."""
|
|
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
|
|
from client import Client, Failure, check, run # noqa: E402
|
|
|
|
COMPOSE = ["docker", "compose", "--env-file", ".env.example"]
|
|
|
|
|
|
def command(*args, capture=False, environment=None):
|
|
result = subprocess.run(
|
|
[*COMPOSE, *args], cwd=ROOT, check=True, text=True,
|
|
capture_output=capture, env=environment)
|
|
return result.stdout.strip() if capture else ""
|
|
|
|
|
|
def database_scalar(sql):
|
|
return command(
|
|
"exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger",
|
|
"-At", "-c", sql, capture=True)
|
|
|
|
|
|
def recreate(service, fault_name=None):
|
|
environment = os.environ.copy()
|
|
environment["FAULT_CRASH_AFTER_OUTBOX_COMMIT"] = "false"
|
|
environment["FAULT_CRASH_AFTER_RESULT_COMMIT"] = "false"
|
|
if fault_name:
|
|
environment[fault_name] = "true"
|
|
command("up", "-d", "--no-deps", "--force-recreate", service,
|
|
environment=environment)
|
|
|
|
|
|
def wait_http(client, seconds=120):
|
|
deadline = time.monotonic() + seconds
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
client.call("GET", "/health/ready", schema="Health")
|
|
return
|
|
except Failure:
|
|
time.sleep(1)
|
|
raise Failure("API did not become ready")
|
|
|
|
|
|
def wait_job(attachment_id, expected="ready", seconds=120):
|
|
deadline = time.monotonic() + seconds
|
|
while time.monotonic() < deadline:
|
|
status = database_scalar(
|
|
"SELECT status FROM attachment_jobs WHERE attachment_id = '"
|
|
+ attachment_id + "';")
|
|
if status == expected:
|
|
return
|
|
if status == "failed":
|
|
raise Failure("fault-injected job reached failed")
|
|
time.sleep(1)
|
|
raise Failure("fault-injected job did not reach " + expected)
|
|
|
|
|
|
def latest_attachment(chat_id, fault_column):
|
|
row = database_scalar(
|
|
"SELECT a.id FROM attachments a JOIN attachment_jobs j ON j.attachment_id = a.id "
|
|
"WHERE a.chat_id = '" + chat_id + "' AND j." + fault_column
|
|
+ " = true ORDER BY a.created_at DESC LIMIT 1;")
|
|
check(bool(row), "fault marker was not persisted")
|
|
return row
|
|
|
|
|
|
def main():
|
|
client = Client("http://localhost:8080")
|
|
wait_http(client)
|
|
alice, bob, _, chat = client.bootstrap()
|
|
original = (ROOT / "tests" / "fixtures" / "sample.png").read_bytes()
|
|
|
|
# Crash exactly after attachment + outbox COMMIT, before the request thread publishes.
|
|
recreate("api", "FAULT_CRASH_AFTER_OUTBOX_COMMIT")
|
|
wait_http(client)
|
|
try:
|
|
client.call(
|
|
"POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=outbox.png",
|
|
202, actor=alice, raw=original, headers={"Content-Type": "image/png"},
|
|
schema="Attachment")
|
|
raise Failure("outbox fault did not terminate the API request")
|
|
except Failure as error:
|
|
if "outbox fault did not" in str(error):
|
|
raise
|
|
|
|
outbox_attachment = latest_attachment(chat["id"], "publish_fault_injected")
|
|
wait_job(outbox_attachment)
|
|
recreate("api")
|
|
wait_http(client)
|
|
recovered = client.call(
|
|
"GET", "/api/v1/attachments/" + outbox_attachment,
|
|
actor=bob, schema="Attachment")["json"]
|
|
check(recovered["status"] == "ready", "outbox recovery did not publish the job")
|
|
check(len(recovered["variants"]) == 3, "outbox recovery did not finish all variants")
|
|
|
|
# Crash after S3 effects + result COMMIT, before RabbitMQ consumer ack.
|
|
recreate("worker", "FAULT_CRASH_AFTER_RESULT_COMMIT")
|
|
queued = client.call(
|
|
"POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=ack-window.png",
|
|
202, actor=alice, raw=original, headers={"Content-Type": "image/png"},
|
|
schema="Attachment")["json"]
|
|
wait_job(queued["id"])
|
|
ready = client.wait_ready(bob, queued, wait_seconds=120)
|
|
check(len(ready["variants"]) == 3, "result crash lost variants")
|
|
result_marker = database_scalar(
|
|
"SELECT result_fault_injected FROM attachment_jobs WHERE attachment_id = '"
|
|
+ queued["id"] + "';")
|
|
check(result_marker == "t", "result commit fault marker was not persisted")
|
|
recreate("worker")
|
|
|
|
deadline = time.monotonic() + 60
|
|
while time.monotonic() < deadline:
|
|
queues = command(
|
|
"exec", "-T", "rabbitmq", "rabbitmqctl", "list_queues",
|
|
"name", "messages_ready", "messages_unacknowledged", capture=True)
|
|
if "attachments.process.level4.v1\t0\t0" in queues:
|
|
break
|
|
time.sleep(1)
|
|
variant_count = database_scalar(
|
|
"SELECT count(*) FROM attachment_variants WHERE attachment_id = '"
|
|
+ queued["id"] + "';")
|
|
check(variant_count == "3", "redelivery duplicated committed variants")
|
|
print("PASS: outbox commit/publish crash and result commit/ack crash recovered")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main)
|