forked from mtusi2026/mtusi2026lab2
reworked
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
"""Track 1: cache consistency — stampede, stale read, read-after-write."""
|
||||
import json
|
||||
import threading
|
||||
import uuid
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
from client import Client
|
||||
|
||||
|
||||
def _lab_metrics(c: Client):
|
||||
return json.loads(c.call("GET", "/api/v1/_lab/metrics")["bytes"].decode())
|
||||
|
||||
|
||||
def _lab_flags(c: Client, body):
|
||||
return json.loads(c.call("POST", "/api/v1/_lab/flags", 200, body=body)["bytes"].decode())
|
||||
|
||||
|
||||
def test_read_after_write(live: Client):
|
||||
alice, bob, _eve, chat = live.bootstrap()
|
||||
_lab_flags(live, {"invalidate": True, "singleflight": True, "reset_sql": True})
|
||||
first = live.message(alice, chat, "raw-visible-" + uuid.uuid4().hex)["json"]
|
||||
page = live.call(
|
||||
"GET",
|
||||
"/api/v1/chats/%s/messages?limit=100" % chat["id"],
|
||||
actor=bob,
|
||||
schema="MessagePage",
|
||||
)["json"]
|
||||
assert any(m["id"] == first["id"] for m in page["items"])
|
||||
|
||||
|
||||
def test_stale_without_invalidation_then_fixed(live: Client):
|
||||
alice, bob, _eve, chat = live.bootstrap()
|
||||
path = "/api/v1/chats/%s/messages?limit=50" % chat["id"]
|
||||
live.call("GET", path, actor=alice, schema="MessagePage")
|
||||
_lab_flags(live, {"invalidate": False, "singleflight": True})
|
||||
try:
|
||||
stale_text = "stale-probe-" + uuid.uuid4().hex
|
||||
live.message(alice, chat, stale_text)
|
||||
page = live.call("GET", path, actor=bob, schema="MessagePage")["json"]
|
||||
assert all(m["text"] != stale_text for m in page["items"])
|
||||
finally:
|
||||
_lab_flags(live, {"invalidate": True})
|
||||
fresh_text = "fresh-probe-" + uuid.uuid4().hex
|
||||
live.message(alice, chat, fresh_text)
|
||||
page2 = live.call("GET", path, actor=bob, schema="MessagePage")["json"]
|
||||
assert any(m["text"] == fresh_text for m in page2["items"])
|
||||
|
||||
|
||||
def test_stampede_singleflight_reduces_sql(live: Client):
|
||||
alice, _bob, _eve, chat = live.bootstrap()
|
||||
live.message(alice, chat, "stampede-seed")
|
||||
path = "/api/v1/chats/%s/messages?limit=50" % chat["id"]
|
||||
|
||||
def burst(n=24):
|
||||
barrier = threading.Barrier(n)
|
||||
|
||||
def one(_i):
|
||||
local = Client(live.base)
|
||||
barrier.wait()
|
||||
return local.call("GET", path, actor=alice, schema="MessagePage")
|
||||
|
||||
with ThreadPoolExecutor(max_workers=n) as pool:
|
||||
list(pool.map(one, range(n)))
|
||||
|
||||
try:
|
||||
_lab_flags(live, {"singleflight": False, "invalidate": True, "reset_sql": True, "delay_ms": 250})
|
||||
live.message(alice, chat, "miss-" + uuid.uuid4().hex)
|
||||
_lab_flags(live, {"reset_sql": True, "singleflight": False, "delay_ms": 250})
|
||||
burst()
|
||||
unprotected = _lab_metrics(live)["history_sql"]
|
||||
|
||||
_lab_flags(live, {"singleflight": True, "reset_sql": True, "delay_ms": 250})
|
||||
live.message(alice, chat, "miss2-" + uuid.uuid4().hex)
|
||||
_lab_flags(live, {"reset_sql": True, "singleflight": True, "delay_ms": 250})
|
||||
burst()
|
||||
protected = _lab_metrics(live)["history_sql"]
|
||||
finally:
|
||||
_lab_flags(live, {"invalidate": True, "singleflight": True, "delay_ms": 0})
|
||||
|
||||
print(json.dumps({"stampede_sql_without_singleflight": unprotected, "stampede_sql_with_singleflight": protected}))
|
||||
assert unprotected >= 8, unprotected
|
||||
assert protected < unprotected, (protected, unprotected)
|
||||
assert protected <= 4, protected
|
||||
Reference in New Issue
Block a user