114 lines
4.0 KiB
Python
114 lines
4.0 KiB
Python
import json
|
|
import sys
|
|
import threading
|
|
import time
|
|
import uuid
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from pathlib import Path
|
|
|
|
from rolling_update import replace_all
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
from client import Client, Failure, arguments, check, run
|
|
from resilience import history
|
|
|
|
|
|
def main():
|
|
args = arguments(__doc__)
|
|
client = Client(args.base_url, args.ca_file)
|
|
alice, bob, _, chat = client.bootstrap()
|
|
key = uuid.uuid4().hex
|
|
original = client.message(alice, chat, "before update " + key, key=key)["json"]
|
|
stop = threading.Event()
|
|
|
|
def worker(index):
|
|
local = Client(args.base_url, args.ca_file, timeout=3)
|
|
result = {"writes": [], "reads": 0, "errors": 0, "instances": set()}
|
|
pending = uuid.uuid4().hex
|
|
while not stop.is_set():
|
|
try:
|
|
if index < 2:
|
|
response = local.message(
|
|
alice, chat, "rolling " + pending, key=pending
|
|
)
|
|
result["writes"].append(response["json"])
|
|
pending = uuid.uuid4().hex
|
|
else:
|
|
response = local.call(
|
|
"GET",
|
|
"/api/v1/chats/" + chat["id"] + "/messages",
|
|
actor=bob,
|
|
schema="MessagePage",
|
|
)
|
|
result["reads"] += 1
|
|
result["instances"].add(response["headers"]["X-Instance-Id"])
|
|
except Failure:
|
|
result["errors"] += 1 # Uncertain POST keeps its original key and text.
|
|
time.sleep(0.02)
|
|
return result
|
|
|
|
started = time.monotonic()
|
|
with ThreadPoolExecutor(max_workers=4) as pool:
|
|
futures = [pool.submit(worker, index) for index in range(4)]
|
|
try:
|
|
time.sleep(2)
|
|
replacements = replace_all()
|
|
time.sleep(2)
|
|
finally:
|
|
stop.set()
|
|
results = [future.result() for future in futures]
|
|
elapsed = time.monotonic() - started
|
|
confirmed = [original] + [
|
|
message for result in results for message in result["writes"]
|
|
]
|
|
messages = history(client, bob, chat)
|
|
by_id = {message["id"]: message for message in messages}
|
|
check(
|
|
all(by_id.get(message["id"]) == message for message in confirmed),
|
|
"confirmed message lost or changed",
|
|
)
|
|
texts = [message["text"] for message in messages]
|
|
check(len(texts) == len(set(texts)), "retry created a duplicate")
|
|
check(
|
|
client.message(alice, chat, "before update " + key, key=key)["json"]
|
|
== original,
|
|
"idempotency lost",
|
|
)
|
|
instances = set().union(*(result["instances"] for result in results))
|
|
check(
|
|
all(
|
|
record["old_instance"] in instances and record["new_instance"] in instances
|
|
for record in replacements
|
|
),
|
|
"load did not observe all original and replacement processes",
|
|
)
|
|
writes = len(confirmed) - 1
|
|
reads = sum(result["reads"] for result in results)
|
|
errors = sum(result["errors"] for result in results)
|
|
report = {
|
|
"elapsed_seconds": round(elapsed, 3),
|
|
"concurrency": 4,
|
|
"successful_writes": writes,
|
|
"successful_reads": reads,
|
|
"errors": errors,
|
|
"successful_rps": round((writes + reads) / elapsed, 3),
|
|
"replacements": replacements,
|
|
"confirmed_messages_preserved": True,
|
|
"session_preserved": True,
|
|
"workload": "two writers, two readers, 20ms pause per worker; not a capacity benchmark",
|
|
}
|
|
ROOT.joinpath(".memory/grade4").mkdir(parents=True, exist_ok=True)
|
|
ROOT.joinpath(".memory/grade4/rolling.json").write_text(
|
|
json.dumps(report, indent=2) + "\n"
|
|
)
|
|
print(json.dumps(report, indent=2))
|
|
check(errors == 0, "planned replacement caused request errors; see metrics")
|
|
print(
|
|
"PASS: sequential replacement under load; all confirmed messages and sessions preserved"
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main)
|