ЛР5: асинхронная обработка вложений (RabbitMQ + worker), transactional outbox, варианты, трек 5.1

This commit is contained in:
2026-11-20 16:57:31 +03:00
parent 276dbdad8a
commit cbaadb7636
67 changed files with 5603 additions and 20 deletions
+42
View File
@@ -0,0 +1,42 @@
-- Lab2 initial schema. Idempotent and safe to re-run via the migration runner.
create table if not exists users (
id uuid primary key,
username text not null unique,
display_name text not null,
password_hash text not null,
created_at timestamptz not null
);
create table if not exists chats (
id uuid primary key,
title text not null,
created_at timestamptz not null
);
create table if not exists chat_members (
chat_id uuid not null references chats (id) on delete cascade,
user_id uuid not null references users (id) on delete cascade,
primary key (chat_id, user_id)
);
create table if not exists messages (
id uuid primary key,
chat_id uuid not null references chats (id) on delete cascade,
sender_id uuid not null references users (id),
text text not null,
created_at timestamptz not null
);
-- Composite index prepared for keyset pagination:
-- where chat_id = $1 and (created_at, id) > ($2, $3) order by created_at, id
create index if not exists messages_chat_created_id_idx
on messages (chat_id, created_at, id);
create table if not exists sessions (
token_hash text primary key,
user_id uuid not null references users (id) on delete cascade,
created_at timestamptz not null,
expires_at timestamptz not null
);
create index if not exists sessions_expires_at_idx on sessions (expires_at);
+19
View File
@@ -0,0 +1,19 @@
-- Lab3 shared idempotency ledger for POST message retries.
--
-- Scope is (user_id, HTTP method + chat path, key); the stored row carries the
-- canonical request hash and the exact 201 body so any replica can replay the
-- same Message. Rows expire after IDEMPOTENCY_TTL_SECONDS (default 24h).
create table if not exists idempotency_keys (
user_id uuid not null references users (id) on delete cascade,
scope text not null,
key text not null,
request_hash text not null,
status_code integer not null,
response_body jsonb not null,
created_at timestamptz not null default now(),
expires_at timestamptz not null,
primary key (user_id, scope, key)
);
create index if not exists idempotency_keys_expires_at_idx
on idempotency_keys (expires_at);
+15
View File
@@ -0,0 +1,15 @@
-- Lab3 durable cache invalidation queue.
--
-- Logout deletes the authoritative session row in PostgreSQL and enqueues the
-- token hash here in the SAME transaction. A lightweight background drainer in
-- each API replica removes the corresponding Valkey cache key and then the row.
-- If Valkey was unreachable at logout time, the row survives and the stale
-- cache entry is removed as soon as the cache comes back, instead of being
-- trusted until its TTL.
create table if not exists cache_invalidations (
token_hash text primary key,
created_at timestamptz not null default now()
);
create index if not exists cache_invalidations_created_at_idx
on cache_invalidations (created_at);
+31
View File
@@ -0,0 +1,31 @@
-- Lab4 attachments: metadata in PostgreSQL, bytes in S3.
--
-- ``s3_key`` is server-generated and never exposed through the API. Linkage to
-- a message is a many-to-many table so a message can carry ordered attachments
-- and the same attachment can be referenced again without duplicating bytes.
create table if not exists attachments (
id uuid primary key,
chat_id uuid not null references chats (id) on delete cascade,
owner_id uuid not null references users (id),
filename text not null,
content_type text not null,
size_bytes bigint not null,
sha256 text not null,
status text not null,
s3_key text not null,
error_code text,
created_at timestamptz not null,
updated_at timestamptz not null
);
create index if not exists attachments_chat_id_idx on attachments (chat_id);
create table if not exists message_attachments (
message_id uuid not null references messages (id) on delete cascade,
attachment_id uuid not null references attachments (id) on delete cascade,
position integer not null,
primary key (message_id, attachment_id)
);
create index if not exists message_attachments_attachment_idx
on message_attachments (attachment_id);
+49
View File
@@ -0,0 +1,49 @@
-- Lab5: asynchronous attachment processing.
--
-- * attachments gains a staging pointer (the durable upload) and a pipeline
-- version; s3_key becomes the FINAL original and is set by the worker, so it
-- must be nullable while the attachment is queued.
-- * attachment_variants holds one row per (attachment, pipeline_version, name)
-- so reprocessing is idempotent and an older version can never overwrite a
-- newer result.
-- * attachment_jobs is both the job registry and the transactional outbox:
-- the API writes a row in the same transaction as the attachment, and a
-- publisher thread moves rows to the broker (published_at) closing the
-- COMMIT -> publish gap.
alter table attachments alter column s3_key drop not null;
alter table attachments add column if not exists staging_key text;
alter table attachments add column if not exists pipeline_version integer not null default 1;
create table if not exists attachment_variants (
id uuid primary key,
attachment_id uuid not null references attachments (id) on delete cascade,
pipeline_version integer not null,
name text not null,
content_type text not null,
size_bytes bigint not null,
width integer not null,
height integer not null,
sha256 text not null,
s3_key text not null,
created_at timestamptz not null,
unique (attachment_id, pipeline_version, name)
);
create table if not exists attachment_jobs (
job_id uuid primary key,
attachment_id uuid not null references attachments (id) on delete cascade,
pipeline_version integer not null,
attempt integer not null default 1,
status text not null default 'pending', -- pending|processing|done|failed
available_at timestamptz not null default now(),
lease_until timestamptz,
published_at timestamptz,
last_error text,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
create index if not exists attachment_jobs_outbox_idx
on attachment_jobs (published_at, available_at);
create index if not exists attachment_jobs_lease_idx
on attachment_jobs (status, lease_until);