forked from mtusi2026/mtusi2026lab3
lab3: second tiny fix
This commit is contained in:
+58
-19
@@ -2,6 +2,7 @@
|
||||
"""Exercise a real Swarm worker loss while reading and writing through the LB."""
|
||||
|
||||
import argparse
|
||||
from collections import Counter
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import json
|
||||
from pathlib import Path
|
||||
@@ -9,6 +10,7 @@ import statistics
|
||||
import subprocess
|
||||
import time
|
||||
import uuid
|
||||
from urllib.parse import quote
|
||||
|
||||
from client import Client, Failure
|
||||
|
||||
@@ -16,10 +18,23 @@ from client import Client, Failure
|
||||
def tasks():
|
||||
result = subprocess.check_output([
|
||||
"docker", "exec", "lab3-manager", "docker", "service", "ps",
|
||||
"--filter", "desired-state=running", "--format", "{{.Node}} {{.CurrentState}}",
|
||||
"--filter", "desired-state=running", "--format", "{{.ID}} {{.Node}} {{.CurrentState}}",
|
||||
"lab3app_api",
|
||||
], text=True)
|
||||
return [line.split()[0] for line in result.splitlines() if "Running" in line]
|
||||
return [{"task_id": parts[0], "node": parts[1]} for line in result.splitlines()
|
||||
if len(parts := line.split()) >= 3 and parts[2] == "Running"]
|
||||
|
||||
|
||||
def history(client, actor, chat):
|
||||
path = "/api/v1/chats/" + chat["id"] + "/messages?limit=100"
|
||||
items = []
|
||||
for _ in range(100):
|
||||
page = client.call("GET", path, actor=actor, schema="MessagePage")["json"]
|
||||
items.extend(page["items"])
|
||||
if page["next_cursor"] is None:
|
||||
return items
|
||||
path = "/api/v1/chats/" + chat["id"] + "/messages?limit=100&cursor=" + quote(page["next_cursor"], safe="")
|
||||
raise AssertionError("history pagination did not terminate")
|
||||
|
||||
|
||||
def main():
|
||||
@@ -31,7 +46,7 @@ def main():
|
||||
client = Client(args.base_url, timeout=2)
|
||||
alice, bob, _, chat = client.bootstrap()
|
||||
before = tasks()
|
||||
assert len(before) == 2 and args.worker in before, before
|
||||
assert len(before) == 2 and args.worker in {task["node"] for task in before}, before
|
||||
instances = {
|
||||
client.call("GET", "/api/v1/users/me", actor=alice, schema="User")["headers"]["X-Instance-Id"]
|
||||
for _ in range(40)
|
||||
@@ -41,13 +56,19 @@ def main():
|
||||
key = uuid.uuid4().hex
|
||||
body = "concurrent " + key
|
||||
with ThreadPoolExecutor(max_workers=16) as executor:
|
||||
results = list(executor.map(lambda _: client.message(alice, chat, body, key=key)["json"], range(16)))
|
||||
responses = list(executor.map(lambda _: client.message(alice, chat, body, key=key), range(16)))
|
||||
results = [response["json"] for response in responses]
|
||||
concurrent_instances = {response["headers"]["X-Instance-Id"] for response in responses}
|
||||
assert all(item == results[0] for item in results)
|
||||
assert len(concurrent_instances) >= 2, concurrent_instances
|
||||
|
||||
subprocess.run(["docker", "stop", "-t", "1", args.worker], check=True, stdout=subprocess.DEVNULL)
|
||||
start = time.monotonic()
|
||||
successes = []
|
||||
errors = 0
|
||||
subprocess.run(["docker", "stop", "-t", "1", args.worker], check=True, stdout=subprocess.DEVNULL)
|
||||
stop_return_seconds = time.monotonic() - start
|
||||
successes = {}
|
||||
post_errors = 0
|
||||
read_errors = 0
|
||||
successful_reads = 0
|
||||
latencies = []
|
||||
moved_at = None
|
||||
first_success = None
|
||||
@@ -56,35 +77,53 @@ def main():
|
||||
while time.monotonic() - start < 75:
|
||||
try:
|
||||
response = client.message(alice, chat, pending_text, key=pending_key)
|
||||
successes.append(response["json"])
|
||||
assert pending_key not in successes
|
||||
successes[pending_key] = response["json"]
|
||||
latest_id = response["json"]["id"]
|
||||
latencies.append(response["elapsed"] * 1000)
|
||||
if first_success is None:
|
||||
first_success = time.monotonic() - start
|
||||
client.call("GET", "/api/v1/chats/" + chat["id"] + "/messages?limit=1", actor=bob, schema="MessagePage")
|
||||
pending_key = uuid.uuid4().hex
|
||||
pending_text = "node-fail " + pending_key
|
||||
except Failure:
|
||||
errors += 1
|
||||
post_errors += 1
|
||||
else:
|
||||
try:
|
||||
assert latest_id in {item["id"] for item in history(client, bob, chat)}
|
||||
successful_reads += 1
|
||||
except Failure:
|
||||
read_errors += 1
|
||||
current = tasks()
|
||||
if len(current) == 2 and args.worker not in current and moved_at is None:
|
||||
if len(current) == 2 and args.worker not in {task["node"] for task in current} and moved_at is None:
|
||||
moved_at = time.monotonic() - start
|
||||
if moved_at is not None and time.monotonic() - start > moved_at + 5:
|
||||
break
|
||||
time.sleep(0.15)
|
||||
assert moved_at is not None, tasks()
|
||||
assert successes, "no writes during node failure"
|
||||
page = client.call("GET", "/api/v1/chats/" + chat["id"] + "/messages?limit=100", actor=bob, schema="MessagePage")["json"]
|
||||
seen = {item["id"] for item in page["items"]}
|
||||
assert results[0]["id"] in seen and all(item["id"] in seen for item in successes)
|
||||
assert sum(item["text"] == body for item in page["items"]) == 1
|
||||
items = history(client, bob, chat)
|
||||
ids = Counter(item["id"] for item in items)
|
||||
texts = Counter(item["text"] for item in items)
|
||||
assert ids[results[0]["id"]] == 1 and texts[body] == 1
|
||||
assert all(ids[item["id"]] == 1 and texts[item["text"]] == 1 for item in successes.values())
|
||||
after_instances = {
|
||||
client.call("GET", "/api/v1/users/me", actor=alice, schema="User")["headers"]["X-Instance-Id"]
|
||||
for _ in range(40)
|
||||
}
|
||||
assert after_instances - instances, (instances, after_instances)
|
||||
report = {
|
||||
"before_nodes": before,
|
||||
"after_nodes": tasks(),
|
||||
"before_instance_count": len(instances),
|
||||
"before_tasks": before,
|
||||
"after_tasks": tasks(),
|
||||
"before_instance_ids": sorted(instances),
|
||||
"after_instance_ids": sorted(after_instances),
|
||||
"concurrent_replays": len(results),
|
||||
"concurrent_message_count": 1,
|
||||
"concurrent_instance_ids": sorted(concurrent_instances),
|
||||
"successful_writes": len(successes),
|
||||
"failed_attempts": errors,
|
||||
"successful_reads": successful_reads,
|
||||
"failed_post_attempts": post_errors,
|
||||
"failed_read_attempts": read_errors,
|
||||
"docker_stop_seconds": round(stop_return_seconds, 3),
|
||||
"first_success_seconds": round(first_success, 3),
|
||||
"replacement_running_seconds": round(moved_at, 3),
|
||||
"confirmed_messages_preserved": True,
|
||||
|
||||
Reference in New Issue
Block a user