Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dc46ac213d |
@@ -18,3 +18,7 @@ __pycache__
|
||||
node_modules
|
||||
evidence
|
||||
.DS_Store
|
||||
.pnpm-store
|
||||
dist
|
||||
src/generated
|
||||
*.tsbuildinfo
|
||||
|
||||
@@ -14,7 +14,25 @@ IDEMPOTENCY_TTL_SECONDS=86400
|
||||
S3_ENDPOINT=http://s3:9000
|
||||
S3_REGION=us-east-1
|
||||
S3_BUCKET=messenger-attachments
|
||||
# Compose: root is used only by RustFS and the one-shot bootstrap.
|
||||
S3_ROOT_ACCESS_KEY_ID=LABSTORAGE
|
||||
S3_ROOT_SECRET_ACCESS_KEY=local_app_storage_password
|
||||
S3_API_ACCESS_KEY_ID=LABAPI
|
||||
S3_API_SECRET_ACCESS_KEY=local_api_storage_password
|
||||
S3_RECONCILER_ACCESS_KEY_ID=LABRECONCILER
|
||||
S3_RECONCILER_SECRET_ACCESS_KEY=local_reconciler_storage_password
|
||||
# Running outside Compose: files must contain the selected restricted role's keys.
|
||||
S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id
|
||||
S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
|
||||
MAX_UPLOAD_BYTES=10485760
|
||||
MAX_IMAGE_PIXELS=40000000
|
||||
# Total time to receive an upload body, even if bytes continue arriving.
|
||||
UPLOAD_BODY_TIMEOUT_MS=15000
|
||||
S3_PUBLIC_PORT=9000
|
||||
S3_PUBLIC_ENDPOINT=http://127.0.0.1:9000
|
||||
DIRECT_UPLOAD_TTL_SECONDS=60
|
||||
# Optional per-stage upload diagnostics; keep disabled outside experiments.
|
||||
UPLOAD_MEMORY_PROFILE=0
|
||||
# S3 orphan cleanup: grace period and pause between reconciliation passes.
|
||||
ORPHAN_GRACE_SECONDS=600
|
||||
RECONCILE_INTERVAL_SECONDS=60
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
infra/*.sh text eol=lf
|
||||
scripts/*.sh text eol=lf
|
||||
scripts/*.cmd text eol=crlf
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"$schema": "./node_modules/oxlint/configuration_schema.json",
|
||||
"ignorePatterns": ["src/generated/**"],
|
||||
"rules": {
|
||||
"typescript/no-explicit-any": "off",
|
||||
"typescript/no-floating-promises": "error"
|
||||
},
|
||||
"env": {
|
||||
"node": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"singleQuote": true,
|
||||
"trailingComma": "all"
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
FROM node:24.20.0-bookworm-slim AS base
|
||||
|
||||
WORKDIR /app
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends openssl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
RUN npm install --global pnpm@10.14.0
|
||||
|
||||
FROM base AS dependencies
|
||||
|
||||
COPY package.json pnpm-lock.yaml ./
|
||||
RUN pnpm install --frozen-lockfile
|
||||
|
||||
FROM dependencies AS migrate
|
||||
|
||||
COPY prisma ./prisma
|
||||
COPY prisma.config.ts ./
|
||||
CMD ["pnpm", "db:deploy"]
|
||||
|
||||
FROM migrate AS build
|
||||
|
||||
COPY nest-cli.json tsconfig.json tsconfig.build.json ./
|
||||
COPY src ./src
|
||||
RUN pnpm build
|
||||
|
||||
FROM base AS production-dependencies
|
||||
|
||||
COPY package.json pnpm-lock.yaml ./
|
||||
RUN pnpm install --prod --frozen-lockfile
|
||||
|
||||
FROM production-dependencies AS storage-init
|
||||
|
||||
COPY infra/rustfs ./infra/rustfs
|
||||
CMD ["node", "infra/rustfs/bootstrap.mjs"]
|
||||
|
||||
FROM node:24.20.0-bookworm-slim AS runtime
|
||||
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends curl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
ENV NODE_ENV=production \
|
||||
APP_HOST=0.0.0.0 \
|
||||
APP_PORT=8080
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN groupadd --system --gid 10001 app \
|
||||
&& useradd --system --uid 10001 --gid app --home-dir /app --shell /usr/sbin/nologin app
|
||||
|
||||
RUN mkdir -p /app/data && chown 10001:10001 /app/data
|
||||
|
||||
COPY --from=production-dependencies --chown=10001:10001 /app/node_modules ./node_modules
|
||||
COPY --from=build --chown=10001:10001 /app/dist ./dist
|
||||
COPY --from=build --chown=10001:10001 /app/package.json ./package.json
|
||||
COPY --chown=10001:10001 contracts ./contracts
|
||||
|
||||
USER 10001:10001
|
||||
EXPOSE 8080
|
||||
|
||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=15s --retries=3 \
|
||||
CMD curl --fail --silent --show-error --max-time 2 --output /dev/null "http://127.0.0.1:${APP_PORT:-8080}/health/ready"
|
||||
|
||||
CMD ["node", "dist/main.js"]
|
||||
@@ -1,8 +0,0 @@
|
||||
# Teaching outline, not a runnable Dockerfile. Implement your own Dockerfile.
|
||||
# 1. Choose a runtime/toolchain version and pin the base image tag/digest.
|
||||
# 2. Copy dependency manifests first; install from the language lockfile.
|
||||
# 3. Copy source and build. For compiled languages use a separate runtime stage.
|
||||
# 4. Run the HTTP process as PID 1; listen on 0.0.0.0:8080.
|
||||
# 5. Add non-root, signals and healthcheck according to the current rubric.
|
||||
# 6. Pass runtime config/secrets at startup, never COPY .env or secret files.
|
||||
# This file deliberately has no FROM/CMD: selecting/implementing them is the task.
|
||||
@@ -1,37 +1,186 @@
|
||||
# Отчёт — лабораторная 4
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор, группа: **Пинаев Олег Константинович, БПИ2302**.
|
||||
- Целевая оценка: **5, трек 1 — прямой upload по presigned URL**.
|
||||
- Версия: локальная реализация, PR пока не опубликован. Образы API и SHA-256 исходников/конфигурации на момент проверки сохранены в [final-regression.json](evidence/final-regression.json). После проверки оформлены отчёт и документация, удалены устаревшие результаты; код приложения не менялся.
|
||||
- Источники: `README.md`, `COURSE.md`, `contracts/`, документация Node.js, Nginx, Sharp, AWS S3 и RustFS. ИИ использовался при разработке, подготовке проверок, замеров и отчёта.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Нужны Docker Desktop с Linux-контейнерами и Compose, Python **3.9+**, Node.js **24.20.0**, pnpm **10.14.0**. На Windows restart-hook использует Git Bash; при нестандартной установке задаётся `GIT_BASH`. Наблюдатели памяти используют Linux `/proc` и cgroup v2 внутри контейнеров.
|
||||
|
||||
Образы закреплены: `node:24.20.0-bookworm-slim`, `postgres:17.6-bookworm`, `redis:7.4.5-alpine`, `nginx:1.30.6-alpine`, `rustfs/rustfs:1.0.4`. Приложение — NestJS 12 / Fastify 5, Prisma 7.10.0, Sharp 0.35.5; конкретные зависимости закреплены в `pnpm-lock.yaml`.
|
||||
|
||||
```sh
|
||||
cp .env.example .env
|
||||
pnpm install --frozen-lockfile
|
||||
docker compose --env-file .env config --quiet
|
||||
docker compose --env-file .env up -d --build --wait --wait-timeout 180
|
||||
chmod +x scripts/restart.sh scripts/stop-one.sh
|
||||
python -X utf8 test/final-regression.py
|
||||
```
|
||||
|
||||
`migrate` применяет миграции PostgreSQL. Одноразовый `s3-init` создаёт приватный bucket и ограниченные роли. API ждут миграции, хранилища и bootstrap; Nginx и reconciler — готовность API. Повторный bootstrap идемпотентен. Перед проверками должны быть healthy все семь runtime-контейнеров и доступны обе API через `/health/ready`. При обновлении существующего стенда возможен переходный unhealthy Nginx во время смены upstream; runner ждёт фактического восстановления маршрутов.
|
||||
|
||||
API доступен на **http://127.0.0.1:8080**, документация — `/swagger` и `/docs`, OpenAPI — `/swagger-json`. **127.0.0.1:9000** — отдельный listener Nginx для presigned PUT, не открытая консоль RustFS.
|
||||
|
||||
| Настройка | Фактическое использование |
|
||||
| --- | --- |
|
||||
| `APP_PORT` | Публичный порт Nginx; внутри API всегда 8080 |
|
||||
| `S3_PUBLIC_PORT`, `S3_PUBLIC_ENDPOINT` | Listener PUT и origin подписи; адрес и порт должны совпадать |
|
||||
| `S3_ENDPOINT`, `S3_REGION`, `S3_BUCKET` | В Compose закреплены как `http://s3:9000`, `us-east-1`, `messenger-attachments`; одноимённые поля примера не переопределяют эти константы |
|
||||
| `POSTGRES_DB/USER/PASSWORD` | Общая БД; Compose собирает `DATABASE_URL`, адрес Redis задаёт через `CACHE_URL` |
|
||||
| `SESSION_TTL_SECONDS`, `IDEMPOTENCY_TTL_SECONDS` | По умолчанию 3600 и 86400 с; сессии и идемпотентность хранятся в PostgreSQL |
|
||||
| `DB_POOL_MAX`, `CACHE_PREFIX` | По умолчанию 15 соединений на API и общее пространство ключей Redis; у сверщика пул 2 |
|
||||
| `S3_ROOT_*`, `S3_API_*`, `S3_RECONCILER_*` | Раздельные credentials bootstrap, API и сверщика |
|
||||
| `MAX_UPLOAD_BYTES`, `MAX_IMAGE_PIXELS` | Не более 10485760 байт и 40000000 пикселей; можно уменьшить |
|
||||
| `UPLOAD_BODY_TIMEOUT_MS` | Общий срок приёма тела, по умолчанию 15000 мс |
|
||||
| `DIRECT_UPLOAD_TTL_SECONDS` | Максимальный срок URL, по умолчанию 60 с, допустимо 1–300 с |
|
||||
| `ORPHAN_GRACE_SECONDS`, `RECONCILE_INTERVAL_SECONDS` | Grace 600 с и пауза между проходами 60 с |
|
||||
| `UPLOAD_MEMORY_PROFILE` | `0` штатно; `1` только для подробного эксперимента |
|
||||
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
```mermaid
|
||||
flowchart LR
|
||||
C[Клиент] --> N[Nginx: API]
|
||||
N --> A1[API 1]
|
||||
N --> A2[API 2]
|
||||
A1 --> P[(PostgreSQL)]
|
||||
A2 --> P
|
||||
A1 --> R[(Redis: общий кеш)]
|
||||
A2 --> R
|
||||
A1 --> S[(RustFS: приватный bucket)]
|
||||
A2 --> S
|
||||
C --> U[Nginx: presigned PUT]
|
||||
U --> S
|
||||
W[Reconciler] --> P
|
||||
W --> S
|
||||
```
|
||||
|
||||
### Proxy upload и скачивание
|
||||
|
||||
`POST /api/v1/chats/{chat_id}/attachments` принимает сырые PNG/JPEG. Fastify собирает ограниченный Buffer; сервис проверяет участие в чате, filename, фактический формат и MIME. Sharp сначала читает размеры и число страниц, отклоняет более 40 млн пикселей, затем выполняет полное декодирование через `stats()`. Оригинал не перекодируется. Пустые имена, `/`, `\` и управляющие символы отклоняются; filename не используется как S3-путь.
|
||||
|
||||
Сервер назначает ключ `originals/<chat UUID>/<attachment UUID>`. API сохраняет объект, считает SHA-256 и записывает метаданные в PostgreSQL; только после успеха возвращает **201 ready**, `variants=[]`, `error_code=null`. Вложение прикрепляется к сообщению через `attachment_ids`. Чужое/неизвестное вложение и файл другого чата дают 404; другое содержимое с прежним Idempotency-Key — 409.
|
||||
|
||||
`GET /api/v1/attachments/{id}/content` проверяет членство в чате и передаёт поток RustFS клиенту, без сбора целого файла в Buffer и без redirect. Bucket приватен, анонимный GET проверяется отдельно.
|
||||
|
||||
Приём тела ограничен 15 с с заголовков; новые байты не продлевают срок. Таймер снимается после получения тела, проверка и запись имеют отдельные бюджеты. При обрыве неполное тело не попадает в сервис вложений. Для раннего 413 остаток тела ограниченно вычитывается и отбрасывается перед ответом: закрытие upstream не должно превращать 413 в 502 у Nginx. Discard ограничен объёмом и оставшимся исходным сроком приёма, остаток не собирается в память. Нормальный upload по-прежнему использует Buffer до 10 MiB.
|
||||
|
||||
### Согласованность PostgreSQL и RustFS
|
||||
|
||||
Распределённой атомарной транзакции между ними нет. Upload держит транзакционную advisory-блокировку по object key от PUT до завершения метаданных. При ошибке выполняется компенсация под той же блокировкой: повторная проверка записи, возврат уже подтверждённого вложения либо удаление неучтённого объекта. Ошибка БД не трактуется как отсутствие метаданных.
|
||||
|
||||
Отдельный `reconciler` выполняет последовательные проходы; после завершения прохода ждёт 60 с. Grace — 10 минут. Сверщик рассматривает только управляемые UUID-ключи, проверяет БД, возраст, блокировку и свежий HEAD, затем повторно проверяет метаданные перед DELETE. Свежие и связанные объекты сохраняются. Отсутствующий файл существующего Attachment диагностируется, метаданные автоматически не удаляются. Есть dry-run и повторяемый apply. Успех обновляет health-marker, ошибка снимает готовность сверщика.
|
||||
|
||||
Транзакция работы с объектом ограничена 20 с, ожидание транзакции — 5 с, idle-транзакция — 15 с. Подключение и SQL — 5 с. S3-операции имеют собственные таймауты; direct-чтение в файл — бюджет 15 с. Падение писателя освобождает транзакционную блокировку.
|
||||
|
||||
### Presigned upload: трек 1
|
||||
|
||||
Клиент считает SHA-256 и вызывает `POST /api/v1/chats/{chat_id}/attachment-uploads` с filename, MIME, размером и checksum. API проверяет доступ, резервирует ожидаемую загрузку в БД и выдаёт короткий PUT URL. Ключ уникален: `staging/<chat UUID>/<upload UUID>`. Подпись связывает ключ, Content-Length, Content-Type, SHA-256 и `If-None-Match: *`; больший TTL клиент запросить не может.
|
||||
|
||||
Байты идут через специальный listener Nginx в RustFS, минуя NestJS. Затем владелец вызывает `complete`. API снова проверяет доступ и срок, потоково читает объект во временный файл, ограничивает длину и считает SHA-256, проверяет изображение через Sharp. Условный серверный COPY публикует `originals`; в одной транзакции PostgreSQL создаётся ready Attachment и разрешение отмечается completed. Временный файл удаляется и не является хранилищем готового оригинала.
|
||||
|
||||
Повтор complete возвращает тот же Attachment. Конкурирующий complete может получить 409 и повториться с прежним ID. Staging и original разделены: повтор PUT по ещё действующей ссылке не меняет готовое вложение. Неверные размер/hash дают 400/413, изображение — 415; нет файла или срок истёк — 409. Старый staging удаляется после истечения разрешения и grace; история разрешений остаётся в БД.
|
||||
|
||||
API получает CRUD для `originals/*` и `staging/*`, без LIST. Сверщик получает ограниченный LIST/GET/HEAD/DELETE, без PUT. Root доступен только RustFS и bootstrap. Listener PUT пропускает только PUT/OPTIONS на staging-пути.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
| Критерий | Команда/сценарий | Результат / доказательство |
|
||||
| --- | --- | --- |
|
||||
| Структура и ссылки | `python -X utf8 scripts/check_contract.py` | PASS, офлайн-проверка отдельно от приложения |
|
||||
| Сборка, линтер, unit | `docker compose build`, `pnpm lint`, `pnpm test --runInBand` | Образы собраны; линтер PASS, 9 suites / **47 tests** PASS |
|
||||
| Обязательный API | `tests/smoke.py --base-url http://127.0.0.1:8080` до и после отказов | PASS: сессии, сообщения, идемпотентность, файл, SHA-256, запрет постороннему |
|
||||
| Форматы и лимиты | `test/attachments.py --lifecycle` | PNG/JPEG побайтно; неверные MIME/изображения/filename, >10 MiB и >40 млн пикселей отклонены |
|
||||
| Ответ на большой body | `test/oversized-uploads.py` | **16** Content-Length/chunked-запросов → канонический 413; обе API, следующая загрузка работает. [JSON](evidence/oversized-uploads.json) |
|
||||
| ACL и связь с сообщением | `test/attachments.py --lifecycle` | Чужой ID/чат — 404; дубли ID — 400, конфликт idem-key — 409 |
|
||||
| Приватность и отказ S3 | `test/attachments.py --lifecycle` | Анонимный GET — 403; при остановке S3 metadata — 200, content/upload/readiness API — 503; LB исключает неготовые API; после восстановления оригинал доступен |
|
||||
| Сохранность после restart | `test/hooks.py`, публичный persistence-hook | Все **7 runtime** действительно перезапущены, ID контейнеров сохранены, прежние данные/сессия/файл доступны. [Состояния](evidence/hooks.json) |
|
||||
| Отказ каждой API | `test/hooks.py`, публичный failover-hook | Обе API остановлены по отдельности, другие сервисы не перезапускались; запись восстановилась за **4,206 / 4,127 с**, подтверждённые сообщения сохранены, без дублей |
|
||||
| Download другой репликой, Swagger | `test/replicas.py --base-url http://127.0.0.1:8080` | Канонические пути/схемы на обеих API; файл и idem-replay доступны после остановки загрузившей реплики |
|
||||
| Обрыв и deadline | `test/upload-interruption.py` | PASS на обеих API и Nginx: нет bytes/metadata после обрыва, медленная передача → 408, следующий upload/download работает. [JSON](evidence/upload-interruption.json) |
|
||||
| Сбой между PUT и БД, очистка | `test/run-reconcile.py` | Реальное завершение writer после PUT, DB outage, dry-run/apply, orphan-delete, защита активного/свежего/связанного, повтор и missing-report — PASS |
|
||||
| Ограниченные S3-роли | `test/s3-permissions.mjs` через `s3-init` | API без LIST, scanner без PUT; чужие bucket/префиксы, IAM и bucket policy запрещены; anonymous access закрыт |
|
||||
| Presigned и негативные сценарии | `test/direct-uploads.py` | ACL, подпись/заголовки/checksum, PNG/JPEG/MIME/pixels, expiry, concurrent/replay complete, staging cleanup и proxy — PASS. [JSON](evidence/direct-uploads.json) |
|
||||
| Гарантии выбранного S3 | `test/s3-direct-capabilities.mjs` через `s3-init` | RustFS 1.0.4 проверяет SHA-256, create-only PUT и условный COPY |
|
||||
| ≥10 upload и RAM | `test/direct-upload-load.py` | **128/128** upload/download, без ошибок, OOM и рестартов API; точный прогрев. [JSON](evidence/direct-upload-load.json) |
|
||||
| Трек 1: ≥2 размера, сеть и RAM | `test/direct-upload-benchmark.py --restart-api --rounds 2` | 24 измеряемых upload, два размера, полный flow, RSS и RX+TX. [JSON](evidence/direct-upload-benchmark-restarted.json) |
|
||||
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
### Гипотеза и среда
|
||||
|
||||
Гипотеза: прямой PUT с серверным COPY уменьшит трафик API; при параллельных больших upload отсутствие нескольких полных входных Buffer должно уменьшить пик памяти. Проверка изображения остаётся на API, поэтому нулевой трафик и гарантированное снижение RSS одиночного запроса не ожидаются.
|
||||
|
||||
Вход — детерминированные плохо сжимаемые PNG: **512×512, 787247 байт (~0,75 MiB)** и **1792×1792, 9638577 байт (~9,19 MiB)**. Две API во всех вариантах; PG/Redis/RustFS между сериями не перезапускаются. У direct измеряется весь flow: выдача URL, PUT и complete. Скачивания выполняются после измерения. Прозрачных повторов запросов нет.
|
||||
|
||||
Основная память — RSS **главного Node-процесса API**, отдельно по репликам, через `/proc/<PID>/status` каждые 50 мс. Наблюдатели в эту RSS не входят. Cgroup memory контейнера, включающая наблюдатель и файловый кеш, сохраняется отдельно. Табличный RSS — наибольший наблюдавшийся пик **одной** реплики. Сеть — RX+TX обеих API, включая БД/S3 и healthchecks.
|
||||
|
||||
### Последовательный upload: два размера
|
||||
|
||||
Перед каждой серией обе API перезапускаются; четыре прогревочные загрузки со скачиванием, подтверждён прогрев обеих API. Затем три измеряемые загрузки. Два раунда, во втором порядок режимов обратный. Все восемь серий прошли: **24/24**, без OOM, исходные байты проверены, fixtures удалены. Диапазоны ниже — по двум раундам, трафик на три файла.
|
||||
|
||||
| Файл | Proxy RX+TX, MiB | Direct RX+TX, MiB | Proxy RSS, MiB | Direct RSS, MiB |
|
||||
| --- | ---: | ---: | ---: | ---: |
|
||||
| 0,75 MiB | 4,555–4,572 | 2,325–2,330 | 190,12–192,58 | 195,95–198,17 |
|
||||
| 9,19 MiB | 55,510–55,861 | 27,687–27,750 | 233,62–238,40 | 245,64–248,80 |
|
||||
|
||||
Трафик API сократился примерно вдвое. Direct исключает передачу клиентского файла через API в S3, но API читает его из S3 для проверки; публикация — server-side COPY. **Снижение RSS в последовательной серии не подтвердилось:** у direct пик был выше. Этот результат не подменяется более удачным последующим прогоном.
|
||||
|
||||
### Конкуренция 1 / 5 / 10
|
||||
|
||||
Большой файл, два раунда с обратным порядком, по две волны каждого уровня. Перед каждым случаем API перезапускаются; временный docker-exec клиент делает **ровно две** прогревочные загрузки/скачивания на каждой API по внутреннему адресу и завершается. Измеряемые запросы идут через публичный Nginx. Адресный прогрев устраняет ситуацию, когда все complete при обычной последовательности запросов попадали на одну реплику.
|
||||
|
||||
Все клиенты отправляют заголовки и незавершённое тело до барьера, затем порциями передают остаток за 2 с. У direct второй барьер одновременно выпускает complete и нагружает проверку изображений API. Это closed-loop волны с pacing, не постоянный arrival rate и не поиск предельной производительности.
|
||||
|
||||
| Одновременных загрузок всего | Proxy RSS, MiB | Direct RSS, MiB |
|
||||
| --- | ---: | ---: |
|
||||
| 1 | 244,19–252,16 | 240,58–242,36 |
|
||||
| 5 | 301,10–303,25 | 264,11–273,75 |
|
||||
| 10 | 369,99–379,35 | 296,47–313,26 |
|
||||
|
||||
Все **128 загрузок и 128 скачиваний** прошли; ошибок, OOM и рестартов API внутри серии нет. При N=10 каждая API завершила по 10 загрузок за две волны. Direct снизил пик одной API на **10–12% при N=5**, **17–20% при N=10**, в обоих раундах. Максимальный прирост от прогретого baseline при N=10 — **149,93–150,64 MiB** у proxy против **71,76–84,69 MiB** у direct.
|
||||
|
||||
| N=10, раунды 1 / 2 | Proxy | Direct |
|
||||
| --- | ---: | ---: |
|
||||
| Успешных upload/с | 0,868 / 1,144 | 0,830 / 0,760 |
|
||||
| p50 полного времени, с | 11,082 / 8,257 | 11,782 / 12,860 |
|
||||
| p95, с | 13,762 / 10,847 | 12,011 / 13,849 |
|
||||
| p99, с | 13,806 / 10,851 | 12,075 / 13,949 |
|
||||
|
||||
|
||||
### Разбор расхода памяти
|
||||
|
||||
Дополнительно сняты 24 HTTP-upload по этапам и **48 вызовов реального валидатора** в 16 свежих процессах: Buffer/файл, обычный/выключенный кеш Sharp, два размера и два раунда. Принудительная GC и очистка кеша выполнялись только после измеряемой серии в отдельном процессе; в рабочем API их нет.
|
||||
|
||||
Для большого файла медиана прироста `arrayBuffers` при приёме proxy — **18,38 MiB**, при direct-чтении S3 — **9,04 MiB**. Основной скачок RSS на `Sharp.stats()` — около **9,88 / 9,75 MiB**, при почти неизменной JS-куче. В изолированном валидаторе с обычным кешем RSS — **162,64–167,71 MiB для Buffer** против **130,26–149,82 MiB для файла**. После диагностической GC буферы уменьшались с ~9,35 до 0,16 MiB; RSS даже после очистки кеша не всегда падала.
|
||||
|
||||
Поток уменьшает необходимость держать целые входные Buffer, но не устраняет декодирование и удержание выделенной памяти. Наблюдение само меняет тайминги: в профилированной HTTP-серии порядок RSS-пиков был противоположен последовательной серии без профиля. Экономия под нагрузкой подтверждается отдельным N=1/5/10 прогоном **без** внутреннего профилировщика.
|
||||
|
||||
### Отказы
|
||||
|
||||
| Воздействие | Ожидание | Наблюдение |
|
||||
| --- | --- | --- |
|
||||
| Остановка API | Запись восстановится ≤10 с, данные/сессия сохранятся | PASS для обеих: 4,206 / 4,127 с, 1 / 2 переходные ошибки, подтверждённые записи сохранены |
|
||||
| Полный restart | Те же данные и токен после рестарта всех хранилищ | PASS; StartedAt изменился у всех семи runtime, ID контейнеров сохранены |
|
||||
| Остановка RustFS | Readiness/upload/content — 503, metadata доступны | PASS; после восстановления оригинал скачивается побайтно |
|
||||
| Writer завершён после PUT до БД | Orphan будет обнаружен и безопасно удалён | PASS: dry-run не удаляет, apply удаляет, повтор безопасен |
|
||||
| БД недоступна при сверке | Ошибка не означает «объект ничей» | PASS, небезопасного удаления нет |
|
||||
| Пропали байты существующего Attachment | Диагностика без удаления метаданных | PASS, missing отражён в отчёте |
|
||||
| Обрыв / медленная передача | Нет неполного вложения, срок приёма ограничен | PASS на обеих API и Nginx, 408, следующий upload/download успешен |
|
||||
| Истёкшая/подменённая подпись | Не публиковать вложение | PASS: PUT отклонён, expired complete — 409, старый staging очищается |
|
||||
| Повтор PUT/complete после публикации | Оригинал не изменится | PASS: create-only PUT, идемпотентный complete, исходные байты сохранены |
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
Proxy буферизует до 10 MiB и не является сквозным streaming upload. Sharp расходует память декодированных пикселей. Grace не заменяет контроль писателей: прямые изменения S3/БД в обход блокировок не поддерживаются. Сбой между хранилищами может оставить объект до следующей безопасной сверки. История разрешений direct сохраняется; отдельная политика её хранения не реализована.
|
||||
|
||||
Серии локальные и короткие: два раунда, общие фоновые процессы/кеши, периодическое сэмплирование, генератор на том же хосте. Быстрые пики могли быть пропущены; это наблюдавшиеся значения, не верхняя граница RAM. Общая память RustFS/ОС/стенда не объявляется сэкономленной. Длительная нагрузка, большие чаты и другая машина этими сериями не проверены.
|
||||
|
||||
Итог исследования: direct примерно вдвое уменьшил трафик API на двух размерах. При последовательном upload преимущества RSS нет; при десяти одновременных больших upload пик одной API снизился на 17–20% в обоих раундах, все запросы успешны. Следующий шаг — больше повторов на выделенном хосте, независимый генератор и профиль нативных выделений декодера.
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
# Topology worksheet, deliberately NOT a runnable stack (no application supplied).
|
||||
# Implement compose.yaml or stack.yaml; do not submit this empty file as deployment.
|
||||
# Service names below are logical roles, not imposed container names.
|
||||
x-lab-number: 4
|
||||
x-required-roles:
|
||||
- api
|
||||
- postgres
|
||||
- load_balancer
|
||||
- s3
|
||||
# TODO: implement real images/builds, network wiring, volumes, healthchecks,
|
||||
# credentials and bootstrap. Details: contracts/infrastructure.json and README.md.
|
||||
services: {}
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
services:
|
||||
api:
|
||||
build:
|
||||
context: .
|
||||
target: runtime
|
||||
restart: unless-stopped
|
||||
init: true
|
||||
deploy:
|
||||
replicas: 2
|
||||
command: ['sh', '-c', 'INSTANCE_ID=$$(hostname) exec node dist/main.js']
|
||||
environment:
|
||||
NODE_ENV: production
|
||||
UPLOAD_MEMORY_PROFILE: ${UPLOAD_MEMORY_PROFILE:-0}
|
||||
APP_ENV: ${APP_ENV:-lab}
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: 8080
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-local_dev_password}@postgres:5432/${POSTGRES_DB:-chat}
|
||||
DB_POOL_MAX: ${DB_POOL_MAX:-15}
|
||||
CACHE_URL: redis://redis:6379/0
|
||||
CACHE_PREFIX: ${CACHE_PREFIX:-messenger:cache:v1}
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
S3_REGION: us-east-1
|
||||
S3_BUCKET: messenger-attachments
|
||||
S3_PUBLIC_ENDPOINT: ${S3_PUBLIC_ENDPOINT:-http://127.0.0.1:${S3_PUBLIC_PORT:-9000}}
|
||||
DIRECT_UPLOAD_TTL_SECONDS: ${DIRECT_UPLOAD_TTL_SECONDS:-60}
|
||||
S3_ACCESS_KEY_ID: ${S3_API_ACCESS_KEY_ID:-LABAPI}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_API_SECRET_ACCESS_KEY:-local_api_storage_password}
|
||||
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
|
||||
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
|
||||
UPLOAD_BODY_TIMEOUT_MS: ${UPLOAD_BODY_TIMEOUT_MS:-15000}
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
healthcheck:
|
||||
test:
|
||||
- CMD
|
||||
- curl
|
||||
- --fail
|
||||
- --silent
|
||||
- --show-error
|
||||
- --max-time
|
||||
- '2'
|
||||
- --output
|
||||
- /dev/null
|
||||
- http://127.0.0.1:8080/health/ready
|
||||
interval: 10s
|
||||
timeout: 3s
|
||||
start_period: 20s
|
||||
retries: 5
|
||||
stop_grace_period: 30s
|
||||
|
||||
reconciler:
|
||||
build:
|
||||
context: .
|
||||
target: runtime
|
||||
command: ['node', 'dist/Attachment/reconcile.cli.js', '--watch', '--apply']
|
||||
restart: unless-stopped
|
||||
init: true
|
||||
environment:
|
||||
NODE_ENV: production
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-local_dev_password}@postgres:5432/${POSTGRES_DB:-chat}
|
||||
DB_POOL_MAX: 2
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
S3_REGION: us-east-1
|
||||
S3_BUCKET: messenger-attachments
|
||||
S3_ACCESS_KEY_ID: ${S3_RECONCILER_ACCESS_KEY_ID:-LABRECONCILER}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_RECONCILER_SECRET_ACCESS_KEY:-local_reconciler_storage_password}
|
||||
ORPHAN_GRACE_SECONDS: ${ORPHAN_GRACE_SECONDS:-600}
|
||||
RECONCILE_INTERVAL_SECONDS: ${RECONCILE_INTERVAL_SECONDS:-60}
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test: ['CMD', 'node', 'dist/Attachment/reconcile-health.js']
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
start_period: 30s
|
||||
retries: 5
|
||||
stop_grace_period: 30s
|
||||
|
||||
nginx:
|
||||
image: nginx:1.30.6-alpine
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- '127.0.0.1:${APP_PORT:-8080}:80'
|
||||
- '127.0.0.1:${S3_PUBLIC_PORT:-9000}:9000'
|
||||
volumes:
|
||||
- ./infra/nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
- ./infra/start-nginx.sh:/etc/nginx/start-nginx.sh:ro
|
||||
entrypoint: ['/bin/sh', '/etc/nginx/start-nginx.sh']
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test: ['CMD', 'wget', '-q', '-T', '2', '-t', '1', '-O', '/dev/null', 'http://127.0.0.1/health/ready']
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
start_period: 10s
|
||||
retries: 5
|
||||
|
||||
migrate:
|
||||
build:
|
||||
context: .
|
||||
target: migrate
|
||||
environment:
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-local_dev_password}@postgres:5432/${POSTGRES_DB:-chat}
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
|
||||
postgres:
|
||||
image: postgres:17.6-bookworm
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
POSTGRES_DB: ${POSTGRES_DB:-chat}
|
||||
POSTGRES_USER: ${POSTGRES_USER:-app}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-local_dev_password}
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ['CMD-SHELL', 'pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"']
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
start_period: 10s
|
||||
retries: 10
|
||||
stop_grace_period: 30s
|
||||
|
||||
redis:
|
||||
image: redis:7.4.5-alpine
|
||||
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: 10
|
||||
stop_grace_period: 30s
|
||||
|
||||
s3:
|
||||
image: rustfs/rustfs:1.0.4
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
RUSTFS_ACCESS_KEY: ${S3_ROOT_ACCESS_KEY_ID:-${S3_ACCESS_KEY_ID:-LABSTORAGE}}
|
||||
RUSTFS_SECRET_KEY: ${S3_ROOT_SECRET_ACCESS_KEY:-${S3_SECRET_ACCESS_KEY:-local_app_storage_password}}
|
||||
RUSTFS_VOLUMES: /data
|
||||
RUSTFS_ADDRESS: 0.0.0.0:9000
|
||||
RUSTFS_CONSOLE_ENABLE: 'false'
|
||||
volumes:
|
||||
- s3_data:/data
|
||||
healthcheck:
|
||||
test: ['CMD', 'curl', '--fail', '--silent', 'http://127.0.0.1:9000/health']
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
start_period: 30s
|
||||
|
||||
s3-init:
|
||||
build:
|
||||
context: .
|
||||
target: storage-init
|
||||
environment:
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
S3_REGION: us-east-1
|
||||
S3_BUCKET: messenger-attachments
|
||||
S3_ACCESS_KEY_ID: ${S3_ROOT_ACCESS_KEY_ID:-${S3_ACCESS_KEY_ID:-LABSTORAGE}}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_ROOT_SECRET_ACCESS_KEY:-${S3_SECRET_ACCESS_KEY:-local_app_storage_password}}
|
||||
S3_API_ACCESS_KEY_ID: ${S3_API_ACCESS_KEY_ID:-LABAPI}
|
||||
S3_API_SECRET_ACCESS_KEY: ${S3_API_SECRET_ACCESS_KEY:-local_api_storage_password}
|
||||
S3_RECONCILER_ACCESS_KEY_ID: ${S3_RECONCILER_ACCESS_KEY_ID:-LABRECONCILER}
|
||||
S3_RECONCILER_SECRET_ACCESS_KEY: ${S3_RECONCILER_SECRET_ACCESS_KEY:-local_reconciler_storage_password}
|
||||
depends_on:
|
||||
s3:
|
||||
condition: service_healthy
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
redis_data:
|
||||
s3_data:
|
||||
@@ -0,0 +1,491 @@
|
||||
{
|
||||
"status": "passed",
|
||||
"repeats": 3,
|
||||
"rounds": 2,
|
||||
"restartApiBeforeCase": true,
|
||||
"warmupUploadsPerCase": 4,
|
||||
"cases": [
|
||||
{
|
||||
"round": 1,
|
||||
"mode": "proxy",
|
||||
"sidePixels": 512,
|
||||
"fileBytes": 787247,
|
||||
"apiStatesAfterRestart": [
|
||||
"healthy 2026-11-11T17:33:36.551142187Z",
|
||||
"healthy 2026-11-11T17:33:36.531080027Z"
|
||||
],
|
||||
"warmupInstances": [
|
||||
"25bc3a55a217",
|
||||
"f44d705c0959"
|
||||
],
|
||||
"successfulUploads": 3,
|
||||
"elapsedSeconds": 0.4835730000049807,
|
||||
"memory": [
|
||||
{
|
||||
"instance": "f44d705c0959",
|
||||
"event": "summary",
|
||||
"pid": 8,
|
||||
"baselineRssBytes": 197386240,
|
||||
"sampledPeakRssBytes": 199352320,
|
||||
"finalRssBytes": 199352320,
|
||||
"baselineCgroupBytes": 148238336,
|
||||
"sampledPeakCgroupBytes": 150237184,
|
||||
"finalCgroupBytes": 150036480,
|
||||
"lifetimeHwmBeforeBytes": 197386240,
|
||||
"lifetimeHwmAfterBytes": 199352320,
|
||||
"oomDelta": 0,
|
||||
"oomKillDelta": 0,
|
||||
"networkRxBytes": 1596189,
|
||||
"networkTxBytes": 1600897,
|
||||
"samples": 10,
|
||||
"maxSampleGapMs": 51.437545999999884,
|
||||
"durationMs": 451.85021800000004
|
||||
},
|
||||
{
|
||||
"instance": "25bc3a55a217",
|
||||
"event": "summary",
|
||||
"pid": 7,
|
||||
"baselineRssBytes": 197595136,
|
||||
"sampledPeakRssBytes": 199168000,
|
||||
"finalRssBytes": 199168000,
|
||||
"baselineCgroupBytes": 148316160,
|
||||
"sampledPeakCgroupBytes": 150261760,
|
||||
"finalCgroupBytes": 150056960,
|
||||
"lifetimeHwmBeforeBytes": 197595136,
|
||||
"lifetimeHwmAfterBytes": 199168000,
|
||||
"oomDelta": 0,
|
||||
"oomKillDelta": 0,
|
||||
"networkRxBytes": 800027,
|
||||
"networkTxBytes": 796656,
|
||||
"samples": 10,
|
||||
"maxSampleGapMs": 51.34110900000019,
|
||||
"durationMs": 452.10706600000003
|
||||
}
|
||||
],
|
||||
"apiRxBytes": 2396216,
|
||||
"apiTxBytes": 2397553
|
||||
},
|
||||
{
|
||||
"round": 1,
|
||||
"mode": "direct",
|
||||
"sidePixels": 512,
|
||||
"fileBytes": 787247,
|
||||
"apiStatesAfterRestart": [
|
||||
"healthy 2026-11-11T17:34:04.643158493Z",
|
||||
"healthy 2026-11-11T17:34:04.540637899Z"
|
||||
],
|
||||
"warmupInstances": [
|
||||
"25bc3a55a217",
|
||||
"f44d705c0959"
|
||||
],
|
||||
"successfulUploads": 3,
|
||||
"elapsedSeconds": 2.872334599989699,
|
||||
"memory": [
|
||||
{
|
||||
"instance": "f44d705c0959",
|
||||
"event": "summary",
|
||||
"pid": 6,
|
||||
"baselineRssBytes": 201015296,
|
||||
"sampledPeakRssBytes": 205471744,
|
||||
"finalRssBytes": 205471744,
|
||||
"baselineCgroupBytes": 151277568,
|
||||
"sampledPeakCgroupBytes": 157130752,
|
||||
"finalCgroupBytes": 156672000,
|
||||
"lifetimeHwmBeforeBytes": 201015296,
|
||||
"lifetimeHwmAfterBytes": 205471744,
|
||||
"oomDelta": 0,
|
||||
"oomKillDelta": 0,
|
||||
"networkRxBytes": 2385418,
|
||||
"networkTxBytes": 35155,
|
||||
"samples": 52,
|
||||
"maxSampleGapMs": 98.02254300000004,
|
||||
"durationMs": 2643.529019
|
||||
},
|
||||
{
|
||||
"instance": "25bc3a55a217",
|
||||
"event": "summary",
|
||||
"pid": 6,
|
||||
"baselineRssBytes": 200581120,
|
||||
"sampledPeakRssBytes": 200712192,
|
||||
"finalRssBytes": 200712192,
|
||||
"baselineCgroupBytes": 151232512,
|
||||
"sampledPeakCgroupBytes": 152133632,
|
||||
"finalCgroupBytes": 152080384,
|
||||
"lifetimeHwmBeforeBytes": 200581120,
|
||||
"lifetimeHwmAfterBytes": 200712192,
|
||||
"oomDelta": 0,
|
||||
"oomKillDelta": 0,
|
||||
"networkRxBytes": 8425,
|
||||
"networkTxBytes": 13670,
|
||||
"samples": 51,
|
||||
"maxSampleGapMs": 105.99864699999989,
|
||||
"durationMs": 2640.8270039999998
|
||||
}
|
||||
],
|
||||
"apiRxBytes": 2393843,
|
||||
"apiTxBytes": 48825
|
||||
},
|
||||
{
|
||||
"round": 1,
|
||||
"mode": "proxy",
|
||||
"sidePixels": 1792,
|
||||
"fileBytes": 9638577,
|
||||
"apiStatesAfterRestart": [
|
||||
"healthy 2026-11-11T17:34:32.616460164Z",
|
||||
"healthy 2026-11-11T17:34:32.511421194Z"
|
||||
],
|
||||
"warmupInstances": [
|
||||
"25bc3a55a217",
|
||||
"f44d705c0959"
|
||||
],
|
||||
"successfulUploads": 3,
|
||||
"elapsedSeconds": 3.1178796000021975,
|
||||
"memory": [
|
||||
{
|
||||
"instance": "f44d705c0959",
|
||||
"event": "summary",
|
||||
"pid": 7,
|
||||
"baselineRssBytes": 229691392,
|
||||
"sampledPeakRssBytes": 242667520,
|
||||
"finalRssBytes": 242667520,
|
||||
"baselineCgroupBytes": 180113408,
|
||||
"sampledPeakCgroupBytes": 195690496,
|
||||
"finalCgroupBytes": 194109440,
|
||||
"lifetimeHwmBeforeBytes": 238940160,
|
||||
"lifetimeHwmAfterBytes": 242667520,
|
||||
"oomDelta": 0,
|
||||
"oomKillDelta": 0,
|
||||
"networkRxBytes": 9722827,
|
||||
"networkTxBytes": 9678622,
|
||||
"samples": 57,
|
||||
"maxSampleGapMs": 66.09682399999838,
|
||||
"durationMs": 2866.474058
|
||||
},
|
||||
{
|
||||
"instance": "25bc3a55a217",
|
||||
"event": "summary",
|
||||
"pid": 7,
|
||||
"baselineRssBytes": 231923712,
|
||||
"sampledPeakRssBytes": 244973568,
|
||||
"finalRssBytes": 244973568,
|
||||
"baselineCgroupBytes": 182202368,
|
||||
"sampledPeakCgroupBytes": 198656000,
|
||||
"finalCgroupBytes": 196403200,
|
||||
"lifetimeHwmBeforeBytes": 240717824,
|
||||
"lifetimeHwmAfterBytes": 244973568,
|
||||
"oomDelta": 0,
|
||||
"oomKillDelta": 0,
|
||||
"networkRxBytes": 19453421,
|
||||
"networkTxBytes": 19351973,
|
||||
"samples": 56,
|
||||
"maxSampleGapMs": 69.09787999999935,
|
||||
"durationMs": 2864.6600660000004
|
||||
}
|
||||
],
|
||||
"apiRxBytes": 29176248,
|
||||
"apiTxBytes": 29030595
|
||||
},
|
||||
{
|
||||
"round": 1,
|
||||
"mode": "direct",
|
||||
"sidePixels": 1792,
|
||||
"fileBytes": 9638577,
|
||||
"apiStatesAfterRestart": [
|
||||
"healthy 2026-11-11T17:35:08.126491359Z",
|
||||
"healthy 2026-11-11T17:35:08.194727176Z"
|
||||
],
|
||||
"warmupInstances": [
|
||||
"25bc3a55a217",
|
||||
"f44d705c0959"
|
||||
],
|
||||
"successfulUploads": 3,
|
||||
"elapsedSeconds": 3.538487499987241,
|
||||
"memory": [
|
||||
{
|
||||
"instance": "f44d705c0959",
|
||||
"event": "summary",
|
||||
"pid": 6,
|
||||
"baselineRssBytes": 243318784,
|
||||
"sampledPeakRssBytes": 243318784,
|
||||
"finalRssBytes": 243318784,
|
||||
"baselineCgroupBytes": 194019328,
|
||||
"sampledPeakCgroupBytes": 195526656,
|
||||
"finalCgroupBytes": 195526656,
|
||||
"lifetimeHwmBeforeBytes": 252203008,
|
||||
"lifetimeHwmAfterBytes": 252203008,
|
||||
"oomDelta": 0,
|
||||
"oomKillDelta": 0,
|
||||
"networkRxBytes": 7683,
|
||||
"networkTxBytes": 12520,
|
||||
"samples": 66,
|
||||
"maxSampleGapMs": 56.471386999999595,
|
||||
"durationMs": 3257.1643679999997
|
||||
},
|
||||
{
|
||||
"instance": "25bc3a55a217",
|
||||
"event": "summary",
|
||||
"pid": 6,
|
||||
"baselineRssBytes": 241340416,
|
||||
"sampledPeakRssBytes": 257572864,
|
||||
"finalRssBytes": 257572864,
|
||||
"baselineCgroupBytes": 192012288,
|
||||
"sampledPeakCgroupBytes": 219344896,
|
||||
"finalCgroupBytes": 209281024,
|
||||
"lifetimeHwmBeforeBytes": 250687488,
|
||||
"lifetimeHwmAfterBytes": 257572864,
|
||||
"oomDelta": 0,
|
||||
"oomKillDelta": 0,
|
||||
"networkRxBytes": 28969512,
|
||||
"networkTxBytes": 41773,
|
||||
"samples": 65,
|
||||
"maxSampleGapMs": 53.53805800000009,
|
||||
"durationMs": 3254.3833879999993
|
||||
}
|
||||
],
|
||||
"apiRxBytes": 28977195,
|
||||
"apiTxBytes": 54293
|
||||
},
|
||||
{
|
||||
"round": 2,
|
||||
"mode": "direct",
|
||||
"sidePixels": 512,
|
||||
"fileBytes": 787247,
|
||||
"apiStatesAfterRestart": [
|
||||
"healthy 2026-11-11T17:35:46.63537715Z",
|
||||
"healthy 2026-11-11T17:35:46.678041266Z"
|
||||
],
|
||||
"warmupInstances": [
|
||||
"25bc3a55a217",
|
||||
"f44d705c0959"
|
||||
],
|
||||
"successfulUploads": 3,
|
||||
"elapsedSeconds": 1.5461034000036307,
|
||||
"memory": [
|
||||
{
|
||||
"instance": "f44d705c0959",
|
||||
"event": "summary",
|
||||
"pid": 7,
|
||||
"baselineRssBytes": 203808768,
|
||||
"sampledPeakRssBytes": 203939840,
|
||||
"finalRssBytes": 203939840,
|
||||
"baselineCgroupBytes": 154058752,
|
||||
"sampledPeakCgroupBytes": 155017216,
|
||||
"finalCgroupBytes": 154923008,
|
||||
"lifetimeHwmBeforeBytes": 203808768,
|
||||
"lifetimeHwmAfterBytes": 203939840,
|
||||
"oomDelta": 0,
|
||||
"oomKillDelta": 0,
|
||||
"networkRxBytes": 7282,
|
||||
"networkTxBytes": 12118,
|
||||
"samples": 29,
|
||||
"maxSampleGapMs": 82.84940600000027,
|
||||
"durationMs": 1436.521577
|
||||
},
|
||||
{
|
||||
"instance": "25bc3a55a217",
|
||||
"event": "summary",
|
||||
"pid": 6,
|
||||
"baselineRssBytes": 199536640,
|
||||
"sampledPeakRssBytes": 207794176,
|
||||
"finalRssBytes": 207794176,
|
||||
"baselineCgroupBytes": 149803008,
|
||||
"sampledPeakCgroupBytes": 159301632,
|
||||
"finalCgroupBytes": 159170560,
|
||||
"lifetimeHwmBeforeBytes": 199536640,
|
||||
"lifetimeHwmAfterBytes": 207794176,
|
||||
"oomDelta": 0,
|
||||
"oomKillDelta": 0,
|
||||
"networkRxBytes": 2384820,
|
||||
"networkTxBytes": 33967,
|
||||
"samples": 30,
|
||||
"maxSampleGapMs": 65.22268299999996,
|
||||
"durationMs": 1425.3379689999997
|
||||
}
|
||||
],
|
||||
"apiRxBytes": 2392102,
|
||||
"apiTxBytes": 46085
|
||||
},
|
||||
{
|
||||
"round": 2,
|
||||
"mode": "proxy",
|
||||
"sidePixels": 512,
|
||||
"fileBytes": 787247,
|
||||
"apiStatesAfterRestart": [
|
||||
"healthy 2026-11-11T17:36:14.736162695Z",
|
||||
"healthy 2026-11-11T17:36:14.760181065Z"
|
||||
],
|
||||
"warmupInstances": [
|
||||
"25bc3a55a217",
|
||||
"f44d705c0959"
|
||||
],
|
||||
"successfulUploads": 3,
|
||||
"elapsedSeconds": 0.42918659999850206,
|
||||
"memory": [
|
||||
{
|
||||
"instance": "f44d705c0959",
|
||||
"event": "summary",
|
||||
"pid": 7,
|
||||
"baselineRssBytes": 200491008,
|
||||
"sampledPeakRssBytes": 201932800,
|
||||
"finalRssBytes": 201932800,
|
||||
"baselineCgroupBytes": 150675456,
|
||||
"sampledPeakCgroupBytes": 152555520,
|
||||
"finalCgroupBytes": 152461312,
|
||||
"lifetimeHwmBeforeBytes": 200491008,
|
||||
"lifetimeHwmAfterBytes": 201932800,
|
||||
"oomDelta": 0,
|
||||
"oomKillDelta": 0,
|
||||
"networkRxBytes": 1594936,
|
||||
"networkTxBytes": 1589334,
|
||||
"samples": 9,
|
||||
"maxSampleGapMs": 52.79885599999989,
|
||||
"durationMs": 400.85065099999997
|
||||
},
|
||||
{
|
||||
"instance": "25bc3a55a217",
|
||||
"event": "summary",
|
||||
"pid": 7,
|
||||
"baselineRssBytes": 200245248,
|
||||
"sampledPeakRssBytes": 200507392,
|
||||
"finalRssBytes": 200507392,
|
||||
"baselineCgroupBytes": 150880256,
|
||||
"sampledPeakCgroupBytes": 151240704,
|
||||
"finalCgroupBytes": 151207936,
|
||||
"lifetimeHwmBeforeBytes": 200245248,
|
||||
"lifetimeHwmAfterBytes": 200507392,
|
||||
"oomDelta": 0,
|
||||
"oomKillDelta": 0,
|
||||
"networkRxBytes": 797831,
|
||||
"networkTxBytes": 794535,
|
||||
"samples": 9,
|
||||
"maxSampleGapMs": 50.77773900000011,
|
||||
"durationMs": 400.2487940000001
|
||||
}
|
||||
],
|
||||
"apiRxBytes": 2392767,
|
||||
"apiTxBytes": 2383869
|
||||
},
|
||||
{
|
||||
"round": 2,
|
||||
"mode": "direct",
|
||||
"sidePixels": 1792,
|
||||
"fileBytes": 9638577,
|
||||
"apiStatesAfterRestart": [
|
||||
"healthy 2026-11-11T17:36:40.239402109Z",
|
||||
"healthy 2026-11-11T17:36:40.210874393Z"
|
||||
],
|
||||
"warmupInstances": [
|
||||
"25bc3a55a217",
|
||||
"f44d705c0959"
|
||||
],
|
||||
"successfulUploads": 3,
|
||||
"elapsedSeconds": 4.982343700015917,
|
||||
"memory": [
|
||||
{
|
||||
"instance": "f44d705c0959",
|
||||
"event": "summary",
|
||||
"pid": 7,
|
||||
"baselineRssBytes": 223092736,
|
||||
"sampledPeakRssBytes": 223092736,
|
||||
"finalRssBytes": 220209152,
|
||||
"baselineCgroupBytes": 174321664,
|
||||
"sampledPeakCgroupBytes": 176312320,
|
||||
"finalCgroupBytes": 176001024,
|
||||
"lifetimeHwmBeforeBytes": 238923776,
|
||||
"lifetimeHwmAfterBytes": 238923776,
|
||||
"oomDelta": 0,
|
||||
"oomKillDelta": 0,
|
||||
"networkRxBytes": 9356,
|
||||
"networkTxBytes": 15273,
|
||||
"samples": 91,
|
||||
"maxSampleGapMs": 57.82966000000124,
|
||||
"durationMs": 4577.512455
|
||||
},
|
||||
{
|
||||
"instance": "25bc3a55a217",
|
||||
"event": "summary",
|
||||
"pid": 7,
|
||||
"baselineRssBytes": 244051968,
|
||||
"sampledPeakRssBytes": 260890624,
|
||||
"finalRssBytes": 260759552,
|
||||
"baselineCgroupBytes": 197177344,
|
||||
"sampledPeakCgroupBytes": 228503552,
|
||||
"finalCgroupBytes": 218468352,
|
||||
"lifetimeHwmBeforeBytes": 253472768,
|
||||
"lifetimeHwmAfterBytes": 260890624,
|
||||
"oomDelta": 0,
|
||||
"oomKillDelta": 0,
|
||||
"networkRxBytes": 28999470,
|
||||
"networkTxBytes": 73576,
|
||||
"samples": 91,
|
||||
"maxSampleGapMs": 68.75278199999957,
|
||||
"durationMs": 4573.949913
|
||||
}
|
||||
],
|
||||
"apiRxBytes": 29008826,
|
||||
"apiTxBytes": 88849
|
||||
},
|
||||
{
|
||||
"round": 2,
|
||||
"mode": "proxy",
|
||||
"sidePixels": 1792,
|
||||
"fileBytes": 9638577,
|
||||
"apiStatesAfterRestart": [
|
||||
"healthy 2026-11-11T17:37:17.834748093Z",
|
||||
"healthy 2026-11-11T17:37:17.891433327Z"
|
||||
],
|
||||
"warmupInstances": [
|
||||
"25bc3a55a217",
|
||||
"f44d705c0959"
|
||||
],
|
||||
"successfulUploads": 3,
|
||||
"elapsedSeconds": 5.799873500014655,
|
||||
"memory": [
|
||||
{
|
||||
"instance": "f44d705c0959",
|
||||
"event": "summary",
|
||||
"pid": 6,
|
||||
"baselineRssBytes": 222253056,
|
||||
"sampledPeakRssBytes": 249942016,
|
||||
"finalRssBytes": 249679872,
|
||||
"baselineCgroupBytes": 179466240,
|
||||
"sampledPeakCgroupBytes": 211116032,
|
||||
"finalCgroupBytes": 211079168,
|
||||
"lifetimeHwmBeforeBytes": 245264384,
|
||||
"lifetimeHwmAfterBytes": 249942016,
|
||||
"oomDelta": 0,
|
||||
"oomKillDelta": 0,
|
||||
"networkRxBytes": 19638145,
|
||||
"networkTxBytes": 19529049,
|
||||
"samples": 104,
|
||||
"maxSampleGapMs": 79.66721099999995,
|
||||
"durationMs": 5368.300846
|
||||
},
|
||||
{
|
||||
"instance": "25bc3a55a217",
|
||||
"event": "summary",
|
||||
"pid": 7,
|
||||
"baselineRssBytes": 240148480,
|
||||
"sampledPeakRssBytes": 249978880,
|
||||
"finalRssBytes": 248406016,
|
||||
"baselineCgroupBytes": 196096000,
|
||||
"sampledPeakCgroupBytes": 208592896,
|
||||
"finalCgroupBytes": 208531456,
|
||||
"lifetimeHwmBeforeBytes": 240148480,
|
||||
"lifetimeHwmAfterBytes": 249978880,
|
||||
"oomDelta": 0,
|
||||
"oomKillDelta": 0,
|
||||
"networkRxBytes": 9723561,
|
||||
"networkTxBytes": 9683497,
|
||||
"samples": 105,
|
||||
"maxSampleGapMs": 76.43506200000047,
|
||||
"durationMs": 5396.637092999999
|
||||
}
|
||||
],
|
||||
"apiRxBytes": 29361706,
|
||||
"apiTxBytes": 29212546
|
||||
}
|
||||
],
|
||||
"method": "One upload at a time; measured direct flow includes grant, PUT, and complete with full image validation. Download verification is outside the measurement window. RSS sampling 50 ms; network counters sum non-loopback API interfaces, including DB/S3/background health traffic. Probe RSS excluded.",
|
||||
"fixturesRemoved": true
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"status": "passed",
|
||||
"checks": [
|
||||
"grant ACL, strict input validation, owner-only complete and missing-upload handling",
|
||||
"unsigned/tampered requests denied; wrong payload checksum rejected by RustFS",
|
||||
"create-only PUT, publication, byte integrity, message binding and replay-safe immutable original",
|
||||
"JPEG publication preserves the exact original bytes",
|
||||
"simultaneous complete publishes one stable attachment across replicas",
|
||||
"corrupt image and MIME spoof rejected without ready metadata",
|
||||
"40-million-pixel limit enforced before full decoding",
|
||||
"real URL expiry, expired-intent cleanup and published-object protection",
|
||||
"mandatory proxy upload remains compatible"
|
||||
],
|
||||
"fixturesRemoved": true
|
||||
}
|
||||
@@ -0,0 +1,394 @@
|
||||
{
|
||||
"status": "passed",
|
||||
"startedAt": "2026-11-11T21:03:10.320137+00:00",
|
||||
"sourceSha256": "12af1c58afaa9c1600a9c95edbb083757447ee1d2ecca11948f6968ad80c3c8c",
|
||||
"steps": [
|
||||
{
|
||||
"name": "offline_contract",
|
||||
"command": [
|
||||
"C:\\Program Files\\Python313\\python.exe",
|
||||
"-X",
|
||||
"utf8",
|
||||
"-u",
|
||||
"scripts/check_contract.py"
|
||||
],
|
||||
"status": "passed",
|
||||
"output": [
|
||||
"PASS: offline skeleton structure, local refs and Python syntax; application NOT tested."
|
||||
],
|
||||
"exitCode": 0,
|
||||
"elapsedSeconds": 16.179012099979445
|
||||
},
|
||||
{
|
||||
"name": "unit",
|
||||
"command": [
|
||||
"cmd.exe",
|
||||
"/d",
|
||||
"/c",
|
||||
"pnpm.cmd",
|
||||
"test",
|
||||
"--runInBand"
|
||||
],
|
||||
"status": "passed",
|
||||
"output": [
|
||||
"Test Suites: 9 passed, 9 total",
|
||||
"Tests: 47 passed, 47 total"
|
||||
],
|
||||
"exitCode": 0,
|
||||
"elapsedSeconds": 32.84614499998861
|
||||
},
|
||||
{
|
||||
"name": "lint",
|
||||
"command": [
|
||||
"cmd.exe",
|
||||
"/d",
|
||||
"/c",
|
||||
"pnpm.cmd",
|
||||
"lint"
|
||||
],
|
||||
"status": "passed",
|
||||
"output": [],
|
||||
"exitCode": 0,
|
||||
"elapsedSeconds": 7.732036200002767
|
||||
},
|
||||
{
|
||||
"name": "public_smoke_before",
|
||||
"command": [
|
||||
"C:\\Program Files\\Python313\\python.exe",
|
||||
"-X",
|
||||
"utf8",
|
||||
"-u",
|
||||
"tests/smoke.py",
|
||||
"--base-url",
|
||||
"http://127.0.0.1:8080"
|
||||
],
|
||||
"status": "passed",
|
||||
"output": [
|
||||
"PASS: lab4 public API scenario; infrastructure and grading evidence remain separate."
|
||||
],
|
||||
"exitCode": 0,
|
||||
"elapsedSeconds": 4.509277600009227
|
||||
},
|
||||
{
|
||||
"name": "oversized_upload_transport",
|
||||
"command": [
|
||||
"C:\\Program Files\\Python313\\python.exe",
|
||||
"-X",
|
||||
"utf8",
|
||||
"-u",
|
||||
"test/oversized-uploads.py"
|
||||
],
|
||||
"status": "passed",
|
||||
"output": [
|
||||
"PASS: 16 oversized Content-Length/chunked uploads through Nginx returned canonical 413; both APIs covered; subsequent upload/download verified"
|
||||
],
|
||||
"exitCode": 0,
|
||||
"elapsedSeconds": 15.628907100006472
|
||||
},
|
||||
{
|
||||
"name": "attachments_s3_outage_persistence",
|
||||
"command": [
|
||||
"C:\\Program Files\\Python313\\python.exe",
|
||||
"-X",
|
||||
"utf8",
|
||||
"-u",
|
||||
"test/attachments.py",
|
||||
"--base-url",
|
||||
"http://127.0.0.1:8080",
|
||||
"--lifecycle"
|
||||
],
|
||||
"status": "passed",
|
||||
"output": [
|
||||
"PASS: attachments, validation, ACL, idempotency, private S3, outage and persistence"
|
||||
],
|
||||
"exitCode": 0,
|
||||
"elapsedSeconds": 92.00613980001071
|
||||
},
|
||||
{
|
||||
"name": "reconciliation_crash_and_database_outage",
|
||||
"command": [
|
||||
"C:\\Program Files\\Python313\\python.exe",
|
||||
"-X",
|
||||
"utf8",
|
||||
"-u",
|
||||
"test/run-reconcile.py"
|
||||
],
|
||||
"status": "passed",
|
||||
"output": [
|
||||
"PASS: real writer crash, DB outage, dry-run, orphan deletion, active/fresh/referenced protection, repeat run and missing-object reporting."
|
||||
],
|
||||
"exitCode": 0,
|
||||
"elapsedSeconds": 14.79472619999433
|
||||
},
|
||||
{
|
||||
"name": "public_persistence_and_each_api_failover",
|
||||
"command": [
|
||||
"C:\\Program Files\\Python313\\python.exe",
|
||||
"-X",
|
||||
"utf8",
|
||||
"-u",
|
||||
"test/hooks.py"
|
||||
],
|
||||
"status": "passed",
|
||||
"output": [
|
||||
"PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.",
|
||||
"PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.",
|
||||
"PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.",
|
||||
"PASS: public persistence/failover hooks; all stores restarted; each API stopped separately; replicas restored"
|
||||
],
|
||||
"exitCode": 0,
|
||||
"elapsedSeconds": 123.80009560001781
|
||||
},
|
||||
{
|
||||
"name": "swagger_and_attachment_failover",
|
||||
"command": [
|
||||
"C:\\Program Files\\Python313\\python.exe",
|
||||
"-X",
|
||||
"utf8",
|
||||
"-u",
|
||||
"test/replicas.py",
|
||||
"--base-url",
|
||||
"http://127.0.0.1:8080"
|
||||
],
|
||||
"status": "passed",
|
||||
"output": [
|
||||
"PASS: two replicas, canonical Swagger and attachment/idempotency failover."
|
||||
],
|
||||
"exitCode": 0,
|
||||
"elapsedSeconds": 17.544774399982998
|
||||
},
|
||||
{
|
||||
"name": "interrupted_upload_and_receive_deadline",
|
||||
"command": [
|
||||
"C:\\Program Files\\Python313\\python.exe",
|
||||
"-X",
|
||||
"utf8",
|
||||
"-u",
|
||||
"test/upload-interruption.py"
|
||||
],
|
||||
"status": "passed",
|
||||
"output": [
|
||||
"PASS: ce0434d224ba: both client aborts leave no bytes/metadata; slow upload gets 408; next upload/download succeeds",
|
||||
"PASS: 752f50d7b664: both client aborts leave no bytes/metadata; slow upload gets 408; next upload/download succeeds",
|
||||
"PASS: nginx: both client aborts leave no bytes/metadata; slow upload gets 408; next upload/download succeeds",
|
||||
"PASS: upload interruption and receive deadline; targets=all; fixtures removed"
|
||||
],
|
||||
"exitCode": 0,
|
||||
"elapsedSeconds": 58.85686539998278
|
||||
},
|
||||
{
|
||||
"name": "presigned_positive_and_negative",
|
||||
"command": [
|
||||
"C:\\Program Files\\Python313\\python.exe",
|
||||
"-X",
|
||||
"utf8",
|
||||
"-u",
|
||||
"test/direct-uploads.py"
|
||||
],
|
||||
"status": "passed",
|
||||
"output": [
|
||||
"PASS: grant ACL, strict input validation, owner-only complete and missing-upload handling",
|
||||
"PASS: unsigned/tampered requests denied; wrong payload checksum rejected by RustFS",
|
||||
"PASS: create-only PUT, publication, byte integrity, message binding and replay-safe immutable original",
|
||||
"PASS: JPEG publication preserves the exact original bytes",
|
||||
"PASS: simultaneous complete publishes one stable attachment across replicas",
|
||||
"PASS: corrupt image and MIME spoof rejected without ready metadata",
|
||||
"PASS: 40-million-pixel limit enforced before full decoding",
|
||||
"PASS: real URL expiry, expired-intent cleanup and published-object protection",
|
||||
"PASS: mandatory proxy upload remains compatible"
|
||||
],
|
||||
"exitCode": 0,
|
||||
"elapsedSeconds": 14.73761030001333
|
||||
},
|
||||
{
|
||||
"name": "s3_restricted_roles",
|
||||
"command": [
|
||||
"docker",
|
||||
"compose",
|
||||
"run",
|
||||
"--rm",
|
||||
"--no-deps",
|
||||
"-T",
|
||||
"-v",
|
||||
"C:\\Users\\olpin\\Projects\\mtusi2026lab4\\test:/app/test:ro",
|
||||
"--entrypoint",
|
||||
"node",
|
||||
"s3-init",
|
||||
"test/s3-permissions.mjs"
|
||||
],
|
||||
"status": "passed",
|
||||
"output": [
|
||||
"PASS: api: object read and readiness allowed; other prefixes/buckets, bucket policy and admin denied",
|
||||
"PASS: reconciler: object read and readiness allowed; other prefixes/buckets, bucket policy and admin denied",
|
||||
"PASS: API CRUD without listing; reconciler scoped listing/HEAD/delete without PUT; anonymous access denied"
|
||||
],
|
||||
"exitCode": 0,
|
||||
"elapsedSeconds": 4.188357200007886
|
||||
},
|
||||
{
|
||||
"name": "s3_conditional_write_checksum_copy",
|
||||
"command": [
|
||||
"docker",
|
||||
"compose",
|
||||
"run",
|
||||
"--rm",
|
||||
"--no-deps",
|
||||
"-T",
|
||||
"-v",
|
||||
"C:\\Users\\olpin\\Projects\\mtusi2026lab4\\test:/app/test:ro",
|
||||
"--entrypoint",
|
||||
"node",
|
||||
"s3-init",
|
||||
"test/s3-direct-capabilities.mjs"
|
||||
],
|
||||
"status": "passed",
|
||||
"output": [
|
||||
"PASS: RustFS 1.0.4 enforces If-None-Match, SHA-256 and conditional server-side copy"
|
||||
],
|
||||
"exitCode": 0,
|
||||
"elapsedSeconds": 3.997638200002257
|
||||
},
|
||||
{
|
||||
"name": "public_smoke_after",
|
||||
"command": [
|
||||
"C:\\Program Files\\Python313\\python.exe",
|
||||
"-X",
|
||||
"utf8",
|
||||
"-u",
|
||||
"tests/smoke.py",
|
||||
"--base-url",
|
||||
"http://127.0.0.1:8080"
|
||||
],
|
||||
"status": "passed",
|
||||
"output": [
|
||||
"PASS: lab4 public API scenario; infrastructure and grading evidence remain separate."
|
||||
],
|
||||
"exitCode": 0,
|
||||
"elapsedSeconds": 2.654254299995955
|
||||
}
|
||||
],
|
||||
"scope": "Existing suites, sequential execution, no load benchmark rerun. IPv4 public endpoint. All runtime services restored without deleting volumes.",
|
||||
"before": [
|
||||
{
|
||||
"service": "api",
|
||||
"name": "mtusi2026lab4-api-1",
|
||||
"id": "ce0434d224ba",
|
||||
"state": "running",
|
||||
"health": "healthy",
|
||||
"image": "mtusi2026lab4-api"
|
||||
},
|
||||
{
|
||||
"service": "api",
|
||||
"name": "mtusi2026lab4-api-2",
|
||||
"id": "752f50d7b664",
|
||||
"state": "running",
|
||||
"health": "healthy",
|
||||
"image": "mtusi2026lab4-api"
|
||||
},
|
||||
{
|
||||
"service": "nginx",
|
||||
"name": "mtusi2026lab4-nginx-1",
|
||||
"id": "dd8840a93c5b",
|
||||
"state": "running",
|
||||
"health": "healthy",
|
||||
"image": "nginx:1.30.6-alpine"
|
||||
},
|
||||
{
|
||||
"service": "postgres",
|
||||
"name": "mtusi2026lab4-postgres-1",
|
||||
"id": "14c8623492ba",
|
||||
"state": "running",
|
||||
"health": "healthy",
|
||||
"image": "postgres:17.6-bookworm"
|
||||
},
|
||||
{
|
||||
"service": "reconciler",
|
||||
"name": "mtusi2026lab4-reconciler-1",
|
||||
"id": "4058a1f65b0b",
|
||||
"state": "running",
|
||||
"health": "healthy",
|
||||
"image": "mtusi2026lab4-reconciler"
|
||||
},
|
||||
{
|
||||
"service": "redis",
|
||||
"name": "mtusi2026lab4-redis-1",
|
||||
"id": "b521d405535f",
|
||||
"state": "running",
|
||||
"health": "healthy",
|
||||
"image": "redis:7.4.5-alpine"
|
||||
},
|
||||
{
|
||||
"service": "s3",
|
||||
"name": "mtusi2026lab4-s3-1",
|
||||
"id": "ef6e12a25440",
|
||||
"state": "running",
|
||||
"health": "healthy",
|
||||
"image": "rustfs/rustfs:1.0.4"
|
||||
}
|
||||
],
|
||||
"apiImages": [
|
||||
"ce0434d224ba sha256:dc12a9dcee0a6f047ac0bea6e12155687ecd831b36e029976ca221f7cacd3c99",
|
||||
"752f50d7b664 sha256:dc12a9dcee0a6f047ac0bea6e12155687ecd831b36e029976ca221f7cacd3c99"
|
||||
],
|
||||
"sourceUnchanged": true,
|
||||
"after": [
|
||||
{
|
||||
"service": "api",
|
||||
"name": "mtusi2026lab4-api-1",
|
||||
"id": "ce0434d224ba",
|
||||
"state": "running",
|
||||
"health": "healthy",
|
||||
"image": "mtusi2026lab4-api"
|
||||
},
|
||||
{
|
||||
"service": "api",
|
||||
"name": "mtusi2026lab4-api-2",
|
||||
"id": "752f50d7b664",
|
||||
"state": "running",
|
||||
"health": "healthy",
|
||||
"image": "mtusi2026lab4-api"
|
||||
},
|
||||
{
|
||||
"service": "nginx",
|
||||
"name": "mtusi2026lab4-nginx-1",
|
||||
"id": "dd8840a93c5b",
|
||||
"state": "running",
|
||||
"health": "healthy",
|
||||
"image": "nginx:1.30.6-alpine"
|
||||
},
|
||||
{
|
||||
"service": "postgres",
|
||||
"name": "mtusi2026lab4-postgres-1",
|
||||
"id": "14c8623492ba",
|
||||
"state": "running",
|
||||
"health": "healthy",
|
||||
"image": "postgres:17.6-bookworm"
|
||||
},
|
||||
{
|
||||
"service": "reconciler",
|
||||
"name": "mtusi2026lab4-reconciler-1",
|
||||
"id": "4058a1f65b0b",
|
||||
"state": "running",
|
||||
"health": "healthy",
|
||||
"image": "mtusi2026lab4-reconciler"
|
||||
},
|
||||
{
|
||||
"service": "redis",
|
||||
"name": "mtusi2026lab4-redis-1",
|
||||
"id": "b521d405535f",
|
||||
"state": "running",
|
||||
"health": "healthy",
|
||||
"image": "redis:7.4.5-alpine"
|
||||
},
|
||||
{
|
||||
"service": "s3",
|
||||
"name": "mtusi2026lab4-s3-1",
|
||||
"id": "ef6e12a25440",
|
||||
"state": "running",
|
||||
"health": "healthy",
|
||||
"image": "rustfs/rustfs:1.0.4"
|
||||
}
|
||||
],
|
||||
"stackRestored": true,
|
||||
"finishedAt": "2026-11-11T21:10:19.452449+00:00"
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
Final regression: PASSED
|
||||
Started (UTC): 2026-11-11T21:03:10.320137+00:00
|
||||
Finished (UTC): 2026-11-11T21:10:19.452449+00:00
|
||||
Source/config/test SHA-256: 12af1c58afaa9c1600a9c95edbb083757447ee1d2ecca11948f6968ad80c3c8c
|
||||
14 sequential suites passed; 47 unit tests passed; lint and Docker build passed.
|
||||
Public tests unchanged; no business-request retries.
|
||||
Runtime restored: both API replicas plus Nginx/PostgreSQL/Redis/RustFS/reconciler healthy.
|
||||
Each API stopped separately by public hooks; all runtime stores restarted with volumes preserved.
|
||||
|
||||
Suites:
|
||||
offline_contract: PASS (16.2 s)
|
||||
unit: PASS (32.8 s)
|
||||
lint: PASS (7.7 s)
|
||||
public_smoke_before: PASS (4.5 s)
|
||||
oversized_upload_transport: PASS (15.6 s)
|
||||
attachments_s3_outage_persistence: PASS (92.0 s)
|
||||
reconciliation_crash_and_database_outage: PASS (14.8 s)
|
||||
public_persistence_and_each_api_failover: PASS (123.8 s)
|
||||
swagger_and_attachment_failover: PASS (17.5 s)
|
||||
interrupted_upload_and_receive_deadline: PASS (58.9 s)
|
||||
presigned_positive_and_negative: PASS (14.7 s)
|
||||
s3_restricted_roles: PASS (4.2 s)
|
||||
s3_conditional_write_checksum_copy: PASS (4.0 s)
|
||||
public_smoke_after: PASS (2.7 s)
|
||||
|
||||
Fix found during regression: early oversized upload rejection could yield Nginx 502 despite API 413.
|
||||
Fix: bounded discard before sending 413, within the original receive deadline; explicit upstream Connection header.
|
||||
Added byte-budget/end/deadline unit tests and real Content-Length/chunked regression through Nginx.
|
||||
New drain code affects rejected bodies; normal upload buffering/validation remains the same.
|
||||
Student lifecycle readiness now waits for both upstreams to remain ready across the reload transition.
|
||||
Public tests and failover thresholds were not relaxed.
|
||||
Earlier transport/recovery failures were superseded after the fixes; obsolete snapshots removed during report cleanup.
|
||||
|
||||
Repeat: python -X utf8 test/final-regression.py
|
||||
@@ -0,0 +1,372 @@
|
||||
{
|
||||
"status": "passed",
|
||||
"failover": [
|
||||
{
|
||||
"stoppedInstance": "ce0434d224ba",
|
||||
"before": {
|
||||
"mtusi2026lab4-api-1": {
|
||||
"service": "api",
|
||||
"instance": "ce0434d224ba",
|
||||
"startedAt": "2026-11-11T21:06:56.691717844Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-api-2": {
|
||||
"service": "api",
|
||||
"instance": "752f50d7b664",
|
||||
"startedAt": "2026-11-11T21:06:57.128454611Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-nginx-1": {
|
||||
"service": "nginx",
|
||||
"instance": "dd8840a93c5b",
|
||||
"startedAt": "2026-11-11T21:07:09.789954207Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-postgres-1": {
|
||||
"service": "postgres",
|
||||
"instance": "14c8623492ba",
|
||||
"startedAt": "2026-11-11T21:06:47.578821968Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-reconciler-1": {
|
||||
"service": "reconciler",
|
||||
"instance": "4058a1f65b0b",
|
||||
"startedAt": "2026-11-11T21:07:09.322356788Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-redis-1": {
|
||||
"service": "redis",
|
||||
"instance": "b521d405535f",
|
||||
"startedAt": "2026-11-11T21:06:47.601706994Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-s3-1": {
|
||||
"service": "s3",
|
||||
"instance": "ef6e12a25440",
|
||||
"startedAt": "2026-11-11T21:06:47.595668528Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
}
|
||||
},
|
||||
"after": {
|
||||
"mtusi2026lab4-api-1": {
|
||||
"service": "api",
|
||||
"instance": "ce0434d224ba",
|
||||
"startedAt": "2026-11-11T21:06:56.691717844Z",
|
||||
"status": "exited",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-api-2": {
|
||||
"service": "api",
|
||||
"instance": "752f50d7b664",
|
||||
"startedAt": "2026-11-11T21:06:57.128454611Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-nginx-1": {
|
||||
"service": "nginx",
|
||||
"instance": "dd8840a93c5b",
|
||||
"startedAt": "2026-11-11T21:07:09.789954207Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-postgres-1": {
|
||||
"service": "postgres",
|
||||
"instance": "14c8623492ba",
|
||||
"startedAt": "2026-11-11T21:06:47.578821968Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-reconciler-1": {
|
||||
"service": "reconciler",
|
||||
"instance": "4058a1f65b0b",
|
||||
"startedAt": "2026-11-11T21:07:09.322356788Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-redis-1": {
|
||||
"service": "redis",
|
||||
"instance": "b521d405535f",
|
||||
"startedAt": "2026-11-11T21:06:47.601706994Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-s3-1": {
|
||||
"service": "s3",
|
||||
"instance": "ef6e12a25440",
|
||||
"startedAt": "2026-11-11T21:06:47.595668528Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
}
|
||||
},
|
||||
"output": "Executing explicit failover hook; hook output suppressed to avoid leaking secrets.\n{\"observed_instances_before\": 2, \"successful_writes\": 86, \"failed_attempts\": 1, \"first_recovery_seconds\": 4.206}\nPASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence."
|
||||
},
|
||||
{
|
||||
"stoppedInstance": "752f50d7b664",
|
||||
"before": {
|
||||
"mtusi2026lab4-api-1": {
|
||||
"service": "api",
|
||||
"instance": "ce0434d224ba",
|
||||
"startedAt": "2026-11-11T21:07:40.215373791Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-api-2": {
|
||||
"service": "api",
|
||||
"instance": "752f50d7b664",
|
||||
"startedAt": "2026-11-11T21:06:57.128454611Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-nginx-1": {
|
||||
"service": "nginx",
|
||||
"instance": "dd8840a93c5b",
|
||||
"startedAt": "2026-11-11T21:07:09.789954207Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-postgres-1": {
|
||||
"service": "postgres",
|
||||
"instance": "14c8623492ba",
|
||||
"startedAt": "2026-11-11T21:06:47.578821968Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-reconciler-1": {
|
||||
"service": "reconciler",
|
||||
"instance": "4058a1f65b0b",
|
||||
"startedAt": "2026-11-11T21:07:09.322356788Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-redis-1": {
|
||||
"service": "redis",
|
||||
"instance": "b521d405535f",
|
||||
"startedAt": "2026-11-11T21:06:47.601706994Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-s3-1": {
|
||||
"service": "s3",
|
||||
"instance": "ef6e12a25440",
|
||||
"startedAt": "2026-11-11T21:06:47.595668528Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
}
|
||||
},
|
||||
"after": {
|
||||
"mtusi2026lab4-api-1": {
|
||||
"service": "api",
|
||||
"instance": "ce0434d224ba",
|
||||
"startedAt": "2026-11-11T21:07:40.215373791Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-api-2": {
|
||||
"service": "api",
|
||||
"instance": "752f50d7b664",
|
||||
"startedAt": "2026-11-11T21:06:57.128454611Z",
|
||||
"status": "exited",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-nginx-1": {
|
||||
"service": "nginx",
|
||||
"instance": "dd8840a93c5b",
|
||||
"startedAt": "2026-11-11T21:07:09.789954207Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-postgres-1": {
|
||||
"service": "postgres",
|
||||
"instance": "14c8623492ba",
|
||||
"startedAt": "2026-11-11T21:06:47.578821968Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-reconciler-1": {
|
||||
"service": "reconciler",
|
||||
"instance": "4058a1f65b0b",
|
||||
"startedAt": "2026-11-11T21:07:09.322356788Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-redis-1": {
|
||||
"service": "redis",
|
||||
"instance": "b521d405535f",
|
||||
"startedAt": "2026-11-11T21:06:47.601706994Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-s3-1": {
|
||||
"service": "s3",
|
||||
"instance": "ef6e12a25440",
|
||||
"startedAt": "2026-11-11T21:06:47.595668528Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
}
|
||||
},
|
||||
"output": "Executing explicit failover hook; hook output suppressed to avoid leaking secrets.\n{\"observed_instances_before\": 2, \"successful_writes\": 93, \"failed_attempts\": 2, \"first_recovery_seconds\": 4.127}\nPASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence."
|
||||
}
|
||||
],
|
||||
"writeWarmups": [
|
||||
{
|
||||
"752f50d7b664": {
|
||||
"writes": 3,
|
||||
"minSeconds": 0.02528520001214929,
|
||||
"maxSeconds": 0.05653770000208169,
|
||||
"uploadSeconds": [
|
||||
0.14060700000845827
|
||||
],
|
||||
"downloadSeconds": [
|
||||
0.02674750000005588
|
||||
]
|
||||
},
|
||||
"ce0434d224ba": {
|
||||
"writes": 3,
|
||||
"minSeconds": 0.024145300005329773,
|
||||
"maxSeconds": 0.0363634999957867,
|
||||
"uploadSeconds": [
|
||||
0.12597850000020117
|
||||
],
|
||||
"downloadSeconds": [
|
||||
0.030548900016583502
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"ce0434d224ba": {
|
||||
"writes": 3,
|
||||
"minSeconds": 0.03536629999871366,
|
||||
"maxSeconds": 0.14764460001606494,
|
||||
"uploadSeconds": [
|
||||
0.10738060000585392
|
||||
],
|
||||
"downloadSeconds": [
|
||||
0.040137300005881116
|
||||
]
|
||||
},
|
||||
"752f50d7b664": {
|
||||
"writes": 3,
|
||||
"minSeconds": 0.020328800019342452,
|
||||
"maxSeconds": 0.03870999999344349,
|
||||
"uploadSeconds": [
|
||||
0.08634139999048784
|
||||
],
|
||||
"downloadSeconds": [
|
||||
0.026475600025150925
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"persistence": {
|
||||
"before": {
|
||||
"mtusi2026lab4-api-1": {
|
||||
"service": "api",
|
||||
"instance": "ce0434d224ba",
|
||||
"startedAt": "2026-11-11T21:05:38.078659211Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-api-2": {
|
||||
"service": "api",
|
||||
"instance": "752f50d7b664",
|
||||
"startedAt": "2026-11-11T21:05:38.603587222Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-nginx-1": {
|
||||
"service": "nginx",
|
||||
"instance": "dd8840a93c5b",
|
||||
"startedAt": "2026-11-11T21:05:50.529959093Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-postgres-1": {
|
||||
"service": "postgres",
|
||||
"instance": "14c8623492ba",
|
||||
"startedAt": "2026-11-11T21:05:24.00716937Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-reconciler-1": {
|
||||
"service": "reconciler",
|
||||
"instance": "4058a1f65b0b",
|
||||
"startedAt": "2026-11-11T21:05:50.258628795Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-redis-1": {
|
||||
"service": "redis",
|
||||
"instance": "b521d405535f",
|
||||
"startedAt": "2026-11-11T21:05:23.98785755Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-s3-1": {
|
||||
"service": "s3",
|
||||
"instance": "ef6e12a25440",
|
||||
"startedAt": "2026-11-11T21:05:23.952757617Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
}
|
||||
},
|
||||
"after": {
|
||||
"mtusi2026lab4-api-1": {
|
||||
"service": "api",
|
||||
"instance": "ce0434d224ba",
|
||||
"startedAt": "2026-11-11T21:06:56.691717844Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-api-2": {
|
||||
"service": "api",
|
||||
"instance": "752f50d7b664",
|
||||
"startedAt": "2026-11-11T21:06:57.128454611Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-nginx-1": {
|
||||
"service": "nginx",
|
||||
"instance": "dd8840a93c5b",
|
||||
"startedAt": "2026-11-11T21:07:09.789954207Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-postgres-1": {
|
||||
"service": "postgres",
|
||||
"instance": "14c8623492ba",
|
||||
"startedAt": "2026-11-11T21:06:47.578821968Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-reconciler-1": {
|
||||
"service": "reconciler",
|
||||
"instance": "4058a1f65b0b",
|
||||
"startedAt": "2026-11-11T21:07:09.322356788Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-redis-1": {
|
||||
"service": "redis",
|
||||
"instance": "b521d405535f",
|
||||
"startedAt": "2026-11-11T21:06:47.601706994Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
},
|
||||
"mtusi2026lab4-s3-1": {
|
||||
"service": "s3",
|
||||
"instance": "ef6e12a25440",
|
||||
"startedAt": "2026-11-11T21:06:47.595668528Z",
|
||||
"status": "running",
|
||||
"restartCount": 0
|
||||
}
|
||||
},
|
||||
"output": "Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.\nPASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
{
|
||||
"status": "passed",
|
||||
"attempts": [
|
||||
{
|
||||
"mode": "content-length",
|
||||
"status": 413,
|
||||
"instance": "752f50d7b664"
|
||||
},
|
||||
{
|
||||
"mode": "chunked",
|
||||
"status": 413,
|
||||
"instance": "ce0434d224ba"
|
||||
},
|
||||
{
|
||||
"mode": "content-length",
|
||||
"status": 413,
|
||||
"instance": "752f50d7b664"
|
||||
},
|
||||
{
|
||||
"mode": "chunked",
|
||||
"status": 413,
|
||||
"instance": "ce0434d224ba"
|
||||
},
|
||||
{
|
||||
"mode": "content-length",
|
||||
"status": 413,
|
||||
"instance": "752f50d7b664"
|
||||
},
|
||||
{
|
||||
"mode": "chunked",
|
||||
"status": 413,
|
||||
"instance": "ce0434d224ba"
|
||||
},
|
||||
{
|
||||
"mode": "content-length",
|
||||
"status": 413,
|
||||
"instance": "752f50d7b664"
|
||||
},
|
||||
{
|
||||
"mode": "chunked",
|
||||
"status": 413,
|
||||
"instance": "ce0434d224ba"
|
||||
},
|
||||
{
|
||||
"mode": "content-length",
|
||||
"status": 413,
|
||||
"instance": "752f50d7b664"
|
||||
},
|
||||
{
|
||||
"mode": "chunked",
|
||||
"status": 413,
|
||||
"instance": "ce0434d224ba"
|
||||
},
|
||||
{
|
||||
"mode": "content-length",
|
||||
"status": 413,
|
||||
"instance": "752f50d7b664"
|
||||
},
|
||||
{
|
||||
"mode": "chunked",
|
||||
"status": 413,
|
||||
"instance": "ce0434d224ba"
|
||||
},
|
||||
{
|
||||
"mode": "content-length",
|
||||
"status": 413,
|
||||
"instance": "752f50d7b664"
|
||||
},
|
||||
{
|
||||
"mode": "chunked",
|
||||
"status": 413,
|
||||
"instance": "ce0434d224ba"
|
||||
},
|
||||
{
|
||||
"mode": "content-length",
|
||||
"status": 413,
|
||||
"instance": "752f50d7b664"
|
||||
},
|
||||
{
|
||||
"mode": "chunked",
|
||||
"status": 413,
|
||||
"instance": "ce0434d224ba"
|
||||
}
|
||||
],
|
||||
"fixturesRemoved": true
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"event": "upload_interruption_summary",
|
||||
"status": "passed",
|
||||
"targets": [
|
||||
{
|
||||
"target": "ce0434d224ba",
|
||||
"contentLengthAbort": "passed",
|
||||
"chunkedAbort": "passed",
|
||||
"deadline": {
|
||||
"status": 408,
|
||||
"elapsedMs": 15008,
|
||||
"instance": "ce0434d224ba",
|
||||
"connection": "close"
|
||||
},
|
||||
"normalUploadAfterAbort": "passed",
|
||||
"databaseAndS3Match": true
|
||||
},
|
||||
{
|
||||
"target": "752f50d7b664",
|
||||
"contentLengthAbort": "passed",
|
||||
"chunkedAbort": "passed",
|
||||
"deadline": {
|
||||
"status": 408,
|
||||
"elapsedMs": 15005,
|
||||
"instance": "752f50d7b664",
|
||||
"connection": "close"
|
||||
},
|
||||
"normalUploadAfterAbort": "passed",
|
||||
"databaseAndS3Match": true
|
||||
},
|
||||
{
|
||||
"target": "nginx",
|
||||
"contentLengthAbort": "passed",
|
||||
"chunkedAbort": "passed",
|
||||
"deadline": {
|
||||
"status": 408,
|
||||
"elapsedMs": 15006,
|
||||
"instance": "ce0434d224ba",
|
||||
"connection": "keep-alive"
|
||||
},
|
||||
"normalUploadAfterAbort": "passed",
|
||||
"databaseAndS3Match": true
|
||||
}
|
||||
],
|
||||
"fixturesRemoved": true
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
events {}
|
||||
|
||||
http {
|
||||
# Capabilities are query strings; never write signed URLs to access logs.
|
||||
server {
|
||||
listen 9000;
|
||||
access_log off;
|
||||
error_log /dev/null;
|
||||
client_max_body_size 10m;
|
||||
client_body_timeout 15s;
|
||||
location ~ "^/messenger-attachments/staging/[0-9a-f-]{36}/[0-9a-f-]{36}$" {
|
||||
add_header Access-Control-Allow-Origin '*' always;
|
||||
add_header Access-Control-Allow-Methods 'PUT, OPTIONS' always;
|
||||
add_header Access-Control-Allow-Headers 'Content-Type, Content-Length, If-None-Match, x-amz-checksum-sha256' always;
|
||||
add_header Access-Control-Expose-Headers 'ETag, x-amz-checksum-sha256' always;
|
||||
if ($request_method = OPTIONS) { return 204; }
|
||||
if ($request_method != PUT) { return 405; }
|
||||
proxy_pass http://s3:9000;
|
||||
proxy_request_buffering off;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $http_host;
|
||||
proxy_set_header Connection '';
|
||||
proxy_hide_header Access-Control-Allow-Origin;
|
||||
proxy_hide_header Access-Control-Allow-Headers;
|
||||
proxy_hide_header Access-Control-Allow-Methods;
|
||||
proxy_connect_timeout 2s;
|
||||
proxy_read_timeout 30s;
|
||||
}
|
||||
location / { return 403; }
|
||||
}
|
||||
|
||||
upstream api {
|
||||
least_conn;
|
||||
zone api 64k;
|
||||
include /etc/nginx/ready-upstreams.conf;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
client_max_body_size 11m;
|
||||
|
||||
location / {
|
||||
proxy_pass http://api;
|
||||
proxy_request_buffering off;
|
||||
proxy_http_version 1.1;
|
||||
# Preserve early 4xx responses while the client is still sending a body.
|
||||
# The default Connection: close can make Node close before Nginx reads 413.
|
||||
proxy_set_header Connection '';
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_connect_timeout 2s;
|
||||
proxy_read_timeout 30s;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import { createHash, createHmac } from 'node:crypto';
|
||||
|
||||
const hash = (value) => createHash('sha256').update(value).digest('hex');
|
||||
const hmac = (key, value) => createHmac('sha256', key).update(value).digest();
|
||||
const encode = (value) =>
|
||||
encodeURIComponent(value).replace(
|
||||
/[!'()*]/g,
|
||||
(character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`,
|
||||
);
|
||||
|
||||
/** Sign RustFS native JSON admin requests; credentials and response bodies are never logged. */
|
||||
export function signAdminRequest({
|
||||
endpoint,
|
||||
credentials,
|
||||
region = 'us-east-1',
|
||||
method,
|
||||
operation,
|
||||
query = {},
|
||||
body,
|
||||
now = new Date(),
|
||||
}) {
|
||||
const url = new URL(`/rustfs/admin/v3/${operation}`, endpoint);
|
||||
const canonicalQuery = Object.entries(query)
|
||||
.map(([key, value]) => [encode(key), encode(String(value))])
|
||||
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
||||
.map(([key, value]) => `${key}=${value}`)
|
||||
.join('&');
|
||||
url.search = canonicalQuery;
|
||||
const payload = body === undefined ? '' : JSON.stringify(body);
|
||||
const date = now.toISOString().replace(/[:-]|\.\d{3}/g, '');
|
||||
const headers = {
|
||||
'content-type': 'application/json',
|
||||
host: url.host,
|
||||
'x-amz-content-sha256': hash(payload),
|
||||
'x-amz-date': date,
|
||||
};
|
||||
const names = Object.keys(headers).sort();
|
||||
const canonicalHeaders = names
|
||||
.map((name) => `${name}:${headers[name]}\n`)
|
||||
.join('');
|
||||
const canonical = [
|
||||
method,
|
||||
url.pathname,
|
||||
canonicalQuery,
|
||||
canonicalHeaders,
|
||||
names.join(';'),
|
||||
hash(payload),
|
||||
].join('\n');
|
||||
const scope = `${date.slice(0, 8)}/${region}/s3/aws4_request`;
|
||||
const stringToSign = ['AWS4-HMAC-SHA256', date, scope, hash(canonical)].join(
|
||||
'\n',
|
||||
);
|
||||
const signingKey = hmac(
|
||||
hmac(
|
||||
hmac(
|
||||
hmac(`AWS4${credentials.secretAccessKey}`, date.slice(0, 8)),
|
||||
region,
|
||||
),
|
||||
's3',
|
||||
),
|
||||
'aws4_request',
|
||||
);
|
||||
const signature = createHmac('sha256', signingKey)
|
||||
.update(stringToSign)
|
||||
.digest('hex');
|
||||
headers.authorization = `AWS4-HMAC-SHA256 Credential=${credentials.accessKeyId}/${scope}, SignedHeaders=${names.join(';')}, Signature=${signature}`;
|
||||
return {
|
||||
url,
|
||||
method,
|
||||
headers,
|
||||
body: body === undefined ? undefined : payload,
|
||||
};
|
||||
}
|
||||
|
||||
export async function adminRequest(config, method, operation, query, body) {
|
||||
const request = signAdminRequest({
|
||||
...config,
|
||||
method,
|
||||
operation,
|
||||
query,
|
||||
body,
|
||||
});
|
||||
const response = await fetch(request.url, {
|
||||
...request,
|
||||
signal: AbortSignal.timeout(30000),
|
||||
});
|
||||
const text = await response.text();
|
||||
if (!response.ok) {
|
||||
const code =
|
||||
text.match(/<Code>([A-Za-z0-9_]+)<\/Code>/)?.[1] ?? 'AdminRequestFailed';
|
||||
const error = new Error(
|
||||
`RustFS admin ${operation}: HTTP ${response.status} ${code}`,
|
||||
);
|
||||
error.status = response.status;
|
||||
throw error;
|
||||
}
|
||||
return text ? JSON.parse(text) : null;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import {
|
||||
S3Client,
|
||||
CreateBucketCommand,
|
||||
HeadBucketCommand,
|
||||
} from '@aws-sdk/client-s3';
|
||||
import { adminRequest } from './admin.mjs';
|
||||
import { bootstrapSettings, storagePolicies } from './policies.mjs';
|
||||
|
||||
const settings = bootstrapSettings();
|
||||
const client = new S3Client({
|
||||
endpoint: settings.endpoint,
|
||||
region: settings.region,
|
||||
forcePathStyle: true,
|
||||
maxAttempts: 1,
|
||||
credentials: settings.credentials,
|
||||
});
|
||||
const Bucket = settings.bucket;
|
||||
try {
|
||||
try {
|
||||
await client.send(new HeadBucketCommand({ Bucket }), {
|
||||
abortSignal: AbortSignal.timeout(30000),
|
||||
});
|
||||
} catch (error) {
|
||||
if (error.$metadata?.httpStatusCode !== 404) throw error;
|
||||
try {
|
||||
await client.send(new CreateBucketCommand({ Bucket }), {
|
||||
abortSignal: AbortSignal.timeout(30000),
|
||||
});
|
||||
} catch (error) {
|
||||
if (error.name !== 'BucketAlreadyOwnedByYou') throw error;
|
||||
}
|
||||
}
|
||||
const policies = storagePolicies(Bucket);
|
||||
for (const role of ['api', 'reconciler']) {
|
||||
const identity = settings[role];
|
||||
const policyName = `${Bucket}-${role}`;
|
||||
await adminRequest(
|
||||
settings,
|
||||
'PUT',
|
||||
'add-canned-policy',
|
||||
{ name: policyName },
|
||||
policies[role],
|
||||
);
|
||||
await adminRequest(
|
||||
settings,
|
||||
'PUT',
|
||||
'add-user',
|
||||
{ accessKey: identity.accessKeyId },
|
||||
{ secretKey: identity.secretAccessKey, status: 'enabled' },
|
||||
);
|
||||
// Replace the direct binding instead of accumulating old policies across bootstraps.
|
||||
await adminRequest(settings, 'PUT', 'set-user-or-group-policy', {
|
||||
policyName,
|
||||
userOrGroup: identity.accessKeyId,
|
||||
isGroup: false,
|
||||
});
|
||||
console.log(`Restricted S3 identity provisioned: ${role}`);
|
||||
}
|
||||
console.log('Private attachments bucket and restricted identities are ready');
|
||||
} finally {
|
||||
client.destroy();
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
export function storagePolicies(bucket) {
|
||||
const bucketArn = `arn:aws:s3:::${bucket}`;
|
||||
const objectsArn = `${bucketArn}/originals/*`;
|
||||
const base = [
|
||||
{
|
||||
Effect: 'Allow',
|
||||
Action: ['s3:GetBucketLocation'],
|
||||
Resource: [bucketArn],
|
||||
},
|
||||
];
|
||||
return {
|
||||
api: {
|
||||
Version: '2012-10-17',
|
||||
Statement: [
|
||||
...base,
|
||||
{
|
||||
Effect: 'Allow',
|
||||
Action: ['s3:GetObject', 's3:PutObject', 's3:DeleteObject'],
|
||||
Resource: [objectsArn, `${bucketArn}/staging/*`],
|
||||
},
|
||||
],
|
||||
},
|
||||
reconciler: {
|
||||
Version: '2012-10-17',
|
||||
Statement: [
|
||||
...base,
|
||||
{
|
||||
Effect: 'Allow',
|
||||
Action: ['s3:ListBucket'],
|
||||
Resource: [bucketArn],
|
||||
Condition: {
|
||||
StringLike: { 's3:prefix': ['originals/*', 'staging/*'] },
|
||||
},
|
||||
},
|
||||
{
|
||||
Effect: 'Allow',
|
||||
Action: ['s3:GetObject', 's3:DeleteObject'],
|
||||
Resource: [objectsArn, `${bucketArn}/staging/*`],
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function bootstrapSettings(env = process.env) {
|
||||
const settings = {
|
||||
endpoint: env.S3_ENDPOINT ?? 'http://s3:9000',
|
||||
region: env.S3_REGION ?? 'us-east-1',
|
||||
bucket: env.S3_BUCKET ?? 'messenger-attachments',
|
||||
credentials: {
|
||||
accessKeyId: env.S3_ACCESS_KEY_ID,
|
||||
secretAccessKey: env.S3_SECRET_ACCESS_KEY,
|
||||
},
|
||||
api: {
|
||||
accessKeyId: env.S3_API_ACCESS_KEY_ID ?? 'LABAPI',
|
||||
secretAccessKey:
|
||||
env.S3_API_SECRET_ACCESS_KEY ?? 'local_api_storage_password',
|
||||
},
|
||||
reconciler: {
|
||||
accessKeyId: env.S3_RECONCILER_ACCESS_KEY_ID ?? 'LABRECONCILER',
|
||||
secretAccessKey:
|
||||
env.S3_RECONCILER_SECRET_ACCESS_KEY ??
|
||||
'local_reconciler_storage_password',
|
||||
},
|
||||
};
|
||||
const identities = [settings.credentials, settings.api, settings.reconciler];
|
||||
for (const identity of identities) {
|
||||
if (
|
||||
!identity.accessKeyId ||
|
||||
!identity.secretAccessKey ||
|
||||
identity.secretAccessKey.length < 8
|
||||
) {
|
||||
throw new Error(
|
||||
'All S3 identities require an access key and a secret of at least 8 characters',
|
||||
);
|
||||
}
|
||||
}
|
||||
if (
|
||||
new Set(identities.map((identity) => identity.accessKeyId)).size !== 3 ||
|
||||
new Set(identities.map((identity) => identity.secretAccessKey)).size !== 3
|
||||
) {
|
||||
throw new Error(
|
||||
'Root, API and reconciler identities must have distinct access and secret keys',
|
||||
);
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
upstreams=/etc/nginx/ready-upstreams.conf
|
||||
printf 'server 127.0.0.1:9 down;\n' > "$upstreams"
|
||||
|
||||
check_ready() {
|
||||
state=/tmp/nginx-readiness.state
|
||||
probes=/tmp/nginx-readiness-probes
|
||||
mkdir -p "$probes"
|
||||
: > "$state"
|
||||
sleep 2
|
||||
while :; do
|
||||
next="${upstreams}.next"
|
||||
previous="${upstreams}.previous"
|
||||
: > "${state}.next"
|
||||
printf 'server 127.0.0.1:9 down;\n' > "$next"
|
||||
|
||||
addresses=$(getent ahostsv4 api | awk '{print $1}' | sort -u)
|
||||
for address in $addresses; do
|
||||
(
|
||||
if wget -q -T 1 -t 1 -O /dev/null "http://${address}:8080/health/ready"; then
|
||||
printf '1\n'
|
||||
else
|
||||
printf '0\n'
|
||||
fi > "$probes/$address"
|
||||
) &
|
||||
done
|
||||
wait
|
||||
|
||||
for address in $addresses; do
|
||||
record=$(awk -v address="$address" '$1 == address {print $2, $3, $4}' "$state")
|
||||
set -- ${record:-0 0 0}
|
||||
ready=$1
|
||||
successes=$2
|
||||
failures=$3
|
||||
if [ "$(cat "$probes/$address")" = 1 ]; then
|
||||
failures=0
|
||||
if [ "$successes" -lt 2 ]; then successes=$((successes + 1)); fi
|
||||
if [ "$successes" -ge 2 ]; then ready=1; fi
|
||||
else
|
||||
successes=0
|
||||
if [ "$failures" -lt 2 ]; then failures=$((failures + 1)); fi
|
||||
if [ "$failures" -ge 2 ]; then ready=0; fi
|
||||
fi
|
||||
printf '%s %s %s %s\n' "$address" "$ready" "$successes" "$failures" >> "${state}.next"
|
||||
if [ "$ready" -eq 1 ]; then
|
||||
printf 'server %s:8080 max_fails=2 fail_timeout=2s;\n' "$address" >> "$next"
|
||||
fi
|
||||
done
|
||||
mv "${state}.next" "$state"
|
||||
|
||||
if cmp -s "$next" "$upstreams"; then
|
||||
rm -f "$next"
|
||||
else
|
||||
cp "$upstreams" "$previous"
|
||||
mv "$next" "$upstreams"
|
||||
if nginx -t -q && nginx -s reload; then
|
||||
rm -f "$previous"
|
||||
else
|
||||
mv "$previous" "$upstreams"
|
||||
printf 'Could not reload Nginx readiness upstreams\n' >&2
|
||||
fi
|
||||
fi
|
||||
|
||||
sleep 2
|
||||
done
|
||||
}
|
||||
|
||||
check_ready &
|
||||
exec nginx -g 'daemon off;'
|
||||
@@ -0,0 +1,31 @@
|
||||
import type { Config } from 'jest';
|
||||
import { pathsToModuleNameMapper } from 'ts-jest';
|
||||
import ts from 'typescript';
|
||||
|
||||
const { config: tsconfig } = ts.readConfigFile(
|
||||
'./tsconfig.json',
|
||||
ts.sys.readFile,
|
||||
);
|
||||
const paths = tsconfig?.compilerOptions?.paths ?? {};
|
||||
|
||||
const config: Config = {
|
||||
moduleFileExtensions: ['js', 'json', 'ts'],
|
||||
rootDir: '.',
|
||||
testRegex: '.*\\.spec\\.ts$',
|
||||
transform: {
|
||||
'^.+\\.(t|j)s$': 'ts-jest',
|
||||
},
|
||||
moduleNameMapper: {
|
||||
...pathsToModuleNameMapper(paths, { prefix: '<rootDir>/' }),
|
||||
'^(\\.{1,2}/.*)\\.js$': '$1',
|
||||
},
|
||||
collectCoverageFrom: [
|
||||
'src/**/*.(t|j)s',
|
||||
'libs/**/*.(t|j)s',
|
||||
'apps/**/*.(t|j)s',
|
||||
],
|
||||
coverageDirectory: './coverage',
|
||||
testEnvironment: 'node',
|
||||
};
|
||||
|
||||
export default config;
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/nest-cli",
|
||||
"collection": "@nestjs/schematics",
|
||||
"sourceRoot": "src",
|
||||
"compilerOptions": {
|
||||
"deleteOutDir": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
{
|
||||
"name": "mtusi2026lab4",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"author": "",
|
||||
"private": true,
|
||||
"license": "UNLICENSED",
|
||||
"scripts": {
|
||||
"db:generate": "prisma generate",
|
||||
"db:validate": "prisma validate",
|
||||
"db:migrate": "prisma migrate dev",
|
||||
"db:deploy": "prisma migrate deploy",
|
||||
"prebuild": "pnpm db:generate",
|
||||
"prestart:dev": "pnpm db:generate",
|
||||
"prestart": "pnpm db:generate",
|
||||
"pretest": "pnpm db:generate",
|
||||
"build": "nest build",
|
||||
"format": "prettier --write \"src/**/*.ts\"",
|
||||
"start": "nest start",
|
||||
"start:dev": "nest start --watch",
|
||||
"start:debug": "nest start --debug --watch",
|
||||
"start:prod": "node dist/main",
|
||||
"lint": "oxlint --type-aware src/",
|
||||
"test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js",
|
||||
"test:watch": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js --watch",
|
||||
"test:cov": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js --coverage",
|
||||
"test:debug": "node --inspect-brk --experimental-vm-modules -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand"
|
||||
},
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-s3": "^3.1164.0",
|
||||
"@aws-sdk/s3-request-presigner": "^3.1165.0",
|
||||
"@fastify/static": "^10.1.4",
|
||||
"@nestjs/common": "^12.0.1",
|
||||
"@nestjs/core": "^12.0.1",
|
||||
"@nestjs/platform-fastify": "12.0.1",
|
||||
"@nestjs/swagger": "^12.0.1",
|
||||
"@prisma/adapter-pg": "^7.10.0",
|
||||
"@prisma/client": "^7.10.0",
|
||||
"@scalar/nestjs-api-reference": "^1.2.19",
|
||||
"dotenv": "^18.0.7",
|
||||
"fastify": "5.12.1",
|
||||
"ioredis": "^6.0.0",
|
||||
"nestjs-pino": "^5.2.0",
|
||||
"pg": "^8.23.1",
|
||||
"pino-http": "^11.0.0",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
"rxjs": "^7.8.1",
|
||||
"sharp": "^0.35.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nestjs/cli": "^12.0.0",
|
||||
"@nestjs/mau": "^0.2.6",
|
||||
"@nestjs/schematics": "^12.0.0",
|
||||
"@nestjs/testing": "^12.0.1",
|
||||
"@types/jest": "^30.0.0",
|
||||
"@types/node": "^24.0.0",
|
||||
"@types/pg": "^8.23.1",
|
||||
"@types/supertest": "^7.0.0",
|
||||
"jest": "^30.0.0",
|
||||
"oxlint": "^1.58.0",
|
||||
"oxlint-tsgolint": "^7.0.2001",
|
||||
"pino-pretty": "^13.1.3",
|
||||
"prettier": "^3.4.2",
|
||||
"prisma": "7.10.0",
|
||||
"source-map-support": "^0.5.21",
|
||||
"supertest": "^7.0.0",
|
||||
"ts-jest": "^29.4.0",
|
||||
"ts-loader": "^9.5.2",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsconfig-paths": "^4.2.0",
|
||||
"typescript": "^6.0.2"
|
||||
}
|
||||
}
|
||||
Generated
+8367
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,8 @@
|
||||
import 'dotenv/config';
|
||||
import { defineConfig } from 'prisma/config';
|
||||
|
||||
export default defineConfig({
|
||||
schema: 'prisma/schema.prisma',
|
||||
migrations: { path: 'prisma/migrations' },
|
||||
datasource: { url: process.env.DATABASE_URL },
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
CREATE SCHEMA IF NOT EXISTS "public";
|
||||
|
||||
CREATE TABLE "users" (
|
||||
"id" UUID NOT NULL,
|
||||
"username" VARCHAR(32) NOT NULL,
|
||||
"display_name" VARCHAR(100) NOT NULL,
|
||||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "users_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE TABLE "chats" (
|
||||
"id" UUID NOT NULL,
|
||||
"title" VARCHAR(100) NOT NULL,
|
||||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "chats_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE TABLE "chat_members" (
|
||||
"chat_id" UUID NOT NULL,
|
||||
"user_id" UUID NOT NULL,
|
||||
"position" INTEGER NOT NULL,
|
||||
|
||||
CONSTRAINT "chat_members_pkey" PRIMARY KEY ("chat_id","user_id")
|
||||
);
|
||||
|
||||
CREATE TABLE "messages" (
|
||||
"id" UUID NOT NULL,
|
||||
"chat_id" UUID NOT NULL,
|
||||
"sender_id" UUID NOT NULL,
|
||||
"text" VARCHAR(4000) NOT NULL,
|
||||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "messages_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX "users_username_key" ON "users"("username");
|
||||
|
||||
CREATE INDEX "chats_created_at_id_idx" ON "chats"("created_at", "id");
|
||||
|
||||
CREATE INDEX "chat_members_user_id_chat_id_idx" ON "chat_members"("user_id", "chat_id");
|
||||
|
||||
CREATE UNIQUE INDEX "chat_members_chat_id_position_key" ON "chat_members"("chat_id", "position");
|
||||
|
||||
CREATE INDEX "messages_chat_id_created_at_id_idx" ON "messages"("chat_id", "created_at", "id");
|
||||
|
||||
CREATE INDEX "messages_sender_id_idx" ON "messages"("sender_id");
|
||||
|
||||
ALTER TABLE "chat_members" ADD CONSTRAINT "chat_members_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "chat_members" ADD CONSTRAINT "chat_members_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "messages" ADD CONSTRAINT "messages_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "messages" ADD CONSTRAINT "messages_sender_id_fkey" FOREIGN KEY ("sender_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,20 @@
|
||||
ALTER TABLE "users" ADD COLUMN "password_hash" TEXT;
|
||||
|
||||
CREATE TABLE "sessions" (
|
||||
"id" UUID NOT NULL,
|
||||
"user_id" UUID NOT NULL,
|
||||
"token_hash" CHAR(64) NOT NULL,
|
||||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"expires_at" TIMESTAMPTZ(3) NOT NULL,
|
||||
"revoked_at" TIMESTAMPTZ(3),
|
||||
|
||||
CONSTRAINT "sessions_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX "sessions_token_hash_key" ON "sessions"("token_hash");
|
||||
|
||||
CREATE INDEX "sessions_user_id_idx" ON "sessions"("user_id");
|
||||
|
||||
CREATE INDEX "sessions_expires_at_idx" ON "sessions"("expires_at");
|
||||
|
||||
ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE "users" ADD COLUMN "chats_epoch" BIGINT NOT NULL DEFAULT 0;
|
||||
ALTER TABLE "chats" ADD COLUMN "messages_epoch" BIGINT NOT NULL DEFAULT 0;
|
||||
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE "chat_message_epochs" (
|
||||
"chat_id" UUID NOT NULL,
|
||||
"bucket" SMALLINT NOT NULL,
|
||||
"epoch" BIGINT NOT NULL DEFAULT 0,
|
||||
CONSTRAINT "chat_message_epochs_pkey" PRIMARY KEY ("chat_id", "bucket"),
|
||||
CONSTRAINT "chat_message_epochs_bucket_check" CHECK ("bucket" BETWEEN 0 AND 15),
|
||||
CONSTRAINT "chat_message_epochs_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE CASCADE ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
INSERT INTO "chat_message_epochs" ("chat_id", "bucket", "epoch")
|
||||
SELECT "id", 0, "messages_epoch" FROM "chats";
|
||||
|
||||
ALTER TABLE "chats" DROP COLUMN "messages_epoch";
|
||||
@@ -0,0 +1,15 @@
|
||||
CREATE TABLE "message_idempotency" (
|
||||
"user_id" UUID NOT NULL,
|
||||
"chat_id" UUID NOT NULL,
|
||||
"key" VARCHAR(128) NOT NULL,
|
||||
"message_id" UUID,
|
||||
"expires_at" TIMESTAMPTZ(3) NOT NULL,
|
||||
CONSTRAINT "message_idempotency_pkey" PRIMARY KEY ("user_id", "chat_id", "key"),
|
||||
CONSTRAINT "message_idempotency_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
CONSTRAINT "message_idempotency_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
CONSTRAINT "message_idempotency_message_id_fkey" FOREIGN KEY ("message_id") REFERENCES "messages"("id") ON DELETE CASCADE ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX "message_idempotency_expires_at_idx" ON "message_idempotency"("expires_at");
|
||||
CREATE INDEX "message_idempotency_chat_id_idx" ON "message_idempotency"("chat_id");
|
||||
CREATE INDEX "message_idempotency_message_id_idx" ON "message_idempotency"("message_id");
|
||||
@@ -0,0 +1,20 @@
|
||||
ALTER TABLE "messages" ADD COLUMN "attachment_ids" UUID[] NOT NULL DEFAULT ARRAY[]::UUID[];
|
||||
|
||||
CREATE TABLE "attachments" (
|
||||
"id" UUID NOT NULL,
|
||||
"chat_id" UUID NOT NULL,
|
||||
"owner_id" UUID NOT NULL,
|
||||
"object_key" TEXT NOT NULL,
|
||||
"filename" VARCHAR(255) NOT NULL,
|
||||
"content_type" VARCHAR(32) NOT NULL,
|
||||
"size_bytes" INTEGER NOT NULL CHECK ("size_bytes" BETWEEN 1 AND 10485760),
|
||||
"sha256" CHAR(64) NOT NULL,
|
||||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updated_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT "attachments_pkey" PRIMARY KEY ("id"),
|
||||
CONSTRAINT "attachments_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE RESTRICT ON UPDATE CASCADE,
|
||||
CONSTRAINT "attachments_owner_id_fkey" FOREIGN KEY ("owner_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE
|
||||
);
|
||||
CREATE UNIQUE INDEX "attachments_object_key_key" ON "attachments"("object_key");
|
||||
CREATE INDEX "attachments_chat_id_idx" ON "attachments"("chat_id");
|
||||
CREATE INDEX "attachments_owner_id_idx" ON "attachments"("owner_id");
|
||||
@@ -0,0 +1,20 @@
|
||||
CREATE TYPE "direct_upload_status" AS ENUM ('pending', 'completed', 'rejected', 'expired');
|
||||
CREATE TABLE "direct_uploads" (
|
||||
"id" UUID NOT NULL,
|
||||
"chat_id" UUID NOT NULL,
|
||||
"owner_id" UUID NOT NULL,
|
||||
"filename" VARCHAR(255) NOT NULL,
|
||||
"content_type" VARCHAR(32) NOT NULL,
|
||||
"size_bytes" INTEGER NOT NULL,
|
||||
"sha256" CHAR(64) NOT NULL,
|
||||
"status" "direct_upload_status" NOT NULL DEFAULT 'pending',
|
||||
"expires_at" TIMESTAMPTZ(3) NOT NULL,
|
||||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT "direct_uploads_pkey" PRIMARY KEY ("id"),
|
||||
CONSTRAINT "direct_uploads_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE RESTRICT ON UPDATE CASCADE,
|
||||
CONSTRAINT "direct_uploads_owner_id_fkey" FOREIGN KEY ("owner_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE,
|
||||
CONSTRAINT "direct_uploads_size_check" CHECK ("size_bytes" BETWEEN 1 AND 10485760)
|
||||
);
|
||||
CREATE INDEX "direct_uploads_status_expires_at_idx" ON "direct_uploads"("status", "expires_at");
|
||||
CREATE INDEX "direct_uploads_chat_id_idx" ON "direct_uploads"("chat_id");
|
||||
CREATE INDEX "direct_uploads_owner_id_idx" ON "direct_uploads"("owner_id");
|
||||
@@ -0,0 +1 @@
|
||||
provider = "postgresql"
|
||||
@@ -0,0 +1,159 @@
|
||||
generator client {
|
||||
provider = "prisma-client"
|
||||
output = "../src/generated/prisma"
|
||||
moduleFormat = "cjs"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
}
|
||||
|
||||
model User {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
username String @unique @db.VarChar(32)
|
||||
displayName String @map("display_name") @db.VarChar(100)
|
||||
passwordHash String? @map("password_hash")
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
|
||||
chatsEpoch BigInt @default(0) @map("chats_epoch")
|
||||
memberships ChatMember[]
|
||||
messages Message[]
|
||||
sessions Session[]
|
||||
idempotencyRecords MessageIdempotency[]
|
||||
attachments Attachment[]
|
||||
directUploads DirectUpload[]
|
||||
|
||||
@@map("users")
|
||||
}
|
||||
|
||||
model Session {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
userId String @map("user_id") @db.Uuid
|
||||
tokenHash String @unique @map("token_hash") @db.Char(64)
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
|
||||
expiresAt DateTime @map("expires_at") @db.Timestamptz(3)
|
||||
revokedAt DateTime? @map("revoked_at") @db.Timestamptz(3)
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@index([userId])
|
||||
@@index([expiresAt])
|
||||
@@map("sessions")
|
||||
}
|
||||
|
||||
model Chat {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
title String @db.VarChar(100)
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
|
||||
messageEpochs ChatMessageEpoch[]
|
||||
members ChatMember[]
|
||||
messages Message[]
|
||||
idempotencyRecords MessageIdempotency[]
|
||||
attachments Attachment[]
|
||||
directUploads DirectUpload[]
|
||||
|
||||
@@index([createdAt, id])
|
||||
@@map("chats")
|
||||
}
|
||||
|
||||
model ChatMember {
|
||||
chatId String @map("chat_id") @db.Uuid
|
||||
userId String @map("user_id") @db.Uuid
|
||||
position Int
|
||||
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Restrict)
|
||||
|
||||
@@id([chatId, userId])
|
||||
@@unique([chatId, position])
|
||||
@@index([userId, chatId])
|
||||
@@map("chat_members")
|
||||
}
|
||||
|
||||
model ChatMessageEpoch {
|
||||
chatId String @map("chat_id") @db.Uuid
|
||||
bucket Int @db.SmallInt
|
||||
epoch BigInt @default(0)
|
||||
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@id([chatId, bucket])
|
||||
@@map("chat_message_epochs")
|
||||
}
|
||||
|
||||
model Message {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
chatId String @map("chat_id") @db.Uuid
|
||||
senderId String @map("sender_id") @db.Uuid
|
||||
text String @db.VarChar(4000)
|
||||
attachmentIds String[] @default([]) @map("attachment_ids") @db.Uuid
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
|
||||
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
|
||||
sender User @relation(fields: [senderId], references: [id], onDelete: Restrict)
|
||||
idempotencyRecords MessageIdempotency[]
|
||||
|
||||
@@index([chatId, createdAt, id])
|
||||
@@index([senderId])
|
||||
@@map("messages")
|
||||
}
|
||||
|
||||
model Attachment {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
chatId String @map("chat_id") @db.Uuid
|
||||
ownerId String @map("owner_id") @db.Uuid
|
||||
objectKey String @unique @map("object_key")
|
||||
filename String @db.VarChar(255)
|
||||
contentType String @map("content_type") @db.VarChar(32)
|
||||
sizeBytes Int @map("size_bytes")
|
||||
sha256 String @db.Char(64)
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
|
||||
updatedAt DateTime @default(now()) @updatedAt @map("updated_at") @db.Timestamptz(3)
|
||||
chat Chat @relation(fields: [chatId], references: [id], onDelete: Restrict)
|
||||
owner User @relation(fields: [ownerId], references: [id], onDelete: Restrict)
|
||||
|
||||
@@index([chatId])
|
||||
@@index([ownerId])
|
||||
@@map("attachments")
|
||||
}
|
||||
|
||||
enum DirectUploadStatus {
|
||||
pending
|
||||
completed
|
||||
rejected
|
||||
expired
|
||||
|
||||
@@map("direct_upload_status")
|
||||
}
|
||||
|
||||
model DirectUpload {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
chatId String @map("chat_id") @db.Uuid
|
||||
ownerId String @map("owner_id") @db.Uuid
|
||||
filename String @db.VarChar(255)
|
||||
contentType String @map("content_type") @db.VarChar(32)
|
||||
sizeBytes Int @map("size_bytes")
|
||||
sha256 String @db.Char(64)
|
||||
status DirectUploadStatus @default(pending)
|
||||
expiresAt DateTime @map("expires_at") @db.Timestamptz(3)
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
|
||||
chat Chat @relation(fields: [chatId], references: [id], onDelete: Restrict)
|
||||
owner User @relation(fields: [ownerId], references: [id], onDelete: Restrict)
|
||||
|
||||
@@index([status, expiresAt])
|
||||
@@index([chatId])
|
||||
@@index([ownerId])
|
||||
@@map("direct_uploads")
|
||||
}
|
||||
|
||||
model MessageIdempotency {
|
||||
userId String @map("user_id") @db.Uuid
|
||||
chatId String @map("chat_id") @db.Uuid
|
||||
key String @db.VarChar(128)
|
||||
messageId String? @map("message_id") @db.Uuid
|
||||
expiresAt DateTime @map("expires_at") @db.Timestamptz(3)
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
|
||||
message Message? @relation(fields: [messageId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@id([userId, chatId, key])
|
||||
@@index([chatId])
|
||||
@@index([messageId])
|
||||
@@index([expiresAt])
|
||||
@@map("message_idempotency")
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
@echo off
|
||||
setlocal
|
||||
if defined GIT_BASH goto run
|
||||
set "GIT_BASH=%ProgramFiles%\Git\bin\bash.exe"
|
||||
if exist "%GIT_BASH%" goto run
|
||||
echo Git Bash was not found. Set GIT_BASH to the full path to bash.exe. 1>&2
|
||||
exit /b 2
|
||||
:run
|
||||
"%GIT_BASH%" "%~dp0restart.sh"
|
||||
exit /b %ERRORLEVEL%
|
||||
@@ -0,0 +1,23 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
project_dir=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$project_dir"
|
||||
|
||||
for service in api nginx reconciler postgres redis s3; do
|
||||
if [ -z "$(docker compose ps --all --quiet "$service")" ]; then
|
||||
printf '%s\n' "Missing container for $service; start the Compose stack first." >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
replicas=$(docker compose ps --all --quiet api | wc -l | tr -d ' ')
|
||||
if [ "$replicas" -lt 1 ]; then
|
||||
printf 'No API replicas found.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
docker compose stop --timeout 30 nginx reconciler api postgres redis s3
|
||||
docker compose up --detach --no-recreate --no-build --no-deps --wait --wait-timeout 45 postgres redis s3
|
||||
docker compose up --detach --no-recreate --no-build --no-deps --scale "api=$replicas" --wait --wait-timeout 45 api
|
||||
docker compose up --detach --no-recreate --no-build --no-deps --wait --wait-timeout 30 nginx reconciler
|
||||
@@ -0,0 +1,33 @@
|
||||
@echo off
|
||||
setlocal EnableDelayedExpansion
|
||||
cd /d "%~dp0.."
|
||||
if not defined TARGET_INSTANCE_ID (
|
||||
echo TARGET_INSTANCE_ID is required 1>&2
|
||||
exit /b 2
|
||||
)
|
||||
set "containers="
|
||||
for /f "delims=" %%C in ('docker compose ps --all --quiet api') do (
|
||||
set "containers=!containers! %%C"
|
||||
set "candidate=%%C"
|
||||
rem This Compose uses Docker's default hostname: the first 12 container-ID characters.
|
||||
if "!candidate:~0,12!"=="%TARGET_INSTANCE_ID%" (
|
||||
docker stop --time 0 %%C >nul
|
||||
if errorlevel 1 exit /b 1
|
||||
exit /b 0
|
||||
)
|
||||
)
|
||||
if not defined containers (
|
||||
echo No API containers found 1>&2
|
||||
exit /b 2
|
||||
)
|
||||
rem Fallback for explicitly configured hostnames, still scoped to this project's API containers.
|
||||
for /f "tokens=1,2 delims=:" %%H in ('docker inspect --format "{{.Config.Hostname}}:{{.Id}}" !containers!') do (
|
||||
if "%%H"=="%TARGET_INSTANCE_ID%" (
|
||||
rem Failure injection: stop immediately, leaving time for recovery within the 10-second test budget.
|
||||
docker stop --time 0 %%I >nul
|
||||
if errorlevel 1 exit /b 1
|
||||
exit /b 0
|
||||
)
|
||||
)
|
||||
echo No API container matches X-Instance-Id 1>&2
|
||||
exit /b 2
|
||||
@@ -0,0 +1,24 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
project_dir=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$project_dir"
|
||||
|
||||
target=${TARGET_INSTANCE_ID:?test provides target API instance ID}
|
||||
|
||||
containers=$(docker compose ps --all --quiet api)
|
||||
if [ -z "$containers" ]; then
|
||||
printf 'No API containers found\n' >&2
|
||||
exit 2
|
||||
fi
|
||||
for entry in $(docker inspect --format '{{.Config.Hostname}}:{{.Id}}' $containers); do
|
||||
instance=${entry%%:*}
|
||||
container=${entry#*:}
|
||||
if [ "$instance" = "$target" ]; then
|
||||
docker stop --time 8 "$container" >/dev/null
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
|
||||
printf 'No API container matches X-Instance-Id\n' >&2
|
||||
exit 2
|
||||
@@ -0,0 +1 @@
|
||||
/generated/
|
||||
@@ -0,0 +1,86 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Headers,
|
||||
HttpCode,
|
||||
Param,
|
||||
Post,
|
||||
Query,
|
||||
Req,
|
||||
StreamableFile,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
ApiBearerAuth,
|
||||
ApiBody,
|
||||
ApiConsumes,
|
||||
ApiOperation,
|
||||
ApiQuery,
|
||||
ApiTags,
|
||||
} from '@nestjs/swagger';
|
||||
import { UserGuard, type AuthenticatedRequest } from '../User/user.guard';
|
||||
import { AttachmentService } from './attachment.service';
|
||||
|
||||
@ApiTags('attachments')
|
||||
@ApiBearerAuth('bearerAuth')
|
||||
@UseGuards(UserGuard)
|
||||
@Controller('api/v1')
|
||||
export class AttachmentController {
|
||||
constructor(private readonly attachments: AttachmentService) {}
|
||||
|
||||
@Post('chats/:chat_id/attachments')
|
||||
@HttpCode(201)
|
||||
@ApiOperation({ operationId: 'uploadAttachment' })
|
||||
@ApiConsumes('image/png', 'image/jpeg')
|
||||
@ApiBody({ schema: { type: 'string', format: 'binary' } })
|
||||
@ApiQuery({ name: 'filename', required: true, type: String })
|
||||
upload(
|
||||
@Req() request: AuthenticatedRequest,
|
||||
@Param('chat_id') chatId: string,
|
||||
@Query('filename') filename: unknown,
|
||||
@Headers('content-type') contentType: unknown,
|
||||
@Body() body: unknown,
|
||||
) {
|
||||
return this.attachments.upload(
|
||||
chatId,
|
||||
request.currentUser.id,
|
||||
filename,
|
||||
contentType,
|
||||
body,
|
||||
);
|
||||
}
|
||||
|
||||
@Get('attachments/:attachment_id')
|
||||
@ApiOperation({ operationId: 'getAttachment' })
|
||||
get(
|
||||
@Req() request: AuthenticatedRequest,
|
||||
@Param('attachment_id') id: string,
|
||||
) {
|
||||
return this.attachments.get(id, request.currentUser.id);
|
||||
}
|
||||
|
||||
@Get('attachments/:attachment_id/content')
|
||||
@ApiOperation({ operationId: 'downloadAttachment' })
|
||||
@ApiQuery({
|
||||
name: 'variant',
|
||||
required: false,
|
||||
enum: ['original', 'thumbnail', 'crop', 'optimized'],
|
||||
})
|
||||
async content(
|
||||
@Req() request: AuthenticatedRequest,
|
||||
@Param('attachment_id') id: string,
|
||||
@Query('variant') variant: unknown,
|
||||
) {
|
||||
const { attachment, stream } = await this.attachments.content(
|
||||
id,
|
||||
request.currentUser.id,
|
||||
variant,
|
||||
);
|
||||
return new StreamableFile(stream, {
|
||||
type: attachment.contentType,
|
||||
length: attachment.sizeBytes,
|
||||
disposition: `attachment; filename*=UTF-8''${encodeURIComponent(attachment.filename).replace(/['()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`)}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UserModule } from '../User/user.module';
|
||||
import { StorageModule } from '../Storage/storage.module';
|
||||
import { AttachmentController } from './attachment.controller';
|
||||
import { AttachmentService } from './attachment.service';
|
||||
import { DirectUploadController } from './direct-upload.controller';
|
||||
import { DirectUploadService } from './direct-upload.service';
|
||||
|
||||
@Module({
|
||||
imports: [UserModule, StorageModule],
|
||||
controllers: [AttachmentController, DirectUploadController],
|
||||
providers: [AttachmentService, DirectUploadService],
|
||||
})
|
||||
export class AttachmentModule {}
|
||||
@@ -0,0 +1,84 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import type { PrismaService } from '../prisma/prisma.service';
|
||||
import type { StorageService } from '../Storage/storage.service';
|
||||
import { AttachmentService } from './attachment.service';
|
||||
|
||||
const chatId = '11111111-1111-4111-8111-111111111111';
|
||||
const userId = '22222222-2222-4222-8222-222222222222';
|
||||
const png = readFileSync(join(__dirname, '../../tests/fixtures/sample.png'));
|
||||
|
||||
function fixture() {
|
||||
const stored = {
|
||||
id: 'uploaded',
|
||||
chatId,
|
||||
ownerId: userId,
|
||||
objectKey: 'stored-key',
|
||||
filename: 'test.png',
|
||||
contentType: 'image/png',
|
||||
sizeBytes: png.length,
|
||||
sha256: 'checksum',
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
const tx = {
|
||||
$executeRaw: jest.fn().mockResolvedValue(0),
|
||||
$queryRaw: jest.fn().mockResolvedValue([{ locked: true }]),
|
||||
attachment: {
|
||||
create: jest.fn().mockResolvedValue(stored),
|
||||
findUnique: jest.fn().mockResolvedValue(null),
|
||||
},
|
||||
};
|
||||
const prisma = {
|
||||
chatMember: { findUnique: jest.fn().mockResolvedValue({ chatId }) },
|
||||
$transaction: jest.fn(
|
||||
async (callback: (value: typeof tx) => Promise<unknown>) => callback(tx),
|
||||
),
|
||||
};
|
||||
const storage = {
|
||||
put: jest.fn().mockResolvedValue(undefined),
|
||||
remove: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
const service = new AttachmentService(
|
||||
prisma as unknown as PrismaService,
|
||||
storage as unknown as StorageService,
|
||||
);
|
||||
return {
|
||||
stored,
|
||||
tx,
|
||||
prisma,
|
||||
storage,
|
||||
upload: () => service.upload(chatId, userId, 'test.png', 'image/png', png),
|
||||
};
|
||||
}
|
||||
|
||||
describe('upload compensation', () => {
|
||||
it('compensates a PUT whose acknowledgement was lost before metadata creation', async () => {
|
||||
const f = fixture();
|
||||
f.storage.put.mockRejectedValue(new Error('uncertain PUT'));
|
||||
await expect(f.upload()).rejects.toThrow('uncertain PUT');
|
||||
expect(f.tx.attachment.create).not.toHaveBeenCalled();
|
||||
expect(f.storage.remove).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('removes bytes after a confirmed metadata failure', async () => {
|
||||
const f = fixture();
|
||||
f.tx.attachment.create.mockRejectedValue(new Error('metadata failure'));
|
||||
await expect(f.upload()).rejects.toThrow('metadata failure');
|
||||
expect(f.storage.remove).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('preserves a committed attachment if the transaction acknowledgement was lost', async () => {
|
||||
const f = fixture();
|
||||
f.prisma.$transaction.mockImplementationOnce(async (callback) => {
|
||||
await callback(f.tx);
|
||||
throw new Error('lost commit acknowledgement');
|
||||
});
|
||||
f.tx.attachment.findUnique.mockResolvedValue(f.stored);
|
||||
await expect(f.upload()).resolves.toMatchObject({
|
||||
id: f.stored.id,
|
||||
status: 'ready',
|
||||
});
|
||||
expect(f.storage.remove).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,235 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { createHash, randomUUID } from 'node:crypto';
|
||||
import sharp from 'sharp';
|
||||
import type { Attachment } from '../generated/prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { StorageService } from '../Storage/storage.service';
|
||||
import { ApiException } from '../common/exceptions/api.exception';
|
||||
import { invalidRequest, uuid } from '../common/validation';
|
||||
import { OBJECT_TRANSACTION, tryAttachmentObjectLock } from './object-lock';
|
||||
import { profileStage } from '../common/http/upload-memory-profile';
|
||||
|
||||
function limit(name: string, maximum: number): number {
|
||||
const value = Number(process.env[name] ?? maximum);
|
||||
if (!Number.isSafeInteger(value) || value < 1 || value > maximum) {
|
||||
throw new Error(`${name} must be between 1 and ${maximum}`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export const MAX_UPLOAD_BYTES = limit('MAX_UPLOAD_BYTES', 10485760);
|
||||
const MAX_IMAGE_PIXELS = limit('MAX_IMAGE_PIXELS', 40000000);
|
||||
|
||||
@Injectable()
|
||||
export class AttachmentService {
|
||||
private readonly logger = new Logger(AttachmentService.name);
|
||||
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly storage: StorageService,
|
||||
) {}
|
||||
|
||||
async upload(
|
||||
chatValue: unknown,
|
||||
userId: string,
|
||||
filename: unknown,
|
||||
mime: unknown,
|
||||
body: unknown,
|
||||
) {
|
||||
profileStage('authorize_and_validate_fields');
|
||||
const chatId = uuid(chatValue, 'chat_id');
|
||||
await this.authorizeChat(chatId, userId);
|
||||
const fields = this.validateFields(filename, mime);
|
||||
if (!Buffer.isBuffer(body) || body.length === 0)
|
||||
throw new ApiException(
|
||||
415,
|
||||
'unsupported_media_type',
|
||||
'Ожидается PNG или JPEG',
|
||||
);
|
||||
if (body.length > MAX_UPLOAD_BYTES)
|
||||
throw new ApiException(413, 'payload_too_large', 'Превышен лимит файла');
|
||||
const contentType = fields.contentType;
|
||||
await this.validateImage(body, contentType);
|
||||
const id = randomUUID();
|
||||
const objectKey = `originals/${chatId}/${id}`;
|
||||
try {
|
||||
profileStage('db_lock');
|
||||
const stored = await this.prisma.$transaction(async (tx) => {
|
||||
if (!(await tryAttachmentObjectLock(tx, objectKey))) {
|
||||
throw new ApiException(503, 'unavailable', 'Объект занят');
|
||||
}
|
||||
profileStage('s3_put');
|
||||
await this.storage.put(objectKey, body, contentType);
|
||||
profileStage('sha256');
|
||||
const sha256 = createHash('sha256').update(body).digest('hex');
|
||||
profileStage('db_insert_and_commit');
|
||||
return tx.attachment.create({
|
||||
data: {
|
||||
id,
|
||||
chatId,
|
||||
ownerId: userId,
|
||||
objectKey,
|
||||
filename: fields.filename,
|
||||
contentType,
|
||||
sizeBytes: body.length,
|
||||
sha256,
|
||||
},
|
||||
});
|
||||
}, OBJECT_TRANSACTION);
|
||||
profileStage('serialize_response');
|
||||
return this.serialize(stored);
|
||||
} catch (error) {
|
||||
// A database timeout can occur after commit. Never delete a referenced object.
|
||||
try {
|
||||
const committed = await this.prisma.$transaction(async (tx) => {
|
||||
if (!(await tryAttachmentObjectLock(tx, objectKey))) return null;
|
||||
const record = await tx.attachment.findUnique({
|
||||
where: { objectKey },
|
||||
});
|
||||
if (record) return record;
|
||||
await this.storage.remove(objectKey);
|
||||
return null;
|
||||
}, OBJECT_TRANSACTION);
|
||||
if (committed) return this.serialize(committed);
|
||||
} catch {
|
||||
this.logger.error(
|
||||
{ attachmentId: id },
|
||||
'Upload requires reconciliation',
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async get(id: unknown, userId: string) {
|
||||
return this.serialize(await this.authorizeAttachment(id, userId));
|
||||
}
|
||||
|
||||
async content(id: unknown, userId: string, variant: unknown) {
|
||||
const attachment = await this.authorizeAttachment(id, userId);
|
||||
if (variant !== undefined && variant !== 'original') {
|
||||
if (
|
||||
typeof variant !== 'string' ||
|
||||
!['thumbnail', 'crop', 'optimized'].includes(variant)
|
||||
) {
|
||||
return invalidRequest('Некорректный variant');
|
||||
}
|
||||
throw new ApiException(404, 'not_found', 'Вариант не найден');
|
||||
}
|
||||
return { attachment, stream: await this.storage.get(attachment.objectKey) };
|
||||
}
|
||||
|
||||
validateFields(filename: unknown, mime: unknown) {
|
||||
if (
|
||||
typeof filename !== 'string' ||
|
||||
filename.trim().length === 0 ||
|
||||
Array.from(filename).length > 255 ||
|
||||
Array.from(filename).some((character) => {
|
||||
const point = character.codePointAt(0)!;
|
||||
return (
|
||||
character === '/' ||
|
||||
character === '\\' ||
|
||||
point < 32 ||
|
||||
(point >= 127 && point <= 159)
|
||||
);
|
||||
})
|
||||
) {
|
||||
return invalidRequest('Некорректный filename');
|
||||
}
|
||||
const contentType =
|
||||
typeof mime === 'string' ? mime.split(';')[0].trim().toLowerCase() : '';
|
||||
if (contentType !== 'image/png' && contentType !== 'image/jpeg') {
|
||||
throw new ApiException(
|
||||
415,
|
||||
'unsupported_media_type',
|
||||
'Ожидается PNG или JPEG',
|
||||
);
|
||||
}
|
||||
return { filename, contentType };
|
||||
}
|
||||
|
||||
async authorizeChat(chatId: string, userId: string) {
|
||||
const member = await this.prisma.chatMember.findUnique({
|
||||
where: { chatId_userId: { chatId, userId } },
|
||||
select: { chatId: true },
|
||||
});
|
||||
if (!member) throw new ApiException(404, 'not_found', 'Объект не найден');
|
||||
}
|
||||
|
||||
private async authorizeAttachment(value: unknown, userId: string) {
|
||||
const id = uuid(value, 'attachment_id');
|
||||
const attachment = await this.prisma.attachment.findFirst({
|
||||
where: { id, chat: { members: { some: { userId } } } },
|
||||
});
|
||||
if (!attachment)
|
||||
throw new ApiException(404, 'not_found', 'Объект не найден');
|
||||
return attachment;
|
||||
}
|
||||
|
||||
async validateImage(body: Buffer | string, contentType: string) {
|
||||
try {
|
||||
profileStage('sharp_metadata');
|
||||
// metadata reads headers without decoding pixels; check dimensions before full validation.
|
||||
const metadata = await sharp(body, {
|
||||
limitInputPixels: false,
|
||||
failOn: 'warning',
|
||||
}).metadata();
|
||||
const actualType =
|
||||
metadata.format === 'png'
|
||||
? 'image/png'
|
||||
: metadata.format === 'jpeg'
|
||||
? 'image/jpeg'
|
||||
: null;
|
||||
if (
|
||||
actualType !== contentType ||
|
||||
!metadata.width ||
|
||||
!metadata.height ||
|
||||
(metadata.pages ?? 1) > 1
|
||||
) {
|
||||
throw new ApiException(
|
||||
415,
|
||||
'unsupported_media_type',
|
||||
'Некорректное изображение',
|
||||
);
|
||||
}
|
||||
if (metadata.width * metadata.height > MAX_IMAGE_PIXELS) {
|
||||
throw new ApiException(
|
||||
413,
|
||||
'payload_too_large',
|
||||
'Превышен лимит пикселей',
|
||||
);
|
||||
}
|
||||
profileStage('sharp_decode_stats');
|
||||
await sharp(body, {
|
||||
limitInputPixels: MAX_IMAGE_PIXELS,
|
||||
failOn: 'warning',
|
||||
}).stats();
|
||||
profileStage('image_validated');
|
||||
} catch (cause) {
|
||||
if (cause instanceof ApiException) throw cause;
|
||||
throw new ApiException(
|
||||
415,
|
||||
'unsupported_media_type',
|
||||
'Некорректное изображение',
|
||||
{ cause },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
serialize(value: Attachment) {
|
||||
return {
|
||||
id: value.id,
|
||||
chat_id: value.chatId,
|
||||
owner_id: value.ownerId,
|
||||
filename: value.filename,
|
||||
content_type: value.contentType,
|
||||
size_bytes: value.sizeBytes,
|
||||
sha256: value.sha256,
|
||||
status: 'ready',
|
||||
variants: [],
|
||||
error_code: null,
|
||||
created_at: value.createdAt.toISOString(),
|
||||
updated_at: value.updatedAt.toISOString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
HttpCode,
|
||||
Header,
|
||||
Param,
|
||||
Post,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
ApiBearerAuth,
|
||||
ApiBody,
|
||||
ApiOperation,
|
||||
ApiParam,
|
||||
ApiResponse,
|
||||
ApiTags,
|
||||
} from '@nestjs/swagger';
|
||||
import { UserGuard, type AuthenticatedRequest } from '../User/user.guard';
|
||||
import { DirectUploadService } from './direct-upload.service';
|
||||
|
||||
@ApiTags('direct uploads')
|
||||
@ApiBearerAuth('bearerAuth')
|
||||
@UseGuards(UserGuard)
|
||||
@Controller('api/v1/chats/:chat_id/attachment-uploads')
|
||||
@ApiParam({ name: 'chat_id', type: 'string', format: 'uuid', required: true })
|
||||
@ApiResponse({
|
||||
status: 401,
|
||||
description: 'Требуется Bearer-сессия',
|
||||
schema: { $ref: '#/components/schemas/Error' },
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 404,
|
||||
description: 'Чат/разрешение недоступно пользователю',
|
||||
schema: { $ref: '#/components/schemas/Error' },
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 503,
|
||||
description: 'БД или хранилище временно недоступно',
|
||||
schema: { $ref: '#/components/schemas/Error' },
|
||||
})
|
||||
export class DirectUploadController {
|
||||
constructor(private readonly uploads: DirectUploadService) {}
|
||||
|
||||
@Post()
|
||||
@HttpCode(201)
|
||||
@Header('Cache-Control', 'no-store')
|
||||
@ApiOperation({
|
||||
operationId: 'initiateDirectUpload',
|
||||
summary: 'Выдать короткоживущее разрешение PUT в staging',
|
||||
})
|
||||
@ApiBody({
|
||||
schema: {
|
||||
type: 'object',
|
||||
additionalProperties: false,
|
||||
required: ['filename', 'content_type', 'size_bytes', 'sha256'],
|
||||
properties: {
|
||||
filename: { type: 'string', minLength: 1, maxLength: 255 },
|
||||
content_type: { type: 'string', enum: ['image/png', 'image/jpeg'] },
|
||||
size_bytes: { type: 'integer', minimum: 1, maximum: 10485760 },
|
||||
sha256: { type: 'string', pattern: '^[a-f0-9]{64}$' },
|
||||
expires_in: {
|
||||
type: 'integer',
|
||||
minimum: 1,
|
||||
maximum: 300,
|
||||
description:
|
||||
'Не больше серверного DIRECT_UPLOAD_TTL_SECONDS (60 по умолчанию)',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 201,
|
||||
description: 'PUT URL и обязательные заголовки; URL не логировать',
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: [
|
||||
'upload_id',
|
||||
'expires_at',
|
||||
'method',
|
||||
'url',
|
||||
'headers',
|
||||
'complete_url',
|
||||
],
|
||||
properties: {
|
||||
upload_id: { type: 'string', format: 'uuid' },
|
||||
expires_at: { type: 'string', format: 'date-time' },
|
||||
method: { type: 'string', enum: ['PUT'] },
|
||||
url: { type: 'string', format: 'uri' },
|
||||
headers: { type: 'object', additionalProperties: { type: 'string' } },
|
||||
complete_url: { type: 'string' },
|
||||
},
|
||||
},
|
||||
})
|
||||
@ApiResponse({ status: 400, description: 'Некорректные поля разрешения' })
|
||||
@ApiResponse({ status: 413, description: 'Запрошен размер больше 10 MiB' })
|
||||
@ApiResponse({ status: 415, description: 'Разрешены только PNG и JPEG' })
|
||||
initiate(
|
||||
@Req() request: AuthenticatedRequest,
|
||||
@Param('chat_id') chatId: string,
|
||||
@Body() body: unknown,
|
||||
) {
|
||||
return this.uploads.initiate(chatId, request.currentUser.id, body);
|
||||
}
|
||||
|
||||
@Post(':upload_id/complete')
|
||||
@HttpCode(201)
|
||||
@ApiParam({
|
||||
name: 'upload_id',
|
||||
type: 'string',
|
||||
format: 'uuid',
|
||||
required: true,
|
||||
})
|
||||
@ApiBody({
|
||||
required: false,
|
||||
schema: { type: 'object', additionalProperties: false, properties: {} },
|
||||
})
|
||||
@ApiOperation({
|
||||
operationId: 'completeDirectUpload',
|
||||
summary:
|
||||
'Проверить байты и опубликовать вложение; идемпотентно для владельца',
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 201,
|
||||
description: 'Готовое Attachment; повтор возвращает то же вложение',
|
||||
schema: { $ref: '#/components/schemas/Attachment' },
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 400,
|
||||
description: 'Размер/checksum не совпадает или некорректные параметры',
|
||||
})
|
||||
@ApiResponse({ status: 404, description: 'Чужая или неизвестная загрузка' })
|
||||
@ApiResponse({
|
||||
status: 409,
|
||||
description:
|
||||
'Истёкшее/отклонённое разрешение, нет файла или complete выполняется другой репликой',
|
||||
})
|
||||
@ApiResponse({ status: 413, description: 'Превышен лимит bytes/pixels' })
|
||||
@ApiResponse({
|
||||
status: 415,
|
||||
description: 'Повреждённое изображение или неверный MIME',
|
||||
})
|
||||
complete(
|
||||
@Req() request: AuthenticatedRequest,
|
||||
@Param('chat_id') chatId: string,
|
||||
@Param('upload_id') uploadId: string,
|
||||
@Body() body: unknown,
|
||||
) {
|
||||
return this.uploads.complete(
|
||||
chatId,
|
||||
uploadId,
|
||||
request.currentUser.id,
|
||||
body,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import type { PrismaService } from '../prisma/prisma.service';
|
||||
import type { StorageService } from '../Storage/storage.service';
|
||||
import type { AttachmentService } from './attachment.service';
|
||||
import { DirectUploadService } from './direct-upload.service';
|
||||
import { ApiException } from '../common/exceptions/api.exception';
|
||||
|
||||
function fixture() {
|
||||
const upload = {
|
||||
id: randomUUID(),
|
||||
chatId: randomUUID(),
|
||||
ownerId: randomUUID(),
|
||||
filename: 'photo.png',
|
||||
contentType: 'image/png',
|
||||
sizeBytes: 200,
|
||||
sha256: 'a'.repeat(64),
|
||||
expiresAt: new Date(Date.now() + 60000),
|
||||
status: 'pending',
|
||||
};
|
||||
let saved: unknown = null;
|
||||
const db = {
|
||||
directUpload: {
|
||||
create: jest.fn().mockResolvedValue(upload),
|
||||
findFirst: jest.fn().mockResolvedValue(upload),
|
||||
update: jest.fn(async ({ data }: { data: { status: string } }) =>
|
||||
Object.assign(upload, data),
|
||||
),
|
||||
},
|
||||
attachment: {
|
||||
findUnique: jest.fn(async () => saved),
|
||||
create: jest.fn(async ({ data }: { data: Record<string, unknown> }) => {
|
||||
saved = { ...data, createdAt: new Date(), updatedAt: new Date() };
|
||||
return saved;
|
||||
}),
|
||||
},
|
||||
$executeRaw: jest.fn().mockResolvedValue(0),
|
||||
$queryRaw: jest.fn().mockResolvedValue([{ locked: true }]),
|
||||
$transaction: jest.fn(),
|
||||
};
|
||||
db.$transaction.mockImplementation((fn: (tx: unknown) => unknown) => fn(db));
|
||||
const storage = {
|
||||
presignPut: jest
|
||||
.fn()
|
||||
.mockResolvedValue({ url: 'http://example.test/upload', headers: {} }),
|
||||
inspectToFile: jest.fn().mockResolvedValue('"etag"'),
|
||||
publishCopy: jest.fn().mockResolvedValue(undefined),
|
||||
remove: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
const attachments = {
|
||||
authorizeChat: jest.fn().mockResolvedValue(undefined),
|
||||
validateFields: jest
|
||||
.fn()
|
||||
.mockReturnValue({
|
||||
filename: upload.filename,
|
||||
contentType: upload.contentType,
|
||||
}),
|
||||
validateImage: jest.fn().mockResolvedValue(undefined),
|
||||
serialize: jest.fn((value: unknown) => value),
|
||||
};
|
||||
const service = new DirectUploadService(
|
||||
db as unknown as PrismaService,
|
||||
storage as unknown as StorageService,
|
||||
attachments as unknown as AttachmentService,
|
||||
);
|
||||
const complete = () =>
|
||||
service.complete(upload.chatId, upload.id, upload.ownerId, undefined);
|
||||
return { upload, db, storage, attachments, service, complete };
|
||||
}
|
||||
|
||||
describe('direct upload publication', () => {
|
||||
it('uses immutable expected fields and a short server-bounded grant', async () => {
|
||||
const f = fixture();
|
||||
await expect(
|
||||
f.service.initiate(f.upload.chatId, f.upload.ownerId, {
|
||||
filename: 'photo.png',
|
||||
content_type: 'image/png',
|
||||
size_bytes: 200,
|
||||
sha256: 'a'.repeat(64),
|
||||
expires_in: 1,
|
||||
}),
|
||||
).resolves.toMatchObject({ upload_id: f.upload.id, method: 'PUT' });
|
||||
expect(f.storage.presignPut).toHaveBeenCalledWith(
|
||||
`staging/${f.upload.chatId}/${f.upload.id}`,
|
||||
200,
|
||||
'image/png',
|
||||
'a'.repeat(64),
|
||||
1,
|
||||
expect.any(Date),
|
||||
);
|
||||
await expect(
|
||||
f.service.initiate(f.upload.chatId, f.upload.ownerId, {
|
||||
filename: 'photo.png',
|
||||
content_type: 'image/png',
|
||||
size_bytes: 10485761,
|
||||
sha256: 'a'.repeat(64),
|
||||
}),
|
||||
).rejects.toMatchObject({ status: 413 });
|
||||
});
|
||||
|
||||
it('publishes through copy and returns the same attachment on replay without trusting staging again', async () => {
|
||||
const f = fixture();
|
||||
const first = await f.complete();
|
||||
expect(await f.complete()).toEqual(first);
|
||||
expect(f.storage.inspectToFile).toHaveBeenCalledTimes(1);
|
||||
expect(f.storage.publishCopy).toHaveBeenCalledWith(
|
||||
`staging/${f.upload.chatId}/${f.upload.id}`,
|
||||
`originals/${f.upload.chatId}/${f.upload.id}`,
|
||||
'"etag"',
|
||||
);
|
||||
expect(f.db.attachment.create).toHaveBeenCalledTimes(1);
|
||||
expect(f.upload.status).toBe('completed');
|
||||
});
|
||||
|
||||
it('commits a rejected reservation when image validation fails, publishing no metadata', async () => {
|
||||
const f = fixture();
|
||||
f.attachments.validateImage.mockRejectedValue(
|
||||
new ApiException(415, 'unsupported_media_type', 'invalid image'),
|
||||
);
|
||||
await expect(f.complete()).rejects.toMatchObject({ status: 415 });
|
||||
expect(f.upload.status).toBe('rejected');
|
||||
expect(f.db.attachment.create).not.toHaveBeenCalled();
|
||||
expect(f.storage.publishCopy).not.toHaveBeenCalled();
|
||||
await expect(f.complete()).rejects.toMatchObject({ status: 409 });
|
||||
});
|
||||
|
||||
it('expires pending reservations without downloading their objects', async () => {
|
||||
const f = fixture();
|
||||
f.upload.expiresAt = new Date(Date.now() - 1000);
|
||||
await expect(f.complete()).rejects.toMatchObject({ status: 409 });
|
||||
expect(f.upload.status).toBe('expired');
|
||||
expect(f.storage.inspectToFile).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not reject an intent for an infrastructure failure or a missing upload', async () => {
|
||||
const f = fixture();
|
||||
f.storage.inspectToFile.mockRejectedValue(
|
||||
new ApiException(503, 'unavailable', 'storage unavailable'),
|
||||
);
|
||||
await expect(f.complete()).rejects.toMatchObject({ status: 503 });
|
||||
expect(f.upload.status).toBe('pending');
|
||||
expect(f.db.attachment.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not fetch an object for a foreign reservation or simultaneous completion', async () => {
|
||||
const f = fixture();
|
||||
f.db.directUpload.findFirst.mockResolvedValueOnce(null);
|
||||
await expect(f.complete()).rejects.toMatchObject({ status: 404 });
|
||||
f.db.$queryRaw.mockResolvedValueOnce([{ locked: false }]);
|
||||
await expect(f.complete()).rejects.toMatchObject({ status: 409 });
|
||||
expect(f.storage.inspectToFile).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,225 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { mkdtemp, rm, rmdir } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import type { Attachment } from '../generated/prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { StorageService } from '../Storage/storage.service';
|
||||
import { ApiException } from '../common/exceptions/api.exception';
|
||||
import { invalidRequest, objectBody, uuid } from '../common/validation';
|
||||
import { AttachmentService, MAX_UPLOAD_BYTES } from './attachment.service';
|
||||
import { OBJECT_TRANSACTION, tryAttachmentObjectLock } from './object-lock';
|
||||
import { profileStage } from '../common/http/upload-memory-profile';
|
||||
|
||||
type Outcome = { attachment: Attachment } | { error: ApiException };
|
||||
|
||||
@Injectable()
|
||||
export class DirectUploadService {
|
||||
private readonly logger = new Logger(DirectUploadService.name);
|
||||
private readonly maxTtl = Number(process.env.DIRECT_UPLOAD_TTL_SECONDS ?? 60);
|
||||
|
||||
constructor(
|
||||
private readonly db: PrismaService,
|
||||
private readonly storage: StorageService,
|
||||
private readonly attachments: AttachmentService,
|
||||
) {
|
||||
if (
|
||||
!Number.isSafeInteger(this.maxTtl) ||
|
||||
this.maxTtl < 1 ||
|
||||
this.maxTtl > 300
|
||||
) {
|
||||
throw new Error('DIRECT_UPLOAD_TTL_SECONDS must be between 1 and 300');
|
||||
}
|
||||
}
|
||||
|
||||
async initiate(chatValue: unknown, userId: string, value: unknown) {
|
||||
const chatId = uuid(chatValue, 'chat_id');
|
||||
await this.attachments.authorizeChat(chatId, userId);
|
||||
const body = objectBody(value, [
|
||||
'filename',
|
||||
'content_type',
|
||||
'size_bytes',
|
||||
'sha256',
|
||||
'expires_in',
|
||||
]);
|
||||
const fields = this.attachments.validateFields(
|
||||
body.filename,
|
||||
body.content_type,
|
||||
);
|
||||
if (
|
||||
typeof body.size_bytes !== 'number' ||
|
||||
!Number.isSafeInteger(body.size_bytes) ||
|
||||
body.size_bytes < 1
|
||||
)
|
||||
invalidRequest('Некорректный size_bytes');
|
||||
if (body.size_bytes > MAX_UPLOAD_BYTES)
|
||||
throw new ApiException(413, 'payload_too_large', 'Превышен лимит файла');
|
||||
if (typeof body.sha256 !== 'string' || !/^[a-f0-9]{64}$/.test(body.sha256))
|
||||
invalidRequest('Ожидается lowercase SHA-256');
|
||||
const ttl = body.expires_in === undefined ? this.maxTtl : body.expires_in;
|
||||
if (
|
||||
typeof ttl !== 'number' ||
|
||||
!Number.isSafeInteger(ttl) ||
|
||||
ttl < 1 ||
|
||||
ttl > this.maxTtl
|
||||
)
|
||||
invalidRequest('Некорректный expires_in');
|
||||
// SigV4 timestamps have second precision; report the same expiry to the client.
|
||||
const now = new Date(Math.floor(Date.now() / 1000) * 1000);
|
||||
const upload = await this.db.directUpload.create({
|
||||
data: {
|
||||
chatId,
|
||||
ownerId: userId,
|
||||
filename: fields.filename,
|
||||
contentType: fields.contentType,
|
||||
sizeBytes: body.size_bytes,
|
||||
sha256: body.sha256,
|
||||
expiresAt: new Date(now.getTime() + ttl * 1000),
|
||||
},
|
||||
});
|
||||
const signed = await this.storage.presignPut(
|
||||
`staging/${chatId}/${upload.id}`,
|
||||
upload.sizeBytes,
|
||||
upload.contentType,
|
||||
upload.sha256,
|
||||
ttl,
|
||||
now,
|
||||
);
|
||||
return {
|
||||
upload_id: upload.id,
|
||||
expires_at: upload.expiresAt.toISOString(),
|
||||
method: 'PUT' as const,
|
||||
...signed,
|
||||
complete_url: `/api/v1/chats/${chatId}/attachment-uploads/${upload.id}/complete`,
|
||||
};
|
||||
}
|
||||
|
||||
async complete(
|
||||
chatValue: unknown,
|
||||
uploadValue: unknown,
|
||||
userId: string,
|
||||
body: unknown,
|
||||
) {
|
||||
profileStage('authorize_and_lookup');
|
||||
const chatId = uuid(chatValue, 'chat_id');
|
||||
const id = uuid(uploadValue, 'upload_id');
|
||||
if (body !== undefined) objectBody(body, []);
|
||||
await this.attachments.authorizeChat(chatId, userId);
|
||||
const where = { id, chatId, ownerId: userId };
|
||||
if (!(await this.db.directUpload.findFirst({ where })))
|
||||
throw new ApiException(404, 'not_found', 'Объект не найден');
|
||||
const stagingKey = `staging/${chatId}/${id}`;
|
||||
const originalKey = `originals/${chatId}/${id}`;
|
||||
const directory = await mkdtemp(join(tmpdir(), 'pe-direct-'));
|
||||
const file = join(directory, 'image');
|
||||
let outcome: Outcome;
|
||||
try {
|
||||
outcome = await this.db.$transaction(async (tx): Promise<Outcome> => {
|
||||
if (
|
||||
!(await tryAttachmentObjectLock(tx, stagingKey)) ||
|
||||
!(await tryAttachmentObjectLock(tx, originalKey))
|
||||
) {
|
||||
throw new ApiException(
|
||||
409,
|
||||
'conflict',
|
||||
'Загрузка уже завершается; повторите complete',
|
||||
);
|
||||
}
|
||||
const upload = await tx.directUpload.findFirst({ where });
|
||||
if (!upload)
|
||||
throw new ApiException(404, 'not_found', 'Объект не найден');
|
||||
const existing = await tx.attachment.findUnique({ where: { id } });
|
||||
if (upload.status === 'completed') {
|
||||
if (!existing)
|
||||
throw new ApiException(
|
||||
503,
|
||||
'unavailable',
|
||||
'Метаданные завершённой загрузки недоступны',
|
||||
);
|
||||
return { attachment: existing };
|
||||
}
|
||||
if (upload.status !== 'pending')
|
||||
return {
|
||||
error: new ApiException(
|
||||
409,
|
||||
'conflict',
|
||||
'Разрешение загрузки больше не действует',
|
||||
),
|
||||
};
|
||||
if (upload.expiresAt <= new Date()) {
|
||||
await tx.directUpload.update({
|
||||
where: { id },
|
||||
data: { status: 'expired' },
|
||||
});
|
||||
return {
|
||||
error: new ApiException(
|
||||
409,
|
||||
'conflict',
|
||||
'Разрешение загрузки истекло',
|
||||
),
|
||||
};
|
||||
}
|
||||
if (existing)
|
||||
throw new ApiException(
|
||||
503,
|
||||
'unavailable',
|
||||
'Конфликт идентификатора вложения',
|
||||
);
|
||||
let etag: string;
|
||||
try {
|
||||
profileStage('s3_get_headers');
|
||||
etag = await this.storage.inspectToFile(stagingKey, file, upload);
|
||||
await this.attachments.validateImage(file, upload.contentType);
|
||||
} catch (error) {
|
||||
if (
|
||||
!(error instanceof ApiException) ||
|
||||
![400, 413, 415].includes(error.getStatus())
|
||||
)
|
||||
throw error;
|
||||
// Commit rejection before reporting a permanent validation error. No attachment is published.
|
||||
await tx.directUpload.update({
|
||||
where: { id },
|
||||
data: { status: 'rejected' },
|
||||
});
|
||||
return { error };
|
||||
}
|
||||
// The signed PUT is create-only. Conditional copy also rejects a changed source.
|
||||
// Readers can only see the immutable original, never the still-signed staging key.
|
||||
profileStage('s3_copy');
|
||||
await this.storage.publishCopy(stagingKey, originalKey, etag);
|
||||
profileStage('db_insert_and_commit');
|
||||
const attachment = await tx.attachment.create({
|
||||
data: {
|
||||
id,
|
||||
chatId,
|
||||
ownerId: userId,
|
||||
objectKey: originalKey,
|
||||
filename: upload.filename,
|
||||
contentType: upload.contentType,
|
||||
sizeBytes: upload.sizeBytes,
|
||||
sha256: upload.sha256,
|
||||
},
|
||||
});
|
||||
await tx.directUpload.update({
|
||||
where: { id },
|
||||
data: { status: 'completed' },
|
||||
});
|
||||
return { attachment };
|
||||
}, OBJECT_TRANSACTION);
|
||||
} finally {
|
||||
profileStage('temp_file_cleanup');
|
||||
await rm(file, { force: true });
|
||||
await rmdir(directory);
|
||||
}
|
||||
if ('error' in outcome) throw outcome.error;
|
||||
// Failure or a late replay of PUT is harmless: the cleaner removes expired staging later.
|
||||
try {
|
||||
profileStage('staging_delete');
|
||||
await this.storage.remove(stagingKey);
|
||||
} catch {
|
||||
this.logger.warn('Completed upload staging cleanup deferred');
|
||||
}
|
||||
profileStage('serialize_response');
|
||||
return this.attachments.serialize(outcome.attachment);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Prisma } from '../generated/prisma/client';
|
||||
|
||||
export const OBJECT_TRANSACTION = { maxWait: 5000, timeout: 20000 };
|
||||
|
||||
/** All writers and cleaners use the same transaction-scoped lock for an immutable key. */
|
||||
export async function tryAttachmentObjectLock(
|
||||
tx: Prisma.TransactionClient,
|
||||
objectKey: string,
|
||||
): Promise<boolean> {
|
||||
// A suspended/crashed writer must not keep an idle transaction open indefinitely.
|
||||
await tx.$executeRaw`SET LOCAL idle_in_transaction_session_timeout = '15s'`;
|
||||
const [result] = await tx.$queryRaw<{ locked: boolean }[]>(Prisma.sql`
|
||||
SELECT pg_try_advisory_xact_lock(hashtextextended(${`attachment:${objectKey}`}, 0)) AS locked
|
||||
`);
|
||||
return result.locked;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { readFileSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
|
||||
const file = join(tmpdir(), 'mtusi2026lab4-reconciler-success');
|
||||
|
||||
export function clearReconcileHealth(): void {
|
||||
rmSync(file, { force: true });
|
||||
}
|
||||
|
||||
export function markReconcileHealth(): void {
|
||||
writeFileSync(file, String(Date.now()), { mode: 0o600 });
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
try {
|
||||
const age = Date.now() - Number(readFileSync(file, 'utf8'));
|
||||
const interval = Number(process.env.RECONCILE_INTERVAL_SECONDS ?? 60);
|
||||
process.exitCode =
|
||||
Number.isFinite(age) &&
|
||||
age >= 0 &&
|
||||
Number.isFinite(interval) &&
|
||||
interval > 0 &&
|
||||
age <= (interval + 120) * 1000
|
||||
? 0
|
||||
: 1;
|
||||
} catch {
|
||||
process.exitCode = 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import { uuidPattern } from '../common/validation';
|
||||
|
||||
export interface ReconcileOptions {
|
||||
apply: boolean;
|
||||
graceSeconds: number;
|
||||
prefix: string;
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
|
||||
export function positiveSeconds(value: string, name: string): number {
|
||||
if (!/^[1-9]\d*$/.test(value))
|
||||
throw new Error(`${name} must be a positive integer`);
|
||||
const seconds = Number(value);
|
||||
if (!Number.isSafeInteger(seconds) || seconds > 2147483) {
|
||||
throw new Error(`${name} is too large`);
|
||||
}
|
||||
return seconds;
|
||||
}
|
||||
|
||||
export function managedObjectKey(key: string): boolean {
|
||||
const parts = key.split('/');
|
||||
return (
|
||||
parts.length === 3 &&
|
||||
parts[0] === 'originals' &&
|
||||
uuidPattern.test(parts[1]) &&
|
||||
uuidPattern.test(parts[2])
|
||||
);
|
||||
}
|
||||
|
||||
export function reconcileOptions(args: string[], env = process.env) {
|
||||
let apply = false;
|
||||
let dryRun = false;
|
||||
let watch = false;
|
||||
let prefix = 'originals/';
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
switch (args[i]) {
|
||||
case '--apply':
|
||||
apply = true;
|
||||
break;
|
||||
case '--dry-run':
|
||||
dryRun = true;
|
||||
break;
|
||||
case '--watch':
|
||||
watch = true;
|
||||
break;
|
||||
case '--prefix': {
|
||||
prefix = args[++i] ?? '';
|
||||
const parts = prefix.split('/');
|
||||
if (
|
||||
prefix !== 'originals/' &&
|
||||
!(
|
||||
parts.length === 3 &&
|
||||
parts[0] === 'originals' &&
|
||||
uuidPattern.test(parts[1]) &&
|
||||
parts[2] === ''
|
||||
)
|
||||
) {
|
||||
throw new Error(
|
||||
'Prefix must be originals/ or originals/<chat UUID>/',
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new Error(`Unknown argument: ${args[i]}`);
|
||||
}
|
||||
}
|
||||
if (apply && dryRun)
|
||||
throw new Error('--apply and --dry-run are mutually exclusive');
|
||||
return {
|
||||
apply,
|
||||
watch,
|
||||
prefix,
|
||||
graceSeconds: positiveSeconds(
|
||||
env.ORPHAN_GRACE_SECONDS ?? '600',
|
||||
'ORPHAN_GRACE_SECONDS',
|
||||
),
|
||||
intervalSeconds: positiveSeconds(
|
||||
env.RECONCILE_INTERVAL_SECONDS ?? '60',
|
||||
'RECONCILE_INTERVAL_SECONDS',
|
||||
),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import 'reflect-metadata';
|
||||
import { setTimeout as delay } from 'node:timers/promises';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { StorageService } from '../Storage/storage.service';
|
||||
import { reconcileOptions } from './reconcile-options';
|
||||
import { ReconcileService } from './reconcile.service';
|
||||
import { clearReconcileHealth, markReconcileHealth } from './reconcile-health';
|
||||
|
||||
async function main() {
|
||||
const options = reconcileOptions(process.argv.slice(2));
|
||||
if (options.watch) clearReconcileHealth();
|
||||
const controller = new AbortController();
|
||||
const stop = () => controller.abort();
|
||||
process.once('SIGTERM', stop);
|
||||
process.once('SIGINT', stop);
|
||||
const prisma = new PrismaService();
|
||||
let storage: StorageService | undefined;
|
||||
const output = (event: object) => console.log(JSON.stringify(event));
|
||||
try {
|
||||
storage = new StorageService();
|
||||
await prisma.onModuleInit();
|
||||
const service = new ReconcileService(prisma, storage, output);
|
||||
do {
|
||||
try {
|
||||
const report = await service.run({
|
||||
...options,
|
||||
signal: controller.signal,
|
||||
});
|
||||
if (options.watch) markReconcileHealth();
|
||||
output({ event: 'reconcile_summary', ...report });
|
||||
if (!options.watch && report.missingObjects > 0) process.exitCode = 2;
|
||||
} catch (error) {
|
||||
if (controller.signal.aborted) break;
|
||||
if (options.watch) clearReconcileHealth();
|
||||
output({
|
||||
event: 'reconcile_failed',
|
||||
errorType: error instanceof Error ? error.name : 'UnknownError',
|
||||
});
|
||||
if (!options.watch) {
|
||||
process.exitCode = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!options.watch || controller.signal.aborted) break;
|
||||
await delay(options.intervalSeconds * 1000, undefined, {
|
||||
signal: controller.signal,
|
||||
}).catch((error: unknown) => {
|
||||
if (!controller.signal.aborted) throw error;
|
||||
});
|
||||
} while (!controller.signal.aborted);
|
||||
} finally {
|
||||
storage?.onModuleDestroy();
|
||||
await prisma.onModuleDestroy();
|
||||
process.removeListener('SIGTERM', stop);
|
||||
process.removeListener('SIGINT', stop);
|
||||
}
|
||||
}
|
||||
|
||||
void main().catch((error: unknown) => {
|
||||
console.error(
|
||||
JSON.stringify({
|
||||
event: 'reconcile_failed',
|
||||
errorType: error instanceof Error ? error.name : 'UnknownError',
|
||||
}),
|
||||
);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
@@ -0,0 +1,200 @@
|
||||
import type { PrismaService } from '../prisma/prisma.service';
|
||||
import type { StorageService, StoredObject } from '../Storage/storage.service';
|
||||
import { ReconcileService } from './reconcile.service';
|
||||
import { reconcileOptions } from './reconcile-options';
|
||||
|
||||
const key =
|
||||
'originals/11111111-1111-4111-8111-111111111111/22222222-2222-4222-8222-222222222222';
|
||||
const now = new Date('2026-11-09T12:00:00Z');
|
||||
const old = new Date(now.getTime() - 601000);
|
||||
const fresh = new Date(now.getTime() - 599000);
|
||||
|
||||
function fixture(objects: StoredObject[] = [{ key, lastModified: old }]) {
|
||||
const tx = {
|
||||
$executeRaw: jest.fn().mockResolvedValue(0),
|
||||
$queryRaw: jest.fn().mockResolvedValue([{ locked: true }]),
|
||||
attachment: { findUnique: jest.fn().mockResolvedValue(null) },
|
||||
directUpload: { findUnique: jest.fn().mockResolvedValue(null) },
|
||||
};
|
||||
const prisma = {
|
||||
$queryRaw: jest.fn().mockResolvedValue([{ '?column?': 1 }]),
|
||||
$transaction: jest.fn(
|
||||
async (callback: (value: typeof tx) => Promise<unknown>) => callback(tx),
|
||||
),
|
||||
attachment: { findMany: jest.fn().mockResolvedValue([]) },
|
||||
directUpload: { updateMany: jest.fn().mockResolvedValue({ count: 0 }) },
|
||||
};
|
||||
const storage = {
|
||||
ready: jest.fn().mockResolvedValue(undefined),
|
||||
objects: jest.fn(async function* (prefix: string) {
|
||||
if (prefix === 'staging/') return;
|
||||
yield* objects;
|
||||
}),
|
||||
stat: jest.fn().mockResolvedValue({ key, lastModified: old }),
|
||||
remove: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
const emit = jest.fn();
|
||||
const service = new ReconcileService(
|
||||
prisma as unknown as PrismaService,
|
||||
storage as unknown as StorageService,
|
||||
emit,
|
||||
);
|
||||
const run = (apply = true) =>
|
||||
service.run({ apply, graceSeconds: 600, prefix: 'originals/' }, now);
|
||||
return { tx, prisma, storage, emit, run };
|
||||
}
|
||||
|
||||
describe('attachment reconciliation', () => {
|
||||
it('reports old orphans in dry-run without removing bytes', async () => {
|
||||
const f = fixture();
|
||||
expect(await f.run(false)).toMatchObject({
|
||||
orphanCandidates: 1,
|
||||
deletedObjects: 0,
|
||||
});
|
||||
expect(f.storage.remove).not.toHaveBeenCalled();
|
||||
expect(f.emit).toHaveBeenCalledWith({
|
||||
event: 'orphan_candidate',
|
||||
objectKey: key,
|
||||
});
|
||||
});
|
||||
|
||||
it('deletes an old orphan and safely handles another pass after it is gone', async () => {
|
||||
const f = fixture();
|
||||
expect(await f.run()).toMatchObject({ deletedObjects: 1 });
|
||||
f.storage.stat.mockResolvedValue(null);
|
||||
expect(await f.run()).toMatchObject({ deletedObjects: 0 });
|
||||
expect(f.storage.remove).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('keeps fresh objects, unknown keys and objects with no trusted timestamp', async () => {
|
||||
const f = fixture([
|
||||
{ key, lastModified: fresh },
|
||||
{ key: 'originals/manual.txt', lastModified: old },
|
||||
{ key },
|
||||
]);
|
||||
expect(await f.run()).toMatchObject({
|
||||
freshObjects: 2,
|
||||
unknownObjects: 1,
|
||||
deletedObjects: 0,
|
||||
});
|
||||
expect(f.prisma.$transaction).not.toHaveBeenCalled();
|
||||
expect(f.storage.remove).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not delete objects referenced by metadata', async () => {
|
||||
const f = fixture();
|
||||
f.tx.attachment.findUnique.mockResolvedValue({ id: 'published' });
|
||||
expect(await f.run()).toMatchObject({
|
||||
referencedObjects: 1,
|
||||
deletedObjects: 0,
|
||||
});
|
||||
expect(f.storage.remove).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('skips an object locked by an active uploader or another cleaner', async () => {
|
||||
const f = fixture();
|
||||
f.tx.$queryRaw.mockResolvedValue([{ locked: false }]);
|
||||
expect(await f.run()).toMatchObject({
|
||||
lockedObjects: 1,
|
||||
deletedObjects: 0,
|
||||
});
|
||||
expect(f.storage.remove).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rechecks current S3 age instead of trusting an old LIST result', async () => {
|
||||
const f = fixture();
|
||||
f.storage.stat.mockResolvedValue({ key, lastModified: fresh });
|
||||
expect(await f.run()).toMatchObject({ freshObjects: 1, deletedObjects: 0 });
|
||||
expect(f.storage.remove).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rechecks metadata immediately before deletion', async () => {
|
||||
const f = fixture();
|
||||
f.tx.attachment.findUnique
|
||||
.mockResolvedValueOnce(null)
|
||||
.mockResolvedValueOnce({ id: 'published' });
|
||||
expect(await f.run()).toMatchObject({
|
||||
referencedObjects: 1,
|
||||
deletedObjects: 0,
|
||||
});
|
||||
expect(f.storage.remove).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('aborts before contacting S3 if the DB is unavailable', async () => {
|
||||
const f = fixture();
|
||||
f.prisma.$queryRaw.mockRejectedValue(new Error('DB unavailable'));
|
||||
await expect(f.run()).rejects.toThrow('DB unavailable');
|
||||
expect(f.storage.ready).not.toHaveBeenCalled();
|
||||
expect(f.storage.remove).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not delete when the DB fails after the S3 age check', async () => {
|
||||
const f = fixture();
|
||||
f.tx.attachment.findUnique
|
||||
.mockResolvedValueOnce(null)
|
||||
.mockRejectedValueOnce(new Error('DB unavailable'));
|
||||
await expect(f.run()).rejects.toThrow('DB unavailable');
|
||||
expect(f.storage.remove).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not treat S3 failure as a missing object or successful cleanup', async () => {
|
||||
const f = fixture();
|
||||
f.storage.stat.mockRejectedValue(new Error('S3 unavailable'));
|
||||
await expect(f.run()).rejects.toThrow('S3 unavailable');
|
||||
expect(f.storage.remove).not.toHaveBeenCalled();
|
||||
expect(f.emit).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('reports missing bytes without deleting metadata', async () => {
|
||||
const f = fixture([]);
|
||||
f.prisma.attachment.findMany.mockResolvedValue([
|
||||
{ id: 'published', objectKey: key },
|
||||
]);
|
||||
f.storage.stat.mockResolvedValue(null);
|
||||
expect(await f.run()).toMatchObject({
|
||||
missingObjects: 1,
|
||||
deletedObjects: 0,
|
||||
});
|
||||
expect(f.emit).toHaveBeenCalledWith({
|
||||
event: 'missing_object',
|
||||
objectKey: key,
|
||||
attachmentId: 'published',
|
||||
});
|
||||
expect(f.storage.remove).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('reconciliation CLI options', () => {
|
||||
it('defaults to dry-run with a ten-minute grace and one-minute interval', () => {
|
||||
expect(reconcileOptions([], {})).toMatchObject({
|
||||
apply: false,
|
||||
watch: false,
|
||||
graceSeconds: 600,
|
||||
intervalSeconds: 60,
|
||||
});
|
||||
});
|
||||
|
||||
it('accepts explicit apply and a scope limited to one chat', () => {
|
||||
expect(
|
||||
reconcileOptions(
|
||||
['--apply', '--prefix', key.slice(0, key.lastIndexOf('/') + 1)],
|
||||
{ ORPHAN_GRACE_SECONDS: '1200' },
|
||||
),
|
||||
).toMatchObject({ apply: true, graceSeconds: 1200 });
|
||||
});
|
||||
|
||||
it.each(['0', '-1', '1.5', 'NaN', '999999999999'])(
|
||||
'rejects invalid grace %s',
|
||||
(value) => {
|
||||
expect(() =>
|
||||
reconcileOptions([], { ORPHAN_GRACE_SECONDS: value }),
|
||||
).toThrow();
|
||||
},
|
||||
);
|
||||
|
||||
it('rejects unsafe prefixes and conflicting modes', () => {
|
||||
expect(() => reconcileOptions(['--prefix', 'other/'], {})).toThrow();
|
||||
expect(() => reconcileOptions(['--apply', '--dry-run'], {})).toThrow();
|
||||
expect(() => reconcileOptions(['--unknown'], {})).toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,132 @@
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { StorageService } from '../Storage/storage.service';
|
||||
import { OBJECT_TRANSACTION, tryAttachmentObjectLock } from './object-lock';
|
||||
import { managedObjectKey, type ReconcileOptions } from './reconcile-options';
|
||||
import { cleanStaging } from './staging-cleanup';
|
||||
|
||||
export interface ReconcileEvent {
|
||||
event: string;
|
||||
objectKey?: string;
|
||||
attachmentId?: string;
|
||||
}
|
||||
|
||||
export class ReconcileService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly storage: StorageService,
|
||||
private readonly emit: (event: ReconcileEvent) => void = () => {},
|
||||
) {}
|
||||
|
||||
async run(options: ReconcileOptions, now = new Date()) {
|
||||
const cutoff = now.getTime() - options.graceSeconds * 1000;
|
||||
const report = {
|
||||
mode: options.apply ? 'apply' : 'dry-run',
|
||||
graceSeconds: options.graceSeconds,
|
||||
prefix: options.prefix,
|
||||
objectsScanned: 0,
|
||||
freshObjects: 0,
|
||||
referencedObjects: 0,
|
||||
lockedObjects: 0,
|
||||
unknownObjects: 0,
|
||||
orphanCandidates: 0,
|
||||
deletedObjects: 0,
|
||||
missingObjects: 0,
|
||||
};
|
||||
const oldEnough = (date?: Date) =>
|
||||
date !== undefined &&
|
||||
Number.isFinite(date.getTime()) &&
|
||||
date.getTime() <= cutoff;
|
||||
// Check the DB before contacting S3. Any subsequent DB/S3 error aborts this pass.
|
||||
await this.prisma.$queryRaw`SELECT 1`;
|
||||
await this.storage.ready();
|
||||
for await (const object of this.storage.objects(options.prefix)) {
|
||||
options.signal?.throwIfAborted();
|
||||
report.objectsScanned++;
|
||||
if (!managedObjectKey(object.key)) {
|
||||
report.unknownObjects++;
|
||||
continue;
|
||||
}
|
||||
if (!oldEnough(object.lastModified)) {
|
||||
report.freshObjects++;
|
||||
continue;
|
||||
}
|
||||
const outcome = await this.prisma.$transaction(async (tx) => {
|
||||
if (!(await tryAttachmentObjectLock(tx, object.key))) return 'locked';
|
||||
if (
|
||||
await tx.attachment.findUnique({
|
||||
where: { objectKey: object.key },
|
||||
select: { id: true },
|
||||
})
|
||||
) {
|
||||
return 'referenced';
|
||||
}
|
||||
// LIST timestamps may be stale. HEAD checks the current object under the writer lock.
|
||||
const current = await this.storage.stat(object.key);
|
||||
if (!current) return 'gone';
|
||||
if (!oldEnough(current.lastModified)) return 'fresh';
|
||||
// Recheck immediately before DELETE, also detecting DB failure after HEAD.
|
||||
if (
|
||||
await tx.attachment.findUnique({
|
||||
where: { objectKey: object.key },
|
||||
select: { id: true },
|
||||
})
|
||||
) {
|
||||
return 'referenced';
|
||||
}
|
||||
options.signal?.throwIfAborted();
|
||||
if (options.apply) await this.storage.remove(object.key);
|
||||
return options.apply ? 'deleted' : 'candidate';
|
||||
}, OBJECT_TRANSACTION);
|
||||
if (outcome === 'locked') report.lockedObjects++;
|
||||
if (outcome === 'referenced') report.referencedObjects++;
|
||||
if (outcome === 'fresh') report.freshObjects++;
|
||||
if (outcome === 'candidate' || outcome === 'deleted') {
|
||||
report.orphanCandidates++;
|
||||
if (outcome === 'deleted') report.deletedObjects++;
|
||||
this.emit({
|
||||
event: outcome === 'deleted' ? 'orphan_deleted' : 'orphan_candidate',
|
||||
objectKey: object.key,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Reverse check: missing bytes are an integrity incident, never a reason to delete metadata.
|
||||
let after: string | undefined;
|
||||
while (true) {
|
||||
options.signal?.throwIfAborted();
|
||||
const rows = await this.prisma.attachment.findMany({
|
||||
where: {
|
||||
objectKey: { startsWith: options.prefix },
|
||||
...(after ? { id: { gt: after } } : {}),
|
||||
},
|
||||
select: { id: true, objectKey: true },
|
||||
orderBy: { id: 'asc' },
|
||||
take: 200,
|
||||
});
|
||||
for (const row of rows) {
|
||||
options.signal?.throwIfAborted();
|
||||
if (!(await this.storage.stat(row.objectKey))) {
|
||||
report.missingObjects++;
|
||||
this.emit({
|
||||
event: 'missing_object',
|
||||
objectKey: row.objectKey,
|
||||
attachmentId: row.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
if (rows.length < 200) break;
|
||||
after = rows[rows.length - 1].id;
|
||||
}
|
||||
// Chat-scoped original-only diagnostics must not scan another chat's staging.
|
||||
if (options.prefix === 'originals/') {
|
||||
const staging = await cleanStaging(
|
||||
this.prisma,
|
||||
this.storage,
|
||||
options,
|
||||
now,
|
||||
);
|
||||
return { ...report, ...staging };
|
||||
}
|
||||
return report;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import type { PrismaService } from '../prisma/prisma.service';
|
||||
import type { StorageService } from '../Storage/storage.service';
|
||||
import { cleanStaging } from './staging-cleanup';
|
||||
|
||||
function fixture() {
|
||||
const chat = randomUUID();
|
||||
const id = randomUUID();
|
||||
const key = `staging/${chat}/${id}`;
|
||||
const now = new Date();
|
||||
const old = new Date(now.getTime() - 1200000);
|
||||
const upload = { chatId: chat, expiresAt: old };
|
||||
const tx = {
|
||||
$queryRaw: jest.fn().mockResolvedValue([{ locked: true }]),
|
||||
$executeRaw: jest.fn().mockResolvedValue(0),
|
||||
directUpload: { findUnique: jest.fn().mockResolvedValue(upload) },
|
||||
};
|
||||
const db = {
|
||||
$queryRaw: jest.fn().mockResolvedValue([]),
|
||||
$transaction: jest.fn(async (fn: (value: unknown) => unknown) => fn(tx)),
|
||||
directUpload: { updateMany: jest.fn().mockResolvedValue({ count: 1 }) },
|
||||
};
|
||||
const storage = {
|
||||
objects: jest.fn(async function* () {
|
||||
yield { key, lastModified: old };
|
||||
}),
|
||||
stat: jest.fn().mockResolvedValue({ key, lastModified: old }),
|
||||
remove: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
const options = {
|
||||
apply: true,
|
||||
graceSeconds: 600,
|
||||
prefix: `originals/${chat}/`,
|
||||
};
|
||||
const run = (apply = true) =>
|
||||
cleanStaging(
|
||||
db as unknown as PrismaService,
|
||||
storage as unknown as StorageService,
|
||||
{ ...options, apply },
|
||||
now,
|
||||
);
|
||||
return { key, chat, now, upload, tx, db, storage, run };
|
||||
}
|
||||
|
||||
describe('staging cleanup', () => {
|
||||
it('deletes only aged objects after reservation expiry plus grace', async () => {
|
||||
const f = fixture();
|
||||
expect(await f.run()).toMatchObject({ stagingDeleted: 1 });
|
||||
expect(f.storage.objects).toHaveBeenCalledWith(`staging/${f.chat}/`);
|
||||
expect(f.storage.remove).toHaveBeenCalledWith(f.key);
|
||||
expect(f.db.directUpload.updateMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: expect.objectContaining({ chatId: f.chat }),
|
||||
}),
|
||||
);
|
||||
});
|
||||
it('protects an unexpired URL even if the object already looks old', async () => {
|
||||
const f = fixture();
|
||||
f.upload.expiresAt = new Date(f.now.getTime() + 60000);
|
||||
expect(await f.run()).toMatchObject({
|
||||
stagingDeleted: 0,
|
||||
stagingProtected: 1,
|
||||
});
|
||||
expect(f.storage.remove).not.toHaveBeenCalled();
|
||||
});
|
||||
it('protects an active complete and a freshly recreated staging object', async () => {
|
||||
const f = fixture();
|
||||
f.tx.$queryRaw.mockResolvedValueOnce([{ locked: false }]);
|
||||
expect(await f.run()).toMatchObject({ stagingProtected: 1 });
|
||||
f.storage.stat.mockResolvedValueOnce({ key: f.key, lastModified: f.now });
|
||||
expect(await f.run()).toMatchObject({ stagingProtected: 1 });
|
||||
expect(f.storage.remove).not.toHaveBeenCalled();
|
||||
});
|
||||
it('performs a dry run without deleting files or changing reservations', async () => {
|
||||
const f = fixture();
|
||||
expect(await f.run(false)).toMatchObject({
|
||||
stagingCandidates: 1,
|
||||
stagingDeleted: 0,
|
||||
});
|
||||
expect(f.storage.remove).not.toHaveBeenCalled();
|
||||
expect(f.db.directUpload.updateMany).not.toHaveBeenCalled();
|
||||
});
|
||||
it('fails closed when PostgreSQL is unavailable', async () => {
|
||||
const f = fixture();
|
||||
f.db.$queryRaw.mockRejectedValueOnce(new Error('database unavailable'));
|
||||
await expect(f.run()).rejects.toThrow('database unavailable');
|
||||
expect(f.storage.objects).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,77 @@
|
||||
import type { PrismaService } from '../prisma/prisma.service';
|
||||
import type { StorageService } from '../Storage/storage.service';
|
||||
import { uuidPattern } from '../common/validation';
|
||||
import type { ReconcileOptions } from './reconcile-options';
|
||||
import { OBJECT_TRANSACTION, tryAttachmentObjectLock } from './object-lock';
|
||||
|
||||
export async function cleanStaging(
|
||||
db: PrismaService,
|
||||
storage: StorageService,
|
||||
options: ReconcileOptions,
|
||||
now: Date,
|
||||
) {
|
||||
const cutoff = new Date(now.getTime() - options.graceSeconds * 1000);
|
||||
const scope =
|
||||
options.prefix === 'originals/' ? undefined : options.prefix.split('/')[1];
|
||||
if (
|
||||
options.prefix !== 'originals/' &&
|
||||
(!scope ||
|
||||
options.prefix !== `originals/${scope}/` ||
|
||||
!uuidPattern.test(scope))
|
||||
)
|
||||
throw new Error('Unsafe staging cleanup scope');
|
||||
const prefix = scope ? `staging/${scope}/` : 'staging/';
|
||||
const report = {
|
||||
stagingScanned: 0,
|
||||
stagingProtected: 0,
|
||||
stagingCandidates: 0,
|
||||
stagingDeleted: 0,
|
||||
};
|
||||
// Fail closed before looking for objects. Never infer "no intent" from a DB outage.
|
||||
await db.$queryRaw`SELECT 1`;
|
||||
for await (const object of storage.objects(prefix)) {
|
||||
options.signal?.throwIfAborted();
|
||||
report.stagingScanned++;
|
||||
const parts = object.key.split('/');
|
||||
if (
|
||||
parts.length !== 3 ||
|
||||
parts[0] !== 'staging' ||
|
||||
!uuidPattern.test(parts[1]) ||
|
||||
!uuidPattern.test(parts[2]) ||
|
||||
!object.lastModified ||
|
||||
object.lastModified > cutoff
|
||||
) {
|
||||
report.stagingProtected++;
|
||||
continue;
|
||||
}
|
||||
const outcome = await db.$transaction(async (tx) => {
|
||||
if (!(await tryAttachmentObjectLock(tx, object.key))) return false;
|
||||
const upload = await tx.directUpload.findUnique({
|
||||
where: { id: parts[2] },
|
||||
});
|
||||
// Wait for BOTH signature expiry and grace. A late replay must not outlive its reservation.
|
||||
if (upload && (upload.chatId !== parts[1] || upload.expiresAt > cutoff))
|
||||
return false;
|
||||
const current = await storage.stat(object.key);
|
||||
if (!current || !current.lastModified || current.lastModified > cutoff)
|
||||
return false;
|
||||
options.signal?.throwIfAborted();
|
||||
if (options.apply) await storage.remove(object.key);
|
||||
return true;
|
||||
}, OBJECT_TRANSACTION);
|
||||
if (outcome) {
|
||||
report.stagingCandidates++;
|
||||
if (options.apply) report.stagingDeleted++;
|
||||
} else report.stagingProtected++;
|
||||
}
|
||||
if (options.apply)
|
||||
await db.directUpload.updateMany({
|
||||
where: {
|
||||
status: 'pending',
|
||||
expiresAt: { lte: cutoff },
|
||||
...(scope ? { chatId: scope } : {}),
|
||||
},
|
||||
data: { status: 'expired' },
|
||||
});
|
||||
return report;
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Post,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
ApiBasicAuth,
|
||||
ApiBearerAuth,
|
||||
ApiCreatedResponse,
|
||||
ApiNoContentResponse,
|
||||
ApiOperation,
|
||||
ApiTags,
|
||||
ApiUnauthorizedResponse,
|
||||
} from '@nestjs/swagger';
|
||||
import { UserGuard, type AuthenticatedRequest } from '../User/user.guard';
|
||||
import { AuthService } from './auth.service';
|
||||
import { Session } from './auth.schema';
|
||||
|
||||
@ApiTags('auth')
|
||||
@Controller('api/v1/auth/sessions')
|
||||
export class AuthController {
|
||||
constructor(private readonly auth: AuthService) {}
|
||||
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
@ApiBasicAuth('basicAuth')
|
||||
@ApiOperation({
|
||||
operationId: 'createSession',
|
||||
summary: 'Вход',
|
||||
})
|
||||
@ApiCreatedResponse({ type: Session })
|
||||
@ApiUnauthorizedResponse({
|
||||
headers: {
|
||||
'WWW-Authenticate': {
|
||||
schema: { type: 'string' },
|
||||
example: 'Basic realm="messenger", charset="UTF-8"',
|
||||
},
|
||||
},
|
||||
})
|
||||
create(@Req() request: AuthenticatedRequest, @Body() body: unknown) {
|
||||
return this.auth.login(request.headers.authorization, body);
|
||||
}
|
||||
|
||||
@Delete('current')
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
@UseGuards(UserGuard)
|
||||
@ApiBearerAuth('bearerAuth')
|
||||
@ApiOperation({
|
||||
operationId: 'revokeSession',
|
||||
summary: 'Отзыв текущей сессии',
|
||||
})
|
||||
@ApiNoContentResponse()
|
||||
@ApiUnauthorizedResponse({
|
||||
headers: {
|
||||
'WWW-Authenticate': {
|
||||
schema: { type: 'string' },
|
||||
example: 'Bearer realm="messenger"',
|
||||
},
|
||||
},
|
||||
})
|
||||
logout(@Req() request: AuthenticatedRequest) {
|
||||
return this.auth.logout(request.currentSessionId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { HttpStatus } from '@nestjs/common';
|
||||
import { ApiException } from '../common/exceptions/api.exception';
|
||||
|
||||
export class AuthException extends ApiException {
|
||||
readonly challenge: string;
|
||||
|
||||
constructor(scheme: 'Basic' | 'Bearer') {
|
||||
super(
|
||||
HttpStatus.UNAUTHORIZED,
|
||||
'unauthorized',
|
||||
'Требуется действительная аутентификация',
|
||||
);
|
||||
this.challenge =
|
||||
scheme === 'Basic'
|
||||
? 'Basic realm="messenger", charset="UTF-8"'
|
||||
: 'Bearer realm="messenger"';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { User } from '../User/user.schema';
|
||||
|
||||
export class Session {
|
||||
token!: string;
|
||||
|
||||
@ApiProperty({ enum: ['Bearer'] })
|
||||
token_type!: 'Bearer';
|
||||
|
||||
@ApiProperty({ format: 'date-time' })
|
||||
expires_at!: string;
|
||||
|
||||
@ApiProperty({ type: User })
|
||||
user!: User;
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { createHash, randomBytes } from 'node:crypto';
|
||||
import { TextDecoder } from 'node:util';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { Prisma } from '../generated/prisma/client';
|
||||
import { UserService } from '../User/user.service';
|
||||
import type { User } from '../User/user.schema';
|
||||
import { invalidRequest, uuid, uuidPattern } from '../common/validation';
|
||||
import { ApiException } from '../common/exceptions/api.exception';
|
||||
import { AuthException } from './auth.exception';
|
||||
import { PasswordService, validPassword } from './password.service';
|
||||
import type { Session } from './auth.schema';
|
||||
|
||||
export interface ChatAccess {
|
||||
chatId: string;
|
||||
userId: string;
|
||||
messagesEpoch: string;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
private readonly ttlSeconds: number;
|
||||
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly users: UserService,
|
||||
private readonly passwords: PasswordService,
|
||||
) {
|
||||
this.ttlSeconds = Number(process.env.SESSION_TTL_SECONDS ?? 3600);
|
||||
if (
|
||||
!Number.isSafeInteger(this.ttlSeconds) ||
|
||||
this.ttlSeconds <= 0 ||
|
||||
Number.isNaN(new Date(Date.now() + this.ttlSeconds * 1000).getTime())
|
||||
) {
|
||||
throw new Error(
|
||||
'SESSION_TTL_SECONDS must be a positive integer representing a valid expiry',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async login(authorization: unknown, body: unknown): Promise<Session> {
|
||||
const { username, password } = this.basicCredentials(authorization);
|
||||
const user = await this.prisma.user.findUnique({ where: { username } });
|
||||
const verified = await this.passwords.verify(
|
||||
password,
|
||||
user?.passwordHash ?? null,
|
||||
);
|
||||
if (!user || !verified) throw new AuthException('Basic');
|
||||
if (body !== undefined)
|
||||
return invalidRequest('Вход через Basic не принимает тело запроса');
|
||||
const token = randomBytes(32).toString('base64url');
|
||||
const session = await this.prisma.session.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
tokenHash: this.tokenHash(token),
|
||||
expiresAt: new Date(Date.now() + this.ttlSeconds * 1000),
|
||||
},
|
||||
});
|
||||
return {
|
||||
token,
|
||||
token_type: 'Bearer',
|
||||
expires_at: session.expiresAt.toISOString(),
|
||||
user: this.users.serialize(user),
|
||||
};
|
||||
}
|
||||
|
||||
async authenticate(
|
||||
authorization: unknown,
|
||||
chatId?: unknown,
|
||||
): Promise<{
|
||||
user: User;
|
||||
sessionId: string;
|
||||
chatsEpoch: string;
|
||||
chatAccess?: ChatAccess;
|
||||
}> {
|
||||
const match =
|
||||
typeof authorization === 'string' &&
|
||||
/^Bearer +([A-Za-z0-9_-]{43})$/i.exec(authorization);
|
||||
if (!match) throw new AuthException('Bearer');
|
||||
const requestedChat =
|
||||
typeof chatId === 'string' && uuidPattern.test(chatId)
|
||||
? chatId.toLowerCase()
|
||||
: null;
|
||||
const [session] = await this.prisma.$queryRaw<
|
||||
{
|
||||
id: string;
|
||||
userId: string;
|
||||
username: string;
|
||||
displayName: string;
|
||||
createdAt: Date;
|
||||
chatsEpoch: bigint;
|
||||
chatId: string | null;
|
||||
messagesEpoch: bigint | null;
|
||||
}[]
|
||||
>(Prisma.sql`
|
||||
SELECT s.id, s.user_id AS "userId", u.chats_epoch AS "chatsEpoch",
|
||||
u.username, u.display_name AS "displayName", u.created_at AS "createdAt",
|
||||
${
|
||||
chatId === undefined
|
||||
? Prisma.sql`NULL::uuid AS "chatId", NULL::bigint AS "messagesEpoch"`
|
||||
: Prisma.sql`c.id AS "chatId", COALESCE((SELECT SUM(e.epoch) FROM "public"."chat_message_epochs" AS e WHERE e.chat_id = c.id), 0)::bigint AS "messagesEpoch"`
|
||||
}
|
||||
FROM sessions AS s
|
||||
JOIN users AS u ON u.id = s.user_id
|
||||
${
|
||||
chatId === undefined
|
||||
? Prisma.empty
|
||||
: Prisma.sql`
|
||||
LEFT JOIN chat_members AS cm
|
||||
ON cm.user_id = s.user_id AND cm.chat_id = ${requestedChat}::uuid
|
||||
LEFT JOIN chats AS c ON c.id = cm.chat_id
|
||||
`
|
||||
}
|
||||
WHERE s.token_hash = ${this.tokenHash(match[1])}
|
||||
AND s.revoked_at IS NULL AND s.expires_at > ${new Date()}
|
||||
LIMIT 1
|
||||
`);
|
||||
if (!session) throw new AuthException('Bearer');
|
||||
if (chatId !== undefined) {
|
||||
uuid(chatId, 'chat_id');
|
||||
if (session.chatId === null || session.messagesEpoch === null)
|
||||
throw new ApiException(404, 'not_found', 'Объект не найден');
|
||||
}
|
||||
return {
|
||||
user: this.users.serialize({ ...session, id: session.userId }),
|
||||
sessionId: session.id,
|
||||
chatsEpoch: session.chatsEpoch.toString(),
|
||||
chatAccess:
|
||||
session.chatId !== null && session.messagesEpoch !== null
|
||||
? {
|
||||
chatId: session.chatId,
|
||||
userId: session.userId,
|
||||
messagesEpoch: session.messagesEpoch.toString(),
|
||||
}
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
async logout(sessionId: string): Promise<void> {
|
||||
const now = new Date();
|
||||
const result = await this.prisma.session.updateMany({
|
||||
where: { id: sessionId, revokedAt: null, expiresAt: { gt: now } },
|
||||
data: { revokedAt: now },
|
||||
});
|
||||
if (result.count !== 1) throw new AuthException('Bearer');
|
||||
}
|
||||
|
||||
private tokenHash(token: string): string {
|
||||
return createHash('sha256').update(token).digest('hex');
|
||||
}
|
||||
|
||||
private basicCredentials(authorization: unknown): {
|
||||
username: string;
|
||||
password: string;
|
||||
} {
|
||||
const match =
|
||||
typeof authorization === 'string' &&
|
||||
/^Basic +([A-Za-z0-9+/]+={0,2})$/i.exec(authorization);
|
||||
if (!match) throw new AuthException('Basic');
|
||||
try {
|
||||
const bytes = Buffer.from(match[1], 'base64');
|
||||
if (
|
||||
bytes.toString('base64').replace(/=+$/, '') !==
|
||||
match[1].replace(/=+$/, '')
|
||||
)
|
||||
throw new AuthException('Basic');
|
||||
const value = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
||||
const colon = value.indexOf(':');
|
||||
const username = value.slice(0, colon);
|
||||
const password = value.slice(colon + 1);
|
||||
if (
|
||||
colon < 0 ||
|
||||
!/^[a-z0-9_]{3,32}$/.test(username) ||
|
||||
!validPassword(password)
|
||||
)
|
||||
throw new AuthException('Basic');
|
||||
return { username, password };
|
||||
} catch {
|
||||
throw new AuthException('Basic');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { argon2, randomBytes, timingSafeEqual } from 'node:crypto';
|
||||
import { invalidRequest } from '../common/validation';
|
||||
|
||||
const argonParameters = {
|
||||
memory: 19456,
|
||||
passes: 2,
|
||||
parallelism: 1,
|
||||
tagLength: 32,
|
||||
};
|
||||
const argonPrefix = '$argon2id$v=19$m=19456,t=2,p=1';
|
||||
|
||||
function decodeBase64(value: string, length: number) {
|
||||
const decoded = Buffer.from(value, 'base64');
|
||||
return decoded.length === length &&
|
||||
decoded.toString('base64').replace(/=+$/, '') === value
|
||||
? decoded
|
||||
: undefined;
|
||||
}
|
||||
|
||||
function argonHash(value: string | null) {
|
||||
const parts = value?.split('$');
|
||||
if (parts?.length !== 6 || parts.slice(0, 4).join('$') !== argonPrefix)
|
||||
return undefined;
|
||||
const salt = decodeBase64(parts[4], 16);
|
||||
const key = decodeBase64(parts[5], 32);
|
||||
return salt && key ? { salt, key } : undefined;
|
||||
}
|
||||
|
||||
export function validPassword(value: unknown): value is string {
|
||||
if (typeof value !== 'string' || value.trim().length === 0) return false;
|
||||
const length = Array.from(value).length;
|
||||
return length >= 12 && length <= 128;
|
||||
}
|
||||
|
||||
export function registrationPassword(value: unknown): string {
|
||||
if (!validPassword(value))
|
||||
return invalidRequest('Пароль должен содержать 12–128 Unicode-символов');
|
||||
return value;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class PasswordService {
|
||||
async hash(password: string): Promise<string> {
|
||||
const salt = randomBytes(16);
|
||||
const key = await this.deriveArgon(password, salt);
|
||||
return `${argonPrefix}$${salt.toString('base64').replace(/=+$/, '')}$${key.toString('base64').replace(/=+$/, '')}`;
|
||||
}
|
||||
|
||||
async verify(password: string, storedHash: string | null): Promise<boolean> {
|
||||
const stored = argonHash(storedHash);
|
||||
const key = await this.deriveArgon(
|
||||
password,
|
||||
stored?.salt ?? Buffer.alloc(16),
|
||||
);
|
||||
const matches = timingSafeEqual(key, stored?.key ?? Buffer.alloc(32));
|
||||
return Boolean(stored && matches);
|
||||
}
|
||||
|
||||
private deriveArgon(password: string, salt: Buffer): Promise<Buffer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
argon2(
|
||||
'argon2id',
|
||||
{ ...argonParameters, message: password, nonce: salt },
|
||||
(error, key) => {
|
||||
if (error) reject(error);
|
||||
else resolve(key);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Headers,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Param,
|
||||
Post,
|
||||
Query,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
ApiBody,
|
||||
ApiBearerAuth,
|
||||
ApiCreatedResponse,
|
||||
ApiHeader,
|
||||
ApiOkResponse,
|
||||
ApiOperation,
|
||||
ApiQuery,
|
||||
ApiTags,
|
||||
} from '@nestjs/swagger';
|
||||
import { UserGuard, type AuthenticatedRequest } from '../User/user.guard';
|
||||
import {
|
||||
CreateChatDto,
|
||||
CreateMessageDto,
|
||||
Chat,
|
||||
Message,
|
||||
ChatPage,
|
||||
MessagePage,
|
||||
} from './chat.schema';
|
||||
import { ChatService } from './chat.service';
|
||||
|
||||
@ApiTags('chats')
|
||||
@ApiBearerAuth('bearerAuth')
|
||||
@UseGuards(UserGuard)
|
||||
@Controller('api/v1/chats')
|
||||
export class ChatController {
|
||||
constructor(private readonly chats: ChatService) {}
|
||||
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
@ApiBody({ type: CreateChatDto })
|
||||
@ApiOperation({ operationId: 'createChat' })
|
||||
@ApiCreatedResponse({ type: Chat })
|
||||
create(@Req() request: AuthenticatedRequest, @Body() body: unknown) {
|
||||
return this.chats.create(body, request.currentUser.id);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ operationId: 'listChats' })
|
||||
@ApiOkResponse({ type: ChatPage })
|
||||
@ApiQuery({
|
||||
name: 'limit',
|
||||
required: false,
|
||||
schema: { type: 'integer', minimum: 1, maximum: 100, default: 50 },
|
||||
})
|
||||
@ApiQuery({ name: 'cursor', required: false, type: String })
|
||||
list(
|
||||
@Req() request: AuthenticatedRequest,
|
||||
@Query('limit') limit: unknown,
|
||||
@Query('cursor') cursor: unknown,
|
||||
) {
|
||||
return this.chats.list(
|
||||
request.currentUser.id,
|
||||
limit,
|
||||
cursor,
|
||||
request.currentChatsEpoch,
|
||||
);
|
||||
}
|
||||
|
||||
@Get(':chat_id')
|
||||
@ApiOperation({ operationId: 'getChat' })
|
||||
@ApiOkResponse({ type: Chat })
|
||||
get(@Req() request: AuthenticatedRequest, @Param('chat_id') chatId: string) {
|
||||
return this.chats.get(chatId, request.currentUser.id);
|
||||
}
|
||||
|
||||
@Post(':chat_id/messages')
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
@ApiBody({ type: CreateMessageDto })
|
||||
@ApiHeader({
|
||||
name: 'Idempotency-Key',
|
||||
required: true,
|
||||
schema: {
|
||||
type: 'string',
|
||||
minLength: 1,
|
||||
maxLength: 128,
|
||||
pattern: '^[A-Za-z0-9._:-]+$',
|
||||
},
|
||||
})
|
||||
@ApiOperation({ operationId: 'createMessage' })
|
||||
@ApiCreatedResponse({ type: Message })
|
||||
createMessage(
|
||||
@Req() request: AuthenticatedRequest,
|
||||
@Param('chat_id') chatId: string,
|
||||
@Headers('idempotency-key') idempotencyKey: unknown,
|
||||
@Body() body: unknown,
|
||||
) {
|
||||
return this.chats.createMessage(
|
||||
chatId,
|
||||
body,
|
||||
request.currentUser.id,
|
||||
idempotencyKey,
|
||||
);
|
||||
}
|
||||
|
||||
@Get(':chat_id/messages')
|
||||
@ApiOperation({ operationId: 'listMessages' })
|
||||
@ApiOkResponse({ type: MessagePage })
|
||||
@ApiQuery({
|
||||
name: 'limit',
|
||||
required: false,
|
||||
schema: { type: 'integer', minimum: 1, maximum: 100, default: 50 },
|
||||
})
|
||||
@ApiQuery({ name: 'cursor', required: false, type: String })
|
||||
listMessages(
|
||||
@Req() request: AuthenticatedRequest,
|
||||
@Param('chat_id') chatId: string,
|
||||
@Query('limit') limit: unknown,
|
||||
@Query('cursor') cursor: unknown,
|
||||
) {
|
||||
return this.chats.listMessages(
|
||||
chatId,
|
||||
request.currentUser.id,
|
||||
limit,
|
||||
cursor,
|
||||
request.currentChatAccess,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UserModule } from '../User/user.module';
|
||||
import { ChatController } from './chat.controller';
|
||||
import { ChatService } from './chat.service';
|
||||
|
||||
@Module({
|
||||
imports: [UserModule],
|
||||
controllers: [ChatController],
|
||||
providers: [ChatService],
|
||||
})
|
||||
export class ChatModule {}
|
||||
@@ -0,0 +1,68 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
|
||||
export class CreateChatDto {
|
||||
@ApiProperty({ example: 'Проект по ВНП', maxLength: 100 })
|
||||
title!: string;
|
||||
|
||||
@ApiProperty({ type: [String], format: 'uuid' })
|
||||
member_ids!: string[];
|
||||
}
|
||||
|
||||
export class CreateMessageDto {
|
||||
@ApiProperty({ example: 'Привет, Боб!', maxLength: 4000 })
|
||||
text!: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
type: [String],
|
||||
format: 'uuid',
|
||||
maxItems: 10,
|
||||
uniqueItems: true,
|
||||
default: [],
|
||||
})
|
||||
attachment_ids?: string[];
|
||||
}
|
||||
|
||||
export class Chat {
|
||||
@ApiProperty({ format: 'uuid' })
|
||||
id!: string;
|
||||
@ApiProperty({ maxLength: 100 })
|
||||
title!: string;
|
||||
@ApiProperty({ type: [String], format: 'uuid' })
|
||||
member_ids!: string[];
|
||||
@ApiProperty({ format: 'date-time' })
|
||||
created_at!: string;
|
||||
}
|
||||
|
||||
export class Message {
|
||||
@ApiProperty({ format: 'uuid' })
|
||||
id!: string;
|
||||
@ApiProperty({ format: 'uuid' })
|
||||
chat_id!: string;
|
||||
@ApiProperty({ format: 'uuid' })
|
||||
sender_id!: string;
|
||||
@ApiProperty({ maxLength: 4000 })
|
||||
text!: string;
|
||||
@ApiProperty({ type: [String], format: 'uuid', maxItems: 10 })
|
||||
attachment_ids!: string[];
|
||||
@ApiProperty({ format: 'date-time' })
|
||||
created_at!: string;
|
||||
}
|
||||
|
||||
export class ChatPage {
|
||||
@ApiProperty({ type: [Chat] })
|
||||
items!: Chat[];
|
||||
@ApiProperty({ type: String, nullable: true })
|
||||
next_cursor!: string | null;
|
||||
}
|
||||
|
||||
export class MessagePage {
|
||||
@ApiProperty({ type: [Message] })
|
||||
items!: Message[];
|
||||
@ApiProperty({ type: String, nullable: true })
|
||||
next_cursor!: string | null;
|
||||
}
|
||||
|
||||
export interface Page<T> {
|
||||
items: T[];
|
||||
next_cursor: string | null;
|
||||
}
|
||||
@@ -0,0 +1,425 @@
|
||||
import { HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import type { ChatAccess } from '../Auth/auth.service';
|
||||
import {
|
||||
Prisma,
|
||||
type Message as StoredMessage,
|
||||
} from '../generated/prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { ApiException } from '../common/exceptions/api.exception';
|
||||
import {
|
||||
invalidRequest,
|
||||
objectBody,
|
||||
pageLimit,
|
||||
requiredText,
|
||||
uuid,
|
||||
} from '../common/validation';
|
||||
import type { Chat, Message, Page } from './chat.schema';
|
||||
import { CacheService, CACHE_TTL } from '../cache/cache.service';
|
||||
|
||||
interface Cursor {
|
||||
scope: string;
|
||||
created_at: string;
|
||||
id: string;
|
||||
}
|
||||
|
||||
const chatInclude = {
|
||||
members: { orderBy: { position: 'asc' as const } },
|
||||
} satisfies Prisma.ChatInclude;
|
||||
type StoredChat = Prisma.ChatGetPayload<{ include: typeof chatInclude }>;
|
||||
|
||||
@Injectable()
|
||||
export class ChatService {
|
||||
private readonly idempotencyTtlSeconds: number;
|
||||
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly cache: CacheService,
|
||||
) {
|
||||
this.idempotencyTtlSeconds = Number(
|
||||
process.env.IDEMPOTENCY_TTL_SECONDS ?? 86400,
|
||||
);
|
||||
if (
|
||||
!Number.isSafeInteger(this.idempotencyTtlSeconds) ||
|
||||
this.idempotencyTtlSeconds < 86400 ||
|
||||
Number.isNaN(
|
||||
new Date(Date.now() + this.idempotencyTtlSeconds * 1000).getTime(),
|
||||
)
|
||||
) {
|
||||
throw new Error('IDEMPOTENCY_TTL_SECONDS must be at least 86400');
|
||||
}
|
||||
}
|
||||
|
||||
async create(value: unknown, userId: string): Promise<Chat> {
|
||||
const body = objectBody(value, ['title', 'member_ids']);
|
||||
const title = requiredText(body.title, 'title', 100);
|
||||
if (
|
||||
!Array.isArray(body.member_ids) ||
|
||||
body.member_ids.length < 1 ||
|
||||
body.member_ids.length > 100
|
||||
) {
|
||||
return invalidRequest('Некорректный member_ids');
|
||||
}
|
||||
const memberIds = body.member_ids.map((id: unknown) =>
|
||||
uuid(id, 'member_ids'),
|
||||
);
|
||||
if (
|
||||
new Set(memberIds).size !== memberIds.length ||
|
||||
!memberIds.includes(userId)
|
||||
) {
|
||||
return invalidRequest(
|
||||
'Список участников содержит дубликат или не содержит вызывающего',
|
||||
);
|
||||
}
|
||||
try {
|
||||
const chat = await this.prisma.$transaction(async (tx) => {
|
||||
const count = await tx.user.count({ where: { id: { in: memberIds } } });
|
||||
if (count !== memberIds.length)
|
||||
return invalidRequest('Неизвестный участник чата');
|
||||
const created = await tx.chat.create({
|
||||
data: {
|
||||
title,
|
||||
members: {
|
||||
create: memberIds.map((userId, position) => ({
|
||||
userId,
|
||||
position,
|
||||
})),
|
||||
},
|
||||
},
|
||||
include: chatInclude,
|
||||
});
|
||||
await tx.$executeRaw(Prisma.sql`
|
||||
WITH locked AS MATERIALIZED (
|
||||
SELECT id FROM users
|
||||
WHERE id IN (${Prisma.join(memberIds.map((id) => Prisma.sql`${id}::uuid`))})
|
||||
ORDER BY id FOR UPDATE
|
||||
)
|
||||
UPDATE users AS u SET chats_epoch = u.chats_epoch + 1
|
||||
FROM locked WHERE u.id = locked.id
|
||||
`);
|
||||
return created;
|
||||
});
|
||||
return this.serializeChat(chat);
|
||||
} catch (error) {
|
||||
if (
|
||||
error instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
error.code === 'P2003'
|
||||
) {
|
||||
return invalidRequest('Неизвестный участник чата');
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async list(
|
||||
userId: string,
|
||||
limitValue: unknown,
|
||||
cursorValue: unknown,
|
||||
epoch: string,
|
||||
): Promise<Page<Chat>> {
|
||||
const limit = pageLimit(limitValue);
|
||||
const scope = `chats:${userId}`;
|
||||
const cursor = this.decodeCursor(cursorValue, scope);
|
||||
return this.cache.remember(
|
||||
scope,
|
||||
{ limit, cursor },
|
||||
CACHE_TTL.page,
|
||||
async () => {
|
||||
const chats = await this.prisma.chat.findMany({
|
||||
where: { members: { some: { userId } }, ...this.after(cursor) },
|
||||
include: chatInclude,
|
||||
orderBy: [{ createdAt: 'asc' }, { id: 'asc' }],
|
||||
take: limit + 1,
|
||||
});
|
||||
return this.page(
|
||||
chats.map((chat) => this.serializeChat(chat)),
|
||||
limit,
|
||||
scope,
|
||||
);
|
||||
},
|
||||
epoch,
|
||||
);
|
||||
}
|
||||
|
||||
async get(value: unknown, userId: string): Promise<Chat> {
|
||||
const { id } = await this.authorizeChat(value, userId);
|
||||
return this.getMetadata(id);
|
||||
}
|
||||
|
||||
private async authorizeChat(value: unknown, userId: string) {
|
||||
const id = uuid(value, 'chat_id');
|
||||
const [membership] = await this.prisma.$queryRaw<
|
||||
{ id: string; messagesEpoch: bigint }[]
|
||||
>(Prisma.sql`
|
||||
SELECT c.id, COALESCE((SELECT SUM(e.epoch) FROM "public"."chat_message_epochs" AS e WHERE e.chat_id = c.id), 0)::bigint AS "messagesEpoch"
|
||||
FROM chat_members AS cm JOIN chats AS c ON c.id = cm.chat_id
|
||||
WHERE cm.chat_id = ${id}::uuid AND cm.user_id = ${userId}::uuid
|
||||
`);
|
||||
if (!membership) {
|
||||
throw new ApiException(
|
||||
HttpStatus.NOT_FOUND,
|
||||
'not_found',
|
||||
'Объект не найден',
|
||||
);
|
||||
}
|
||||
return membership;
|
||||
}
|
||||
|
||||
private getMetadata(id: string): Promise<Chat> {
|
||||
return this.cache.remember(
|
||||
`chat:${id}`,
|
||||
'metadata',
|
||||
CACHE_TTL.entity,
|
||||
async () => {
|
||||
const chat = await this.prisma.chat.findUnique({
|
||||
where: { id },
|
||||
include: chatInclude,
|
||||
});
|
||||
if (!chat)
|
||||
throw new ApiException(
|
||||
HttpStatus.NOT_FOUND,
|
||||
'not_found',
|
||||
'Объект не найден',
|
||||
);
|
||||
return this.serializeChat(chat);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async createMessage(
|
||||
chatId: unknown,
|
||||
value: unknown,
|
||||
userId: string,
|
||||
keyValue: unknown,
|
||||
): Promise<Message> {
|
||||
const id = uuid(chatId, 'chat_id');
|
||||
if (
|
||||
typeof keyValue !== 'string' ||
|
||||
!/^[A-Za-z0-9._:-]{1,128}$/.test(keyValue)
|
||||
) {
|
||||
return invalidRequest('Некорректный Idempotency-Key');
|
||||
}
|
||||
const body = objectBody(value, ['text', 'attachment_ids']);
|
||||
const ids = body.attachment_ids === undefined ? [] : body.attachment_ids;
|
||||
if (!Array.isArray(ids) || ids.length > 10)
|
||||
return invalidRequest('Некорректный attachment_ids');
|
||||
const attachmentIds = ids.map((value: unknown) =>
|
||||
uuid(value, 'attachment_ids'),
|
||||
);
|
||||
if (new Set(attachmentIds).size !== attachmentIds.length)
|
||||
return invalidRequest('Дубликат attachment_ids');
|
||||
if (
|
||||
typeof body.text !== 'string' ||
|
||||
Array.from(body.text).length > 4000 ||
|
||||
(body.text.trim().length === 0 && attachmentIds.length === 0)
|
||||
) {
|
||||
return invalidRequest('Некорректный text');
|
||||
}
|
||||
const text = body.text;
|
||||
await this.authorizeChat(id, userId);
|
||||
const count = await this.prisma.attachment.count({
|
||||
where: { id: { in: attachmentIds }, chatId: id },
|
||||
});
|
||||
if (count !== attachmentIds.length)
|
||||
throw new ApiException(404, 'not_found', 'Вложение не найдено');
|
||||
const sqlAttachmentIds = `{${attachmentIds.join(',')}}`;
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
await tx.$executeRaw(Prisma.sql`
|
||||
DELETE FROM message_idempotency
|
||||
WHERE user_id = ${userId}::uuid AND chat_id = ${id}::uuid
|
||||
AND "key" = ${keyValue} AND expires_at <= NOW()
|
||||
`);
|
||||
const claimed = await tx.$queryRaw<{ key: string }[]>(Prisma.sql`
|
||||
INSERT INTO message_idempotency (user_id, chat_id, "key", expires_at)
|
||||
VALUES (
|
||||
${userId}::uuid, ${id}::uuid, ${keyValue},
|
||||
NOW() + (${this.idempotencyTtlSeconds} * INTERVAL '1 second')
|
||||
)
|
||||
ON CONFLICT (user_id, chat_id, "key") DO NOTHING
|
||||
RETURNING "key"
|
||||
`);
|
||||
if (claimed.length === 0) {
|
||||
const [existing] = await tx.$queryRaw<StoredMessage[]>(Prisma.sql`
|
||||
SELECT m.id, m.chat_id AS "chatId", m.sender_id AS "senderId",
|
||||
m.text, m.attachment_ids AS "attachmentIds", m.created_at AS "createdAt"
|
||||
FROM message_idempotency AS i
|
||||
JOIN messages AS m ON m.id = i.message_id
|
||||
WHERE i.user_id = ${userId}::uuid AND i.chat_id = ${id}::uuid
|
||||
AND i."key" = ${keyValue}
|
||||
`);
|
||||
if (!existing) throw new Error('Idempotency record has no message');
|
||||
if (
|
||||
existing.text !== text ||
|
||||
JSON.stringify(existing.attachmentIds) !==
|
||||
JSON.stringify(attachmentIds)
|
||||
) {
|
||||
throw new ApiException(
|
||||
HttpStatus.CONFLICT,
|
||||
'conflict',
|
||||
'Idempotency-Key уже использован с другим сообщением',
|
||||
);
|
||||
}
|
||||
return this.serializeMessage(existing);
|
||||
}
|
||||
|
||||
const messageId = randomUUID();
|
||||
const bucket = parseInt(messageId.slice(0, 2), 16) % 16;
|
||||
const [message] = await tx.$queryRaw<StoredMessage[]>(Prisma.sql`
|
||||
WITH updated AS (
|
||||
INSERT INTO "public"."chat_message_epochs" AS e (chat_id, bucket, epoch)
|
||||
SELECT cm.chat_id, ${bucket}::smallint, 1 FROM chat_members AS cm
|
||||
WHERE cm.chat_id = ${id}::uuid AND cm.user_id = ${userId}::uuid
|
||||
ON CONFLICT (chat_id, bucket) DO UPDATE SET epoch = e.epoch + 1
|
||||
RETURNING chat_id
|
||||
)
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, attachment_ids)
|
||||
SELECT ${messageId}::uuid, updated.chat_id, ${userId}::uuid, ${text}, ${sqlAttachmentIds}::uuid[] FROM updated
|
||||
RETURNING id, chat_id AS "chatId", sender_id AS "senderId", text, attachment_ids AS "attachmentIds", created_at AS "createdAt"
|
||||
`);
|
||||
if (!message)
|
||||
throw new ApiException(
|
||||
HttpStatus.NOT_FOUND,
|
||||
'not_found',
|
||||
'Объект не найден',
|
||||
);
|
||||
await tx.$executeRaw(Prisma.sql`
|
||||
UPDATE message_idempotency SET message_id = ${message.id}::uuid
|
||||
WHERE user_id = ${userId}::uuid AND chat_id = ${id}::uuid
|
||||
AND "key" = ${keyValue}
|
||||
`);
|
||||
return this.serializeMessage(message);
|
||||
});
|
||||
}
|
||||
|
||||
async listMessages(
|
||||
chatId: unknown,
|
||||
userId: string,
|
||||
limitValue: unknown,
|
||||
cursorValue: unknown,
|
||||
access?: ChatAccess,
|
||||
): Promise<Page<Message>> {
|
||||
const id = uuid(chatId, 'chat_id');
|
||||
const chat =
|
||||
access?.chatId === id && access.userId === userId
|
||||
? { id, messagesEpoch: access.messagesEpoch }
|
||||
: await this.authorizeChat(id, userId);
|
||||
const limit = pageLimit(limitValue);
|
||||
const scope = `messages:${chat.id}`;
|
||||
const cursor = this.decodeCursor(cursorValue, scope);
|
||||
return this.cache.remember(
|
||||
scope,
|
||||
{ limit, cursor },
|
||||
CACHE_TTL.page,
|
||||
async () => {
|
||||
const messages = cursor
|
||||
? await this.prisma.$queryRaw<StoredMessage[]>(Prisma.sql`
|
||||
SELECT id, chat_id AS "chatId", sender_id AS "senderId",
|
||||
text, attachment_ids AS "attachmentIds", created_at AS "createdAt"
|
||||
FROM "public"."messages"
|
||||
WHERE chat_id = ${chat.id}::uuid
|
||||
AND (created_at, id) >
|
||||
(${new Date(cursor.created_at)}::timestamptz, ${cursor.id}::uuid)
|
||||
ORDER BY created_at ASC, id ASC
|
||||
LIMIT ${limit + 1}
|
||||
`)
|
||||
: await this.prisma.message.findMany({
|
||||
where: { chatId: chat.id },
|
||||
orderBy: [{ createdAt: 'asc' }, { id: 'asc' }],
|
||||
take: limit + 1,
|
||||
});
|
||||
return this.page(
|
||||
messages.map((message) => this.serializeMessage(message)),
|
||||
limit,
|
||||
scope,
|
||||
);
|
||||
},
|
||||
chat.messagesEpoch.toString(),
|
||||
);
|
||||
}
|
||||
|
||||
private after(cursor: Cursor | null) {
|
||||
if (!cursor) return {};
|
||||
const createdAt = new Date(cursor.created_at);
|
||||
return {
|
||||
OR: [
|
||||
{ createdAt: { gt: createdAt } },
|
||||
{ createdAt, id: { gt: cursor.id } },
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
private serializeChat(chat: StoredChat): Chat {
|
||||
return {
|
||||
id: chat.id,
|
||||
title: chat.title,
|
||||
member_ids: chat.members.map((member) => member.userId),
|
||||
created_at: chat.createdAt.toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
private serializeMessage(message: StoredMessage): Message {
|
||||
return {
|
||||
id: message.id,
|
||||
chat_id: message.chatId,
|
||||
sender_id: message.senderId,
|
||||
text: message.text,
|
||||
attachment_ids: message.attachmentIds,
|
||||
created_at: message.createdAt.toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
private page<T extends { id: string; created_at: string }>(
|
||||
items: T[],
|
||||
limit: number,
|
||||
scope: string,
|
||||
): Page<T> {
|
||||
const result = items.slice(0, limit);
|
||||
const last = result.at(-1);
|
||||
return {
|
||||
items: result,
|
||||
next_cursor:
|
||||
items.length > limit && last
|
||||
? Buffer.from(
|
||||
JSON.stringify({
|
||||
scope,
|
||||
created_at: last.created_at,
|
||||
id: last.id,
|
||||
}),
|
||||
).toString('base64url')
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
private decodeCursor(value: unknown, scope: string): Cursor | null {
|
||||
if (value === undefined) return null;
|
||||
if (typeof value !== 'string' || !/^[A-Za-z0-9_-]+$/.test(value))
|
||||
return invalidRequest('Некорректный cursor');
|
||||
try {
|
||||
const cursor: unknown = JSON.parse(
|
||||
Buffer.from(value, 'base64url').toString('utf8'),
|
||||
);
|
||||
if (
|
||||
typeof cursor !== 'object' ||
|
||||
cursor === null ||
|
||||
Array.isArray(cursor)
|
||||
)
|
||||
return invalidRequest('Некорректный cursor');
|
||||
const data = cursor as Record<string, unknown>;
|
||||
if (
|
||||
data.scope !== scope ||
|
||||
typeof data.created_at !== 'string' ||
|
||||
Number.isNaN(Date.parse(data.created_at)) ||
|
||||
typeof data.id !== 'string'
|
||||
) {
|
||||
return invalidRequest('Некорректный cursor');
|
||||
}
|
||||
return {
|
||||
scope,
|
||||
created_at: data.created_at,
|
||||
id: uuid(data.id, 'cursor'),
|
||||
};
|
||||
} catch {
|
||||
return invalidRequest('Некорректный cursor');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { StorageService } from './storage.service';
|
||||
|
||||
@Module({ providers: [StorageService], exports: [StorageService] })
|
||||
export class StorageModule {}
|
||||
@@ -0,0 +1,112 @@
|
||||
import { S3Client } from '@aws-sdk/client-s3';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { mkdtemp, readFile, rm, rmdir } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { Readable } from 'node:stream';
|
||||
import { StorageService } from './storage.service';
|
||||
|
||||
describe('direct upload storage constraints', () => {
|
||||
let storage: StorageService;
|
||||
let directory: string;
|
||||
let saved: Record<string, string | undefined>;
|
||||
const names = [
|
||||
'S3_ACCESS_KEY_ID',
|
||||
'S3_SECRET_ACCESS_KEY',
|
||||
'S3_ACCESS_KEY_ID_FILE',
|
||||
'S3_SECRET_ACCESS_KEY_FILE',
|
||||
'S3_PUBLIC_ENDPOINT',
|
||||
];
|
||||
const data = Buffer.from('streamed fixture');
|
||||
const sha256 = createHash('sha256').update(data).digest('hex');
|
||||
beforeEach(async () => {
|
||||
saved = Object.fromEntries(names.map((name) => [name, process.env[name]]));
|
||||
for (const name of names) delete process.env[name];
|
||||
process.env.S3_ACCESS_KEY_ID = 'UNITONLY';
|
||||
process.env.S3_SECRET_ACCESS_KEY = 'unit-only-synthetic-secret';
|
||||
process.env.S3_PUBLIC_ENDPOINT = 'http://127.0.0.1:9000';
|
||||
storage = new StorageService();
|
||||
directory = await mkdtemp(join(tmpdir(), 'storage-unit-'));
|
||||
});
|
||||
afterEach(async () => {
|
||||
jest.restoreAllMocks();
|
||||
storage.onModuleDestroy();
|
||||
await rm(join(directory, 'image'), { force: true });
|
||||
await rmdir(directory);
|
||||
for (const name of names) {
|
||||
if (saved[name] === undefined) delete process.env[name];
|
||||
else process.env[name] = saved[name];
|
||||
}
|
||||
});
|
||||
it('binds size, MIME, checksum and create-only semantics into the signature', async () => {
|
||||
const grant = await storage.presignPut(
|
||||
'staging/test/object',
|
||||
data.length,
|
||||
'image/png',
|
||||
sha256,
|
||||
60,
|
||||
new Date(),
|
||||
);
|
||||
const url = new URL(grant.url);
|
||||
const signed = url.searchParams.get('X-Amz-SignedHeaders')?.split(';');
|
||||
expect(signed).toEqual(
|
||||
expect.arrayContaining([
|
||||
'content-length',
|
||||
'content-type',
|
||||
'if-none-match',
|
||||
'x-amz-checksum-sha256',
|
||||
]),
|
||||
);
|
||||
expect(grant.headers['If-None-Match']).toBe('*');
|
||||
expect(url.origin).toBe('http://127.0.0.1:9000');
|
||||
expect(url.searchParams.get('X-Amz-Expires')).toBe('60');
|
||||
});
|
||||
it('hashes a bounded stream to disk, returning its source ETag', async () => {
|
||||
jest
|
||||
.spyOn(S3Client.prototype, 'send')
|
||||
.mockResolvedValue({
|
||||
Body: Readable.from([data]),
|
||||
ContentLength: data.length,
|
||||
ContentType: 'image/png',
|
||||
ETag: '"etag"',
|
||||
} as never);
|
||||
await expect(
|
||||
storage.inspectToFile('staging/test/object', join(directory, 'image'), {
|
||||
sizeBytes: data.length,
|
||||
sha256,
|
||||
contentType: 'image/png',
|
||||
}),
|
||||
).resolves.toBe('"etag"');
|
||||
expect(await readFile(join(directory, 'image'))).toEqual(data);
|
||||
});
|
||||
it('rejects a real checksum mismatch and a stream larger than its declared length', async () => {
|
||||
const send = jest.spyOn(S3Client.prototype, 'send');
|
||||
send.mockResolvedValueOnce({
|
||||
Body: Readable.from([data]),
|
||||
ContentLength: data.length,
|
||||
ContentType: 'image/png',
|
||||
ETag: '"etag"',
|
||||
} as never);
|
||||
await expect(
|
||||
storage.inspectToFile('staging/test/object', join(directory, 'image'), {
|
||||
sizeBytes: data.length,
|
||||
sha256: '0'.repeat(64),
|
||||
contentType: 'image/png',
|
||||
}),
|
||||
).rejects.toMatchObject({ status: 400 });
|
||||
await rm(join(directory, 'image'));
|
||||
send.mockResolvedValueOnce({
|
||||
Body: Readable.from([data, data]),
|
||||
ContentLength: data.length,
|
||||
ContentType: 'image/png',
|
||||
ETag: '"etag"',
|
||||
} as never);
|
||||
await expect(
|
||||
storage.inspectToFile('staging/test/object', join(directory, 'image'), {
|
||||
sizeBytes: data.length,
|
||||
sha256,
|
||||
contentType: 'image/png',
|
||||
}),
|
||||
).rejects.toMatchObject({ status: 413 });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,343 @@
|
||||
import { Injectable, type OnModuleDestroy } from '@nestjs/common';
|
||||
import { createWriteStream, readFileSync } from 'node:fs';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { Readable, Transform } from 'node:stream';
|
||||
import { pipeline } from 'node:stream/promises';
|
||||
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
||||
import {
|
||||
S3Client,
|
||||
GetBucketLocationCommand,
|
||||
PutObjectCommand,
|
||||
GetObjectCommand,
|
||||
DeleteObjectCommand,
|
||||
HeadObjectCommand,
|
||||
ListObjectsV2Command,
|
||||
CopyObjectCommand,
|
||||
} from '@aws-sdk/client-s3';
|
||||
import { ApiException } from '../common/exceptions/api.exception';
|
||||
import { profileStage } from '../common/http/upload-memory-profile';
|
||||
|
||||
export interface StoredObject {
|
||||
key: string;
|
||||
lastModified?: Date;
|
||||
}
|
||||
|
||||
function credential(name: string): string {
|
||||
const file = process.env[`${name}_FILE`];
|
||||
const value = file ? readFileSync(file, 'utf8').trim() : process.env[name];
|
||||
if (!value) throw new Error(`${name} or ${name}_FILE is required`);
|
||||
return value;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class StorageService implements OnModuleDestroy {
|
||||
private readonly bucket = process.env.S3_BUCKET ?? 'messenger-attachments';
|
||||
private readonly client: S3Client;
|
||||
private readonly signer: S3Client;
|
||||
|
||||
constructor() {
|
||||
this.client = new S3Client({
|
||||
endpoint: process.env.S3_ENDPOINT ?? 'http://s3:9000',
|
||||
region: process.env.S3_REGION ?? 'us-east-1',
|
||||
forcePathStyle: true,
|
||||
maxAttempts: 1,
|
||||
credentials: {
|
||||
accessKeyId: credential('S3_ACCESS_KEY_ID'),
|
||||
secretAccessKey: credential('S3_SECRET_ACCESS_KEY'),
|
||||
},
|
||||
requestHandler: { connectionTimeout: 2000, socketTimeout: 5000 },
|
||||
});
|
||||
const endpoint = new URL(
|
||||
process.env.S3_PUBLIC_ENDPOINT ?? 'http://127.0.0.1:9000',
|
||||
);
|
||||
if (
|
||||
!['http:', 'https:'].includes(endpoint.protocol) ||
|
||||
endpoint.username ||
|
||||
endpoint.password ||
|
||||
endpoint.search ||
|
||||
endpoint.hash ||
|
||||
endpoint.pathname !== '/'
|
||||
) {
|
||||
throw new Error(
|
||||
'S3_PUBLIC_ENDPOINT must be an HTTP(S) origin without credentials or a path',
|
||||
);
|
||||
}
|
||||
this.signer = new S3Client({
|
||||
endpoint: endpoint.origin,
|
||||
region: process.env.S3_REGION ?? 'us-east-1',
|
||||
forcePathStyle: true,
|
||||
requestChecksumCalculation: 'WHEN_REQUIRED',
|
||||
credentials: {
|
||||
accessKeyId: credential('S3_ACCESS_KEY_ID'),
|
||||
secretAccessKey: credential('S3_SECRET_ACCESS_KEY'),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async presignPut(
|
||||
key: string,
|
||||
size: number,
|
||||
contentType: string,
|
||||
sha256: string,
|
||||
seconds: number,
|
||||
now: Date,
|
||||
) {
|
||||
const checksum = Buffer.from(sha256, 'hex').toString('base64');
|
||||
const url = await getSignedUrl(
|
||||
this.signer,
|
||||
new PutObjectCommand({
|
||||
Bucket: this.bucket,
|
||||
Key: key,
|
||||
ContentLength: size,
|
||||
ContentType: contentType,
|
||||
ChecksumSHA256: checksum,
|
||||
IfNoneMatch: '*',
|
||||
}),
|
||||
{
|
||||
expiresIn: seconds,
|
||||
signingDate: now,
|
||||
signableHeaders: new Set([
|
||||
'content-length',
|
||||
'content-type',
|
||||
'if-none-match',
|
||||
]),
|
||||
unhoistableHeaders: new Set(['x-amz-checksum-sha256']),
|
||||
},
|
||||
);
|
||||
const signed =
|
||||
new URL(url).searchParams.get('X-Amz-SignedHeaders')?.split(';') ?? [];
|
||||
if (
|
||||
![
|
||||
'content-length',
|
||||
'content-type',
|
||||
'if-none-match',
|
||||
'x-amz-checksum-sha256',
|
||||
].every((name) => signed.includes(name))
|
||||
) {
|
||||
throw new Error('Presigner omitted a required upload constraint');
|
||||
}
|
||||
return {
|
||||
url,
|
||||
headers: {
|
||||
'Content-Type': contentType,
|
||||
'Content-Length': String(size),
|
||||
'If-None-Match': '*',
|
||||
'x-amz-checksum-sha256': checksum,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async inspectToFile(
|
||||
key: string,
|
||||
filename: string,
|
||||
expected: { sizeBytes: number; contentType: string; sha256: string },
|
||||
) {
|
||||
return this.run(async () => {
|
||||
let object;
|
||||
try {
|
||||
object = await this.client.send(
|
||||
new GetObjectCommand({ Bucket: this.bucket, Key: key }),
|
||||
{ abortSignal: AbortSignal.timeout(15000) },
|
||||
);
|
||||
} catch (error) {
|
||||
if (
|
||||
typeof error === 'object' &&
|
||||
error !== null &&
|
||||
'$metadata' in error &&
|
||||
(error.$metadata as { httpStatusCode?: number } | undefined)
|
||||
?.httpStatusCode === 404
|
||||
) {
|
||||
throw new ApiException(409, 'conflict', 'Файл ещё не загружен');
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
if (!(object.Body instanceof Readable))
|
||||
throw new Error('Missing S3 body');
|
||||
const body = object.Body;
|
||||
try {
|
||||
if (object.ContentLength !== expected.sizeBytes)
|
||||
throw new ApiException(
|
||||
(object.ContentLength ?? 0) > 10485760 ? 413 : 400,
|
||||
(object.ContentLength ?? 0) > 10485760
|
||||
? 'payload_too_large'
|
||||
: 'invalid_request',
|
||||
'Размер файла не совпадает с разрешением',
|
||||
);
|
||||
if (object.ContentType !== expected.contentType)
|
||||
throw new ApiException(
|
||||
415,
|
||||
'unsupported_media_type',
|
||||
'Content-Type не совпадает с разрешением',
|
||||
);
|
||||
if (!object.ETag) throw new Error('S3 did not provide an ETag');
|
||||
const hash = createHash('sha256');
|
||||
let received = 0;
|
||||
const bounded = new Transform({
|
||||
transform(chunk: Buffer, _encoding, done) {
|
||||
received += chunk.length;
|
||||
if (received > expected.sizeBytes) {
|
||||
done(
|
||||
new ApiException(
|
||||
413,
|
||||
'payload_too_large',
|
||||
'Превышен разрешённый размер',
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
hash.update(chunk);
|
||||
done(null, chunk);
|
||||
},
|
||||
});
|
||||
profileStage('s3_stream_sha256_to_file');
|
||||
await pipeline(
|
||||
body,
|
||||
bounded,
|
||||
createWriteStream(filename, { flags: 'wx', mode: 0o600 }),
|
||||
{ signal: AbortSignal.timeout(15000) },
|
||||
);
|
||||
profileStage('sha256_finalize');
|
||||
if (
|
||||
received !== expected.sizeBytes ||
|
||||
hash.digest('hex') !== expected.sha256
|
||||
)
|
||||
throw new ApiException(
|
||||
400,
|
||||
'invalid_request',
|
||||
'Checksum или размер файла не совпадает',
|
||||
);
|
||||
return object.ETag;
|
||||
} finally {
|
||||
body.destroy();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async publishCopy(
|
||||
source: string,
|
||||
destination: string,
|
||||
etag: string,
|
||||
): Promise<void> {
|
||||
await this.run(() =>
|
||||
this.client.send(
|
||||
new CopyObjectCommand({
|
||||
Bucket: this.bucket,
|
||||
Key: destination,
|
||||
CopySource: `${this.bucket}/${source.split('/').map(encodeURIComponent).join('/')}`,
|
||||
CopySourceIfMatch: etag,
|
||||
MetadataDirective: 'COPY',
|
||||
}),
|
||||
{ abortSignal: AbortSignal.timeout(5000) },
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async ready(): Promise<void> {
|
||||
await this.run(() =>
|
||||
this.client.send(new GetBucketLocationCommand({ Bucket: this.bucket }), {
|
||||
abortSignal: AbortSignal.timeout(5000),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async put(key: string, bytes: Buffer, contentType: string): Promise<void> {
|
||||
await this.run(() =>
|
||||
this.client.send(
|
||||
new PutObjectCommand({
|
||||
Bucket: this.bucket,
|
||||
Key: key,
|
||||
Body: bytes,
|
||||
ContentLength: bytes.length,
|
||||
ContentType: contentType,
|
||||
}),
|
||||
{ abortSignal: AbortSignal.timeout(5000) },
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async get(key: string): Promise<Readable> {
|
||||
return this.run(async () => {
|
||||
const result = await this.client.send(
|
||||
new GetObjectCommand({ Bucket: this.bucket, Key: key }),
|
||||
{ abortSignal: AbortSignal.timeout(5000) },
|
||||
);
|
||||
if (!(result.Body instanceof Readable))
|
||||
throw new Error('Missing S3 body');
|
||||
return result.Body;
|
||||
});
|
||||
}
|
||||
|
||||
async remove(key: string): Promise<void> {
|
||||
await this.run(() =>
|
||||
this.client.send(
|
||||
new DeleteObjectCommand({ Bucket: this.bucket, Key: key }),
|
||||
{ abortSignal: AbortSignal.timeout(5000) },
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async *objects(prefix: string): AsyncGenerator<StoredObject> {
|
||||
let token: string | undefined;
|
||||
do {
|
||||
const page = await this.run(() =>
|
||||
this.client.send(
|
||||
new ListObjectsV2Command({
|
||||
Bucket: this.bucket,
|
||||
Prefix: prefix,
|
||||
MaxKeys: 200,
|
||||
ContinuationToken: token,
|
||||
}),
|
||||
{ abortSignal: AbortSignal.timeout(5000) },
|
||||
),
|
||||
);
|
||||
for (const object of page.Contents ?? []) {
|
||||
if (object.Key)
|
||||
yield { key: object.Key, lastModified: object.LastModified };
|
||||
}
|
||||
if (!page.IsTruncated) break;
|
||||
if (!page.NextContinuationToken || page.NextContinuationToken === token) {
|
||||
throw new ApiException(503, 'unavailable', 'Некорректная страница S3');
|
||||
}
|
||||
token = page.NextContinuationToken;
|
||||
} while (token);
|
||||
}
|
||||
|
||||
async stat(key: string): Promise<StoredObject | null> {
|
||||
return this.run(async () => {
|
||||
try {
|
||||
const object = await this.client.send(
|
||||
new HeadObjectCommand({ Bucket: this.bucket, Key: key }),
|
||||
{ abortSignal: AbortSignal.timeout(5000) },
|
||||
);
|
||||
return { key, lastModified: object.LastModified };
|
||||
} catch (error) {
|
||||
if (
|
||||
typeof error === 'object' &&
|
||||
error !== null &&
|
||||
'$metadata' in error
|
||||
) {
|
||||
const metadata = error.$metadata as
|
||||
{ httpStatusCode?: number } | undefined;
|
||||
if (metadata?.httpStatusCode === 404) return null;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
onModuleDestroy(): void {
|
||||
this.client.destroy();
|
||||
this.signer.destroy();
|
||||
}
|
||||
|
||||
private async run<T>(operation: () => Promise<T>): Promise<T> {
|
||||
try {
|
||||
return await operation();
|
||||
} catch (cause) {
|
||||
if (cause instanceof ApiException) throw cause;
|
||||
throw new ApiException(503, 'unavailable', 'Хранилище недоступно', {
|
||||
cause,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Post,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
ApiBody,
|
||||
ApiBearerAuth,
|
||||
ApiCreatedResponse,
|
||||
ApiOkResponse,
|
||||
ApiOperation,
|
||||
ApiTags,
|
||||
} from '@nestjs/swagger';
|
||||
import { UserGuard, type AuthenticatedRequest } from './user.guard';
|
||||
import { CreateUserDto, User } from './user.schema';
|
||||
import { UserService } from './user.service';
|
||||
|
||||
@ApiTags('users')
|
||||
@Controller('api/v1/users')
|
||||
export class UserController {
|
||||
constructor(private readonly users: UserService) {}
|
||||
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
@ApiBody({ type: CreateUserDto })
|
||||
@ApiOperation({ operationId: 'registerUser' })
|
||||
@ApiCreatedResponse({ type: User })
|
||||
create(@Body() body: unknown) {
|
||||
return this.users.create(body);
|
||||
}
|
||||
|
||||
@Get('me')
|
||||
@UseGuards(UserGuard)
|
||||
@ApiBearerAuth('bearerAuth')
|
||||
@ApiOperation({ operationId: 'currentUser' })
|
||||
@ApiOkResponse({ type: User })
|
||||
me(@Req() request: AuthenticatedRequest) {
|
||||
return request.currentUser;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
||||
import type { FastifyRequest } from 'fastify';
|
||||
import { AuthService, type ChatAccess } from '../Auth/auth.service';
|
||||
import type { User } from './user.schema';
|
||||
|
||||
export type AuthenticatedRequest = FastifyRequest & {
|
||||
currentUser: User;
|
||||
currentSessionId: string;
|
||||
currentChatsEpoch: string;
|
||||
currentChatAccess?: ChatAccess;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class UserGuard implements CanActivate {
|
||||
constructor(private readonly auth: AuthService) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const request = context.switchToHttp().getRequest<AuthenticatedRequest>();
|
||||
const chatId =
|
||||
request.method === 'GET' &&
|
||||
request.routeOptions.url === '/api/v1/chats/:chat_id/messages'
|
||||
? (request.params as { chat_id: string }).chat_id
|
||||
: undefined;
|
||||
const session = await this.auth.authenticate(
|
||||
request.headers.authorization,
|
||||
chatId,
|
||||
);
|
||||
request.currentUser = session.user;
|
||||
request.currentSessionId = session.sessionId;
|
||||
request.currentChatsEpoch = session.chatsEpoch;
|
||||
request.currentChatAccess = session.chatAccess;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UserController } from './user.controller';
|
||||
import { UserGuard } from './user.guard';
|
||||
import { UserService } from './user.service';
|
||||
import { AuthController } from '../Auth/auth.controller';
|
||||
import { AuthService } from '../Auth/auth.service';
|
||||
import { PasswordService } from '../Auth/password.service';
|
||||
|
||||
@Module({
|
||||
controllers: [UserController, AuthController],
|
||||
providers: [UserService, UserGuard, AuthService, PasswordService],
|
||||
exports: [UserService, UserGuard, AuthService],
|
||||
})
|
||||
export class UserModule {}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class CreateUserDto {
|
||||
@ApiProperty({ example: 'alice', pattern: '^[a-z0-9_]{3,32}$' })
|
||||
username!: string;
|
||||
|
||||
@ApiProperty({ example: 'Алиса', maxLength: 100 })
|
||||
display_name!: string;
|
||||
|
||||
@ApiProperty({
|
||||
writeOnly: true,
|
||||
minLength: 12,
|
||||
maxLength: 128,
|
||||
example: 'Example-only-not-a-real-secret-123',
|
||||
})
|
||||
password!: string;
|
||||
}
|
||||
|
||||
export class User {
|
||||
@ApiProperty({ format: 'uuid' })
|
||||
id!: string;
|
||||
@ApiProperty({ pattern: '^[a-z0-9_]{3,32}$' })
|
||||
username!: string;
|
||||
@ApiProperty({ minLength: 1, maxLength: 100 })
|
||||
display_name!: string;
|
||||
@ApiProperty({ format: 'date-time' })
|
||||
created_at!: string;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import { HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { Prisma, type User as StoredUser } from '../generated/prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { ApiException } from '../common/exceptions/api.exception';
|
||||
import { objectBody, requiredText } from '../common/validation';
|
||||
import {
|
||||
PasswordService,
|
||||
registrationPassword,
|
||||
} from '../Auth/password.service';
|
||||
import type { User } from './user.schema';
|
||||
import { CacheService, CACHE_TTL } from '../cache/cache.service';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly passwords: PasswordService,
|
||||
private readonly cache: CacheService,
|
||||
) {}
|
||||
|
||||
async create(value: unknown): Promise<User> {
|
||||
const body = objectBody(value, ['username', 'display_name', 'password']);
|
||||
const username = body.username;
|
||||
if (typeof username !== 'string' || !/^[a-z0-9_]{3,32}$/.test(username)) {
|
||||
throw new ApiException(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
'invalid_request',
|
||||
'Некорректный username',
|
||||
);
|
||||
}
|
||||
const displayName = requiredText(body.display_name, 'display_name', 100);
|
||||
const password = registrationPassword(body.password);
|
||||
const passwordHash = await this.passwords.hash(password);
|
||||
try {
|
||||
return this.serialize(
|
||||
await this.prisma.user.create({
|
||||
data: { username, displayName, passwordHash },
|
||||
}),
|
||||
);
|
||||
} catch (error) {
|
||||
if (
|
||||
error instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
error.code === 'P2002'
|
||||
) {
|
||||
throw new ApiException(
|
||||
HttpStatus.CONFLICT,
|
||||
'conflict',
|
||||
'Username уже занят',
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async get(id: string): Promise<User | undefined> {
|
||||
return this.cache.remember(
|
||||
`user:${id}`,
|
||||
'profile',
|
||||
CACHE_TTL.entity,
|
||||
async () => {
|
||||
const user = await this.prisma.user.findUnique({ where: { id } });
|
||||
return user ? this.serialize(user) : undefined;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
serialize(
|
||||
user: Pick<StoredUser, 'id' | 'username' | 'displayName' | 'createdAt'>,
|
||||
): User {
|
||||
return {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
display_name: user.displayName,
|
||||
created_at: user.createdAt.toISOString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { PrismaService } from './prisma/prisma.service';
|
||||
import { StorageService } from './Storage/storage.service';
|
||||
|
||||
describe('AppController', () => {
|
||||
let appController: AppController;
|
||||
const queryRaw = jest.fn();
|
||||
|
||||
beforeEach(async () => {
|
||||
queryRaw.mockReset();
|
||||
const app: TestingModule = await Test.createTestingModule({
|
||||
controllers: [AppController],
|
||||
providers: [
|
||||
AppService,
|
||||
{ provide: PrismaService, useValue: { $queryRaw: queryRaw } },
|
||||
{
|
||||
provide: StorageService,
|
||||
useValue: { ready: jest.fn().mockResolvedValue(undefined) },
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
appController = app.get<AppController>(AppController);
|
||||
});
|
||||
|
||||
describe('root', () => {
|
||||
it('should return "Hello World!"', () => {
|
||||
expect(appController.getHello()).toBe('Hello World!');
|
||||
});
|
||||
});
|
||||
|
||||
it('reports ready when PostgreSQL is reachable', async () => {
|
||||
queryRaw.mockResolvedValue([{ '?column?': 1 }]);
|
||||
await expect(appController.ready()).resolves.toEqual({ status: 'ok' });
|
||||
});
|
||||
|
||||
it('returns 503 when PostgreSQL is unavailable', async () => {
|
||||
queryRaw.mockRejectedValue(new Error('connection refused'));
|
||||
await expect(appController.ready()).rejects.toMatchObject({ status: 503 });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { Controller, Get, HttpStatus } from '@nestjs/common';
|
||||
import { HttpAdapterHost } from '@nestjs/core';
|
||||
import { AppService } from './app.service';
|
||||
import { PrismaService } from './prisma/prisma.service';
|
||||
import { ApiException } from './common/exceptions/api.exception';
|
||||
import { DrainingFastifyAdapter } from './common/http/fastify-adapter';
|
||||
import { StorageService } from './Storage/storage.service';
|
||||
|
||||
@Controller()
|
||||
export class AppController {
|
||||
constructor(
|
||||
private readonly appService: AppService,
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly adapterHost: HttpAdapterHost,
|
||||
private readonly storage: StorageService,
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
getHello(): string {
|
||||
return this.appService.getHello();
|
||||
}
|
||||
|
||||
@Get('health/live')
|
||||
live() {
|
||||
return { status: 'ok' };
|
||||
}
|
||||
|
||||
@Get('health/ready')
|
||||
async ready() {
|
||||
this.assertNotDraining();
|
||||
try {
|
||||
await this.prisma.$queryRaw`SELECT 1`;
|
||||
} catch {
|
||||
throw new ApiException(
|
||||
HttpStatus.SERVICE_UNAVAILABLE,
|
||||
'unavailable',
|
||||
'База данных недоступна',
|
||||
);
|
||||
}
|
||||
await this.storage.ready();
|
||||
this.assertNotDraining();
|
||||
return { status: 'ok' };
|
||||
}
|
||||
|
||||
private assertNotDraining(): void {
|
||||
const adapter = this.adapterHost.httpAdapter;
|
||||
if (adapter instanceof DrainingFastifyAdapter && adapter.isDraining) {
|
||||
throw new ApiException(
|
||||
HttpStatus.SERVICE_UNAVAILABLE,
|
||||
'unavailable',
|
||||
'API завершает работу',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { APP_FILTER } from '@nestjs/core';
|
||||
import { LoggerModule } from 'nestjs-pino';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { AllExceptionsFilter } from './common/filters/all-exceptions.filter';
|
||||
import { UserModule } from './User/user.module';
|
||||
import { ChatModule } from './Chat/chat.module';
|
||||
import { PrismaModule } from './prisma/prisma.module';
|
||||
import { CacheModule } from './cache/cache.module';
|
||||
import { AttachmentModule } from './Attachment/attachment.module';
|
||||
import { StorageModule } from './Storage/storage.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
PrismaModule,
|
||||
CacheModule,
|
||||
UserModule,
|
||||
ChatModule,
|
||||
AttachmentModule,
|
||||
StorageModule,
|
||||
LoggerModule.forRoot({
|
||||
pinoHttp: {
|
||||
level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
|
||||
|
||||
transport:
|
||||
process.env.NODE_ENV !== 'production'
|
||||
? {
|
||||
target: 'pino-pretty',
|
||||
options: {
|
||||
colorize: true,
|
||||
singleLine: true,
|
||||
translateTime: 'SYS:standard',
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
|
||||
redact: {
|
||||
paths: [
|
||||
'req.headers.authorization',
|
||||
'req.headers.cookie',
|
||||
'res.headers.set-cookie',
|
||||
],
|
||||
censor: '[REDACTED]',
|
||||
},
|
||||
|
||||
autoLogging: {
|
||||
ignore: (request) => request.url === '/health',
|
||||
},
|
||||
},
|
||||
}),
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [
|
||||
AppService,
|
||||
{
|
||||
provide: APP_FILTER,
|
||||
useClass: AllExceptionsFilter,
|
||||
},
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
}
|
||||
}
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { CacheService } from './cache.service';
|
||||
|
||||
@Global()
|
||||
@Module({ providers: [CacheService], exports: [CacheService] })
|
||||
export class CacheModule {}
|
||||
Vendored
+149
@@ -0,0 +1,149 @@
|
||||
import {
|
||||
Injectable,
|
||||
Logger,
|
||||
type OnModuleDestroy,
|
||||
type OnModuleInit,
|
||||
} from '@nestjs/common';
|
||||
import { createHash, randomUUID } from 'node:crypto';
|
||||
import Redis from 'ioredis';
|
||||
|
||||
export const CACHE_TTL = { entity: 300, page: 30 } as const;
|
||||
const generationTtl = 600;
|
||||
const storeIfCurrent = `
|
||||
if redis.call('GET', KEYS[1]) == ARGV[1] then
|
||||
return redis.call('SET', KEYS[2], ARGV[2], 'EX', ARGV[3])
|
||||
end
|
||||
return false`;
|
||||
|
||||
@Injectable()
|
||||
export class CacheService implements OnModuleInit, OnModuleDestroy {
|
||||
private readonly logger = new Logger(CacheService.name);
|
||||
private readonly redis?: Redis;
|
||||
private readonly prefix = process.env.CACHE_PREFIX ?? 'messenger:cache:v1';
|
||||
private warned = false;
|
||||
|
||||
constructor() {
|
||||
if (process.env.CACHE_URL) {
|
||||
this.redis = new Redis(process.env.CACHE_URL, {
|
||||
lazyConnect: true,
|
||||
connectTimeout: 1000,
|
||||
commandTimeout: 300,
|
||||
maxRetriesPerRequest: 0,
|
||||
enableOfflineQueue: false,
|
||||
autoResendUnfulfilledCommands: false,
|
||||
retryStrategy: () => 1000,
|
||||
});
|
||||
this.redis.on('error', () => this.unavailable());
|
||||
this.redis.on('ready', () => {
|
||||
this.warned = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async onModuleInit(): Promise<void> {
|
||||
try {
|
||||
await this.redis?.connect();
|
||||
} catch {
|
||||
this.unavailable();
|
||||
}
|
||||
}
|
||||
|
||||
onModuleDestroy(): void {
|
||||
this.redis?.disconnect();
|
||||
}
|
||||
|
||||
async remember<T>(
|
||||
namespace: string,
|
||||
parameters: unknown,
|
||||
ttl: number,
|
||||
load: () => Promise<T>,
|
||||
epoch?: string,
|
||||
): Promise<T> {
|
||||
if (!this.redis || this.redis.status !== 'ready') return load();
|
||||
if (epoch !== undefined) {
|
||||
const digest = createHash('sha256')
|
||||
.update(JSON.stringify(parameters))
|
||||
.digest('hex');
|
||||
const key = `${this.prefix}:${namespace}:epoch:${epoch}:${digest}`;
|
||||
try {
|
||||
const value = await this.redis.get(key);
|
||||
if (value !== null) return JSON.parse(value) as T;
|
||||
} catch {
|
||||
this.unavailable();
|
||||
}
|
||||
const result = await load();
|
||||
const serialized = JSON.stringify(result);
|
||||
if (serialized !== undefined && this.redis.status === 'ready') {
|
||||
try {
|
||||
await this.redis.set(key, serialized, 'EX', ttl);
|
||||
} catch {
|
||||
this.unavailable();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
const versionKey = `${this.prefix}:${namespace}:generation`;
|
||||
let version: string | null = null;
|
||||
let dataKey = '';
|
||||
try {
|
||||
await this.redis.set(versionKey, randomUUID(), 'EX', generationTtl, 'NX');
|
||||
version = await this.redis.get(versionKey);
|
||||
if (version) {
|
||||
const digest = createHash('sha256')
|
||||
.update(JSON.stringify(parameters))
|
||||
.digest('hex');
|
||||
dataKey = `${this.prefix}:${namespace}:${version}:${digest}`;
|
||||
const value = await this.redis.get(dataKey);
|
||||
if (value !== null) return JSON.parse(value) as T;
|
||||
}
|
||||
} catch {
|
||||
this.unavailable();
|
||||
}
|
||||
|
||||
const result = await load();
|
||||
const serialized = JSON.stringify(result);
|
||||
if (version && serialized !== undefined && this.redis.status === 'ready') {
|
||||
try {
|
||||
await this.redis.eval(
|
||||
storeIfCurrent,
|
||||
2,
|
||||
versionKey,
|
||||
dataKey,
|
||||
version,
|
||||
serialized,
|
||||
ttl,
|
||||
);
|
||||
} catch {
|
||||
this.unavailable();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
async invalidate(...namespaces: string[]): Promise<void> {
|
||||
if (!this.redis || this.redis.status !== 'ready' || namespaces.length === 0)
|
||||
return;
|
||||
try {
|
||||
const transaction = this.redis.multi();
|
||||
for (const namespace of new Set(namespaces)) {
|
||||
transaction.set(
|
||||
`${this.prefix}:${namespace}:generation`,
|
||||
randomUUID(),
|
||||
'EX',
|
||||
generationTtl,
|
||||
);
|
||||
}
|
||||
const results = await transaction.exec();
|
||||
if (results?.some(([error]) => error !== null)) this.unavailable();
|
||||
} catch {
|
||||
this.unavailable();
|
||||
}
|
||||
}
|
||||
|
||||
private unavailable(): void {
|
||||
if (!this.warned) {
|
||||
this.logger.warn('Redis cache unavailable');
|
||||
this.warned = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import type { OpenAPIObject } from '@nestjs/swagger';
|
||||
import { completeApiDocument } from './api-document';
|
||||
|
||||
const contract = JSON.parse(
|
||||
readFileSync(join(__dirname, '../../contracts/openapi.json'), 'utf8'),
|
||||
) as OpenAPIObject;
|
||||
|
||||
describe('published OpenAPI document', () => {
|
||||
const generated: OpenAPIObject = {
|
||||
openapi: '3.0.0',
|
||||
info: { title: 'Messenger API', version: '1.4.0' },
|
||||
paths: {
|
||||
'/': { get: { responses: { 200: { description: 'Root endpoint' } } } },
|
||||
},
|
||||
components: { schemas: {} },
|
||||
};
|
||||
for (const [path, item] of Object.entries(contract.paths)) {
|
||||
generated.paths[path] = {};
|
||||
for (const method of ['get', 'post', 'delete'] as const) {
|
||||
if (item[method]) generated.paths[path][method] = { responses: {} };
|
||||
}
|
||||
}
|
||||
const document = completeApiDocument(generated, contract);
|
||||
|
||||
it('documents every contract operation, including all response headers and binary schemas', () => {
|
||||
expect(document.openapi).toBe(contract.openapi);
|
||||
for (const [path, item] of Object.entries(contract.paths)) {
|
||||
for (const method of ['get', 'post', 'delete'] as const) {
|
||||
if (!item[method]) continue;
|
||||
expect(document.paths[path][method]).toEqual({
|
||||
...item[method],
|
||||
security: item[method].security ?? contract.security ?? [],
|
||||
});
|
||||
}
|
||||
}
|
||||
expect(document.components?.schemas).toMatchObject(
|
||||
contract.components!.schemas!,
|
||||
);
|
||||
});
|
||||
|
||||
it('preserves extra endpoints and leaves the generated document unchanged', () => {
|
||||
expect(document.paths['/']).toEqual(generated.paths['/']);
|
||||
expect(generated.openapi).toBe('3.0.0');
|
||||
expect(
|
||||
generated.paths['/api/v1/attachments/{attachment_id}'].get?.responses,
|
||||
).toEqual({});
|
||||
});
|
||||
|
||||
it('does not advertise contract endpoints missing from the application', () => {
|
||||
const missing = structuredClone(generated);
|
||||
delete missing.paths['/api/v1/attachments/{attachment_id}'];
|
||||
expect(() => completeApiDocument(missing, contract)).toThrow(
|
||||
'Contract endpoint is not implemented',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import type { OpenAPIObject } from '@nestjs/swagger';
|
||||
|
||||
/** Keep the published API documentation aligned with the lab's normative contract. */
|
||||
export function completeApiDocument(
|
||||
generated: OpenAPIObject,
|
||||
contract: OpenAPIObject,
|
||||
): OpenAPIObject {
|
||||
const paths = { ...generated.paths };
|
||||
const methods = [
|
||||
'get',
|
||||
'put',
|
||||
'post',
|
||||
'delete',
|
||||
'options',
|
||||
'head',
|
||||
'patch',
|
||||
'trace',
|
||||
] as const;
|
||||
for (const [path, item] of Object.entries(contract.paths)) {
|
||||
paths[path] = { ...item };
|
||||
for (const method of methods) {
|
||||
const operation = item[method];
|
||||
if (operation) {
|
||||
if (!generated.paths[path]?.[method]) {
|
||||
throw new Error(
|
||||
`Contract endpoint is not implemented: ${method.toUpperCase()} ${path}`,
|
||||
);
|
||||
}
|
||||
paths[path][method] = {
|
||||
...generated.paths[path]?.[method],
|
||||
...operation,
|
||||
security: operation.security ?? contract.security ?? [],
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
...generated,
|
||||
openapi: contract.openapi,
|
||||
info: { ...generated.info, version: contract.info.version },
|
||||
paths,
|
||||
components: {
|
||||
...generated.components,
|
||||
...contract.components,
|
||||
schemas: {
|
||||
...generated.components?.schemas,
|
||||
...contract.components?.schemas,
|
||||
},
|
||||
securitySchemes: {
|
||||
...generated.components?.securitySchemes,
|
||||
...contract.components?.securitySchemes,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import {
|
||||
HttpException,
|
||||
HttpExceptionOptions,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
|
||||
export class ApiException extends HttpException {
|
||||
constructor(
|
||||
status: HttpStatus,
|
||||
code: string,
|
||||
message: string,
|
||||
options?: HttpExceptionOptions,
|
||||
) {
|
||||
super({ code, message }, status, options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
import {
|
||||
ArgumentsHost,
|
||||
Catch,
|
||||
ExceptionFilter,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import type { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { Prisma } from '../../generated/prisma/client';
|
||||
import { ApiException } from '../exceptions/api.exception';
|
||||
import { AuthException } from '../../Auth/auth.exception';
|
||||
|
||||
interface ExceptionPayload {
|
||||
code?: unknown;
|
||||
}
|
||||
|
||||
const defaultCodes: Partial<Record<HttpStatus, string>> = {
|
||||
[HttpStatus.BAD_REQUEST]: 'invalid_request',
|
||||
[HttpStatus.UNAUTHORIZED]: 'unauthorized',
|
||||
[HttpStatus.FORBIDDEN]: 'forbidden',
|
||||
[HttpStatus.NOT_FOUND]: 'not_found',
|
||||
[HttpStatus.CONFLICT]: 'conflict',
|
||||
[HttpStatus.PAYLOAD_TOO_LARGE]: 'payload_too_large',
|
||||
[HttpStatus.UNSUPPORTED_MEDIA_TYPE]: 'unsupported_media_type',
|
||||
[HttpStatus.TOO_MANY_REQUESTS]: 'rate_limited',
|
||||
[HttpStatus.SERVICE_UNAVAILABLE]: 'unavailable',
|
||||
};
|
||||
|
||||
const defaultMessages: Partial<Record<HttpStatus, string>> = {
|
||||
[HttpStatus.BAD_REQUEST]: 'Некорректный запрос',
|
||||
[HttpStatus.UNAUTHORIZED]: 'Требуется авторизация',
|
||||
[HttpStatus.FORBIDDEN]: 'Доступ запрещён',
|
||||
[HttpStatus.NOT_FOUND]: 'Ресурс не найден',
|
||||
[HttpStatus.METHOD_NOT_ALLOWED]: 'Метод не поддерживается',
|
||||
[HttpStatus.CONFLICT]: 'Конфликт данных',
|
||||
[HttpStatus.PAYLOAD_TOO_LARGE]: 'Тело запроса слишком большое',
|
||||
[HttpStatus.UNSUPPORTED_MEDIA_TYPE]: 'Неподдерживаемый формат данных',
|
||||
[HttpStatus.TOO_MANY_REQUESTS]: 'Слишком много запросов',
|
||||
[HttpStatus.INTERNAL_SERVER_ERROR]: 'Внутренняя ошибка сервера',
|
||||
[HttpStatus.SERVICE_UNAVAILABLE]: 'Сервис временно недоступен',
|
||||
};
|
||||
|
||||
const databaseUnavailableCodes = new Set([
|
||||
'P1000',
|
||||
'P1001',
|
||||
'P1002',
|
||||
'P1008',
|
||||
'P1017',
|
||||
'P2024',
|
||||
'P2037',
|
||||
'ECONNREFUSED',
|
||||
'ECONNRESET',
|
||||
'ETIMEDOUT',
|
||||
'EPIPE',
|
||||
'ENOTFOUND',
|
||||
'EAI_AGAIN',
|
||||
'EACCES',
|
||||
'08000',
|
||||
'08001',
|
||||
'08003',
|
||||
'08004',
|
||||
'08006',
|
||||
'53300',
|
||||
'57P01',
|
||||
'57P02',
|
||||
'57P03',
|
||||
'57014',
|
||||
]);
|
||||
const driverTimeoutMessages = [
|
||||
'Query read timeout',
|
||||
'timeout exceeded when trying to connect',
|
||||
'Connection terminated unexpectedly',
|
||||
'Connection terminated due to connection timeout',
|
||||
];
|
||||
|
||||
function databaseUnavailable(exception: unknown): boolean {
|
||||
if (exception instanceof Prisma.PrismaClientInitializationError) return true;
|
||||
if (exception instanceof HttpException || !(exception instanceof Error))
|
||||
return false;
|
||||
const error = exception as Error & { code?: unknown };
|
||||
if (
|
||||
typeof error.code === 'string' &&
|
||||
databaseUnavailableCodes.has(error.code)
|
||||
)
|
||||
return true;
|
||||
if (exception instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
const originalCode = exception.meta?.code;
|
||||
if (
|
||||
typeof originalCode === 'string' &&
|
||||
databaseUnavailableCodes.has(originalCode)
|
||||
)
|
||||
return true;
|
||||
const adapter = exception.meta?.driverAdapterError;
|
||||
if (adapter !== null && typeof adapter === 'object' && 'cause' in adapter) {
|
||||
const cause = adapter.cause;
|
||||
if (cause !== null && typeof cause === 'object') {
|
||||
if ('kind' in cause && cause.kind === 'ConnectionClosed') return true;
|
||||
if (
|
||||
'originalCode' in cause &&
|
||||
typeof cause.originalCode === 'string' &&
|
||||
databaseUnavailableCodes.has(cause.originalCode)
|
||||
)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return driverTimeoutMessages.some((message) =>
|
||||
exception.message.includes(message),
|
||||
);
|
||||
}
|
||||
|
||||
@Catch()
|
||||
export class AllExceptionsFilter implements ExceptionFilter {
|
||||
catch(exception: unknown, host: ArgumentsHost): void {
|
||||
if (databaseUnavailable(exception)) {
|
||||
exception = new ApiException(
|
||||
HttpStatus.SERVICE_UNAVAILABLE,
|
||||
'unavailable',
|
||||
'База данных недоступна',
|
||||
);
|
||||
}
|
||||
const context = host.switchToHttp();
|
||||
const request = context.getRequest<FastifyRequest>();
|
||||
const response = context.getResponse<FastifyReply>();
|
||||
if (exception instanceof AuthException) {
|
||||
void response.header('WWW-Authenticate', exception.challenge);
|
||||
}
|
||||
|
||||
const status =
|
||||
exception instanceof HttpException
|
||||
? exception.getStatus()
|
||||
: exception instanceof Error &&
|
||||
'statusCode' in exception &&
|
||||
typeof exception.statusCode === 'number' &&
|
||||
[400, 413, 415].includes(exception.statusCode)
|
||||
? exception.statusCode
|
||||
: HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
const payload =
|
||||
exception instanceof HttpException ? exception.getResponse() : undefined;
|
||||
const { code, message } = this.extractError(status, payload);
|
||||
const requestId = request.id;
|
||||
|
||||
if (status >= HttpStatus.INTERNAL_SERVER_ERROR) {
|
||||
request.log.error(
|
||||
{ err: exception, status, requestId },
|
||||
'Request failed',
|
||||
);
|
||||
}
|
||||
|
||||
void response.status(status).send({
|
||||
error: { code, message, request_id: requestId },
|
||||
});
|
||||
}
|
||||
|
||||
private extractError(
|
||||
status: HttpStatus,
|
||||
payload: string | object | undefined,
|
||||
): { code: string; message: string } {
|
||||
if (status === HttpStatus.INTERNAL_SERVER_ERROR) {
|
||||
return {
|
||||
code: 'internal_error',
|
||||
message: defaultMessages[status] ?? 'Ошибка сервера',
|
||||
};
|
||||
}
|
||||
|
||||
const body = (
|
||||
payload !== null && typeof payload === 'object' ? payload : {}
|
||||
) as ExceptionPayload;
|
||||
|
||||
return {
|
||||
code:
|
||||
typeof body.code === 'string'
|
||||
? body.code
|
||||
: (defaultCodes[status] ?? 'http_error'),
|
||||
message:
|
||||
defaultMessages[status] ??
|
||||
(status >= HttpStatus.INTERNAL_SERVER_ERROR
|
||||
? 'Ошибка сервера'
|
||||
: 'Ошибка запроса'),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import type { IncomingMessage } from 'node:http';
|
||||
import { PassThrough } from 'node:stream';
|
||||
import { setImmediate as tick } from 'node:timers/promises';
|
||||
import { drainRejectedBody } from './drain-rejected-body';
|
||||
|
||||
describe('bounded discard of rejected upload bodies', () => {
|
||||
it('waits for the complete body before releasing the error response', async () => {
|
||||
const body = new PassThrough();
|
||||
const done = jest.fn();
|
||||
const result = new Promise<boolean>((resolve) =>
|
||||
drainRejectedBody(
|
||||
body as unknown as IncomingMessage,
|
||||
100,
|
||||
1000,
|
||||
(consumed) => {
|
||||
done(consumed);
|
||||
resolve(consumed);
|
||||
},
|
||||
),
|
||||
);
|
||||
body.write('first');
|
||||
await tick();
|
||||
expect(done).not.toHaveBeenCalled();
|
||||
body.end('last');
|
||||
expect(await result).toBe(true);
|
||||
expect(body.readableEnded).toBe(true);
|
||||
expect(done).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('stops draining beyond its byte budget', async () => {
|
||||
const body = new PassThrough();
|
||||
try {
|
||||
const result = new Promise<boolean>((resolve) =>
|
||||
drainRejectedBody(body as unknown as IncomingMessage, 4, 1000, resolve),
|
||||
);
|
||||
body.write('12345');
|
||||
expect(await result).toBe(false);
|
||||
expect(body.isPaused()).toBe(true);
|
||||
} finally {
|
||||
body.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
it('releases a stalled rejected request when its remaining deadline expires', async () => {
|
||||
const body = new PassThrough();
|
||||
try {
|
||||
const result = new Promise<boolean>((resolve) =>
|
||||
drainRejectedBody(body as unknown as IncomingMessage, 100, 25, resolve),
|
||||
);
|
||||
expect(await result).toBe(false);
|
||||
expect(body.isPaused()).toBe(true);
|
||||
} finally {
|
||||
body.destroy();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
import type { IncomingMessage } from 'node:http';
|
||||
|
||||
/** Discard a bounded remainder before sending an early 413 through a streaming proxy. */
|
||||
export function drainRejectedBody(
|
||||
raw: IncomingMessage,
|
||||
maxBytes: number,
|
||||
timeoutMs: number,
|
||||
done: (consumed: boolean) => void,
|
||||
) {
|
||||
let received = 0;
|
||||
let finished = false;
|
||||
const finish = (consumed: boolean) => {
|
||||
if (finished) return;
|
||||
finished = true;
|
||||
clearTimeout(timer);
|
||||
raw.removeListener('data', onData);
|
||||
raw.removeListener('end', onEnd);
|
||||
raw.removeListener('aborted', onAborted);
|
||||
raw.removeListener('error', onAborted);
|
||||
raw.removeListener('close', onClose);
|
||||
done(consumed);
|
||||
};
|
||||
const onData = (chunk: Buffer) => {
|
||||
received += chunk.length;
|
||||
if (received > maxBytes) {
|
||||
raw.pause();
|
||||
finish(false);
|
||||
}
|
||||
};
|
||||
const onEnd = () => finish(true);
|
||||
const onAborted = () => finish(false);
|
||||
const onClose = () => finish(raw.readableEnded);
|
||||
const timer = setTimeout(() => {
|
||||
raw.pause();
|
||||
finish(false);
|
||||
}, timeoutMs);
|
||||
timer.unref();
|
||||
raw.on('data', onData);
|
||||
raw.once('end', onEnd);
|
||||
raw.once('aborted', onAborted);
|
||||
raw.once('error', onAborted);
|
||||
raw.once('close', onClose);
|
||||
raw.resume();
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import { request } from 'node:http';
|
||||
import type { AddressInfo } from 'node:net';
|
||||
import { setTimeout as delay } from 'node:timers/promises';
|
||||
import { DrainingFastifyAdapter } from './fastify-adapter';
|
||||
|
||||
describe('upload body deadline over real HTTP', () => {
|
||||
it('returns 408 for a client that keeps sending and never invokes the handler', async () => {
|
||||
const adapter = new DrainingFastifyAdapter(0, 1000);
|
||||
const server = adapter.getInstance();
|
||||
let calls = 0;
|
||||
server.post('/api/v1/chats/:chat_id/attachments', () => {
|
||||
calls++;
|
||||
return {};
|
||||
});
|
||||
await server.listen({ host: '127.0.0.1', port: 0 });
|
||||
let timer: ReturnType<typeof setInterval> | undefined;
|
||||
let watchdog: ReturnType<typeof setTimeout> | undefined;
|
||||
try {
|
||||
const port = (server.server.address() as AddressInfo).port;
|
||||
const result = await new Promise<{
|
||||
status?: number;
|
||||
body: string;
|
||||
id?: string;
|
||||
}>((resolve, reject) => {
|
||||
const client = request(
|
||||
{
|
||||
host: '127.0.0.1',
|
||||
port,
|
||||
method: 'POST',
|
||||
path: '/api/v1/chats/test/attachments',
|
||||
headers: { 'content-type': 'image/png', 'content-length': '10000' },
|
||||
},
|
||||
(response) => {
|
||||
clearInterval(timer);
|
||||
clearTimeout(watchdog);
|
||||
let body = '';
|
||||
response.on('data', (part: Buffer) => {
|
||||
body += part.toString();
|
||||
});
|
||||
response.on('end', () => {
|
||||
client.destroy();
|
||||
resolve({
|
||||
status: response.statusCode,
|
||||
body,
|
||||
id: response.headers['x-request-id'] as string,
|
||||
});
|
||||
});
|
||||
response.on('error', reject);
|
||||
},
|
||||
);
|
||||
client.on('error', reject);
|
||||
watchdog = setTimeout(
|
||||
() => client.destroy(new Error('deadline did not fire')),
|
||||
7000,
|
||||
);
|
||||
client.write('partial');
|
||||
timer = setInterval(() => client.write('x'), 100);
|
||||
});
|
||||
expect(result.status).toBe(408);
|
||||
expect(result.id).toBeTruthy();
|
||||
expect(JSON.parse(result.body)).toMatchObject({
|
||||
error: { code: 'invalid_request', request_id: result.id },
|
||||
});
|
||||
expect(calls).toBe(0);
|
||||
} finally {
|
||||
clearInterval(timer);
|
||||
clearTimeout(watchdog);
|
||||
await server.close();
|
||||
}
|
||||
}, 15000);
|
||||
|
||||
it('clears the reception deadline before a completed body is processed', async () => {
|
||||
const adapter = new DrainingFastifyAdapter(0, 1000);
|
||||
const server = adapter.getInstance();
|
||||
server.post('/api/v1/chats/:chat_id/attachments', async () => {
|
||||
await delay(1200);
|
||||
return { stored: true };
|
||||
});
|
||||
try {
|
||||
const response = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/chats/test/attachments',
|
||||
headers: { 'content-type': 'image/png' },
|
||||
payload: Buffer.from('complete'),
|
||||
});
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.json()).toEqual({ stored: true });
|
||||
} finally {
|
||||
await server.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,175 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { MAX_UPLOAD_BYTES } from '../../Attachment/attachment.service';
|
||||
import { profileUpload } from './upload-memory-profile';
|
||||
import { drainRejectedBody } from './drain-rejected-body';
|
||||
import {
|
||||
FastifyAdapter,
|
||||
NestFastifyApplication,
|
||||
} from '@nestjs/platform-fastify';
|
||||
|
||||
const requestIdHeader = 'x-request-id';
|
||||
const instanceIdHeader = 'x-instance-id';
|
||||
const instanceId = process.env.INSTANCE_ID?.trim() || randomUUID();
|
||||
|
||||
// Two parallel readiness rounds: 2 * (1s request timeout + 2s interval),
|
||||
// plus one second for the upstream reload. Keep below the public hook's 10s limit.
|
||||
export const DRAIN_DELAY_MS = 7000;
|
||||
|
||||
export class DrainingFastifyAdapter extends FastifyAdapter {
|
||||
private draining = false;
|
||||
private closing?: Promise<void>;
|
||||
private httpClosing?: Promise<undefined>;
|
||||
private readonly uploadDeadlines = new WeakMap<object, number>();
|
||||
|
||||
constructor(
|
||||
private readonly drainDelayMs: number,
|
||||
uploadBodyTimeoutMs = Number(process.env.UPLOAD_BODY_TIMEOUT_MS ?? 15000),
|
||||
) {
|
||||
super({ requestIdHeader, genReqId: () => randomUUID() });
|
||||
this.getInstance().addHook('onRequest', (request, reply, done) => {
|
||||
const path = request.routeOptions.url;
|
||||
const mode =
|
||||
request.method !== 'POST'
|
||||
? undefined
|
||||
: path === '/api/v1/chats/:chat_id/attachments'
|
||||
? 'proxy'
|
||||
: path ===
|
||||
'/api/v1/chats/:chat_id/attachment-uploads/:upload_id/complete'
|
||||
? 'direct'
|
||||
: path === '/api/v1/chats/:chat_id/attachment-uploads'
|
||||
? 'grant'
|
||||
: undefined;
|
||||
if (mode) profileUpload(mode, request.id, reply.raw, done);
|
||||
else done();
|
||||
});
|
||||
if (
|
||||
!Number.isSafeInteger(uploadBodyTimeoutMs) ||
|
||||
uploadBodyTimeoutMs < 1000 ||
|
||||
uploadBodyTimeoutMs > 120000
|
||||
) {
|
||||
throw new Error('UPLOAD_BODY_TIMEOUT_MS must be between 1000 and 120000');
|
||||
}
|
||||
this.getInstance().addHook('onRequest', (request, reply, done) => {
|
||||
if (
|
||||
request.method !== 'POST' ||
|
||||
request.routeOptions.url !== '/api/v1/chats/:chat_id/attachments'
|
||||
) {
|
||||
done();
|
||||
return;
|
||||
}
|
||||
// A total deadline, independent of whether the client keeps sending tiny chunks.
|
||||
// Clear it when reception finishes; decoding and persistence have separate budgets.
|
||||
const raw = request.raw;
|
||||
this.uploadDeadlines.set(raw, Date.now() + uploadBodyTimeoutMs);
|
||||
const cleanup = () => {
|
||||
this.uploadDeadlines.delete(raw);
|
||||
clearTimeout(timer);
|
||||
raw.removeListener('end', cleanup);
|
||||
raw.removeListener('close', cleanup);
|
||||
reply.raw.removeListener('finish', cleanup);
|
||||
};
|
||||
const timer = setTimeout(() => {
|
||||
cleanup();
|
||||
if (
|
||||
raw.complete ||
|
||||
reply.sent ||
|
||||
raw.destroyed ||
|
||||
reply.statusCode === 413
|
||||
)
|
||||
return;
|
||||
// Let HTTP close after flushing the response. Destroying the request here can
|
||||
// reset the TCP connection before a still-writing client receives the 408.
|
||||
void reply
|
||||
.code(408)
|
||||
.header('connection', 'close')
|
||||
.header(requestIdHeader, request.id)
|
||||
.header(instanceIdHeader, instanceId)
|
||||
.send({
|
||||
error: {
|
||||
code: 'invalid_request',
|
||||
message: 'Истёк срок приёма изображения',
|
||||
request_id: request.id,
|
||||
},
|
||||
});
|
||||
}, uploadBodyTimeoutMs);
|
||||
timer.unref();
|
||||
raw.once('end', cleanup);
|
||||
raw.once('close', cleanup);
|
||||
reply.raw.once('finish', cleanup);
|
||||
done();
|
||||
});
|
||||
this.getInstance().addContentTypeParser(
|
||||
['image/png', 'image/jpeg', 'application/octet-stream'],
|
||||
{ parseAs: 'buffer', bodyLimit: MAX_UPLOAD_BYTES },
|
||||
(_request, body, done) => done(null, body),
|
||||
);
|
||||
this.getInstance().addHook('onSend', (request, reply, payload, done) => {
|
||||
// An already active request may finish after Fastify starts closing.
|
||||
// Finish its response, then close its keep-alive socket as well.
|
||||
if (this.httpClosing) void reply.header('connection', 'close');
|
||||
if (
|
||||
reply.statusCode === 413 &&
|
||||
request.routeOptions.url === '/api/v1/chats/:chat_id/attachments' &&
|
||||
!request.raw.readableEnded &&
|
||||
!request.raw.destroyed
|
||||
) {
|
||||
const deadline =
|
||||
this.uploadDeadlines.get(request.raw) ??
|
||||
Date.now() + uploadBodyTimeoutMs;
|
||||
drainRejectedBody(
|
||||
request.raw,
|
||||
MAX_UPLOAD_BYTES + 1048576,
|
||||
Math.max(1, deadline - Date.now()),
|
||||
(consumed) => {
|
||||
if (!consumed) void reply.header('connection', 'close');
|
||||
done(null, payload);
|
||||
},
|
||||
);
|
||||
return;
|
||||
}
|
||||
done(null, payload);
|
||||
});
|
||||
}
|
||||
|
||||
get isDraining(): boolean {
|
||||
return this.draining;
|
||||
}
|
||||
|
||||
override beforeClose(): Promise<void> {
|
||||
return (this.closing ??= this.drainAndClose());
|
||||
}
|
||||
|
||||
override close(): Promise<undefined> {
|
||||
return (this.httpClosing ??= super.close());
|
||||
}
|
||||
|
||||
private async drainAndClose(): Promise<void> {
|
||||
this.draining = true;
|
||||
if (this.drainDelayMs > 0) {
|
||||
await new Promise<void>((resolve) =>
|
||||
setTimeout(resolve, this.drainDelayMs),
|
||||
);
|
||||
}
|
||||
// Nest awaits beforeClose before destroying Prisma/Redis providers.
|
||||
// Fastify first stops new connections and finishes active HTTP requests.
|
||||
await this.close();
|
||||
}
|
||||
}
|
||||
|
||||
export function createFastifyAdapter(
|
||||
drainDelayMs = 0,
|
||||
uploadBodyTimeoutMs?: number,
|
||||
): DrainingFastifyAdapter {
|
||||
return new DrainingFastifyAdapter(drainDelayMs, uploadBodyTimeoutMs);
|
||||
}
|
||||
|
||||
export function exposeRequestIdHeader(app: NestFastifyApplication): void {
|
||||
app
|
||||
.getHttpAdapter()
|
||||
.getInstance()
|
||||
.addHook('onRequest', (request, reply, done) => {
|
||||
void reply.header(requestIdHeader, request.id);
|
||||
void reply.header(instanceIdHeader, instanceId);
|
||||
done();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
import { AsyncLocalStorage } from 'node:async_hooks';
|
||||
import { hostname } from 'node:os';
|
||||
import { performance } from 'node:perf_hooks';
|
||||
import type { ServerResponse } from 'node:http';
|
||||
import sharp from 'sharp';
|
||||
|
||||
type Memory = ReturnType<typeof process.memoryUsage>;
|
||||
type Stage = {
|
||||
name: string;
|
||||
startedMs: number;
|
||||
durationMs: number;
|
||||
before: Memory;
|
||||
after: Memory;
|
||||
peak: Memory;
|
||||
samples: number;
|
||||
maxSampleGapMs: number;
|
||||
sharpBefore: ReturnType<typeof sharp.cache>;
|
||||
sharpAfter: ReturnType<typeof sharp.cache>;
|
||||
};
|
||||
type Context = {
|
||||
started: number;
|
||||
stages: Stage[];
|
||||
current: Stage;
|
||||
lastSample: number;
|
||||
closed: boolean;
|
||||
};
|
||||
const contexts = new AsyncLocalStorage<Context>();
|
||||
|
||||
function sample(context: Context) {
|
||||
const now = performance.now();
|
||||
const memory = process.memoryUsage();
|
||||
const stage = context.current;
|
||||
for (const key of Object.keys(memory) as (keyof Memory)[])
|
||||
stage.peak[key] = Math.max(stage.peak[key], memory[key]);
|
||||
stage.after = memory;
|
||||
stage.samples++;
|
||||
stage.maxSampleGapMs = Math.max(
|
||||
stage.maxSampleGapMs,
|
||||
now - context.lastSample,
|
||||
);
|
||||
context.lastSample = now;
|
||||
stage.durationMs = now - context.started - stage.startedMs;
|
||||
return memory;
|
||||
}
|
||||
|
||||
function stage(name: string, startedMs: number): Stage {
|
||||
const memory = process.memoryUsage();
|
||||
const cache = sharp.cache();
|
||||
return {
|
||||
name,
|
||||
startedMs,
|
||||
durationMs: 0,
|
||||
before: memory,
|
||||
after: memory,
|
||||
peak: { ...memory },
|
||||
samples: 0,
|
||||
maxSampleGapMs: 0,
|
||||
sharpBefore: cache,
|
||||
sharpAfter: cache,
|
||||
};
|
||||
}
|
||||
|
||||
/** Profiling is opt-in. Contains numeric metrics and a request ID, never URLs or payloads. */
|
||||
export function profileUpload(
|
||||
mode: 'proxy' | 'direct' | 'grant',
|
||||
requestId: string,
|
||||
response: ServerResponse,
|
||||
done: () => void,
|
||||
) {
|
||||
if (process.env.UPLOAD_MEMORY_PROFILE !== '1') return done();
|
||||
const now = performance.now();
|
||||
const context: Context = {
|
||||
started: now,
|
||||
stages: [],
|
||||
current: stage('receive_and_authenticate', 0),
|
||||
lastSample: now,
|
||||
closed: false,
|
||||
};
|
||||
const timer = setInterval(() => sample(context), 20);
|
||||
timer.unref();
|
||||
const finish = () => {
|
||||
if (context.closed) return;
|
||||
context.closed = true;
|
||||
clearInterval(timer);
|
||||
sample(context);
|
||||
context.current.sharpAfter = sharp.cache();
|
||||
context.stages.push(context.current);
|
||||
console.log(
|
||||
'UPLOAD_MEMORY_PROFILE ' +
|
||||
JSON.stringify({
|
||||
mode,
|
||||
requestId,
|
||||
instance: hostname(),
|
||||
pid: process.pid,
|
||||
status: response.statusCode,
|
||||
finished: response.writableFinished,
|
||||
intervalMs: 20,
|
||||
durationMs: performance.now() - context.started,
|
||||
stages: context.stages,
|
||||
}),
|
||||
);
|
||||
};
|
||||
response.once('finish', finish);
|
||||
response.once('close', finish);
|
||||
contexts.run(context, done);
|
||||
}
|
||||
|
||||
export function profileStage(name: string) {
|
||||
const context = contexts.getStore();
|
||||
if (!context || context.closed || context.stages.length >= 64) return;
|
||||
sample(context);
|
||||
context.current.sharpAfter = sharp.cache();
|
||||
context.stages.push(context.current);
|
||||
context.current = stage(name, performance.now() - context.started);
|
||||
context.lastSample = performance.now();
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import { ApiException } from './exceptions/api.exception';
|
||||
import { HttpStatus } from '@nestjs/common';
|
||||
|
||||
export const uuidPattern =
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
||||
|
||||
export function invalidRequest(message: string): never {
|
||||
throw new ApiException(HttpStatus.BAD_REQUEST, 'invalid_request', message);
|
||||
}
|
||||
|
||||
export function objectBody(
|
||||
value: unknown,
|
||||
keys: string[],
|
||||
): Record<string, unknown> {
|
||||
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
||||
return invalidRequest('Ожидается JSON-объект');
|
||||
}
|
||||
const body = value as Record<string, unknown>;
|
||||
if (Object.keys(body).some((key) => !keys.includes(key))) {
|
||||
return invalidRequest('Неизвестное поле запроса');
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
export function requiredText(
|
||||
value: unknown,
|
||||
field: string,
|
||||
max: number,
|
||||
): string {
|
||||
if (typeof value !== 'string' || value.trim().length === 0) {
|
||||
return invalidRequest(`Некорректное поле ${field}`);
|
||||
}
|
||||
let codePoints = 0;
|
||||
for (const _character of value) codePoints++;
|
||||
if (codePoints > max) return invalidRequest(`Некорректное поле ${field}`);
|
||||
return value;
|
||||
}
|
||||
|
||||
export function uuid(value: unknown, field: string): string {
|
||||
if (typeof value !== 'string' || !uuidPattern.test(value)) {
|
||||
return invalidRequest(`Некорректное поле ${field}`);
|
||||
}
|
||||
return value.toLowerCase();
|
||||
}
|
||||
|
||||
export function pageLimit(value: unknown): number {
|
||||
if (value === undefined) return 50;
|
||||
if (typeof value !== 'string' || !/^[1-9]\d*$/.test(value)) {
|
||||
return invalidRequest('Некорректный limit');
|
||||
}
|
||||
const limit = Number(value);
|
||||
if (limit > 100) return invalidRequest('Некорректный limit');
|
||||
return limit;
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { NestFastifyApplication } from '@nestjs/platform-fastify';
|
||||
import {
|
||||
DocumentBuilder,
|
||||
SwaggerModule,
|
||||
type OpenAPIObject,
|
||||
} from '@nestjs/swagger';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { apiReference } from '@scalar/nestjs-api-reference';
|
||||
import { AppModule } from './app.module';
|
||||
import { Logger } from 'nestjs-pino';
|
||||
import { completeApiDocument } from './common/api-document';
|
||||
import {
|
||||
createFastifyAdapter,
|
||||
DRAIN_DELAY_MS,
|
||||
exposeRequestIdHeader,
|
||||
} from './common/http/fastify-adapter';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create<NestFastifyApplication>(
|
||||
AppModule,
|
||||
createFastifyAdapter(DRAIN_DELAY_MS),
|
||||
{
|
||||
bufferLogs: true,
|
||||
},
|
||||
);
|
||||
|
||||
exposeRequestIdHeader(app);
|
||||
app.useLogger(app.get(Logger));
|
||||
app.enableShutdownHooks();
|
||||
|
||||
const swaggerConfig = new DocumentBuilder()
|
||||
.setTitle('Messenger API')
|
||||
.setDescription('Документация API')
|
||||
.setVersion('1.4.0')
|
||||
.addBasicAuth({ type: 'http', scheme: 'basic' }, 'basicAuth')
|
||||
.addBearerAuth(
|
||||
{ type: 'http', scheme: 'bearer', bearerFormat: 'opaque' },
|
||||
'bearerAuth',
|
||||
)
|
||||
.build();
|
||||
|
||||
const contract = JSON.parse(
|
||||
readFileSync(join(__dirname, '../contracts/openapi.json'), 'utf8'),
|
||||
) as OpenAPIObject;
|
||||
const document = completeApiDocument(
|
||||
SwaggerModule.createDocument(app, swaggerConfig),
|
||||
contract,
|
||||
);
|
||||
|
||||
SwaggerModule.setup('swagger', app, document);
|
||||
|
||||
app.use(
|
||||
'/docs',
|
||||
apiReference({
|
||||
content: document,
|
||||
theme: 'laserwave',
|
||||
withFastify: true,
|
||||
}),
|
||||
);
|
||||
|
||||
await app.listen(
|
||||
Number(process.env.APP_PORT ?? process.env.PORT ?? 8080),
|
||||
process.env.APP_HOST ?? '0.0.0.0',
|
||||
);
|
||||
}
|
||||
|
||||
void bootstrap();
|
||||
@@ -0,0 +1,6 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { PrismaService } from './prisma.service';
|
||||
|
||||
@Global()
|
||||
@Module({ providers: [PrismaService], exports: [PrismaService] })
|
||||
export class PrismaModule {}
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'dotenv/config';
|
||||
import {
|
||||
Injectable,
|
||||
type OnModuleDestroy,
|
||||
type OnModuleInit,
|
||||
} from '@nestjs/common';
|
||||
import { PrismaPg } from '@prisma/adapter-pg';
|
||||
import { PrismaClient } from '../generated/prisma/client';
|
||||
|
||||
@Injectable()
|
||||
export class PrismaService
|
||||
extends PrismaClient
|
||||
implements OnModuleInit, OnModuleDestroy
|
||||
{
|
||||
constructor() {
|
||||
const connectionString = process.env.DATABASE_URL;
|
||||
if (!connectionString) throw new Error('DATABASE_URL is required');
|
||||
const poolMax = Number(process.env.DB_POOL_MAX ?? 15);
|
||||
if (!Number.isSafeInteger(poolMax) || poolMax < 1) {
|
||||
throw new Error('DB_POOL_MAX must be a positive integer');
|
||||
}
|
||||
super({
|
||||
adapter: new PrismaPg({
|
||||
connectionString,
|
||||
max: poolMax,
|
||||
connectionTimeoutMillis: 5000,
|
||||
query_timeout: 5000,
|
||||
idleTimeoutMillis: 30000,
|
||||
options: '-c statement_timeout=5000',
|
||||
}),
|
||||
transactionOptions: { maxWait: 5000, timeout: 10000 },
|
||||
});
|
||||
}
|
||||
|
||||
async onModuleInit(): Promise<void> {
|
||||
await this.$connect();
|
||||
}
|
||||
|
||||
async onModuleDestroy(): Promise<void> {
|
||||
await this.$disconnect();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
"""Integration checks against the local Compose stack. --lifecycle restarts its services."""
|
||||
import argparse
|
||||
import base64
|
||||
import json
|
||||
from pathlib import Path
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import uuid
|
||||
from urllib.parse import quote
|
||||
import zlib
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client, Failure, check, run
|
||||
|
||||
|
||||
def compose(*args):
|
||||
result = subprocess.run(['docker', 'compose', *args], cwd=ROOT, capture_output=True, text=True)
|
||||
check(result.returncode == 0, 'Compose action failed: ' + ' '.join(args[:2]))
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def wait_ready(client):
|
||||
deadline = time.monotonic() + 90
|
||||
observed = set()
|
||||
stable_since = None
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
response = client.call('GET', '/health/ready', schema='Health')
|
||||
upstreams = compose('exec', '-T', 'nginx', 'cat', '/etc/nginx/ready-upstreams.conf')
|
||||
# A single 200 can come from an old Nginx worker while a removal
|
||||
# reload is still pending. Wait for both re-added replicas to settle.
|
||||
check(sum(':8080' in line for line in upstreams.splitlines()) == 2,
|
||||
'Both upstreams have not recovered')
|
||||
observed.add(response['headers']['X-Instance-Id'])
|
||||
if stable_since is None:
|
||||
stable_since = time.monotonic()
|
||||
if len(observed) == 2 and time.monotonic() - stable_since >= 4:
|
||||
return
|
||||
except Failure:
|
||||
observed.clear()
|
||||
stable_since = None
|
||||
time.sleep(1)
|
||||
raise Failure('API did not recover')
|
||||
|
||||
|
||||
def direct_call(method, path, expected, actor=None, raw=None, content_type=None):
|
||||
"""Probe the API process when the LB has excluded every unready replica."""
|
||||
headers = {'Authorization': 'Bearer ' + actor['token']} if actor else {}
|
||||
if content_type:
|
||||
headers['Content-Type'] = content_type
|
||||
request = {'method': method, 'path': path, 'headers': headers,
|
||||
'body': base64.b64encode(raw).decode() if raw is not None else None}
|
||||
script = """const q=JSON.parse(require('node:fs').readFileSync(0,'utf8'));
|
||||
fetch('http://127.0.0.1:8080'+q.path,{method:q.method,headers:q.headers,
|
||||
body:q.body===null?undefined:Buffer.from(q.body,'base64')}).then(async r=>{
|
||||
console.log(JSON.stringify({status:r.status,body:await r.json(),
|
||||
requestId:r.headers.get('x-request-id'),instanceId:r.headers.get('x-instance-id')}));
|
||||
}).catch(()=>process.exit(1));"""
|
||||
result = subprocess.run(['docker', 'compose', 'exec', '-T', '--index', '1',
|
||||
'api', 'node', '-e', script], cwd=ROOT,
|
||||
input=json.dumps(request), capture_output=True, text=True)
|
||||
check(result.returncode == 0, 'direct API probe failed')
|
||||
response = json.loads(result.stdout)
|
||||
check(response['status'] == expected, 'direct API returned wrong status')
|
||||
check(response['requestId'] and response['instanceId'], 'direct API lacks response identifiers')
|
||||
if expected >= 400:
|
||||
check(response['body']['error']['request_id'] == response['requestId'], 'error request ID differs')
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--base-url', default='http://localhost:8080')
|
||||
parser.add_argument('--lifecycle', action='store_true')
|
||||
args = parser.parse_args()
|
||||
c = Client(args.base_url)
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
path = '/api/v1/chats/' + chat['id']
|
||||
png = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
||||
|
||||
def upload(data=png, name='фото.png', mime='image/png', expected=201, actor=alice):
|
||||
return c.call('POST', path + '/attachments?filename=' + quote(name, safe=''), expected,
|
||||
actor=actor, raw=data, headers={'Content-Type': mime},
|
||||
schema='Attachment' if expected == 201 else None)
|
||||
|
||||
att = upload()['json']
|
||||
item = '/api/v1/attachments/' + att['id']
|
||||
check(c.call('GET', item, actor=bob, schema='Attachment')['json'] == att, 'metadata differs')
|
||||
check(c.call('GET', item + '/content', actor=bob)['bytes'] == png, 'PNG changed')
|
||||
c.call('GET', item + '/content?variant=thumbnail', 404, actor=bob)
|
||||
c.call('GET', item, 401)
|
||||
c.call('GET', item, 404, actor=eve)
|
||||
c.call('GET', item + '/content', 404, actor=eve)
|
||||
upload(actor=eve, expected=404)
|
||||
upload(name='../photo.png', expected=400)
|
||||
upload(name='photo\\bad.png', expected=400)
|
||||
upload(name='bad\x00.png', expected=400)
|
||||
upload(name=' ', expected=400)
|
||||
upload(data=b'not an image', expected=415)
|
||||
upload(data=png[:50], expected=415)
|
||||
upload(mime='image/jpeg', expected=415)
|
||||
upload(data=b'x' * (10485760 + 1), expected=413)
|
||||
|
||||
def chunk(kind, value):
|
||||
return struct.pack('>I', len(value)) + kind + value + struct.pack('>I', zlib.crc32(kind + value))
|
||||
|
||||
huge = b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', 10000, 5000, 8, 2, 0, 0, 0))
|
||||
huge += chunk(b'IDAT', zlib.compress(b'\x00\x00\x00\x00')) + chunk(b'IEND', b'')
|
||||
upload(data=huge, expected=413)
|
||||
jpeg = subprocess.run(['node', '-e',
|
||||
"require('sharp')('tests/fixtures/sample.png').jpeg().toBuffer().then(b=>process.stdout.write(b))"],
|
||||
cwd=ROOT, capture_output=True, check=True).stdout
|
||||
jpg = upload(data=jpeg, name='photo.jpg', mime='image/jpeg')['json']
|
||||
check(c.call('GET', '/api/v1/attachments/' + jpg['id'] + '/content', actor=bob)['bytes'] == jpeg, 'JPEG changed')
|
||||
key = uuid.uuid4().hex
|
||||
message = c.message(alice, chat, '', key, [att['id']])['json']
|
||||
check(c.message(alice, chat, '', key, [att['id']])['json'] == message, 'replay differs')
|
||||
c.call('POST', path + '/messages', 409, actor=alice, body={'text': '', 'attachment_ids': [jpg['id']]}, headers={'Idempotency-Key': key})
|
||||
for ids, status in (([att['id'], att['id']], 400), ([str(uuid.uuid4())], 404), (None, 400)):
|
||||
c.call('POST', path + '/messages', status, actor=alice, body={'text': '', 'attachment_ids': ids}, headers={'Idempotency-Key': uuid.uuid4().hex})
|
||||
other = c.call('POST', '/api/v1/chats', 201, actor=alice,
|
||||
body={'title': 'Other chat', 'member_ids': [alice['id']]}, schema='Chat')['json']
|
||||
c.call('POST', '/api/v1/chats/' + other['id'] + '/messages', 404, actor=alice,
|
||||
body={'text': '', 'attachment_ids': [att['id']]}, headers={'Idempotency-Key': uuid.uuid4().hex})
|
||||
|
||||
if args.lifecycle:
|
||||
anonymous_url = 'http://s3:9000/messenger-attachments/originals/' + chat['id'] + '/' + att['id']
|
||||
status = compose('exec', '-T', 'api', 'node', '-e',
|
||||
'fetch(process.argv[1]).then(r=>console.log(r.status))', anonymous_url)
|
||||
check(status == '403', 'S3 object is publicly readable')
|
||||
try:
|
||||
compose('stop', 's3')
|
||||
direct_call('GET', '/health/ready', 503)
|
||||
direct_call('GET', item, 200, actor=bob)
|
||||
direct_call('GET', item + '/content', 503, actor=bob)
|
||||
direct_call('POST', path + '/attachments?filename=outage.png', 503,
|
||||
actor=alice, raw=png, content_type='image/png')
|
||||
deadline = time.monotonic() + 15
|
||||
while True:
|
||||
upstreams = compose('exec', '-T', 'nginx', 'cat', '/etc/nginx/ready-upstreams.conf')
|
||||
if ':8080' not in upstreams:
|
||||
break
|
||||
check(time.monotonic() < deadline, 'Nginx still routes to unready replicas')
|
||||
time.sleep(0.5)
|
||||
finally:
|
||||
compose('start', 's3')
|
||||
wait_ready(c)
|
||||
check(c.call('GET', item + '/content', actor=bob)['bytes'] == png, 'original lost after storage recovery')
|
||||
compose('stop')
|
||||
compose('up', '-d', '--wait', '--wait-timeout', '180')
|
||||
wait_ready(c)
|
||||
check(c.call('GET', item + '/content', actor=bob)['bytes'] == png, 'original lost after stack restart')
|
||||
check(c.message(alice, chat, '', key, [att['id']])['json'] == message, 'message lost after restart')
|
||||
print('PASS: attachments, validation, ACL, idempotency' + (', private S3, outage and persistence' if args.lifecycle else ''))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
@@ -0,0 +1,77 @@
|
||||
// Remove only fixtures belonging to this one concurrency run, using restricted API keys.
|
||||
import assert from 'node:assert/strict';
|
||||
import { createRequire } from 'node:module';
|
||||
const require = createRequire(`${process.cwd()}/package.json`);
|
||||
const { PrismaService } = require('./dist/prisma/prisma.service.js');
|
||||
const { StorageService } = require('./dist/Storage/storage.service.js');
|
||||
const {
|
||||
tryAttachmentObjectLock,
|
||||
OBJECT_TRANSACTION,
|
||||
} = require('./dist/Attachment/object-lock.js');
|
||||
let input = '';
|
||||
for await (const part of process.stdin) input += part;
|
||||
const {
|
||||
runId,
|
||||
chatId,
|
||||
chatIds = chatId ? [chatId] : [],
|
||||
userIds,
|
||||
kind = 'concurrency',
|
||||
} = JSON.parse(input);
|
||||
assert.ok(
|
||||
['concurrency', 'upload-interruption', 'direct-upload'].includes(kind),
|
||||
);
|
||||
const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||
assert.match(runId, uuid);
|
||||
assert.ok(Array.isArray(userIds) && userIds.length <= 2);
|
||||
for (const id of userIds) assert.match(id, uuid);
|
||||
if (chatId) assert.match(chatId, uuid);
|
||||
assert.ok(
|
||||
Array.isArray(chatIds) &&
|
||||
chatIds.length <= 3 &&
|
||||
new Set(chatIds).size === chatIds.length,
|
||||
);
|
||||
for (const value of chatIds) assert.match(value, uuid);
|
||||
const db = new PrismaService();
|
||||
const storage = new StorageService();
|
||||
try {
|
||||
await db.onModuleInit();
|
||||
const users = await db.user.findMany({ where: { id: { in: userIds } } });
|
||||
assert.ok(
|
||||
users.every((user) => user.username.startsWith(`u_cu${runId.slice(0, 8)}`)),
|
||||
);
|
||||
for (const chatId of chatIds) {
|
||||
const chat = await db.chat.findUnique({
|
||||
where: { id: chatId },
|
||||
include: { members: true },
|
||||
});
|
||||
assert.equal(chat?.title, `${kind}:${runId}`);
|
||||
assert.ok(chat.members.every((member) => userIds.includes(member.userId)));
|
||||
const directUploads = await db.directUpload.findMany({ where: { chatId } });
|
||||
for (const upload of directUploads) {
|
||||
assert.ok(userIds.includes(upload.ownerId));
|
||||
await storage.remove(`staging/${chatId}/${upload.id}`);
|
||||
}
|
||||
await db.directUpload.deleteMany({ where: { chatId } });
|
||||
const attachments = await db.attachment.findMany({ where: { chatId } });
|
||||
for (const attachment of attachments) {
|
||||
assert.ok(userIds.includes(attachment.ownerId));
|
||||
assert.equal(
|
||||
attachment.objectKey,
|
||||
`originals/${chatId}/${attachment.id}`,
|
||||
);
|
||||
await db.$transaction(async (tx) => {
|
||||
assert.ok(await tryAttachmentObjectLock(tx, attachment.objectKey));
|
||||
await storage.remove(attachment.objectKey);
|
||||
await tx.attachment.delete({ where: { id: attachment.id } });
|
||||
}, OBJECT_TRANSACTION);
|
||||
}
|
||||
await db.chat.delete({ where: { id: chatId } });
|
||||
}
|
||||
await db.user.deleteMany({
|
||||
where: { id: { in: users.map((user) => user.id) } },
|
||||
});
|
||||
console.log('PASS: concurrency fixtures removed');
|
||||
} finally {
|
||||
storage.onModuleDestroy();
|
||||
await db.onModuleDestroy();
|
||||
}
|
||||
@@ -0,0 +1,281 @@
|
||||
"""Concurrent raw PNG uploads through Nginx with per-API RSS measurements. Python 3.9+."""
|
||||
import argparse
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from datetime import datetime, timezone
|
||||
import hashlib
|
||||
import http.client
|
||||
import json
|
||||
from pathlib import Path
|
||||
from queue import Queue, Empty
|
||||
import random
|
||||
import ssl
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from urllib.parse import urlsplit
|
||||
import uuid
|
||||
import zlib
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client, Failure, check, shape
|
||||
|
||||
|
||||
class UploadFailure(Failure):
|
||||
def __init__(self, response):
|
||||
super().__init__(f'Concurrent upload returned {response.status}, expected 201')
|
||||
self.details = {'status': response.status,
|
||||
'instance': response.getheader('X-Instance-Id'),
|
||||
'requestId': response.getheader('X-Request-Id')}
|
||||
|
||||
|
||||
def docker(*args, **kwargs):
|
||||
return subprocess.run(['docker', *args], cwd=ROOT, capture_output=True,
|
||||
text=True, check=True, timeout=90, **kwargs).stdout
|
||||
|
||||
|
||||
class MemoryProbe:
|
||||
def __init__(self, container):
|
||||
self.container = container
|
||||
self.messages = Queue()
|
||||
source = (ROOT / 'test/upload-memory-probe.mjs').read_text(encoding='utf-8')
|
||||
self.process = subprocess.Popen(
|
||||
['docker', 'exec', '-i', container, 'node', '--input-type=module', '-e', source],
|
||||
cwd=ROOT, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL, text=True, bufsize=1,
|
||||
)
|
||||
threading.Thread(target=self.read, daemon=True).start()
|
||||
|
||||
def read(self):
|
||||
try:
|
||||
for line in self.process.stdout:
|
||||
self.messages.put(json.loads(line))
|
||||
except (ValueError, OSError):
|
||||
self.messages.put({'event': 'error'})
|
||||
finally:
|
||||
self.messages.put({'event': 'closed'})
|
||||
|
||||
def receive(self, expected):
|
||||
try:
|
||||
value = self.messages.get(timeout=45)
|
||||
except Empty:
|
||||
raise Failure('API memory probe timed out') from None
|
||||
check(value.get('event') == expected, 'API memory probe failed: ' + value.get('event', 'unknown'))
|
||||
return value
|
||||
|
||||
def command(self, command, response):
|
||||
self.process.stdin.write(command + '\n')
|
||||
self.process.stdin.flush()
|
||||
return self.receive(response)
|
||||
|
||||
def close(self):
|
||||
if self.process.poll() is None:
|
||||
try:
|
||||
self.process.stdin.write('quit\n')
|
||||
self.process.stdin.flush()
|
||||
self.process.stdin.close()
|
||||
self.process.wait(timeout=15)
|
||||
except (OSError, subprocess.TimeoutExpired):
|
||||
self.process.kill()
|
||||
self.process.wait(timeout=10)
|
||||
self.process.stdout.close()
|
||||
|
||||
|
||||
def chunk(kind, data):
|
||||
return struct.pack('>I', len(data)) + kind + data + struct.pack('>I', zlib.crc32(kind + data))
|
||||
|
||||
|
||||
def png_image(side):
|
||||
# Deterministic noise is poorly compressible: the encoded file really occupies RAM.
|
||||
rng = random.Random(side)
|
||||
rows = b''.join(b'\0' + rng.randbytes(side * 3) for _ in range(side))
|
||||
return (b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', side, side, 8, 2, 0, 0, 0))
|
||||
+ chunk(b'IDAT', zlib.compress(rows, level=1)))
|
||||
|
||||
|
||||
def upload(base, actor, chat, body, filename, gate, duration, timeout):
|
||||
target = urlsplit(base + '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + filename)
|
||||
connection_type = http.client.HTTPSConnection if target.scheme == 'https' else http.client.HTTPConnection
|
||||
kwargs = {'context': ssl.create_default_context()} if target.scheme == 'https' else {}
|
||||
connection = connection_type(target.hostname, target.port, timeout=timeout, **kwargs)
|
||||
try:
|
||||
connection.putrequest('POST', target.path + '?' + target.query)
|
||||
connection.putheader('Authorization', 'Bearer ' + actor['token'])
|
||||
connection.putheader('Content-Type', 'image/png')
|
||||
connection.putheader('Content-Length', str(len(body)))
|
||||
connection.endheaders()
|
||||
first = min(65536, len(body) - 1)
|
||||
connection.send(body[:first])
|
||||
# Every connection has sent headers and an incomplete body before any may finish.
|
||||
gate.wait(timeout=timeout)
|
||||
started = time.monotonic()
|
||||
for offset in range(first, len(body), 65536):
|
||||
end = min(offset + 65536, len(body))
|
||||
deadline = started + duration * (end - first) / (len(body) - first)
|
||||
time.sleep(max(0, deadline - time.monotonic()))
|
||||
connection.send(body[offset:end])
|
||||
response = connection.getresponse()
|
||||
data = response.read(1024 * 1024 + 1)
|
||||
if response.status != 201:
|
||||
raise UploadFailure(response)
|
||||
check(len(data) <= 1024 * 1024, 'Upload response unexpectedly large')
|
||||
check(response.getheader('Content-Type', '').startswith('application/json'), 'Upload response is not JSON')
|
||||
check(response.getheader('X-Request-Id'), 'Upload lacks request identifier')
|
||||
instance = response.getheader('X-Instance-Id')
|
||||
check(instance, 'Upload lacks instance identifier')
|
||||
attachment = shape(json.loads(data), 'Attachment')
|
||||
check(attachment['status'] == 'ready', 'Concurrent attachment is not ready')
|
||||
check(attachment['chat_id'] == chat['id'] and attachment['owner_id'] == actor['id'], 'Incorrect attachment ownership')
|
||||
check(attachment['filename'] == filename and attachment['content_type'] == 'image/png', 'Incorrect attachment metadata')
|
||||
digest = hashlib.sha256(body).hexdigest()
|
||||
check(attachment['sha256'] == digest and attachment['size_bytes'] == len(body), 'Upload bytes/metadata mismatch')
|
||||
return {'attachment': attachment, 'instance': instance, 'elapsedSeconds': time.monotonic() - started,
|
||||
'sha256': digest, 'sizeBytes': len(body)}
|
||||
except BaseException:
|
||||
gate.abort()
|
||||
raise
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--base-url', default='http://localhost:8080')
|
||||
parser.add_argument('--concurrency', type=int, default=10)
|
||||
parser.add_argument('--rounds', type=int, default=2)
|
||||
parser.add_argument('--sizes', type=int, nargs='+', default=[512, 1792], metavar='SIDE_PX')
|
||||
parser.add_argument('--upload-seconds', type=float, default=2)
|
||||
parser.add_argument('--timeout', type=float, default=60)
|
||||
parser.add_argument('--max-rss-mib', type=float, help='Optional assertion budget per API process, not a container resource limit')
|
||||
parser.add_argument('--output', type=Path, default=ROOT / 'evidence/concurrent-uploads.json')
|
||||
args = parser.parse_args()
|
||||
check(10 <= args.concurrency <= 64, '--concurrency must be between 10 and 64')
|
||||
check(1 <= args.rounds <= 10, '--rounds must be between 1 and 10')
|
||||
check(all(64 <= side <= 1800 for side in args.sizes), '--sizes must be between 64 and 1800 pixels')
|
||||
check(0.5 <= args.upload_seconds <= 10 and args.timeout > args.upload_seconds + 5, 'Invalid upload timing')
|
||||
check(args.max_rss_mib is None or args.max_rss_mib > 0, 'RSS budget must be positive')
|
||||
client = Client(args.base_url, timeout=args.timeout)
|
||||
client.call('GET', '/health/ready', schema='Health')
|
||||
run_id = str(uuid.uuid4())
|
||||
report = {'status': 'running', 'startedAt': datetime.now(timezone.utc).isoformat(),
|
||||
'concurrency': args.concurrency, 'roundsPerSize': args.rounds,
|
||||
'uploadSeconds': args.upload_seconds, 'maxRssMiB': args.max_rss_mib,
|
||||
'memoryMethod': 'API /proc VmRSS sampled every 50 ms; VmHWM is lifetime, not reset; probe RSS excluded',
|
||||
'waves': []}
|
||||
actors, probes, chat = [], [], None
|
||||
try:
|
||||
containers = docker('compose', 'ps', '-q', 'api').split()
|
||||
check(len(containers) >= 2, 'Expected at least two running API replicas')
|
||||
before = {state['Id']: (state['State']['StartedAt'], state['RestartCount'])
|
||||
for state in json.loads(docker('inspect', *containers))}
|
||||
for container in containers:
|
||||
probe = MemoryProbe(container)
|
||||
probes.append(probe)
|
||||
probe.ready = probe.receive('ready')
|
||||
owner = client.user('cu' + run_id[:8] + 'a')
|
||||
actors.append(owner)
|
||||
reader = client.user('cu' + run_id[:8] + 'b')
|
||||
actors.append(reader)
|
||||
chat = client.call('POST', '/api/v1/chats', 201, actor=owner,
|
||||
body={'title': 'concurrency:' + run_id, 'member_ids': [actor['id'] for actor in actors]},
|
||||
schema='Chat')['json']
|
||||
for side in args.sizes:
|
||||
image = png_image(side)
|
||||
for number in range(1, args.rounds + 1):
|
||||
bodies = [image + chunk(b'tEXt', f'fixture\0{run_id}-{side}-{number}-{i}'.encode()) + chunk(b'IEND', b'')
|
||||
for i in range(args.concurrency)]
|
||||
check(all(len(body) <= 10485760 for body in bodies), 'Generated PNG exceeds 10 MiB')
|
||||
wave = {'sidePixels': side, 'round': number, 'fileBytes': len(bodies[0]),
|
||||
'totalBytes': sum(map(len, bodies)), 'incompleteUploadsAtBarrier': 0,
|
||||
'successfulUploads': 0, 'verifiedDownloads': 0, 'memory': []}
|
||||
report['waves'].append(wave)
|
||||
for probe in probes:
|
||||
probe.command('begin', 'begin')
|
||||
gate = threading.Barrier(args.concurrency + 1)
|
||||
started = time.monotonic()
|
||||
futures = []
|
||||
print(f'Uploading: {args.concurrency} x {len(bodies[0]) / 1048576:.2f} MiB, round {number}', flush=True)
|
||||
try:
|
||||
with ThreadPoolExecutor(max_workers=args.concurrency) as pool:
|
||||
futures = [pool.submit(upload, client.base, owner, chat, body,
|
||||
f'{side}-{number}-{i}.png', gate, args.upload_seconds, args.timeout)
|
||||
for i, body in enumerate(bodies)]
|
||||
gate.wait(timeout=args.timeout)
|
||||
wave['incompleteUploadsAtBarrier'] = args.concurrency
|
||||
results = [future.result() for future in futures]
|
||||
finally:
|
||||
wave['uploadWallSeconds'] = time.monotonic() - started
|
||||
memory = [{'instance': probe.ready['instance'], **probe.command('end', 'summary')} for probe in probes]
|
||||
wave['memory'] = memory
|
||||
wave['successfulUploads'] = sum(future.done() and not future.cancelled() and future.exception() is None for future in futures)
|
||||
wave['failedUploads'] = [
|
||||
{'index': index, 'errorType': type(future.exception()).__name__,
|
||||
**getattr(future.exception(), 'details', {})}
|
||||
for index, future in enumerate(futures)
|
||||
if future.done() and not future.cancelled() and future.exception() is not None
|
||||
]
|
||||
wave['instances'] = {name: sum(r['instance'] == name for r in results) for name in sorted({r['instance'] for r in results})}
|
||||
wave['latencySeconds'] = {'min': min(r['elapsedSeconds'] for r in results), 'max': max(r['elapsedSeconds'] for r in results)}
|
||||
check(len({r['attachment']['id'] for r in results}) == args.concurrency, 'Attachment IDs collide')
|
||||
check(len(wave['instances']) >= 2, 'Concurrent uploads did not reach both replicas')
|
||||
check(set(wave['instances']).issubset({probe.ready['instance'] for probe in probes}), 'Unmeasured API replica served uploads')
|
||||
for sample in memory:
|
||||
check(sample['samples'] >= 2, 'Insufficient memory samples during upload')
|
||||
check(sample['oomDelta'] == 0 and sample['oomKillDelta'] == 0, 'API cgroup reported OOM during upload')
|
||||
if args.max_rss_mib is not None:
|
||||
check(sample['sampledPeakRssBytes'] <= args.max_rss_mib * 1048576, 'API sampled RSS exceeded assertion budget')
|
||||
for result in results:
|
||||
path = '/api/v1/attachments/' + result['attachment']['id']
|
||||
meta = client.call('GET', path, actor=reader, schema='Attachment')['json']
|
||||
check(meta == result['attachment'], 'Concurrent metadata changed')
|
||||
data = client.call('GET', path + '/content', actor=reader)['bytes']
|
||||
check(len(data) == result['sizeBytes'] and hashlib.sha256(data).hexdigest() == result['sha256'], 'Concurrent download checksum mismatch')
|
||||
wave['verifiedDownloads'] += 1
|
||||
peaks = ', '.join(f"{s['instance']}: {s['sampledPeakRssBytes'] / 1048576:.1f} MiB" for s in memory)
|
||||
print('PASS: all uploads/downloads verified; sampled API RSS peaks: ' + peaks, flush=True)
|
||||
after = json.loads(docker('inspect', *containers))
|
||||
check(all(before[state['Id']] == (state['State']['StartedAt'], state['RestartCount'])
|
||||
and state['State']['Running'] and not state['State']['OOMKilled'] for state in after), 'API restarted or was OOM-killed')
|
||||
client.call('GET', '/health/ready', schema='Health')
|
||||
report['status'] = 'passed'
|
||||
except BaseException as error:
|
||||
report['status'] = 'failed'
|
||||
report['errorType'] = type(error).__name__
|
||||
if isinstance(error, Failure):
|
||||
report['failure'] = str(error)
|
||||
raise
|
||||
finally:
|
||||
for probe in probes:
|
||||
if hasattr(probe, 'process'):
|
||||
try:
|
||||
probe.close()
|
||||
except (OSError, subprocess.SubprocessError):
|
||||
report['probeCloseFailed'] = True
|
||||
try:
|
||||
source = (ROOT / 'test/concurrent-cleanup.mjs').read_text(encoding='utf-8')
|
||||
payload = json.dumps({'runId': run_id, 'chatId': chat['id'] if chat else None,
|
||||
'userIds': [actor['id'] for actor in actors]})
|
||||
docker('compose', 'exec', '-T', '--index', '1', 'api', 'node', '--input-type=module', '-e', source, input=payload)
|
||||
report['fixturesRemoved'] = True
|
||||
except BaseException:
|
||||
report['fixturesRemoved'] = False
|
||||
if report['status'] == 'passed':
|
||||
report['status'] = 'failed'
|
||||
raise Failure('Concurrent fixtures cleanup failed') from None
|
||||
finally:
|
||||
report['finishedAt'] = datetime.now(timezone.utc).isoformat()
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
args.output.write_text(json.dumps(report, indent=2) + '\n', encoding='utf-8')
|
||||
print('PASS: concurrent uploads, byte integrity, memory sampling and no API restarts/OOM', flush=True)
|
||||
print('Report: ' + str(args.output))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except (Failure, ValueError, OSError, subprocess.SubprocessError, threading.BrokenBarrierError) as error:
|
||||
# Never include subprocess arguments or request headers: they can contain credentials.
|
||||
print('FAIL: ' + (str(error) if isinstance(error, Failure) else type(error).__name__), file=sys.stderr)
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,167 @@
|
||||
"""Compare the entire proxy/direct upload workflows, including complete validation."""
|
||||
import argparse
|
||||
import importlib.util
|
||||
import datetime
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
from direct_client import ROOT, Client, Failure, check, docker, fixture, grant, put, complete, verify
|
||||
|
||||
spec = importlib.util.spec_from_file_location('upload_metrics', ROOT / 'test/concurrent-uploads.py')
|
||||
metrics = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(metrics)
|
||||
|
||||
|
||||
def collect_profiles(case, since, repeats):
|
||||
records = {}
|
||||
for line in docker('compose', 'logs', '--no-color', '--since', since, 'api').splitlines():
|
||||
if 'UPLOAD_MEMORY_PROFILE ' in line:
|
||||
record = json.loads(line.split('UPLOAD_MEMORY_PROFILE ', 1)[1])
|
||||
records[record['requestId']] = record
|
||||
case['profiles'] = [records[request_id] for request_id in case['requestIds'] if request_id in records]
|
||||
check(len(case['profiles']) == repeats, 'Missing opt-in API stage profiles')
|
||||
for record in case['profiles']:
|
||||
check(record['mode'] == case['mode'] and record['status'] == 201 and record['finished'], 'Invalid stage profile')
|
||||
names = {stage['name'] for stage in record['stages']}
|
||||
check({'sharp_metadata', 'sharp_decode_stats', 'serialize_response'} <= names, 'Async stage context was lost')
|
||||
if case['mode'] == 'direct':
|
||||
check({'s3_stream_sha256_to_file', 'sha256_finalize', 's3_copy'} <= names, 'Missing direct profile stages')
|
||||
|
||||
|
||||
def restart_api(client, containers):
|
||||
def states():
|
||||
return docker('inspect', '--format', '{{.State.Health.Status}} {{.State.StartedAt}}', *containers).splitlines()
|
||||
before = states()
|
||||
docker('compose', 'restart', 'api')
|
||||
deadline = time.monotonic() + 120
|
||||
routed = set()
|
||||
while time.monotonic() < deadline:
|
||||
current = states()
|
||||
if len(current) == 2 and all(line.startswith('healthy ') for line in current):
|
||||
check(all(old != new for old, new in zip(before, current)), 'API did not restart')
|
||||
try:
|
||||
response = client.call('GET', '/health/ready')
|
||||
routed.add(response['headers']['X-Instance-Id'])
|
||||
if len(routed) == 2:
|
||||
return current
|
||||
except Failure:
|
||||
pass
|
||||
time.sleep(1)
|
||||
raise Failure('API did not become ready after restart')
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--sizes', type=int, nargs='+', default=[512, 1792])
|
||||
parser.add_argument('--repeats', type=int, default=3)
|
||||
parser.add_argument('--restart-api', action='store_true', help='Restart both API replicas before each measured case')
|
||||
parser.add_argument('--rounds', type=int, default=1, help='Reverse workflow order on alternate rounds')
|
||||
parser.add_argument('--profile', action='store_true', help='Collect opt-in API stage profiles and RSS after a five-second idle period')
|
||||
args = parser.parse_args()
|
||||
check(len(set(args.sizes)) >= 2 and all(64 <= side <= 1800 for side in args.sizes), 'Choose at least two image sides between 64 and 1800')
|
||||
check(1 <= args.repeats <= 10, 'Repeats must be between 1 and 10')
|
||||
check(1 <= args.rounds <= 5, 'Rounds must be between 1 and 5')
|
||||
warmups = 4 if args.restart_api else 1
|
||||
report = {'status': 'failed', 'repeats': args.repeats, 'rounds': args.rounds,
|
||||
'restartApiBeforeCase': args.restart_api, 'warmupUploadsPerCase': warmups, 'cases': [],
|
||||
'method': 'One upload at a time; measured direct flow includes grant, PUT, and complete with full image validation. Download verification is outside the measurement window. RSS sampling 50 ms; network counters sum non-loopback API interfaces, including DB/S3/background health traffic. Probe RSS excluded.'}
|
||||
probes = []
|
||||
log_start = datetime.datetime.now(datetime.timezone.utc).isoformat()
|
||||
try:
|
||||
readiness = Client('http://127.0.0.1:8080', timeout=5)
|
||||
deadline = time.monotonic() + 90
|
||||
routed = set()
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
routed.add(readiness.call('GET', '/health/ready')['headers']['X-Instance-Id'])
|
||||
if len(routed) == 2: break
|
||||
except Failure:
|
||||
pass
|
||||
time.sleep(1)
|
||||
check(len(routed) == 2, 'Both API replicas must be ready through Nginx')
|
||||
with fixture() as (client, owner, reader, chat, _):
|
||||
containers = docker('compose', 'ps', '-q', 'api').split()
|
||||
check(len(containers) == 2, 'Expected two API replicas')
|
||||
cases = [(round_index + 1, side, mode) for round_index in range(args.rounds)
|
||||
for side in args.sizes
|
||||
for mode in (['proxy', 'direct'] if round_index % 2 == 0 else ['direct', 'proxy'])]
|
||||
for round_number, side, mode in cases:
|
||||
image = metrics.png_image(side) + metrics.chunk(b'IEND', b'')
|
||||
for probe in probes: probe.close()
|
||||
probes.clear()
|
||||
restarted = restart_api(client, containers) if args.restart_api else None
|
||||
for container in containers:
|
||||
probe = metrics.MemoryProbe(container)
|
||||
probes.append(probe)
|
||||
probe.ready = probe.receive('ready')
|
||||
def upload():
|
||||
if mode == 'direct':
|
||||
cap = grant(client, owner, chat, image)
|
||||
put(client, cap, image)
|
||||
response = complete(client, owner, cap)
|
||||
else:
|
||||
response = client.call('POST', f"/api/v1/chats/{chat['id']}/attachments?filename=benchmark.png", 201,
|
||||
actor=owner, raw=image, headers={'Content-Type': 'image/png'}, schema='Attachment')
|
||||
return response['json'], response['headers']['X-Instance-Id'], response['headers']['X-Request-Id']
|
||||
# Warm the selected workflow before recording it. Warm-up failures fail the run.
|
||||
warmed = set()
|
||||
for warmup_index in range(warmups):
|
||||
# Vary request parity so least_conn does not pin every complete
|
||||
# to the same replica in the four-request direct workflow.
|
||||
if args.restart_api and warmup_index % 2:
|
||||
client.call('GET', '/health/live')
|
||||
attachment, instance, _ = upload()
|
||||
warmed.add(instance)
|
||||
verify(client, reader, attachment, image)
|
||||
if args.restart_api:
|
||||
check(warmed == {probe.ready['instance'] for probe in probes}, 'Warm-up did not cover both API replicas')
|
||||
for probe in probes: probe.command('begin', 'begin')
|
||||
started = time.monotonic()
|
||||
attachments = []
|
||||
request_ids = []
|
||||
try:
|
||||
for _ in range(args.repeats):
|
||||
attachment, _, request_id = upload()
|
||||
attachments.append(attachment)
|
||||
request_ids.append(request_id)
|
||||
finally:
|
||||
elapsed = time.monotonic() - started
|
||||
samples = [{'instance': probe.ready['instance'], **probe.command('end', 'summary')} for probe in probes]
|
||||
report['cases'].append({'round': round_number, 'mode': mode, 'sidePixels': side, 'fileBytes': len(image),
|
||||
'apiStatesAfterRestart': restarted, 'warmupInstances': sorted(warmed),
|
||||
'requestIds': request_ids,
|
||||
'successfulUploads': len(attachments), 'elapsedSeconds': elapsed,
|
||||
'memory': samples, 'apiRxBytes': sum(m['networkRxBytes'] for m in samples),
|
||||
'apiTxBytes': sum(m['networkTxBytes'] for m in samples)})
|
||||
for sample in samples:
|
||||
check(sample['samples'] >= 2 and sample['oomDelta'] == sample['oomKillDelta'] == 0, 'Invalid memory samples or OOM')
|
||||
if args.profile:
|
||||
time.sleep(5)
|
||||
report['cases'][-1]['idleAfterFiveSeconds'] = [
|
||||
{'instance': probe.ready['instance'], **probe.command('snapshot', 'snapshot')} for probe in probes]
|
||||
collect_profiles(report['cases'][-1], log_start, args.repeats)
|
||||
for attachment in attachments: verify(client, reader, attachment, image)
|
||||
print(f"PASS: round {round_number}, {mode}, {side}px, {len(image)} bytes, {len(attachments)} uploads; API RX/TX={sum(m['networkRxBytes'] for m in samples)}/{sum(m['networkTxBytes'] for m in samples)}", flush=True)
|
||||
for probe in probes: probe.close()
|
||||
probes.clear()
|
||||
report['fixturesRemoved'] = True
|
||||
report['status'] = 'passed'
|
||||
except BaseException as error:
|
||||
report['errorType'] = type(error).__name__
|
||||
if isinstance(error, Failure): report['failure'] = str(error)
|
||||
raise
|
||||
finally:
|
||||
for probe in probes: probe.close()
|
||||
filename = 'direct-upload-memory-profile.json' if args.profile else (
|
||||
'direct-upload-benchmark-restarted.json' if args.restart_api else 'direct-upload-benchmark.json')
|
||||
(ROOT / 'evidence' / filename).write_text(json.dumps(report, indent=2) + '\n', encoding='utf-8')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except (Failure, ValueError, OSError, subprocess.SubprocessError) as error:
|
||||
print('FAIL: ' + (str(error) if isinstance(error, Failure) else type(error).__name__), file=sys.stderr)
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,237 @@
|
||||
"""Compare proxy/direct uploads under synchronized load; measure the main API processes."""
|
||||
import argparse
|
||||
import base64
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import hashlib
|
||||
import http.client
|
||||
import importlib.util
|
||||
import json
|
||||
import statistics
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
from direct_client import ROOT, Client, Failure, check, docker, fixture, grant, put, complete, verify
|
||||
|
||||
spec = importlib.util.spec_from_file_location('direct_benchmark', ROOT / 'test/direct-upload-benchmark.py')
|
||||
benchmark = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(benchmark)
|
||||
metrics = benchmark.metrics
|
||||
|
||||
|
||||
class HttpFailure(Failure):
|
||||
def __init__(self, stage, status, headers):
|
||||
super().__init__(f'{stage} returned HTTP {status}')
|
||||
self.details = {'stage': stage, 'status': status,
|
||||
'instance': headers.get('X-Instance-Id'), 'requestId': headers.get('X-Request-Id')}
|
||||
|
||||
|
||||
def paced_request(method, url, headers, image, gate, duration):
|
||||
target = urlsplit(url)
|
||||
check(target.scheme == 'http', 'This benchmark targets the local HTTP stack')
|
||||
connection = http.client.HTTPConnection(target.hostname, target.port, timeout=60)
|
||||
try:
|
||||
connection.putrequest(method, target.path + ('?' + target.query if target.query else ''))
|
||||
for name, value in headers.items(): connection.putheader(name, value)
|
||||
connection.endheaders()
|
||||
first = min(65536, len(image) - 1)
|
||||
connection.send(image[:first])
|
||||
gate.wait(timeout=60)
|
||||
started = time.monotonic()
|
||||
for offset in range(first, len(image), 65536):
|
||||
end = min(offset + 65536, len(image))
|
||||
deadline = started + duration * (end - first) / (len(image) - first)
|
||||
time.sleep(max(0, deadline - time.monotonic()))
|
||||
connection.send(image[offset:end])
|
||||
response = connection.getresponse()
|
||||
body = response.read(1024 * 1024 + 1)
|
||||
check(len(body) <= 1024 * 1024, 'Unexpectedly large response')
|
||||
return response.status, response.headers, body
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
|
||||
def error_result(error, stage):
|
||||
# Never serialize exceptions containing a signed URL or request headers.
|
||||
return {'ok': False, 'stage': stage, 'errorType': type(error).__name__,
|
||||
**getattr(error, 'details', {})}
|
||||
|
||||
|
||||
def upload(client, owner, chat, image, digest, mode, gate, completion_gate, duration):
|
||||
started = time.monotonic()
|
||||
error = None
|
||||
attachment = None
|
||||
instance = None
|
||||
stage = 'grant' if mode == 'direct' else 'proxy'
|
||||
try:
|
||||
if mode == 'direct':
|
||||
capability = grant(client, owner, chat, image)
|
||||
stage = 'put'
|
||||
status, headers, _ = paced_request('PUT', capability['url'], capability['headers'], image, gate, duration)
|
||||
if status != 200: raise HttpFailure(stage, status, headers)
|
||||
else:
|
||||
status, headers, body = paced_request(
|
||||
'POST', client.base + f"/api/v1/chats/{chat['id']}/attachments?filename=load.png",
|
||||
{'Authorization': 'Bearer ' + owner['token'], 'Content-Type': 'image/png',
|
||||
'Content-Length': str(len(image))}, image, gate, duration)
|
||||
if status != 201: raise HttpFailure(stage, status, headers)
|
||||
attachment = metrics.shape(json.loads(body), 'Attachment')
|
||||
instance = headers.get('X-Instance-Id')
|
||||
except Exception as caught:
|
||||
error = error_result(caught, stage)
|
||||
gate.abort()
|
||||
if mode == 'direct':
|
||||
# All workers arrive even if their PUT failed; successful PUTs can still complete.
|
||||
try:
|
||||
completion_gate.wait(timeout=65)
|
||||
if error is None:
|
||||
stage = 'complete'
|
||||
response = complete(client, owner, capability, [201, 400, 401, 404, 408, 409, 413, 415, 429, 500, 503])
|
||||
if response['status'] != 201: raise HttpFailure(stage, response['status'], response['headers'])
|
||||
attachment = metrics.shape(json.loads(response['bytes']), 'Attachment')
|
||||
instance = response['headers'].get('X-Instance-Id')
|
||||
except Exception as caught:
|
||||
if error is None: error = error_result(caught, stage)
|
||||
if error is None:
|
||||
try:
|
||||
check(attachment['sha256'] == digest and attachment['size_bytes'] == len(image), 'Incorrect checksum/size')
|
||||
check(attachment['chat_id'] == chat['id'] and attachment['owner_id'] == owner['id'], 'Incorrect ownership')
|
||||
check(bool(instance), 'Missing instance ID')
|
||||
except Exception as caught:
|
||||
error = error_result(caught, 'validate_response')
|
||||
return {**(error or {'ok': True, 'attachment': attachment, 'instance': instance}),
|
||||
'elapsedSeconds': time.monotonic() - started}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--sizes', type=int, nargs='+', default=[1792])
|
||||
parser.add_argument('--concurrency', type=int, nargs='+', default=[1, 5, 10])
|
||||
parser.add_argument('--rounds', type=int, default=2)
|
||||
parser.add_argument('--waves', type=int, default=2)
|
||||
parser.add_argument('--upload-seconds', type=float, default=2)
|
||||
args = parser.parse_args()
|
||||
check(all(64 <= n <= 1800 for n in args.sizes), 'Invalid image size')
|
||||
check(all(1 <= n <= 20 for n in args.concurrency), 'Concurrency must be 1..20')
|
||||
check(1 <= args.rounds <= 3 and 1 <= args.waves <= 5, 'Invalid rounds/waves')
|
||||
check(0.1 <= args.upload_seconds <= 5, 'Pacing must be 0.1..5 seconds')
|
||||
report = {'status': 'running', 'cases': [], 'settings': vars(args),
|
||||
'method': 'Fresh API restart per case, exactly 2 verified warm-up uploads per replica via internal API addresses; temporary docker-exec client exits before measurement. Measured uploads use public Nginx. Two paced waves by default; all bodies are incomplete at the upload barrier, direct completes start at a second barrier. Full workflow latency includes grant/PUT/complete. External per-API-process RSS sampling at 50 ms; cgroup memory recorded separately. Independent replica peaks are not summed. No retries. Download verification is outside the measurement window.'}
|
||||
output = ROOT / 'evidence/direct-upload-load.json'
|
||||
def save(): output.write_text(json.dumps(report, indent=2) + '\n', encoding='utf-8')
|
||||
probes = []
|
||||
try:
|
||||
readiness = Client('http://127.0.0.1:8080', timeout=5)
|
||||
deadline = time.monotonic() + 90
|
||||
routed = set()
|
||||
while time.monotonic() < deadline:
|
||||
try: routed.add(readiness.call('GET', '/health/ready')['headers']['X-Instance-Id'])
|
||||
except Failure: pass
|
||||
if len(routed) == 2: break
|
||||
time.sleep(1)
|
||||
check(len(routed) == 2, 'Both API replicas must be ready')
|
||||
with fixture() as (client, owner, reader, chat, _):
|
||||
containers = docker('compose', 'ps', '-q', 'api').split()
|
||||
check(len(containers) == 2, 'Expected two replicas')
|
||||
for container in containers:
|
||||
flag = docker('exec', container, 'node', '-p', "process.env.UPLOAD_MEMORY_PROFILE || '0'").strip()
|
||||
check(flag == '0', 'Disable detailed profiling for the load comparison')
|
||||
cases = [(r + 1, side, concurrency, mode) for r in range(args.rounds)
|
||||
for side in args.sizes for concurrency in args.concurrency
|
||||
for mode in (['proxy', 'direct'] if r % 2 == 0 else ['direct', 'proxy'])]
|
||||
for round_number, side, concurrency, mode in cases:
|
||||
image = metrics.png_image(side) + metrics.chunk(b'IEND', b'')
|
||||
digest = hashlib.sha256(image).hexdigest()
|
||||
for probe in probes: probe.close()
|
||||
probes.clear()
|
||||
restarted = benchmark.restart_api(client, containers)
|
||||
for container in containers:
|
||||
probe = metrics.MemoryProbe(container)
|
||||
probes.append(probe)
|
||||
probe.ready = probe.receive('ready')
|
||||
target_rows = docker('inspect', '--format', '{{.Config.Hostname}} {{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}', *containers).splitlines()
|
||||
targets = [{'instance': row.split()[0], 'address': row.split()[1]} for row in target_rows]
|
||||
source = (ROOT / 'test/warm-upload-replicas.mjs').read_text(encoding='utf-8')
|
||||
warmup = json.loads(docker('exec', '-i', containers[0], 'node', '--input-type=module', '-e', source,
|
||||
input=json.dumps({'targets': targets, 'owner': owner, 'reader': reader,
|
||||
'chat': chat, 'imageBase64': base64.b64encode(image).decode(), 'mode': mode})))
|
||||
check(warmup['counts'] == {p.ready['instance']: 2 for p in probes}, 'Unequal per-replica warm-up')
|
||||
identities = docker('inspect', '--format', '{{.State.StartedAt}} {{.RestartCount}} {{.State.OOMKilled}} {{.State.Running}}', *containers)
|
||||
workers = [Client(client.base, timeout=60) for _ in range(concurrency)]
|
||||
case = {'round': round_number, 'sidePixels': side, 'fileBytes': len(image), 'mode': mode,
|
||||
'concurrency': concurrency, 'apiStatesAfterRestart': restarted,
|
||||
'warmupCounts': warmup['counts'], 'waves': [], 'verifiedDownloads': 0}
|
||||
report['cases'].append(case)
|
||||
results = []
|
||||
for probe in probes: probe.command('begin', 'begin')
|
||||
started = time.monotonic()
|
||||
try:
|
||||
with ThreadPoolExecutor(max_workers=concurrency) as pool:
|
||||
for wave in range(args.waves):
|
||||
gate = threading.Barrier(concurrency + 1)
|
||||
complete_gate = threading.Barrier(concurrency)
|
||||
futures = [pool.submit(upload, worker, owner, chat, image, digest, mode, gate, complete_gate, args.upload_seconds)
|
||||
for worker in workers]
|
||||
overlapped = False
|
||||
try:
|
||||
gate.wait(timeout=60)
|
||||
overlapped = True
|
||||
except threading.BrokenBarrierError: pass
|
||||
wave_results = [future.result() for future in futures]
|
||||
results.extend(wave_results)
|
||||
case['waves'].append({'wave': wave + 1,
|
||||
'incompleteBodiesAtBarrier': concurrency if overlapped else 0,
|
||||
'attempts': [{k: v for k, v in result.items() if k != 'attachment'} for result in wave_results]})
|
||||
finally:
|
||||
case['elapsedSeconds'] = time.monotonic() - started
|
||||
case['memory'] = [{'instance': p.ready['instance'], **p.command('end', 'summary')} for p in probes]
|
||||
case['successfulUploads'] = sum(result['ok'] for result in results)
|
||||
case['failedUploads'] = len(results) - case['successfulUploads']
|
||||
case['successfulUploadsPerSecond'] = case['successfulUploads'] / case['elapsedSeconds']
|
||||
case['instances'] = {name: sum(r['ok'] and r.get('instance') == name for r in results)
|
||||
for name in sorted({r['instance'] for r in results if r['ok']})}
|
||||
latencies = sorted(result['elapsedSeconds'] for result in results)
|
||||
case['latencySeconds'] = {'median': statistics.median(latencies), 'max': max(latencies)}
|
||||
case['apiIdentityUnchanged'] = identities == docker('inspect', '--format', '{{.State.StartedAt}} {{.RestartCount}} {{.State.OOMKilled}} {{.State.Running}}', *containers)
|
||||
save()
|
||||
check(case['apiIdentityUnchanged'], 'API restarted during load')
|
||||
for sample in case['memory']:
|
||||
check(sample['samples'] >= 2, 'Insufficient RSS samples')
|
||||
check(sample['oomDelta'] == sample['oomKillDelta'] == 0, 'API OOM; stopping further load')
|
||||
for result in results:
|
||||
if result['ok']:
|
||||
verify(client, reader, result['attachment'], image)
|
||||
case['verifiedDownloads'] += 1
|
||||
check(len({r['attachment']['id'] for r in results if r['ok']}) == case['successfulUploads'], 'Duplicate attachment IDs')
|
||||
if concurrency >= 5 and case['failedUploads'] == 0:
|
||||
check(len(case['instances']) == 2, 'Concurrent work did not reach both replicas')
|
||||
try:
|
||||
case['apiReadyAfterCase'] = client.call('GET', '/health/ready')['status'] == 200
|
||||
except Failure:
|
||||
case['apiReadyAfterCase'] = False
|
||||
save()
|
||||
peaks = ', '.join(f"{m['sampledPeakRssBytes'] / 2**20:.1f}" for m in case['memory'])
|
||||
print(f"DONE: round {round_number}, {mode}, {side}px, N={concurrency}, {case['successfulUploads']}/{len(results)}, RSS MiB=[{peaks}]", flush=True)
|
||||
for probe in probes: probe.close()
|
||||
probes.clear()
|
||||
report['fixturesRemoved'] = True
|
||||
report['status'] = 'completed_with_errors' if any(c['failedUploads'] or not c['apiReadyAfterCase'] for c in report['cases']) else 'passed'
|
||||
return report['status'] == 'passed'
|
||||
except BaseException as error:
|
||||
report['status'] = 'failed'
|
||||
report['errorType'] = type(error).__name__
|
||||
if isinstance(error, Failure): report['failure'] = str(error)
|
||||
raise
|
||||
finally:
|
||||
for probe in probes: probe.close()
|
||||
save()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
sys.exit(0 if main() else 1)
|
||||
except (Failure, ValueError, OSError, subprocess.SubprocessError) as error:
|
||||
print('FAIL: ' + (str(error) if isinstance(error, Failure) else type(error).__name__), file=sys.stderr)
|
||||
sys.exit(1)
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user