ЛР 5 — БПИ2304 — Ремез — на 5 #71

Closed
OlegRood wants to merge 1 commits from OlegRood/mtusi2026lab5:solution/BPI2304-remez into main
163 changed files with 17194 additions and 24 deletions
+6 -1
View File
@@ -19,7 +19,12 @@ S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
MAX_UPLOAD_BYTES=10485760
MAX_IMAGE_PIXELS=40000000
BROKER_URL_FILE=/run/secrets/broker_url
WORKER_CONCURRENCY=2
WORKER_CONCURRENCY=1
WORKER_PREFETCH=1
JOB_MAX_ATTEMPTS=5
JOB_TIMEOUT_SECONDS=60
JOB_LEASE_SECONDS=90
JOB_RETRY_BASE_SECONDS=2
STAGING_RETENTION_SECONDS=86400
CLEANUP_GRACE_SECONDS=600
CLEANUP_INTERVAL_SECONDS=5
+21
View File
@@ -0,0 +1,21 @@
name: Lab 5 application
on: [push, pull_request]
permissions:
contents: read
jobs:
compose-smoke:
runs-on: ubuntu-latest
timeout-minutes: 20
env:
COMPOSE_PROJECT_NAME: lab5-ci
steps:
- uses: actions/checkout@v4
- name: Initialize disposable configuration
run: sh scripts/init-local.sh
- name: Build and start
run: docker compose --env-file .env up -d --build --wait --wait-timeout 180
- name: Run contract smoke
run: make check test BASE_URL=http://127.0.0.1:8085
- name: Remove the CI project
if: always()
run: docker compose --env-file .env down --volumes
+45
View File
@@ -0,0 +1,45 @@
FROM golang:1.27.1-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY cmd/api/ ./cmd/api/
COPY cmd/jobs/ ./cmd/jobs/
COPY cmd/relay/ ./cmd/relay/
COPY internal/ ./internal/
RUN --mount=type=cache,id=highload-go-build,target=/root/.cache/go-build,sharing=locked \
CGO_ENABLED=0 GOOS=linux go build -p 2 -trimpath -o /app/server ./cmd/api && \
CGO_ENABLED=0 GOOS=linux go build -p 2 -trimpath -o /app/jobs ./cmd/jobs && \
CGO_ENABLED=0 GOOS=linux go build -p 2 -trimpath -o /app/relay ./cmd/relay
FROM alpine:3.22 AS runtime
RUN addgroup -S app && adduser -S app -G app
WORKDIR /app
USER app
FROM runtime AS cleanup
COPY --from=builder /app/jobs ./jobs
ENTRYPOINT ["/app/jobs"]
CMD ["cleanup"]
FROM runtime AS relay
COPY --from=builder /app/relay ./relay
CMD ["./relay"]
FROM runtime AS api
COPY --from=builder /app/server ./server
EXPOSE 8080
HEALTHCHECK --interval=10s --timeout=3s --start-period=3s --retries=3 \
CMD wget -q -O /dev/null http://127.0.0.1:8080/health/ready || exit 1
CMD ["./server"]
+20
View File
@@ -0,0 +1,20 @@
FROM golang:1.27.1-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY cmd/migrate/ ./cmd/migrate/
COPY db/migrations/ ./db/migrations/
RUN --mount=type=cache,id=highload-go-build,target=/root/.cache/go-build,sharing=locked \
CGO_ENABLED=0 GOOS=linux go build -p 2 -trimpath -o /app/migrate ./cmd/migrate
FROM alpine:3.22
RUN addgroup -S app && adduser -S app -G app
WORKDIR /app
COPY --from=builder /app/migrate ./migrate
USER app
ENTRYPOINT ["/app/migrate"]
+23
View File
@@ -0,0 +1,23 @@
FROM golang:1.27.1-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY cmd/worker/ ./cmd/worker/
COPY internal/ ./internal/
RUN --mount=type=cache,id=highload-go-build,target=/root/.cache/go-build,sharing=locked \
CGO_ENABLED=0 GOOS=linux go build -p 2 -trimpath -o /app/worker ./cmd/worker
FROM alpine:3.22@sha256:49340b346798875b5592e5d420241f2f9cfba5f47e62ebb1446a1b34734e898f
RUN apk add --no-cache imagemagick imagemagick-jpeg imagemagick-webp \
&& addgroup -S app && adduser -S -G app app
COPY infra/imagemagick/policy.xml /etc/ImageMagick-7/policy.xml
COPY --from=builder /app/worker /usr/local/bin/worker
WORKDIR /tmp
USER app
CMD ["/usr/local/bin/worker"]
+133 -23
View File
@@ -1,37 +1,147 @@
# Отчёт — лабораторная 5
# Лабораторная 5. Очереди и асинхронная обработка изображений
- Автор, группа: TODO
- Целевая оценка; выбранный трек 5, если нужен: TODO
- Commit/PR: TODO после публикации
- Использованные источники, библиотеки и помощь ИИ: TODO
- Автор: Олег Ремез, БПИ2304.
- Целевая оценка «5», трек 2 по идемпотентности версий pipeline.
- Ветка: solution/BPI2304-remez. База PR: main репозитория mtusi2026/mtusi2026lab5.
- Использованы Go, PostgreSQL/pgx/sqlc, RabbitMQ/amqp091-go, Redis, HAProxy, RustFS/AWS SDK и ImageMagick. Codex участвовал в проектировании, реализации и подготовке отчёта. Чат «Лаба 5 тесты» подготовил и выполнил независимые проверки. [Подтверждения доставки RabbitMQ](https://www.rabbitmq.com/docs/confirms), [политика ImageMagick](https://imagemagick.org/security-policy/), [параметры WebP](https://imagemagick.org/webp/).
## Запуск из чистого clone
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
Нужны Docker с Compose v2, OpenSSL и make. Тесты проверены с Python 3.14.6. На macOS с системным Python 3.9 укажите `PYTHON=python3.14` в командах make, так как парсер поставляемых тестов может отклонить допустимую дробную часть RFC3339 длиннее шести цифр. Go 1.27.1 и Alpine 3.22 используются при сборке. ImageMagick с поддержкой JPEG и WebP входит в образ worker. Версии RabbitMQ, HAProxy и RustFS и digest образов PostgreSQL/Redis заданы в compose.yaml. Все подключения для демонстрации используют синтетические данные и локальный HTTP.
## Архитектура и решения
```sh
sh scripts/init-local.sh
docker compose --env-file .env config --quiet
docker compose --env-file .env up -d --build --wait
make check
make test BASE_URL=http://127.0.0.1:8085
make test-persistence BASE_URL=http://127.0.0.1:8085 ACTION_SCRIPT=scripts/restart.sh
make test-failover BASE_URL=http://127.0.0.1:8085 ACTION_SCRIPT=scripts/stop-one.sh
docker compose --env-file .env up -d --no-deps api
```
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab4, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
init-local.sh создаёт локальный .env, пароли и URL подключений в secrets/. Запуск использует мигратор Goose и приватную настройку S3. Процессы API, worker и relay стартуют после успешной миграции. Compose подключает Redis для сессий. API readiness проверяет PostgreSQL, Redis и S3. При недоступном RabbitMQ API всё ещё может сохранить работу в outbox. Сменить локальные порты можно через LAB5_HTTP_PORT и LAB5_POSTGRES_PORT. Для безопасной остановки использовать docker compose --env-file .env stop. Команда down --volumes удаляет данные проекта и годится только для изолированного одноразового стенда.
## Доказательства критериев
## Что происходит с вложением
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
| --- | --- | --- | --- |
| Публичный smoke | make test | TODO | TODO |
| Остальные пункты уровня 3 | TODO | TODO | TODO |
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
Синхронный путь API принимает PNG/JPEG, проверяет размер, MIME, имя, доступ и размеры картинки, вычисляет SHA-256. Под блокировкой ключа сохраняет байты в приватный S3 staging, затем в одной транзакции записывает attachment, job и событие outbox. После COMMIT API выдаёт 202 со снимком queued. Процесс обработки ищет исходник по ключу из PostgreSQL. Пользователь может отправить сообщение с queued или processing-вложением. Для failed новое прикрепление даёт 409, а повтор уже принятого POST с тем же ключом возвращает сохранённое сообщение после проверки доступа. Скачивание доступно участникам чата после ready.
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
Relay выбирает готовое событие под блокировкой строки, публикует persistent JSON с mandatory в durable очередь RabbitMQ и отмечает sent_at после confirm и проверки маршрутизации. Если процесс убит между публикацией и коммитом, тот же job_id может приехать повторно. Содержимое файла и секреты в сообщении отсутствуют.
## Эксперименты и отказы
Worker захватывает lease и увеличивает счётчик начатых попыток. Он сверяет исходные байты с SHA-256, готовит варианты и записывает оригинал и результаты в S3 под отдельным префиксом поколения lease. PostgreSQL публикует оригинал, метаданные всех вариантов и статус ready одной транзакцией под проверкой поколения и срока lease. Затем worker подтверждает AMQP delivery. Доставка готовой job проверяет опубликованный результат и не создаёт новую попытку. Восстановитель пишет одно событие на каждое истёкшее поколение lease. Старое поколение не может записать поверх результата нового.
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
## Варианты, лимиты и ошибки
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
| --- | --- | --- | --- |
| TODO | TODO | TODO | TODO |
| Имя | Формат и параметры |
| --- | --- |
| original | Неизменённый PNG или JPEG с исходным SHA-256 |
| thumbnail | JPEG quality 85, вписан в 256×256, меньшие изображения сохраняют размер, результат округляется вниз, alpha на белом |
| crop | JPEG quality 85, центральный квадрат 128×128, допускается увеличение, alpha на белом |
| optimized | WebP quality 80, method 4, вписан в 1280×1280 без увеличения, прозрачность сохраняется |
## Ограничения и следующие шаги
Для вариантов применяется EXIF orientation перед геометрией, а затем удаляются метаданные через -strip. Все изменения относятся к вариантам, исходный файл скачивается побайтно. Не гарантируется, что оптимизированный вариант каждого маленького файла займёт меньше места.
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
Таблица описывает v1, которую получает обычный upload. В v2 JPEG кодируется с quality 90, WebP с quality 72. Геометрия, форматы и оригинальные байты сохраняются. Версия рецепта задаётся `pipeline_version`, для обеих версий формат сообщения брокера остаётся `attachment.process.v1`.
HTTP принимает до 10 МиБ и 40 млн пикселей. Worker повторно проверяет размер, заголовок и SHA-256, ограничивает время отдельного Go-контекста и убивает дочерний ImageMagick при отмене. Compose задаёт worker 1,5 ГиБ RAM, 2 CPU и tmpfs /tmp 768 МиБ. Политика ImageMagick задаёт один поток, 256 МиБ памяти и mmap, 512 МиБ диска для pixel cache и 45 секунд на команду, разрешает JPEG, PNG и WebP. Граничный файл, исчерпавший ресурс конкретного стенда, получает ошибку обработки. Worker по умолчанию использует одну активную обработку и prefetch 1 на consumer.
Изменяемые параметры worker: WORKER_CONCURRENCY, WORKER_PREFETCH, JOB_TIMEOUT_SECONDS, JOB_LEASE_SECONDS, JOB_MAX_ATTEMPTS, JOB_RETRY_BASE_SECONDS. По умолчанию timeout обработки 60 секунд, lease 90 секунд, максимум 5 начатых обработок. Повторы не расходуют попытку, пока заново не получен lease. Для временного сбоя задержка равномерно выбирается от 1 секунды до min(30 секунд, 2 секунды × 2^(номер попытки − 1)), с ограничением роста степени. Для постоянной ошибки, например несоответствия исходника, попытки прекращаются. Для исчерпания временных повторов код retry_exhausted. Историю попыток и операторские действия храним в PostgreSQL. Неразбираемые сообщения идут в очередь image.invalid.
## Операции с failed и staging
```sh
docker compose exec -T attachment-cleanup /app/jobs failed
request_id=$(python3 -c 'import uuid; print(uuid.uuid4())')
# Установите job_id в ID из вывода failed.
docker compose exec -T attachment-cleanup /app/jobs replay \
-job-id "$job_id" -request-id "$request_id" -operator student \
-reason 'исправлена причина ошибки' -extra-attempts 1
docker compose exec -T attachment-cleanup /app/jobs cleanup -once -dry-run
```
Replay проверяет failed и наличие исходника, записывает оператора, причину и ограниченный бюджет в одной транзакции с новым outbox. Повтор того же request-id и replay уже готовой job не меняют результат. Уборка удаляет staging после ready, исходники failed — после STAGING_RETENTION_SECONDS, бесхозный staging и неопубликованные ключи попыток — после CLEANUP_GRACE_SECONDS. Учитываются блокировка загрузки, lease и ссылки на оригинал и варианты. По умолчанию retention 24 часа, grace 10 минут, проход каждые 5 секунд. Очередь сама по себе может ждать дольше retention, queued и processing исходники сохраняются.
Оператор переключает конкретное вложение на второй рецепт по ID.
```sh
docker compose exec -T attachment-cleanup /app/jobs upgrade \
-attachment-id "$attachment_id" -to-version 2
```
При первом принятом переходе команда возвращает `queued: true` и ID новой job. Повтор возвращает `queued: false` с тем же ID. Готовое v1-вложение можно переключить после удаления staging. Источник для v2 тогда берётся из опубликованного оригинала. Во время перехода тот же ID вложения снова проходит `queued` и `processing`, скачивание отвечает 409. После ready API показывает набор текущего рецепта. Ссылки сообщений и SHA-256 оригинала сохраняются.
## Доказательства и сценарий защиты
Для управляемых отказов запускайте отдельную копию Compose по [инструкции приёмочных тестов](student-tests/lab5/README.md). В ней изолированы порты, volumes и каталог маркеров. Файлы `.arm` включают паузу один раз. Файл `.hit` связывает окно отказа с конкретным UUID. В обычном Compose паузы выключены. [Описание точек отказа](student-tests/lab5/FAULT-HOOKS.md).
На защите сначала запустите `make test`, затем воспроизведите `durable-broker-worker-stop-and-redelivery` и `crash-COMMIT-before-publish`. Покажите один и тот же ID до и после рестартов, 409 до готовности и скачивание вариантов после неё. В `crash-object-before-ack-lease` сравните историю попыток и опубликованные ключи. В `backoff-jitter-permanent-failed-replay` покажите интервалы, причину `failed` и единственную запись аудита replay.
Publisher confirm показывает, что брокер принял публикацию. Consumer ack закрывает конкретную доставку после устойчивого результата или сохранённого решения о retry/failed. При 202 уже существуют staging и атомарно сохранённые attachment, job и outbox. При redelivery таблица jobs определяет текущий lease и состояние, а ключ `(attachment_id, pipeline_version, name)` определяет уникальный опубликованный вариант. При истечении lease восстановитель публикует одну новую запись outbox на поколение. Предыдущая попытка теряет право на COMMIT, даже если её процесс ещё работает.
[Протокол чистого запуска профиля «4»](evidence/lab5/clean-run.md). На пустых volumes были применены все 7 миграций того снимка. `make test`, `make test-persistence` и `make test-failover` прошли. После сценариев отказа лог мигратора остался прежним. При failover было 111 успешных записей и одна переходная ошибка, запись восстановилась за 3,04 с. Отдельно прошли `make check`, полная валидация контрактов, `sqlc compile`, `go vet` и `go test`.
На чистом стенде проверены ограничение загрузки по байтам и пикселям, ошибки для повреждённых данных, доступ к вложениям и результат запроса через две реплики. Дополнительные PNG 1600×800 и JPEG с EXIF orientation 6 дали WebP 1280×640 и 640×1280. Размеры проверены декодером по скачанным байтам. Оригиналы совпали побайтно, контрольная строка метаданных и EXIF отсутствовали во всех вариантах. [Выдержки HTTP-проверок](evidence/lab5/http-and-geometry.jsonl).
Отдельный SIGKILL API после записи staging остановил загрузку до 202. Изолированный тест подтвердил наличие объекта в S3, ноль строк attachment/job/outbox, удаление бесхозного объекта уборщиком и 404 для того же ID после подъёма API. [Результат аварии перед COMMIT](evidence/lab5/crash-before-commit.jsonl).
### Независимая приёмка очереди и worker
| Сценарий | Наблюдавшийся результат |
| --- | --- |
| Остановленный worker, рестарты API и RabbitMQ | Upload вернул 202, сообщение с вложением сохранилось, скачивание до ready вернуло 409. У durable очереди до и после рестарта было одно ожидающее сообщение и ноль consumers. Relay во время рестарта был остановлен. После запуска worker то же вложение стало ready, дубль не начал новую попытку. |
| SIGKILL relay после COMMIT до publish | Перед отказом существовал один outbox со статусом ожидания, опубликованных событий было ноль. После рестарта та же job завершилась первой попыткой, опубликовала четыре объекта, staging удалён. |
| SIGKILL relay после confirm до отметки outbox | Повторная публикация завершилась одним набором результатов и пустой очередью. |
| SIGKILL worker после записи объектов до COMMIT результата | У первой попытки было четыре объекта в S3 и статус processing. Истёк lease поколения 1, поколение 2 стало ready. После уборки остались четыре опубликованных объекта второй попытки. |
| SIGKILL worker после ready до ack | Redelivery сохранила поколение и историю единственной успешной попытки. RabbitMQ подтвердил отсутствие ожидающих и неподтверждённых доставок. |
| Дедлайн и лимиты | При test timeout 14 с и lease 18 с первая попытка закончилась `processing_timeout`, вторая стала ready. Наблюдались один consumer с manual ack и prefetch 1, 1,5 ГиБ лимита worker, ImageMagick 256 МиБ RAM, 256 МиБ mmap, 512 МиБ disk, один поток и 45 с на команду. |
| Временные и постоянные ошибки, failed и replay | В двух опытах с недоступным S3 задержки составили 1806/1252 и 1705/2627 мс, затем `retry_exhausted` на третьей попытке. Повреждённый checksum привёл к одному `invalid_image`. Операторский replay после устранения причин дал ready и одну запись аудита на job. Replay готовой job сохранил результат и число попыток. |
| Некорректные задания в брокере | Три сообщения с несовпадающими идентификаторами или версией попали в `image.invalid`. Готовый результат и история попыток сохранились. |
[Наблюдения из PostgreSQL, RabbitMQ и S3](evidence/lab5/pipeline-faults.jsonl). После аварийной серии мигратор по-прежнему имел ровно один успешный запуск и по одной записи каждой версии. При отключении доступа к хранилищам одна API продолжала отвечать `live=200`, сообщала `ready=503`, HAProxy направил все 29 запросов контрольного окна на вторую API. После восстановления трафик снова шёл на обе. [Наблюдения проверки readiness](evidence/lab5/readiness.jsonl).
Интеграционные Go-тесты с отдельными PostgreSQL и Redis прошли 53 сценария, включая откат транзакций, проверку доступа при replay и совместимость старых сообщений. Три дополнительных Redis-теста требуют собственных хуков рестарта и были пропущены. Отдельные сквозные сценарии подтвердили сохранность сессии, данных и ключа идемпотентности при общем рестарте. Дополнительный сценарий rolling update ожидает хука замены реплики. [Воспроизведение независимого прогона](student-tests/lab5/README.md).
На основном стенде профиля «4» прямой запрос в RustFS без подписи на существующий объект вернул 403.
### Нагрузка на API при отказе реплик
Независимый прогон выполнил 6 параллельных потоков, каждый чередовал POST сообщения и GET профиля. Прогрев длился 2 с. В окне 28,37 с скрипт выполнил 15 861 попытку. Успешных было 15 843, или 558,37 запроса/с. Ошибок было 18 (0,1135%). Для успешных запросов p50/p95/p99 составили 5,516 / 21,228 / 47,711 мс. Сохранились все 7 926 подтверждённых записей. После SIGTERM запись и чтение восстановились за 3,104 с и 3,301 с для двух реплик.
Снимок под нагрузкой показал 36,80% CPU и 15,35 МиБ у первой API, 37,26% и 12,92 МиБ у второй, 32,09% и 77,19 МиБ у HAProxy. Лимиты одной API — 2 CPU и 512 МиБ. Замер сделан на Apple M2 с 8 ГиБ RAM, Docker VM получила 8 CPU и 4 ГБ RAM. Прикладная нагрузка и отказ измерены на синтетическом чате. Каждый клиент ждал ответа предыдущего запроса. Перцентили учитывают успешные ответы, а пропускная способность относится к одному этому профилю. [Сырые метрики](evidence/lab5/api-failover.jsonl).
## Трек 2. Идемпотентность версий pipeline
**Гипотеза.** Если публикация проверяет и текущую версию, и поколение действующего lease, а физические ключи S3 включают обе величины, перестановка сообщений и запоздавшие попытки сохранят готовый результат v2. После падения worker принятая работа текущей версии продолжится с новым поколением.
1. PostgreSQL хранит одно задание на `(attachment_id, pipeline_version)` и один вариант каждого имени на эту пару. Для переключения оператор берёт ту же блокировку строки v1, которую используют финализация, retry и очистка. У смены рецепта и попытки v1 есть один порядок фиксации.
2. В одной транзакции переход закрывает старый lease и откладываемый retry, завершает активную попытку как `superseded`, меняет версию вложения и записывает v2 job с outbox. Идентификатор вложения и строки сообщений сохраняются. Завершённая v1 остаётся в истории.
3. Worker может записать файлы только под префиксом своей версии и поколения. Публикация в PostgreSQL проходит под проверкой актуальной версии, поколения, срока lease и наличия метаданных всех трёх вариантов. В той же транзакции фиксируются оригинал версии, итоговый указатель и ready. GET читает согласованный набор в одной snapshot-транзакции.
4. Дубль текущей ready job подтверждается после проверки сохранённых объектов. Валидная доставка v1 после переключения подтверждается по уже зафиксированному переходу. Непубликованные файлы старых попыток удаляются по grace, опубликованные наборы обеих версий защищены ссылками. История увеличивает объём хранения на один набор при переходе с готовой v1.
В каждом сценарии загружается новый экземпляр одного PNG. Скрипт фиксирует реальное сообщение и состояние PostgreSQL до и после вмешательства. Он скачивает исходник и варианты по HTTP, перечитывает объекты по сохранённым ключам S3, сравнивает SHA-256, размеры и историю попыток. Он также проверяет отсутствие лишних ключей после уборки. Для перестановки скрипт откладывает исходный outbox v1 и разрешает его публикацию после ready v2. Для падений используется SIGKILL живого worker в точках до первого S3 PUT и после записи всех объектов. Для гонки используются два процесса worker. [Методика и команда запуска](student-tests/lab5/VERSIONED.md).
15 ноября 2026 года тестовик выполнил три полные серии из пяти сценариев на изолированном Docker-проекте со схемой лабораторной v8, lease 36 с, deadline 30 с и grace очистки 1 с. Получено **15 из 15 успешных опытов**. Время приведено как минимум, медиана и максимум. Для падений оно отсчитывается от отправки SIGKILL, для остальных сценариев охватывает ожидание ready при переходе или после запуска relay.
| Сценарий | Успех | До ready, с | Наблюдение |
| --- | ---: | ---: | --- |
| Дубль v2 и запоздавшая доставка v1 | 3/3 | 0,142 / 0,161 / 0,162 | По одной успешной попытке на версию. Переотправленные валидные envelopes прошли RabbitMQ, ready и SHA v2 сохранились, DLQ не выросла. |
| Перестановка v2 → v1 | 3/3 | 0,377 / 0,432 / 0,960 | Исходный outbox v1 прошёл брокер после ready v2. У v1 ноль попыток, у v2 одна. |
| SIGKILL до первого S3 PUT | 3/3 | 31,613 / 31,802 / 32,085 | Перед падением ноль объектов нового поколения. Первая попытка v2 истекла, вторая завершила ту же job. |
| SIGKILL после S3 PUT | 3/3 | 31,915 / 33,189 / 33,353 | Перед падением четыре объекта первой попытки. Следующее поколение завершило job, старая попытка очищена. |
| Возобновление старого worker v1 после ready v2 | 3/3 | 0,161 / 0,419 / 0,873 | Живой процесс получил отказ в финализации за 19,52–21,47 с до прежнего дедлайна. Итоговые ключи v2 сохранились, сироты v1 удалены. |
Все 15 v2 получили одинаковые три SHA-256 на одном исходнике. Для готовой v1 контрольные SHA отличались по каждому из трёх вариантов, оригинальные байты совпадали. Два конкурентных вызова `jobs upgrade` после очистки staging отдельно подтвердили один новый job ID, один outbox и ровно один ответ с `queued: true`. [Агрегаты](evidence/lab5/track-2/summary.json), [сырые наблюдения](evidence/lab5/track-2/versioned.jsonl.gz), [параллельный upgrade](evidence/lab5/track-2/concurrent-upgrade.jsonl).
После перехода на схему v8 снова прошли чистый bootstrap, `make test`, проверки сохранности и failover, лимиты HTTP и скачивание всех вариантов. Контракт и публичные тесты не изменились. [Регрессионные проверки после v8](evidence/lab5/track-2/regression.jsonl.gz).
При обновлении основного стенда 16 ноября мигратор применил только `00008`. До обновления в PostgreSQL было шесть готовых вложений, после перехода у каждого сохранилась ссылка на опубликованный оригинал. На `127.0.0.1:8085` прошли smoke, readiness и проверки со штатными скриптами рестарта и отказа. При failover запись восстановилась за 2,599 с, лог мигратора после рестартов содержал тот же успешный запуск. [Переход основного стенда v7 → v8](evidence/lab5/track-2/migration-live.json).
RabbitMQ обеспечивает доставку at least once. Одна и та же job появлялась в очереди повторно, а после падения worker повторно вычислял и записывал объекты под другим поколением. Гарантия приложения относится к единственной зафиксированной публикации результата для текущей версии. Эти наблюдения совместимы с повторной доставкой и выполнением. Успешное восстановление зависит от сохранности PostgreSQL, S3, тома RabbitMQ и наличия рабочего relay и worker.
## Ограничения
Проверяем один Docker-хост. Две реплики дают проверяемый отказ одной API, а данные broker переживают рестарт с существующим volume. Потеря самого хоста или всех volumes остаётся общей точкой отказа. Очередь даёт доставку at least once. Идемпотентность реализована приложением на уровне итогового набора объектов и транзакции, поэтому повтор можно наблюдать, хотя результат один. Уборка проходит по страницам S3, её стоимость растёт с объёмом bucket.
Проверки трека 2 выполнены на одном образе ImageMagick, одном PNG и одном Docker-хосте. Число повторов позволяет воспроизвести инварианты в указанных окнах отказа. Интервалы восстановления относятся к заданному профилю lease и локальному стенду.
[Сценарии для защиты трека 2](student-tests/lab5/VERSIONED.md).
+155
View File
@@ -0,0 +1,155 @@
package main
import (
"context"
"errors"
"highload/internal/config"
"highload/internal/database"
"highload/internal/database/sqlc"
"highload/internal/repository/postgres"
"highload/internal/repository/redis"
"highload/internal/service"
"highload/internal/storage/s3"
httpdeliver "highload/internal/transport/http"
"highload/internal/transport/http/handler"
"highload/internal/transport/http/middleware"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
cfg, err := config.Load()
if err != nil {
slog.Error("invalid configuration", "error", err)
os.Exit(1)
}
sessionStore := cfg.Session.Store
if sessionStore != "redis" {
slog.Error("session store is "+sessionStore+"must redis", "error", nil)
os.Exit(1)
}
databaseURL := cfg.Database.PostgresURL
pool, err := database.NewPostgres(context.Background(), databaseURL)
if err != nil {
slog.Error("postgres error", "error", err)
os.Exit(1)
}
defer pool.Close()
if os.Getenv("PG_POOL_DIAGNOSTICS") == "1" {
diagnosticsCtx, stopDiagnostics := context.WithCancel(context.Background())
defer stopDiagnostics()
go logPoolStats(diagnosticsCtx, pool)
}
queries := sqlc.New(pool)
redisAddr := cfg.Database.RedisAddr
redisClient, err := database.NewRedis(context.Background(), redisAddr)
if err != nil {
slog.Error("redis error", "error", err)
os.Exit(1)
}
defer redisClient.Close()
instanceID, err := os.Hostname()
if err != nil || instanceID == "" {
slog.Error("cannot get instance ID", "error", err)
os.Exit(1)
}
s3Storage, err := s3.NewStorage(context.Background(), &cfg.S3)
if err != nil {
slog.Error("cannot create new storage", "error", err)
os.Exit(1)
}
userRepo := postgres.NewUserRepository(queries)
chatRepo := postgres.NewChatRepository(queries, pool)
messageRepo := postgres.NewMessageRepository(queries, pool)
sessionRepo := redis.NewSessionRepository(redisClient)
attachmentRepo := postgres.NewAttachmentRepository(queries, pool, int32(cfg.JobMaxAttempts))
authService := service.NewAuthService(userRepo, sessionRepo, cfg.Session.TTL)
userService := service.NewUserService(userRepo)
chatService := service.NewChatService(chatRepo, userRepo, userService)
messageService := service.NewMessageService(messageRepo, chatService, userService)
attachmentService := service.NewAttachmentService(attachmentRepo, chatService, s3Storage)
healthHandler := handler.NewHealthHandler(pool, redisClient, s3Storage)
authHandler := handler.NewAuthHandler(authService)
userHandler := handler.NewUserHandler(userService)
chatHandler := handler.NewChatHandler(chatService)
messageHandler := handler.NewMessageHandler(messageService)
attachmentHandler := handler.NewAttachmentHandler(attachmentService)
mid := middleware.NewMiddleware(authService, instanceID)
router := httpdeliver.NewRouter(mid, authHandler, healthHandler, chatHandler, userHandler, messageHandler, attachmentHandler)
server := &http.Server{
Addr: net.JoinHostPort(cfg.HTTP.Host, cfg.HTTP.Port),
Handler: router.Handler(),
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 30 * time.Second,
}
listener, err := net.Listen("tcp", server.Addr)
if err != nil {
slog.Error("server failed to start", "error", err)
return
}
slog.Info("server started", "addr", server.Addr)
serverErr := make(chan error, 1)
go func() {
err := server.Serve(listener)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
serverErr <- err
slog.Error("server failed", "error", err)
}
}()
ctx, stop := signal.NotifyContext(
context.Background(),
os.Interrupt,
syscall.SIGTERM,
)
defer stop()
select {
case <-ctx.Done():
slog.Info("shutdown signal received")
case err := <-serverErr:
slog.Error("server failed", "error", err)
return
}
shutdownCtx, cancel := context.WithTimeout(
context.Background(),
15*time.Second,
)
defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil {
slog.Error("shutdown failed", "error", err)
}
}
+29
View File
@@ -0,0 +1,29 @@
package main
import (
"context"
"log/slog"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
// logPoolStats is opt-in diagnostics for the isolated scaling experiment.
// It contains only aggregate counters, never connection strings or SQL values.
func logPoolStats(ctx context.Context, pool *pgxpool.Pool) {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
s := pool.Stat()
slog.Info("pg_pool_stats", "max_conns", s.MaxConns(),
"acquired", s.AcquiredConns(), "idle", s.IdleConns(),
"acquire_count", s.AcquireCount(), "empty_count", s.EmptyAcquireCount(),
"empty_wait_ns", s.EmptyAcquireWaitTime().Nanoseconds(),
"canceled_count", s.CanceledAcquireCount())
}
}
}
+164
View File
@@ -0,0 +1,164 @@
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"highload/internal/config"
"highload/internal/database"
"highload/internal/database/sqlc"
"highload/internal/repository/postgres"
"highload/internal/service"
"highload/internal/storage/s3"
"log/slog"
"os"
"os/signal"
"syscall"
"time"
"github.com/jackc/pgx/v5/pgconn"
)
type cleanupConfig struct {
once bool
dryRun bool
gracePeriod time.Duration
interval time.Duration
passTimeout time.Duration
reconcile bool
restoreID string
backupFile string
}
func main() {
var cfg cleanupConfig
flag.BoolVar(&cfg.once, "once", false, "Run once and exit")
flag.BoolVar(&cfg.dryRun, "dry-run", true, "Do not actually delete files")
flag.DurationVar(&cfg.gracePeriod, "grace-period", 10*time.Minute, "Grace period for old files")
flag.DurationVar(&cfg.interval, "interval", 5*time.Minute, "Interval between runs")
flag.DurationVar(&cfg.passTimeout, "pass-timeout", 2*time.Minute, "Timeout for a single pass")
flag.BoolVar(&cfg.reconcile, "reconcile", false, "Scan both stores once and print JSON observations")
flag.StringVar(&cfg.restoreID, "restore-id", "", "Restore the missing original for this attachment ID")
flag.StringVar(&cfg.backupFile, "backup-file", "", "Path to the original backup used for restore")
flag.Parse()
if cfg.gracePeriod <= 0 || cfg.interval <= 0 || cfg.passTimeout <= 0 {
slog.Error("all duration flags must be positive")
os.Exit(1)
}
if (cfg.restoreID == "") != (cfg.backupFile == "") || (cfg.reconcile && cfg.restoreID != "") {
slog.Error("use -reconcile alone, or supply both -restore-id and -backup-file")
os.Exit(1)
}
if err := run(cfg); err != nil {
slog.Error("attachment cleanup failed", "error", err)
os.Exit(1)
}
}
func run(cfg cleanupConfig) error {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
databaseURL, s3Config, err := config.LoadAttachmentCleanup()
if err != nil {
return fmt.Errorf("load cleanup configuration: %w", err)
}
startupCtx, cancelStartup := context.WithTimeout(ctx, 10*time.Second)
defer cancelStartup()
pool, err := database.NewPostgres(startupCtx, databaseURL)
if err != nil {
var configErr *pgconn.ParseConfigError
if errors.As(err, &configErr) {
return errors.New("invalid PostgreSQL connection string")
}
return fmt.Errorf("connect to PostgreSQL: %w", err)
}
defer pool.Close()
s3Storage, err := s3.NewStorage(startupCtx, &s3Config)
if err != nil {
return fmt.Errorf("create S3 client: %w", err)
}
cancelStartup()
attachmentRepo := postgres.NewAttachmentRepository(sqlc.New(pool), pool)
options := &service.AttachmentCleanupOptions{
GracePeriod: cfg.gracePeriod,
DryRun: cfg.dryRun,
ListTimeout: 5 * time.Second,
}
attachmentCleanupService := service.NewAttachmentCleanupService(attachmentRepo, s3Storage, options)
if cfg.reconcile || cfg.restoreID != "" {
passCtx, cancel := context.WithTimeout(ctx, cfg.passTimeout)
defer cancel()
reconciler := service.NewAttachmentReconciler(attachmentRepo, s3Storage, *options)
start := time.Now()
report := struct {
DryRun bool `json:"dry_run"`
DurationMS int64 `json:"duration_ms"`
Complete bool `json:"complete"`
Orphans *service.OrphanReport `json:"orphan_scan,omitempty"`
Missing *service.MissingReport `json:"missing_scan,omitempty"`
Restore string `json:"restore,omitempty"`
}{DryRun: cfg.dryRun}
var actionErr error
if cfg.reconcile {
missing, err := reconciler.ScanMissing(passCtx)
report.Missing = &missing
actionErr = err
if err == nil {
orphans, err := attachmentCleanupService.ScanOrphans(passCtx)
report.Orphans = &orphans
actionErr = err
}
} else {
file, err := os.Open(cfg.backupFile)
if err != nil {
return fmt.Errorf("open backup original: %w", err)
}
defer file.Close()
report.Restore, actionErr = reconciler.Restore(passCtx, cfg.restoreID, file)
}
report.DurationMS = time.Since(start).Milliseconds()
report.Complete = actionErr == nil
return errors.Join(actionErr, json.NewEncoder(os.Stdout).Encode(report))
}
runPass := func() error {
passCtx, cancelPass := context.WithTimeout(ctx, cfg.passTimeout)
defer cancelPass()
return attachmentCleanupService.RunOnce(passCtx)
}
if cfg.once {
return runPass()
}
if err := runPass(); err != nil && ctx.Err() == nil {
slog.Error("attachment cleanup pass failed", "error", err)
}
ticker := time.NewTicker(cfg.interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
if ctx.Err() != nil {
return nil
}
if err := runPass(); err != nil && ctx.Err() == nil {
slog.Error("attachment cleanup pass failed", "error", err)
}
}
}
}
+133
View File
@@ -0,0 +1,133 @@
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"strings"
"syscall"
"time"
"highload/internal/config"
"highload/internal/database"
"highload/internal/database/sqlc"
"highload/internal/pipeline"
"highload/internal/repository/postgres"
"highload/internal/storage/s3"
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if err := run(ctx, os.Args[1:]); err != nil {
slog.Error("attachment jobs command failed", "error", err)
os.Exit(1)
}
}
func run(ctx context.Context, args []string) error {
const usage = "usage: jobs failed | replay | upgrade | cleanup"
if len(args) == 0 {
return fmt.Errorf("%s", usage)
}
flags := flag.NewFlagSet(args[0], flag.ContinueOnError)
job := flags.String("job-id", "", "failed job ID")
request := flags.String("request-id", "", "unique operator request UUID")
operator := flags.String("operator", "", "operator ID")
reason := flags.String("reason", "", "replay reason")
extra := flags.Int("extra-attempts", 1, "budget for replay (1–5)")
attachment := flags.String("attachment-id", "", "attachment ID to upgrade")
version := flags.Int("to-version", 0, "target pipeline version (2)")
dry := flags.Bool("dry-run", false, "report eligible objects without deleting")
once := flags.Bool("once", false, "execute one cleanup pass")
if err := flags.Parse(args[1:]); err != nil {
return err
}
command := args[0]
if command != "failed" && command != "cleanup" && command != "replay" && command != "upgrade" {
return fmt.Errorf("%s", usage)
}
if command == "replay" && (*job == "" || *request == "" || strings.TrimSpace(*operator) == "" ||
strings.TrimSpace(*reason) == "" || *extra < 1 || *extra > 5) {
return fmt.Errorf("replay needs job-id, request-id, operator, reason and 1–5 extra-attempts")
}
if command == "upgrade" && (*attachment == "" || *version != 2) {
return fmt.Errorf("upgrade needs attachment-id and to-version=2")
}
attemptLimit := 5
if command == "upgrade" {
var err error
attemptLimit, err = config.EnvInt("JOB_MAX_ATTEMPTS", 5, 1, 20)
if err != nil {
return err
}
}
url, storageConfig, err := config.LoadAttachmentCleanup()
if err != nil {
return err
}
pool, err := database.NewPostgres(ctx, url)
if err != nil {
return fmt.Errorf("PostgreSQL unavailable")
}
defer pool.Close()
repo := postgres.NewAttachmentRepository(sqlc.New(pool), pool, int32(attemptLimit))
if command == "failed" {
dbCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
rows, err := repo.ListFailedJobs(dbCtx)
if err != nil {
return err
}
type failed struct {
JobID string `json:"job_id"`
AttachmentID string `json:"attachment_id"`
ErrorCode string `json:"error_code"`
Attempts int32 `json:"attempts"`
UpdatedAt time.Time `json:"updated_at"`
}
result := make([]failed, 0, len(rows))
for _, row := range rows {
result = append(result, failed{row.ID.String(), row.AttachmentID.String(), row.ErrorCode.String, row.AttemptsStarted, row.UpdatedAt.Time})
}
return json.NewEncoder(os.Stdout).Encode(result)
}
objects, err := s3.NewStorage(ctx, &storageConfig)
if err != nil {
return err
}
if command == "replay" {
changed, err := repo.ReplayFailed(ctx, *job, *request, *operator, *reason, int32(*extra), objects.Head)
if err != nil {
return err
}
return json.NewEncoder(os.Stdout).Encode(map[string]any{"job_id": *job, "replayed": changed})
}
if command == "upgrade" {
jobID, queued, err := repo.UpgradePipeline(ctx, *attachment, objects.Head)
if err != nil {
return err
}
return json.NewEncoder(os.Stdout).Encode(map[string]any{
"attachment_id": *attachment, "pipeline_version": *version, "job_id": jobID, "queued": queued,
})
}
cfg, err := config.LoadPipeline()
if err != nil {
return err
}
cleanup := pipeline.Cleanup{Repo: repo, Objects: objects, Grace: cfg.CleanupGrace, Retention: cfg.Retention, DryRun: *dry}
if *once {
count, err := cleanup.RunOnce(ctx)
if err != nil {
return err
}
return json.NewEncoder(os.Stdout).Encode(map[string]any{"objects": count, "dry_run": *dry})
}
cleanup.Serve(ctx, cfg.CleanupPeriod)
return nil
}
+254
View File
@@ -0,0 +1,254 @@
package main
import (
"encoding/json"
"errors"
"io"
"log/slog"
"os"
"strings"
"testing"
"time"
"highload/db/migrations"
"highload/internal/database/sqlc"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/pressly/goose/v3"
)
func TestAsyncAttachmentsMigrationAndJobTransitions(t *testing.T) {
db, ctx := migrationPostgresFixture(t)
provider, err := goose.NewProvider(goose.DialectPostgres, db, migrations.Files,
goose.WithDisableGlobalRegistry(true), goose.WithSlog(slog.New(slog.NewTextHandler(io.Discard, nil))))
if err != nil {
t.Fatal(err)
}
if _, err := provider.UpTo(ctx, 6); err != nil {
t.Fatal(err)
}
const original = "attachments/step-two-original"
_, err = db.ExecContext(ctx, `
INSERT INTO users (id, username, display_name, password_hash) VALUES
('00000000-0000-4000-8000-000000000001', 'step_two', 'Step two', 'synthetic');
INSERT INTO chats (id, title) VALUES ('00000000-0000-4000-8000-000000000002', 'Step two');
INSERT INTO chat_members VALUES
('00000000-0000-4000-8000-000000000002', '00000000-0000-4000-8000-000000000001');
INSERT INTO messages (id, chat_id, sender_id, text) VALUES
('00000000-0000-4000-8000-000000000003', '00000000-0000-4000-8000-000000000002',
'00000000-0000-4000-8000-000000000001', 'Old message');
INSERT INTO attachments (id, chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256) VALUES
('00000000-0000-4000-8000-000000000004', '00000000-0000-4000-8000-000000000002',
'00000000-0000-4000-8000-000000000001', 'old.png', 'attachments/step-two-original',
'image/png', 123, repeat('a', 64));
INSERT INTO messages_attachments (message_id, attachment_id, chat_id, position) VALUES
('00000000-0000-4000-8000-000000000003', '00000000-0000-4000-8000-000000000004',
'00000000-0000-4000-8000-000000000002', 0);
`)
if err != nil {
t.Fatal(err)
}
if _, err := provider.UpTo(ctx, 7); err != nil {
t.Fatal(err)
}
if _, err := provider.Down(ctx); err != nil {
t.Fatal(err)
}
var preservedKey string
if err := db.QueryRowContext(ctx, "SELECT object_key FROM attachments JOIN messages_attachments ON attachment_id = id").Scan(&preservedKey); err != nil || preservedKey != original {
t.Fatalf("rollback lost the legacy attachment reference: %v", err)
}
if _, err := provider.UpTo(ctx, 7); err != nil {
t.Fatal(err)
}
if _, err := provider.UpTo(ctx, lab5MigrationCount); err != nil {
t.Fatal(err)
}
dsn, err := os.ReadFile(os.Getenv("DATABASE_URL_FILE"))
if err != nil {
t.Fatal("cannot read test connection configuration")
}
pool, err := pgxpool.New(ctx, string(dsn))
if err != nil {
t.Fatal("cannot configure test pool")
}
defer pool.Close()
q := sqlc.New(pool)
var jobID, attachmentID pgtype.UUID
var payload []byte
err = pool.QueryRow(ctx, `
SELECT j.id, a.id, o.payload
FROM messages_attachments AS m
JOIN attachments AS a ON a.id = m.attachment_id
JOIN attachment_jobs AS j ON j.attachment_id = a.id
JOIN outbox AS o ON o.job_id = j.id
WHERE a.status = 'queued' AND a.object_key = $1 AND a.staging_key = $1
AND o.dedup_key = 'initial/' || j.id
`, original).Scan(&jobID, &attachmentID, &payload)
if err != nil {
t.Fatalf("legacy attachment was not queued with its initial event: %v", err)
}
var event struct {
JobID string `json:"job_id"`
Attempt int `json:"attempt"`
CreatedAt time.Time `json:"created_at"`
}
if err := json.Unmarshal(payload, &event); err != nil || event.JobID != jobID.String() ||
event.Attempt != 1 || event.CreatedAt.IsZero() {
t.Fatalf("invalid backfill payload: %v", err)
}
start := make(chan struct{})
results := make(chan error, 2)
claim := sqlc.ClaimJobParams{
JobID: jobID, NewLeaseUntil: pgtype.Timestamptz{Time: time.Now().Add(time.Minute), Valid: true},
}
for range 2 {
go func() {
<-start
_, err := q.ClaimJob(ctx, claim)
results <- err
}()
}
close(start)
winners := 0
for range 2 {
switch err := <-results; {
case err == nil:
winners++
case errors.Is(err, pgx.ErrNoRows):
default:
t.Fatalf("concurrent claim failed: %v", err)
}
}
if winners != 1 {
t.Fatalf("gave the lease to %d workers", winners)
}
exec := func(query string, args ...any) {
t.Helper()
if _, err := pool.Exec(ctx, query, args...); err != nil {
t.Fatal(err)
}
}
exec("UPDATE attachment_jobs SET lease_until = clock_timestamp() - interval '1 second' WHERE id = $1", jobID)
for _, want := range []int{1, 0} {
recovery, err := q.EnqueueExpiredLeases(ctx, 10)
if err != nil || len(recovery) != want {
t.Fatalf("recovery events = %d, want %d: %v", len(recovery), want, err)
}
if want == 1 {
exec("UPDATE outbox SET sent_at = now() WHERE id = $1", recovery[0].ID)
}
}
row, err := q.ClaimJob(ctx, claim)
if err != nil || row.LeaseGeneration != 2 || row.AttemptsStarted != 2 {
t.Fatalf("expired lease was not replaced correctly: %v", err)
}
fail := sqlc.FailJobParams{JobID: jobID, LeaseGeneration: 1, ErrorCode: "invalid_image"}
if n, err := q.FailJob(ctx, fail); err != nil || n != 0 {
t.Fatalf("stale worker could fail attachment: rows=%d, err=%v", n, err)
}
retry := sqlc.ScheduleRetryParams{
JobID: jobID, LeaseGeneration: 2, ErrorCode: "s3_unavailable",
AvailableAt: pgtype.Timestamptz{Time: time.Now().Add(time.Minute), Valid: true},
}
if n, err := q.ScheduleRetry(ctx, retry); err != nil || n != 1 {
t.Fatalf("retry could not be scheduled: rows=%d, err=%v", n, err)
}
if _, err := q.ClaimJob(ctx, claim); !errors.Is(err, pgx.ErrNoRows) {
t.Fatalf("worker bypassed the retry delay: %v", err)
}
exec("UPDATE attachment_jobs SET next_attempt_at = now(), max_attempts = 3 WHERE id = $1", jobID)
row, err = q.ClaimJob(ctx, claim)
if err != nil || row.LeaseGeneration != 3 {
t.Fatalf("due retry could not claim job: %v", err)
}
exec("UPDATE attachment_jobs SET lease_until = clock_timestamp() - interval '1 second' WHERE id = $1", jobID)
if _, err := q.ClaimJob(ctx, claim); !errors.Is(err, pgx.ErrNoRows) {
t.Fatalf("worker bypassed the attempt limit: %v", err)
}
fail.LeaseGeneration = 3
fail.ErrorCode = "retry_exhausted"
if n, err := q.FailJob(ctx, fail); err != nil || n != 1 {
t.Fatalf("exhausted job could not be failed: rows=%d, err=%v", n, err)
}
var requestID pgtype.UUID
if err := requestID.Scan("00000000-0000-4000-8000-000000000005"); err != nil {
t.Fatal(err)
}
replay := sqlc.ReplayFailedParams{
JobID: jobID, RequestID: requestID, OperatorID: "operator", Reason: "test replay", ExtraAttempts: 1,
}
for _, want := range []int64{1, 0} {
if n, err := q.ReplayFailed(ctx, replay); err != nil || n != want {
t.Fatalf("replay changed %d rows, want %d: %v", n, want, err)
}
}
row, err = q.ClaimJob(ctx, claim)
if err != nil || row.AttemptsStarted != 4 || row.LeaseGeneration != 4 {
t.Fatalf("replay did not preserve attempt and lease counters: %v", err)
}
tx, err := pool.Begin(ctx)
if err != nil {
t.Fatal(err)
}
defer tx.Rollback(ctx)
locked := q.WithTx(tx)
if _, err := locked.LockActiveAttachmentJob(ctx, sqlc.LockActiveAttachmentJobParams{JobID: jobID, LeaseGeneration: 4}); err != nil {
t.Fatal(err)
}
complete := sqlc.CompleteJobParams{JobID: jobID, LeaseGeneration: 4, ObjectKey: "results/original"}
if n, err := locked.CompleteJob(ctx, complete); err != nil || n != 0 {
t.Fatalf("published result without variants: rows=%d, err=%v", n, err)
}
variant := sqlc.CreateAttachmentVariantParams{
AttachmentID: attachmentID, PipelineVersion: 1, Name: "thumbnail", ObjectKey: "results/thumb",
ContentType: "image/jpeg", SizeBytes: 123, Width: 16, Height: 10, Sha256: strings.Repeat("a", 64),
}
if err := locked.CreateAttachmentVariant(ctx, variant); err != nil {
t.Fatal(err)
}
if n, err := locked.CompleteJob(ctx, complete); err != nil || n != 0 {
t.Fatalf("published result without crop and optimized: rows=%d, err=%v", n, err)
}
variant.Name, variant.ObjectKey = "crop", "results/crop"
if err := locked.CreateAttachmentVariant(ctx, variant); err != nil {
t.Fatal(err)
}
variant.Name, variant.ObjectKey, variant.ContentType = "optimized", "results/webp", "image/webp"
if err := locked.CreateAttachmentVariant(ctx, variant); err != nil {
t.Fatal(err)
}
if n, err := locked.CompleteJob(ctx, complete); err != nil || n != 1 {
t.Fatalf("current owner could not publish result: rows=%d, err=%v", n, err)
}
if err := tx.Commit(ctx); err != nil {
t.Fatal(err)
}
if _, err := q.ClaimJob(ctx, claim); !errors.Is(err, pgx.ErrNoRows) {
t.Fatalf("worker claimed a ready job: %v", err)
}
published, err := q.GetPublishedOriginalKey(ctx, sqlc.GetPublishedOriginalKeyParams{
AttachmentID: attachmentID, PipelineVersion: 1,
})
if err != nil || published.String != complete.ObjectKey {
t.Fatalf("published original not recorded for its version: %v", err)
}
var attempts, events, replays int
err = pool.QueryRow(ctx, `
SELECT (SELECT count(*) FROM attachment_attempts WHERE job_id = $1 AND finished_at IS NOT NULL),
(SELECT count(*) FROM outbox WHERE job_id = $1),
(SELECT count(*) FROM attachment_replays WHERE job_id = $1)
`, jobID).Scan(&attempts, &events, &replays)
if err != nil || attempts != 4 || events != 4 || replays != 1 {
t.Fatalf("unexpected history: attempts=%d, events=%d, replays=%d: %v", attempts, events, replays, err)
}
}
+152
View File
@@ -0,0 +1,152 @@
package main
import (
"context"
"database/sql"
"errors"
"flag"
"log/slog"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"highload/db/migrations"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/stdlib"
"github.com/pressly/goose/v3"
"github.com/pressly/goose/v3/lock"
)
// Migration errors retain their cause for classification, but deliberately do
// not print connection strings, PostgreSQL DETAIL fields or raw SQL.
type migrationError struct {
stage string
cause error
}
func (e *migrationError) Error() string { return e.stage }
func (e *migrationError) Unwrap() error { return e.cause }
type boundedSessionLocker struct {
lock.SessionLocker
}
func (l boundedSessionLocker) SessionUnlock(ctx context.Context, conn *sql.Conn) error {
// Goose unlocks with a detached context; bound this cleanup separately.
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return l.SessionLocker.SessionUnlock(ctx, conn)
}
func main() {
command := flag.String("command", "up", "migration action: up or status")
timeout := flag.Duration("timeout", 2*time.Minute, "total migration/connection/lock budget")
flag.Parse()
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
if flag.NArg() != 0 || (*command != "up" && *command != "status") || *timeout < time.Second {
logger.Error("invalid migration arguments", "allowed_commands", "up,status",
"minimum_timeout", "1s")
os.Exit(1)
}
signalCtx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
ctx, cancel := context.WithTimeout(signalCtx, *timeout)
defer cancel()
if err := run(ctx, *command, *timeout, logger); err != nil {
fields := []any{"stage", err.Error(), "timeout", errors.Is(err, context.DeadlineExceeded),
"cancelled", errors.Is(err, context.Canceled)}
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
fields = append(fields, "sqlstate", pgErr.Code)
}
var partial *goose.PartialError
if errors.As(err, &partial) && partial.Failed != nil && partial.Failed.Source != nil {
fields = append(fields, "migration", partial.Failed.Source.Path,
"version", partial.Failed.Source.Version)
}
logger.Error("migration failed", fields...)
os.Exit(1)
}
}
func run(ctx context.Context, command string, timeout time.Duration, logger *slog.Logger) error {
fail := func(stage string, err error) error {
return &migrationError{stage: stage, cause: err}
}
path := os.Getenv("DATABASE_URL_FILE")
if path == "" {
path = "/run/secrets/database_url"
}
data, err := os.ReadFile(path)
if err != nil {
return fail("read database secret file", err)
}
url := strings.TrimSpace(string(data))
if url == "" {
return fail("database secret file is empty", errors.New("empty database URL"))
}
cfg, err := pgx.ParseConfig(url)
if err != nil {
return fail("parse database secret", err)
}
cfg.ConnectTimeout = 5 * time.Second
cfg.RuntimeParams["application_name"] = "highload-migrate"
cfg.RuntimeParams["lock_timeout"] = "10000"
cfg.RuntimeParams["statement_timeout"] = strconv.FormatInt(min(timeout.Milliseconds(), 2147483647), 10)
db := stdlib.OpenDB(*cfg)
db.SetMaxOpenConns(2)
db.SetMaxIdleConns(2)
db.SetConnMaxLifetime(5 * time.Minute)
defer db.Close()
if err := db.PingContext(ctx); err != nil {
return fail("connect to PostgreSQL", err)
}
locker, err := lock.NewPostgresSessionLocker(
lock.WithLockTimeout(1, 120),
lock.WithUnlockTimeout(1, 5),
)
if err != nil {
return fail("configure migration lock", err)
}
provider, err := goose.NewProvider(goose.DialectPostgres, db, migrations.Files,
goose.WithSessionLocker(boundedSessionLocker{locker}),
goose.WithDisableGlobalRegistry(true),
goose.WithSlog(logger),
// Goose verbose logging includes SQL statements. Log only selected
// migration metadata ourselves, even when a migration fails.
goose.WithVerbose(false),
)
if err != nil {
return fail("load embedded migrations", err)
}
if command == "status" {
statuses, err := provider.Status(ctx)
if err != nil {
return fail("read migration status", err)
}
for _, status := range statuses {
logger.Info("migration status", "file", status.Source.Path,
"version", status.Source.Version, "state", status.State)
}
return nil
}
started := time.Now()
results, err := provider.Up(ctx)
if err != nil {
return fail("apply pending migrations", err)
}
for _, result := range results {
logger.Info("migration applied", "file", result.Source.Path,
"version", result.Source.Version, "duration_seconds", result.Duration.Seconds())
}
logger.Info("migrations completed", "applied", len(results),
"duration_seconds", time.Since(started).Seconds())
return nil
}
+111
View File
@@ -0,0 +1,111 @@
package main
import (
"context"
"database/sql"
"errors"
"io"
"log/slog"
"os"
"path/filepath"
"strings"
"testing"
"time"
"highload/db/migrations"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/stdlib"
"github.com/pressly/goose/v3"
)
func TestConnectionSecretErrorsAreSafe(t *testing.T) {
for _, test := range []struct {
name string
content string
missing bool
}{
{name: "missing", missing: true},
{name: "empty", content: " \n\t"},
{name: "malformed", content: "postgres://synthetic:fixture-password@localhost/%zz"},
} {
t.Run(test.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "database_url")
if !test.missing {
if err := os.WriteFile(path, []byte(test.content), 0600); err != nil {
t.Fatal("cannot create test secret")
}
}
t.Setenv("DATABASE_URL_FILE", path)
logger := slog.New(slog.NewJSONHandler(io.Discard, nil))
err := run(context.Background(), "up", time.Second, logger)
if err == nil {
t.Fatal("invalid connection secret was accepted")
}
if strings.Contains(err.Error(), "fixture-password") ||
strings.Contains(err.Error(), "postgres://") {
t.Fatal("migration error exposed a connection secret")
}
var classified *migrationError
if !errors.As(err, &classified) || classified.Unwrap() == nil {
t.Fatal("migration error lost its classification/cause")
}
})
}
}
func TestEmbeddedMigrationsAreVersionedAndCollectable(t *testing.T) {
cfg, err := pgx.ParseConfig("postgres://synthetic@localhost/synthetic?sslmode=disable")
if err != nil {
t.Fatal("cannot configure test driver")
}
db := stdlib.OpenDB(*cfg)
defer db.Close()
// NewProvider collects sources without connecting to PostgreSQL.
provider, err := goose.NewProvider(goose.DialectPostgres, db, migrations.Files)
if err != nil {
t.Fatal("embedded migration names/versions cannot be collected by Goose")
}
sources := provider.ListSources()
if len(sources) == 0 {
t.Fatal("migrator build contains no SQL")
}
var previous int64
for _, source := range sources {
if source.Version <= previous {
t.Fatal("embedded versions are not unique and increasing")
}
previous = source.Version
data, err := migrations.Files.ReadFile(source.Path)
if err != nil || !strings.Contains(string(data), "-- +goose Up") {
t.Fatal("embedded migration is unavailable or has no Up section")
}
}
}
type deadlineProbe struct {
deadline time.Time
cancel error
}
func (*deadlineProbe) SessionLock(context.Context, *sql.Conn) error { return nil }
func (p *deadlineProbe) SessionUnlock(ctx context.Context, _ *sql.Conn) error {
p.deadline, _ = ctx.Deadline()
p.cancel = ctx.Err()
return nil
}
func TestUnlockHasBudgetEvenWithDetachedContext(t *testing.T) {
original, cancel := context.WithCancel(context.Background())
cancel()
probe := &deadlineProbe{}
locker := boundedSessionLocker{probe}
started := time.Now()
if err := locker.SessionUnlock(context.WithoutCancel(original), nil); err != nil {
t.Fatal("unlock delegation failed")
}
if probe.cancel != nil || probe.deadline.IsZero() ||
probe.deadline.Before(started) || probe.deadline.After(started.Add(6*time.Second)) {
t.Fatal("detached cleanup did not receive a bounded, live context")
}
}
+212
View File
@@ -0,0 +1,212 @@
package main
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"errors"
"log/slog"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/stdlib"
)
// These tests need an explicitly supplied disposable PostgreSQL database.
// Each test creates and drops only its own randomly named schema.
func migrationPostgresFixture(t *testing.T) (*sql.DB, context.Context) {
t.Helper()
connectionURL := os.Getenv("MIGRATE_TEST_DATABASE_URL")
if connectionURL == "" {
t.Skip("set MIGRATE_TEST_DATABASE_URL to run PostgreSQL integration tests")
}
cfg, err := pgx.ParseConfig(connectionURL)
if err != nil {
t.Fatal("invalid test database configuration")
}
cfg.ConnectTimeout = 5 * time.Second
admin := stdlib.OpenDB(*cfg)
t.Cleanup(func() { admin.Close() })
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
t.Cleanup(cancel)
schema := "migration_test_" + strings.ReplaceAll(uuid.NewString(), "-", "")
quoted := pgx.Identifier{schema}.Sanitize()
if _, err := admin.ExecContext(ctx, "CREATE SCHEMA "+quoted); err != nil {
t.Fatal("cannot create isolated migration test schema")
}
t.Cleanup(func() {
cleanupCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if _, err := admin.ExecContext(cleanupCtx, "DROP SCHEMA "+quoted+" CASCADE"); err != nil {
t.Error("cannot remove isolated migration test schema")
}
})
cfg.RuntimeParams["search_path"] = schema
// ConnString retains the original parsed URL; changing RuntimeParams alone
// would not pass search_path to the separate migrator connection.
scopedURL, err := url.Parse(connectionURL)
if err != nil || (scopedURL.Scheme != "postgres" && scopedURL.Scheme != "postgresql") {
t.Fatal("MIGRATE_TEST_DATABASE_URL must be a PostgreSQL URL")
}
query := scopedURL.Query()
query.Set("search_path", schema)
scopedURL.RawQuery = query.Encode()
path := filepath.Join(t.TempDir(), "database_url")
if err := os.WriteFile(path, []byte(scopedURL.String()), 0600); err != nil {
t.Fatal("cannot write test connection secret")
}
t.Setenv("DATABASE_URL_FILE", path)
db := stdlib.OpenDB(*cfg)
t.Cleanup(func() { db.Close() })
return db, ctx
}
func migrationLogRecords(t *testing.T, output *bytes.Buffer) []map[string]any {
t.Helper()
var records []map[string]any
for _, line := range bytes.Split(bytes.TrimSpace(output.Bytes()), []byte("\n")) {
if len(line) == 0 {
continue
}
var record map[string]any
if err := json.Unmarshal(line, &record); err != nil {
t.Fatal("migrator emitted a non-JSON log record")
}
for key := range record {
switch key {
case "time", "level", "msg", "file", "version", "state", "applied", "duration_seconds":
default:
t.Fatalf("unexpected log field %q; SQL or connection details must not be logged", key)
}
}
if strings.Contains(string(line), "CREATE TABLE") || strings.Contains(string(line), "postgres://") {
t.Fatal("migration log contains SQL or a connection URL")
}
records = append(records, record)
}
return records
}
func completedMigrationCount(t *testing.T, output *bytes.Buffer) int {
t.Helper()
for _, record := range migrationLogRecords(t, output) {
if record["msg"] == "migrations completed" {
count, ok := record["applied"].(float64)
if !ok {
t.Fatal("migration completion log has no numeric applied count")
}
return int(count)
}
}
t.Fatal("successful migration run has no completion log")
return 0
}
const lab5MigrationCount = 8
func TestPostgresMigrationsRepeatWithoutChangingDataOrLoggingSQL(t *testing.T) {
db, ctx := migrationPostgresFixture(t)
var output bytes.Buffer
logger := slog.New(slog.NewJSONHandler(&output, nil))
if err := run(ctx, "up", time.Minute, logger); err != nil {
t.Fatal("migration of empty schema failed")
}
if count := completedMigrationCount(t, &output); count != lab5MigrationCount {
t.Fatalf("empty schema: applied %d migrations, want %d", count, lab5MigrationCount)
}
if _, err := db.ExecContext(ctx, "INSERT INTO users (username, display_name, password_hash) VALUES ($1, $2, $3)",
"migration_fixture", "Migration fixture", "synthetic-test-only-hash"); err != nil {
t.Fatal("cannot insert persistent migration fixture")
}
output.Reset()
if err := run(ctx, "up", time.Minute, logger); err != nil {
t.Fatal("repeat migration failed")
}
if count := completedMigrationCount(t, &output); count != 0 {
t.Fatalf("repeat run: applied %d migrations, want 0", count)
}
var users, versions int
if err := db.QueryRowContext(ctx, "SELECT count(*) FROM users WHERE username = $1", "migration_fixture").Scan(&users); err != nil || users != 1 {
t.Fatal("repeat migration lost or duplicated existing data")
}
if err := db.QueryRowContext(ctx, "SELECT count(*) FROM goose_db_version WHERE version_id > 0 AND is_applied").Scan(&versions); err != nil || versions != lab5MigrationCount {
t.Fatal("repeat migration changed version history")
}
output.Reset()
if err := run(ctx, "status", time.Minute, logger); err != nil {
t.Fatal("migration status failed")
}
records := migrationLogRecords(t, &output)
if len(records) != lab5MigrationCount {
t.Fatal("status did not report all seven migrations")
}
for _, record := range records {
if record["state"] != "applied" {
t.Fatal("status did not report applied migrations")
}
}
}
func TestPostgresConcurrentMigratorsApplyEachVersionOnce(t *testing.T) {
db, ctx := migrationPostgresFixture(t)
var outputs [2]bytes.Buffer
var runErrors [2]error
var workers sync.WaitGroup
start := make(chan struct{})
for i := range outputs {
workers.Add(1)
go func(i int) {
defer workers.Done()
<-start
runErrors[i] = run(ctx, "up", time.Minute, slog.New(slog.NewJSONHandler(&outputs[i], nil)))
}(i)
}
close(start)
workers.Wait()
var applied int
for i, err := range runErrors {
if err != nil {
t.Fatal("concurrent migration run failed")
}
applied += completedMigrationCount(t, &outputs[i])
}
if applied != lab5MigrationCount {
t.Fatalf("concurrent runners applied %d migrations in total, want %d", applied, lab5MigrationCount)
}
var versions, distinctVersions int
if err := db.QueryRowContext(ctx, "SELECT count(*), count(DISTINCT version_id) FROM goose_db_version WHERE version_id > 0 AND is_applied").Scan(&versions, &distinctVersions); err != nil || versions != lab5MigrationCount || distinctVersions != lab5MigrationCount {
t.Fatal("concurrent runners duplicated or lost version history")
}
}
func TestPostgresMigrationFailureRollsBackAndPreservesCause(t *testing.T) {
db, ctx := migrationPostgresFixture(t)
// The first migration creates users, then conflicts with this chats table.
if _, err := db.ExecContext(ctx, "CREATE TABLE chats (marker text)"); err != nil {
t.Fatal("cannot create conflicting test table")
}
var output bytes.Buffer
err := run(ctx, "up", time.Minute, slog.New(slog.NewJSONHandler(&output, nil)))
var pgErr *pgconn.PgError
if err == nil || err.Error() != "apply pending migrations" || !errors.As(err, &pgErr) || pgErr.Code != "42P07" {
t.Fatal("migration failure did not retain a safely reportable duplicate-table error")
}
migrationLogRecords(t, &output)
var usersAbsent bool
if err := db.QueryRowContext(ctx, "SELECT to_regclass('users') IS NULL").Scan(&usersAbsent); err != nil || !usersAbsent {
t.Fatal("failed migration left a partially applied users table")
}
var versions int
if err := db.QueryRowContext(ctx, "SELECT count(*) FROM goose_db_version WHERE version_id > 0 AND is_applied").Scan(&versions); err != nil || versions != 0 {
t.Fatal("failed migration was recorded as applied")
}
}
+167
View File
@@ -0,0 +1,167 @@
package main
import (
"context"
"database/sql"
"io"
"log/slog"
"testing"
"github.com/pressly/goose/v3"
"highload/db/migrations"
)
func sessionSchema(t *testing.T, db *sql.DB, ctx context.Context) []string {
t.Helper()
rows, err := db.QueryContext(ctx, `
SELECT 'column:'||attname||':'||format_type(atttypid,atttypmod)||':'||attnotnull||':'||
COALESCE(pg_get_expr(d.adbin,d.adrelid),'')
FROM pg_attribute a LEFT JOIN pg_attrdef d ON d.adrelid=a.attrelid AND d.adnum=a.attnum
WHERE a.attrelid='sessions'::regclass AND a.attnum>0 AND NOT a.attisdropped
UNION ALL SELECT 'constraint:'||conname||':'||pg_get_constraintdef(oid)
FROM pg_constraint WHERE conrelid='sessions'::regclass
UNION ALL SELECT 'index:'||indexname||':'||indexdef
FROM pg_indexes WHERE schemaname=current_schema() AND tablename='sessions'
ORDER BY 1`)
if err != nil {
t.Fatal("cannot inspect restored session columns/defaults/constraints/indexes")
}
defer rows.Close()
var result []string
for rows.Next() {
var s string
if rows.Scan(&s) != nil {
t.Fatal("schema scan failed")
}
result = append(result, s)
}
if rows.Err() != nil || len(result) < 10 {
t.Fatal("incomplete session structure snapshot")
}
return result
}
func businessData(t *testing.T, db *sql.DB, ctx context.Context) []string {
t.Helper()
result := []string{}
for _, table := range []string{"users", "chats", "chat_members", "messages"} {
rows, err := db.QueryContext(ctx, "SELECT row_to_json(t)::text FROM "+table+" t ORDER BY row_to_json(t)::text")
if err != nil {
t.Fatal("business table disappeared during migration")
}
count := 0
for rows.Next() {
var value string
if rows.Scan(&value) != nil {
t.Fatal("data scan failed")
}
result = append(result, table+value)
count++
}
err = rows.Err()
rows.Close()
if err != nil || count == 0 {
t.Fatal("business fixture missing during migration")
}
}
return result
}
func equalStrings(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func TestPostgresMigrationThreeDownTwoDownOneAndReapply(t *testing.T) {
db, ctx := migrationPostgresFixture(t)
provider, err := goose.NewProvider(goose.DialectPostgres, db, migrations.Files,
goose.WithDisableGlobalRegistry(true), goose.WithSlog(slog.New(slog.NewTextHandler(io.Discard, nil))))
if err != nil {
t.Fatal("cannot load actual embedded migrations")
}
if _, err = provider.UpTo(ctx, 1); err != nil {
t.Fatal("cannot apply version 1")
}
v1 := sessionSchema(t, db, ctx)
if _, err = db.ExecContext(ctx, `
INSERT INTO users(id,username,display_name,password_hash,created_at) VALUES
('00000000-0000-4000-8000-000000000001','roundtrip_user','Roundtrip','synthetic-hash','2026-01-01T00:00:00Z');
INSERT INTO chats(id,title,created_at) VALUES
('00000000-0000-4000-8000-000000000002','Roundtrip','2026-01-01T00:00:00Z');
INSERT INTO chat_members VALUES('00000000-0000-4000-8000-000000000002','00000000-0000-4000-8000-000000000001');
INSERT INTO messages(id,chat_id,sender_id,text,created_at) VALUES
('00000000-0000-4000-8000-000000000003','00000000-0000-4000-8000-000000000002','00000000-0000-4000-8000-000000000001','Keep this message','2026-01-01T00:00:00Z');
INSERT INTO sessions VALUES(decode(repeat('ab',32),'hex'),'00000000-0000-4000-8000-000000000001','2026-01-01T00:00:00Z','2026-01-02T00:00:00Z',NULL);
`); err != nil {
t.Fatal("cannot create migration preservation fixtures")
}
original := businessData(t, db, ctx)
if _, err = provider.UpTo(ctx, 2); err != nil {
t.Fatal("cannot apply version 2")
}
v2 := sessionSchema(t, db, ctx)
assertVersion := func(want int64) {
t.Helper()
got, e := provider.GetDBVersion(ctx)
if e != nil || got != want {
t.Fatalf("want migration version %d, got %d", want, got)
}
if !equalStrings(original, businessData(t, db, ctx)) {
t.Fatal("migration changed unrelated users/chats/members/messages")
}
}
assertAbsent := func() {
t.Helper()
var absent bool
if db.QueryRowContext(ctx, "SELECT to_regclass('sessions') IS NULL").Scan(&absent) != nil || !absent {
t.Fatal("version 3 must remove SQL sessions")
}
}
if _, err = provider.Up(ctx); err != nil {
t.Fatal("cannot migrate to latest version")
}
assertVersion(lab5MigrationCount)
assertAbsent()
if _, err = provider.DownTo(ctx, 2); err != nil {
t.Fatal("latest -> 2 failed")
}
assertVersion(2)
if !equalStrings(v2, sessionSchema(t, db, ctx)) {
t.Fatal("3 -> 2 did not restore exact version-2 session columns, defaults, keys, FK, CHECK and indexes")
}
var count int
if db.QueryRowContext(ctx, "SELECT count(*) FROM sessions").Scan(&count) != nil || count != 0 {
t.Fatal("rollback must restore an empty sessions table; deleted credentials cannot be resurrected")
}
if _, err = db.ExecContext(ctx, `INSERT INTO sessions(token_hash,user_id,expires_at) VALUES(decode(repeat('cd',32),'hex'),'00000000-0000-4000-8000-000000000001',now()+interval '1 hour')`); err != nil {
t.Fatal("restored table/default/FK cannot accept a valid session")
}
for _, statement := range []string{
`INSERT INTO sessions(token_hash,user_id,expires_at) VALUES(decode(repeat('ef',32),'hex'),'00000000-0000-4000-8000-000000000099',now()+interval '1 hour')`,
`INSERT INTO sessions(token_hash,user_id,expires_at) VALUES(decode(repeat('ef',32),'hex'),'00000000-0000-4000-8000-000000000001',now()-interval '1 hour')`,
`INSERT INTO sessions(token_hash,user_id,expires_at) VALUES(decode(repeat('cd',32),'hex'),'00000000-0000-4000-8000-000000000001',now()+interval '1 hour')`,
} {
if _, err = db.ExecContext(ctx, statement); err == nil {
t.Fatal("restored table lost FK, expiry CHECK or primary key")
}
}
if _, err = provider.DownTo(ctx, 1); err != nil {
t.Fatal("2 -> 1 failed")
}
assertVersion(1)
if !equalStrings(v1, sessionSchema(t, db, ctx)) {
t.Fatal("2 -> 1 did not restore original sessions structure without created_at default")
}
if _, err = provider.Up(ctx); err != nil {
t.Fatal("1 -> latest reapplication failed")
}
assertVersion(lab5MigrationCount)
assertAbsent()
}
+113
View File
@@ -0,0 +1,113 @@
package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"time"
"highload/internal/broker"
"highload/internal/config"
"highload/internal/database"
"highload/internal/database/sqlc"
"highload/internal/fault"
"highload/internal/repository/postgres"
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if err := run(ctx); err != nil {
slog.Error("relay startup failed", "error", err)
os.Exit(1)
}
}
func run(ctx context.Context) error {
cfg, err := config.LoadPipeline()
if err != nil {
return err
}
if cfg.BrokerURL == "" {
return fmt.Errorf("BROKER_URL_FILE must contain an AMQP URL")
}
url, err := config.LoadPostgresURL()
if err != nil {
return err
}
pool, err := database.NewPostgres(ctx, url)
if err != nil {
return fmt.Errorf("PostgreSQL unavailable")
}
defer pool.Close()
repo := postgres.NewAttachmentRepository(sqlc.New(pool), pool)
recovered := make(chan struct{})
go func() {
defer close(recovered)
for ctx.Err() == nil {
if n, err := repo.EnqueueExpiredLeases(ctx, 32); err != nil && ctx.Err() == nil {
slog.Warn("lease recovery failed", "error", err)
} else if n > 0 {
slog.Info("expired leases enqueued", "count", n)
}
select {
case <-ctx.Done():
case <-time.After(250 * time.Millisecond):
}
}
}()
for ctx.Err() == nil {
if err := publish(ctx, repo, cfg.BrokerURL); err != nil && ctx.Err() == nil {
slog.Warn("relay reconnecting", "error", err)
}
select {
case <-ctx.Done():
case <-time.After(500 * time.Millisecond):
}
}
<-recovered
return nil
}
func publish(ctx context.Context, repo *postgres.AttachmentRepository, url string) error {
conn, ch, err := broker.Connect(url)
if err != nil {
return err
}
defer broker.Close(conn)
publisher, err := broker.NewPublisher(ch)
if err != nil {
return err
}
for ctx.Err() == nil {
sent, err := repo.PublishPending(ctx, func(txCtx context.Context, event sqlc.Outbox) error {
if err := fault.Pause(txCtx, "before_publish", event.JobID.String()); err != nil {
return err
}
confirmCtx, cancel := context.WithTimeout(txCtx, 5*time.Second)
err := publisher.Publish(confirmCtx, event.ID.String(), event.Payload)
cancel()
if err != nil {
return err
}
return fault.Pause(txCtx, "after_publish", event.JobID.String())
})
if err != nil {
return err
}
if !sent {
if conn.IsClosed() {
return fmt.Errorf("RabbitMQ connection closed")
}
select {
case <-ctx.Done():
case <-time.After(150 * time.Millisecond):
}
}
}
return nil
}
+58
View File
@@ -0,0 +1,58 @@
package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"sync"
"syscall"
"highload/internal/config"
"highload/internal/database"
"highload/internal/database/sqlc"
"highload/internal/pipeline"
"highload/internal/repository/postgres"
"highload/internal/storage/s3"
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if err := run(ctx); err != nil {
slog.Error("worker startup failed", "error", err)
os.Exit(1)
}
}
func run(ctx context.Context) error {
cfg, err := config.LoadPipeline()
if err != nil {
return err
}
if cfg.BrokerURL == "" {
return fmt.Errorf("BROKER_URL_FILE must contain an AMQP URL")
}
url, storageConfig, err := config.LoadAttachmentCleanup()
if err != nil {
return err
}
pool, err := database.NewPostgres(ctx, url)
if err != nil {
return fmt.Errorf("PostgreSQL unavailable")
}
defer pool.Close()
objects, err := s3.NewStorage(ctx, &storageConfig)
if err != nil {
return fmt.Errorf("S3 configuration invalid")
}
worker := pipeline.Worker{Repo: postgres.NewAttachmentRepository(sqlc.New(pool), pool), Objects: objects, Config: cfg}
slog.Info("worker started", "concurrency", cfg.Concurrency, "prefetch_per_consumer", cfg.Prefetch)
var lanes sync.WaitGroup
for range cfg.Concurrency {
lanes.Go(func() { worker.Serve(ctx) })
}
lanes.Wait()
return nil
}
+298
View File
@@ -0,0 +1,298 @@
name: highload-lab5
services:
api:
build:
context: .
dockerfile: Dockerfile
scale: 2
cpus: "2.0"
mem_limit: 512m
expose:
- "8080"
env_file:
- .env
environment:
SESSION_STORE: redis
REDIS_ADDR: redis:6379
secrets:
- database_url
- s3_access_key_id
- s3_secret_access_key
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
migrate:
condition: service_completed_successfully
s3-init:
condition: service_completed_successfully
restart: unless-stopped
stop_grace_period: 20s
read_only: true
tmpfs:
- /tmp:size=32m,mode=1777
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
attachment-cleanup:
build:
context: .
dockerfile: Dockerfile
target: cleanup
command: ["cleanup", "-dry-run=${LAB5_CLEANUP_DRY_RUN:-false}"]
cpus: "0.5"
mem_limit: 128m
env_file:
- .env
environment:
BROKER_URL_FILE: ""
secrets:
- database_url
- s3_access_key_id
- s3_secret_access_key
depends_on:
postgres:
condition: service_healthy
migrate:
condition: service_completed_successfully
s3-init:
condition: service_completed_successfully
restart: unless-stopped
stop_grace_period: 20s
read_only: true
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
relay:
build:
context: .
dockerfile: Dockerfile
target: relay
env_file: [.env]
environment:
BROKER_URL_FILE: /run/secrets/broker_url
secrets: [database_url, broker_url]
depends_on:
migrate:
condition: service_completed_successfully
rabbitmq:
condition: service_healthy
cpus: "0.5"
mem_limit: 128m
read_only: true
cap_drop: [ALL]
security_opt: [no-new-privileges:true]
restart: unless-stopped
worker:
build:
context: .
dockerfile: Dockerfile.worker
env_file: [.env]
environment:
BROKER_URL_FILE: /run/secrets/broker_url
secrets: [database_url, broker_url, s3_access_key_id, s3_secret_access_key]
depends_on:
migrate:
condition: service_completed_successfully
s3-init:
condition: service_completed_successfully
rabbitmq:
condition: service_healthy
cpus: "2.0"
mem_limit: 1536m
read_only: true
tmpfs:
- /tmp:size=768m,mode=1777
cap_drop: [ALL]
security_opt: [no-new-privileges:true]
restart: unless-stopped
stop_grace_period: 15s
rabbitmq:
image: rabbitmq:4.2.5-alpine@sha256:d4a3806070111cc69130a6542aa4049b612789852ef11fc3862abc94631b2572
hostname: rabbitmq
entrypoint: ["/bin/sh", "-ec"]
command:
- |
export RABBITMQ_DEFAULT_PASS="$$(cat /run/secrets/rabbitmq_password)"
exec docker-entrypoint.sh rabbitmq-server
environment:
RABBITMQ_DEFAULT_USER: messenger
RABBITMQ_DEFAULT_VHOST: messenger
secrets: [rabbitmq_password]
volumes:
- rabbitmq_data:/var/lib/rabbitmq
healthcheck:
test: ["CMD", "rabbitmq-diagnostics", "-q", "check_running"]
interval: 5s
timeout: 5s
retries: 12
mem_limit: 768m
cpus: "1.0"
restart: unless-stopped
stop_grace_period: 30s
lb:
image: haproxy:3.2.24-alpine@sha256:1aca32ee932b96d2a75f1882909325839dd23c138ba9c6fd26e4d88179f30f13
cpus: "1.0"
mem_limit: 256m
ports:
- "127.0.0.1:${LAB5_HTTP_PORT:-8085}:8080"
volumes:
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
depends_on:
api:
condition: service_healthy
restart: unless-stopped
read_only: true
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
migrate:
build:
context: .
dockerfile: Dockerfile.migrate
cpus: "1.0"
mem_limit: 256m
environment:
DATABASE_URL_FILE: /run/secrets/database_url
secrets:
- database_url
depends_on:
postgres:
condition: service_healthy
restart: "no"
stop_grace_period: 20s
read_only: true
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
postgres:
image: postgres:18@sha256:5a5a84b19854a9ffaa54082c166ff4ec27473a361e496e5ea167f298f2da9722
cpus: "2.0"
mem_limit: 512m
environment:
POSTGRES_DB: messenger
POSTGRES_USER: messenger
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
secrets:
- postgres_password
ports:
- "127.0.0.1:${LAB5_POSTGRES_PORT:-15485}:5432"
volumes:
- postgres_data:/var/lib/postgresql
healthcheck:
test: ["CMD-SHELL", "pg_isready -h 127.0.0.1 -U messenger -d messenger"]
interval: 5s
timeout: 5s
retries: 5
restart: unless-stopped
stop_grace_period: 60s
redis:
image: redis:7.4-alpine@sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499
cpus: "0.5"
mem_limit: 128m
restart: unless-stopped
command:
- redis-server
- --appendonly
- "yes"
- --appendfsync
- everysec
volumes:
- redis_data:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 5
s3:
image: rustfs/rustfs:1.0.4@sha256:ed9210d6411d77ffceb70d94b964a3cb7d225983368bc25bebe01e995293dcc2
cpus: "1.0"
mem_limit: 512m
environment:
RUSTFS_VOLUMES: /data
RUSTFS_ACCESS_KEY_FILE: /run/secrets/rustfs_root_access_key
RUSTFS_SECRET_KEY_FILE: /run/secrets/rustfs_root_secret_key
RUSTFS_CONSOLE_ENABLE: "false"
secrets:
- rustfs_root_access_key
- rustfs_root_secret_key
volumes:
- rustfs_data:/data
healthcheck:
test: ["CMD-SHELL", "curl -fsS http://127.0.0.1:9000/health/ready >/dev/null"]
interval: 5s
timeout: 3s
retries: 12
start_period: 30s
restart: unless-stopped
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
s3-init:
image: rustfs/rc:v0.1.30@sha256:1296510ec27800668d26a8bdca6251e587086187a9368c7b80134a169e8a3bb8
cpus: "0.5"
mem_limit: 128m
entrypoint: ["/bin/sh", "/bootstrap.sh"]
environment:
HOME: /tmp
S3_BUCKET: messenger-attachments
S3_REGION: us-east-1
secrets:
- rustfs_root_access_key
- rustfs_root_secret_key
- s3_access_key_id
- s3_secret_access_key
volumes:
- ./infra/s3/bootstrap.sh:/bootstrap.sh:ro
depends_on:
s3:
condition: service_healthy
restart: "no"
read_only: true
tmpfs:
- /tmp:uid=100,gid=101,mode=0700
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
secrets:
broker_url:
file: ./secrets/broker_url
rabbitmq_password:
file: ./secrets/rabbitmq_password
database_url:
file: ./secrets/database_url
postgres_password:
file: ./secrets/postgres_password
rustfs_root_access_key:
file: ./secrets/rustfs_root_access_key
rustfs_root_secret_key:
file: ./secrets/rustfs_root_secret_key
s3_access_key_id:
file: ./secrets/s3_access_key_id
s3_secret_access_key:
file: ./secrets/s3_secret_access_key
volumes:
rabbitmq_data:
postgres_data:
redis_data:
rustfs_data:
+79
View File
@@ -0,0 +1,79 @@
-- +goose Up
CREATE TABLE users
(
id uuid PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
username VARCHAR(32) UNIQUE NOT NULL,
display_name VARCHAR(100) NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
CONSTRAINT users_username_format_check CHECK (username COLLATE "C" ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT users_display_name_length_check CHECK (char_length(display_name) BETWEEN 1 AND 100),
CONSTRAINT users_display_name_not_blank_check CHECK (char_length(btrim(display_name)) > 0),
CONSTRAINT users_password_hash_not_empty_check CHECK (char_length(password_hash) > 0)
);
CREATE TABLE chats
(
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
title VARCHAR(100) NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
CONSTRAINT chats_title_length_check CHECK (char_length(title) BETWEEN 1 AND 100 ),
CONSTRAINT chats_title_not_blank_check CHECK (char_length(btrim(title)) > 0)
);
CREATE TABLE chat_members
(
chat_id UUID NOT NULL REFERENCES chats (id),
user_id UUID NOT NULL REFERENCES users (id),
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX idx_user_chat_id ON chat_members (user_id, chat_id);
CREATE TABLE messages
(
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
chat_id UUID NOT NULL,
sender_id UUID NOT NULL,
text TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
FOREIGN KEY (chat_id, sender_id)
REFERENCES chat_members (chat_id, user_id),
CONSTRAINT messages_text_length_check CHECK (char_length(text) BETWEEN 1 AND 4000),
CONSTRAINT messages_text_not_blank_check CHECK (char_length(btrim(text)) > 0)
);
CREATE INDEX idx_messages_chat_created_id ON messages (chat_id, created_at, id);
CREATE TABLE sessions
(
token_hash BYTEA PRIMARY KEY NOT NULL,
user_id UUID NOT NULL REFERENCES users (id),
created_at TIMESTAMPTZ NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
revoked_at TIMESTAMPTZ NULL,
CONSTRAINT sessions_expiry_after_creation_check CHECK (expires_at > created_at)
);
CREATE INDEX idx_session_user_id ON sessions (user_id);
-- +goose Down
DROP TABLE IF EXISTS sessions;
DROP TABLE IF EXISTS messages;
DROP TABLE IF EXISTS chat_members;
DROP TABLE IF EXISTS chats;
DROP TABLE IF EXISTS users;
@@ -0,0 +1,20 @@
-- +goose Up
ALTER TABLE users
ALTER COLUMN created_at SET DEFAULT now();
ALTER TABLE chats
ALTER COLUMN created_at SET DEFAULT now();
ALTER TABLE messages
ALTER COLUMN created_at SET DEFAULT now();
ALTER TABLE sessions
ALTER COLUMN created_at SET DEFAULT now();
-- +goose Down
ALTER TABLE sessions
ALTER COLUMN created_at DROP DEFAULT;
ALTER TABLE messages
ALTER COLUMN created_at DROP DEFAULT;
ALTER TABLE chats
ALTER COLUMN created_at DROP DEFAULT;
ALTER TABLE users
ALTER COLUMN created_at DROP DEFAULT;
@@ -0,0 +1,17 @@
-- +goose Up
DROP TABLE IF EXISTS sessions;
-- +goose Down
-- Restore the schema at version 2; deleted session data cannot be recovered.
CREATE TABLE sessions
(
token_hash BYTEA PRIMARY KEY NOT NULL,
user_id UUID NOT NULL REFERENCES users (id),
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
expires_at TIMESTAMPTZ NOT NULL,
revoked_at TIMESTAMPTZ NULL,
CONSTRAINT sessions_expiry_after_creation_check CHECK (expires_at > created_at)
);
CREATE INDEX idx_session_user_id ON sessions (user_id);
@@ -0,0 +1,23 @@
-- +goose Up
CREATE TABLE message_idempotency
(
user_id UUID NOT NULL REFERENCES users(id),
chat_id UUID NOT NULL REFERENCES chats(id),
idempotency_key VARCHAR(128) NOT NULL,
message_id UUID NOT NULL REFERENCES messages(id),
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
expires_at TIMESTAMPTZ NOT NULL DEFAULT (now() + interval '24 hours'),
PRIMARY KEY (user_id, chat_id, idempotency_key),
UNIQUE (message_id),
CONSTRAINT message_idempotency_key_format_check
CHECK (idempotency_key COLLATE "C" ~ '^[A-Za-z0-9._:-]{1,128}$'),
CONSTRAINT message_idempotency_expiry_check
CHECK (expires_at > created_at)
);
-- +goose Down
DROP TABLE IF EXISTS message_idempotency;
+54
View File
@@ -0,0 +1,54 @@
-- +goose Up
CREATE TABLE attachments
(
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
chat_id UUID NOT NULL,
owner_id UUID NOT NULL,
filename text NOT NULL,
object_key text UNIQUE NOT NULL,
content_type VARCHAR(10) NOT NULL,
size_bytes bigint NOT NULL,
sha256 text NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT fk_attachments_chat_member FOREIGN KEY (chat_id, owner_id)
REFERENCES chat_members(chat_id, user_id),
CONSTRAINT attachments_sha256_format_check CHECK (sha256 COLLATE "C" ~ '^[0-9a-f]{64}$'),
CONSTRAINT attachments_content_type_check CHECK (content_type IN ('image/png', 'image/jpeg')),
CONSTRAINT attachments_filename_check CHECK (
char_length(filename) BETWEEN 1 AND 255
AND filename !~ '[/\\]'
),
CONSTRAINT attachments_size_bytes_check CHECK (size_bytes BETWEEN 1 AND 10485760),
CONSTRAINT attachments_id_chat_unique UNIQUE (id, chat_id),
CONSTRAINT attachments_object_key_length_check CHECK (char_length(object_key) > 0)
);
ALTER TABLE messages
ADD CONSTRAINT messages_id_chat_unique UNIQUE (id, chat_id);
CREATE TABLE messages_attachments
(
message_id UUID NOT NULL ,
attachment_id UUID NOT NULL ,
chat_id UUID NOT NULL,
position SMALLINT NOT NULL,
CONSTRAINT message_attachments_pk PRIMARY KEY (message_id, attachment_id),
CONSTRAINT message_attachments_position_unique UNIQUE (message_id, position),
CONSTRAINT message_attachments_position_check CHECK (position BETWEEN 0 AND 9),
CONSTRAINT message_attachments_message_chat_fk
FOREIGN KEY (message_id, chat_id) REFERENCES messages(id, chat_id),
CONSTRAINT message_attachments_attachment_chat_fk
FOREIGN KEY (attachment_id, chat_id) REFERENCES attachments(id, chat_id)
);
-- +goose Down
DROP TABLE IF EXISTS messages_attachments;
DROP TABLE IF EXISTS attachments;
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_id_chat_unique;
@@ -0,0 +1,18 @@
-- +goose Up
ALTER TABLE messages
DROP CONSTRAINT messages_text_length_check,
DROP CONSTRAINT messages_text_not_blank_check;
ALTER TABLE messages
ADD CONSTRAINT messages_text_length_check
CHECK (char_length(text) <= 4000);
-- +goose Down
ALTER TABLE messages
DROP CONSTRAINT messages_text_length_check;
ALTER TABLE messages
ADD CONSTRAINT messages_text_length_check
CHECK (char_length(text) BETWEEN 1 AND 4000),
ADD CONSTRAINT messages_text_not_blank_check
CHECK (char_length(btrim(text)) > 0);
+144
View File
@@ -0,0 +1,144 @@
-- +goose Up
ALTER TABLE attachments
ADD COLUMN status TEXT NOT NULL DEFAULT 'queued',
ADD COLUMN error_code TEXT,
ADD COLUMN staging_key TEXT,
ADD COLUMN pipeline_version INT NOT NULL DEFAULT 1,
ALTER COLUMN object_key DROP NOT NULL;
-- Retain the original download path while the copied synchronous API is in use.
UPDATE attachments SET staging_key = object_key;
ALTER TABLE attachments
ADD CONSTRAINT attachments_status_check
CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
ADD CONSTRAINT attachments_pipeline_check CHECK (pipeline_version > 0),
ADD CONSTRAINT attachments_staging_key_check CHECK (length(staging_key) > 0),
ADD CONSTRAINT attachments_object_state_check CHECK (
(status NOT IN ('queued', 'processing') OR staging_key IS NOT NULL)
AND (status <> 'ready' OR object_key IS NOT NULL)
),
ADD CONSTRAINT attachments_error_check CHECK (
(status = 'failed') = (error_code IS NOT NULL)
AND (error_code IS NULL OR error_code COLLATE "C" ~ '^[a-z][a-z0-9_]{0,49}$')
);
CREATE UNIQUE INDEX attachments_staging_key_unique ON attachments (staging_key)
WHERE staging_key IS NOT NULL;
CREATE TABLE attachment_jobs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
attachment_id UUID NOT NULL REFERENCES attachments(id),
pipeline_version INT NOT NULL CHECK (pipeline_version > 0),
attempts_started INT NOT NULL DEFAULT 0,
max_attempts INT NOT NULL DEFAULT 5,
next_attempt_at TIMESTAMPTZ DEFAULT now(),
lease_generation INT NOT NULL DEFAULT 0 CHECK (lease_generation >= 0),
lease_until TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (attachment_id, pipeline_version),
CHECK (attempts_started >= 0 AND max_attempts > 0 AND attempts_started <= max_attempts),
CHECK (lease_until IS NULL OR next_attempt_at IS NULL)
);
CREATE INDEX attachment_jobs_expired_lease_idx ON attachment_jobs (lease_until, id)
WHERE lease_until IS NOT NULL;
CREATE TABLE attachment_variants (
attachment_id UUID NOT NULL,
pipeline_version INT NOT NULL,
name TEXT NOT NULL,
object_key TEXT NOT NULL UNIQUE CHECK (length(object_key) > 0),
content_type TEXT NOT NULL,
size_bytes BIGINT NOT NULL CHECK (size_bytes > 0),
width INT NOT NULL CHECK (width > 0),
height INT NOT NULL CHECK (height > 0),
sha256 TEXT NOT NULL CHECK (sha256 COLLATE "C" ~ '^[0-9a-f]{64}$'),
PRIMARY KEY (attachment_id, pipeline_version, name),
FOREIGN KEY (attachment_id, pipeline_version)
REFERENCES attachment_jobs(attachment_id, pipeline_version),
CHECK (
(name IN ('thumbnail', 'crop') AND content_type = 'image/jpeg')
OR (name = 'optimized' AND content_type = 'image/webp')
)
);
CREATE TABLE attachment_attempts (
job_id UUID NOT NULL REFERENCES attachment_jobs(id),
lease_generation INT NOT NULL CHECK (lease_generation > 0),
attempt INT NOT NULL CHECK (attempt > 0),
started_at TIMESTAMPTZ NOT NULL,
finished_at TIMESTAMPTZ,
outcome TEXT CHECK (outcome IN ('ready', 'retry', 'failed', 'lease_expired')),
error_code TEXT CHECK (error_code COLLATE "C" ~ '^[a-z][a-z0-9_]{0,49}$'),
PRIMARY KEY (job_id, lease_generation),
UNIQUE (job_id, attempt),
CHECK ((outcome IS NULL) = (finished_at IS NULL))
);
CREATE TABLE attachment_replays (
request_id UUID PRIMARY KEY,
job_id UUID NOT NULL REFERENCES attachment_jobs(id),
operator_id TEXT NOT NULL CHECK (length(btrim(operator_id)) > 0),
reason TEXT NOT NULL CHECK (length(btrim(reason)) > 0),
extra_attempts INT NOT NULL CHECK (extra_attempts BETWEEN 1 AND 5),
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE outbox (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
job_id UUID NOT NULL REFERENCES attachment_jobs(id),
payload JSONB NOT NULL,
available_at TIMESTAMPTZ NOT NULL DEFAULT now(),
sent_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
dedup_key TEXT UNIQUE NOT NULL CHECK (length(dedup_key) > 0)
);
CREATE INDEX outbox_pending_idx ON outbox (available_at, id) WHERE sent_at IS NULL;
-- +goose StatementBegin
CREATE FUNCTION attachment_job_payload(uuid, uuid, integer, integer, timestamptz)
RETURNS jsonb LANGUAGE sql STABLE
AS $$
SELECT jsonb_build_object(
'schema_version', 1,
'type', 'attachment.process.v1',
'job_id', $1,
'attachment_id', $2,
'pipeline_version', $3,
'attempt', $4,
'created_at', to_char($5 AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"')
);
$$;
-- +goose StatementEnd
WITH jobs AS (
INSERT INTO attachment_jobs (attachment_id, pipeline_version)
SELECT id, pipeline_version FROM attachments
RETURNING *
)
INSERT INTO outbox (job_id, payload, dedup_key)
SELECT id, attachment_job_payload(id, attachment_id, pipeline_version, 1, created_at),
'initial/' || id
FROM jobs;
-- +goose Down
-- Restore the legacy reference when the staging original is the only copy.
-- SET NOT NULL aborts the rollback if an original no longer exists.
UPDATE attachments SET object_key = staging_key WHERE object_key IS NULL;
ALTER TABLE attachments ALTER COLUMN object_key SET NOT NULL;
DROP FUNCTION attachment_job_payload(uuid, uuid, integer, integer, timestamptz);
DROP TABLE outbox;
DROP TABLE attachment_replays;
DROP TABLE attachment_attempts;
DROP TABLE attachment_variants;
DROP TABLE attachment_jobs;
ALTER TABLE attachments
DROP COLUMN status,
DROP COLUMN error_code,
DROP COLUMN staging_key,
DROP COLUMN pipeline_version;
@@ -0,0 +1,27 @@
-- +goose Up
ALTER TABLE attachment_jobs ADD COLUMN published_original_key TEXT
CHECK (length(published_original_key) > 0);
UPDATE attachment_jobs AS j
SET published_original_key = a.object_key
FROM attachments AS a
WHERE a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version
AND a.status = 'ready';
CREATE INDEX attachment_jobs_published_original_idx ON attachment_jobs (published_original_key)
WHERE published_original_key IS NOT NULL;
ALTER TABLE attachment_attempts
DROP CONSTRAINT attachment_attempts_outcome_check,
ADD CONSTRAINT attachment_attempts_outcome_check
CHECK (outcome IN ('ready', 'retry', 'failed', 'lease_expired', 'superseded'));
-- +goose Down
UPDATE attachment_attempts SET outcome = 'failed' WHERE outcome = 'superseded';
ALTER TABLE attachment_attempts
DROP CONSTRAINT attachment_attempts_outcome_check,
ADD CONSTRAINT attachment_attempts_outcome_check
CHECK (outcome IN ('ready', 'retry', 'failed', 'lease_expired'));
ALTER TABLE attachment_jobs DROP COLUMN published_original_key;
+9
View File
@@ -0,0 +1,9 @@
package migrations
import "embed"
// Files contains the SQL shipped with this exact migrator build.
// Add schema changes as new numbered files; do not edit applied migrations.
//
//go:embed *.sql
var Files embed.FS
+11
View File
@@ -0,0 +1,11 @@
-- name: CreateAttachmentVariant :exec
INSERT INTO attachment_variants (
attachment_id, pipeline_version, name, object_key, content_type, size_bytes, width, height, sha256
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9
);
-- name: ListAttachmentVariants :many
SELECT * FROM attachment_variants
WHERE attachment_id = $1 AND pipeline_version = $2
ORDER BY name;
+69
View File
@@ -0,0 +1,69 @@
-- name: CreateAttachment :one
-- Compatibility for the synchronous API until CreateQueuedAttachment is wired in.
WITH legacy AS (
INSERT INTO attachments
(chat_id, owner_id, filename, object_key, staging_key, content_type, size_bytes, sha256)
VALUES (sqlc.arg(chat_id), sqlc.arg(owner_id), sqlc.arg(filename),
sqlc.arg(object_key)::text, sqlc.arg(object_key)::text,
sqlc.arg(content_type), sqlc.arg(size_bytes), sqlc.arg(sha256))
RETURNING *
), job AS (
INSERT INTO attachment_jobs (attachment_id, pipeline_version)
SELECT id, pipeline_version FROM legacy
RETURNING *
), event AS (
INSERT INTO outbox (job_id, payload, dedup_key)
SELECT id, attachment_job_payload(id, attachment_id, pipeline_version, 1, created_at),
'initial/' || id
FROM job
RETURNING job_id
)
SELECT legacy.* FROM legacy CROSS JOIN event;
-- name: GetAttachmentForUser :one
SELECT a.*
FROM attachments AS a
JOIN chat_members AS cm ON cm.chat_id = a.chat_id
WHERE a.id = sqlc.arg(attachment_id)
AND cm.user_id = sqlc.arg(user_id);
-- name: GetAttachmentsForMessages :many
SELECT message_id, attachment_id, position
FROM messages_attachments
WHERE message_id = ANY(sqlc.arg(message_ids)::uuid[])
ORDER BY message_id, position;
-- name: AttachmentExistsByObjectKey :one
SELECT EXISTS (
SELECT 1 FROM attachments
WHERE object_key = sqlc.arg(object_key)::text OR staging_key = sqlc.arg(object_key)::text
UNION ALL
SELECT 1 FROM attachment_variants WHERE object_key = sqlc.arg(object_key)::text
UNION ALL
SELECT 1 FROM attachment_jobs WHERE published_original_key = sqlc.arg(object_key)::text
);
-- name: TryLockAttachmentObjectKey :one
SELECT pg_try_advisory_xact_lock(
hashtextextended(sqlc.arg(object_key)::text, 0)
)::boolean AS locked;
-- name: ListAttachmentKeys :many
SELECT id, object_key, created_at
FROM attachments
WHERE object_key > sqlc.arg(after_key)
ORDER BY object_key
LIMIT sqlc.arg(page_size);
-- name: GetAttachmentByID :one
SELECT * FROM attachments WHERE id = sqlc.arg(attachment_id);
-- name: CreateQueuedAttachment :one
INSERT INTO attachments (
id, chat_id, owner_id, filename, content_type, size_bytes, sha256, staging_key, status
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, 'queued'
) RETURNING *;
+197
View File
@@ -0,0 +1,197 @@
-- name: CreateAttachmentJob :one
INSERT INTO attachment_jobs (attachment_id, pipeline_version, next_attempt_at, max_attempts)
VALUES (sqlc.arg(attachment_id), sqlc.arg(pipeline_version),
sqlc.arg(available_at)::timestamptz, sqlc.arg(max_attempts))
RETURNING *;
-- name: ClaimJob :one
WITH claimed AS (
UPDATE attachment_jobs AS j
SET lease_generation = j.lease_generation + 1,
lease_until = sqlc.arg(new_lease_until)::timestamptz,
attempts_started = j.attempts_started + 1,
next_attempt_at = NULL,
updated_at = clock_timestamp()
FROM attachments AS a
WHERE j.id = sqlc.arg(job_id)
AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version
AND j.attempts_started < j.max_attempts
AND sqlc.arg(new_lease_until)::timestamptz > clock_timestamp()
AND (
(a.status = 'queued' AND j.lease_until IS NULL AND j.next_attempt_at <= clock_timestamp())
OR (a.status = 'processing' AND j.lease_until <= clock_timestamp())
)
RETURNING j.*
), expired AS (
UPDATE attachment_attempts AS attempt
SET outcome = 'lease_expired', error_code = 'lease_expired', finished_at = claimed.updated_at
FROM claimed
WHERE attempt.job_id = claimed.id
AND attempt.lease_generation = claimed.lease_generation - 1 AND attempt.finished_at IS NULL
), recorded AS (
INSERT INTO attachment_attempts (job_id, lease_generation, attempt, started_at)
SELECT id, lease_generation, attempts_started, updated_at FROM claimed
RETURNING job_id
), processing AS (
UPDATE attachments AS a
SET status = 'processing', updated_at = claimed.updated_at
FROM claimed
WHERE a.id = claimed.attachment_id
RETURNING a.id
)
SELECT claimed.* FROM claimed JOIN recorded ON recorded.job_id = claimed.id
JOIN processing ON processing.id = claimed.attachment_id;
-- name: LockActiveAttachmentJob :one
-- Hold this lock through variant insertion and CompleteJob. Roll back on a zero-row completion.
SELECT j.*
FROM attachment_jobs AS j JOIN attachments AS a ON a.id = j.attachment_id
WHERE j.id = sqlc.arg(job_id)
AND j.lease_generation = sqlc.arg(lease_generation)
AND j.lease_until > clock_timestamp()
AND a.status = 'processing' AND a.pipeline_version = j.pipeline_version
FOR UPDATE OF j;
-- name: CompleteJob :execrows
WITH completed AS (
UPDATE attachment_jobs AS j
SET lease_until = NULL, next_attempt_at = NULL, updated_at = clock_timestamp(),
published_original_key = sqlc.arg(object_key)::text
FROM attachments AS a
WHERE j.id = sqlc.arg(job_id)
AND j.lease_generation = sqlc.arg(lease_generation) AND j.lease_until > clock_timestamp()
AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version AND a.status = 'processing'
AND (SELECT count(*) FROM attachment_variants AS v
WHERE v.attachment_id = a.id AND v.pipeline_version = j.pipeline_version) = 3
RETURNING j.*
), recorded AS (
UPDATE attachment_attempts AS attempt
SET outcome = 'ready', finished_at = completed.updated_at
FROM completed
WHERE attempt.job_id = completed.id AND attempt.lease_generation = completed.lease_generation
)
UPDATE attachments AS a
SET status = 'ready', object_key = sqlc.arg(object_key)::text, updated_at = completed.updated_at
FROM completed
WHERE a.id = completed.attachment_id;
-- name: ScheduleRetry :execrows
WITH scheduled AS (
UPDATE attachment_jobs AS j
SET lease_until = NULL, next_attempt_at = sqlc.arg(available_at)::timestamptz,
updated_at = clock_timestamp()
FROM attachments AS a
WHERE j.id = sqlc.arg(job_id)
AND j.lease_generation = sqlc.arg(lease_generation) AND j.lease_until > clock_timestamp()
AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version AND a.status = 'processing'
AND j.attempts_started < j.max_attempts
AND sqlc.arg(available_at)::timestamptz > clock_timestamp()
RETURNING j.*
), recorded AS (
UPDATE attachment_attempts AS attempt
SET outcome = 'retry', error_code = sqlc.arg(error_code)::text, finished_at = scheduled.updated_at
FROM scheduled
WHERE attempt.job_id = scheduled.id AND attempt.lease_generation = scheduled.lease_generation
), event AS (
INSERT INTO outbox (job_id, payload, available_at, dedup_key)
SELECT id, attachment_job_payload(id, attachment_id, pipeline_version, attempts_started + 1, updated_at),
next_attempt_at, 'retry/' || id || '/' || (attempts_started + 1)
FROM scheduled
)
UPDATE attachments AS a
SET status = 'queued', updated_at = scheduled.updated_at
FROM scheduled WHERE a.id = scheduled.attachment_id;
-- name: FailJob :execrows
WITH failed AS (
UPDATE attachment_jobs AS j
SET lease_until = NULL, next_attempt_at = NULL, updated_at = clock_timestamp()
FROM attachments AS a
WHERE j.id = sqlc.arg(job_id)
AND j.lease_generation = sqlc.arg(lease_generation)
AND (
j.lease_until > clock_timestamp()
OR (j.lease_until <= clock_timestamp() AND j.attempts_started = j.max_attempts
AND sqlc.arg(error_code)::text = 'retry_exhausted')
)
AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version AND a.status = 'processing'
RETURNING j.*
), recorded AS (
UPDATE attachment_attempts AS attempt
SET outcome = 'failed', error_code = sqlc.arg(error_code)::text, finished_at = failed.updated_at
FROM failed
WHERE attempt.job_id = failed.id AND attempt.lease_generation = failed.lease_generation
)
UPDATE attachments AS a
SET status = 'failed', error_code = sqlc.arg(error_code)::text, updated_at = failed.updated_at
FROM failed WHERE a.id = failed.attachment_id;
-- name: ReplayFailed :execrows
WITH replayed AS (
UPDATE attachment_jobs AS j
SET max_attempts = j.attempts_started + sqlc.arg(extra_attempts)::int,
next_attempt_at = clock_timestamp(), updated_at = clock_timestamp()
FROM attachments AS a
WHERE j.id = sqlc.arg(job_id)
AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version
AND a.status = 'failed' AND a.staging_key IS NOT NULL
AND j.lease_until IS NULL AND j.next_attempt_at IS NULL
AND NOT EXISTS (
SELECT 1 FROM attachment_replays AS r WHERE r.request_id = sqlc.arg(request_id)::uuid
)
RETURNING j.*
), recorded AS (
INSERT INTO attachment_replays (request_id, job_id, operator_id, reason, extra_attempts)
SELECT sqlc.arg(request_id)::uuid, id, sqlc.arg(operator_id), sqlc.arg(reason), sqlc.arg(extra_attempts)
FROM replayed
), event AS (
INSERT INTO outbox (job_id, payload, available_at, dedup_key)
SELECT id, attachment_job_payload(id, attachment_id, pipeline_version, attempts_started + 1, updated_at),
next_attempt_at, 'replay/' || id || '/' || sqlc.arg(request_id)::uuid
FROM replayed
)
UPDATE attachments AS a
SET status = 'queued', error_code = NULL, updated_at = replayed.updated_at
FROM replayed WHERE a.id = replayed.attachment_id;
-- name: GetAttachmentJobState :one
SELECT
j.id, j.attachment_id, j.pipeline_version,
CASE WHEN a.pipeline_version = j.pipeline_version THEN a.status ELSE 'superseded' END::text AS status,
j.lease_generation,
j.lease_until,
coalesce(j.lease_until <= clock_timestamp(), false)::boolean AS lease_expired,
j.attempts_started,
j.max_attempts,
j.next_attempt_at
FROM attachment_jobs AS j
JOIN attachments AS a ON a.id = j.attachment_id
WHERE j.id = sqlc.arg(job_id)::uuid;
-- name: ListFailedAttachmentJobs :many
SELECT j.id, j.attachment_id, j.attempts_started, a.error_code, a.updated_at
FROM attachment_jobs j
JOIN attachments a ON a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version
WHERE a.status = 'failed'
ORDER BY a.updated_at DESC, j.id
LIMIT sqlc.arg(batch_size);
-- name: LockAttachmentJobForPipeline :one
SELECT j.*
FROM attachment_jobs j
WHERE j.attachment_id = $1 AND j.pipeline_version = $2
FOR UPDATE;
-- name: LockAttachmentJobForReplay :one
SELECT j.*
FROM attachment_jobs j WHERE j.id = $1
FOR UPDATE;
-- name: ClearStagingReference :execrows
UPDATE attachments
SET object_key = CASE WHEN status = 'failed' AND object_key = staging_key THEN NULL ELSE object_key END,
staging_key = NULL
WHERE id = sqlc.arg(id) AND staging_key = sqlc.arg(staging_key);
-- name: GetAttachmentByStagingKey :one
SELECT * FROM attachments WHERE staging_key = sqlc.arg(staging_key)::text;
+3
View File
@@ -0,0 +1,3 @@
-- name: CreateChatMember :exec
INSERT INTO chat_members(chat_id, user_id)
VALUES ($1, $2);
+34
View File
@@ -0,0 +1,34 @@
-- name: CreateChat :one
INSERT INTO chats (title)
VALUES ($1)
RETURNING id, title, created_at;
-- name: GetChatByID :one
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id)::uuid[] AS member_ids
FROM chats c
JOIN chat_members cm ON cm.chat_id = c.id
WHERE c.id = $1
GROUP BY c.id;
-- name: GetChatsByUserID :many
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id)::uuid[] AS member_ids
FROM chats c
JOIN chat_members cm ON cm.chat_id = c.id
WHERE EXISTS (SELECT 1
FROM chat_members membership
WHERE membership.chat_id = c.id
AND membership.user_id = sqlc.arg(user_id))
AND (
sqlc.narg(after_created_at)::timestamptz IS NULL
OR (c.created_at, c.id) >
(
sqlc.narg(after_created_at)::timestamptz,
sqlc.narg(after_id)::uuid
)
)
GROUP BY c.id
ORDER BY c.created_at ASC, c.id ASC
LIMIT sqlc.arg(limit_count);
+22
View File
@@ -0,0 +1,22 @@
-- name: InsertMessageIdempotency :execrows
INSERT INTO message_idempotency(user_id, chat_id, idempotency_key, message_id)
VALUES ($1, $2, $3, $4)
ON CONFLICT (user_id, chat_id, idempotency_key) DO NOTHING;
-- name: GetIdempotentMessage :one
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at
FROM message_idempotency AS i
JOIN messages AS m ON m.id = i.message_id
WHERE i.user_id = $1
AND i.chat_id = $2
AND i.idempotency_key = $3;
-- name: LockMessageRequest :exec
SELECT pg_advisory_xact_lock(hashtextextended(
'message/' || (sqlc.arg(user_id)::uuid)::text || '/' ||
(sqlc.arg(chat_id)::uuid)::text || '/' || sqlc.arg(idempotency_key)::text, 0));
-- name: DeleteExpiredMessageIdempotency :exec
DELETE FROM message_idempotency
WHERE user_id = $1 AND chat_id = $2 AND idempotency_key = $3 AND expires_at <= now();
+19
View File
@@ -0,0 +1,19 @@
-- name: CreateMessage :one
INSERT INTO messages (chat_id, sender_id, text)
VALUES ($1, $2, $3)
RETURNING *;
-- name: GetMessagesByChatID :many
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = sqlc.arg(chat_id)
AND (
sqlc.narg(after_created_at)::timestamptz IS NULL
OR (created_at, id) >
(sqlc.narg(after_created_at)::timestamptz,
sqlc.narg(after_id)::uuid
)
)
ORDER BY created_at ASC, id ASC
LIMIT sqlc.arg(limit_count);
+16
View File
@@ -0,0 +1,16 @@
-- name: CreateMessagesAttachments :execrows
INSERT INTO messages_attachments (message_id, attachment_id, chat_id, position)
SELECT $1, $2, $3, $4
FROM attachments
WHERE id = $2 AND chat_id = $3;
-- name: GetMessagesAttachments :many
SELECT attachment_id, position
FROM messages_attachments
WHERE message_id = sqlc.arg(message_id)
ORDER BY position ASC;
-- name: LockAttachmentsForMessage :many
SELECT id, status FROM attachments
WHERE chat_id = sqlc.arg(chat_id) AND id = ANY(sqlc.arg(attachment_ids)::uuid[])
ORDER BY id FOR SHARE;
+39
View File
@@ -0,0 +1,39 @@
-- name: CreateInitialOutboxEvent :exec
INSERT INTO outbox (job_id, payload, available_at, dedup_key)
SELECT id, attachment_job_payload(id, attachment_id, pipeline_version, 1, created_at),
next_attempt_at, 'initial/' || id
FROM attachment_jobs WHERE id = sqlc.arg(job_id)::uuid;
-- name: EnqueueExpiredLeases :many
WITH expired AS (
SELECT j.*, 'recovery/' || j.id || '/' || j.lease_generation AS recovery_key
FROM attachment_jobs AS j
JOIN attachments AS a ON a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version
WHERE a.status = 'processing' AND j.lease_until <= clock_timestamp()
AND NOT EXISTS (
SELECT 1 FROM outbox AS o WHERE o.dedup_key = 'recovery/' || j.id || '/' || j.lease_generation
)
ORDER BY j.lease_until, j.id
LIMIT sqlc.arg(batch_size)
FOR UPDATE OF j SKIP LOCKED
)
INSERT INTO outbox (job_id, payload, dedup_key)
SELECT id, attachment_job_payload(id, attachment_id, pipeline_version,
LEAST(attempts_started + 1, max_attempts), clock_timestamp()),
recovery_key
FROM expired
ON CONFLICT (dedup_key) DO NOTHING
RETURNING *;
-- name: NextPendingOutboxEvent :one
SELECT id, job_id, payload, available_at, sent_at, created_at, dedup_key
FROM outbox
WHERE sent_at IS NULL AND available_at <= statement_timestamp()
ORDER BY available_at, id
LIMIT 1
FOR UPDATE SKIP LOCKED;
-- name: MarkOutboxEventSent :execrows
UPDATE outbox SET sent_at = clock_timestamp()
WHERE id = sqlc.arg(id) AND sent_at IS NULL;
+21
View File
@@ -0,0 +1,21 @@
-- name: CreateUser :one
INSERT INTO users (username, display_name, password_hash)
VALUES ($1, $2, $3)
RETURNING id, username, display_name, password_hash, created_at;
-- name: GetUserByID :one
SELECT id, username, display_name, password_hash, created_at
FROM users
WHERE id = $1;
-- name: GetUserByUsername :one
SELECT id, username, display_name, password_hash, created_at
FROM users
WHERE username = $1;
+30
View File
@@ -0,0 +1,30 @@
-- name: SupersedeAttachmentJob :exec
WITH superseded AS (
UPDATE attachment_jobs
SET lease_until = NULL, next_attempt_at = NULL, updated_at = clock_timestamp()
WHERE id = sqlc.arg(job_id)::uuid
RETURNING id, updated_at
)
UPDATE attachment_attempts AS attempt
SET outcome = 'superseded', error_code = 'superseded', finished_at = superseded.updated_at
FROM superseded
WHERE attempt.job_id = superseded.id AND attempt.finished_at IS NULL;
-- name: QueueAttachmentUpgrade :one
UPDATE attachments
SET pipeline_version = 2, status = 'queued', staging_key = sqlc.arg(source_key)::text,
object_key = NULL, error_code = NULL, updated_at = clock_timestamp()
WHERE id = sqlc.arg(id)::uuid AND pipeline_version = 1
RETURNING *;
-- name: GetPipelineJobID :one
SELECT id FROM attachment_jobs
WHERE attachment_id = sqlc.arg(attachment_id)::uuid AND pipeline_version = sqlc.arg(pipeline_version)::int;
-- name: GetPublishedOriginalKey :one
SELECT published_original_key FROM attachment_jobs
WHERE attachment_id = sqlc.arg(attachment_id)::uuid AND pipeline_version = sqlc.arg(pipeline_version)::int
AND published_original_key IS NOT NULL;
-- name: IsPublishedOriginalKey :one
SELECT EXISTS (SELECT 1 FROM attachment_jobs WHERE published_original_key = sqlc.arg(object_key)::text);
+7
View File
@@ -0,0 +1,7 @@
{"event": "resource_sample", "t_s": 16.705083, "phase": "each-SIGTERM-read-write-with-load", "samples": ["966a4329e0d5\t36.80%\t15.35MiB / 512MiB", "33e9426ff450\t37.26%\t12.92MiB / 512MiB", "03a23dff1ff8\t32.09%\t77.19MiB / 256MiB"]}
{"event": "recovery", "t_s": 19.895726, "phase": "each-SIGTERM-read-write-with-load", "target": "966a4329e0d5", "seconds": 3.10449383300147}
{"event": "load_summary", "t_s": 25.620087, "phase": "each-SIGTERM-read-write-with-load", "window": "target 966a4329e0d5", "elapsed_s": 8.823298916002386, "attempts": 5984, "successes": 5978, "successful_rps": 677.5243655361141, "failures": 6, "error_ratio": 0.001002673796791444, "statuses": {"201": 2992, "200": 2986, "None": 6}, "p50_ms": 5.04, "p95_ms": 15.011, "p99_ms": 34.484}
{"event": "recovery", "t_s": 32.90963, "phase": "each-SIGTERM-read-write-with-load", "target": "33e9426ff450", "seconds": 3.301144083001418}
{"event": "load_summary", "t_s": 38.676268, "phase": "each-SIGTERM-read-write-with-load", "window": "target 33e9426ff450", "elapsed_s": 9.06493366601353, "attempts": 4430, "successes": 4424, "successful_rps": 488.0344592687499, "failures": 6, "error_ratio": 0.0013544018058690745, "statuses": {"201": 2215, "200": 2209, "None": 6}, "p50_ms": 5.677, "p95_ms": 24.386, "p99_ms": 65.408}
{"event": "load_summary", "t_s": 39.564574, "phase": "each-SIGTERM-read-write-with-load", "window": "full", "elapsed_s": 28.373479084009887, "attempts": 15861, "successes": 15843, "successful_rps": 558.3735414712838, "failures": 18, "error_ratio": 0.0011348590883298658, "statuses": {"201": 7926, "200": 7917, "None": 18}, "p50_ms": 5.516, "p95_ms": 21.228, "p99_ms": 47.711}
{"event": "load_history", "t_s": 40.011926, "phase": "each-SIGTERM-read-write-with-load", "observed": 7929, "confirmed": 7926, "offered": 7933}
+39
View File
@@ -0,0 +1,39 @@
# Чистый запуск и публичные проверки ЛР 5
15 ноября 2026 года. Apple M2, 8 ГиБ RAM. Docker Desktop 28.4.0, Linux ARM64, 8 доступных CPU и 4 ГБ RAM для VM. Go 1.27.1, sqlc 1.31.1. Чистый каталог исходников без `.env`, `secrets/`, Git и существующих volumes. Проект `lab5-final-vugkv4`, LB `127.0.0.1:18085`.
```sh
export COMPOSE_PROJECT_NAME=lab5-final-vugkv4
export LAB5_HTTP_PORT=18085 LAB5_POSTGRES_PORT=15486
sh scripts/init-local.sh
docker compose --env-file .env config --quiet
docker compose --env-file .env up -d --build --wait
docker compose --env-file .env logs migrate
make test BASE_URL=http://127.0.0.1:18085
make test-persistence BASE_URL=http://127.0.0.1:18085 ACTION_SCRIPT=scripts/restart.sh
make test-failover BASE_URL=http://127.0.0.1:18085 ACTION_SCRIPT=scripts/stop-one.sh
docker compose --env-file .env up -d --no-deps api
```
Выдержка из отчёта Docker и скриптов проверки.
```text
lab5-final-vugkv4-api-1 Healthy
lab5-final-vugkv4-api-2 Healthy
lab5-final-vugkv4-rabbitmq-1 Healthy
lab5-final-vugkv4-worker-1 Healthy
lab5-final-vugkv4-relay-1 Healthy
lab5-final-vugkv4-attachment-cleanup-1 Healthy
lab5-final-vugkv4-lb-1 Healthy
lab5-final-vugkv4-migrate-1 Exited
lab5-final-vugkv4-s3-init-1 Exited
{"msg":"migrations completed","applied":7,"duration_seconds":0.487786833}
PASS: lab5 public API scenario; infrastructure and grading evidence remain separate.
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
{"observed_instances_before":2,"successful_writes":111,"failed_attempts":1,"first_recovery_seconds":3.04}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
```
Публичный failover остановил обслужившую запрос реплику API на 15-секундном окне нагрузки. После прогона реплика снова запущена. Переходный запрос покрылся повтором, подтверждённые записи сохранились. Лог мигратора до и после persistence/failover совпал побайтно. Штатный restart перезапускает существующие контейнеры по ID. [scripts/restart.sh](../../scripts/restart.sh).
Локально выполнены `make check`, `make validate` с зависимостями из `requirements-dev.txt`, `sqlc compile`, `go vet ./...` и `go test -count=1 ./...`. Полный инфраструктурный прогон Go выполняется отдельно на стенде тестовика. `git diff --exit-code HEAD -- README.md COURSE.md contracts tests` подтвердил целостность задания и публичных тестов.
+1
View File
@@ -0,0 +1 @@
{"scenario": "crash-staging-before-commit", "attachment_id": "75bc5374-82f0-4ecb-9428-bed7b411b798", "upload_acknowledged": false, "staging_present_after_crash": true, "postgres_rows": [0, 0, 0], "staging_removed": true, "metadata_after_restart": 404, "status": "PASS"}
+12
View File
@@ -0,0 +1,12 @@
{"event": "result", "t_s": 0.049496, "phase": "health", "status": "PASS", "note": ""}
{"event": "result", "t_s": 1.596437, "phase": "upload-byte-pixel-media-limits", "status": "PASS", "note": ""}
{"event": "variant", "t_s": 2.910612, "phase": "async-output-variants", "name": "thumbnail", "width": 256, "height": 160, "bytes": 4654, "sha256": "d7cc662e82b52806da6245e82ad3c0c699ef7b4c6107cb2ee451e05930d3eabb"}
{"event": "variant", "t_s": 2.915311, "phase": "async-output-variants", "name": "crop", "width": 128, "height": 128, "bytes": 1808, "sha256": "507e48dc62a637e590ee652440576280e0d7e1de4bc3287010dc8ce104dc1697"}
{"event": "variant", "t_s": 2.919665, "phase": "async-output-variants", "name": "optimized", "width": 320, "height": 200, "bytes": 2462, "sha256": "1090d84ed8411226010ae498b08e1b9e6d81c8eee02ee77143a0dc327b894beb"}
{"event": "result", "t_s": 2.923246, "phase": "async-output-variants", "status": "PASS", "note": ""}
{"event": "result", "t_s": 6.717249, "phase": "idempotency-across-LB", "status": "PASS", "note": ""}
{"event": "result", "t_s": 13.465561, "phase": "CPU-RAM-ports", "status": "PASS", "note": ""}
{"event": "migrations", "t_s": 16.944801, "phase": "single-migration-start", "container": {"id": "082e76176ac67bbb25f0679db1c221f7cb21cd3c79277a1ea07309a36986c2a1", "alias": "082e76176ac6", "image": "sha256:7ea2786bc3979d268be58e18ce5015f48a24528e70ef5a77919fb15e275c5195", "state": "exited", "started_at": "2026-11-15T18:07:15.954914964Z", "finished_at": "2026-11-15T18:07:17.136262798Z", "exit_code": 0, "oom": false, "nano_cpus": 1000000000, "cpu_quota": 0, "cpu_period": 0, "memory": 268435456, "ports": {}, "bindings": {}, "network": "lab5-final-vugkv4_default", "publish_all": false}, "applied": [7], "versions": ["0|t|1", "1|t|1", "2|t|1", "3|t|1", "4|t|1", "5|t|1", "6|t|1", "7|t|1"]}
{"event": "result", "t_s": 16.944906, "phase": "single-migration-start", "status": "PASS", "note": ""}
{"event": "summary", "t_s": 16.944968, "phase": "single-migration-start", "PASS": 6, "FAIL": 0, "PENDING": 0}
{"scenario": "orientation-and-metadata", "status": "PASS", "fixtures": [{"filename": "wide.png", "attachment_id": "d9ac344a-26e1-4ac8-999b-0756abe247eb", "variants": {"crop": [128, 128], "optimized": [1280, 640], "thumbnail": [256, 128]}, "original_unchanged": true, "metadata_removed": true}, {"filename": "orientation6.jpg", "attachment_id": "16bec890-61b4-45ab-b690-87754329108b", "variants": {"crop": [128, 128], "optimized": [640, 1280], "thumbnail": [128, 256]}, "original_unchanged": true, "metadata_removed": true}]}
+64
View File
@@ -0,0 +1,64 @@
{"run": "pipeline-verified", "event": "pipeline_state", "t_s": 4.372676, "phase": "durable-broker-worker-stop-and-redelivery", "attachment": "f4d0ec9c-b2f7-46ef-aedf-bf8663e7b1be", "job_id": "19d77db0-0b79-4597-a1aa-75331cfa0cd8", "jobs": 1, "outbox": 1, "staging_exists": true, "variant_objects": [], "failed_registered": false, "attempts": [], "max_attempts": 3, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 1, "generation": 0, "result_count": 0, "original_exists": false, "audit_replays": 0, "envelope_valid": true, "scheduled_delays_ms": []}
{"run": "pipeline-verified", "event": "pipeline_state", "t_s": 8.012036, "phase": "durable-broker-worker-stop-and-redelivery", "attachment": "f4d0ec9c-b2f7-46ef-aedf-bf8663e7b1be", "job_id": "19d77db0-0b79-4597-a1aa-75331cfa0cd8", "jobs": 1, "outbox": 1, "staging_exists": true, "variant_objects": [], "failed_registered": false, "attempts": [], "max_attempts": 3, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 1, "generation": 0, "result_count": 0, "original_exists": false, "audit_replays": 0, "envelope_valid": true, "scheduled_delays_ms": []}
{"run": "pipeline-verified", "event": "restart-apis", "t_s": 10.380454, "phase": "durable-broker-worker-stop-and-redelivery", "evidence": {"started_before": {"98b96c5ce7f444e511e24dc0beb5e47ac7d8b99cccf3a21f523dc086705cd158": "2026-11-15T18:11:14.407616171Z", "93e9bf9769bc45e7b2de2939335b31bf91fb38bb9a270c4268ded3bbe27d0861": "2026-11-15T18:11:19.325341674Z"}, "started_after": {"98b96c5ce7f444e511e24dc0beb5e47ac7d8b99cccf3a21f523dc086705cd158": "2026-11-15T18:18:30.280046304Z", "93e9bf9769bc45e7b2de2939335b31bf91fb38bb9a270c4268ded3bbe27d0861": "2026-11-15T18:18:31.126341596Z"}}}
{"run": "pipeline-verified", "event": "broker-restart", "t_s": 22.193264, "phase": "durable-broker-worker-stop-and-redelivery", "evidence": {"broker_before": {"name": "image.jobs", "durable": true, "messages_ready": 1, "messages_unacknowledged": 0, "consumers": 0}, "broker_after": {"name": "image.jobs", "durable": true, "messages_ready": 1, "messages_unacknowledged": 0, "consumers": 0}}}
{"run": "pipeline-verified", "event": "attachment_state", "t_s": 23.346459, "phase": "durable-broker-worker-stop-and-redelivery", "attachment": "f4d0ec9c-b2f7-46ef-aedf-bf8663e7b1be", "status": "ready", "seconds": 0.5566292910079937}
{"run": "pipeline-verified", "event": "pipeline_state", "t_s": 25.236213, "phase": "durable-broker-worker-stop-and-redelivery", "attachment": "f4d0ec9c-b2f7-46ef-aedf-bf8663e7b1be", "job_id": "19d77db0-0b79-4597-a1aa-75331cfa0cd8", "jobs": 1, "outbox": 1, "staging_exists": false, "variant_objects": ["crop", "optimized", "thumbnail"], "failed_registered": false, "attempts": [{"attempt": 1, "kind": "success", "delay_seconds": null, "error_code": null}], "max_attempts": 3, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 1, "generation": 1, "result_count": 4, "original_exists": true, "audit_replays": 0, "envelope_valid": true, "scheduled_delays_ms": []}
{"run": "pipeline-verified", "event": "attachment_state", "t_s": 29.29045, "phase": "durable-broker-worker-stop-and-redelivery", "attachment": "f4d0ec9c-b2f7-46ef-aedf-bf8663e7b1be", "status": "ready", "seconds": 0.006781375006539747}
{"run": "pipeline-verified", "event": "pipeline_state", "t_s": 31.997314, "phase": "durable-broker-worker-stop-and-redelivery", "attachment": "f4d0ec9c-b2f7-46ef-aedf-bf8663e7b1be", "job_id": "19d77db0-0b79-4597-a1aa-75331cfa0cd8", "jobs": 1, "outbox": 2, "staging_exists": false, "variant_objects": ["crop", "optimized", "thumbnail"], "failed_registered": false, "attempts": [{"attempt": 1, "kind": "success", "delay_seconds": null, "error_code": null}], "max_attempts": 3, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 2, "generation": 1, "result_count": 4, "original_exists": true, "audit_replays": 0, "envelope_valid": true, "scheduled_delays_ms": []}
{"run": "pipeline-verified", "event": "result", "t_s": 32.922155, "phase": "durable-broker-worker-stop-and-redelivery", "status": "PASS", "note": ""}
{"run": "pipeline-verified", "event": "fault_reached", "t_s": 37.994659, "phase": "crash-COMMIT-before-publish", "evidence": {"point": "commit-before-publish", "attachment_id": "df24f2a0-6db9-4f78-9208-ec5eb8b79480", "job_id": "05677c5f-2e2c-448a-a959-7b483b4e3d85", "container": "57f78f4109d706a21c3ee56c12080c8b44440722bc38912692043dc126ebd743", "signal": "SIGKILL", "status": "queued", "generation": 0, "attempts": [], "outbox": 1, "published": 0, "result_count": null, "kill_elapsed_s": 0.2771513330080779}}
{"run": "pipeline-verified", "event": "attachment_state", "t_s": 38.271831, "phase": "crash-COMMIT-before-publish", "attachment": "df24f2a0-6db9-4f78-9208-ec5eb8b79480", "status": "ready", "seconds": 0.27709124999819323}
{"run": "pipeline-verified", "event": "pipeline_state", "t_s": 41.528209, "phase": "crash-COMMIT-before-publish", "attachment": "df24f2a0-6db9-4f78-9208-ec5eb8b79480", "job_id": "05677c5f-2e2c-448a-a959-7b483b4e3d85", "jobs": 1, "outbox": 1, "staging_exists": false, "variant_objects": ["crop", "optimized", "thumbnail"], "failed_registered": false, "attempts": [{"attempt": 1, "kind": "success", "delay_seconds": null, "error_code": null}], "max_attempts": 3, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 1, "generation": 1, "result_count": 4, "original_exists": true, "audit_replays": 0, "envelope_valid": true, "scheduled_delays_ms": []}
{"run": "pipeline-verified", "event": "drain", "t_s": 43.080636, "phase": "crash-COMMIT-before-publish", "evidence": {"name": "image.jobs", "durable": true, "messages_ready": 0, "messages_unacknowledged": 0, "consumers": 1}}
{"run": "pipeline-verified", "event": "result", "t_s": 43.958164, "phase": "crash-COMMIT-before-publish", "status": "PASS", "note": ""}
{"run": "pipeline-verified", "event": "result", "t_s": 45.899941, "phase": "single-migration-start", "status": "PASS", "note": ""}
{"run": "pipeline", "event": "fault_reached", "t_s": 142.343631, "phase": "crash-relay-after-confirm-before-mark", "evidence": {"point": "publish-before-mark", "attachment_id": "3a50ae05-e281-4ab5-91a4-4da3e4503b25", "job_id": "a7c0bdf7-b813-4458-a2f1-ec7deee48fb4", "container": "57f78f4109d706a21c3ee56c12080c8b44440722bc38912692043dc126ebd743", "signal": "SIGKILL", "status": "queued", "generation": 0, "attempts": [], "outbox": 1, "published": 0, "result_count": null, "kill_elapsed_s": 0.4695620830025291}}
{"run": "pipeline", "event": "attachment_state", "t_s": 143.814662, "phase": "crash-relay-after-confirm-before-mark", "attachment": "3a50ae05-e281-4ab5-91a4-4da3e4503b25", "status": "ready", "seconds": 0.5884555420052493}
{"run": "pipeline", "event": "pipeline_state", "t_s": 147.804815, "phase": "crash-relay-after-confirm-before-mark", "attachment": "3a50ae05-e281-4ab5-91a4-4da3e4503b25", "job_id": "a7c0bdf7-b813-4458-a2f1-ec7deee48fb4", "jobs": 1, "outbox": 1, "staging_exists": false, "variant_objects": ["crop", "optimized", "thumbnail"], "failed_registered": false, "attempts": [{"attempt": 1, "kind": "success", "delay_seconds": null, "error_code": null}], "max_attempts": 3, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 1, "generation": 1, "result_count": 4, "original_exists": true, "audit_replays": 0, "envelope_valid": true, "scheduled_delays_ms": []}
{"run": "pipeline", "event": "drain", "t_s": 154.443119, "phase": "crash-relay-after-confirm-before-mark", "evidence": {"name": "image.jobs", "durable": true, "messages_ready": 0, "messages_unacknowledged": 0, "consumers": 1}}
{"run": "pipeline", "event": "result", "t_s": 157.460635, "phase": "crash-relay-after-confirm-before-mark", "status": "PASS", "note": ""}
{"run": "pipeline", "event": "fault_reached", "t_s": 169.008007, "phase": "crash-object-before-ack-lease", "evidence": {"point": "object-before-ack", "attachment_id": "1265ca47-355e-446a-83e6-6785d69769ca", "job_id": "52417b87-c9a4-45b8-8919-58df3d7cfc53", "container": "78d995bc352b12d71068d5ad1763a00663a03399c3713b0c3198e708463d7cd8", "signal": "SIGKILL", "status": "processing", "generation": 1, "attempts": [{"attempt": 1, "kind": "processing", "delay_seconds": null, "error_code": null}], "outbox": 1, "published": 1, "result_count": 4, "kill_elapsed_s": 0.3759508750081295}}
{"run": "pipeline", "event": "attachment_state", "t_s": 180.985656, "phase": "crash-object-before-ack-lease", "attachment": "1265ca47-355e-446a-83e6-6785d69769ca", "status": "ready", "seconds": 11.97748087499349}
{"run": "pipeline", "event": "pipeline_state", "t_s": 184.385681, "phase": "crash-object-before-ack-lease", "attachment": "1265ca47-355e-446a-83e6-6785d69769ca", "job_id": "52417b87-c9a4-45b8-8919-58df3d7cfc53", "jobs": 1, "outbox": 2, "staging_exists": false, "variant_objects": ["crop", "optimized", "thumbnail"], "failed_registered": false, "attempts": [{"attempt": 1, "kind": "lease_expired", "delay_seconds": null, "error_code": "lease_expired"}, {"attempt": 2, "kind": "success", "delay_seconds": null, "error_code": null}], "max_attempts": 3, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 2, "generation": 2, "result_count": 4, "original_exists": true, "audit_replays": 0, "envelope_valid": true, "scheduled_delays_ms": []}
{"run": "pipeline", "event": "drain", "t_s": 189.315926, "phase": "crash-object-before-ack-lease", "evidence": {"name": "image.jobs", "durable": true, "messages_ready": 0, "messages_unacknowledged": 0, "consumers": 1}}
{"run": "pipeline", "event": "result", "t_s": 190.309213, "phase": "crash-object-before-ack-lease", "status": "PASS", "note": ""}
{"run": "pipeline", "event": "fault_reached", "t_s": 194.170989, "phase": "crash-ready-before-ack", "evidence": {"point": "ready-before-ack", "attachment_id": "f494dfcc-7346-4cb7-9905-6bee04e82d91", "job_id": "225feb85-a9d4-4970-86e0-891c595417b6", "container": "78d995bc352b12d71068d5ad1763a00663a03399c3713b0c3198e708463d7cd8", "signal": "SIGKILL", "status": "ready", "generation": 1, "attempts": [{"attempt": 1, "kind": "success", "delay_seconds": null, "error_code": null}], "outbox": 1, "published": 1, "result_count": null, "kill_elapsed_s": 0.257462082998245}}
{"run": "pipeline", "event": "attachment_state", "t_s": 194.225224, "phase": "crash-ready-before-ack", "attachment": "f494dfcc-7346-4cb7-9905-6bee04e82d91", "status": "ready", "seconds": 0.05395666600088589}
{"run": "pipeline", "event": "pipeline_state", "t_s": 199.530009, "phase": "crash-ready-before-ack", "attachment": "f494dfcc-7346-4cb7-9905-6bee04e82d91", "job_id": "225feb85-a9d4-4970-86e0-891c595417b6", "jobs": 1, "outbox": 1, "staging_exists": false, "variant_objects": ["crop", "optimized", "thumbnail"], "failed_registered": false, "attempts": [{"attempt": 1, "kind": "success", "delay_seconds": null, "error_code": null}], "max_attempts": 3, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 1, "generation": 1, "result_count": 4, "original_exists": true, "audit_replays": 0, "envelope_valid": true, "scheduled_delays_ms": []}
{"run": "pipeline", "event": "drain", "t_s": 201.403392, "phase": "crash-ready-before-ack", "evidence": {"name": "image.jobs", "durable": true, "messages_ready": 0, "messages_unacknowledged": 0, "consumers": 1}}
{"run": "pipeline", "event": "result", "t_s": 202.729892, "phase": "crash-ready-before-ack", "status": "PASS", "note": ""}
{"run": "pipeline", "event": "worker-limits", "t_s": 205.694563, "phase": "bounded-processing-timeout-and-prefetch", "evidence": {"worker_memory_bytes": 1610612736, "concurrency": 1, "prefetch": 1, "lease_seconds": 18, "job_timeout_seconds": 14, "retry_base_seconds": 2, "image_limits": {"Memory": "256MiB", "Map": "256MiB", "Disk": "512MiB", "Thread": "1", "Time": "45 seconds"}, "consumers": [{"prefetch": 1, "ack_required": true, "active": true}]}}
{"run": "pipeline", "event": "attachment_state", "t_s": 222.229049, "phase": "bounded-processing-timeout-and-prefetch", "attachment": "902c9560-0551-4daf-b54c-c1ac3a7de21b", "status": "ready", "seconds": 16.136552875002963}
{"run": "pipeline", "event": "pipeline_state", "t_s": 226.208075, "phase": "bounded-processing-timeout-and-prefetch", "attachment": "902c9560-0551-4daf-b54c-c1ac3a7de21b", "job_id": "2f11304e-ea5c-46e3-926e-e8bc3d63fac3", "jobs": 1, "outbox": 2, "staging_exists": false, "variant_objects": ["crop", "optimized", "thumbnail"], "failed_registered": false, "attempts": [{"attempt": 1, "kind": "temporary", "delay_seconds": 1.765518, "error_code": "processing_timeout"}, {"attempt": 2, "kind": "success", "delay_seconds": null, "error_code": null}], "max_attempts": 3, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 2, "generation": 2, "result_count": 4, "original_exists": true, "audit_replays": 0, "envelope_valid": true, "scheduled_delays_ms": [1773]}
{"run": "pipeline", "event": "result", "t_s": 227.389709, "phase": "bounded-processing-timeout-and-prefetch", "status": "PASS", "note": ""}
{"run": "pipeline", "event": "attachment_state", "t_s": 229.395054, "phase": "mismatched-broker-envelope-DLQ", "attachment": "300be646-6ff7-42ce-a339-938cc6b0a04b", "status": "ready", "seconds": 0.27098874999501277}
{"run": "pipeline", "event": "pipeline_state", "t_s": 233.627999, "phase": "mismatched-broker-envelope-DLQ", "attachment": "300be646-6ff7-42ce-a339-938cc6b0a04b", "job_id": "ef05368b-aff6-4c34-bd87-3cf53bff52db", "jobs": 1, "outbox": 1, "staging_exists": false, "variant_objects": ["crop", "optimized", "thumbnail"], "failed_registered": false, "attempts": [{"attempt": 1, "kind": "success", "delay_seconds": null, "error_code": null}], "max_attempts": 3, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 1, "generation": 1, "result_count": 4, "original_exists": true, "audit_replays": 0, "envelope_valid": true, "scheduled_delays_ms": []}
{"run": "pipeline", "event": "attachment_state", "t_s": 233.708161, "phase": "mismatched-broker-envelope-DLQ", "attachment": "300be646-6ff7-42ce-a339-938cc6b0a04b", "status": "ready", "seconds": 0.08003708299656864}
{"run": "pipeline", "event": "pipeline_state", "t_s": 239.325047, "phase": "mismatched-broker-envelope-DLQ", "attachment": "300be646-6ff7-42ce-a339-938cc6b0a04b", "job_id": "ef05368b-aff6-4c34-bd87-3cf53bff52db", "jobs": 1, "outbox": 1, "staging_exists": false, "variant_objects": ["crop", "optimized", "thumbnail"], "failed_registered": false, "attempts": [{"attempt": 1, "kind": "success", "delay_seconds": null, "error_code": null}], "max_attempts": 3, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 1, "generation": 1, "result_count": 4, "original_exists": true, "audit_replays": 0, "envelope_valid": true, "scheduled_delays_ms": []}
{"run": "pipeline", "event": "invalid-job", "t_s": 245.076344, "phase": "mismatched-broker-envelope-DLQ", "evidence": {"dead_letters_before": 0, "dead_letters_after": 3}}
{"run": "pipeline", "event": "attachment_state", "t_s": 245.087, "phase": "mismatched-broker-envelope-DLQ", "attachment": "300be646-6ff7-42ce-a339-938cc6b0a04b", "status": "ready", "seconds": 0.010581125010503456}
{"run": "pipeline", "event": "pipeline_state", "t_s": 249.616821, "phase": "mismatched-broker-envelope-DLQ", "attachment": "300be646-6ff7-42ce-a339-938cc6b0a04b", "job_id": "ef05368b-aff6-4c34-bd87-3cf53bff52db", "jobs": 1, "outbox": 4, "staging_exists": false, "variant_objects": ["crop", "optimized", "thumbnail"], "failed_registered": false, "attempts": [{"attempt": 1, "kind": "success", "delay_seconds": null, "error_code": null}], "max_attempts": 3, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 4, "generation": 1, "result_count": 4, "original_exists": true, "audit_replays": 0, "envelope_valid": true, "scheduled_delays_ms": []}
{"run": "pipeline", "event": "result", "t_s": 252.266017, "phase": "mismatched-broker-envelope-DLQ", "status": "PASS", "note": ""}
{"run": "pipeline", "event": "attachment_state", "t_s": 272.908274, "phase": "backoff-jitter-permanent-failed-replay", "attachment": "ac55549d-9641-4928-bfc5-fc489649be9f", "status": "failed", "seconds": 17.926805250011967}
{"run": "pipeline", "event": "pipeline_state", "t_s": 275.288526, "phase": "backoff-jitter-permanent-failed-replay", "attachment": "ac55549d-9641-4928-bfc5-fc489649be9f", "job_id": "3cc4c841-a7b6-455c-a698-46898ca96023", "jobs": 1, "outbox": 3, "staging_exists": true, "variant_objects": [], "failed_registered": true, "attempts": [{"attempt": 1, "kind": "temporary", "delay_seconds": 1.8016, "error_code": "s3_unavailable"}, {"attempt": 2, "kind": "temporary", "delay_seconds": 1.250712, "error_code": "s3_unavailable"}, {"attempt": 3, "kind": "temporary", "delay_seconds": null, "error_code": "retry_exhausted"}], "max_attempts": 3, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 3, "generation": 3, "result_count": 0, "original_exists": false, "audit_replays": 0, "envelope_valid": true, "scheduled_delays_ms": [1806, 1252]}
{"run": "pipeline", "event": "replay", "t_s": 276.677482, "phase": "backoff-jitter-permanent-failed-replay", "evidence": {"accepted": true, "request_id": "c814bdc8-10b6-4e7c-a5df-e02c3689233a", "job_id": "3cc4c841-a7b6-455c-a698-46898ca96023"}}
{"run": "pipeline", "event": "attachment_state", "t_s": 276.948792, "phase": "backoff-jitter-permanent-failed-replay", "attachment": "ac55549d-9641-4928-bfc5-fc489649be9f", "status": "ready", "seconds": 0.27123812500212807}
{"run": "pipeline", "event": "pipeline_state", "t_s": 280.188617, "phase": "backoff-jitter-permanent-failed-replay", "attachment": "ac55549d-9641-4928-bfc5-fc489649be9f", "job_id": "3cc4c841-a7b6-455c-a698-46898ca96023", "jobs": 1, "outbox": 4, "staging_exists": false, "variant_objects": ["crop", "optimized", "thumbnail"], "failed_registered": false, "attempts": [{"attempt": 1, "kind": "temporary", "delay_seconds": 1.8016, "error_code": "s3_unavailable"}, {"attempt": 2, "kind": "temporary", "delay_seconds": 1.250712, "error_code": "s3_unavailable"}, {"attempt": 3, "kind": "temporary", "delay_seconds": null, "error_code": "retry_exhausted"}, {"attempt": 4, "kind": "success", "delay_seconds": null, "error_code": null}], "max_attempts": 4, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 4, "generation": 4, "result_count": 4, "original_exists": true, "audit_replays": 1, "envelope_valid": true, "scheduled_delays_ms": [1806, 1252]}
{"run": "pipeline", "event": "replay", "t_s": 281.19427, "phase": "backoff-jitter-permanent-failed-replay", "evidence": {"accepted": false, "request_id": "e76941b0-52f4-4491-aed4-07f9262518ed", "job_id": "3cc4c841-a7b6-455c-a698-46898ca96023"}}
{"run": "pipeline", "event": "pipeline_state", "t_s": 284.774071, "phase": "backoff-jitter-permanent-failed-replay", "attachment": "ac55549d-9641-4928-bfc5-fc489649be9f", "job_id": "3cc4c841-a7b6-455c-a698-46898ca96023", "jobs": 1, "outbox": 4, "staging_exists": false, "variant_objects": ["crop", "optimized", "thumbnail"], "failed_registered": false, "attempts": [{"attempt": 1, "kind": "temporary", "delay_seconds": 1.8016, "error_code": "s3_unavailable"}, {"attempt": 2, "kind": "temporary", "delay_seconds": 1.250712, "error_code": "s3_unavailable"}, {"attempt": 3, "kind": "temporary", "delay_seconds": null, "error_code": "retry_exhausted"}, {"attempt": 4, "kind": "success", "delay_seconds": null, "error_code": null}], "max_attempts": 4, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 4, "generation": 4, "result_count": 4, "original_exists": true, "audit_replays": 1, "envelope_valid": true, "scheduled_delays_ms": [1806, 1252]}
{"run": "pipeline", "event": "attachment_state", "t_s": 304.056015, "phase": "backoff-jitter-permanent-failed-replay", "attachment": "78ad94a2-3f17-4a26-8aff-80850ce69cb1", "status": "failed", "seconds": 18.656614292005543}
{"run": "pipeline", "event": "pipeline_state", "t_s": 309.228063, "phase": "backoff-jitter-permanent-failed-replay", "attachment": "78ad94a2-3f17-4a26-8aff-80850ce69cb1", "job_id": "5d86f479-1974-4c78-9fca-87c2c2b28c10", "jobs": 1, "outbox": 3, "staging_exists": true, "variant_objects": [], "failed_registered": true, "attempts": [{"attempt": 1, "kind": "temporary", "delay_seconds": 1.700484, "error_code": "s3_unavailable"}, {"attempt": 2, "kind": "temporary", "delay_seconds": 2.625588, "error_code": "s3_unavailable"}, {"attempt": 3, "kind": "temporary", "delay_seconds": null, "error_code": "retry_exhausted"}], "max_attempts": 3, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 3, "generation": 3, "result_count": 0, "original_exists": false, "audit_replays": 0, "envelope_valid": true, "scheduled_delays_ms": [1705, 2627]}
{"run": "pipeline", "event": "replay", "t_s": 311.389768, "phase": "backoff-jitter-permanent-failed-replay", "evidence": {"accepted": true, "request_id": "a2a8b35a-cd21-4506-b5de-94d17cb253ec", "job_id": "5d86f479-1974-4c78-9fca-87c2c2b28c10"}}
{"run": "pipeline", "event": "attachment_state", "t_s": 311.653564, "phase": "backoff-jitter-permanent-failed-replay", "attachment": "78ad94a2-3f17-4a26-8aff-80850ce69cb1", "status": "ready", "seconds": 0.2637419590028003}
{"run": "pipeline", "event": "pipeline_state", "t_s": 314.848283, "phase": "backoff-jitter-permanent-failed-replay", "attachment": "78ad94a2-3f17-4a26-8aff-80850ce69cb1", "job_id": "5d86f479-1974-4c78-9fca-87c2c2b28c10", "jobs": 1, "outbox": 4, "staging_exists": false, "variant_objects": ["crop", "optimized", "thumbnail"], "failed_registered": false, "attempts": [{"attempt": 1, "kind": "temporary", "delay_seconds": 1.700484, "error_code": "s3_unavailable"}, {"attempt": 2, "kind": "temporary", "delay_seconds": 2.625588, "error_code": "s3_unavailable"}, {"attempt": 3, "kind": "temporary", "delay_seconds": null, "error_code": "retry_exhausted"}, {"attempt": 4, "kind": "success", "delay_seconds": null, "error_code": null}], "max_attempts": 4, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 4, "generation": 4, "result_count": 4, "original_exists": true, "audit_replays": 1, "envelope_valid": true, "scheduled_delays_ms": [1705, 2627]}
{"run": "pipeline", "event": "replay", "t_s": 315.459565, "phase": "backoff-jitter-permanent-failed-replay", "evidence": {"accepted": false, "request_id": "dfbc8483-47ad-4374-a7ec-0f2e47881594", "job_id": "5d86f479-1974-4c78-9fca-87c2c2b28c10"}}
{"run": "pipeline", "event": "pipeline_state", "t_s": 317.862922, "phase": "backoff-jitter-permanent-failed-replay", "attachment": "78ad94a2-3f17-4a26-8aff-80850ce69cb1", "job_id": "5d86f479-1974-4c78-9fca-87c2c2b28c10", "jobs": 1, "outbox": 4, "staging_exists": false, "variant_objects": ["crop", "optimized", "thumbnail"], "failed_registered": false, "attempts": [{"attempt": 1, "kind": "temporary", "delay_seconds": 1.700484, "error_code": "s3_unavailable"}, {"attempt": 2, "kind": "temporary", "delay_seconds": 2.625588, "error_code": "s3_unavailable"}, {"attempt": 3, "kind": "temporary", "delay_seconds": null, "error_code": "retry_exhausted"}, {"attempt": 4, "kind": "success", "delay_seconds": null, "error_code": null}], "max_attempts": 4, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 4, "generation": 4, "result_count": 4, "original_exists": true, "audit_replays": 1, "envelope_valid": true, "scheduled_delays_ms": [1705, 2627]}
{"run": "pipeline", "event": "attachment_state", "t_s": 321.023425, "phase": "backoff-jitter-permanent-failed-replay", "attachment": "0531a347-d57a-4dd7-aec8-7ab7f3311213", "status": "failed", "seconds": 0.46231995899870526}
{"run": "pipeline", "event": "pipeline_state", "t_s": 329.469513, "phase": "backoff-jitter-permanent-failed-replay", "attachment": "0531a347-d57a-4dd7-aec8-7ab7f3311213", "job_id": "7fe94350-23b9-4722-a9ba-c1b28e89f6b0", "jobs": 1, "outbox": 1, "staging_exists": true, "variant_objects": [], "failed_registered": true, "attempts": [{"attempt": 1, "kind": "permanent", "delay_seconds": null, "error_code": "invalid_image"}], "max_attempts": 3, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 1, "generation": 1, "result_count": 0, "original_exists": false, "audit_replays": 0, "envelope_valid": true, "scheduled_delays_ms": []}
{"run": "pipeline", "event": "replay", "t_s": 332.977678, "phase": "backoff-jitter-permanent-failed-replay", "evidence": {"accepted": true, "request_id": "8c8724a8-4ef9-4daf-a947-b9469609f7f9", "job_id": "7fe94350-23b9-4722-a9ba-c1b28e89f6b0"}}
{"run": "pipeline", "event": "attachment_state", "t_s": 333.242295, "phase": "backoff-jitter-permanent-failed-replay", "attachment": "0531a347-d57a-4dd7-aec8-7ab7f3311213", "status": "ready", "seconds": 0.2645675000094343}
{"run": "pipeline", "event": "pipeline_state", "t_s": 337.745274, "phase": "backoff-jitter-permanent-failed-replay", "attachment": "0531a347-d57a-4dd7-aec8-7ab7f3311213", "job_id": "7fe94350-23b9-4722-a9ba-c1b28e89f6b0", "jobs": 1, "outbox": 2, "staging_exists": false, "variant_objects": ["crop", "optimized", "thumbnail"], "failed_registered": false, "attempts": [{"attempt": 1, "kind": "permanent", "delay_seconds": null, "error_code": "invalid_image"}, {"attempt": 2, "kind": "success", "delay_seconds": null, "error_code": null}], "max_attempts": 2, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 2, "generation": 2, "result_count": 4, "original_exists": true, "audit_replays": 1, "envelope_valid": true, "scheduled_delays_ms": []}
{"run": "pipeline", "event": "replay", "t_s": 338.728727, "phase": "backoff-jitter-permanent-failed-replay", "evidence": {"accepted": false, "request_id": "872a20d3-0396-4a76-9afa-44987d21135f", "job_id": "7fe94350-23b9-4722-a9ba-c1b28e89f6b0"}}
{"run": "pipeline", "event": "pipeline_state", "t_s": 340.942477, "phase": "backoff-jitter-permanent-failed-replay", "attachment": "0531a347-d57a-4dd7-aec8-7ab7f3311213", "job_id": "7fe94350-23b9-4722-a9ba-c1b28e89f6b0", "jobs": 1, "outbox": 2, "staging_exists": false, "variant_objects": ["crop", "optimized", "thumbnail"], "failed_registered": false, "attempts": [{"attempt": 1, "kind": "permanent", "delay_seconds": null, "error_code": "invalid_image"}, {"attempt": 2, "kind": "success", "delay_seconds": null, "error_code": null}], "max_attempts": 2, "lease_seconds": 18, "prefetch": 1, "concurrency": 1, "job_timeout_seconds": 14, "worker_memory_bytes": 1610612736, "retry_base_seconds": 2, "published": 2, "generation": 2, "result_count": 4, "original_exists": true, "audit_replays": 1, "envelope_valid": true, "scheduled_delays_ms": []}
{"run": "pipeline", "event": "result", "t_s": 342.330574, "phase": "backoff-jitter-permanent-failed-replay", "status": "PASS", "note": ""}
+12
View File
@@ -0,0 +1,12 @@
{"event": "docker_environment", "t_s": 0.509421, "phase": "LB-excludes-unready-API", "version": "28.4.0", "os": "linux", "architecture": "aarch64", "logical_cpus": 8, "memory_bytes": 4109803520}
{"event": "routing", "t_s": 5.547801, "phase": "LB-excludes-unready-API", "wanted": ["33e9426ff450", "966a4329e0d5"], "observed": ["33e9426ff450", "966a4329e0d5"], "excluded": null, "attempts": 2}
{"event": "direct_health", "t_s": 7.986095, "phase": "LB-excludes-unready-API", "endpoint": "live", "status": 200, "instance": "966a4329e0d5"}
{"event": "direct_health", "t_s": 11.310146, "phase": "LB-excludes-unready-API", "endpoint": "ready", "status": 200, "instance": "966a4329e0d5"}
{"event": "direct_health", "t_s": 17.483132, "phase": "LB-excludes-unready-API", "endpoint": "ready", "status": 503, "instance": "966a4329e0d5"}
{"event": "direct_health", "t_s": 22.457978, "phase": "LB-excludes-unready-API", "endpoint": "live", "status": 200, "instance": "966a4329e0d5"}
{"event": "routing", "t_s": 25.540731, "phase": "LB-excludes-unready-API", "wanted": ["33e9426ff450"], "observed": ["33e9426ff450"], "excluded": "966a4329e0d5", "attempts": 29}
{"event": "direct_health", "t_s": 34.334113, "phase": "LB-excludes-unready-API", "endpoint": "live", "status": 200, "instance": "966a4329e0d5"}
{"event": "direct_health", "t_s": 39.591823, "phase": "LB-excludes-unready-API", "endpoint": "ready", "status": 503, "instance": "966a4329e0d5"}
{"event": "direct_health", "t_s": 44.769431, "phase": "LB-excludes-unready-API", "endpoint": "ready", "status": 200, "instance": "966a4329e0d5"}
{"event": "routing", "t_s": 44.931393, "phase": "LB-excludes-unready-API", "wanted": ["33e9426ff450", "966a4329e0d5"], "observed": ["33e9426ff450", "966a4329e0d5"], "excluded": null, "attempts": 2}
{"event": "result", "t_s": 44.931548, "phase": "LB-excludes-unready-API", "status": "PASS", "note": ""}
@@ -0,0 +1 @@
{"scenario": "concurrent-upgrade-after-staging-cleanup", "status": "PASS", "attachment_id": "d75fde0c-bac1-43a8-ba01-ec6e54de182d", "upgrades": [{"attachment_id": "d75fde0c-bac1-43a8-ba01-ec6e54de182d", "job_id": "3c54e884-c76b-4458-8406-8321bf0c61db", "pipeline_version": 2, "queued": true}, {"attachment_id": "d75fde0c-bac1-43a8-ba01-ec6e54de182d", "job_id": "3c54e884-c76b-4458-8406-8321bf0c61db", "pipeline_version": 2, "queued": false}], "variant_sha256": {"crop": {"v1": "507e48dc62a637e590ee652440576280e0d7e1de4bc3287010dc8ce104dc1697", "v2": "708fde25e98b0564fdc6d3d8da1ffd46c625becc1f09aaae7b5785ba928f37b5"}, "optimized": {"v1": "1090d84ed8411226010ae498b08e1b9e6d81c8eee02ee77143a0dc327b894beb", "v2": "0dbde6e7be396cb48efb5ad3ab4df33836d7b1c5def672d3474c04223823e48c"}, "thumbnail": {"v1": "d7cc662e82b52806da6245e82ad3c0c699ef7b4c6107cb2ee451e05930d3eabb", "v2": "cb02ee0bf2718ebd074dfb9bcb7e2779482c034d0889412b5154265e1d85fdc5"}}, "sql": ["1|1|results/d75fde0c-bac1-43a8-ba01-ec6e54de182d/v1/1/original|1", "2|1|results/d75fde0c-bac1-43a8-ba01-ec6e54de182d/v2/1/original|1"], "original_unchanged": true}
+16
View File
@@ -0,0 +1,16 @@
{
"date": "2026-11-16",
"project": "highload-lab5",
"schema_before": 7,
"schema_after": 8,
"migrations_applied": 1,
"ready_attachments_before": 6,
"ready_original_references_after": 6,
"public_smoke": "PASS",
"public_persistence": "PASS",
"public_failover": "PASS",
"failover_successful_writes": 110,
"failover_failed_attempts": 1,
"failover_first_recovery_seconds": 2.599,
"readiness": "ok"
}
Binary file not shown.
+77
View File
@@ -0,0 +1,77 @@
{
"event": "versioned_summary",
"t_s": 698.745594,
"phase": "pipeline-versioned-series",
"repeats": 3,
"results": {
"duplicate-v2-and-stale-v1": {
"attempted": 3,
"passed": 3,
"failed": 0,
"ready_seconds": [
0.14191362498968374,
0.16081770800519735,
0.1624453340045875
]
},
"reorder-v2-before-v1": {
"attempted": 3,
"passed": 3,
"failed": 0,
"ready_seconds": [
0.37723675000597723,
0.43153191699821036,
0.9601519999996526
]
},
"crash-before-S3": {
"attempted": 3,
"passed": 3,
"failed": 0,
"ready_seconds": [
31.613037041999632,
31.801774457999272,
32.08479704199999
]
},
"crash-after-S3": {
"attempted": 3,
"passed": 3,
"failed": 0,
"ready_seconds": [
31.915060792001896,
33.189212958997814,
33.35308025000268
]
},
"late-v1-worker-after-v2": {
"attempted": 3,
"passed": 3,
"failed": 0,
"ready_seconds": [
0.16078695899341255,
0.41879433300346136,
0.8732750420022057
]
}
},
"identical_v2": true,
"v2_sha256": {
"crop": "708fde25e98b0564fdc6d3d8da1ffd46c625becc1f09aaae7b5785ba928f37b5",
"optimized": "0dbde6e7be396cb48efb5ad3ab4df33836d7b1c5def672d3474c04223823e48c",
"thumbnail": "cb02ee0bf2718ebd074dfb9bcb7e2779482c034d0889412b5154265e1d85fdc5"
},
"seconds_before_original_deadline_at_rejection": [
19.875689,
19.516368,
21.473015
],
"regressions": [
"single-migration-start",
"upload-byte-pixel-media-limits",
"async-output-variants",
"make-test",
"make-test-persistence",
"make-test-failover"
]
}
Binary file not shown.
+17
View File
@@ -0,0 +1,17 @@
RUN: pipeline-versioned-series
PASS: duplicate-v2-and-stale-v1, repeat 1/3
PASS: reorder-v2-before-v1, repeat 1/3
PASS: crash-before-S3, repeat 1/3
PASS: crash-after-S3, repeat 1/3
PASS: late-v1-worker-after-v2, repeat 1/3
PASS: duplicate-v2-and-stale-v1, repeat 2/3
PASS: reorder-v2-before-v1, repeat 2/3
PASS: crash-before-S3, repeat 2/3
PASS: crash-after-S3, repeat 2/3
PASS: late-v1-worker-after-v2, repeat 2/3
PASS: duplicate-v2-and-stale-v1, repeat 3/3
PASS: reorder-v2-before-v1, repeat 3/3
PASS: crash-before-S3, repeat 3/3
PASS: crash-after-S3, repeat 3/3
PASS: late-v1-worker-after-v2, repeat 3/3
PASS: pipeline-versioned-series
+44
View File
@@ -0,0 +1,44 @@
module highload
go 1.27.1
require (
github.com/google/uuid v1.6.0
github.com/jackc/pgx/v5 v5.11.0
github.com/pressly/goose/v3 v3.26.0
github.com/rabbitmq/amqp091-go v1.10.0
github.com/redis/go-redis/v9 v9.23.0
golang.org/x/crypto v0.57.0
)
require (
github.com/aws/aws-sdk-go-v2 v1.48.0 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.27 // indirect
github.com/aws/aws-sdk-go-v2/config v1.33.18 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.21.0 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.9 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.12 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.25 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.13 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.12 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.12 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.10 // indirect
github.com/aws/aws-sdk-go-v2/service/signin v1.13.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.39.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.52.0 // indirect
github.com/aws/smithy-go v1.28.9 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/sethvargo/go-retry v0.3.0 // indirect
go.uber.org/atomic v1.12.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sync v0.23.0 // indirect
golang.org/x/sys v0.48.0 // indirect
golang.org/x/text v0.42.0 // indirect
)
+107
View File
@@ -0,0 +1,107 @@
github.com/aws/aws-sdk-go-v2 v1.48.0 h1:tFU7EJVm7MfyaXLsXdYTZ0Vd6vwt9OljCyqq0gqk0Mc=
github.com/aws/aws-sdk-go-v2 v1.48.0/go.mod h1:oeKU5p+TQBnpeLmU42WZLWvzv+ORLn+74n4QVLnm/Z0=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.27 h1:3dMP9eEmHVosFN1QiD24IJBKVY8m6iUfVYXqRCT118Q=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.27/go.mod h1:KH4PKcqtjy1m0656eqQV2v53xE04ddJzuNloEKqlHkI=
github.com/aws/aws-sdk-go-v2/config v1.33.18 h1:WC0C4jM/tewXG8DWXnoxXnusJPZTryPazqk2k/zgd2s=
github.com/aws/aws-sdk-go-v2/config v1.33.18/go.mod h1:0kRpFgsnD59mC2bbbyOTVpaZ9RnXW/pFkqlfiwbgZaU=
github.com/aws/aws-sdk-go-v2/credentials v1.21.0 h1:OViwhqeipkQzSHuO2eQDUCdLrI6r3nzGxFUyM8GqnCs=
github.com/aws/aws-sdk-go-v2/credentials v1.21.0/go.mod h1:OffaVQod3fyZzhZJyPtw11WpHPF1uIHADc+/MYlXmbA=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.9 h1:oH1E0tjN0g+ZRusbX1dsuat4FJ0z05l8hWaycSbDuNc=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.9/go.mod h1:3ZaQGigIoaS45x9HgnLZ4sP475Fs7QoxU3IsZX/J3h4=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.12 h1:W0yUUzKBA8SofGQ0yZafEUuZq9UxfvijRgdc6vcRQPA=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.12/go.mod h1:xwKv6vwBNdpPFOxEm/ld4IODZK2s/1ksHvdBqLfH6Gs=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.12 h1:lqSi8qvAnKC1vfosImdmOsuiJLr/jdybZX5299beB6Q=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.12/go.mod h1:rqPQE2vqkW9YHx5+Gp55sx1wsTM4xtGQLl+TKMjxxrs=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.12 h1:uvX2yaL+aMjkmnrLPPlR/zwjoKFRMoNhhJfeGjfk6qc=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.12/go.mod h1:dicT+2eNhHHqGkxHO7LoXw3QghIUGuzgdmFqaSTopgI=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.25 h1:rKwkY3Z+fvt2W0msjdNs6uNewPmXzPayQ961oyuWhes=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.25/go.mod h1:dBuAadQKd1F4Q4ZOg8gnFmuSHMRE40Pe3IfFwbtC4lE=
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.13 h1:U5HZ8FpTr6tyvulf1nT11Nw4KG1CAsXkuQGdUrIjZ/E=
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.13/go.mod h1:dUfvCU1NaE+b3vqNENDiJncUQW7Djl5pmgUlcjesVqk=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.12 h1:wzAqMZ2O4+ij9HQqIrk+P9PBGacE0wOTuWB7Mo97//I=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.12/go.mod h1:/0TfyruByHsOlphzP26xDTRe2DKD0J1I1Uyy2TyP8EI=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.12 h1:H0IaQayT3TLiGIHiNnleQZTbndDlx7LGZ0NWZ5mDwjA=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.12/go.mod h1:YuJJNxaOjnknLIrscBmkLhIpnmg9LzHd25sQRKUboGU=
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.10 h1:WBJb2VxRzzKnuN94YWqZPiu5a5zc3IleFqgZnmVCyvU=
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.10/go.mod h1:8NhAIBVYkVHq0hIbbzujdoou0VkKwMdG+De9e5oX6qI=
github.com/aws/aws-sdk-go-v2/service/signin v1.13.1 h1:ssrDA6AKfXEc3h0Pf4EI4vQ8sN5Zd136SrU9Tjr0XPc=
github.com/aws/aws-sdk-go-v2/service/signin v1.13.1/go.mod h1:LdwHjKqPE9VCC7sTlWcOAizZx3ZJYjmfwC/EW2tIIAE=
github.com/aws/aws-sdk-go-v2/service/sso v1.39.5 h1:ANAoa+qt3/9kXnvQVgUyxcTzhzZf4DqaBS+M2DQrELU=
github.com/aws/aws-sdk-go-v2/service/sso v1.39.5/go.mod h1:vsMi+SSR7xGQXtV430LVi49qbv/R+U+upHHSp1W/+QI=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.5 h1:uIOBLMhef4N16WguadoHKuBy9etdz+RKTeqzchXErAI=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.5/go.mod h1:NwWmkFLtsTVyXQpbzPOlYLwEoANkdCFjJRtrNcbg/vY=
github.com/aws/aws-sdk-go-v2/service/sts v1.52.0 h1:IaBLmJWiIEtKkjXMgMbYGZk7WKXbYA8148wCMCXq9hU=
github.com/aws/aws-sdk-go-v2/service/sts v1.52.0/go.mod h1:MG6N8U9+lY4LqchrlHPqCOSr7hyxw70XU0Et2YR3eNA=
github.com/aws/smithy-go v1.28.9 h1:zkZ058OpNX0iGTK24b+ZUEQSMc11CBA6oO7xtKZfzkY=
github.com/aws/smithy-go v1.28.9/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg=
github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pressly/goose/v3 v3.26.0 h1:KJakav68jdH0WDvoAcj8+n61WqOIaPGgH0bJWS6jpmM=
github.com/pressly/goose/v3 v3.26.0/go.mod h1:4hC1KrritdCxtuFsqgs1R4AU5bWtTAf+cnWvfhf2DNY=
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
github.com/redis/go-redis/v9 v9.23.0 h1:/TTVdJa8BRNsybwwGGnmiYktHNlUXtbOQxlWpHBPL3s=
github.com/redis/go-redis/v9 v9.23.0/go.mod h1:EWP2UUk+XDaI1s5nQB5mablZN6vccXlmBQTyfnF87D0=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE=
go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk=
golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
modernc.org/libc v1.66.3 h1:cfCbjTUcdsKyyZZfEUKfoHcP3S0Wkvz3jgSzByEWVCQ=
modernc.org/libc v1.66.3/go.mod h1:XD9zO8kt59cANKvHPXpx7yS2ELPheAey0vjIuZOhOU8=
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
modernc.org/sqlite v1.38.2 h1:Aclu7+tgjgcQVShZqim41Bbw9Cho0y/7WzYptXqkEek=
modernc.org/sqlite v1.38.2/go.mod h1:cPTJYSlgg3Sfg046yBShXENNtPrWrDX8bsbAQBzgQ5E=
+28
View File
@@ -0,0 +1,28 @@
global
log stdout format raw local0
stats socket ipv4@127.0.0.1:9999 level admin
defaults
mode http
log global
option httplog
timeout connect 2s
timeout client 30s
timeout server 30s
timeout http-request 10s
resolvers docker
nameserver docker 127.0.0.11:53
accepted_payload_size 8192
hold valid 1s
frontend api
bind *:8080
default_backend api_replicas
backend api_replicas
balance roundrobin
timeout check 3s
option httpchk GET /health/ready
http-check expect status 200
server-template api 4 api:8080 check inter 1s fall 2 rise 2 resolvers docker init-addr none
+16
View File
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<policymap>
<policy domain="resource" name="thread" value="1"/>
<policy domain="resource" name="memory" value="256MiB"/>
<policy domain="resource" name="map" value="256MiB"/>
<policy domain="resource" name="disk" value="512MiB"/>
<policy domain="resource" name="time" value="45"/>
<policy domain="resource" name="list-length" value="2"/>
<policy domain="delegate" rights="none" pattern="*"/>
<policy domain="filter" rights="none" pattern="*"/>
<policy domain="path" rights="none" pattern="@*"/>
<policy domain="module" rights="none" pattern="*"/>
<policy domain="module" rights="read | write" pattern="{JPEG,PNG,WEBP}"/>
<policy domain="coder" rights="none" pattern="*"/>
<policy domain="coder" rights="read | write" pattern="{JPEG,JPG,PNG,WEBP}"/>
</policymap>
+65
View File
@@ -0,0 +1,65 @@
#!/bin/sh
set -eu
for name in rustfs_root_access_key rustfs_root_secret_key s3_access_key_id s3_secret_access_key; do
if [ ! -s "/run/secrets/$name" ]; then
echo "missing S3 secret: $name" >&2
exit 1
fi
done
root_key=$(cat /run/secrets/rustfs_root_access_key)
root_secret=$(cat /run/secrets/rustfs_root_secret_key)
app_key=$(cat /run/secrets/s3_access_key_id)
app_secret=$(cat /run/secrets/s3_secret_access_key)
if [ "$root_key" = "$app_key" ]; then
echo 'API and RustFS root access keys must differ' >&2
exit 1
fi
# These values become path and JSON fragments below.
case "$S3_BUCKET" in
messenger-attachments) ;;
*) echo 'unsupported S3_BUCKET in bootstrap' >&2; exit 1 ;;
esac
rc=/usr/bin/rc
policy=messenger-attachments-api-v1
"$rc" alias set root http://s3:9000 "$root_key" "$root_secret" \
--region "$S3_REGION" --bucket-lookup path >/dev/null
"$rc" bucket create "root/$S3_BUCKET" --ignore-existing
# A new bucket is private by default; this also removes a stale public policy.
"$rc" bucket anonymous set private "root/$S3_BUCKET"
cat > /tmp/api-policy.json <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:ListBucket", "s3:GetBucketLocation"],
"Resource": "arn:aws:s3:::$S3_BUCKET"
},
{
"Effect": "Allow",
"Action": ["s3:PutObject", "s3:GetObject", "s3:DeleteObject"],
"Resource": "arn:aws:s3:::$S3_BUCKET/*"
}
]
}
EOF
if ! "$rc" admin policy info root/ "$policy" >/dev/null 2>&1; then
"$rc" admin policy create root/ "$policy" /tmp/api-policy.json
fi
if ! "$rc" admin user info root/ "$app_key" >/dev/null 2>&1; then
"$rc" admin user add root/ "$app_key" "$app_secret" >/dev/null
fi
"$rc" admin policy attach root/ "$policy" --user "$app_key" >/dev/null
"$rc" alias set app http://s3:9000 "$app_key" "$app_secret" \
--region "$S3_REGION" --bucket-lookup path >/dev/null
"$rc" bucket list "app/$S3_BUCKET" >/dev/null
echo "S3 bucket and API identity ready: $S3_BUCKET"
+119
View File
@@ -0,0 +1,119 @@
package broker
import (
"context"
"errors"
"net"
"time"
amqp "github.com/rabbitmq/amqp091-go"
)
const (
Exchange = "images"
Queue = "image.jobs"
Route = "process"
dead = "image.invalid"
)
type boundedConn struct{ net.Conn }
func (c boundedConn) Write(p []byte) (int, error) {
if err := c.SetWriteDeadline(time.Now().Add(5 * time.Second)); err != nil {
return 0, err
}
return c.Conn.Write(p)
}
// Connect supplies a freshly declared channel after a broker connection loss.
func Connect(url string) (*amqp.Connection, *amqp.Channel, error) {
dial := amqp.DefaultDial(5 * time.Second)
conn, err := amqp.DialConfig(url, amqp.Config{
Heartbeat: 10 * time.Second, Locale: "en_US",
Dial: func(network, addr string) (net.Conn, error) {
c, err := dial(network, addr)
if err != nil {
return nil, err
}
return boundedConn{c}, nil
},
})
if err != nil {
// Connection errors may include credentials in a URL.
return nil, nil, errors.New("RabbitMQ connection unavailable")
}
ch, err := conn.Channel()
if err == nil {
err = declare(ch)
}
if err != nil {
Close(conn)
return nil, nil, err
}
return conn, ch, nil
}
func Close(conn *amqp.Connection) {
_ = conn.CloseDeadline(time.Now().Add(time.Second))
}
func declare(ch *amqp.Channel) error {
if err := ch.ExchangeDeclare(Exchange, "direct", true, false, false, false, nil); err != nil {
return err
}
if _, err := ch.QueueDeclare(dead, true, false, false, false, nil); err != nil {
return err
}
if _, err := ch.QueueDeclare(Queue, true, false, false, false, amqp.Table{
"x-queue-type": "classic", "x-dead-letter-exchange": "", "x-dead-letter-routing-key": dead,
}); err != nil {
return err
}
return ch.QueueBind(Queue, Route, Exchange, false, nil)
}
// Publisher has exactly one in-flight publish. A failed/ambiguous publish
// invalidates its channel before the next event can be attempted.
type Publisher struct {
ch *amqp.Channel
returns <-chan amqp.Return
confirms <-chan amqp.Confirmation
}
func NewPublisher(ch *amqp.Channel) (*Publisher, error) {
if err := ch.Confirm(false); err != nil {
return nil, err
}
return &Publisher{
ch: ch, returns: ch.NotifyReturn(make(chan amqp.Return, 1)),
confirms: ch.NotifyPublish(make(chan amqp.Confirmation, 1)),
}, nil
}
func (p *Publisher) Publish(ctx context.Context, id string, payload []byte) error {
seq := p.ch.GetNextPublishSeqNo()
if err := p.ch.PublishWithContext(ctx, Exchange, Route, true, false, amqp.Publishing{
ContentType: "application/json", DeliveryMode: amqp.Persistent, MessageId: id, Body: payload,
}); err != nil {
return err
}
select {
case <-p.returns:
return errors.New("outbox message was not routed")
case confirm, ok := <-p.confirms:
if !ok || confirm.DeliveryTag != seq || !confirm.Ack {
return errors.New("outbox publish was not confirmed")
}
// RabbitMQ sends basic.return before its confirm for an unrouted
// mandatory publish. The client's frame reader enqueues both in order.
// With a single in-flight message any queued return is for this ID.
select {
case <-p.returns:
return errors.New("outbox message was not routed")
default:
return nil
}
case <-ctx.Done():
return ctx.Err()
}
}
+180
View File
@@ -0,0 +1,180 @@
package config
import (
"fmt"
"os"
"strconv"
"strings"
"time"
)
type Database struct {
PostgresURL string
RedisAddr string
}
type S3 struct {
Endpoint string
Region string
Bucket string
AccessKeyID string
SecretAccessKey string
}
type Config struct {
JobMaxAttempts int
HTTP struct {
Host string
Port string
}
Database Database
Session struct {
TTL time.Duration
Store string
}
S3 S3
AppEnv string
CacheURL string
}
func Load() (*Config, error) {
config := &Config{}
sessionTTL, err := getSessionTTL()
if err != nil {
return nil, err
}
config.Session.TTL = sessionTTL
config.JobMaxAttempts, err = EnvInt("JOB_MAX_ATTEMPTS", 5, 1, 20)
if err != nil {
return nil, err
}
config.HTTP.Host = os.Getenv("APP_HOST")
if config.HTTP.Host == "" {
config.HTTP.Host = "0.0.0.0"
}
config.HTTP.Port = os.Getenv("APP_PORT")
if config.HTTP.Port == "" {
config.HTTP.Port = "8080"
}
config.AppEnv = os.Getenv("APP_ENV")
if config.AppEnv == "" {
config.AppEnv = "lab"
}
store := os.Getenv("SESSION_STORE")
config.Session.Store = strings.TrimSpace(string(store))
if config.Session.Store == "" {
return nil, fmt.Errorf("empty session store data")
}
config.Database, err = getDatabaseURL()
if err != nil {
return nil, err
}
config.S3, err = getS3Data()
if err != nil {
return nil, err
}
return config, nil
}
func getSessionTTL() (time.Duration, error) {
value, ok := os.LookupEnv("SESSION_TTL_SECONDS")
if !ok {
return time.Hour, nil
}
seconds, err := strconv.ParseInt(value, 10, 64)
if err != nil || seconds <= 0 || seconds > int64((1<<63-1)/int64(time.Second)) {
return 0, fmt.Errorf("invalid SESSION_TTL_SECONDS: %q", value)
}
return time.Duration(seconds) * time.Second, nil
}
func LoadAttachmentCleanup() (postgresURL string, objectStorage S3, err error) {
postgresURL, err = LoadPostgresURL()
if err != nil {
return "", S3{}, err
}
objectStorage, err = getS3Data()
return postgresURL, objectStorage, err
}
func LoadPostgresURL() (string, error) {
postgresData, err := os.ReadFile(os.Getenv("DATABASE_URL_FILE"))
if err != nil {
return "", fmt.Errorf("read database URL: %w", err)
}
postgresURL := strings.TrimSpace(string(postgresData))
if postgresURL == "" {
return "", fmt.Errorf("empty database URL")
}
return postgresURL, nil
}
func getDatabaseURL() (Database, error) {
postgresURL, err := LoadPostgresURL()
if err != nil {
return Database{}, err
}
db := Database{PostgresURL: postgresURL}
redisData := os.Getenv("REDIS_ADDR")
db.RedisAddr = strings.TrimSpace(string(redisData))
if db.RedisAddr == "" {
return db, fmt.Errorf("redis addres is empty")
}
return db, nil
}
func getS3Data() (S3, error) {
s3 := S3{
Endpoint: os.Getenv("S3_ENDPOINT"),
Region: os.Getenv("S3_REGION"),
Bucket: os.Getenv("S3_BUCKET"),
}
if s3.Endpoint == "" {
return s3, fmt.Errorf("empty s3 endpoint")
}
if s3.Region == "" {
return s3, fmt.Errorf("empty s3 region")
}
if s3.Bucket == "" {
return s3, fmt.Errorf("emtpy s3 bucket")
}
accessKeyIDPath := os.Getenv("S3_ACCESS_KEY_ID_FILE")
accessKeyID, err := os.ReadFile(accessKeyIDPath)
if err != nil {
return s3, fmt.Errorf("empty s3 access_key_id")
}
s3.AccessKeyID = strings.TrimSpace(string(accessKeyID))
secretAccessKeyPath := os.Getenv("S3_SECRET_ACCESS_KEY_FILE")
secretAccessKey, err := os.ReadFile(secretAccessKeyPath)
if err != nil {
return s3, fmt.Errorf("empty s3 secret_access_key_file")
}
s3.SecretAccessKey = strings.TrimSpace(string(secretAccessKey))
return s3, nil
}
+77
View File
@@ -0,0 +1,77 @@
package config
import (
"fmt"
"os"
"strconv"
"strings"
"time"
)
type Pipeline struct {
BrokerURL string
Concurrency int
Prefetch int
Timeout time.Duration
Lease time.Duration
Retention time.Duration
CleanupGrace time.Duration
CleanupPeriod time.Duration
RetryBase time.Duration
}
func EnvInt(name string, fallback, minimum, maximum int) (int, error) {
value := os.Getenv(name)
if value == "" {
return fallback, nil
}
n, err := strconv.Atoi(value)
if err != nil || n < minimum || n > maximum {
return 0, fmt.Errorf("%s must be between %d and %d", name, minimum, maximum)
}
return n, nil
}
func LoadPipeline() (Pipeline, error) {
var cfg Pipeline
path := os.Getenv("BROKER_URL_FILE")
if path != "" {
data, err := os.ReadFile(path)
if err != nil {
return cfg, fmt.Errorf("cannot read broker URL file: %w", err)
}
cfg.BrokerURL = strings.TrimSpace(string(data))
}
duration := func(dst *time.Duration, name string, fallback, minimum, maximum int) error {
n, err := EnvInt(name, fallback, minimum, maximum)
*dst = time.Duration(n) * time.Second
return err
}
var err error
if cfg.Concurrency, err = EnvInt("WORKER_CONCURRENCY", 1, 1, 4); err != nil {
return cfg, err
}
if cfg.Prefetch, err = EnvInt("WORKER_PREFETCH", 1, 1, 16); err != nil {
return cfg, err
}
for _, d := range []struct {
dst *time.Duration
name string
fallback, minimum, maximum int
}{
{&cfg.Timeout, "JOB_TIMEOUT_SECONDS", 60, 1, 600},
{&cfg.Lease, "JOB_LEASE_SECONDS", 90, 2, 900},
{&cfg.Retention, "STAGING_RETENTION_SECONDS", 86400, 1, 604800},
{&cfg.CleanupGrace, "CLEANUP_GRACE_SECONDS", 600, 1, 604800},
{&cfg.CleanupPeriod, "CLEANUP_INTERVAL_SECONDS", 5, 1, 600},
{&cfg.RetryBase, "JOB_RETRY_BASE_SECONDS", 2, 1, 15},
} {
if err := duration(d.dst, d.name, d.fallback, d.minimum, d.maximum); err != nil {
return cfg, err
}
}
if cfg.Lease < cfg.Timeout+time.Second {
return cfg, fmt.Errorf("JOB_LEASE_SECONDS must exceed JOB_TIMEOUT_SECONDS")
}
return cfg, nil
}
+21
View File
@@ -0,0 +1,21 @@
package database
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
)
func NewPostgres(ctx context.Context, databaseURL string) (*pgxpool.Pool, error) {
pool, err := pgxpool.New(ctx, databaseURL)
if err != nil {
return nil, err
}
if err = pool.Ping(ctx); err != nil {
pool.Close()
return nil, err
}
return pool, nil
}
+31
View File
@@ -0,0 +1,31 @@
package database
import (
"context"
"time"
"github.com/redis/go-redis/v9"
)
func NewRedis(ctx context.Context, addr string) (*redis.Client, error) {
options := &redis.Options{
Addr: addr,
DialTimeout: 3 * time.Second,
ReadTimeout: 3 * time.Second,
WriteTimeout: 2 * time.Second,
PoolSize: 10,
PoolTimeout: 2 * time.Second,
ContextTimeoutEnabled: true,
}
client := redis.NewClient(options)
if err := client.Ping(ctx).Err(); err != nil {
_ = client.Close()
return nil, err
}
return client, nil
}
@@ -0,0 +1,88 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: attachment_variants.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createAttachmentVariant = `-- name: CreateAttachmentVariant :exec
INSERT INTO attachment_variants (
attachment_id, pipeline_version, name, object_key, content_type, size_bytes, width, height, sha256
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9
)
`
type CreateAttachmentVariantParams struct {
AttachmentID pgtype.UUID
PipelineVersion int32
Name string
ObjectKey string
ContentType string
SizeBytes int64
Width int32
Height int32
Sha256 string
}
func (q *Queries) CreateAttachmentVariant(ctx context.Context, arg CreateAttachmentVariantParams) error {
_, err := q.db.Exec(ctx, createAttachmentVariant,
arg.AttachmentID,
arg.PipelineVersion,
arg.Name,
arg.ObjectKey,
arg.ContentType,
arg.SizeBytes,
arg.Width,
arg.Height,
arg.Sha256,
)
return err
}
const listAttachmentVariants = `-- name: ListAttachmentVariants :many
SELECT attachment_id, pipeline_version, name, object_key, content_type, size_bytes, width, height, sha256 FROM attachment_variants
WHERE attachment_id = $1 AND pipeline_version = $2
ORDER BY name
`
type ListAttachmentVariantsParams struct {
AttachmentID pgtype.UUID
PipelineVersion int32
}
func (q *Queries) ListAttachmentVariants(ctx context.Context, arg ListAttachmentVariantsParams) ([]AttachmentVariant, error) {
rows, err := q.db.Query(ctx, listAttachmentVariants, arg.AttachmentID, arg.PipelineVersion)
if err != nil {
return nil, err
}
defer rows.Close()
var items []AttachmentVariant
for rows.Next() {
var i AttachmentVariant
if err := rows.Scan(
&i.AttachmentID,
&i.PipelineVersion,
&i.Name,
&i.ObjectKey,
&i.ContentType,
&i.SizeBytes,
&i.Width,
&i.Height,
&i.Sha256,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
+306
View File
@@ -0,0 +1,306 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: attachments.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const attachmentExistsByObjectKey = `-- name: AttachmentExistsByObjectKey :one
SELECT EXISTS (
SELECT 1 FROM attachments
WHERE object_key = $1::text OR staging_key = $1::text
UNION ALL
SELECT 1 FROM attachment_variants WHERE object_key = $1::text
UNION ALL
SELECT 1 FROM attachment_jobs WHERE published_original_key = $1::text
)
`
func (q *Queries) AttachmentExistsByObjectKey(ctx context.Context, objectKey string) (bool, error) {
row := q.db.QueryRow(ctx, attachmentExistsByObjectKey, objectKey)
var exists bool
err := row.Scan(&exists)
return exists, err
}
const createAttachment = `-- name: CreateAttachment :one
WITH legacy AS (
INSERT INTO attachments
(chat_id, owner_id, filename, object_key, staging_key, content_type, size_bytes, sha256)
VALUES ($1, $2, $3,
$4::text, $4::text,
$5, $6, $7)
RETURNING id, chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256, created_at, updated_at, status, error_code, staging_key, pipeline_version
), job AS (
INSERT INTO attachment_jobs (attachment_id, pipeline_version)
SELECT id, pipeline_version FROM legacy
RETURNING id, attachment_id, pipeline_version, attempts_started, max_attempts, next_attempt_at, lease_generation, lease_until, created_at, updated_at, published_original_key
), event AS (
INSERT INTO outbox (job_id, payload, dedup_key)
SELECT id, attachment_job_payload(id, attachment_id, pipeline_version, 1, created_at),
'initial/' || id
FROM job
RETURNING job_id
)
SELECT legacy.id, legacy.chat_id, legacy.owner_id, legacy.filename, legacy.object_key, legacy.content_type, legacy.size_bytes, legacy.sha256, legacy.created_at, legacy.updated_at, legacy.status, legacy.error_code, legacy.staging_key, legacy.pipeline_version FROM legacy CROSS JOIN event
`
type CreateAttachmentParams struct {
ChatID pgtype.UUID
OwnerID pgtype.UUID
Filename string
ObjectKey string
ContentType string
SizeBytes int64
Sha256 string
}
type CreateAttachmentRow struct {
ID pgtype.UUID
ChatID pgtype.UUID
OwnerID pgtype.UUID
Filename string
ObjectKey pgtype.Text
ContentType string
SizeBytes int64
Sha256 string
CreatedAt pgtype.Timestamptz
UpdatedAt pgtype.Timestamptz
Status string
ErrorCode pgtype.Text
StagingKey pgtype.Text
PipelineVersion int32
}
// Compatibility for the synchronous API until CreateQueuedAttachment is wired in.
func (q *Queries) CreateAttachment(ctx context.Context, arg CreateAttachmentParams) (CreateAttachmentRow, error) {
row := q.db.QueryRow(ctx, createAttachment,
arg.ChatID,
arg.OwnerID,
arg.Filename,
arg.ObjectKey,
arg.ContentType,
arg.SizeBytes,
arg.Sha256,
)
var i CreateAttachmentRow
err := row.Scan(
&i.ID,
&i.ChatID,
&i.OwnerID,
&i.Filename,
&i.ObjectKey,
&i.ContentType,
&i.SizeBytes,
&i.Sha256,
&i.CreatedAt,
&i.UpdatedAt,
&i.Status,
&i.ErrorCode,
&i.StagingKey,
&i.PipelineVersion,
)
return i, err
}
const createQueuedAttachment = `-- name: CreateQueuedAttachment :one
INSERT INTO attachments (
id, chat_id, owner_id, filename, content_type, size_bytes, sha256, staging_key, status
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, 'queued'
) RETURNING id, chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256, created_at, updated_at, status, error_code, staging_key, pipeline_version
`
type CreateQueuedAttachmentParams struct {
ID pgtype.UUID
ChatID pgtype.UUID
OwnerID pgtype.UUID
Filename string
ContentType string
SizeBytes int64
Sha256 string
StagingKey pgtype.Text
}
func (q *Queries) CreateQueuedAttachment(ctx context.Context, arg CreateQueuedAttachmentParams) (Attachment, error) {
row := q.db.QueryRow(ctx, createQueuedAttachment,
arg.ID,
arg.ChatID,
arg.OwnerID,
arg.Filename,
arg.ContentType,
arg.SizeBytes,
arg.Sha256,
arg.StagingKey,
)
var i Attachment
err := row.Scan(
&i.ID,
&i.ChatID,
&i.OwnerID,
&i.Filename,
&i.ObjectKey,
&i.ContentType,
&i.SizeBytes,
&i.Sha256,
&i.CreatedAt,
&i.UpdatedAt,
&i.Status,
&i.ErrorCode,
&i.StagingKey,
&i.PipelineVersion,
)
return i, err
}
const getAttachmentByID = `-- name: GetAttachmentByID :one
SELECT id, chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256, created_at, updated_at, status, error_code, staging_key, pipeline_version FROM attachments WHERE id = $1
`
func (q *Queries) GetAttachmentByID(ctx context.Context, attachmentID pgtype.UUID) (Attachment, error) {
row := q.db.QueryRow(ctx, getAttachmentByID, attachmentID)
var i Attachment
err := row.Scan(
&i.ID,
&i.ChatID,
&i.OwnerID,
&i.Filename,
&i.ObjectKey,
&i.ContentType,
&i.SizeBytes,
&i.Sha256,
&i.CreatedAt,
&i.UpdatedAt,
&i.Status,
&i.ErrorCode,
&i.StagingKey,
&i.PipelineVersion,
)
return i, err
}
const getAttachmentForUser = `-- name: GetAttachmentForUser :one
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.object_key, a.content_type, a.size_bytes, a.sha256, a.created_at, a.updated_at, a.status, a.error_code, a.staging_key, a.pipeline_version
FROM attachments AS a
JOIN chat_members AS cm ON cm.chat_id = a.chat_id
WHERE a.id = $1
AND cm.user_id = $2
`
type GetAttachmentForUserParams struct {
AttachmentID pgtype.UUID
UserID pgtype.UUID
}
func (q *Queries) GetAttachmentForUser(ctx context.Context, arg GetAttachmentForUserParams) (Attachment, error) {
row := q.db.QueryRow(ctx, getAttachmentForUser, arg.AttachmentID, arg.UserID)
var i Attachment
err := row.Scan(
&i.ID,
&i.ChatID,
&i.OwnerID,
&i.Filename,
&i.ObjectKey,
&i.ContentType,
&i.SizeBytes,
&i.Sha256,
&i.CreatedAt,
&i.UpdatedAt,
&i.Status,
&i.ErrorCode,
&i.StagingKey,
&i.PipelineVersion,
)
return i, err
}
const getAttachmentsForMessages = `-- name: GetAttachmentsForMessages :many
SELECT message_id, attachment_id, position
FROM messages_attachments
WHERE message_id = ANY($1::uuid[])
ORDER BY message_id, position
`
type GetAttachmentsForMessagesRow struct {
MessageID pgtype.UUID
AttachmentID pgtype.UUID
Position int16
}
func (q *Queries) GetAttachmentsForMessages(ctx context.Context, messageIds []pgtype.UUID) ([]GetAttachmentsForMessagesRow, error) {
rows, err := q.db.Query(ctx, getAttachmentsForMessages, messageIds)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetAttachmentsForMessagesRow
for rows.Next() {
var i GetAttachmentsForMessagesRow
if err := rows.Scan(&i.MessageID, &i.AttachmentID, &i.Position); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listAttachmentKeys = `-- name: ListAttachmentKeys :many
SELECT id, object_key, created_at
FROM attachments
WHERE object_key > $1
ORDER BY object_key
LIMIT $2
`
type ListAttachmentKeysParams struct {
AfterKey pgtype.Text
PageSize int32
}
type ListAttachmentKeysRow struct {
ID pgtype.UUID
ObjectKey pgtype.Text
CreatedAt pgtype.Timestamptz
}
func (q *Queries) ListAttachmentKeys(ctx context.Context, arg ListAttachmentKeysParams) ([]ListAttachmentKeysRow, error) {
rows, err := q.db.Query(ctx, listAttachmentKeys, arg.AfterKey, arg.PageSize)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListAttachmentKeysRow
for rows.Next() {
var i ListAttachmentKeysRow
if err := rows.Scan(&i.ID, &i.ObjectKey, &i.CreatedAt); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const tryLockAttachmentObjectKey = `-- name: TryLockAttachmentObjectKey :one
SELECT pg_try_advisory_xact_lock(
hashtextextended($1::text, 0)
)::boolean AS locked
`
func (q *Queries) TryLockAttachmentObjectKey(ctx context.Context, objectKey string) (bool, error) {
row := q.db.QueryRow(ctx, tryLockAttachmentObjectKey, objectKey)
var locked bool
err := row.Scan(&locked)
return locked, err
}
@@ -0,0 +1,529 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: attachments_jobs.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const claimJob = `-- name: ClaimJob :one
WITH claimed AS (
UPDATE attachment_jobs AS j
SET lease_generation = j.lease_generation + 1,
lease_until = $1::timestamptz,
attempts_started = j.attempts_started + 1,
next_attempt_at = NULL,
updated_at = clock_timestamp()
FROM attachments AS a
WHERE j.id = $2
AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version
AND j.attempts_started < j.max_attempts
AND $1::timestamptz > clock_timestamp()
AND (
(a.status = 'queued' AND j.lease_until IS NULL AND j.next_attempt_at <= clock_timestamp())
OR (a.status = 'processing' AND j.lease_until <= clock_timestamp())
)
RETURNING j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key
), expired AS (
UPDATE attachment_attempts AS attempt
SET outcome = 'lease_expired', error_code = 'lease_expired', finished_at = claimed.updated_at
FROM claimed
WHERE attempt.job_id = claimed.id
AND attempt.lease_generation = claimed.lease_generation - 1 AND attempt.finished_at IS NULL
), recorded AS (
INSERT INTO attachment_attempts (job_id, lease_generation, attempt, started_at)
SELECT id, lease_generation, attempts_started, updated_at FROM claimed
RETURNING job_id
), processing AS (
UPDATE attachments AS a
SET status = 'processing', updated_at = claimed.updated_at
FROM claimed
WHERE a.id = claimed.attachment_id
RETURNING a.id
)
SELECT claimed.id, claimed.attachment_id, claimed.pipeline_version, claimed.attempts_started, claimed.max_attempts, claimed.next_attempt_at, claimed.lease_generation, claimed.lease_until, claimed.created_at, claimed.updated_at, claimed.published_original_key FROM claimed JOIN recorded ON recorded.job_id = claimed.id
JOIN processing ON processing.id = claimed.attachment_id
`
type ClaimJobParams struct {
NewLeaseUntil pgtype.Timestamptz
JobID pgtype.UUID
}
type ClaimJobRow struct {
ID pgtype.UUID
AttachmentID pgtype.UUID
PipelineVersion int32
AttemptsStarted int32
MaxAttempts int32
NextAttemptAt pgtype.Timestamptz
LeaseGeneration int32
LeaseUntil pgtype.Timestamptz
CreatedAt pgtype.Timestamptz
UpdatedAt pgtype.Timestamptz
PublishedOriginalKey pgtype.Text
}
func (q *Queries) ClaimJob(ctx context.Context, arg ClaimJobParams) (ClaimJobRow, error) {
row := q.db.QueryRow(ctx, claimJob, arg.NewLeaseUntil, arg.JobID)
var i ClaimJobRow
err := row.Scan(
&i.ID,
&i.AttachmentID,
&i.PipelineVersion,
&i.AttemptsStarted,
&i.MaxAttempts,
&i.NextAttemptAt,
&i.LeaseGeneration,
&i.LeaseUntil,
&i.CreatedAt,
&i.UpdatedAt,
&i.PublishedOriginalKey,
)
return i, err
}
const clearStagingReference = `-- name: ClearStagingReference :execrows
UPDATE attachments
SET object_key = CASE WHEN status = 'failed' AND object_key = staging_key THEN NULL ELSE object_key END,
staging_key = NULL
WHERE id = $1 AND staging_key = $2
`
type ClearStagingReferenceParams struct {
ID pgtype.UUID
StagingKey pgtype.Text
}
func (q *Queries) ClearStagingReference(ctx context.Context, arg ClearStagingReferenceParams) (int64, error) {
result, err := q.db.Exec(ctx, clearStagingReference, arg.ID, arg.StagingKey)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const completeJob = `-- name: CompleteJob :execrows
WITH completed AS (
UPDATE attachment_jobs AS j
SET lease_until = NULL, next_attempt_at = NULL, updated_at = clock_timestamp(),
published_original_key = $1::text
FROM attachments AS a
WHERE j.id = $2
AND j.lease_generation = $3 AND j.lease_until > clock_timestamp()
AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version AND a.status = 'processing'
AND (SELECT count(*) FROM attachment_variants AS v
WHERE v.attachment_id = a.id AND v.pipeline_version = j.pipeline_version) = 3
RETURNING j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key
), recorded AS (
UPDATE attachment_attempts AS attempt
SET outcome = 'ready', finished_at = completed.updated_at
FROM completed
WHERE attempt.job_id = completed.id AND attempt.lease_generation = completed.lease_generation
)
UPDATE attachments AS a
SET status = 'ready', object_key = $1::text, updated_at = completed.updated_at
FROM completed
WHERE a.id = completed.attachment_id
`
type CompleteJobParams struct {
ObjectKey string
JobID pgtype.UUID
LeaseGeneration int32
}
func (q *Queries) CompleteJob(ctx context.Context, arg CompleteJobParams) (int64, error) {
result, err := q.db.Exec(ctx, completeJob, arg.ObjectKey, arg.JobID, arg.LeaseGeneration)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const createAttachmentJob = `-- name: CreateAttachmentJob :one
INSERT INTO attachment_jobs (attachment_id, pipeline_version, next_attempt_at, max_attempts)
VALUES ($1, $2,
$3::timestamptz, $4)
RETURNING id, attachment_id, pipeline_version, attempts_started, max_attempts, next_attempt_at, lease_generation, lease_until, created_at, updated_at, published_original_key
`
type CreateAttachmentJobParams struct {
AttachmentID pgtype.UUID
PipelineVersion int32
AvailableAt pgtype.Timestamptz
MaxAttempts int32
}
func (q *Queries) CreateAttachmentJob(ctx context.Context, arg CreateAttachmentJobParams) (AttachmentJob, error) {
row := q.db.QueryRow(ctx, createAttachmentJob,
arg.AttachmentID,
arg.PipelineVersion,
arg.AvailableAt,
arg.MaxAttempts,
)
var i AttachmentJob
err := row.Scan(
&i.ID,
&i.AttachmentID,
&i.PipelineVersion,
&i.AttemptsStarted,
&i.MaxAttempts,
&i.NextAttemptAt,
&i.LeaseGeneration,
&i.LeaseUntil,
&i.CreatedAt,
&i.UpdatedAt,
&i.PublishedOriginalKey,
)
return i, err
}
const failJob = `-- name: FailJob :execrows
WITH failed AS (
UPDATE attachment_jobs AS j
SET lease_until = NULL, next_attempt_at = NULL, updated_at = clock_timestamp()
FROM attachments AS a
WHERE j.id = $2
AND j.lease_generation = $3
AND (
j.lease_until > clock_timestamp()
OR (j.lease_until <= clock_timestamp() AND j.attempts_started = j.max_attempts
AND $1::text = 'retry_exhausted')
)
AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version AND a.status = 'processing'
RETURNING j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key
), recorded AS (
UPDATE attachment_attempts AS attempt
SET outcome = 'failed', error_code = $1::text, finished_at = failed.updated_at
FROM failed
WHERE attempt.job_id = failed.id AND attempt.lease_generation = failed.lease_generation
)
UPDATE attachments AS a
SET status = 'failed', error_code = $1::text, updated_at = failed.updated_at
FROM failed WHERE a.id = failed.attachment_id
`
type FailJobParams struct {
ErrorCode string
JobID pgtype.UUID
LeaseGeneration int32
}
func (q *Queries) FailJob(ctx context.Context, arg FailJobParams) (int64, error) {
result, err := q.db.Exec(ctx, failJob, arg.ErrorCode, arg.JobID, arg.LeaseGeneration)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const getAttachmentByStagingKey = `-- name: GetAttachmentByStagingKey :one
SELECT id, chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256, created_at, updated_at, status, error_code, staging_key, pipeline_version FROM attachments WHERE staging_key = $1::text
`
func (q *Queries) GetAttachmentByStagingKey(ctx context.Context, stagingKey string) (Attachment, error) {
row := q.db.QueryRow(ctx, getAttachmentByStagingKey, stagingKey)
var i Attachment
err := row.Scan(
&i.ID,
&i.ChatID,
&i.OwnerID,
&i.Filename,
&i.ObjectKey,
&i.ContentType,
&i.SizeBytes,
&i.Sha256,
&i.CreatedAt,
&i.UpdatedAt,
&i.Status,
&i.ErrorCode,
&i.StagingKey,
&i.PipelineVersion,
)
return i, err
}
const getAttachmentJobState = `-- name: GetAttachmentJobState :one
SELECT
j.id, j.attachment_id, j.pipeline_version,
CASE WHEN a.pipeline_version = j.pipeline_version THEN a.status ELSE 'superseded' END::text AS status,
j.lease_generation,
j.lease_until,
coalesce(j.lease_until <= clock_timestamp(), false)::boolean AS lease_expired,
j.attempts_started,
j.max_attempts,
j.next_attempt_at
FROM attachment_jobs AS j
JOIN attachments AS a ON a.id = j.attachment_id
WHERE j.id = $1::uuid
`
type GetAttachmentJobStateRow struct {
ID pgtype.UUID
AttachmentID pgtype.UUID
PipelineVersion int32
Status string
LeaseGeneration int32
LeaseUntil pgtype.Timestamptz
LeaseExpired bool
AttemptsStarted int32
MaxAttempts int32
NextAttemptAt pgtype.Timestamptz
}
func (q *Queries) GetAttachmentJobState(ctx context.Context, jobID pgtype.UUID) (GetAttachmentJobStateRow, error) {
row := q.db.QueryRow(ctx, getAttachmentJobState, jobID)
var i GetAttachmentJobStateRow
err := row.Scan(
&i.ID,
&i.AttachmentID,
&i.PipelineVersion,
&i.Status,
&i.LeaseGeneration,
&i.LeaseUntil,
&i.LeaseExpired,
&i.AttemptsStarted,
&i.MaxAttempts,
&i.NextAttemptAt,
)
return i, err
}
const listFailedAttachmentJobs = `-- name: ListFailedAttachmentJobs :many
SELECT j.id, j.attachment_id, j.attempts_started, a.error_code, a.updated_at
FROM attachment_jobs j
JOIN attachments a ON a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version
WHERE a.status = 'failed'
ORDER BY a.updated_at DESC, j.id
LIMIT $1
`
type ListFailedAttachmentJobsRow struct {
ID pgtype.UUID
AttachmentID pgtype.UUID
AttemptsStarted int32
ErrorCode pgtype.Text
UpdatedAt pgtype.Timestamptz
}
func (q *Queries) ListFailedAttachmentJobs(ctx context.Context, batchSize int32) ([]ListFailedAttachmentJobsRow, error) {
rows, err := q.db.Query(ctx, listFailedAttachmentJobs, batchSize)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListFailedAttachmentJobsRow
for rows.Next() {
var i ListFailedAttachmentJobsRow
if err := rows.Scan(
&i.ID,
&i.AttachmentID,
&i.AttemptsStarted,
&i.ErrorCode,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const lockActiveAttachmentJob = `-- name: LockActiveAttachmentJob :one
SELECT j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key
FROM attachment_jobs AS j JOIN attachments AS a ON a.id = j.attachment_id
WHERE j.id = $1
AND j.lease_generation = $2
AND j.lease_until > clock_timestamp()
AND a.status = 'processing' AND a.pipeline_version = j.pipeline_version
FOR UPDATE OF j
`
type LockActiveAttachmentJobParams struct {
JobID pgtype.UUID
LeaseGeneration int32
}
// Hold this lock through variant insertion and CompleteJob. Roll back on a zero-row completion.
func (q *Queries) LockActiveAttachmentJob(ctx context.Context, arg LockActiveAttachmentJobParams) (AttachmentJob, error) {
row := q.db.QueryRow(ctx, lockActiveAttachmentJob, arg.JobID, arg.LeaseGeneration)
var i AttachmentJob
err := row.Scan(
&i.ID,
&i.AttachmentID,
&i.PipelineVersion,
&i.AttemptsStarted,
&i.MaxAttempts,
&i.NextAttemptAt,
&i.LeaseGeneration,
&i.LeaseUntil,
&i.CreatedAt,
&i.UpdatedAt,
&i.PublishedOriginalKey,
)
return i, err
}
const lockAttachmentJobForPipeline = `-- name: LockAttachmentJobForPipeline :one
SELECT j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key
FROM attachment_jobs j
WHERE j.attachment_id = $1 AND j.pipeline_version = $2
FOR UPDATE
`
type LockAttachmentJobForPipelineParams struct {
AttachmentID pgtype.UUID
PipelineVersion int32
}
func (q *Queries) LockAttachmentJobForPipeline(ctx context.Context, arg LockAttachmentJobForPipelineParams) (AttachmentJob, error) {
row := q.db.QueryRow(ctx, lockAttachmentJobForPipeline, arg.AttachmentID, arg.PipelineVersion)
var i AttachmentJob
err := row.Scan(
&i.ID,
&i.AttachmentID,
&i.PipelineVersion,
&i.AttemptsStarted,
&i.MaxAttempts,
&i.NextAttemptAt,
&i.LeaseGeneration,
&i.LeaseUntil,
&i.CreatedAt,
&i.UpdatedAt,
&i.PublishedOriginalKey,
)
return i, err
}
const lockAttachmentJobForReplay = `-- name: LockAttachmentJobForReplay :one
SELECT j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key
FROM attachment_jobs j WHERE j.id = $1
FOR UPDATE
`
func (q *Queries) LockAttachmentJobForReplay(ctx context.Context, id pgtype.UUID) (AttachmentJob, error) {
row := q.db.QueryRow(ctx, lockAttachmentJobForReplay, id)
var i AttachmentJob
err := row.Scan(
&i.ID,
&i.AttachmentID,
&i.PipelineVersion,
&i.AttemptsStarted,
&i.MaxAttempts,
&i.NextAttemptAt,
&i.LeaseGeneration,
&i.LeaseUntil,
&i.CreatedAt,
&i.UpdatedAt,
&i.PublishedOriginalKey,
)
return i, err
}
const replayFailed = `-- name: ReplayFailed :execrows
WITH replayed AS (
UPDATE attachment_jobs AS j
SET max_attempts = j.attempts_started + $1::int,
next_attempt_at = clock_timestamp(), updated_at = clock_timestamp()
FROM attachments AS a
WHERE j.id = $2
AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version
AND a.status = 'failed' AND a.staging_key IS NOT NULL
AND j.lease_until IS NULL AND j.next_attempt_at IS NULL
AND NOT EXISTS (
SELECT 1 FROM attachment_replays AS r WHERE r.request_id = $3::uuid
)
RETURNING j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key
), recorded AS (
INSERT INTO attachment_replays (request_id, job_id, operator_id, reason, extra_attempts)
SELECT $3::uuid, id, $4, $5, $1
FROM replayed
), event AS (
INSERT INTO outbox (job_id, payload, available_at, dedup_key)
SELECT id, attachment_job_payload(id, attachment_id, pipeline_version, attempts_started + 1, updated_at),
next_attempt_at, 'replay/' || id || '/' || $3::uuid
FROM replayed
)
UPDATE attachments AS a
SET status = 'queued', error_code = NULL, updated_at = replayed.updated_at
FROM replayed WHERE a.id = replayed.attachment_id
`
type ReplayFailedParams struct {
ExtraAttempts int32
JobID pgtype.UUID
RequestID pgtype.UUID
OperatorID string
Reason string
}
func (q *Queries) ReplayFailed(ctx context.Context, arg ReplayFailedParams) (int64, error) {
result, err := q.db.Exec(ctx, replayFailed,
arg.ExtraAttempts,
arg.JobID,
arg.RequestID,
arg.OperatorID,
arg.Reason,
)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const scheduleRetry = `-- name: ScheduleRetry :execrows
WITH scheduled AS (
UPDATE attachment_jobs AS j
SET lease_until = NULL, next_attempt_at = $1::timestamptz,
updated_at = clock_timestamp()
FROM attachments AS a
WHERE j.id = $2
AND j.lease_generation = $3 AND j.lease_until > clock_timestamp()
AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version AND a.status = 'processing'
AND j.attempts_started < j.max_attempts
AND $1::timestamptz > clock_timestamp()
RETURNING j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key
), recorded AS (
UPDATE attachment_attempts AS attempt
SET outcome = 'retry', error_code = $4::text, finished_at = scheduled.updated_at
FROM scheduled
WHERE attempt.job_id = scheduled.id AND attempt.lease_generation = scheduled.lease_generation
), event AS (
INSERT INTO outbox (job_id, payload, available_at, dedup_key)
SELECT id, attachment_job_payload(id, attachment_id, pipeline_version, attempts_started + 1, updated_at),
next_attempt_at, 'retry/' || id || '/' || (attempts_started + 1)
FROM scheduled
)
UPDATE attachments AS a
SET status = 'queued', updated_at = scheduled.updated_at
FROM scheduled WHERE a.id = scheduled.attachment_id
`
type ScheduleRetryParams struct {
AvailableAt pgtype.Timestamptz
JobID pgtype.UUID
LeaseGeneration int32
ErrorCode string
}
func (q *Queries) ScheduleRetry(ctx context.Context, arg ScheduleRetryParams) (int64, error) {
result, err := q.db.Exec(ctx, scheduleRetry,
arg.AvailableAt,
arg.JobID,
arg.LeaseGeneration,
arg.ErrorCode,
)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
@@ -0,0 +1,27 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: chat_members.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createChatMember = `-- name: CreateChatMember :exec
INSERT INTO chat_members(chat_id, user_id)
VALUES ($1, $2)
`
type CreateChatMemberParams struct {
ChatID pgtype.UUID
UserID pgtype.UUID
}
func (q *Queries) CreateChatMember(ctx context.Context, arg CreateChatMemberParams) error {
_, err := q.db.Exec(ctx, createChatMember, arg.ChatID, arg.UserID)
return err
}
+118
View File
@@ -0,0 +1,118 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: chats.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createChat = `-- name: CreateChat :one
INSERT INTO chats (title)
VALUES ($1)
RETURNING id, title, created_at
`
func (q *Queries) CreateChat(ctx context.Context, title string) (Chat, error) {
row := q.db.QueryRow(ctx, createChat, title)
var i Chat
err := row.Scan(&i.ID, &i.Title, &i.CreatedAt)
return i, err
}
const getChatByID = `-- name: GetChatByID :one
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id)::uuid[] AS member_ids
FROM chats c
JOIN chat_members cm ON cm.chat_id = c.id
WHERE c.id = $1
GROUP BY c.id
`
type GetChatByIDRow struct {
ID pgtype.UUID
Title string
CreatedAt pgtype.Timestamptz
MemberIds []pgtype.UUID
}
func (q *Queries) GetChatByID(ctx context.Context, id pgtype.UUID) (GetChatByIDRow, error) {
row := q.db.QueryRow(ctx, getChatByID, id)
var i GetChatByIDRow
err := row.Scan(
&i.ID,
&i.Title,
&i.CreatedAt,
&i.MemberIds,
)
return i, err
}
const getChatsByUserID = `-- name: GetChatsByUserID :many
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id)::uuid[] AS member_ids
FROM chats c
JOIN chat_members cm ON cm.chat_id = c.id
WHERE EXISTS (SELECT 1
FROM chat_members membership
WHERE membership.chat_id = c.id
AND membership.user_id = $1)
AND (
$2::timestamptz IS NULL
OR (c.created_at, c.id) >
(
$2::timestamptz,
$3::uuid
)
)
GROUP BY c.id
ORDER BY c.created_at ASC, c.id ASC
LIMIT $4
`
type GetChatsByUserIDParams struct {
UserID pgtype.UUID
AfterCreatedAt pgtype.Timestamptz
AfterID pgtype.UUID
LimitCount int32
}
type GetChatsByUserIDRow struct {
ID pgtype.UUID
Title string
CreatedAt pgtype.Timestamptz
MemberIds []pgtype.UUID
}
func (q *Queries) GetChatsByUserID(ctx context.Context, arg GetChatsByUserIDParams) ([]GetChatsByUserIDRow, error) {
rows, err := q.db.Query(ctx, getChatsByUserID,
arg.UserID,
arg.AfterCreatedAt,
arg.AfterID,
arg.LimitCount,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetChatsByUserIDRow
for rows.Next() {
var i GetChatsByUserIDRow
if err := rows.Scan(
&i.ID,
&i.Title,
&i.CreatedAt,
&i.MemberIds,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
+32
View File
@@ -0,0 +1,32 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
package sqlc
import (
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type DBTX interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
return &Queries{
db: tx,
}
}
@@ -0,0 +1,99 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: message_idempotence.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const deleteExpiredMessageIdempotency = `-- name: DeleteExpiredMessageIdempotency :exec
DELETE FROM message_idempotency
WHERE user_id = $1 AND chat_id = $2 AND idempotency_key = $3 AND expires_at <= now()
`
type DeleteExpiredMessageIdempotencyParams struct {
UserID pgtype.UUID
ChatID pgtype.UUID
IdempotencyKey string
}
func (q *Queries) DeleteExpiredMessageIdempotency(ctx context.Context, arg DeleteExpiredMessageIdempotencyParams) error {
_, err := q.db.Exec(ctx, deleteExpiredMessageIdempotency, arg.UserID, arg.ChatID, arg.IdempotencyKey)
return err
}
const getIdempotentMessage = `-- name: GetIdempotentMessage :one
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at
FROM message_idempotency AS i
JOIN messages AS m ON m.id = i.message_id
WHERE i.user_id = $1
AND i.chat_id = $2
AND i.idempotency_key = $3
`
type GetIdempotentMessageParams struct {
UserID pgtype.UUID
ChatID pgtype.UUID
IdempotencyKey string
}
func (q *Queries) GetIdempotentMessage(ctx context.Context, arg GetIdempotentMessageParams) (Message, error) {
row := q.db.QueryRow(ctx, getIdempotentMessage, arg.UserID, arg.ChatID, arg.IdempotencyKey)
var i Message
err := row.Scan(
&i.ID,
&i.ChatID,
&i.SenderID,
&i.Text,
&i.CreatedAt,
)
return i, err
}
const insertMessageIdempotency = `-- name: InsertMessageIdempotency :execrows
INSERT INTO message_idempotency(user_id, chat_id, idempotency_key, message_id)
VALUES ($1, $2, $3, $4)
ON CONFLICT (user_id, chat_id, idempotency_key) DO NOTHING
`
type InsertMessageIdempotencyParams struct {
UserID pgtype.UUID
ChatID pgtype.UUID
IdempotencyKey string
MessageID pgtype.UUID
}
func (q *Queries) InsertMessageIdempotency(ctx context.Context, arg InsertMessageIdempotencyParams) (int64, error) {
result, err := q.db.Exec(ctx, insertMessageIdempotency,
arg.UserID,
arg.ChatID,
arg.IdempotencyKey,
arg.MessageID,
)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const lockMessageRequest = `-- name: LockMessageRequest :exec
SELECT pg_advisory_xact_lock(hashtextextended(
'message/' || ($1::uuid)::text || '/' ||
($2::uuid)::text || '/' || $3::text, 0))
`
type LockMessageRequestParams struct {
UserID pgtype.UUID
ChatID pgtype.UUID
IdempotencyKey string
}
func (q *Queries) LockMessageRequest(ctx context.Context, arg LockMessageRequestParams) error {
_, err := q.db.Exec(ctx, lockMessageRequest, arg.UserID, arg.ChatID, arg.IdempotencyKey)
return err
}
+91
View File
@@ -0,0 +1,91 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: messages.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createMessage = `-- name: CreateMessage :one
INSERT INTO messages (chat_id, sender_id, text)
VALUES ($1, $2, $3)
RETURNING id, chat_id, sender_id, text, created_at
`
type CreateMessageParams struct {
ChatID pgtype.UUID
SenderID pgtype.UUID
Text string
}
func (q *Queries) CreateMessage(ctx context.Context, arg CreateMessageParams) (Message, error) {
row := q.db.QueryRow(ctx, createMessage, arg.ChatID, arg.SenderID, arg.Text)
var i Message
err := row.Scan(
&i.ID,
&i.ChatID,
&i.SenderID,
&i.Text,
&i.CreatedAt,
)
return i, err
}
const getMessagesByChatID = `-- name: GetMessagesByChatID :many
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = $1
AND (
$2::timestamptz IS NULL
OR (created_at, id) >
($2::timestamptz,
$3::uuid
)
)
ORDER BY created_at ASC, id ASC
LIMIT $4
`
type GetMessagesByChatIDParams struct {
ChatID pgtype.UUID
AfterCreatedAt pgtype.Timestamptz
AfterID pgtype.UUID
LimitCount int32
}
func (q *Queries) GetMessagesByChatID(ctx context.Context, arg GetMessagesByChatIDParams) ([]Message, error) {
rows, err := q.db.Query(ctx, getMessagesByChatID,
arg.ChatID,
arg.AfterCreatedAt,
arg.AfterID,
arg.LimitCount,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Message
for rows.Next() {
var i Message
if err := rows.Scan(
&i.ID,
&i.ChatID,
&i.SenderID,
&i.Text,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
@@ -0,0 +1,107 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: messages_attachments.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createMessagesAttachments = `-- name: CreateMessagesAttachments :execrows
INSERT INTO messages_attachments (message_id, attachment_id, chat_id, position)
SELECT $1, $2, $3, $4
FROM attachments
WHERE id = $2 AND chat_id = $3
`
type CreateMessagesAttachmentsParams struct {
MessageID pgtype.UUID
AttachmentID pgtype.UUID
ChatID pgtype.UUID
Position int16
}
func (q *Queries) CreateMessagesAttachments(ctx context.Context, arg CreateMessagesAttachmentsParams) (int64, error) {
result, err := q.db.Exec(ctx, createMessagesAttachments,
arg.MessageID,
arg.AttachmentID,
arg.ChatID,
arg.Position,
)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const getMessagesAttachments = `-- name: GetMessagesAttachments :many
SELECT attachment_id, position
FROM messages_attachments
WHERE message_id = $1
ORDER BY position ASC
`
type GetMessagesAttachmentsRow struct {
AttachmentID pgtype.UUID
Position int16
}
func (q *Queries) GetMessagesAttachments(ctx context.Context, messageID pgtype.UUID) ([]GetMessagesAttachmentsRow, error) {
rows, err := q.db.Query(ctx, getMessagesAttachments, messageID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetMessagesAttachmentsRow
for rows.Next() {
var i GetMessagesAttachmentsRow
if err := rows.Scan(&i.AttachmentID, &i.Position); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const lockAttachmentsForMessage = `-- name: LockAttachmentsForMessage :many
SELECT id, status FROM attachments
WHERE chat_id = $1 AND id = ANY($2::uuid[])
ORDER BY id FOR SHARE
`
type LockAttachmentsForMessageParams struct {
ChatID pgtype.UUID
AttachmentIds []pgtype.UUID
}
type LockAttachmentsForMessageRow struct {
ID pgtype.UUID
Status string
}
func (q *Queries) LockAttachmentsForMessage(ctx context.Context, arg LockAttachmentsForMessageParams) ([]LockAttachmentsForMessageRow, error) {
rows, err := q.db.Query(ctx, lockAttachmentsForMessage, arg.ChatID, arg.AttachmentIds)
if err != nil {
return nil, err
}
defer rows.Close()
var items []LockAttachmentsForMessageRow
for rows.Next() {
var i LockAttachmentsForMessageRow
if err := rows.Scan(&i.ID, &i.Status); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
+124
View File
@@ -0,0 +1,124 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
package sqlc
import (
"github.com/jackc/pgx/v5/pgtype"
)
type Attachment struct {
ID pgtype.UUID
ChatID pgtype.UUID
OwnerID pgtype.UUID
Filename string
ObjectKey pgtype.Text
ContentType string
SizeBytes int64
Sha256 string
CreatedAt pgtype.Timestamptz
UpdatedAt pgtype.Timestamptz
Status string
ErrorCode pgtype.Text
StagingKey pgtype.Text
PipelineVersion int32
}
type AttachmentAttempt struct {
JobID pgtype.UUID
LeaseGeneration int32
Attempt int32
StartedAt pgtype.Timestamptz
FinishedAt pgtype.Timestamptz
Outcome pgtype.Text
ErrorCode pgtype.Text
}
type AttachmentJob struct {
ID pgtype.UUID
AttachmentID pgtype.UUID
PipelineVersion int32
AttemptsStarted int32
MaxAttempts int32
NextAttemptAt pgtype.Timestamptz
LeaseGeneration int32
LeaseUntil pgtype.Timestamptz
CreatedAt pgtype.Timestamptz
UpdatedAt pgtype.Timestamptz
PublishedOriginalKey pgtype.Text
}
type AttachmentReplay struct {
RequestID pgtype.UUID
JobID pgtype.UUID
OperatorID string
Reason string
ExtraAttempts int32
CreatedAt pgtype.Timestamptz
}
type AttachmentVariant struct {
AttachmentID pgtype.UUID
PipelineVersion int32
Name string
ObjectKey string
ContentType string
SizeBytes int64
Width int32
Height int32
Sha256 string
}
type Chat struct {
ID pgtype.UUID
Title string
CreatedAt pgtype.Timestamptz
}
type ChatMember struct {
ChatID pgtype.UUID
UserID pgtype.UUID
}
type Message struct {
ID pgtype.UUID
ChatID pgtype.UUID
SenderID pgtype.UUID
Text string
CreatedAt pgtype.Timestamptz
}
type MessageIdempotency struct {
UserID pgtype.UUID
ChatID pgtype.UUID
IdempotencyKey string
MessageID pgtype.UUID
CreatedAt pgtype.Timestamptz
ExpiresAt pgtype.Timestamptz
}
type MessagesAttachment struct {
MessageID pgtype.UUID
AttachmentID pgtype.UUID
ChatID pgtype.UUID
Position int16
}
type Outbox struct {
ID pgtype.UUID
JobID pgtype.UUID
Payload []byte
AvailableAt pgtype.Timestamptz
SentAt pgtype.Timestamptz
CreatedAt pgtype.Timestamptz
DedupKey string
}
type User struct {
ID pgtype.UUID
Username string
DisplayName string
PasswordHash string
CreatedAt pgtype.Timestamptz
}
+111
View File
@@ -0,0 +1,111 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: outbox.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createInitialOutboxEvent = `-- name: CreateInitialOutboxEvent :exec
INSERT INTO outbox (job_id, payload, available_at, dedup_key)
SELECT id, attachment_job_payload(id, attachment_id, pipeline_version, 1, created_at),
next_attempt_at, 'initial/' || id
FROM attachment_jobs WHERE id = $1::uuid
`
func (q *Queries) CreateInitialOutboxEvent(ctx context.Context, jobID pgtype.UUID) error {
_, err := q.db.Exec(ctx, createInitialOutboxEvent, jobID)
return err
}
const enqueueExpiredLeases = `-- name: EnqueueExpiredLeases :many
WITH expired AS (
SELECT j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key, 'recovery/' || j.id || '/' || j.lease_generation AS recovery_key
FROM attachment_jobs AS j
JOIN attachments AS a ON a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version
WHERE a.status = 'processing' AND j.lease_until <= clock_timestamp()
AND NOT EXISTS (
SELECT 1 FROM outbox AS o WHERE o.dedup_key = 'recovery/' || j.id || '/' || j.lease_generation
)
ORDER BY j.lease_until, j.id
LIMIT $1
FOR UPDATE OF j SKIP LOCKED
)
INSERT INTO outbox (job_id, payload, dedup_key)
SELECT id, attachment_job_payload(id, attachment_id, pipeline_version,
LEAST(attempts_started + 1, max_attempts), clock_timestamp()),
recovery_key
FROM expired
ON CONFLICT (dedup_key) DO NOTHING
RETURNING id, job_id, payload, available_at, sent_at, created_at, dedup_key
`
func (q *Queries) EnqueueExpiredLeases(ctx context.Context, batchSize int32) ([]Outbox, error) {
rows, err := q.db.Query(ctx, enqueueExpiredLeases, batchSize)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Outbox
for rows.Next() {
var i Outbox
if err := rows.Scan(
&i.ID,
&i.JobID,
&i.Payload,
&i.AvailableAt,
&i.SentAt,
&i.CreatedAt,
&i.DedupKey,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const markOutboxEventSent = `-- name: MarkOutboxEventSent :execrows
UPDATE outbox SET sent_at = clock_timestamp()
WHERE id = $1 AND sent_at IS NULL
`
func (q *Queries) MarkOutboxEventSent(ctx context.Context, id pgtype.UUID) (int64, error) {
result, err := q.db.Exec(ctx, markOutboxEventSent, id)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const nextPendingOutboxEvent = `-- name: NextPendingOutboxEvent :one
SELECT id, job_id, payload, available_at, sent_at, created_at, dedup_key
FROM outbox
WHERE sent_at IS NULL AND available_at <= statement_timestamp()
ORDER BY available_at, id
LIMIT 1
FOR UPDATE SKIP LOCKED
`
func (q *Queries) NextPendingOutboxEvent(ctx context.Context) (Outbox, error) {
row := q.db.QueryRow(ctx, nextPendingOutboxEvent)
var i Outbox
err := row.Scan(
&i.ID,
&i.JobID,
&i.Payload,
&i.AvailableAt,
&i.SentAt,
&i.CreatedAt,
&i.DedupKey,
)
return i, err
}
+77
View File
@@ -0,0 +1,77 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: users.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createUser = `-- name: CreateUser :one
INSERT INTO users (username, display_name, password_hash)
VALUES ($1, $2, $3)
RETURNING id, username, display_name, password_hash, created_at
`
type CreateUserParams struct {
Username string
DisplayName string
PasswordHash string
}
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
row := q.db.QueryRow(ctx, createUser, arg.Username, arg.DisplayName, arg.PasswordHash)
var i User
err := row.Scan(
&i.ID,
&i.Username,
&i.DisplayName,
&i.PasswordHash,
&i.CreatedAt,
)
return i, err
}
const getUserByID = `-- name: GetUserByID :one
SELECT id, username, display_name, password_hash, created_at
FROM users
WHERE id = $1
`
func (q *Queries) GetUserByID(ctx context.Context, id pgtype.UUID) (User, error) {
row := q.db.QueryRow(ctx, getUserByID, id)
var i User
err := row.Scan(
&i.ID,
&i.Username,
&i.DisplayName,
&i.PasswordHash,
&i.CreatedAt,
)
return i, err
}
const getUserByUsername = `-- name: GetUserByUsername :one
SELECT id, username, display_name, password_hash, created_at
FROM users
WHERE username = $1
`
func (q *Queries) GetUserByUsername(ctx context.Context, username string) (User, error) {
row := q.db.QueryRow(ctx, getUserByUsername, username)
var i User
err := row.Scan(
&i.ID,
&i.Username,
&i.DisplayName,
&i.PasswordHash,
&i.CreatedAt,
)
return i, err
}
@@ -0,0 +1,111 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: versioned_pipeline.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const getPipelineJobID = `-- name: GetPipelineJobID :one
SELECT id FROM attachment_jobs
WHERE attachment_id = $1::uuid AND pipeline_version = $2::int
`
type GetPipelineJobIDParams struct {
AttachmentID pgtype.UUID
PipelineVersion int32
}
func (q *Queries) GetPipelineJobID(ctx context.Context, arg GetPipelineJobIDParams) (pgtype.UUID, error) {
row := q.db.QueryRow(ctx, getPipelineJobID, arg.AttachmentID, arg.PipelineVersion)
var id pgtype.UUID
err := row.Scan(&id)
return id, err
}
const getPublishedOriginalKey = `-- name: GetPublishedOriginalKey :one
SELECT published_original_key FROM attachment_jobs
WHERE attachment_id = $1::uuid AND pipeline_version = $2::int
AND published_original_key IS NOT NULL
`
type GetPublishedOriginalKeyParams struct {
AttachmentID pgtype.UUID
PipelineVersion int32
}
func (q *Queries) GetPublishedOriginalKey(ctx context.Context, arg GetPublishedOriginalKeyParams) (pgtype.Text, error) {
row := q.db.QueryRow(ctx, getPublishedOriginalKey, arg.AttachmentID, arg.PipelineVersion)
var published_original_key pgtype.Text
err := row.Scan(&published_original_key)
return published_original_key, err
}
const isPublishedOriginalKey = `-- name: IsPublishedOriginalKey :one
SELECT EXISTS (SELECT 1 FROM attachment_jobs WHERE published_original_key = $1::text)
`
func (q *Queries) IsPublishedOriginalKey(ctx context.Context, objectKey string) (bool, error) {
row := q.db.QueryRow(ctx, isPublishedOriginalKey, objectKey)
var exists bool
err := row.Scan(&exists)
return exists, err
}
const queueAttachmentUpgrade = `-- name: QueueAttachmentUpgrade :one
UPDATE attachments
SET pipeline_version = 2, status = 'queued', staging_key = $1::text,
object_key = NULL, error_code = NULL, updated_at = clock_timestamp()
WHERE id = $2::uuid AND pipeline_version = 1
RETURNING id, chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256, created_at, updated_at, status, error_code, staging_key, pipeline_version
`
type QueueAttachmentUpgradeParams struct {
SourceKey string
ID pgtype.UUID
}
func (q *Queries) QueueAttachmentUpgrade(ctx context.Context, arg QueueAttachmentUpgradeParams) (Attachment, error) {
row := q.db.QueryRow(ctx, queueAttachmentUpgrade, arg.SourceKey, arg.ID)
var i Attachment
err := row.Scan(
&i.ID,
&i.ChatID,
&i.OwnerID,
&i.Filename,
&i.ObjectKey,
&i.ContentType,
&i.SizeBytes,
&i.Sha256,
&i.CreatedAt,
&i.UpdatedAt,
&i.Status,
&i.ErrorCode,
&i.StagingKey,
&i.PipelineVersion,
)
return i, err
}
const supersedeAttachmentJob = `-- name: SupersedeAttachmentJob :exec
WITH superseded AS (
UPDATE attachment_jobs
SET lease_until = NULL, next_attempt_at = NULL, updated_at = clock_timestamp()
WHERE id = $1::uuid
RETURNING id, updated_at
)
UPDATE attachment_attempts AS attempt
SET outcome = 'superseded', error_code = 'superseded', finished_at = superseded.updated_at
FROM superseded
WHERE attempt.job_id = superseded.id AND attempt.finished_at IS NULL
`
func (q *Queries) SupersedeAttachmentJob(ctx context.Context, jobID pgtype.UUID) error {
_, err := q.db.Exec(ctx, supersedeAttachmentJob, jobID)
return err
}
+43
View File
@@ -0,0 +1,43 @@
// Package fault provides one-shot pause points for an isolated lab Compose project.
package fault
import (
"context"
"errors"
"os"
"path/filepath"
"time"
)
func Pause(ctx context.Context, point, id string) error {
dir := os.Getenv("LAB5_FAULT_DIR")
if dir == "" {
return nil
}
base := filepath.Join(dir, point)
if err := os.Rename(base+".arm", base+".used"); err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}
if err := os.WriteFile(base+".hit", []byte(id+"\n"), 0644); err != nil {
return err
}
wait, cancel := context.WithTimeout(ctx, 90*time.Second)
defer cancel()
poll := time.NewTicker(50 * time.Millisecond)
defer poll.Stop()
for {
if _, err := os.Stat(base + ".release"); err == nil {
return nil
} else if !errors.Is(err, os.ErrNotExist) {
return err
}
select {
case <-wait.Done():
return wait.Err()
case <-poll.C:
}
}
}
+164
View File
@@ -0,0 +1,164 @@
package imageproc
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"highload/internal/model"
)
const (
MaxBytes = 10 * 1024 * 1024
MaxPixels = 40_000_000
)
var (
ErrInvalidImage = errors.New("invalid image")
ErrResourceLimit = errors.New("image processing resource limit")
)
type File struct {
Path string
Variant model.AttachmentVariant
}
type variantSpec struct {
name, extension, mime string
width, height int
args []string
}
func Generate(ctx context.Context, dir, input, mime string, version int32) ([]File, error) {
var jpegQuality, webpQuality string
switch version {
case 1:
jpegQuality, webpQuality = "85", "80"
case 2:
jpegQuality, webpQuality = "90", "72"
default:
return nil, fmt.Errorf("unsupported image pipeline version %d", version)
}
f, err := os.Open(input)
if err != nil {
return nil, err
}
cfg, format, decodeErr := image.DecodeConfig(io.LimitReader(f, MaxBytes))
_ = f.Close()
if decodeErr != nil || (format != "png" && format != "jpeg") || mime != "image/"+format ||
cfg.Width <= 0 || cfg.Height <= 0 || cfg.Width > MaxPixels/cfg.Height {
return nil, ErrInvalidImage
}
// Paths are generated on the server; input format is independently verified.
source := format + ":" + input
orient, err := magick(ctx, "identify", "-ping", "-format", "%[orientation]", source)
if err != nil {
return nil, err
}
w, h := cfg.Width, cfg.Height
switch strings.TrimSpace(orient) {
case "LeftTop", "RightTop", "RightBottom", "LeftBottom":
w, h = h, w
}
tw, th := fit(w, h, 256)
ow, oh := fit(w, h, 1280)
side := min(w, h)
specs := []variantSpec{
{name: "thumbnail", extension: "jpg", mime: "image/jpeg", width: tw, height: th,
args: []string{"-resize", geometry(tw, th), "-quality", jpegQuality}},
{name: "crop", extension: "jpg", mime: "image/jpeg", width: 128, height: 128,
args: []string{"-gravity", "center", "-crop", fmt.Sprintf("%dx%d+0+0", side, side), "+repage", "-resize", "128x128!", "-quality", jpegQuality}},
{name: "optimized", extension: "webp", mime: "image/webp", width: ow, height: oh,
args: []string{"-resize", geometry(ow, oh), "-quality", webpQuality, "-define", "webp:method=4"}},
}
files := make([]File, 0, len(specs))
for _, spec := range specs {
out := filepath.Join(dir, spec.name+"."+spec.extension)
args := []string{"-regard-warnings", source, "-auto-orient"}
if spec.mime == "image/jpeg" {
args = append(args, "-background", "white", "-alpha", "remove", "-alpha", "off")
}
args = append(args, spec.args...)
args = append(args, "-strip", out)
if _, err := magick(ctx, args...); err != nil {
return nil, err
}
meta, err := inspectFile(ctx, out, spec)
if err != nil {
return nil, err
}
files = append(files, File{Path: out, Variant: meta})
}
return files, nil
}
func fit(w, h, bound int) (int, int) {
if max(w, h) <= bound {
return w, h
}
if w >= h {
return bound, max(1, h*bound/w)
}
return max(1, w*bound/h), bound
}
func geometry(w, h int) string {
return fmt.Sprintf("%dx%d!", w, h)
}
func inspectFile(ctx context.Context, path string, spec variantSpec) (model.AttachmentVariant, error) {
var meta model.AttachmentVariant
desc, err := magick(ctx, "identify", "-ping", "-format", "%m %w %h", path)
if err != nil {
return meta, err
}
format := strings.ToUpper(strings.TrimPrefix(spec.mime, "image/"))
if strings.TrimSpace(desc) != format+" "+strconv.Itoa(spec.width)+" "+strconv.Itoa(spec.height) {
return meta, fmt.Errorf("generated variant has unexpected format or size")
}
f, err := os.Open(path)
if err != nil {
return meta, err
}
defer f.Close()
sum := sha256.New()
size, err := io.Copy(sum, f)
if err != nil {
return meta, err
}
return model.AttachmentVariant{Name: spec.name, ContentType: spec.mime, Width: spec.width, Height: spec.height,
SizeBytes: size, SHA256: hex.EncodeToString(sum.Sum(nil))}, nil
}
func magick(ctx context.Context, args ...string) (string, error) {
cmd := exec.CommandContext(ctx, "magick", args...)
out, err := cmd.CombinedOutput()
if ctx.Err() != nil {
return "", ctx.Err()
}
if err == nil {
return string(out), nil
}
message := strings.ToLower(string(out))
if strings.Contains(message, "cache resources exhausted") || strings.Contains(message, "time limit") ||
strings.Contains(message, "memory allocation") || strings.Contains(message, "list length exceeds") {
return "", ErrResourceLimit
}
if strings.Contains(message, "corrupt") || strings.Contains(message, "unexpected end") ||
strings.Contains(message, "insufficient image data") || strings.Contains(message, "decode") {
return "", ErrInvalidImage
}
return "", fmt.Errorf("ImageMagick command failed: %w", err)
}
+31
View File
@@ -0,0 +1,31 @@
package model
import "time"
type Attachment struct {
ID string
ChatID string
OwnerID string
Filename string
ObjectKey string
StagingKey string
ErrorCode *string
Variants []AttachmentVariant
ContentType string
SizeBytes int64
SHA256 string
Status string
PipelineVersion int32
CreatedAt time.Time
UpdatedAt time.Time
}
type AttachmentVariant struct {
Name string
ObjectKey string
ContentType string
SizeBytes int64
Width int
Height int
SHA256 string
}
+26
View File
@@ -0,0 +1,26 @@
package model
import "time"
type AttachmentJob struct {
ID string
AttachmentID string
PipelineVersion int32
AttemptsStarted int32
MaxAttempts int32
LeaseGeneration int32
LeaseUntil time.Time
}
type AttachmentJobState struct {
AttachmentID string
PipelineVersion int32
ID string
Status string
LeaseGeneration int32
AttemptsStarted int32
MaxAttempts int32
LeaseUntil *time.Time
LeaseExpired bool
NextAttemptAt *time.Time
}
+10
View File
@@ -0,0 +1,10 @@
package model
import "time"
type Chat struct {
ID string
Title string
MemberIDs []string
CreatedAt time.Time
}
+12
View File
@@ -0,0 +1,12 @@
package model
import "time"
type Message struct {
ID string
ChatID string
SenderID string
Text string
AttachmentIDs []string
CreatedAt time.Time
}
+11
View File
@@ -0,0 +1,11 @@
package model
import "time"
type Session struct {
TokenHash []byte
UserID string
CreatedAt time.Time
ExpiresAt time.Time
RevokedAt *time.Time
}
+11
View File
@@ -0,0 +1,11 @@
package model
import "time"
type User struct {
ID string
Username string
DisplayName string
PasswordHash string
CreatedAt time.Time
}
+64
View File
@@ -0,0 +1,64 @@
package pipeline
import (
"context"
"log/slog"
"time"
"highload/internal/repository/postgres"
"highload/internal/storage"
)
type Cleanup struct {
Repo *postgres.AttachmentRepository
Objects storage.ObjectCleanup
Grace time.Duration
Retention time.Duration
DryRun bool
}
func (c Cleanup) RunOnce(ctx context.Context) (int, error) {
policy := postgres.CleanupPolicy{Now: time.Now(), Grace: c.Grace, Retention: c.Retention, DryRun: c.DryRun}
removed := 0
for _, prefix := range []string{"staging/", "results/", "attachments/"} {
var token *string
for {
pageCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
objects, next, err := c.Objects.ListPage(pageCtx, token, prefix)
cancel()
if err != nil {
return removed, err
}
for _, obj := range objects {
eligible, err := c.Repo.CleanupObject(ctx, obj, policy, c.Objects.Delete)
if err != nil {
return removed, err
}
if eligible {
removed++
}
}
if next == nil {
break
}
token = next
}
}
return removed, nil
}
func (c Cleanup) Serve(ctx context.Context, period time.Duration) {
for ctx.Err() == nil {
count, err := c.RunOnce(ctx)
if err != nil && ctx.Err() == nil {
slog.Warn("cleanup pass failed", "error", err)
}
if count > 0 {
slog.Info("cleanup pass", "objects", count, "dry_run", c.DryRun)
}
select {
case <-ctx.Done():
case <-time.After(period):
}
}
}
+353
View File
@@ -0,0 +1,353 @@
package pipeline
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"math/rand/v2"
"os"
"path/filepath"
"time"
"highload/internal/broker"
"highload/internal/config"
"highload/internal/fault"
"highload/internal/imageproc"
"highload/internal/model"
"highload/internal/repository"
"highload/internal/storage"
"github.com/google/uuid"
)
type WorkerRepository interface {
ClaimJob(context.Context, string, time.Time) (*model.AttachmentJob, error)
GetAttachmentJobState(context.Context, string) (*model.AttachmentJobState, error)
GetAttachmentByID(context.Context, string) (*model.Attachment, error)
CompleteJob(context.Context, string, int32, string, []model.AttachmentVariant) (bool, error)
PublishedObjectKeys(context.Context, string, int32) ([]string, error)
ScheduleRetry(context.Context, string, int32, string, time.Time) (bool, error)
FailJob(context.Context, string, int32, string) (bool, error)
}
type WorkerObjects interface {
storage.ObjectWriter
Head(context.Context, string) error
}
type Worker struct {
Repo WorkerRepository
Objects WorkerObjects
Config config.Pipeline
}
type message struct {
SchemaVersion int `json:"schema_version"`
Type string `json:"type"`
JobID string `json:"job_id"`
AttachmentID string `json:"attachment_id"`
PipelineVersion int32 `json:"pipeline_version"`
Attempt int `json:"attempt"`
CreatedAt time.Time `json:"created_at"`
Traceparent string `json:"traceparent,omitempty"`
Tracestate string `json:"tracestate,omitempty"`
}
func decodeMessage(payload []byte) (message, error) {
var msg message
if len(payload) > 4096 {
return msg, errors.New("oversize job")
}
d := json.NewDecoder(bytes.NewReader(payload))
d.DisallowUnknownFields()
if err := d.Decode(&msg); err != nil {
return msg, err
}
if err := d.Decode(new(any)); err != io.EOF {
return msg, errors.New("trailing job data")
}
jobID, err1 := uuid.Parse(msg.JobID)
attachmentID, err2 := uuid.Parse(msg.AttachmentID)
if err1 != nil || err2 != nil || msg.SchemaVersion != 1 || msg.Type != "attachment.process.v1" ||
msg.PipelineVersion <= 0 || msg.Attempt <= 0 || msg.CreatedAt.IsZero() {
return msg, errors.New("invalid job envelope")
}
msg.JobID, msg.AttachmentID = jobID.String(), attachmentID.String()
return msg, nil
}
func (w Worker) Serve(ctx context.Context) {
for ctx.Err() == nil {
if err := w.consume(ctx); err != nil && ctx.Err() == nil {
slog.Warn("worker consumer reconnecting", "error", err)
}
select {
case <-ctx.Done():
case <-time.After(500 * time.Millisecond):
}
}
}
func (w Worker) consume(ctx context.Context) error {
conn, ch, err := broker.Connect(w.Config.BrokerURL)
if err != nil {
return err
}
defer broker.Close(conn)
if err := ch.Qos(w.Config.Prefetch, 0, false); err != nil {
return err
}
deliveries, err := ch.ConsumeWithContext(ctx, broker.Queue, "", false, false, false, false, nil)
if err != nil {
return err
}
for {
select {
case <-ctx.Done():
return ctx.Err()
case delivery, ok := <-deliveries:
if !ok {
return errors.New("broker delivery channel closed")
}
msg, err := decodeMessage(delivery.Body)
if err != nil {
slog.Warn("rejecting invalid job envelope")
if err := delivery.Reject(false); err != nil {
return err
}
continue
}
reject, err := w.handle(ctx, msg)
switch {
case err != nil:
// Closing the channel requeues an unacknowledged delivery.
return err
case reject:
if err := delivery.Reject(false); err != nil {
return err
}
default:
if err := delivery.Ack(false); err != nil {
return err
}
}
}
}
}
func (w Worker) handle(ctx context.Context, msg message) (bool, error) {
state, err := w.Repo.GetAttachmentJobState(ctx, msg.JobID)
if errors.Is(err, repository.ErrNotFound) {
return true, nil
}
if err != nil {
return false, err
}
if state.AttachmentID != msg.AttachmentID || state.PipelineVersion != msg.PipelineVersion {
return true, nil
}
if state.Status == "superseded" {
return false, nil
}
job, err := w.Repo.ClaimJob(ctx, msg.JobID, time.Now().Add(w.Config.Lease))
if err != nil {
return false, err
}
if job == nil {
return false, w.handleUnclaimed(ctx, msg)
}
slog.Info("job claimed", "job_id", job.ID, "attempt", job.AttemptsStarted, "lease_generation", job.LeaseGeneration)
attemptCtx, cancel := context.WithTimeout(ctx, w.Config.Timeout)
err = w.process(attemptCtx, job)
cancel()
if ctx.Err() != nil {
return false, ctx.Err()
}
if err != nil {
if errors.Is(err, errLostLease) {
return false, w.handleUnclaimed(ctx, msg)
}
return false, w.saveFailure(ctx, job, err)
}
pauseCtx, pauseCancel := context.WithTimeout(ctx, 30*time.Second)
defer pauseCancel()
return false, fault.Pause(pauseCtx, "before_ack", job.ID)
}
func (w Worker) handleUnclaimed(ctx context.Context, msg message) error {
state, err := w.Repo.GetAttachmentJobState(ctx, msg.JobID)
if err != nil {
return err
}
switch state.Status {
case "ready":
checkCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
keys, err := w.Repo.PublishedObjectKeys(checkCtx, state.AttachmentID, state.PipelineVersion)
if err != nil {
return err
}
for _, key := range keys {
if err := w.Objects.Head(checkCtx, key); err != nil {
return err
}
}
return nil
case "failed", "queued", "superseded":
return nil // A queued retry is already in the transactionally durable outbox.
case "processing":
if state.AttemptsStarted >= state.MaxAttempts && state.LeaseExpired {
_, err = w.Repo.FailJob(ctx, msg.JobID, state.LeaseGeneration, "retry_exhausted")
return err
}
return nil // The recovery loop publishes one event for this expired lease.
default:
return errors.New("unrecognized job state")
}
}
var (
errLostLease = errors.New("job lease lost")
errOriginalMismatch = errors.New("staging hash or size differs")
)
func (w Worker) process(ctx context.Context, job *model.AttachmentJob) error {
att, err := w.Repo.GetAttachmentByID(ctx, job.AttachmentID)
if err != nil {
return err
}
if att.PipelineVersion != job.PipelineVersion {
return errLostLease
}
if att.StagingKey == "" {
return errOriginalMismatch
}
dir, err := os.MkdirTemp("", "attachment-job-")
if err != nil {
return err
}
defer os.RemoveAll(dir)
source := filepath.Join(dir, "input")
if err := w.download(ctx, att, source); err != nil {
return err
}
files, err := imageproc.Generate(ctx, dir, source, att.ContentType, job.PipelineVersion)
if err != nil {
return err
}
if err := fault.Pause(ctx, "before_objects", job.ID); err != nil {
return err
}
prefix := fmt.Sprintf("results/%s/v%d/%d/", job.AttachmentID, job.PipelineVersion, job.LeaseGeneration)
originalKey := prefix + "original"
if err := w.putFile(ctx, originalKey, att.ContentType, source, att.SizeBytes); err != nil {
return err
}
variants := make([]model.AttachmentVariant, 0, len(files))
for _, f := range files {
v := f.Variant
v.ObjectKey = prefix + filepath.Base(f.Path)
if err := w.putFile(ctx, v.ObjectKey, v.ContentType, f.Path, v.SizeBytes); err != nil {
return err
}
variants = append(variants, v)
}
if err := fault.Pause(ctx, "after_objects", job.ID); err != nil {
return err
}
done, err := w.Repo.CompleteJob(ctx, job.ID, job.LeaseGeneration, originalKey, variants)
if err != nil {
return err
}
if !done {
slog.Info("job finalization rejected", "job_id", job.ID, "lease_generation", job.LeaseGeneration)
return errLostLease
}
slog.Info("job ready", "job_id", job.ID, "lease_generation", job.LeaseGeneration)
return nil
}
func (w Worker) download(ctx context.Context, att *model.Attachment, path string) error {
if att.SizeBytes <= 0 || att.SizeBytes > imageproc.MaxBytes {
return errOriginalMismatch
}
reader, err := w.Objects.Get(ctx, att.StagingKey)
if err != nil {
return err
}
defer reader.Close()
f, err := os.Create(path)
if err != nil {
return err
}
sum := sha256.New()
n, copyErr := io.Copy(io.MultiWriter(f, sum), io.LimitReader(reader, att.SizeBytes+1))
closeErr := f.Close()
if copyErr != nil {
return copyErr
}
if closeErr != nil {
return closeErr
}
if n != att.SizeBytes || hex.EncodeToString(sum.Sum(nil)) != att.SHA256 {
return errOriginalMismatch
}
return nil
}
func (w Worker) putFile(ctx context.Context, key, mime, path string, size int64) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
return w.Objects.Put(ctx, key, mime, f, size)
}
func (w Worker) saveFailure(ctx context.Context, job *model.AttachmentJob, failure error) error {
code := "processing_failed"
permanent := false
switch {
case errors.Is(failure, errOriginalMismatch), errors.Is(failure, imageproc.ErrInvalidImage):
code, permanent = "invalid_image", true
case errors.Is(failure, imageproc.ErrResourceLimit):
code, permanent = "resource_limit", true
case errors.Is(failure, storage.ErrObjectNotFound):
code, permanent = "original_missing", true
case errors.Is(failure, storage.ErrServiceUnavailable):
code = "s3_unavailable"
case errors.Is(failure, context.DeadlineExceeded):
code = "processing_timeout"
}
dbCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if permanent || job.AttemptsStarted >= job.MaxAttempts {
if !permanent {
code = "retry_exhausted"
}
changed, err := w.Repo.FailJob(dbCtx, job.ID, job.LeaseGeneration, code)
if changed {
slog.Warn("job failed", "job_id", job.ID, "error_code", code)
}
return err
}
// The floor avoids immediate requeue loops. The upper bound grows with
// each attempt and is capped to keep the lab recoverable.
upper := min(30*time.Second, w.Config.RetryBase<<min(job.AttemptsStarted-1, 4))
delay := time.Second + time.Duration(rand.Int64N(int64(upper-time.Second+1)))
changed, err := w.Repo.ScheduleRetry(dbCtx, job.ID, job.LeaseGeneration, code, time.Now().Add(delay))
if changed {
slog.Info("job retry scheduled", "job_id", job.ID, "error_code", code, "delay_ms", delay.Milliseconds())
}
return err
}
+36
View File
@@ -0,0 +1,36 @@
package repository
import (
"context"
"highload/internal/model"
"time"
)
type AttachmentRepository interface {
CreateAttachment(ctx context.Context, params CreateAttachmentParams) (*model.Attachment, error)
GetAttachmentForUser(ctx context.Context, userID, attachmentID string) (*model.Attachment, error)
ExistsByObjectKey(ctx context.Context, key string) (bool, error)
WithObjectKeyLock(ctx context.Context, key string, fn func(context.Context, AttachmentTx) error) (bool, error)
}
type AttachmentKey struct {
ID string
ObjectKey string
CreatedAt time.Time
}
type AttachmentReconcileRepository interface {
ListAttachmentKeys(ctx context.Context, afterKey string, pageSize int32) ([]AttachmentKey, error)
GetAttachmentByID(ctx context.Context, id string) (*model.Attachment, error)
WithObjectKeyLock(ctx context.Context, key string, fn func(context.Context, AttachmentTx) error) (bool, error)
}
type CreateAttachmentParams struct {
ChatID string
OwnerID string
Filename string
ObjectKey string
ContentType string
Sha256 string
SizeBytes int64
}
+13
View File
@@ -0,0 +1,13 @@
package repository
import (
"context"
"highload/internal/model"
)
type AttachmentTx interface {
CreateAttachment(ctx context.Context, params CreateAttachmentParams) (*model.Attachment, error)
CreateQueued(ctx context.Context, id, chatID, ownerID, mime, sha256, key, filename string, size int64) (*model.Attachment, error)
ExistsByObjectKey(ctx context.Context, key string) (bool, error)
GetAttachmentByID(ctx context.Context, id string) (*model.Attachment, error)
}
+18
View File
@@ -0,0 +1,18 @@
package repository
import (
"context"
"highload/internal/model"
"time"
)
type ChatRepository interface {
CreateChat(ctx context.Context, title string, memberIDs []string) (*model.Chat, error)
GetChatByID(ctx context.Context, id string) (*model.Chat, error)
GetChatsByUserID(ctx context.Context, userID string, limit int, after *ChatPosition) ([]*model.Chat, error)
}
type ChatPosition struct {
CreatedAt time.Time
ID string
}
+16
View File
@@ -0,0 +1,16 @@
package repository
import (
"errors"
)
var (
ErrNotFound = errors.New("not found")
ErrAlreadyExists = errors.New("already exists")
ErrDatabase = errors.New("database error")
ErrInvalidInput = errors.New("invalid input")
ErrServiceUnavailable = errors.New("database unavailable")
ErrServerError = errors.New("server error")
ErrRedisNotAnswer = errors.New("redis not not answer")
ErrConflict = errors.New("data conflict")
)
+17
View File
@@ -0,0 +1,17 @@
package repository
import (
"context"
"highload/internal/model"
"time"
)
type MessageRepository interface {
CreateMessage(ctx context.Context, chatID, senderID, text, key string, attachmentIDs []string) (*model.Message, error)
GetMessagesByChatID(ctx context.Context, chatID string, limit int, after *MessagePosition) ([]*model.Message, error)
}
type MessagePosition struct {
CreatedAt time.Time
ID string
}
+440
View File
@@ -0,0 +1,440 @@
package postgres
import (
"context"
"errors"
"time"
"highload/internal/database/sqlc"
"highload/internal/model"
"highload/internal/repository"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
)
const attachmentTransactionTimeout = 5 * time.Second
type AttachmentRepository struct {
queries *sqlc.Queries
pool *pgxpool.Pool
maxAttempts int32
}
func NewAttachmentRepository(queries *sqlc.Queries, pool *pgxpool.Pool, attemptLimit ...int32) *AttachmentRepository {
maxAttempts := int32(5)
if len(attemptLimit) > 0 {
maxAttempts = attemptLimit[0]
}
return &AttachmentRepository{
queries: queries,
pool: pool,
maxAttempts: maxAttempts,
}
}
func (r *AttachmentRepository) CreateAttachment(ctx context.Context, params repository.CreateAttachmentParams) (*model.Attachment, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
chatUUID, err := getUUIDFromString(params.ChatID)
if err != nil {
return nil, convertDBError(err)
}
ownerUUID, err := getUUIDFromString(params.OwnerID)
if err != nil {
return nil, convertDBError(err)
}
sqlcParams := sqlc.CreateAttachmentParams{
ChatID: *chatUUID,
OwnerID: *ownerUUID,
Filename: params.Filename,
ObjectKey: params.ObjectKey,
ContentType: params.ContentType,
SizeBytes: params.SizeBytes,
Sha256: params.Sha256,
}
sqlcAttachment, err := r.queries.CreateAttachment(dbCtx, sqlcParams)
if err != nil {
return nil, convertDBError(err)
}
attachment := mapAttachmentSqlcToModel(sqlc.Attachment(sqlcAttachment))
return attachment, nil
}
func (r *AttachmentRepository) GetAttachmentForUser(ctx context.Context, userID, attachmentID string) (*model.Attachment, error) {
userUUID, err := getUUIDFromString(userID)
if err != nil {
return nil, err
}
attachmentUUID, err := getUUIDFromString(attachmentID)
if err != nil {
return nil, err
}
txCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
params := sqlc.GetAttachmentForUserParams{
AttachmentID: *attachmentUUID,
UserID: *userUUID,
}
tx, err := r.pool.BeginTx(txCtx, pgx.TxOptions{
IsoLevel: pgx.RepeatableRead,
AccessMode: pgx.ReadOnly,
})
if err != nil {
return nil, convertDBError(err)
}
defer rollbackTx(ctx, tx)
txQueries := r.queries.WithTx(tx)
sqlcAttachment, err := txQueries.GetAttachmentForUser(txCtx, params)
if err != nil {
return nil, convertDBError(err)
}
attachment := mapAttachmentSqlcToModel(sqlcAttachment)
if sqlcAttachment.Status == "ready" {
variants, err := txQueries.ListAttachmentVariants(txCtx, sqlc.ListAttachmentVariantsParams{
AttachmentID: sqlcAttachment.ID,
PipelineVersion: sqlcAttachment.PipelineVersion,
})
if err != nil {
return nil, convertDBError(err)
}
for _, v := range variants {
attachment.Variants = append(attachment.Variants, model.AttachmentVariant{
Name: v.Name,
ObjectKey: v.ObjectKey,
ContentType: v.ContentType,
SizeBytes: v.SizeBytes,
Width: int(v.Width),
Height: int(v.Height),
SHA256: v.Sha256,
})
}
}
if err = tx.Commit(txCtx); err != nil {
return nil, convertDBError(err)
}
return attachment, nil
}
func (r *AttachmentRepository) ExistsByObjectKey(ctx context.Context, key string) (bool, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
exists, err := r.queries.AttachmentExistsByObjectKey(dbCtx, key)
if err != nil {
return false, convertDBError(err)
}
return exists, nil
}
func (r *AttachmentRepository) ListAttachmentKeys(ctx context.Context, afterKey string, pageSize int32) ([]repository.AttachmentKey, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
rows, err := r.queries.ListAttachmentKeys(dbCtx, sqlc.ListAttachmentKeysParams{
AfterKey: pgtype.Text{String: afterKey, Valid: true},
PageSize: pageSize,
})
if err != nil {
return nil, convertDBError(err)
}
keys := make([]repository.AttachmentKey, 0, len(rows))
for _, row := range rows {
keys = append(keys, repository.AttachmentKey{
ID: row.ID.String(), ObjectKey: row.ObjectKey.String, CreatedAt: row.CreatedAt.Time,
})
}
return keys, nil
}
func (r *AttachmentRepository) GetAttachmentByID(ctx context.Context, id string) (*model.Attachment, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
attachmentID, err := getUUIDFromString(id)
if err != nil {
return nil, err
}
row, err := r.queries.GetAttachmentByID(dbCtx, *attachmentID)
if err != nil {
return nil, convertDBError(err)
}
return mapAttachmentSqlcToModel(row), nil
}
func (r *AttachmentRepository) WithObjectKeyLock(ctx context.Context, key string, fn func(context.Context, repository.AttachmentTx) error) (bool, error) {
txCtx, cancel := context.WithTimeout(ctx, attachmentTransactionTimeout)
defer cancel()
tx, err := r.pool.BeginTx(txCtx, pgx.TxOptions{IsoLevel: pgx.ReadCommitted})
if err != nil {
return false, convertDBError(err)
}
defer rollbackTx(ctx, tx)
txQueries := r.queries.WithTx(tx)
locked, err := txQueries.TryLockAttachmentObjectKey(txCtx, key)
if err != nil {
return false, convertDBError(err)
}
if !locked {
return false, nil
}
txRepo := &AttachmentRepository{queries: txQueries, maxAttempts: r.maxAttempts}
if err = fn(txCtx, txRepo); err != nil {
return true, err
}
if err = tx.Commit(txCtx); err != nil {
return true, convertDBError(err)
}
return true, nil
}
func (r *AttachmentRepository) CreateQueued(ctx context.Context, queuedID, chatID, ownerID, mime, sha256, key, filename string, size int64) (*model.Attachment, error) {
queuedUUID, err := getUUIDFromString(queuedID)
if err != nil {
return nil, convertDBError(err)
}
chatUUID, err := getUUIDFromString(chatID)
if err != nil {
return nil, convertDBError(err)
}
ownerUUID, err := getUUIDFromString(ownerID)
if err != nil {
return nil, convertDBError(err)
}
cqaParams := sqlc.CreateQueuedAttachmentParams{
ID: *queuedUUID,
ChatID: *chatUUID,
OwnerID: *ownerUUID,
Filename: filename,
ContentType: mime,
SizeBytes: size,
Sha256: sha256,
StagingKey: pgtype.Text{String: key, Valid: true},
}
// The upload path already holds the object lock in a transaction.
if r.pool == nil {
return r.insertQueued(ctx, r.queries, cqaParams)
}
txCtx, cancel := context.WithTimeout(ctx, attachmentTransactionTimeout)
defer cancel()
tx, err := r.pool.BeginTx(txCtx, pgx.TxOptions{})
if err != nil {
return nil, convertDBError(err)
}
defer rollbackTx(ctx, tx)
queued, err := r.insertQueued(txCtx, r.queries.WithTx(tx), cqaParams)
if err != nil {
return nil, err
}
if err := tx.Commit(txCtx); err != nil {
return nil, convertDBError(err)
}
return queued, nil
}
func (r *AttachmentRepository) insertQueued(txCtx context.Context, txQueries *sqlc.Queries, cqaParams sqlc.CreateQueuedAttachmentParams) (*model.Attachment, error) {
sqlcAttachment, err := txQueries.CreateQueuedAttachment(txCtx, cqaParams)
if err != nil {
return nil, convertDBError(err)
}
if _, err := r.createPipelineJob(txCtx, txQueries, sqlcAttachment); err != nil {
return nil, err
}
return mapAttachmentSqlcToModel(sqlcAttachment), nil
}
func (r *AttachmentRepository) ClaimJob(ctx context.Context, jobID string, leaseUntil time.Time) (*model.AttachmentJob, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
jobUUID, err := getUUIDFromString(jobID)
if err != nil {
return nil, convertDBError(err)
}
arg := sqlc.ClaimJobParams{
NewLeaseUntil: pgtype.Timestamptz{Time: leaseUntil, Valid: true},
JobID: *jobUUID,
}
row, err := r.queries.ClaimJob(dbCtx, arg)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
return nil, convertDBError(err)
}
return &model.AttachmentJob{
ID: row.ID.String(),
AttachmentID: row.AttachmentID.String(),
PipelineVersion: row.PipelineVersion,
AttemptsStarted: row.AttemptsStarted,
MaxAttempts: row.MaxAttempts,
LeaseGeneration: row.LeaseGeneration,
LeaseUntil: row.LeaseUntil.Time,
}, nil
}
func (r *AttachmentRepository) GetAttachmentJobState(ctx context.Context, jobID string) (*model.AttachmentJobState, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
jobUUID, err := getUUIDFromString(jobID)
if err != nil {
return nil, convertDBError(err)
}
row, err := r.queries.GetAttachmentJobState(dbCtx, *jobUUID)
if err != nil {
return nil, convertDBError(err)
}
state := &model.AttachmentJobState{
AttachmentID: row.AttachmentID.String(),
PipelineVersion: row.PipelineVersion,
ID: row.ID.String(),
Status: row.Status,
LeaseGeneration: row.LeaseGeneration,
AttemptsStarted: row.AttemptsStarted,
MaxAttempts: row.MaxAttempts,
LeaseExpired: row.LeaseExpired,
}
if row.LeaseUntil.Valid {
state.LeaseUntil = &row.LeaseUntil.Time
}
if row.NextAttemptAt.Valid {
state.NextAttemptAt = &row.NextAttemptAt.Time
}
return state, nil
}
func (r *AttachmentRepository) CompleteJob(
ctx context.Context, jobID string, leaseGeneration int32,
originalKey string, variants []model.AttachmentVariant,
) (bool, error) {
jobUUID, err := getUUIDFromString(jobID)
if err != nil {
return false, err
}
txCtx, cancel := context.WithTimeout(ctx, attachmentTransactionTimeout)
defer cancel()
tx, err := r.pool.BeginTx(txCtx, pgx.TxOptions{})
if err != nil {
return false, convertDBError(err)
}
defer rollbackTx(ctx, tx)
txQueries := r.queries.WithTx(tx)
lockedJob, err := txQueries.LockActiveAttachmentJob(txCtx, sqlc.LockActiveAttachmentJobParams{
JobID: *jobUUID,
LeaseGeneration: leaseGeneration,
})
if errors.Is(err, pgx.ErrNoRows) {
return false, nil
}
if err != nil {
return false, convertDBError(err)
}
const maxDimension = 1<<31 - 1
for _, variant := range variants {
if variant.Width <= 0 || variant.Width > maxDimension ||
variant.Height <= 0 || variant.Height > maxDimension {
return false, repository.ErrInvalidInput
}
err := txQueries.CreateAttachmentVariant(txCtx, sqlc.CreateAttachmentVariantParams{
AttachmentID: lockedJob.AttachmentID,
PipelineVersion: lockedJob.PipelineVersion,
Name: variant.Name,
ObjectKey: variant.ObjectKey,
ContentType: variant.ContentType,
SizeBytes: variant.SizeBytes,
Width: int32(variant.Width),
Height: int32(variant.Height),
Sha256: variant.SHA256,
})
if err != nil {
return false, convertDBError(err)
}
}
rows, err := txQueries.CompleteJob(txCtx, sqlc.CompleteJobParams{
JobID: *jobUUID,
LeaseGeneration: leaseGeneration,
ObjectKey: originalKey,
})
if err != nil {
return false, convertDBError(err)
}
if rows == 0 {
return false, nil
}
if err := tx.Commit(txCtx); err != nil {
return false, convertDBError(err)
}
return true, nil
}
func mapAttachmentSqlcToModel(sa sqlc.Attachment) *model.Attachment {
var errorCode *string
if sa.ErrorCode.Valid {
errorCode = &sa.ErrorCode.String
}
return &model.Attachment{
Variants: []model.AttachmentVariant{},
ErrorCode: errorCode,
StagingKey: sa.StagingKey.String,
ID: sa.ID.String(),
ChatID: sa.ChatID.String(),
OwnerID: sa.OwnerID.String(),
Filename: sa.Filename,
ObjectKey: sa.ObjectKey.String,
ContentType: sa.ContentType,
SizeBytes: sa.SizeBytes,
Status: sa.Status,
PipelineVersion: sa.PipelineVersion,
SHA256: sa.Sha256,
CreatedAt: sa.CreatedAt.Time,
UpdatedAt: sa.UpdatedAt.Time,
}
}
+163
View File
@@ -0,0 +1,163 @@
package postgres
import (
"context"
"highload/internal/database/sqlc"
"highload/internal/model"
"highload/internal/repository"
"slices"
"time"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
)
type ChatRepository struct {
queries *sqlc.Queries
pool *pgxpool.Pool
}
func NewChatRepository(queries *sqlc.Queries, pool *pgxpool.Pool) *ChatRepository {
return &ChatRepository{
queries: queries,
pool: pool,
}
}
func (r *ChatRepository) CreateChat(ctx context.Context, title string, memberIDs []string) (*model.Chat, error) {
txCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
tx, err := r.pool.Begin(txCtx)
if err != nil {
return nil, convertDBError(err)
}
defer func() {
rollbackCtx, rollbackCancel := context.WithTimeout(
context.WithoutCancel(ctx),
time.Second,
)
defer rollbackCancel()
_ = tx.Rollback(rollbackCtx)
}()
txQueries := r.queries.WithTx(tx)
sqlcChat, err := txQueries.CreateChat(txCtx, title)
if err != nil {
return nil, convertDBError(err)
}
chat := &model.Chat{
ID: sqlcChat.ID.String(),
Title: sqlcChat.Title,
CreatedAt: sqlcChat.CreatedAt.Time,
}
for _, id := range memberIDs {
userID, err := getUUIDFromString(id)
if err != nil {
return nil, convertDBError(err)
}
err = txQueries.CreateChatMember(txCtx, sqlc.CreateChatMemberParams{ChatID: sqlcChat.ID, UserID: *userID})
if err != nil {
return nil, convertDBError(err)
}
}
chat.MemberIDs = slices.Clone(memberIDs)
err = tx.Commit(txCtx)
if err != nil {
return nil, convertDBError(err)
}
return chat, nil
}
func (r *ChatRepository) GetChatByID(ctx context.Context, id string) (*model.Chat, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
uuidID, err := getUUIDFromString(id)
if err != nil {
return nil, convertDBError(err)
}
sqlcChat, err := r.queries.GetChatByID(dbCtx, *uuidID)
if err != nil {
return nil, convertDBError(err)
}
chat := mapChatSqlcToModel(&sqlcChat)
return chat, nil
}
func (r *ChatRepository) GetChatsByUserID(ctx context.Context, userID string, limit int, after *repository.ChatPosition) ([]*model.Chat, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
uuidUserID, err := getUUIDFromString(userID)
if err != nil {
return nil, convertDBError(err)
}
afterCreatedAt := pgtype.Timestamptz{Valid: false}
afterID := pgtype.UUID{Valid: false}
if after != nil {
afterCreatedAt = pgtype.Timestamptz{Time: after.CreatedAt, Valid: true}
if uuidFromString, err := getUUIDFromString(after.ID); err == nil {
afterID = *uuidFromString
} else {
return nil, convertDBError(err)
}
}
chats, err := r.queries.GetChatsByUserID(dbCtx, sqlc.GetChatsByUserIDParams{UserID: *uuidUserID, AfterCreatedAt: afterCreatedAt, AfterID: afterID, LimitCount: int32(limit)})
if err != nil {
return nil, convertDBError(err)
}
resultChats := make([]*model.Chat, len(chats))
for index, sqlcChat := range chats {
memberIDs := make([]string, len(sqlcChat.MemberIds))
for i, memberID := range sqlcChat.MemberIds {
memberIDs[i] = memberID.String()
}
resultChats[index] = &model.Chat{
ID: sqlcChat.ID.String(),
MemberIDs: memberIDs,
CreatedAt: sqlcChat.CreatedAt.Time,
Title: sqlcChat.Title,
}
}
return resultChats, nil
}
func mapChatSqlcToModel(sqlcChat *sqlc.GetChatByIDRow) *model.Chat {
memberIDs := make([]string, len(sqlcChat.MemberIds))
for i, memberID := range sqlcChat.MemberIds {
memberIDs[i] = memberID.String()
}
return &model.Chat{
ID: sqlcChat.ID.String(),
MemberIDs: memberIDs,
CreatedAt: sqlcChat.CreatedAt.Time,
Title: sqlcChat.Title,
}
}
+46
View File
@@ -0,0 +1,46 @@
package postgres
import (
"context"
"errors"
"fmt"
"highload/internal/repository"
"io"
"net"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
func convertDBError(err error) error {
var pgErr *pgconn.PgError
var netErr *net.OpError
var dnsErr *net.DNSError
switch {
case errors.Is(err, context.DeadlineExceeded):
return fmt.Errorf("%w: %v", repository.ErrServiceUnavailable, err)
case errors.Is(err, pgx.ErrNoRows):
return repository.ErrNotFound
case errors.As(err, &pgErr):
switch pgErr.Code {
case "23505":
return repository.ErrAlreadyExists
case "53300", "57P03", "08006", "08001", "57P01", "57P02":
return repository.ErrServiceUnavailable
}
case errors.Is(err, io.EOF) ||
errors.Is(err, io.ErrUnexpectedEOF) ||
errors.Is(err, pgconn.ErrConnClosed):
return repository.ErrServiceUnavailable
case errors.Is(err, repository.ErrInvalidInput):
return repository.ErrInvalidInput
case errors.As(err, &netErr):
return repository.ErrServiceUnavailable
case errors.As(err, &dnsErr):
return repository.ErrServiceUnavailable
}
return fmt.Errorf("%w: %v", repository.ErrDatabase, err)
}
+191
View File
@@ -0,0 +1,191 @@
package postgres
import (
"context"
"errors"
"highload/internal/database/sqlc"
"highload/internal/model"
"highload/internal/repository"
"slices"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
)
type MessageRepository struct {
queries *sqlc.Queries
pool *pgxpool.Pool
}
func NewMessageRepository(queries *sqlc.Queries, pool *pgxpool.Pool) *MessageRepository {
return &MessageRepository{
queries: queries,
pool: pool,
}
}
func (r *MessageRepository) CreateMessage(ctx context.Context, chatID, senderID, text, key string, attachmentIDs []string) (*model.Message, error) {
chat, err := getUUIDFromString(chatID)
if err != nil {
return nil, err
}
sender, err := getUUIDFromString(senderID)
if err != nil {
return nil, err
}
ids := make([]pgtype.UUID, 0, len(attachmentIDs))
canonical := make([]string, 0, len(attachmentIDs))
for _, attachmentID := range attachmentIDs {
id, err := getUUIDFromString(attachmentID)
if err != nil {
return nil, err
}
ids = append(ids, *id)
canonical = append(canonical, id.String())
}
txCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
tx, err := r.pool.Begin(txCtx)
if err != nil {
return nil, convertDBError(err)
}
defer rollbackTx(ctx, tx)
q := r.queries.WithTx(tx)
if err := q.LockMessageRequest(txCtx, sqlc.LockMessageRequestParams{UserID: *sender, ChatID: *chat, IdempotencyKey: key}); err != nil {
return nil, convertDBError(err)
}
if err := q.DeleteExpiredMessageIdempotency(txCtx, sqlc.DeleteExpiredMessageIdempotencyParams{UserID: *sender, ChatID: *chat, IdempotencyKey: key}); err != nil {
return nil, convertDBError(err)
}
message, err := q.GetIdempotentMessage(txCtx, sqlc.GetIdempotentMessageParams{UserID: *sender, ChatID: *chat, IdempotencyKey: key})
switch {
case err == nil:
rows, err := q.GetMessagesAttachments(txCtx, message.ID)
if err != nil {
return nil, convertDBError(err)
}
saved := make([]string, 0, len(rows))
for _, row := range rows {
saved = append(saved, row.AttachmentID.String())
}
if message.Text != text || !slices.Equal(saved, canonical) {
return nil, repository.ErrConflict
}
case errors.Is(err, pgx.ErrNoRows):
if len(ids) > 0 {
rows, err := q.LockAttachmentsForMessage(txCtx, sqlc.LockAttachmentsForMessageParams{ChatID: *chat, AttachmentIds: ids})
if err != nil {
return nil, convertDBError(err)
}
if len(rows) != len(ids) {
return nil, repository.ErrNotFound
}
for _, row := range rows {
if row.Status == "failed" {
return nil, repository.ErrConflict
}
}
}
message, err = q.CreateMessage(txCtx, sqlc.CreateMessageParams{ChatID: *chat, SenderID: *sender, Text: text})
if err != nil {
return nil, convertDBError(err)
}
for i, id := range ids {
n, err := q.CreateMessagesAttachments(txCtx, sqlc.CreateMessagesAttachmentsParams{
MessageID: message.ID, AttachmentID: id, ChatID: *chat, Position: int16(i),
})
if err != nil {
return nil, convertDBError(err)
}
if n != 1 {
return nil, repository.ErrNotFound
}
}
n, err := q.InsertMessageIdempotency(txCtx, sqlc.InsertMessageIdempotencyParams{
UserID: *sender, ChatID: *chat, IdempotencyKey: key, MessageID: message.ID,
})
if err != nil {
return nil, convertDBError(err)
}
if n != 1 {
return nil, repository.ErrConflict
}
default:
return nil, convertDBError(err)
}
if err := tx.Commit(txCtx); err != nil {
return nil, convertDBError(err)
}
return &model.Message{
ID: message.ID.String(), ChatID: message.ChatID.String(), SenderID: message.SenderID.String(),
Text: message.Text, CreatedAt: message.CreatedAt.Time, AttachmentIDs: canonical,
}, nil
}
func (r *MessageRepository) GetMessagesByChatID(ctx context.Context, chatID string, limit int, after *repository.MessagePosition) ([]*model.Message, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
uuidChatID, err := getUUIDFromString(chatID)
if err != nil {
return nil, convertDBError(err)
}
afterCreatedAt := pgtype.Timestamptz{Valid: false}
afterID := pgtype.UUID{Valid: false}
if after != nil {
afterCreatedAt = pgtype.Timestamptz{Time: after.CreatedAt, Valid: true}
if uuidFromString, err := getUUIDFromString(after.ID); err == nil {
afterID = *uuidFromString
} else {
return nil, convertDBError(err)
}
}
sqlcMessages, err := r.queries.GetMessagesByChatID(dbCtx, sqlc.GetMessagesByChatIDParams{ChatID: *uuidChatID, AfterID: afterID, AfterCreatedAt: afterCreatedAt, LimitCount: int32(limit)})
if err != nil {
return nil, convertDBError(err)
}
messageIDs := make([]pgtype.UUID, len(sqlcMessages))
for index, _ := range sqlcMessages {
messageIDs[index] = sqlcMessages[index].ID
}
messageRow, err := r.queries.GetAttachmentsForMessages(dbCtx, messageIDs)
if err != nil {
return nil, convertDBError(err)
}
mapMessagesAtt := make(map[string][]string)
for index, _ := range messageRow {
messageID := messageRow[index].MessageID.String()
attachmentID := messageRow[index].AttachmentID.String()
mapMessagesAtt[messageID] = append(mapMessagesAtt[messageID], attachmentID)
}
messages := make([]*model.Message, len(sqlcMessages))
for index, sqlcMessage := range sqlcMessages {
messages[index] = &model.Message{
ID: sqlcMessage.ID.String(),
ChatID: sqlcMessage.ChatID.String(),
SenderID: sqlcMessage.SenderID.String(),
Text: sqlcMessage.Text,
AttachmentIDs: mapMessagesAtt[sqlcMessage.ID.String()],
CreatedAt: sqlcMessage.CreatedAt.Time}
}
return messages, nil
}
+167
View File
@@ -0,0 +1,167 @@
package postgres
import (
"context"
"errors"
"fmt"
"time"
"highload/internal/database/sqlc"
"highload/internal/repository"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
)
func (r *AttachmentRepository) ScheduleRetry(ctx context.Context, jobID string, generation int32, code string, at time.Time) (bool, error) {
id, err := getUUIDFromString(jobID)
if err != nil {
return false, err
}
n, err := r.queries.ScheduleRetry(ctx, sqlc.ScheduleRetryParams{
JobID: *id, LeaseGeneration: generation, ErrorCode: code,
AvailableAt: pgtype.Timestamptz{Time: at, Valid: true},
})
if err != nil {
return false, convertDBError(err)
}
return n == 1, nil
}
func (r *AttachmentRepository) FailJob(ctx context.Context, jobID string, generation int32, code string) (bool, error) {
id, err := getUUIDFromString(jobID)
if err != nil {
return false, err
}
n, err := r.queries.FailJob(ctx, sqlc.FailJobParams{
JobID: *id, LeaseGeneration: generation, ErrorCode: code,
})
if err != nil {
return false, convertDBError(err)
}
return n == 1, nil
}
func (r *AttachmentRepository) EnqueueExpiredLeases(ctx context.Context, batchSize int32) (int, error) {
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
rows, err := r.queries.EnqueueExpiredLeases(ctx, batchSize)
if err != nil {
return 0, convertDBError(err)
}
return len(rows), nil
}
// PublishPending holds the outbox row until the broker confirms delivery.
func (r *AttachmentRepository) PublishPending(ctx context.Context, publish func(context.Context, sqlc.Outbox) error) (bool, error) {
txCtx, cancel := context.WithTimeout(ctx, 8*time.Second)
defer cancel()
tx, err := r.pool.Begin(txCtx)
if err != nil {
return false, convertDBError(err)
}
defer rollbackTx(ctx, tx)
q := r.queries.WithTx(tx)
row, err := q.NextPendingOutboxEvent(txCtx)
if errors.Is(err, pgx.ErrNoRows) {
return false, nil
}
if err != nil {
return false, convertDBError(err)
}
if err := publish(txCtx, row); err != nil {
return false, err
}
n, err := q.MarkOutboxEventSent(txCtx, row.ID)
if err != nil {
return false, convertDBError(err)
}
if n != 1 {
return false, fmt.Errorf("outbox row disappeared during publish")
}
if err := tx.Commit(txCtx); err != nil {
return false, convertDBError(err)
}
return true, nil
}
func (r *AttachmentRepository) ListFailedJobs(ctx context.Context) ([]sqlc.ListFailedAttachmentJobsRow, error) {
return r.queries.ListFailedAttachmentJobs(ctx, 100)
}
// ReplayFailed checks the staged object while holding the same job lock as cleanup.
func (r *AttachmentRepository) ReplayFailed(ctx context.Context, jobID, requestID, operator, reason string, extra int32, check func(context.Context, string) error) (bool, error) {
id, err := getUUIDFromString(jobID)
if err != nil {
return false, err
}
request, err := getUUIDFromString(requestID)
if err != nil {
return false, err
}
txCtx, cancel := context.WithTimeout(ctx, attachmentTransactionTimeout)
defer cancel()
tx, err := r.pool.Begin(txCtx)
if err != nil {
return false, convertDBError(err)
}
defer rollbackTx(ctx, tx)
q := r.queries.WithTx(tx)
job, err := q.LockAttachmentJobForReplay(txCtx, *id)
if err != nil {
return false, convertDBError(err)
}
att, err := q.GetAttachmentByID(txCtx, job.AttachmentID)
if err != nil {
return false, convertDBError(err)
}
if att.Status != "failed" || att.PipelineVersion != job.PipelineVersion {
return false, nil
}
if !att.StagingKey.Valid {
return false, fmt.Errorf("staging has expired: %w", repository.ErrNotFound)
}
if err := check(txCtx, att.StagingKey.String); err != nil {
return false, err
}
rows, err := q.ReplayFailed(txCtx, sqlc.ReplayFailedParams{
JobID: *id, RequestID: *request, OperatorID: operator, Reason: reason, ExtraAttempts: extra,
})
if err != nil {
return false, convertDBError(err)
}
if rows == 0 {
return false, nil
}
if err := tx.Commit(txCtx); err != nil {
return false, convertDBError(err)
}
return true, nil
}
func (r *AttachmentRepository) PublishedObjectKeys(ctx context.Context, attachmentID string, version int32) ([]string, error) {
id, err := getUUIDFromString(attachmentID)
if err != nil {
return nil, err
}
original, err := r.queries.GetPublishedOriginalKey(ctx, sqlc.GetPublishedOriginalKeyParams{
AttachmentID: *id, PipelineVersion: version,
})
if err != nil {
return nil, convertDBError(err)
}
variants, err := r.queries.ListAttachmentVariants(ctx, sqlc.ListAttachmentVariantsParams{
AttachmentID: *id, PipelineVersion: version,
})
if err != nil {
return nil, convertDBError(err)
}
keys := []string{original.String}
for _, v := range variants {
keys = append(keys, v.ObjectKey)
}
if len(keys) != 4 {
return nil, fmt.Errorf("ready job has an incomplete variant set")
}
return keys, nil
}
@@ -0,0 +1,118 @@
package postgres
import (
"context"
"errors"
"strconv"
"strings"
"time"
"highload/internal/database/sqlc"
"highload/internal/storage"
"github.com/jackc/pgx/v5"
)
type CleanupPolicy struct {
Now time.Time
Grace time.Duration
Retention time.Duration
DryRun bool
}
// CleanupObject synchronizes with upload, finalization and operator replay.
// All candidate metadata is rechecked while holding the relevant lock.
func (r *AttachmentRepository) CleanupObject(ctx context.Context, obj storage.ObjectInfo, policy CleanupPolicy, deleteObject func(context.Context, string) error) (bool, error) {
txCtx, cancel := context.WithTimeout(ctx, attachmentTransactionTimeout)
defer cancel()
tx, err := r.pool.Begin(txCtx)
if err != nil {
return false, convertDBError(err)
}
defer rollbackTx(ctx, tx)
q := r.queries.WithTx(tx)
locked, err := q.TryLockAttachmentObjectKey(txCtx, obj.Key)
if err != nil || !locked {
return false, err
}
staging, err := q.GetAttachmentByStagingKey(txCtx, obj.Key)
keepObject := false
switch {
case err == nil:
version := staging.PipelineVersion
_, err = q.LockAttachmentJobForPipeline(txCtx, sqlc.LockAttachmentJobForPipelineParams{
AttachmentID: staging.ID, PipelineVersion: staging.PipelineVersion,
})
if err != nil {
return false, err
}
staging, err = q.GetAttachmentByID(txCtx, staging.ID)
if err != nil {
return false, err
}
if staging.PipelineVersion != version || staging.StagingKey.String != obj.Key ||
(staging.Status == "ready" && staging.ObjectKey.String == obj.Key) {
return false, nil
}
if staging.Status != "ready" && (staging.Status != "failed" || staging.UpdatedAt.Time.Add(policy.Retention).After(policy.Now)) {
return false, nil
}
// An upgrade can stage the original published by the previous version.
keepObject, err = q.IsPublishedOriginalKey(txCtx, obj.Key)
if err != nil {
return false, err
}
case !errors.Is(err, pgx.ErrNoRows):
return false, err
default:
if obj.LastModified.IsZero() || obj.LastModified.Add(policy.Grace).After(policy.Now) {
return false, nil
}
if strings.HasPrefix(obj.Key, "results/") {
// Keys include the job version and the generating lease.
parts := strings.Split(obj.Key, "/")
if len(parts) != 5 {
return false, nil
}
id, idErr := getUUIDFromString(parts[1])
version, versionErr := strconv.ParseInt(strings.TrimPrefix(parts[2], "v"), 10, 32)
generation, generationErr := strconv.ParseInt(parts[3], 10, 32)
if idErr != nil || versionErr != nil || generationErr != nil || version < 1 || generation < 1 {
return false, nil
}
job, err := q.LockAttachmentJobForPipeline(txCtx, sqlc.LockAttachmentJobForPipelineParams{
AttachmentID: *id, PipelineVersion: int32(version),
})
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
return false, err
}
if err == nil && job.LeaseGeneration == int32(generation) && job.LeaseUntil.Valid {
return false, nil
}
}
exists, err := q.AttachmentExistsByObjectKey(txCtx, obj.Key)
if err != nil || exists {
return false, err
}
}
if policy.DryRun {
return true, nil
}
if !keepObject {
if err := deleteObject(txCtx, obj.Key); err != nil && !errors.Is(err, storage.ErrObjectNotFound) {
return false, err
}
}
if staging.StagingKey.Valid {
if _, err = q.ClearStagingReference(txCtx, sqlc.ClearStagingReferenceParams{
ID: staging.ID, StagingKey: staging.StagingKey,
}); err != nil {
return false, err
}
}
if err := tx.Commit(txCtx); err != nil {
return false, err
}
return !keepObject, nil
}
+85
View File
@@ -0,0 +1,85 @@
package postgres
import (
"context"
"highload/internal/database/sqlc"
"highload/internal/model"
"time"
)
type UserRepository struct {
queries *sqlc.Queries
}
func NewUserRepository(queries *sqlc.Queries) *UserRepository {
return &UserRepository{queries: queries}
}
func (r *UserRepository) Create(ctx context.Context, username, displayName string, passwordHash string) (*model.User, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
sqlcUser, err := r.queries.CreateUser(dbCtx, sqlc.CreateUserParams{Username: username, DisplayName: displayName, PasswordHash: passwordHash})
if err != nil {
return nil, convertDBError(err)
}
user := &model.User{
ID: sqlcUser.ID.String(),
Username: sqlcUser.Username,
DisplayName: sqlcUser.DisplayName,
CreatedAt: sqlcUser.CreatedAt.Time,
PasswordHash: sqlcUser.PasswordHash,
}
return user, nil
}
func (r *UserRepository) GetUserByID(ctx context.Context, id string) (*model.User, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
uuidID, err := getUUIDFromString(id)
if err != nil {
return nil, convertDBError(err)
}
sqlcUser, err := r.queries.GetUserByID(dbCtx, *uuidID)
if err != nil {
return nil, convertDBError(err)
}
user := &model.User{
ID: sqlcUser.ID.String(),
Username: sqlcUser.Username,
DisplayName: sqlcUser.DisplayName,
CreatedAt: sqlcUser.CreatedAt.Time,
PasswordHash: sqlcUser.PasswordHash,
}
return user, nil
}
func (r *UserRepository) GetUserByUsername(ctx context.Context, username string) (*model.User, error) {
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
sqlcUser, err := r.queries.GetUserByUsername(dbCtx, username)
if err != nil {
return nil, convertDBError(err)
}
user := &model.User{
ID: sqlcUser.ID.String(),
Username: sqlcUser.Username,
DisplayName: sqlcUser.DisplayName,
CreatedAt: sqlcUser.CreatedAt.Time,
PasswordHash: sqlcUser.PasswordHash,
}
return user, nil
}
+26
View File
@@ -0,0 +1,26 @@
package postgres
import (
"context"
"highload/internal/repository"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
)
func getUUIDFromString(id string) (*pgtype.UUID, error) {
var uuidID pgtype.UUID
if err := uuidID.Scan(id); err != nil {
return nil, repository.ErrInvalidInput
}
return &uuidID, nil
}
func rollbackTx(ctx context.Context, tx pgx.Tx) {
rollbackCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), time.Second)
defer cancel()
_ = tx.Rollback(rollbackCtx)
}
@@ -0,0 +1,87 @@
package postgres
import (
"context"
"fmt"
"highload/internal/database/sqlc"
"highload/internal/repository"
"github.com/jackc/pgx/v5/pgtype"
)
func (r *AttachmentRepository) createPipelineJob(ctx context.Context, q *sqlc.Queries, att sqlc.Attachment) (pgtype.UUID, error) {
job, err := q.CreateAttachmentJob(ctx, sqlc.CreateAttachmentJobParams{
AttachmentID: att.ID, PipelineVersion: att.PipelineVersion,
AvailableAt: att.UpdatedAt, MaxAttempts: r.maxAttempts,
})
if err != nil {
return pgtype.UUID{}, convertDBError(err)
}
if err := q.CreateInitialOutboxEvent(ctx, job.ID); err != nil {
return pgtype.UUID{}, convertDBError(err)
}
return job.ID, nil
}
// UpgradePipeline serializes the version switch with version 1's finalization,
// retries and staging cleanup on its job row. The version 2 job starts fresh.
func (r *AttachmentRepository) UpgradePipeline(ctx context.Context, attachmentID string, checkSource func(context.Context, string) error) (string, bool, error) {
id, err := getUUIDFromString(attachmentID)
if err != nil {
return "", false, err
}
txCtx, cancel := context.WithTimeout(ctx, attachmentTransactionTimeout)
defer cancel()
tx, err := r.pool.Begin(txCtx)
if err != nil {
return "", false, convertDBError(err)
}
defer rollbackTx(ctx, tx)
q := r.queries.WithTx(tx)
oldJob, err := q.LockAttachmentJobForPipeline(txCtx, sqlc.LockAttachmentJobForPipelineParams{
AttachmentID: *id, PipelineVersion: 1,
})
if err != nil {
return "", false, convertDBError(err)
}
att, err := q.GetAttachmentByID(txCtx, *id)
if err != nil {
return "", false, convertDBError(err)
}
if att.PipelineVersion == 2 {
jobID, err := q.GetPipelineJobID(txCtx, sqlc.GetPipelineJobIDParams{AttachmentID: *id, PipelineVersion: 2})
if err != nil {
return "", false, convertDBError(err)
}
return jobID.String(), false, nil
}
if att.PipelineVersion != 1 {
return "", false, fmt.Errorf("cannot upgrade pipeline version %d", att.PipelineVersion)
}
source := att.StagingKey
if att.Status == "ready" {
source = att.ObjectKey
}
if !source.Valid {
return "", false, fmt.Errorf("original has expired: %w", repository.ErrNotFound)
}
if err := checkSource(txCtx, source.String); err != nil {
return "", false, err
}
if err := q.SupersedeAttachmentJob(txCtx, oldJob.ID); err != nil {
return "", false, convertDBError(err)
}
upgraded, err := q.QueueAttachmentUpgrade(txCtx, sqlc.QueueAttachmentUpgradeParams{ID: *id, SourceKey: source.String})
if err != nil {
return "", false, convertDBError(err)
}
jobID, err := r.createPipelineJob(txCtx, q, upgraded)
if err != nil {
return "", false, err
}
if err := tx.Commit(txCtx); err != nil {
return "", false, convertDBError(err)
}
return jobID.String(), true, nil
}
+38
View File
@@ -0,0 +1,38 @@
package redis
import (
"context"
"errors"
"fmt"
"highload/internal/repository"
"io"
"net"
"github.com/redis/go-redis/v9"
)
func convertDBError(err error) error {
category := repository.ErrDatabase
var netErr *net.OpError
if errors.Is(err, redis.Nil) {
category = repository.ErrNotFound
}
if errors.As(err, &netErr) {
category = repository.ErrServiceUnavailable
}
if errors.Is(err, context.DeadlineExceeded) {
category = repository.ErrServiceUnavailable
}
if errors.Is(err, redis.ErrPoolTimeout) {
category = repository.ErrServiceUnavailable
}
if errors.Is(err, io.EOF) ||
errors.Is(err, io.ErrUnexpectedEOF) {
category = repository.ErrServiceUnavailable
}
return fmt.Errorf("%w: %v", category, err)
}
+127
View File
@@ -0,0 +1,127 @@
package redis
import (
"context"
"encoding/hex"
"encoding/json/v2"
"fmt"
"highload/internal/model"
"highload/internal/repository"
"time"
"github.com/redis/go-redis/v9"
)
type SessionRepository struct {
client *redis.Client
}
func NewSessionRepository(client *redis.Client) *SessionRepository {
return &SessionRepository{client: client}
}
type sessionData struct {
UserID string `json:"user_id"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
}
func (r *SessionRepository) Create(ctx context.Context, tokenHash []byte, userID string, sessionTTL time.Duration) (*model.Session, error) {
key := "session:" + hex.EncodeToString(tokenHash)
now := time.Now().UTC()
data := sessionData{
UserID: userID,
CreatedAt: now,
ExpiresAt: now.Add(sessionTTL),
}
value, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("%w: encoding session %v", repository.ErrDatabase, err)
}
err = r.client.Set(ctx, key, value, sessionTTL).Err()
if err != nil {
return nil, convertDBError(err)
}
session := &model.Session{
TokenHash: tokenHash,
UserID: data.UserID,
CreatedAt: data.CreatedAt,
ExpiresAt: data.ExpiresAt,
RevokedAt: nil,
}
return session, nil
}
func (r *SessionRepository) GetSessionByTokenHash(ctx context.Context, tokenHash []byte) (*model.Session, error) {
key := "session:" + hex.EncodeToString(tokenHash)
value, err := r.client.Get(ctx, key).Result()
if err != nil {
return nil, convertDBError(err)
}
var data sessionData
err = json.Unmarshal([]byte(value), &data)
if err != nil {
return nil, fmt.Errorf("%w: decode session %v", repository.ErrDatabase, err)
}
session := &model.Session{
TokenHash: tokenHash,
UserID: data.UserID,
CreatedAt: data.CreatedAt,
ExpiresAt: data.ExpiresAt,
RevokedAt: nil,
}
return session, nil
}
func (r *SessionRepository) RevokeSession(
ctx context.Context,
tokenHash []byte,
) (int, error) {
key := "session:" + hex.EncodeToString(tokenHash)
conn := r.client.Conn()
defer conn.Close()
deleted, err := conn.Del(ctx, key).Result()
if err != nil {
return 0, convertDBError(err)
}
result, err := conn.Do(ctx, "WAITAOF", 1, 0, 2000).Slice()
if err != nil {
return 0, convertDBError(err)
}
if len(result) != 2 {
return 0, fmt.Errorf(
"%w: WAITAOF: expected 2 counters, got %d",
repository.ErrDatabase, len(result),
)
}
local, ok := result[0].(int64)
if !ok {
return 0, fmt.Errorf("%w: expected int64, got %T", repository.ErrDatabase, result[0])
}
if local != 1 {
return 0, fmt.Errorf("%w: WAITAOF expected local fsync = 1, got %d", repository.ErrServiceUnavailable, local)
}
return int(deleted), nil
}
+13
View File
@@ -0,0 +1,13 @@
package repository
import (
"context"
"highload/internal/model"
"time"
)
type SessionRepository interface {
Create(ctx context.Context, tokenHash []byte, userID string, sessionTTL time.Duration) (*model.Session, error)
GetSessionByTokenHash(ctx context.Context, tokenHash []byte) (*model.Session, error)
RevokeSession(ctx context.Context, tokenHash []byte) (int, error)
}

Some files were not shown because too many files have changed in this diff Show More