Files
mtusi2026lab6/migrations/0006_async_processing.sql
T
alenchha 0638263029 ЛР 6 — БВТ2301 — Курлова — на 5 (трек 1 «Цена наблюдаемости»)
Перенесён минимум lab5 без изменения API и добавлена наблюдаемость:
- OTel SDK в API и worker: traces, metrics, logs по OTLP; W3C trace context
  API -> outbox -> RabbitMQ -> worker (producer/consumer spans, retry в той же трассе);
- OTel Collector (memory_limiter, scrub, batch, ограниченные очереди, профиль tail sampling),
  Prometheus, Loki, Tempo, Grafana с provisioning, dashboard и 6 alert rules с runbooks;
- проверки: наблюдаемость и приватность, инциденты A/B/C, насыщение worker,
  переполнение очереди exporter, эксперимент трека 1 (4 конфигурации x 3 прогона);
- CI job со сборкой и запуском стенда; REPORT.md с доказательствами в evidence/.
2026-11-11 09:24:27 +03:00

76 lines
4.2 KiB
SQL

-- lab5: upload -> staging -> job/outbox -> broker -> worker -> final prefix + variants.
ALTER TABLE attachments DROP CONSTRAINT attachments_status;
ALTER TABLE attachments ADD CONSTRAINT attachments_status
CHECK (status IN ('uploading', 'queued', 'processing', 'ready', 'failed'));
-- staging_key: private S3 prefix written by the API before 202; object_key: final original written by the worker.
ALTER TABLE attachments ADD COLUMN staging_key text;
ALTER TABLE attachments ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1;
CREATE UNIQUE INDEX attachments_staging_key_idx ON attachments (staging_key) WHERE staging_key IS NOT NULL;
-- Effect key of the pipeline: (attachment_id, pipeline_version, name). A repeated job cannot add a second set.
CREATE TABLE attachment_variants (
attachment_id uuid NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
pipeline_version integer NOT NULL,
name text NOT NULL CONSTRAINT variants_name CHECK (name IN ('thumbnail', 'crop', 'optimized')),
object_key text NOT NULL UNIQUE,
content_type text NOT NULL CONSTRAINT variants_ct CHECK (content_type IN ('image/jpeg', 'image/png', 'image/webp')),
size_bytes integer NOT NULL CONSTRAINT variants_size CHECK (size_bytes > 0),
width integer NOT NULL CONSTRAINT variants_width CHECK (width > 0),
height integer NOT NULL CONSTRAINT variants_height CHECK (height > 0),
sha256 text NOT NULL CONSTRAINT variants_sha CHECK (sha256 ~ '^[a-f0-9]{64}$'),
created_at timestamptz NOT NULL,
PRIMARY KEY (attachment_id, pipeline_version, name)
);
-- One logical job per (attachment, pipeline_version); job_id never changes on redelivery or retry.
CREATE TABLE attachment_jobs (
id uuid PRIMARY KEY,
attachment_id uuid NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
pipeline_version integer NOT NULL,
state text NOT NULL
CONSTRAINT jobs_state CHECK (state IN ('queued', 'processing', 'succeeded', 'failed')),
attempt integer NOT NULL DEFAULT 1 CONSTRAINT jobs_attempt CHECK (attempt >= 1),
max_attempts integer NOT NULL CONSTRAINT jobs_max_attempts CHECK (max_attempts >= 1),
next_attempt_at timestamptz NOT NULL,
lease_owner text,
lease_until timestamptz,
error_code text,
last_error text,
created_at timestamptz NOT NULL,
first_started_at timestamptz,
started_at timestamptz,
finished_at timestamptz,
updated_at timestamptz NOT NULL,
UNIQUE (attachment_id, pipeline_version)
);
CREATE INDEX attachment_jobs_lease_idx ON attachment_jobs (lease_until) WHERE state = 'processing';
CREATE INDEX attachment_jobs_failed_idx ON attachment_jobs (updated_at) WHERE state = 'failed';
CREATE INDEX attachment_jobs_pending_idx ON attachment_jobs (state) WHERE state IN ('queued', 'processing');
-- Transactional outbox: written in the same transaction as the job; the relay publishes with confirms.
CREATE TABLE outbox (
id bigserial PRIMARY KEY,
job_id uuid NOT NULL REFERENCES attachment_jobs (id) ON DELETE CASCADE,
payload jsonb NOT NULL,
available_at timestamptz NOT NULL,
published_at timestamptz,
publish_attempts integer NOT NULL DEFAULT 0,
last_error text,
created_at timestamptz NOT NULL
);
CREATE INDEX outbox_pending_idx ON outbox (available_at, id) WHERE published_at IS NULL;
CREATE INDEX outbox_published_idx ON outbox (published_at) WHERE published_at IS NOT NULL;
-- Operator-visible history of every state change of a job (including explicit replay).
CREATE TABLE job_audit (
id bigserial PRIMARY KEY,
job_id uuid NOT NULL REFERENCES attachment_jobs (id) ON DELETE CASCADE,
attachment_id uuid NOT NULL,
action text NOT NULL,
actor text NOT NULL,
attempt integer NOT NULL,
detail jsonb NOT NULL DEFAULT '{}'::jsonb,
created_at timestamptz NOT NULL
);
CREATE INDEX job_audit_job_idx ON job_audit (job_id, id);