forked from mtusi2026/mtusi2026lab4
выполнение ЛР4 на 4
- S3: SeaweedFS 4.51 (MinIO-образы недоступны), приватный bucket, отдельные ограниченные ключи API, служебные порты закрыты; идемпотентный s3-init с проверкой приватности и прогревом записи - вложения: потоковая загрузка PNG/JPEG, лимиты 10 MiB / 40 Мпикс, тип по содержимому, проверка filename, скачивание только участникам - согласованность PG/S3: pending -> PUT -> ready, компенсация, sweeper с grace и dry-run; attachment_ids в сообщениях, FK по чату - back-pressure на S3 PUT после найденного OOM SeaweedFS - тесты student-tests, evidence, REPORT.md, DEMO.md
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Upload -> stop exactly the API replica that served the upload -> download through the other one.
|
||||
|
||||
Repeats so that each replica is once the "uploader" that dies; restores the replica in between.
|
||||
"""
|
||||
import hashlib
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
from stack import ROOT, SAMPLE_PNG, dc
|
||||
from client import Client, Failure, arguments, check, run
|
||||
|
||||
|
||||
def container_of(instance_id):
|
||||
for cid in dc("ps", "-q", "api").stdout.split():
|
||||
alias = subprocess.run(["docker", "exec", cid, "cat", "/tmp/instance-id"], capture_output=True, text=True).stdout.strip()
|
||||
if alias == instance_id:
|
||||
return subprocess.run(["docker", "inspect", "-f", "{{.Name}}", cid], capture_output=True, text=True).stdout.strip("/\n")
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments("Attachment survives the loss of the replica that stored it")
|
||||
c = Client(args.base_url, timeout=10)
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
stopped = set()
|
||||
for rnd in range(1, 3):
|
||||
# Upload until the serving replica is one we have not stopped yet.
|
||||
for _ in range(10):
|
||||
r = c.call("POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=r%d.png" % rnd, 201, actor=alice,
|
||||
raw=SAMPLE_PNG, headers={"Content-Type": "image/png"}, schema="Attachment")
|
||||
uploader = r["headers"]["X-Instance-Id"]
|
||||
name = container_of(uploader)
|
||||
if name not in stopped:
|
||||
break
|
||||
att = r["json"]
|
||||
env = dict(os.environ, TARGET_INSTANCE_ID=uploader)
|
||||
out = subprocess.run([str(ROOT / "scripts/stop-one.sh")], env=env, capture_output=True, text=True)
|
||||
check(out.returncode == 0, "stop-one.sh failed")
|
||||
print("round %d: uploaded via %s (%s); %s" % (rnd, uploader, name, out.stdout.strip()))
|
||||
served, ok = set(), 0
|
||||
deadline = time.monotonic() + 10
|
||||
while ok < 10:
|
||||
try:
|
||||
d = c.call("GET", "/api/v1/attachments/" + att["id"] + "/content", actor=bob)
|
||||
except Failure:
|
||||
check(time.monotonic() < deadline, "download did not recover within 10 s")
|
||||
time.sleep(0.2)
|
||||
continue
|
||||
check(hashlib.sha256(d["bytes"]).hexdigest() == att["sha256"], "bytes differ after failover")
|
||||
served.add(d["headers"]["X-Instance-Id"])
|
||||
ok += 1
|
||||
check(uploader not in served, "stopped replica still served")
|
||||
print("round %d: 10/10 downloads byte-identical via %s" % (rnd, sorted(served)))
|
||||
stopped.add(name)
|
||||
dc("up", "-d", "--no-build", "--no-recreate", "--wait", "api")
|
||||
print("PASS: attachment downloaded through another replica after its uploader stopped (%d rounds)" % len(stopped))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user