Make message retries idempotent across replicas

This commit is contained in:
Самохвалов Андрей Ремович
2026-10-02 11:36:23 +03:00
parent f295b716fe
commit 44fcce080f
5 changed files with 102 additions and 6 deletions
+11
View File
@@ -0,0 +1,11 @@
CREATE TABLE message_replays (
user_id uuid NOT NULL REFERENCES users(id),
chat_id uuid NOT NULL REFERENCES chats(id),
request_key text NOT NULL,
request_hash bytea NOT NULL,
message_id uuid REFERENCES messages(id),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (user_id, chat_id, request_key)
);
CREATE INDEX message_replays_expiry_idx ON message_replays (created_at);
+6 -2
View File
@@ -1,5 +1,6 @@
"""Messenger HTTP routes.""" """Messenger HTTP routes."""
import re
from uuid import UUID from uuid import UUID
from fastapi import APIRouter, Depends, HTTPException, Query, Request, Response from fastapi import APIRouter, Depends, HTTPException, Query, Request, Response
@@ -70,8 +71,11 @@ def get_chat(chat_id: UUID, actor: dict = Depends(current_user)):
@router.post("/api/v1/chats/{chat_id}/messages", status_code=201) @router.post("/api/v1/chats/{chat_id}/messages", status_code=201)
def create_message(chat_id: UUID, body: CreateMessage, actor: dict = Depends(current_user)): def create_message(chat_id: UUID, body: CreateMessage, request: Request, actor: dict = Depends(current_user)):
return store.create_message(actor["id"], str(chat_id), body.text) key = request.headers.get("Idempotency-Key")
if key is None or not re.fullmatch(r"[A-Za-z0-9._:-]{1,128}", key):
raise HTTPException(400)
return store.create_message(actor["id"], str(chat_id), body.text, key)
@router.get("/api/v1/chats/{chat_id}/messages") @router.get("/api/v1/chats/{chat_id}/messages")
+29 -1
View File
@@ -2,6 +2,7 @@
import base64 import base64
import binascii import binascii
import hashlib
import json import json
import os import os
import secrets import secrets
@@ -205,19 +206,46 @@ class Store:
items = [chat_view(row) for row in rows[:limit]] items = [chat_view(row) for row in rows[:limit]]
return {"items": items, "next_cursor": encode_cursor(f"chats:{actor_id}", items[-1]) if len(rows) > limit else None} return {"items": items, "next_cursor": encode_cursor(f"chats:{actor_id}", items[-1]) if len(rows) > limit else None}
def create_message(self, actor_id: str, chat_id: str, text: str) -> dict: def create_message(self, actor_id: str, chat_id: str, text: str, key: str) -> dict:
chat_uuid, actor_uuid = UUID(chat_id), UUID(actor_id) chat_uuid, actor_uuid = UUID(chat_id), UUID(actor_id)
request_hash = hashlib.sha256(json.dumps({"text": text}, ensure_ascii=False, sort_keys=True).encode()).digest()
with connection() as conn: with connection() as conn:
member = conn.execute( member = conn.execute(
"SELECT 1 FROM memberships WHERE chat_id=%s AND user_id=%s", (chat_uuid, actor_uuid) "SELECT 1 FROM memberships WHERE chat_id=%s AND user_id=%s", (chat_uuid, actor_uuid)
).fetchone() ).fetchone()
if member is None: if member is None:
raise HTTPException(404) raise HTTPException(404)
conn.execute(
"DELETE FROM message_replays WHERE user_id=%s AND chat_id=%s AND request_key=%s "
"AND created_at<clock_timestamp()-interval '24 hours'",
(actor_uuid, chat_uuid, key),
)
reserved = conn.execute(
"INSERT INTO message_replays (user_id,chat_id,request_key,request_hash) VALUES (%s,%s,%s,%s) "
"ON CONFLICT DO NOTHING RETURNING request_key",
(actor_uuid, chat_uuid, key, request_hash),
).fetchone()
if reserved is None:
replay = conn.execute(
"SELECT r.request_hash,m.id,m.chat_id,m.sender_id,m.text,m.created_at "
"FROM message_replays r JOIN messages m ON m.id=r.message_id "
"WHERE r.user_id=%s AND r.chat_id=%s AND r.request_key=%s",
(actor_uuid, chat_uuid, key),
).fetchone()
if replay is None:
raise HTTPException(503)
if replay["request_hash"] != request_hash:
raise HTTPException(409)
return message_view(replay)
row = conn.execute( row = conn.execute(
"INSERT INTO messages (id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s) " "INSERT INTO messages (id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s) "
"RETURNING id,chat_id,sender_id,text,created_at", "RETURNING id,chat_id,sender_id,text,created_at",
(uuid4(), chat_uuid, actor_uuid, text), (uuid4(), chat_uuid, actor_uuid, text),
).fetchone() ).fetchone()
conn.execute(
"UPDATE message_replays SET message_id=%s WHERE user_id=%s AND chat_id=%s AND request_key=%s",
(row["id"], actor_uuid, chat_uuid, key),
)
return message_view(row) return message_view(row)
def list_messages(self, actor_id: str, chat_id: str, limit: int, cursor: str | None) -> dict: def list_messages(self, actor_id: str, chat_id: str, limit: int, cursor: str | None) -> dict:
+3 -3
View File
@@ -30,7 +30,7 @@ class DatabaseTests(unittest.TestCase):
def test_migration_and_unique_username_race(self): def test_migration_and_unique_username_race(self):
with psycopg.connect(URL) as conn: with psycopg.connect(URL) as conn:
versions = conn.execute("SELECT version FROM schema_migrations").fetchall() versions = conn.execute("SELECT version FROM schema_migrations").fetchall()
self.assertEqual(versions, [(1,), (2,)]) self.assertEqual(versions, [(1,), (2,), (3,)])
name = "u_" + uuid4().hex[:16] name = "u_" + uuid4().hex[:16]
def register(_): def register(_):
@@ -67,8 +67,8 @@ class DatabaseTests(unittest.TestCase):
chat_page = self.store.list_chats(alice["id"], 1, None) chat_page = self.store.list_chats(alice["id"], 1, None)
self.assertEqual(chat_page["items"], [chat]) self.assertEqual(chat_page["items"], [chat])
self.assertEqual(self.store.list_chats(alice["id"], 1, chat_page["next_cursor"]), {"items": [chat2], "next_cursor": None}) self.assertEqual(self.store.list_chats(alice["id"], 1, chat_page["next_cursor"]), {"items": [chat2], "next_cursor": None})
first = self.store.create_message(alice["id"], chat["id"], "первое") first = self.store.create_message(alice["id"], chat["id"], "первое", uuid4().hex)
second = self.store.create_message(bob["id"], chat["id"], "второе") second = self.store.create_message(bob["id"], chat["id"], "второе", uuid4().hex)
page = self.store.list_messages(alice["id"], chat["id"], 1, None) page = self.store.list_messages(alice["id"], chat["id"], 1, None)
self.assertEqual(page["items"], [first]) self.assertEqual(page["items"], [first])
self.assertEqual(self.store.list_messages(bob["id"], chat["id"], 1, page["next_cursor"]), {"items": [second], "next_cursor": None}) self.assertEqual(self.store.list_messages(bob["id"], chat["id"], 1, page["next_cursor"]), {"items": [second], "next_cursor": None})
+53
View File
@@ -0,0 +1,53 @@
"""Concurrent PostgreSQL-backed message retries create one row."""
import os
import subprocess
import sys
import unittest
from concurrent.futures import ThreadPoolExecutor
from uuid import uuid4
import psycopg
from src.store import Store
URL = os.environ.get("DATABASE_URL")
@unittest.skipUnless(URL, "set DATABASE_URL to a disposable PostgreSQL database")
class IdempotencyTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
subprocess.run([sys.executable, "scripts/migrate.py"], check=True)
cls.store = Store()
def test_parallel_replay_conflict_and_scope(self):
alice = self.store.register("u_" + uuid4().hex[:16], "Алиса", "Test-password-123")
bob = self.store.register("u_" + uuid4().hex[:16], "Боб", "Test-password-123")
chat = self.store.create_chat(alice["id"], "Test", [alice["id"], bob["id"]])
other_chat = self.store.create_chat(alice["id"], "Other", [alice["id"]])
key = uuid4().hex
def send(_):
return Store().create_message(alice["id"], chat["id"], "same body", key)
with ThreadPoolExecutor(max_workers=2) as pool:
first, replay = list(pool.map(send, range(2)))
self.assertEqual(first, replay)
with psycopg.connect(URL) as conn:
count = conn.execute(
"SELECT count(*) FROM messages WHERE chat_id=%s AND sender_id=%s AND text=%s",
(chat["id"], alice["id"], "same body"),
).fetchone()[0]
self.assertEqual(count, 1)
with self.assertRaises(Exception) as conflict:
self.store.create_message(alice["id"], chat["id"], "different body", key)
self.assertEqual(conflict.exception.status_code, 409)
self.assertNotEqual(self.store.create_message(bob["id"], chat["id"], "same body", key)["id"], first["id"])
self.assertNotEqual(self.store.create_message(alice["id"], other_chat["id"], "same body", key)["id"], first["id"])
if __name__ == "__main__":
unittest.main()