forked from mtusi2026/mtusi2026lab4
lab4 трек 2: reconciler S3/PostgreSQL, тесты вложений, отказов и согласованности
- python -m messenger.reconcile: merge-join листинга S3 и attachments по object_key; dry-run read-only конструктивно (default_transaction_read_only + S3 без записи), --apply: orphan/stale intent удаляются, ready-строки только в карантин (обратимо), перепроверка под FOR UPDATE, предохранитель от массового удаления; - attachment_ids хранится в строке сообщения (история остаётся одним index range scan), message_attachments — для FK и обратного поиска; - семафор полной проверки изображений (IMAGE_VERIFY_CONCURRENCY); - тесты: вложения (56), отказ/пауза S3 и деградированный режим LB, failover загрузки, приватность bucket, FAULT_POINTS после PUT/после COMMIT, janitor, reconciler; - бенчмарки памяти загрузок и стоимости обхода reconciler. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
335df4b2b2
commit
a2d867bec6
@@ -50,7 +50,14 @@ CREATE TABLE attachments (
|
||||
-- Janitor: stale intents, oldest first; tiny partial index (only rows of unfinished uploads).
|
||||
CREATE INDEX attachments_pending_created ON attachments (created_at) WHERE status = 'pending';
|
||||
|
||||
-- Which attachments a message references, in the order of attachment_ids.
|
||||
-- attachment_ids of a message, twice on purpose (both written in the message's transaction):
|
||||
-- * messages.attachment_ids (ordered array): the history page reads it from the same row, so the
|
||||
-- keyset range scan of messages stays ONE index scan, without a per-row sub-query;
|
||||
-- * message_attachments: foreign keys (a referenced attachment can never be deleted) and the
|
||||
-- reverse lookup "is this attachment used?" (janitor/reconciler) by an index on attachment_id.
|
||||
ALTER TABLE messages ADD COLUMN attachment_ids uuid[] NOT NULL DEFAULT '{}';
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_attachment_ids_len CHECK (cardinality(attachment_ids) <= 10);
|
||||
|
||||
CREATE TABLE message_attachments (
|
||||
message_id uuid NOT NULL REFERENCES messages (id),
|
||||
position smallint NOT NULL,
|
||||
@@ -115,8 +122,8 @@ BEGIN
|
||||
IF FOUND THEN
|
||||
UPDATE chats c SET last_message_at = greatest(clock_timestamp(), c.last_message_at + interval '1 microsecond')
|
||||
WHERE c.id = p_chat RETURNING c.last_message_at INTO v_created;
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
VALUES (p_message, p_chat, p_user, p_text, v_created);
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at, attachment_ids)
|
||||
VALUES (p_message, p_chat, p_user, p_text, v_created, p_attachments);
|
||||
INSERT INTO message_attachments (message_id, position, attachment_id)
|
||||
SELECT p_message, (a.ord - 1)::smallint, a.id
|
||||
FROM unnest(p_attachments) WITH ORDINALITY AS a(id, ord);
|
||||
@@ -127,8 +134,7 @@ BEGIN
|
||||
|
||||
RETURN QUERY
|
||||
SELECT CASE WHEN k.request_hash = p_hash THEN 'replayed' ELSE 'conflict' END, m.id, m.text, m.created_at,
|
||||
ARRAY(SELECT ma.attachment_id FROM message_attachments ma
|
||||
WHERE ma.message_id = m.id ORDER BY ma.position)
|
||||
m.attachment_ids
|
||||
FROM idempotency_keys k JOIN messages m ON m.id = k.message_id
|
||||
WHERE k.user_id = p_user AND k.chat_id = p_chat AND k.key = p_key;
|
||||
IF NOT FOUND THEN
|
||||
|
||||
Reference in New Issue
Block a user