161 lines
3.9 KiB
Python
161 lines
3.9 KiB
Python
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from client import Client, check
|
|
from fault_injection_grade5 import job_state
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
COMPOSE = ["docker", "compose", "-f", "compose.yaml", "-f", "compose.concurrent.yaml"]
|
|
MARKER = "/tmp/lab5_pause_before_commit"
|
|
|
|
|
|
def run(*args, check_result=True):
|
|
return subprocess.run(
|
|
args,
|
|
cwd=ROOT,
|
|
capture_output=True,
|
|
text=True,
|
|
check=check_result,
|
|
)
|
|
|
|
|
|
def compose(*args):
|
|
return run(*COMPOSE, *args)
|
|
|
|
|
|
def sql(query):
|
|
result = run(
|
|
"docker", "compose", "exec", "-T", "postgres",
|
|
"psql", "-U", "lab2", "-d", "messenger",
|
|
"-t", "-A", "-c", query,
|
|
)
|
|
return result.stdout.strip()
|
|
|
|
|
|
def wait_until(predicate, timeout, message):
|
|
deadline = time.monotonic() + timeout
|
|
|
|
while time.monotonic() < deadline:
|
|
if predicate():
|
|
return
|
|
time.sleep(0.25)
|
|
|
|
raise AssertionError(message)
|
|
|
|
|
|
def marker_exists():
|
|
result = run(
|
|
*COMPOSE,
|
|
"exec", "-T", "worker",
|
|
"test", "-f", MARKER,
|
|
check_result=False,
|
|
)
|
|
return result.returncode == 0
|
|
|
|
|
|
def release_worker():
|
|
compose("exec", "-T", "worker", "rm", "-f", MARKER)
|
|
|
|
|
|
def main():
|
|
try:
|
|
compose("up", "-d", "--build", "--force-recreate", "worker")
|
|
|
|
client = Client()
|
|
alice, bob, _, chat = client.bootstrap()
|
|
attachment, original = client.attachment(alice, chat)
|
|
attachment_id = attachment["id"]
|
|
|
|
wait_until(
|
|
marker_exists,
|
|
60,
|
|
"first worker did not pause",
|
|
)
|
|
|
|
first = job_state(attachment_id)
|
|
|
|
check(first["job_status"] == "processing", "first execution not processing")
|
|
check(first["execution_id"] is not None, "missing execution ID")
|
|
|
|
sql(f"""
|
|
UPDATE attachment_jobs
|
|
SET started_at = now() - interval '120 seconds'
|
|
WHERE id = '{first["job_id"]}'
|
|
AND status = 'processing';
|
|
""")
|
|
|
|
wait_until(
|
|
lambda: (
|
|
job_state(attachment_id)["execution_id"]
|
|
!= first["execution_id"]
|
|
),
|
|
15,
|
|
"dispatcher did not release the old execution",
|
|
)
|
|
|
|
compose("up", "-d", "--build", "worker-second")
|
|
|
|
deadline = time.monotonic() + 120
|
|
|
|
while time.monotonic() < deadline:
|
|
state = job_state(attachment_id)
|
|
|
|
if state["job_status"] == "ready":
|
|
break
|
|
|
|
time.sleep(0.25)
|
|
else:
|
|
raise AssertionError("second worker did not complete the job")
|
|
|
|
check(state["execution_id"] != first["execution_id"], "execution ID unchanged")
|
|
check(state["attachment_status"] == "ready", "attachment not ready")
|
|
|
|
release_worker()
|
|
|
|
time.sleep(3)
|
|
|
|
after = job_state(attachment_id)
|
|
|
|
check(after == state, "old execution overwrote newer result")
|
|
|
|
content = client.call(
|
|
"GET",
|
|
f"/api/v1/attachments/{attachment_id}/content",
|
|
actor=bob,
|
|
)
|
|
|
|
check(content["bytes"] == original, "original content changed")
|
|
|
|
print("PASS: stale execution rejected after newer execution completed")
|
|
|
|
finally:
|
|
try:
|
|
release_worker()
|
|
except Exception:
|
|
pass
|
|
|
|
try:
|
|
compose("stop", "worker-second")
|
|
except Exception:
|
|
pass
|
|
|
|
run(
|
|
"docker", "compose",
|
|
"up", "-d", "--force-recreate", "worker",
|
|
)
|
|
|
|
print("Normal worker configuration restored")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as exc:
|
|
print(f"FAIL: {exc}", file=sys.stderr)
|
|
raise SystemExit(1)
|