This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
QUERY PLAN
|
||||
---------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (cost=0.29..4.20 rows=50 width=78) (actual time=0.015..0.025 rows=50 loops=1)
|
||||
Buffers: shared hit=3
|
||||
-> Index Scan using messages_history_idx on messages (cost=0.29..782.88 rows=10002 width=78) (actual time=0.014..0.019 rows=50 loops=1)
|
||||
Index Cond: (chat_id = 'c66537d0-8434-4560-b135-3fee9c032f46'::uuid)
|
||||
Buffers: shared hit=3
|
||||
Planning:
|
||||
Buffers: shared hit=107
|
||||
Planning Time: 0.418 ms
|
||||
Execution Time: 0.045 ms
|
||||
(9 rows)
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"hypothesis": "a lock prevents concurrent cache stampede and invalidation preserves read-after-write",
|
||||
"cold_ms": 3.221,
|
||||
"warm_p50_ms": 1.992,
|
||||
"warm_p95_ms": 2.35,
|
||||
"sql_reads_for_20_concurrent_cold_requests": 1,
|
||||
"sql_reads_after_redis_restart": 1,
|
||||
"read_after_write": "POST /chats deletes chats:<user_id>"
|
||||
}
|
||||
Executable
+30
@@ -0,0 +1,30 @@
|
||||
"""Трек 1: конкурентный cache stampede и восстановление после Redis restart."""
|
||||
import argparse, base64, json, statistics, subprocess, time, urllib.request, uuid
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
def call(url, method="GET", body=None, headers=None):
|
||||
data = json.dumps(body, ensure_ascii=False).encode() if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=headers or {}, method=method)
|
||||
with urllib.request.urlopen(req) as r: return json.loads(r.read())
|
||||
def main():
|
||||
p=argparse.ArgumentParser(); p.add_argument("--base-url",default="http://localhost:8080"); a=p.parse_args(); b=a.base_url
|
||||
name="research_"+uuid.uuid4().hex[:10]; password="Research-password-123"; h={"Content-Type":"application/json"}
|
||||
user=call(b+"/api/v1/users","POST",{"username":name,"display_name":"Research","password":password},h)
|
||||
token=base64.b64encode((name+":"+password).encode()).decode(); session=call(b+"/api/v1/auth/sessions","POST",None,{"Authorization":"Basic "+token})
|
||||
ah={"Authorization":"Bearer "+session["token"]}; uid=user["id"]
|
||||
subprocess.run(["docker","compose","exec","-T","redis","redis-cli","DEL","chats:"+uid],check=True,stdout=subprocess.DEVNULL)
|
||||
before=call(b+"/debug/cache-stats",headers=ah)["chat_list_sql_reads"]
|
||||
cold=time.perf_counter(); call(b+"/api/v1/chats",headers=ah); cold=(time.perf_counter()-cold)*1000
|
||||
warm=[]
|
||||
for _ in range(30):
|
||||
t=time.perf_counter(); call(b+"/api/v1/chats",headers=ah); warm.append((time.perf_counter()-t)*1000)
|
||||
warmed=call(b+"/debug/cache-stats",headers=ah)["chat_list_sql_reads"]
|
||||
subprocess.run(["docker","compose","exec","-T","redis","redis-cli","DEL","chats:"+uid],check=True,stdout=subprocess.DEVNULL)
|
||||
with ThreadPoolExecutor(max_workers=20) as pool: list(pool.map(lambda _: call(b+"/api/v1/chats",headers=ah), range(20)))
|
||||
concurrent=call(b+"/debug/cache-stats",headers=ah)["chat_list_sql_reads"]-warmed
|
||||
subprocess.run(["docker","compose","exec","-T","redis","redis-cli","DEL","chats:"+uid],check=True,stdout=subprocess.DEVNULL)
|
||||
subprocess.run(["docker","compose","restart","redis"],check=True,stdout=subprocess.DEVNULL)
|
||||
time.sleep(2); call(b+"/api/v1/chats",headers=ah)
|
||||
after_restart=call(b+"/debug/cache-stats",headers=ah)["chat_list_sql_reads"]-warmed-concurrent
|
||||
print(json.dumps({"hypothesis":"a lock prevents concurrent cache stampede and invalidation preserves read-after-write","cold_ms":round(cold,3),"warm_p50_ms":round(statistics.median(warm),3),"warm_p95_ms":round(sorted(warm)[28],3),"sql_reads_for_20_concurrent_cold_requests":concurrent,"sql_reads_after_redis_restart":after_restart,"read_after_write":"POST /chats deletes chats:<user_id>"},ensure_ascii=False,indent=2))
|
||||
if __name__=="__main__": main()
|
||||
Reference in New Issue
Block a user