forked from mtusi2026/mtusi2026lab5
Реализовать lab5 на оценку 4: async attachments, outbox, worker и variants.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
-- Lab2 schema: users, chats, membership, messages.
|
||||
-- Applied once via schema_migrations; safe to re-run application bootstrap.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version TEXT PRIMARY KEY,
|
||||
applied_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id UUID PRIMARY KEY,
|
||||
username TEXT NOT NULL,
|
||||
display_name TEXT NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL,
|
||||
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
CONSTRAINT users_username_unique UNIQUE (username)
|
||||
);
|
||||
|
||||
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),
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_chat_members_user
|
||||
ON chat_members (user_id, chat_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
|
||||
);
|
||||
|
||||
-- Keyset pagination by (created_at, id) within a chat; avoids full history scan.
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_chat_created_id
|
||||
ON messages (chat_id, created_at ASC, id ASC);
|
||||
|
||||
-- List chats for a user ordered by chat created_at/id.
|
||||
CREATE INDEX IF NOT EXISTS idx_chats_created_id
|
||||
ON chats (created_at ASC, id ASC);
|
||||
@@ -0,0 +1,16 @@
|
||||
-- Lab3: shared idempotency records for POST /messages (across API replicas).
|
||||
|
||||
CREATE TABLE IF NOT EXISTS idempotency_records (
|
||||
user_id UUID NOT NULL,
|
||||
method TEXT NOT NULL,
|
||||
path TEXT NOT NULL,
|
||||
idem_key TEXT NOT NULL,
|
||||
request_hash TEXT NOT NULL,
|
||||
response_status INT NOT NULL,
|
||||
response_body JSONB NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL,
|
||||
PRIMARY KEY (user_id, method, path, idem_key)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_idempotency_created
|
||||
ON idempotency_records (created_at);
|
||||
@@ -0,0 +1,55 @@
|
||||
-- Lab4: attachment metadata (bytes live only in the private S3 bucket).
|
||||
-- Additive migration: lab3 replicas keep working while it is applied.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS attachments (
|
||||
id UUID PRIMARY KEY,
|
||||
chat_id UUID NOT NULL REFERENCES chats(id),
|
||||
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,
|
||||
width INT NOT NULL,
|
||||
height INT NOT NULL,
|
||||
-- Server-generated S3 key; never derived from filename, never exposed by the API.
|
||||
object_key TEXT NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL,
|
||||
updated_at TIMESTAMPTZ NOT NULL,
|
||||
CONSTRAINT attachments_object_key_unique UNIQUE (object_key),
|
||||
CONSTRAINT attachments_content_type_check CHECK (content_type IN ('image/png', 'image/jpeg')),
|
||||
CONSTRAINT attachments_size_check CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
CONSTRAINT attachments_sha256_check CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
CONSTRAINT attachments_pixels_check CHECK (width > 0 AND height > 0 AND width::bigint * height <= 40000000),
|
||||
CONSTRAINT attachments_status_check CHECK (status IN ('ready'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_attachments_chat
|
||||
ON attachments (chat_id, created_at, id);
|
||||
|
||||
-- Ordered links message -> attachment (an attachment may be referenced by several messages).
|
||||
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),
|
||||
position SMALLINT NOT NULL,
|
||||
PRIMARY KEY (message_id, attachment_id),
|
||||
CONSTRAINT message_attachments_position_check CHECK (position BETWEEN 0 AND 9),
|
||||
CONSTRAINT message_attachments_position_unique UNIQUE (message_id, position)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_message_attachments_attachment
|
||||
ON message_attachments (attachment_id);
|
||||
|
||||
-- Write-ahead journal of S3 objects whose metadata is not committed yet.
|
||||
-- A row exists from just before the S3 PUT until the metadata commit (same
|
||||
-- transaction deletes it) or until compensation/reconciliation removes the object.
|
||||
CREATE TABLE IF NOT EXISTS upload_intents (
|
||||
object_key TEXT PRIMARY KEY,
|
||||
attachment_id UUID NOT NULL,
|
||||
chat_id UUID NOT NULL,
|
||||
instance_id TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_upload_intents_created
|
||||
ON upload_intents (created_at);
|
||||
@@ -0,0 +1,8 @@
|
||||
-- Lab4: message text may contain U+0000, which PostgreSQL TEXT/JSONB cannot store.
|
||||
-- Text is kept as exact UTF-8 bytes; the idempotent response snapshot as JSON
|
||||
-- (textual JSON keeps the "\u0000" escape, JSONB rejects it).
|
||||
ALTER TABLE messages
|
||||
ALTER COLUMN text TYPE BYTEA USING convert_to(text, 'UTF8');
|
||||
|
||||
ALTER TABLE idempotency_records
|
||||
ALTER COLUMN response_body TYPE JSON USING response_body::json;
|
||||
@@ -0,0 +1,95 @@
|
||||
-- Lab5: asynchronous attachment processing (queued → worker → ready/failed).
|
||||
-- Additive: extends lab4 attachments with staging, jobs, outbox, variants.
|
||||
|
||||
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_check;
|
||||
ALTER TABLE attachments
|
||||
ADD CONSTRAINT attachments_status_check
|
||||
CHECK (status IN ('queued', 'processing', 'ready', 'failed'));
|
||||
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS error_code TEXT;
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS staging_key TEXT;
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS pipeline_version INT NOT NULL DEFAULT 1;
|
||||
|
||||
-- Final original may be filled only after the worker publishes it.
|
||||
ALTER TABLE attachments ALTER COLUMN object_key DROP NOT NULL;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS attachment_variants (
|
||||
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
||||
pipeline_version INT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
content_type TEXT NOT NULL,
|
||||
size_bytes BIGINT NOT NULL,
|
||||
width INT NOT NULL,
|
||||
height INT NOT NULL,
|
||||
sha256 TEXT NOT NULL,
|
||||
object_key TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (attachment_id, pipeline_version, name),
|
||||
CONSTRAINT attachment_variants_name_check
|
||||
CHECK (name IN ('thumbnail', 'crop', 'optimized')),
|
||||
CONSTRAINT attachment_variants_sha256_check CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
CONSTRAINT attachment_variants_size_check CHECK (size_bytes > 0),
|
||||
CONSTRAINT attachment_variants_pixels_check CHECK (width > 0 AND height > 0)
|
||||
);
|
||||
|
||||
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 INT NOT NULL DEFAULT 1,
|
||||
attempt INT NOT NULL DEFAULT 1,
|
||||
status TEXT NOT NULL,
|
||||
lease_owner TEXT,
|
||||
lease_until TIMESTAMPTZ,
|
||||
next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
last_error TEXT,
|
||||
error_code TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT attachment_jobs_status_check
|
||||
CHECK (status IN ('pending', 'leased', 'done', 'failed')),
|
||||
CONSTRAINT attachment_jobs_attempt_check CHECK (attempt >= 1)
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_attachment_jobs_one_active
|
||||
ON attachment_jobs (attachment_id, pipeline_version)
|
||||
WHERE status IN ('pending', 'leased');
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_attachment_jobs_lease
|
||||
ON attachment_jobs (status, lease_until)
|
||||
WHERE status = 'leased';
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_attachment_jobs_pending
|
||||
ON attachment_jobs (next_attempt_at)
|
||||
WHERE status = 'pending';
|
||||
|
||||
-- Transactional outbox: COMMIT of job intent and broker publish are decoupled.
|
||||
CREATE TABLE IF NOT EXISTS outbox (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
job_id UUID NOT NULL REFERENCES attachment_jobs(job_id) ON DELETE CASCADE,
|
||||
payload JSONB NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
published_at TIMESTAMPTZ,
|
||||
publish_attempts INT NOT NULL DEFAULT 0
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_outbox_unpublished
|
||||
ON outbox (id)
|
||||
WHERE published_at IS NULL;
|
||||
|
||||
-- Operator-visible failed registry (DLQ mirror) with safe explicit replay.
|
||||
CREATE TABLE IF NOT EXISTS dead_letter_jobs (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
job_id UUID NOT NULL,
|
||||
attachment_id UUID NOT NULL,
|
||||
pipeline_version INT NOT NULL DEFAULT 1,
|
||||
payload JSONB NOT NULL,
|
||||
error_code TEXT NOT NULL,
|
||||
attempts INT NOT NULL,
|
||||
failed_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
replayed_at TIMESTAMPTZ,
|
||||
replay_note TEXT
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_dead_letter_open
|
||||
ON dead_letter_jobs (failed_at)
|
||||
WHERE replayed_at IS NULL;
|
||||
@@ -0,0 +1,18 @@
|
||||
-- Lab5 ops hardening: durable session revocation + indexes for janitor/replay.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS session_revocations (
|
||||
token_hash TEXT PRIMARY KEY,
|
||||
revoked_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
expires_at TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_session_revocations_expires
|
||||
ON session_revocations (expires_at);
|
||||
|
||||
-- Attachments may keep staging_key while queued/processing; janitor must see it.
|
||||
CREATE INDEX IF NOT EXISTS idx_attachments_staging_key
|
||||
ON attachments (staging_key)
|
||||
WHERE staging_key IS NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_attachments_status
|
||||
ON attachments (status);
|
||||
Reference in New Issue
Block a user