forked from mtusi2026/mtusi2026lab4
Реализовать лабораторную 4
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
from random import Random
|
||||
import struct
|
||||
import sys
|
||||
from threading import Barrier, Event, Thread
|
||||
import time
|
||||
from urllib.error import HTTPError
|
||||
from urllib.parse import quote
|
||||
from urllib.request import urlopen
|
||||
import uuid
|
||||
import zlib
|
||||
|
||||
from check_replicas import compose, wait_instances, REPLICAS
|
||||
from client import Client, Failure, check
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def png(width, height, textured=False):
|
||||
def chunk(kind, data):
|
||||
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data))
|
||||
header = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
|
||||
random = Random(2026)
|
||||
scanlines = b"".join(
|
||||
b"\x00" + (random.randbytes(width * 3) if textured else bytes(width * 3))
|
||||
for _ in range(min(height, 512))
|
||||
)
|
||||
return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", header)
|
||||
+ chunk(b"IDAT", zlib.compress(scanlines))
|
||||
+ chunk(b"IEND", b""))
|
||||
|
||||
|
||||
def upload(client, owner, chat, data, expected=201, name="image.png", mime="image/png"):
|
||||
path = f"/api/v1/chats/{chat['id']}/attachments?filename={quote(name, safe='')}"
|
||||
return client.call("POST", path, expected, actor=owner, raw=data,
|
||||
headers={"Content-Type": mime}, schema="Attachment" if expected == 201 else None)
|
||||
|
||||
|
||||
def memory():
|
||||
return {name: int(compose("exec", "-T", name, "cat", "/sys/fs/cgroup/memory.current"))
|
||||
for name in REPLICAS}
|
||||
|
||||
|
||||
def run_concurrent(owner, chat, data):
|
||||
ready = Barrier(10)
|
||||
stop = Event()
|
||||
baseline = memory()
|
||||
peak = dict(baseline)
|
||||
|
||||
def sample():
|
||||
while not stop.is_set():
|
||||
for name, value in memory().items():
|
||||
peak[name] = max(peak[name], value)
|
||||
stop.wait(0.1)
|
||||
|
||||
def send(_):
|
||||
ready.wait(timeout=10)
|
||||
return upload(Client(timeout=15), owner, chat, data)["json"]["id"]
|
||||
|
||||
sampler = Thread(target=sample)
|
||||
sampler.start()
|
||||
try:
|
||||
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||
ids = list(pool.map(send, range(10)))
|
||||
finally:
|
||||
stop.set()
|
||||
sampler.join()
|
||||
check(len(set(ids)) == 10, "uploads returned duplicate IDs")
|
||||
print(f"PASS: 10 concurrent uploads of {len(data)} bytes; "
|
||||
f"cgroup memory baseline={baseline}, sampled_peak={peak}", flush=True)
|
||||
|
||||
|
||||
def main():
|
||||
compose("start", *REPLICAS)
|
||||
wait_instances(set(REPLICAS))
|
||||
client = Client(timeout=6)
|
||||
owner, member, outsider, chat = client.bootstrap()
|
||||
data = png(512, 512)
|
||||
sent = upload(client, owner, chat, data)
|
||||
attachment = sent["json"]
|
||||
serving = sent["headers"]["X-Instance-Id"]
|
||||
path = f"/api/v1/attachments/{attachment['id']}"
|
||||
upload(client, owner, chat, data, 400, name="../image.png")
|
||||
upload(client, owner, chat, data, 415, mime="image/jpeg")
|
||||
upload(client, owner, chat, b"broken image", 415)
|
||||
upload(client, owner, chat, b"\0" * (10 * 1024**2 + 1), 413)
|
||||
upload(client, owner, chat, png(10000, 4001), 413)
|
||||
client.call("GET", path, 404, actor=outsider)
|
||||
client.call("GET", path + "/content", 404, actor=outsider)
|
||||
other = client.call("POST", "/api/v1/chats", 201, actor=owner, body={
|
||||
"title": "Other chat", "member_ids": [owner["id"]],
|
||||
}, schema="Chat")["json"]
|
||||
wrong_path = f"/api/v1/chats/{other['id']}/messages"
|
||||
client.call("POST", wrong_path, 404, actor=owner,
|
||||
body={"text": "", "attachment_ids": [attachment["id"]]},
|
||||
headers={"Idempotency-Key": uuid.uuid4().hex})
|
||||
print("PASS: size, pixels, malformed image, MIME, filename and cross-chat access", flush=True)
|
||||
|
||||
compose("stop", serving)
|
||||
try:
|
||||
survivor = next(name for name in REPLICAS if name != serving)
|
||||
deadline = time.monotonic() + 10
|
||||
while True:
|
||||
try:
|
||||
result = client.call("GET", path + "/content", actor=member)
|
||||
break
|
||||
except Failure:
|
||||
if time.monotonic() >= deadline:
|
||||
raise
|
||||
time.sleep(0.2)
|
||||
check(result["bytes"] == data and result["headers"]["X-Instance-Id"] == survivor,
|
||||
"original unavailable on surviving replica")
|
||||
finally:
|
||||
compose("start", serving)
|
||||
wait_instances(set(REPLICAS))
|
||||
print(f"PASS: original bytes read through {survivor} after {serving} stopped", flush=True)
|
||||
|
||||
compose("exec", "-T", "api-a", "python", "-c",
|
||||
"import os,urllib.error,urllib.request,sys;"
|
||||
"url=os.environ['S3_ENDPOINT']+'/'+os.environ['S3_BUCKET']+'/'+sys.argv[1];"
|
||||
"exec('try:\\n urllib.request.urlopen(url,timeout=3)\\nexcept urllib.error.HTTPError as e:"
|
||||
"\\n assert e.code == 403\\nelse:\\n raise AssertionError()')",
|
||||
f"attachments/{attachment['id']}")
|
||||
print("PASS: anonymous direct access to S3 object returned 403", flush=True)
|
||||
|
||||
run_concurrent(owner, chat, png(512, 512, textured=True))
|
||||
compose("stop", "s3")
|
||||
try:
|
||||
status = None
|
||||
deadline = time.monotonic() + 10
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
with urlopen(client.base + "/health/ready", timeout=2):
|
||||
pass
|
||||
except HTTPError as error:
|
||||
if error.code == 503:
|
||||
status = error.code
|
||||
break
|
||||
except OSError:
|
||||
pass
|
||||
time.sleep(0.1)
|
||||
check(status == 503, "API did not report S3 failure")
|
||||
finally:
|
||||
compose("start", "s3")
|
||||
wait_instances(set(REPLICAS))
|
||||
check(client.call("GET", path + "/content", actor=owner)["bytes"] == data,
|
||||
"original lost after restarting S3")
|
||||
print("PASS: readiness returned 503 during S3 outage, original survived restart")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,118 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
from threading import Barrier, Event
|
||||
import time
|
||||
import uuid
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
||||
from client import Client, Failure, check
|
||||
|
||||
REPLICAS = ("api-a", "api-b")
|
||||
|
||||
|
||||
def compose(*args):
|
||||
return subprocess.check_output(["docker", "compose", *args], text=True, timeout=45).strip()
|
||||
|
||||
|
||||
def wait_instances(expected):
|
||||
client = Client(timeout=1)
|
||||
deadline = time.monotonic() + 25
|
||||
seen = set()
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
instance = client.call("GET", "/health/ready", schema="Health")["headers"]["X-Instance-Id"]
|
||||
seen.add(instance)
|
||||
if seen == expected:
|
||||
return
|
||||
except Failure:
|
||||
pass
|
||||
time.sleep(0.1)
|
||||
raise AssertionError(f"Expected {expected}, observed {seen}")
|
||||
|
||||
|
||||
def parallel_replay(actor, chat):
|
||||
start = Barrier(8)
|
||||
key = uuid.uuid4().hex
|
||||
|
||||
def write(_):
|
||||
start.wait(timeout=10)
|
||||
return Client().message(actor, chat, "Concurrent replay", key=key)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=8) as workers:
|
||||
results = list(workers.map(write, range(8)))
|
||||
ids = {item["json"]["id"] for item in results}
|
||||
instances = {item["headers"]["X-Instance-Id"] for item in results}
|
||||
check(len(ids) == 1 and instances == set(REPLICAS), "parallel replay across replicas failed")
|
||||
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||
page = Client().call("GET", path, actor=actor, schema="MessagePage")["json"]
|
||||
check(sum(item["id"] in ids for item in page["items"]) == 1, "duplicate in history")
|
||||
print("PASS: 8 concurrent replays through both replicas, 1 message", flush=True)
|
||||
|
||||
|
||||
def exercise(actor, chat, target, recreate):
|
||||
stop = Event()
|
||||
results = []
|
||||
client = Client(timeout=2)
|
||||
started = time.monotonic()
|
||||
|
||||
def traffic():
|
||||
key = uuid.uuid4().hex
|
||||
while not stop.is_set():
|
||||
try:
|
||||
response = client.message(actor, chat, "Write " + key, key=key)
|
||||
results.append((time.monotonic(), response["headers"]["X-Instance-Id"], response["json"]["id"]))
|
||||
key = uuid.uuid4().hex
|
||||
except Failure:
|
||||
results.append((time.monotonic(), None, None))
|
||||
stop.wait(0.05)
|
||||
|
||||
old_id = compose("ps", "-q", target)
|
||||
with ThreadPoolExecutor(max_workers=1) as workers:
|
||||
worker = workers.submit(traffic)
|
||||
try:
|
||||
time.sleep(0.5)
|
||||
changed = time.monotonic()
|
||||
if recreate:
|
||||
compose("up", "-d", "--no-deps", "--force-recreate", target)
|
||||
check(old_id != compose("ps", "-q", target), "container was not replaced")
|
||||
wait_instances(set(REPLICAS))
|
||||
else:
|
||||
compose("stop", "--timeout", "5", target)
|
||||
check(not compose("ps", "-q", "--status", "running", target), "replica is still running")
|
||||
time.sleep(2.5)
|
||||
finally:
|
||||
stop.set()
|
||||
worker.result(timeout=5)
|
||||
if not recreate:
|
||||
compose("start", target)
|
||||
wait_instances(set(REPLICAS))
|
||||
|
||||
survivor = next(name for name in REPLICAS if name != target)
|
||||
successful = [row for row in results if row[1] is not None]
|
||||
first = next(row[0] for row in successful if row[0] >= changed and row[1] == survivor)
|
||||
check(first - changed <= 10, "surviving replica took too long")
|
||||
path = f"/api/v1/chats/{chat['id']}/messages?limit=100"
|
||||
observed = set()
|
||||
while path is not None:
|
||||
page = client.call("GET", path, actor=actor, schema="MessagePage")["json"]
|
||||
observed.update(item["id"] for item in page["items"])
|
||||
cursor = page["next_cursor"]
|
||||
path = f"/api/v1/chats/{chat['id']}/messages?limit=100&cursor={cursor}" if cursor else None
|
||||
check({row[2] for row in successful} <= observed, "confirmed writes lost")
|
||||
action = "recreate" if recreate else "stop"
|
||||
elapsed = results[-1][0] - started
|
||||
print(f"PASS: {action} {target}, {len(successful) / elapsed:.1f} write/s, "
|
||||
f"errors={len(results) - len(successful)}, survivor={first - changed:.3f}s", flush=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
compose("start", *REPLICAS)
|
||||
wait_instances(set(REPLICAS))
|
||||
owner, _, _, chat = Client().bootstrap()
|
||||
parallel_replay(owner, chat)
|
||||
for name in REPLICAS:
|
||||
exercise(owner, chat, name, recreate=False)
|
||||
for name in REPLICAS:
|
||||
exercise(owner, chat, name, recreate=True)
|
||||
Reference in New Issue
Block a user