diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..dfdb8b77 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.sh text eol=lf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..4f463b95 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,38 @@ +FROM golang:1.26.3 AS build + +WORKDIR /src + +ENV GOTOOLCHAIN=local +ENV CGO_ENABLED=0 + +COPY go.mod go.sum ./ +RUN go mod download +COPY cmd ./cmd +COPY internal ./internal + +RUN go vet ./... \ + && go test ./... \ + && go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server \ + && go build -trimpath -ldflags="-s -w" -o /out/resourceprobe ./cmd/resourceprobe + +FROM build AS s3-client-test +ENTRYPOINT ["go", "test", "-count=1", "-v", "./internal/objectstore"] + +FROM scratch + +COPY --from=build /out/server /server +COPY --from=build /out/resourceprobe /resourceprobe + +USER 65532:65532 + +ENV APP_HOST=0.0.0.0 +ENV APP_PORT=8080 + +EXPOSE 8080 + +STOPSIGNAL SIGTERM + +HEALTHCHECK --interval=5s --timeout=3s --start-period=5s --retries=3 \ + CMD ["/server", "healthcheck"] + +ENTRYPOINT ["/server"] diff --git a/REPORT.md b/REPORT.md index 9ad2b84f..46cccf70 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,274 @@ # Отчёт — лабораторная 4 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +| Поле | Значение | +| --- | --- | +| Автор | Ситникова Дарья Евгеньевна | +| Группа | БВТ2303 | +| Целевая оценка | 4; трек на 5 не заявлен | +| Commit / PR | Ветка `solution/bvt2303-sitnikova | +| Статус | Критерии на 3 и 4 проверены локально; сборка и функциональные сценарии повторены из чистого клона; нагрузка — 120 загрузок без ошибок, OOM и swap | -## Запуск из чистого clone +## 1. Запуск и проверки -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +**Требования:** Docker Desktop с Linux-контейнерами, Compose V2, Python 3.9+, PowerShell. Для `make` нужен GNU Make; Go на хосте — только для генерации JPEG вне Docker. -## Архитектура и решения +Все команды выполняются из корня репозитория. -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +### Подготовка и запуск -## Доказательства критериев +```powershell +./scripts/init-local.ps1 +docker compose --env-file .env config --quiet +docker compose --env-file .env up -d --build --wait --wait-timeout 180 +``` -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +После запуска API доступен через балансировщик. Настройки и данные хранятся следующим образом: -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Настройка | Значение | +| --- | --- | +| Единая точка входа | http://127.0.0.1:8084 | +| Bootstrap | Создаёт отсутствующие `.env` и файлы в `secrets/`; существующие сохраняет | +| Секреты | Пароль PostgreSQL, отдельные административные и прикладные S3 credentials; значения не печатаются | +| Файлы вне Git | `.env`, `secrets/`, данные и сгенерированные изображения | +| PostgreSQL | Volume `postgres_lab4_data_v1` в проекте Compose | +| MinIO | Volume `s3_lab4_data_v1` в проекте Compose | +| Существующий volume БД | Требует исходного пароля: замена файла secret не меняет пароль роли | +| Миграции и bucket | Сервисы migrate и s3-bootstrap завершаются до запуска API; повторное применение сохраняет данные | -## Эксперименты и отказы +При занятом порте задаётся `LB_PORT` в локальном `.env`; для тестов меняется `--base-url`. Dockerfile выполняет `go vet ./...`, `go test ./...` и сборку приложения. -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +### Функциональные проверки -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +```powershell +python -X utf8 scripts/check_contract.py +python -X utf8 tests/smoke.py --base-url http://127.0.0.1:8084 +python -X utf8 student-tests/attachments_api.py --base-url http://127.0.0.1:8084 +python -X utf8 student-tests/attachment_messages.py --base-url http://127.0.0.1:8084 +python -X utf8 student-tests/upload_boundaries.py --base-url http://127.0.0.1:8084 +./scripts/check-s3.ps1 +``` -## Ограничения и следующие шаги +- Эквивалент проверки контракта и smoke через Make: `make check test PYTHON=python BASE_URL=http://127.0.0.1:8084`. +- Если Python отсутствует в PATH, используется полный путь к установленному `python.exe`. Локальные проверки выполнены Python 3.12.14 из runtime Codex. +- Полная OpenAPI/JSON Schema-валидация: установить `requirements-dev.txt` в отдельное venv и запустить `scripts/validate_openapi.py`. Результат не заменяет проверку приложения. -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +### Отказы и сохранность + +```powershell +python -X utf8 student-tests/upload_cleanup.py --base-url http://127.0.0.1:8084 +python -X utf8 student-tests/attachment_resilience.py --base-url http://127.0.0.1:8084 +``` + +- Сценарии создают синтетические данные. В API нет удаления пользователей и чатов. +- `upload_cleanup.py` отклоняет INSERT только для уникального тестового имени файла и удаляет свой trigger в finally. Grace проверяется явным состариванием собственных записей журнала. +- `attachment_resilience.py` запускает публичные persistence/failover через собственные hooks, останавливает точного uploader и S3, восстанавливает API/S3 в finally. +- `restart.cmd` перезапускает LB, API, PostgreSQL, Redis и S3 с сохранением volumes. `stop-one.cmd` оставляет выбранную API остановленной. +- Windows-hooks используют `LAB_PYTHON`; собственный сценарий задаёт его автоматически. Linux-hooks требуют Python 3, Docker CLI и `chmod +x`; отдельно на Linux не проверялись. + +### Нагрузка + +```powershell +go run ./student-tests/fixturegen +python -X utf8 -u student-tests/upload_memory.py --output evidence/upload-memory.json +``` + +Если Go на хосте не установлен, JPEG генерируется в контейнере: + +```powershell +docker run --rm -v "${PWD}:/src" -w /src golang:1.26.3 go run ./student-tests/fixturegen +``` + +Генератор PNG использует стандартную библиотеку Python. Перед каждым профилем нагрузки пересоздаются только API; volumes сохраняются. Методика и результаты приведены в разделе 4. + +### Остановка + +| Действие | Команда | +| --- | --- | +| Остановка с сохранением данных | `docker compose down` | +| Намеренное удаление данных текущего стенда | `docker compose down --volumes` | + +**При проверках сохранности `--volumes` не используется.** + +### Проверка чистого клона + +Ветка получена командой `git clone` с Gitea в отдельный каталог `mtusi2026lab4-clean-check`. Удалённая ветка и HEAD клона соответствуют `879463c947ae3480a82a7dac1873ae7782b0e277`. Отслеживаемые файлы клона не редактировались; итоговый `git status --porcelain` пустой. + +```powershell +git clone --branch solution/bvt2303-sitnikova --single-branch https://gitea.privcloud.tech/Daria_Sitnikova/mtusi2026lab4.git mtusi2026lab4-clean-check +cd mtusi2026lab4-clean-check +./scripts/init-local.ps1 +Add-Content .env "`nCOMPOSE_PROJECT_NAME=messenger-lab4-clean-check`nLB_PORT=8094" +docker compose --env-file .env config --quiet +docker compose --env-file .env up -d --build --wait --wait-timeout 180 +python -X utf8 tests/smoke.py --base-url http://127.0.0.1:8094 +``` + +| Проверка | Результат | Доказательство | +| --- | --- | --- | +| Источник и изоляция | Клон с сервера, новый проект, порт 8094; до bootstrap отсутствовали .env/secrets; все пять credentials отличаются от основного стенда | [Источник](evidence/clean-source.txt), [bootstrap](evidence/clean-init.txt), [изоляция](evidence/clean-isolation.txt) | +| Сборка | Dockerfile выполнил go vet/test/build; обе API готовы | [Сборка](evidence/clean-build.txt), [состояние](evidence/clean-final-stack.txt) | +| Новая БД и S3 | Применены миграции 1–5; до тестов users/messages/attachments=0; bucket и прикладная политика созданы автоматически | [БД](evidence/clean-initial-db.txt), [миграции и bucket](evidence/clean-bootstrap.txt) | +| Контракт и API | Структура, полная OpenAPI/JSON Schema и публичный smoke — PASS; итоговый smoke после отказов — PASS | [Структура](evidence/clean-contract.txt), [схема](evidence/clean-schema.txt), [smoke](evidence/clean-smoke.txt), [итоговый](evidence/clean-final-smoke.txt) | +| Вложения и сообщения | Upload/download/ACL, MIME/filename, чужой чат, идемпотентность через две API — PASS; ровно 10 MiB принято, chunked-превышение отклонено | [Вложения](evidence/clean-attachments-api.txt), [сообщения](evidence/clean-attachment-messages.txt), [лимит](evidence/clean-upload-boundaries.txt) | +| Private bucket и bootstrap | Повторная настройка и restart S3 сохраняют объект; анонимный GET=403; права API ограничены | [S3](evidence/clean-s3.txt) | +| Компенсация | Отказ INSERT после PUT, grace/dry-run, повтор/поздний PUT, защита готового вложения и обрыв HTTP — PASS | [Очистка](evidence/clean-cleanup.txt) | +| Отказ API | Две реплики до отказа; 120 успешных записей, 2 переходные ошибки; восстановление записи за 2,234 с; сессия, сообщения и оригинал сохранены | [Failover](evidence/clean-failover.txt) | +| Перезапуск всего стенда | Прежние токены и данные сохранены; StartedAt изменился у API/LB/PostgreSQL/Redis/S3, volumes прежние | [Persistence](evidence/clean-persistence.txt), [процессы и volumes](evidence/clean-containers.jsonl), [сводка](evidence/clean-resilience-summary.txt) | +| Завершение проверки | Исходники клона не изменены; тестовый стенд остановлен без удаления volumes | [Git](evidence/clean-final-source.txt), [остановка](evidence/clean-shutdown.txt), [сохранённые volumes](evidence/clean-volumes-retained.txt) | + +Изоляция задана только в локальном `.env`; копирования secrets и данных основного стенда не было. Общий кэш Docker сохранялся. Проверки выполнены Python 3.12.14 из установленного runtime, полная валидация схемы — из существующего venv с библиотеками проверки; код приложения из соседних каталогов не использовался. Результаты сохранены в основном репозитории. Нагрузочный эксперимент повторно в клоне не выполнялся: раздел 4 относится к исходному стенду с той же реализацией `879463c`. Основной стенд на порту 8084 продолжил работу. + +## 2. Архитектура + +```text +Клиент → HAProxy → API ×2 → PostgreSQL (данные и метаданные) + ├→ MinIO (оригиналы файлов) + └→ Redis (кэш профилей) +``` + +Состав стенда и лимиты ресурсов заданы в `compose.yaml`. Лимит API указан для каждой реплики отдельно. PostgreSQL — источник истины; оригиналы файлов хранятся в приватном S3 bucket. + +| Роль / service | Версия | Данные / доступ / готовность | +| --- | --- | --- | +| api × 2 | Go 1.26.3, сборка из исходников, scratch runtime, UID 65532 | 1 CPU, 1536 MiB, swap отключён, GOMEMLIMIT=1024MiB; readiness PostgreSQL+private bucket, liveness только процесс | +| lb | haproxy:3.2.4-alpine | Только `127.0.0.1:8084` опубликован; round-robin, HTTP readiness | +| postgres / migrate | postgres:17.9-bookworm | `postgres_lab4_data_v1`; pg_isready; миграции под общей advisory-блокировкой в транзакции | +| redis | redis:7.4.11-alpine | Кэш без persistence; потеря кэша не удаляет данные; Redis не источник сессий | +| s3 | MinIO RELEASE.2025-04-22T22-12-26Z | `s3_lab4_data_v1`, private bucket, `http://s3:9000` только во внутренней сети; консоль выключена, host-порты не опубликованы | +| s3-bootstrap | mc RELEASE.2025-04-16T18-13-26Z | Создание bucket с --ignore-existing, anonymous policy none, отдельный пользователь API и ограниченная политика | + +MinIO/mc собираются по `infra/s3/Dockerfile` из официальных Git-тегов с Go 1.26.3; runtime Alpine 3.22.2. Полученные commits: сервер `0d7408fc9969caf07de6a8c3a84f9fbb10a6739e`, mc `b00526b153a31b36767991a4f5ce2cced435ee8e`. Готовые образы Quay/Docker Hub оказались недоступны без авторизации; стороннее зеркало не используется. Теги базовых образов закреплены. Путь Go-модуля сохранён из предыдущей реализации; зависимости закреплены в go.mod/go.sum: pgx/v5 5.7.6, go-redis/v9 9.7.3, minio-go/v7 7.0.90, x/crypto 0.37.0. + +API получает только PostgreSQL secret и собственные S3 credentials. S3 root credentials доступны серверу S3 и bootstrap. Политика API разрешает GetBucketLocation/ListBucket только выбранного bucket и Put/Get/DeleteObject только `attachments/*`; административных прав нет. + +Attachment содержит chat/owner, filename, MIME, размер, SHA-256, статус и timestamps. S3 key: `attachments/`; filename — только отображаемое имя. Ключи bucket и внутренние адреса не входят в публичный JSON. Связи message_attachments сохраняют порядок 0..9; составные FK запрещают разные чаты сообщения/вложения. + +Upload: Bearer → членство в чате → filename/MIME → ограниченное чтение → DecodeConfig и предел 40 млн пикселей → полное декодирование PNG/JPEG → durable intent → S3 PUT → транзакция публикации metadata/удаления intent → 201 ready. Оригинал не перекодируется. Размер чтения ограничен 10 MiB+1 независимо от Content-Length; SHA-256 считается при чтении. Staging — tmpfs 16 MiB на API, удаляется после запроса. Один слот на реплику удерживается до окончания S3/БД и удаления staging. + +Download проверяет членство до S3 и передаёт bytes через API без redirect: истинный Content-Type, Content-Length, Content-Disposition, nosniff, private/no-store. Сбой уже начатого потока обрывает соединение без добавления JSON в файл. Неавторизованный пользователь получает 401, посторонний — 404. + +Сообщения поддерживают 0–10 уникальных attachment_ids, по умолчанию []. Пустой текст допустим только с вложением. Членство, вложения своего чата, сообщение, связи и ключ идемпотентности проверяются/сохраняются в одной PostgreSQL-транзакции. Повтор сравнивает текст и упорядоченный список IDs; отсутствие поля и [] эквивалентны; другой body с тем же ключом — 409. + +### Готовность и таймауты + +Балансировщик направляет запросы только готовым репликам. Приложение ограничивает время чтения, обработки и обращения к S3. + +| Параметр | Настройка | +| --- | --- | +| `/health/live` | Проверка жизни процесса | +| `/health/ready` | PostgreSQL и приватный S3 bucket; отказ Redis допускает работу без кэша | +| HTTP API | Заголовки — 5 с, чтение — 30 с, запись — 60 с | +| Обработка upload | Контекст — 45 с | +| S3 | Операция — 30 с; connect, TLS и ожидание заголовков — по 5 с | +| Проверка S3 readiness | 1,5 с | +| HAProxy | client/server — 65 с | +| Повторы PUT | Одна попытка; multipart не используется | +| Ошибки | Пользовательские данные — 400/413/415; зависимости — 503 без внутренних деталей | + +### Конфигурация окружения + +| Поле примера | Фактическое использование | +| --- | --- | +| APP_HOST / APP_PORT | Приложение читает env; в Compose используются Dockerfile defaults 0.0.0.0:8080, host-порт задаёт LB_PORT | +| SESSION_TTL_SECONDS | Передаётся API, default 3600 s | +| DATABASE_URL_FILE | Логическая подсказка skeleton; реально DATABASE_PASSWORD_FILE, PG host/db/user фиксированы в коде | +| SESSION_STORE / CACHE_URL_FILE | Сессии PostgreSQL; Redis-кэш по REDIS_ADDR=redis:6379; CACHE_URL_FILE не читается | +| API_REPLICAS / IDEMPOTENCY_TTL_SECONDS | Фактически scale=2 и SQL TTL=24 h; эти поля примера не изменяют их | +| S3_ENDPOINT / S3_REGION / S3_BUCKET | Endpoint фиксирован Compose как http://s3:9000; region/bucket подставляются из .env | +| S3_ACCESS_KEY_ID_FILE / S3_SECRET_ACCESS_KEY_FILE | Реальные пути /run/secrets/s3_access_key_id и /run/secrets/s3_secret_access_key задаёт Compose | +| MAX_UPLOAD_BYTES / MAX_IMAGE_PIXELS | Фиксированные константы 10485760 и 40000000; .env не меняет обязательные пределы | +| APP_ENV | Не используется | + +### Согласованность S3/БД + +До PUT фиксируется upload_intent. Публикация ready-метаданных и удаление intent атомарны. При неизвестном исходе COMMIT немедленный DELETE не выполняется. Очистка раз в минуту выбирает до 100 записей после grace 10 минут по времени PostgreSQL, повторно проверяет их под FOR UPDATE SKIP LOCKED. Ready-объект сохраняется. Для отсутствующих metadata выполняется DELETE, оставляется abandoned-отметка с повтором через 10 минут; она учитывает поздний PUT. При недоступной БД/S3 запись остаётся для повтора. + +```powershell +docker compose exec -T api /server cleanup --dry-run +docker compose exec -T api /server cleanup +``` + +Подробности и границы: [attachment-consistency.md](docs/attachment-consistency.md). Tombstone хранится бессрочно; полный bucket reconciler из трека на 5 не заявлен. + +## 3. Критерии на 3 и 4 + +Все пункты проверены в локальном стенде. Нумерация соответствует порядку критериев README. Контракт и публичные тесты сохранены. + +| Пункт | Проверка и результат | Доказательства | +| --- | --- | --- | +| 3.1. Основа lab3 и общие хранилища | Две API за LB; метаданные в PostgreSQL, оригиналы в приватном MinIO; оба хранилища используют постоянные volumes | [Bucket и политика](evidence/s3-runtime-checks.txt), [процессы и volumes](evidence/attachment-resilience-containers.jsonl) | +| 3.2. Загрузка, оригинал, сообщения и доступ | POST возвращает 201 ready; оригинал и SHA-256 совпадают; участник читает вложение, посторонний получает 404; сообщения сохраняют attachment_ids | [Вложения](evidence/attachments-api-final.txt), [сообщения](evidence/attachment-messages-final.txt) | +| 3.3. Форматы, имена и лимиты | Реальные PNG/JPEG приняты; ровно 10 MiB принято, chunked 10 MiB+1 отклонено; 40 млн пикселей принято, превышение отклонено; опасные имена запрещены | [Валидация](evidence/upload-validation.txt), [граница bytes](evidence/upload-boundaries.txt), [профили изображений](evidence/upload-memory.json) | +| 3.4. Отказ uploader и перезапуск | Остановлена каждая загрузившая API; другая возвращает прежний оригинал. После restart всех компонентов прежние токены, сообщения и файлы сохранены | [Отказы](evidence/attachment-resilience.txt), [persistence](evidence/lab4-persistence.txt), [состояния контейнеров](evidence/attachment-resilience-containers.jsonl) | +| 4.1. Негативные сценарии | Повреждение, подмена MIME, превышение размера, чужой ID и файл другого чата дают 400/404/409/413/415, не 500 | [Файлы](evidence/attachments-api-final.txt), [межчатовая привязка](evidence/attachment-messages-final.txt), [лимит потока](evidence/upload-boundaries.txt) | +| 4.2. Прерывания и согласованность S3/БД | После PUT и отказа INSERT остаётся intent; свежий объект сохраняется, просроченный удаляется. Проверены dry-run, отказ DELETE, поздний PUT, повтор, защита ready и обрыв body | [Компенсация](evidence/upload-cleanup.txt), [описание механизма](docs/attachment-consistency.md) | +| 4.3. Лимиты, таймауты и параллельные загрузки | Четыре профиля, по три серии из 10 запросов; 120 загрузок и скачиваний без ошибок, OOM и swap; максимальный пик одной API 1232,39 MiB при лимите 1536 MiB | [Результаты](evidence/upload-memory.json), [сводка](evidence/upload-memory.txt), [уникальные строки БД](evidence/upload-memory-db-counts.txt) | +| 4.4. Bootstrap, права и приватность bucket | Повторная настройка сохраняет объект; анонимный GET — 403; API не может писать вне prefix и обращаться к admin info; консоль отключена, host-порты S3 отсутствуют | [Инфраструктура](evidence/s3-runtime-checks.txt), [Go-клиент ключами API](evidence/s3-go-client.txt) | + +Дополнительно: публичный smoke — PASS [в итоговом состоянии](evidence/lab4-final-smoke.txt) и [после отказов](evidence/lab4-smoke-after-resilience.txt). `go vet ./...`, `go test ./...` и сборка — PASS: [вывод](evidence/upload-memory-build.txt). Полная OpenAPI/JSON Schema-валидация — [PASS](evidence/lab4-schema-final.txt); она не заменяет проверку приложения. + +## 4. Результаты экспериментов + +### Методика + +Цель: подтвердить работоспособность при 10 одновременных HTTP-upload и измерить конечное потребление памяти при допустимых bytes/pixels, включая 16-битный PNG с экстремальной шириной. + +| Параметр | Значение | +| --- | --- | +| Хост | Ryzen 7 7745HX, 8 ядер / 16 потоков, около 32 GB RAM, Windows | +| Docker | Engine 29.1.2, Compose 2.40.3-desktop.1; 16 vCPU и 16 389 435 392 байт RAM | +| Ресурсы API | По 1 CPU / 1536 MiB; swap отключён; GOMEMLIMIT=1024MiB | +| Генератор | `student-tests/upload_memory.py`; конечные синхронные серии POST вложений | +| Параллелизм | 10 HTTP-запросов; один слот обработки на каждую API | +| Данные | Четыре детерминированных профиля PNG/JPEG; новый чат и три синтетических пользователя на профиль | +| Подготовка | Перед профилем пересоздаются только API, volumes сохраняются; новый cgroup и счётчик peak | +| Прогрев и повторы | Два sample.png; затем три последовательные серии по 10 запросов | +| Интенсивность | Фиксированный arrival rate не задаётся; следующая серия после завершения предыдущей | +| Повторы POST | Отключены; ошибки учитываются отдельно | +| Контроль результата | Каждый оригинал скачан вторым участником; проверены bytes, SHA-256 и MIME | + +[Аппаратная конфигурация](evidence/upload-hardware.json). PNG генерируются стандартной библиотекой Python, JPEG — Go. Изображения в `data/upload-fixtures` исключены из Git и воспроизводятся из исходников. В каждом профиле наблюдались 10 одновременно выполняемых запросов и две обслуживающие API; в БД — по 30 уникальных вложений. + +### Нагрузка + +| Профиль | Размеры / байт | Длительность серий, с / успешных RPS | p50 / p95 / p99, с | Пик RAM API-1 / API-2, MiB | Средний CPU API-1 / API-2, % одного ядра | +| --- | --- | --- | --- | --- | --- | +| RGB8 PNG | 1024×768 / 2362575 | 2,016 / 14,881 | 0,3515 / 0,5016 / 0,5267 | 139,09 / 139,53 | 21,34 / 20,92 | +| JPEG | 1024×768 / 342078 | 1,406 / 21,337 | 0,1720 / 0,2810 / 0,3144 | 138,64 / 136,61 | 26,62 / 26,13 | +| RGBA16 PNG | 8000×5000 / 1716094 | 14,234 / 2,108 | 3,0620 / 5,5607 / 5,9024 | 740,33 / 740,72 | 83,49 / 82,65 | +| RGBA16 wide PNG | 40000000×1 / 1707450 | 21,578 / 1,390 | 4,1795 / 7,5697 / 8,1507 | 1231,86 / 1232,39 | 86,85 / 86,76 | + +В каждой строке 30 успешных запросов, 0 ошибок, OOM counters=0, swap.current/max=0. RPS = 30 / сумма длительностей серий; квантили — успешных HTTP-вызовов. CPU mean считается по cpu.stat за более широкое окно наблюдения, включая замеры; docker stats samples сохранены в raw JSON. Пик берётся из memory.peak с момента создания cgroup до конца POST-серий: включает startup/регистрации/прогрев/tmpfs/healthchecks и небольшой resourceprobe, не только Go heap. Пики двух реплик не обязательно одновременны. + +Предварительный единичный wide-upload при прежних 384 MiB занял 16,375 s и достиг лимита; Docker разрешал ещё 384 MiB swap: [диагностика](evidence/upload-memory-before.json). Поэтому увеличен конечный RAM-бюджет и отключён swap. Декодер Go хранит пиксельный буфер и scanline buffers; ограничение сжатого файла 10 MiB не ограничивает RAM десятью MiB. GOMEMLIMIT — мягкий предел runtime, не жёсткий предел контейнера. Предварительный и итоговый прогоны не являются контролируемым сравнением производительности: изменены concurrency, лимиты и инструмент замера. + +Три короткие серии — проверка данного профиля, не максимальная пропускная способность, не steady-state/SLA и не доказательство пика для любого возможного файла. Клиент/VM/сервисы разделяют host. [Аудит результатов](evidence/upload-memory-audit.txt). + +### Отказы и сохранность + +| Воздействие | Наблюдение | Восстановление / данные | +| --- | --- | --- | +| Public API failover | 119 подтверждений, 2 переходные ошибки, ≥2 API до отказа | Первое подтверждение после старта hook 2,344 s; дубликатов/потери подтверждений нет: [вывод](evidence/lab4-failover.txt) | +| Остановка точного uploader, обе API по очереди | Другая API скачивает прежний оригинал; 0/1 переходных ошибок | SHA-256 совпал; обе API восстановлены: [вывод](evidence/attachment-resilience.txt) | +| Restart API/LB/PostgreSQL/Redis/MinIO | StartedAt изменился у всех, named volumes прежние | Старые сессии/сообщения/оригинал сохранены: [snapshots](evidence/attachment-resilience-containers.jsonl) | +| Реальная остановка S3 | Обе readiness=503, liveness через внутреннюю сеть=200, download=503 (API или LB после исключения backend) | После start S3 старые данные/токен и новый upload работают, API не перезапускались | +| INSERT после PUT отклонён | Временный trigger только для уникального тестового filename; 503, metadata нет, intent есть | После grace объект удалён, ready-файл сообщения сохранён: [вывод](evidence/upload-cleanup.txt) | + +Время собственного download-check считалось после snapshot и не является полным RTO от отказа. Grace в тесте ускорен SQL-сдвигом только собственных записей, а не реальным ожиданием 10 минут. Недоступность DELETE проверена отдельным CLI с закрытым endpoint, реальный S3 outage — отдельным сценарием. Потеря ответа COMMIT не внедрялась; механизм защищён атомарной публикацией/журналом. Очистка проверена явным CLI с тем же кодом, что использует scheduler; минутный запуск отдельно не измерялся. + +## 5. Ограничения + +- Один Docker-host, единственные PostgreSQL/S3/LB. Отказ host, утрата volume и HA этих сервисов не проверялись и не обеспечиваются двумя API. +- Сессии/идемпотентность сохраняются в PostgreSQL; Redis — только кэш. Пользователь БД — bootstrap-role официального образа, учебное упрощение. +- HTTP допустим только для локального синтетического стенда. TLS, presigned upload, thumbnails, broker/worker и треки на 5 не реализованы. +- Cleanup работает при наличии API или ручном запуске; tombstones растут бессрочно, ready без сообщений не удаляются, ручные orphan/missing объекты полного bucket не сверяются. +- Linux-hooks отдельно не проверены. Чистый клон проверен для реализации `879463c`; обновлённые отчёт и доказательства необходимо добавить в следующий commit и опубликовать. Ссылка на PR заполняется после его оформления. +- Измерения локальные и короткие; перед иным оборудованием/нагрузкой нужны новые проверки RAM/таймаутов. Публичный smoke не выставляет оценку автоматически. + +## 6. Источники и помощь + +- [Контракт текущего репозитория](contracts/README.md), README/COURSE и публичные tests; собственная реализация lab3. +- [MinIO multi-user](https://github.com/minio/minio/blob/master/docs/multi-user/README.md), [релизы MinIO](https://github.com/minio/minio/releases), [релиз mc](https://github.com/minio/mc/releases/tag/RELEASE.2025-04-16T18-13-26Z), [minio-go API](https://github.com/minio/minio-go/blob/master/docs/API.md). +- [Go GC / soft memory limit](https://go.dev/doc/gc-guide), [Linux cgroup v2](https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html), [Docker Compose memory/swap](https://docs.docker.com/reference/compose-file/services/#memswap_limit). + +- Библиотеки и версии закреплены в go.mod/go.sum. Codex: помощь с переносом, реализацией вложений и очистки, собственными проверками, локальными экспериментами и подготовкой отчёта. diff --git a/cmd/resourceprobe/main.go b/cmd/resourceprobe/main.go new file mode 100644 index 00000000..eb1dbdf5 --- /dev/null +++ b/cmd/resourceprobe/main.go @@ -0,0 +1,55 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + "strconv" + "strings" +) + +func main() { + if err := resourcesCommand(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func resourcesCommand() error { + result := make(map[string]uint64) + for file, label := range map[string]string{ + "memory.current": "memory_current_bytes", "memory.peak": "memory_peak_bytes", "memory.max": "memory_limit_bytes", + "memory.swap.current": "swap_current_bytes", "memory.swap.max": "swap_limit_bytes", + } { + data, err := os.ReadFile("/sys/fs/cgroup/" + file) + if err != nil { + return fmt.Errorf("cgroup v2 %s unavailable", file) + } + value, err := strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64) + if err != nil { + return fmt.Errorf("cgroup v2 %s must have a numeric limit", file) + } + result[label] = value + } + for file, fields := range map[string]map[string]string{ + "memory.events": {"oom": "oom_events", "oom_kill": "oom_kills"}, + "memory.stat": {"anon": "anon_bytes", "file": "file_bytes", "shmem": "shmem_bytes"}, + "cpu.stat": {"usage_usec": "cpu_usage_usec", "throttled_usec": "cpu_throttled_usec"}, + } { + data, err := os.ReadFile("/sys/fs/cgroup/" + file) + if err != nil { + return fmt.Errorf("cgroup v2 %s unavailable", file) + } + words := strings.Fields(string(data)) + for i := 0; i+1 < len(words); i += 2 { + if label, ok := fields[words[i]]; ok { + value, err := strconv.ParseUint(words[i+1], 10, 64) + if err != nil { + return fmt.Errorf("invalid cgroup v2 counter") + } + result[label] = value + } + } + } + return json.NewEncoder(os.Stdout).Encode(result) +} diff --git a/cmd/server/cleanup.go b/cmd/server/cleanup.go new file mode 100644 index 00000000..b205fc3c --- /dev/null +++ b/cmd/server/cleanup.go @@ -0,0 +1,61 @@ +package main + +import ( + "context" + "encoding/json" + "log" + "os" + "time" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/objectstore" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" +) + +func newObjectStore() (*objectstore.S3, error) { + return objectstore.New(objectstore.Config{ + Endpoint: envOrDefault("S3_ENDPOINT", "http://s3:9000"), Region: envOrDefault("S3_REGION", "us-east-1"), + Bucket: envOrDefault("S3_BUCKET", "messenger-attachments"), + AccessKeyFile: envOrDefault("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id"), + SecretKeyFile: envOrDefault("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key"), + }) +} + +func cleanupCommand(dryRun bool) error { + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + defer cancel() + pool, err := store.OpenPostgres(ctx, envOrDefault("DATABASE_PASSWORD_FILE", "/run/secrets/postgres_password")) + if err != nil { + return err + } + defer pool.Close() + objects, err := newObjectStore() + if err != nil { + return err + } + defer objects.Close() + summary, err := service.NewAttachments(store.NewPostgresStore(pool), objects).Cleanup(ctx, dryRun) + if encodeErr := json.NewEncoder(os.Stdout).Encode(summary); encodeErr != nil { + return encodeErr + } + return err +} + +func cleanupLoop(ctx context.Context, attachments *service.Attachments) { + ticker := time.NewTicker(time.Minute) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + summary, err := attachments.Cleanup(ctx, false) + if err != nil { + log.Print("attachment cleanup incomplete; will retry") + } + if summary.Candidates > 0 { + log.Printf("attachment cleanup deleted=%d protected=%d skipped=%d errors=%d", summary.Deleted, summary.Protected, summary.Skipped, summary.Errors) + } + } + } +} diff --git a/cmd/server/dbcheck.go b/cmd/server/dbcheck.go new file mode 100644 index 00000000..3daac573 --- /dev/null +++ b/cmd/server/dbcheck.go @@ -0,0 +1,41 @@ +package main + +import ( + "context" + "errors" + "fmt" + "time" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" +) + +func checkDatabase() error { + ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second) + defer cancel() + + pool, err := store.OpenPostgres(ctx, envOrDefault( + "DATABASE_PASSWORD_FILE", "/run/secrets/postgres_password", + )) + if err != nil { + return err + } + defer pool.Close() + + queryCtx, queryCancel := context.WithTimeout(ctx, 3*time.Second) + defer queryCancel() + + var applied bool + err = pool.QueryRow(queryCtx, + "SELECT EXISTS (SELECT 1 FROM schema_migrations WHERE version = $1)", + int64(1), + ).Scan(&applied) + if err != nil { + return errors.New("cannot read schema_migrations; check database and migrations") + } + if !applied { + return errors.New("migration 000001 is not applied") + } + + fmt.Println("PASS: PostgreSQL connection, migration 000001; pool max_conns=10") + return nil +} diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 00000000..6b6fc819 --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,189 @@ +package main + +import ( + "context" + "errors" + "fmt" + "log" + "net" + "net/http" + "os" + "os/signal" + "strconv" + "sync/atomic" + "syscall" + "time" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/cache" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/httpapi" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" +) + +func main() { + log.SetFlags(log.LstdFlags | log.LUTC) + if len(os.Args) > 1 && os.Args[1] == "cleanup" { + if len(os.Args) > 3 || (len(os.Args) == 3 && os.Args[2] != "--dry-run") { + log.Print("usage: /server cleanup [--dry-run]") + os.Exit(2) + } + if err := cleanupCommand(len(os.Args) == 3); err != nil { + log.Print(err) + os.Exit(1) + } + return + } + + if len(os.Args) == 2 && os.Args[1] == "dbcheck" { + if err := checkDatabase(); err != nil { + log.Print(err) + os.Exit(1) + } + return + } + + if len(os.Args) == 2 && (os.Args[1] == "healthcheck" || os.Args[1] == "instance-id") { + if err := healthcheck(); err != nil { + log.Print(err) + os.Exit(1) + } + return + } + + if err := run(); err != nil { + log.Print(err) + os.Exit(1) + } +} + +func run() error { + host := envOrDefault("APP_HOST", "0.0.0.0") + port := envOrDefault("APP_PORT", "8080") + + pool, err := store.OpenPostgres(context.Background(), envOrDefault( + "DATABASE_PASSWORD_FILE", "/run/secrets/postgres_password", + )) + if err != nil { + return err + } + defer pool.Close() + profiles := cache.NewProfiles(envOrDefault("REDIS_ADDR", "redis:6379")) + defer profiles.Close() + db := store.NewPostgresStore(pool).WithProfiles(profiles) + objects, err := newObjectStore() + if err != nil { + return err + } + defer objects.Close() + + var stopping atomic.Bool + ttl, err := strconv.ParseInt(envOrDefault("SESSION_TTL_SECONDS", "3600"), 10, 64) + if err != nil { + return errors.New("invalid SESSION_TTL_SECONDS") + } + attachments := service.NewAttachments(db, objects) + handler, err := httpapi.NewPostgresHandler(db, &stopping, ttl, attachments) + if err != nil { + return err + } + + server := &http.Server{ + Addr: net.JoinHostPort(host, port), + Handler: handler, + ReadHeaderTimeout: 5 * time.Second, + ReadTimeout: 30 * time.Second, + WriteTimeout: 60 * time.Second, + IdleTimeout: 60 * time.Second, + } + + ctx, stop := signal.NotifyContext( + context.Background(), + os.Interrupt, + syscall.SIGTERM, + ) + defer stop() + go cleanupLoop(ctx, attachments) + + serverErrors := make(chan error, 1) + + go func() { + log.Printf("server starting address=%s", server.Addr) + serverErrors <- server.ListenAndServe() + }() + + select { + case err := <-serverErrors: + if errors.Is(err, http.ErrServerClosed) { + return nil + } + return err + + case <-ctx.Done(): + stopping.Store(true) + log.Print("shutdown started timeout=8s") + } + + started := time.Now() + + shutdownCtx, cancel := context.WithTimeout( + context.Background(), + 8*time.Second, + ) + defer cancel() + + if err := server.Shutdown(shutdownCtx); err != nil { + _ = server.Close() + return fmt.Errorf("shutdown forced: %w", err) + } + + log.Printf( + "shutdown completed duration_ms=%d", + time.Since(started).Milliseconds(), + ) + + return nil +} + +func healthcheck() error { + host := envOrDefault("APP_HOST", "0.0.0.0") + port := envOrDefault("APP_PORT", "8080") + + switch host { + case "0.0.0.0", "": + host = "127.0.0.1" + case "::": + host = "::1" + } + + client := &http.Client{ + Timeout: 2 * time.Second, + } + + url := "http://" + net.JoinHostPort(host, port) + "/health/ready" + + response, err := client.Get(url) + if err != nil { + return fmt.Errorf("healthcheck: %w", err) + } + defer response.Body.Close() + + if response.StatusCode != http.StatusOK { + return fmt.Errorf("healthcheck: status=%d", response.StatusCode) + } + if len(os.Args) == 2 && os.Args[1] == "instance-id" { + id := response.Header.Get("X-Instance-Id") + if id == "" { + return errors.New("missing X-Instance-Id") + } + fmt.Println(id) + } + + return nil +} + +func envOrDefault(name string, fallback string) string { + if value := os.Getenv(name); value != "" { + return value + } + return fallback +} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..8dafbe22 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,149 @@ +name: messenger-lab4 + +services: + api: + build: . + scale: 2 + environment: + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + GOMEMLIMIT: 1024MiB + DATABASE_PASSWORD_FILE: /run/secrets/postgres_password + REDIS_ADDR: redis:6379 + S3_ENDPOINT: http://s3:9000 + S3_REGION: ${S3_REGION:-us-east-1} + S3_BUCKET: ${S3_BUCKET:-messenger-attachments} + S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id + S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key + secrets: [postgres_password, s3_access_key_id, s3_secret_access_key] + # Bounded ephemeral staging; one upload at a time per API. + tmpfs: + - /tmp:size=16m,mode=1777 + cpus: 1.0 + # 16-bit PNG decoding includes the image and scanline buffers, not just bytes. + mem_limit: 1536m + memswap_limit: 1536m + stop_grace_period: 15s + depends_on: + postgres: + condition: service_healthy + migrate: + condition: service_completed_successfully + redis: + condition: service_started + s3-bootstrap: + condition: service_completed_successfully + + s3: + build: + context: ./infra/s3 + target: server + command: [server, /data, --console-address, "127.0.0.1:9001"] + environment: + MINIO_ROOT_USER_FILE: /run/secrets/s3_root_user + MINIO_ROOT_PASSWORD_FILE: /run/secrets/s3_root_password + MINIO_BROWSER: "off" + MINIO_REGION_NAME: ${S3_REGION:-us-east-1} + secrets: [s3_root_user, s3_root_password] + volumes: + - s3_lab4_data_v1:/data + cpus: 1.0 + mem_limit: 512m + stop_grace_period: 30s + # No published ports: S3 is reachable only inside the Compose network. + + s3-bootstrap: + build: + context: ./infra/s3 + target: client + entrypoint: [/bin/sh, /bootstrap/bootstrap.sh] + environment: + S3_BUCKET: ${S3_BUCKET:-messenger-attachments} + MC_CONFIG_DIR: /tmp/mc + secrets: [s3_root_user, s3_root_password, s3_access_key_id, s3_secret_access_key] + volumes: + - ./infra/s3:/bootstrap:ro + tmpfs: [/tmp] + cpus: 0.25 + mem_limit: 128m + depends_on: + s3: + condition: service_started + + lb: + image: haproxy:3.2.4-alpine + ports: + - "127.0.0.1:${LB_PORT:-8084}:8080" + volumes: + - ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro + cpus: 0.5 + mem_limit: 64m + depends_on: + api: + condition: service_healthy + + postgres: + image: postgres:17.9-bookworm + environment: + POSTGRES_DB: messenger + POSTGRES_USER: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + POSTGRES_INITDB_ARGS: --encoding=UTF8 + secrets: [postgres_password] + volumes: + - postgres_lab4_data_v1:/var/lib/postgresql/data + cpus: 1.0 + mem_limit: 512m + healthcheck: + test: [CMD-SHELL, "pg_isready -U messenger -d messenger"] + interval: 3s + timeout: 3s + retries: 20 + start_period: 10s + stop_grace_period: 30s + + redis: + image: redis:7.4.11-alpine + command: [redis-server, --save, "", --appendonly, "no", --maxmemory, 64mb, --maxmemory-policy, allkeys-lru] + tmpfs: [/data] + cpus: 0.25 + mem_limit: 128m + healthcheck: + test: [CMD, redis-cli, ping] + interval: 3s + timeout: 2s + retries: 10 + + migrate: + image: postgres:17.9-bookworm + environment: + PGHOST: postgres + PGUSER: messenger + PGDATABASE: messenger + PGCONNECT_TIMEOUT: "5" + secrets: [postgres_password] + volumes: + - ./migrations:/migrations:ro + entrypoint: [/bin/sh, -ec] + command: + - 'export PGPASSWORD="$$(cat /run/secrets/postgres_password)"; exec psql -X -f /migrations/apply.psql' + cpus: 0.5 + mem_limit: 128m + depends_on: + postgres: + condition: service_healthy + +secrets: + postgres_password: + file: ./secrets/postgres_password.txt + s3_root_user: + file: ./secrets/s3_root_user.txt + s3_root_password: + file: ./secrets/s3_root_password.txt + s3_access_key_id: + file: ./secrets/s3_access_key_id.txt + s3_secret_access_key: + file: ./secrets/s3_secret_access_key.txt + +volumes: + postgres_lab4_data_v1: + s3_lab4_data_v1: diff --git a/docs/attachment-consistency.md b/docs/attachment-consistency.md new file mode 100644 index 00000000..8d1435b3 --- /dev/null +++ b/docs/attachment-consistency.md @@ -0,0 +1,46 @@ +# Согласованность вложений: уровень 4 + +## Публикация + +1. Авторизация и проверка доступа к чату. +2. Ограниченное чтение/проверка изображения во временном tmpfs. При прерывании до конца проверки S3 не вызывается. +3. Отдельный COMMIT намерения в `upload_intents`: серверный UUID, chat/owner, grace 10 минут по времени PostgreSQL. +4. Один PUT исходных байтов в приватный S3. +5. Транзакция PostgreSQL блокирует намерение, проверяет `pending` и срок, вставляет ready-метаданные и удаляет намерение. +6. После COMMIT возвращается 201. Временный файл удаляется, слот Validator освобождается. + +При ошибке или неизвестном исходе PUT/COMMIT немедленный DELETE не выполняется. Если публикация закоммичена, метаданные существуют, а намерение удалено в той же транзакции. Если публикация не закоммичена, намерение остаётся для очистки. + +## Очистка + +Каждая API проверяет журнал раз в минуту. За один проход выбирается не более 100 кандидатов с истёкшим grace и наступившим `next_cleanup_at`. Для каждого кандидата открывается транзакция, строка блокируется `FOR UPDATE SKIP LOCKED`, условия возраста проверяются снова. Публикация использует ту же строку и блокировку. + +- Свежая или занятая строка пропускается. +- Если ready-метаданные существуют, объект сохраняется, лишняя запись журнала удаляется. +- Если метаданных нет, выполняется S3 DELETE. После успеха запись получает `abandoned`, следующий проход для неё — через 10 минут. +- При отказе S3/БД транзакция не фиксируется; запись остаётся для повтора. При недоступной БД S3 DELETE не начинается. +- В `--dry-run` S3 и журнал не изменяются. + +Отметки abandoned сохраняются: повторный DELETE убирает поздний PUT после сбоя API. DELETE отсутствующего объекта идемпотентен. Отказ между DELETE и COMMIT приводит к безопасному повтору. Публикация abandoned/истёкшего намерения запрещена. + +Grace 10 минут больше штатного upload context (45 секунд) и S3 operation timeout (30 секунд). Отдельный источник времени API не используется для возраста записей. Приостановленный процесс или поздняя запись не дают права удалить готовое вложение: публикация проверяет состояние журнала, а tombstone остаётся для повторной очистки. + +## Команды + +Из корня репозитория, при работающем стенде: + +```powershell +docker compose exec -T api /server cleanup --dry-run +docker compose exec -T api /server cleanup +python -X utf8 student-tests/upload_cleanup.py --base-url http://127.0.0.1:8084 +``` + +Команда выбирает одну API; stdout содержит только агрегаты JSON. Тест создаёт синтетических пользователей/чат/файл и временный SQL-trigger, который отклоняет INSERT только для уникального тестового filename. Trigger удаляется в finally. Для ускорения тест явно состаривает только собственные записи журнала; десять минут реального ожидания этим не подтверждаются. Один проход cleanup может также обработать другие уже просроченные записи учебного стенда. + +## Границы + +- Очистка работает, пока запущена хотя бы одна API или оператор запускает команду. После полной остановки обработка возобновляется после старта API. +- Очистка не удаляет ready-вложения, даже если они пока не прикреплены к сообщениям. +- Tombstone занимает небольшую строку PostgreSQL бессрочно и вызывает периодический DELETE. Это сознательная цена обработки поздних PUT; нагрузка ограничена размером прохода, но при большом журнале очистка может отставать. +- Механизм учитывает загрузки, созданные приложением после миграции 5. Он не обходит весь bucket, не ищет ручные записи посторонних клиентов и не восстанавливает удалённые извне оригиналы. Полный reconciler из трека на 5 не заявлен. +- Прямой доступ оператора к таблицам/S3 может нарушить инварианты; тестовые SQL-вмешательства обозначаются отдельно. diff --git a/evidence/attachment-messages-build.txt b/evidence/attachment-messages-build.txt new file mode 100644 index 00000000..263b8656 --- /dev/null +++ b/evidence/attachment-messages-build.txt @@ -0,0 +1,201 @@ +#1 [internal] load local bake definitions +#1 reading from stdin 1.75kB 0.0s done +#1 DONE 0.0s + +#2 [s3-bootstrap internal] load build definition from Dockerfile +#2 transferring dockerfile: 691B 0.0s done +#2 DONE 0.0s + +#3 [api internal] load build definition from Dockerfile +#3 transferring dockerfile: 727B done +#3 DONE 0.0s + +#4 [api internal] load metadata for docker.io/library/golang:1.26.3 +#4 DONE 0.1s + +#5 [api internal] load .dockerignore +#5 transferring context: 216B done +#5 DONE 0.0s + +#6 [api build 1/7] FROM docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a +#6 resolve docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a 0.0s done +#6 DONE 0.0s + +#7 [api internal] load build context +#7 transferring context: 12.75kB 0.0s done +#7 DONE 0.0s + +#8 [s3 internal] load metadata for docker.io/library/alpine:3.22.2 +#8 ... + +#9 [api build 2/7] WORKDIR /src +#9 CACHED + +#10 [api build 3/7] COPY go.mod go.sum ./ +#10 CACHED + +#11 [api build 4/7] RUN go mod download +#11 CACHED + +#12 [api build 5/7] COPY cmd ./cmd +#12 CACHED + +#13 [api build 6/7] COPY internal ./internal +#13 DONE 0.1s + +#14 [api build 7/7] RUN go vet ./... && go test ./... && go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server +#14 ... + +#6 [s3 build 1/7] FROM docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a +#6 resolve docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a 0.0s done +#6 DONE 0.0s + +#8 [s3-bootstrap internal] load metadata for docker.io/library/alpine:3.22.2 +#8 DONE 0.7s + +#15 [s3 internal] load .dockerignore +#15 transferring context: 2B done +#15 DONE 0.0s + +#16 [s3 client 1/2] FROM docker.io/library/alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 +#16 resolve docker.io/library/alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 0.0s done +#16 DONE 0.0s + +#17 [s3-bootstrap client-build 2/4] RUN git clone --depth 1 --branch RELEASE.2025-04-16T18-13-26Z https://github.com/minio/mc.git /src +#17 CACHED + +#18 [s3-bootstrap client-build 4/4] RUN go build -trimpath -o /out/mc . +#18 CACHED + +#19 [s3-bootstrap client-build 3/4] WORKDIR /src +#19 CACHED + +#20 [s3 server-build 2/4] RUN git clone --depth 1 --branch RELEASE.2025-04-22T22-12-26Z https://github.com/minio/minio.git /src +#20 CACHED + +#21 [s3 server-build 4/4] RUN go build -trimpath -o /out/minio . +#21 CACHED + +#22 [s3 server-build 3/4] WORKDIR /src +#22 CACHED + +#23 [s3-bootstrap client 2/2] COPY --from=client-build /out/mc /usr/local/bin/mc +#23 CACHED + +#24 [s3 server 2/2] COPY --from=server-build /out/minio /usr/local/bin/minio +#24 CACHED + +#25 [s3] exporting to image +#25 exporting layers done +#25 exporting manifest sha256:8410921329eec1208439c3cd9dcb0b1ced8a8fb0a7348cea1d338f6f0a79c049 done +#25 exporting config sha256:350ec0fe229ab9c2b91b2c87cfb4e1e43d6dc3bbad5d7d9c0c7af1e5cf528867 done +#25 exporting attestation manifest sha256:d01fde671342f07725d369deb13d1791aa47667326ab2ec3c9003b663b16b5f5 0.1s done +#25 exporting manifest list sha256:21e8273ef94a7b496a3c98f3c3846fdde4081c284e1ace51f1e6c60e3f774f93 +#25 ... + +#26 [s3-bootstrap] exporting to image +#26 exporting layers done +#26 exporting manifest sha256:2103cc3b4036db5a1e5ee6580cef84645bc7465a14181fa62db60394f79fddfe done +#26 exporting config sha256:86a478d925e08843fe09c5d8f53e001d9daffdf9baab39f6bbfc48ce67db5b4d done +#26 exporting attestation manifest sha256:ff723fa1b7db29b5618f3765f6d86887a0f2a8c649465f8c2c2d5150f9ffe4aa 0.1s done +#26 exporting manifest list sha256:aa0263ea4b39017072f52d3aae04cc165ab869a60cbc7eceec3f736a00c243f3 0.1s done +#26 naming to docker.io/library/messenger-lab4-s3-bootstrap:latest done +#26 unpacking to docker.io/library/messenger-lab4-s3-bootstrap:latest done +#26 DONE 0.3s + +#25 [s3] exporting to image +#25 exporting manifest list sha256:21e8273ef94a7b496a3c98f3c3846fdde4081c284e1ace51f1e6c60e3f774f93 0.0s done +#25 naming to docker.io/library/messenger-lab4-s3:latest done +#25 unpacking to docker.io/library/messenger-lab4-s3:latest done +#25 DONE 0.3s + +#14 [api build 7/7] RUN go vet ./... && go test ./... && go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server +#14 ... + +#27 [s3] resolving provenance for metadata file +#27 DONE 0.0s + +#28 [s3-bootstrap] resolving provenance for metadata file +#28 DONE 0.0s + +#14 [api build 7/7] RUN go vet ./... && go test ./... && go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server +#14 33.66 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/cmd/server [no test files] +#14 33.66 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/cache [no test files] +#14 33.96 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/httpapi 0.286s +#14 33.96 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model [no test files] +#14 33.96 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/objectstore 0.012s +#14 34.53 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/password 0.862s +#14 34.53 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service 0.733s +#14 34.53 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store 0.008s +#14 34.53 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/upload 0.089s +#14 DONE 62.6s + +#29 [api stage-2 1/1] COPY --from=build /out/server /server +#29 DONE 0.1s + +#30 [api] exporting to image +#30 exporting layers +#30 exporting layers 0.9s done +#30 exporting manifest sha256:95b4db2aa90dcb74e9b29cbe6cbd9c7edec76d6b5e2cc3f21012ddff4b1a6d66 0.0s done +#30 exporting config sha256:991156718301b72fdddae99e1e77f895e5e4fdf646cab8de6590c552e70bdac3 0.0s done +#30 exporting attestation manifest sha256:d086c2a28d573ddfd481ee66ae7b4780537d281c636302c2c4546815f2e24422 0.0s done +#30 exporting manifest list sha256:add0a2bd74108be69f57403b061d9bf1790f9a444e46a452a6009f30dc3466e0 +#30 exporting manifest list sha256:add0a2bd74108be69f57403b061d9bf1790f9a444e46a452a6009f30dc3466e0 0.0s done +#30 naming to docker.io/library/messenger-lab4-api:latest done +#30 unpacking to docker.io/library/messenger-lab4-api:latest 0.1s done +#30 DONE 1.2s + +#31 [api] resolving provenance for metadata file +#31 DONE 0.0s + messenger-lab4-s3 Built + messenger-lab4-s3-bootstrap Built + messenger-lab4-api Built + Container messenger-lab4-redis-1 Running + Container messenger-lab4-postgres-1 Running + Container messenger-lab4-s3-1 Recreate + Container messenger-lab4-s3-1 Recreated + Container messenger-lab4-s3-bootstrap-1 Recreate + Container messenger-lab4-s3-bootstrap-1 Recreated + Container messenger-lab4-api-2 Recreate + Container messenger-lab4-api-1 Recreate + Container messenger-lab4-api-2 Recreated + Container messenger-lab4-api-1 Recreated + Container messenger-lab4-lb-1 Running + Container messenger-lab4-s3-1 Starting + Container messenger-lab4-postgres-1 Waiting + Container messenger-lab4-s3-1 Started + Container messenger-lab4-s3-bootstrap-1 Starting + Container messenger-lab4-postgres-1 Healthy + Container messenger-lab4-migrate-1 Starting + Container messenger-lab4-migrate-1 Started + Container messenger-lab4-s3-bootstrap-1 Started + Container messenger-lab4-s3-bootstrap-1 Waiting + Container messenger-lab4-postgres-1 Waiting + Container messenger-lab4-migrate-1 Waiting + Container messenger-lab4-migrate-1 Exited + Container messenger-lab4-postgres-1 Healthy + Container messenger-lab4-s3-bootstrap-1 Exited + Container messenger-lab4-api-1 Starting + Container messenger-lab4-api-1 Started + Container messenger-lab4-api-2 Starting + Container messenger-lab4-api-2 Started + Container messenger-lab4-api-1 Waiting + Container messenger-lab4-api-2 Waiting + Container messenger-lab4-api-1 Healthy + Container messenger-lab4-api-2 Healthy + Container messenger-lab4-s3-bootstrap-1 Waiting + Container messenger-lab4-lb-1 Waiting + Container messenger-lab4-postgres-1 Waiting + Container messenger-lab4-redis-1 Waiting + Container messenger-lab4-migrate-1 Waiting + Container messenger-lab4-api-1 Waiting + Container messenger-lab4-api-2 Waiting + Container messenger-lab4-s3-1 Waiting + Container messenger-lab4-redis-1 Healthy + Container messenger-lab4-lb-1 Healthy + Container messenger-lab4-s3-1 Healthy + Container messenger-lab4-api-1 Healthy + Container messenger-lab4-api-2 Healthy + Container messenger-lab4-migrate-1 Exited + Container messenger-lab4-s3-bootstrap-1 Exited + Container messenger-lab4-postgres-1 Healthy diff --git a/evidence/attachment-messages-final.txt b/evidence/attachment-messages-final.txt new file mode 100644 index 00000000..42cfce3b --- /dev/null +++ b/evidence/attachment-messages-final.txt @@ -0,0 +1,7 @@ +PASS: attachment-only message, non-owner participant, list order, replay and changed-list 409 +PASS: text-only compatibility; omitted attachment_ids and [] replay identically +PASS: empty message, duplicates, >10 IDs, bad UUID/types/null/unknown fields return 400 +PASS: foreign-chat/unknown attachment and outsider return 404; rejected request does not consume key +PASS: exactly ten distinct attachments are accepted +PASS: 16 concurrent retries across two replicas return one message, history preserves attachment order +PASS: message attachment checks completed diff --git a/evidence/attachment-messages.txt b/evidence/attachment-messages.txt new file mode 100644 index 00000000..42cfce3b --- /dev/null +++ b/evidence/attachment-messages.txt @@ -0,0 +1,7 @@ +PASS: attachment-only message, non-owner participant, list order, replay and changed-list 409 +PASS: text-only compatibility; omitted attachment_ids and [] replay identically +PASS: empty message, duplicates, >10 IDs, bad UUID/types/null/unknown fields return 400 +PASS: foreign-chat/unknown attachment and outsider return 404; rejected request does not consume key +PASS: exactly ten distinct attachments are accepted +PASS: 16 concurrent retries across two replicas return one message, history preserves attachment order +PASS: message attachment checks completed diff --git a/evidence/attachment-resilience-containers.jsonl b/evidence/attachment-resilience-containers.jsonl new file mode 100644 index 00000000..783c4b0c --- /dev/null +++ b/evidence/attachment-resilience-containers.jsonl @@ -0,0 +1,13 @@ +{"stage": "initial", "utc": "2026-11-06T13:38:15.865959+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "running", "started_at": "2026-11-06T13:26:47.744708442Z", "pid": 127588, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "running", "started_at": "2026-11-06T13:36:29.512778649Z", "pid": 140804, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:05:54.008269622Z", "pid": 54566, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:36:30.96778572Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T12:41:09.47886048Z", "pid": 20816, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:05:39.610516836Z", "pid": 53864, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "running", "started_at": "2026-11-06T13:26:38.817247404Z", "pid": 127088, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:36:30.477275015Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} +{"stage": "before-public-failover", "utc": "2026-11-06T13:38:16.724702+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "running", "started_at": "2026-11-06T13:26:47.744708442Z", "pid": 127588, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "running", "started_at": "2026-11-06T13:36:29.512778649Z", "pid": 140804, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:05:54.008269622Z", "pid": 54566, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:36:30.96778572Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T12:41:09.47886048Z", "pid": 20816, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:05:39.610516836Z", "pid": 53864, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "running", "started_at": "2026-11-06T13:26:38.817247404Z", "pid": 127088, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:36:30.477275015Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} +{"stage": "after-public-failover", "utc": "2026-11-06T13:38:34.762311+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "exited", "started_at": "2026-11-06T13:26:47.744708442Z", "pid": 0, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "running", "started_at": "2026-11-06T13:36:29.512778649Z", "pid": 140804, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:05:54.008269622Z", "pid": 54566, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:36:30.96778572Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T12:41:09.47886048Z", "pid": 20816, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:05:39.610516836Z", "pid": 53864, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "running", "started_at": "2026-11-06T13:26:38.817247404Z", "pid": 127088, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:36:30.477275015Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} +{"stage": "before-uploader-stop-1", "utc": "2026-11-06T13:38:40.311493+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "running", "started_at": "2026-11-06T13:38:35.381831522Z", "pid": 143620, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "running", "started_at": "2026-11-06T13:36:29.512778649Z", "pid": 140804, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:05:54.008269622Z", "pid": 54566, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:36:30.96778572Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T12:41:09.47886048Z", "pid": 20816, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:05:39.610516836Z", "pid": 53864, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "running", "started_at": "2026-11-06T13:26:38.817247404Z", "pid": 127088, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:36:30.477275015Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} +{"stage": "after-uploader-stop-1", "utc": "2026-11-06T13:38:41.598470+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "exited", "started_at": "2026-11-06T13:38:35.381831522Z", "pid": 0, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "running", "started_at": "2026-11-06T13:36:29.512778649Z", "pid": 140804, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:05:54.008269622Z", "pid": 54566, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:36:30.96778572Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T12:41:09.47886048Z", "pid": 20816, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:05:39.610516836Z", "pid": 53864, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "running", "started_at": "2026-11-06T13:26:38.817247404Z", "pid": 127088, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:36:30.477275015Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} +{"stage": "before-uploader-stop-2", "utc": "2026-11-06T13:38:46.435953+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "running", "started_at": "2026-11-06T13:38:42.008445146Z", "pid": 143871, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "running", "started_at": "2026-11-06T13:36:29.512778649Z", "pid": 140804, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:05:54.008269622Z", "pid": 54566, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:36:30.96778572Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T12:41:09.47886048Z", "pid": 20816, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:05:39.610516836Z", "pid": 53864, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "running", "started_at": "2026-11-06T13:26:38.817247404Z", "pid": 127088, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:36:30.477275015Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} +{"stage": "after-uploader-stop-2", "utc": "2026-11-06T13:38:47.724517+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "running", "started_at": "2026-11-06T13:38:42.008445146Z", "pid": 143871, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "exited", "started_at": "2026-11-06T13:36:29.512778649Z", "pid": 0, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:05:54.008269622Z", "pid": 54566, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:36:30.96778572Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T12:41:09.47886048Z", "pid": 20816, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:05:39.610516836Z", "pid": 53864, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "running", "started_at": "2026-11-06T13:26:38.817247404Z", "pid": 127088, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:36:30.477275015Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} +{"stage": "before-public-persistence", "utc": "2026-11-06T13:38:53.116484+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "running", "started_at": "2026-11-06T13:38:42.008445146Z", "pid": 143871, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "running", "started_at": "2026-11-06T13:38:49.176617456Z", "pid": 144102, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:05:54.008269622Z", "pid": 54566, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:36:30.96778572Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T12:41:09.47886048Z", "pid": 20816, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:05:39.610516836Z", "pid": 53864, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "running", "started_at": "2026-11-06T13:26:38.817247404Z", "pid": 127088, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:36:30.477275015Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} +{"stage": "after-public-persistence", "utc": "2026-11-06T13:39:14.975023+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "running", "started_at": "2026-11-06T13:39:07.240399732Z", "pid": 145134, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "running", "started_at": "2026-11-06T13:39:06.896828238Z", "pid": 145066, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:39:12.971627216Z", "pid": 145302, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:39:04.145721821Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T13:38:58.379439276Z", "pid": 144593, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:38:58.333215572Z", "pid": 144576, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "running", "started_at": "2026-11-06T13:38:58.383492519Z", "pid": 144601, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:38:58.806975519Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} +{"stage": "before-s3-outage", "utc": "2026-11-06T13:39:17.470874+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "running", "started_at": "2026-11-06T13:39:07.240399732Z", "pid": 145134, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "running", "started_at": "2026-11-06T13:39:06.896828238Z", "pid": 145066, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:39:12.971627216Z", "pid": 145302, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:39:04.145721821Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T13:38:58.379439276Z", "pid": 144593, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:38:58.333215572Z", "pid": 144576, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "running", "started_at": "2026-11-06T13:38:58.383492519Z", "pid": 144601, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:38:58.806975519Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} +{"stage": "during-s3-outage", "utc": "2026-11-06T13:39:19.075481+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "running", "started_at": "2026-11-06T13:39:07.240399732Z", "pid": 145134, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "running", "started_at": "2026-11-06T13:39:06.896828238Z", "pid": 145066, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:39:12.971627216Z", "pid": 145302, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:39:04.145721821Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T13:38:58.379439276Z", "pid": 144593, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:38:58.333215572Z", "pid": 144576, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "exited", "started_at": "2026-11-06T13:38:58.383492519Z", "pid": 0, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:38:58.806975519Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} +{"stage": "after-s3-outage", "utc": "2026-11-06T13:39:29.483542+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "running", "started_at": "2026-11-06T13:39:07.240399732Z", "pid": 145134, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "running", "started_at": "2026-11-06T13:39:06.896828238Z", "pid": 145066, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:39:12.971627216Z", "pid": 145302, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:39:04.145721821Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T13:38:58.379439276Z", "pid": 144593, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:38:58.333215572Z", "pid": 144576, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "running", "started_at": "2026-11-06T13:39:23.987572109Z", "pid": 145765, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:38:58.806975519Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} +{"stage": "final-restored", "utc": "2026-11-06T13:39:32.361689+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "running", "started_at": "2026-11-06T13:39:07.240399732Z", "pid": 145134, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "running", "started_at": "2026-11-06T13:39:06.896828238Z", "pid": 145066, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:39:12.971627216Z", "pid": 145302, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:39:04.145721821Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T13:38:58.379439276Z", "pid": 144593, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:38:58.333215572Z", "pid": 144576, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "running", "started_at": "2026-11-06T13:39:23.987572109Z", "pid": 145765, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:38:58.806975519Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} diff --git a/evidence/attachment-resilience-final-stack.txt b/evidence/attachment-resilience-final-stack.txt new file mode 100644 index 00000000..0bd4a5e3 --- /dev/null +++ b/evidence/attachment-resilience-final-stack.txt @@ -0,0 +1,7 @@ +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +messenger-lab4-api-1 messenger-lab4-api "/server" api 14 minutes ago Up About a minute (healthy) 8080/tcp +messenger-lab4-api-2 messenger-lab4-api "/server" api 14 minutes ago Up About a minute (healthy) 8080/tcp +messenger-lab4-lb-1 haproxy:3.2.4-alpine "docker-entrypoint.s…" lb 35 minutes ago Up About a minute 127.0.0.1:8084->8080/tcp +messenger-lab4-postgres-1 postgres:17.9-bookworm "docker-entrypoint.s…" postgres 59 minutes ago Up About a minute (healthy) 5432/tcp +messenger-lab4-redis-1 redis:7.4.11-alpine "docker-entrypoint.s…" redis 35 minutes ago Up About a minute (healthy) 6379/tcp +messenger-lab4-s3-1 messenger-lab4-s3 "minio server /data …" s3 14 minutes ago Up About a minute diff --git a/evidence/attachment-resilience-first-attempt-containers.jsonl b/evidence/attachment-resilience-first-attempt-containers.jsonl new file mode 100644 index 00000000..03442001 --- /dev/null +++ b/evidence/attachment-resilience-first-attempt-containers.jsonl @@ -0,0 +1,4 @@ +{"stage": "initial", "utc": "2026-11-06T13:36:03.325580+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "running", "started_at": "2026-11-06T13:26:47.744708442Z", "pid": 127588, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "running", "started_at": "2026-11-06T13:26:47.376065891Z", "pid": 127523, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:05:54.008269622Z", "pid": 54566, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:35:57.299909733Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T12:41:09.47886048Z", "pid": 20816, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:05:39.610516836Z", "pid": 53864, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "running", "started_at": "2026-11-06T13:26:38.817247404Z", "pid": 127088, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:35:56.807641478Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} +{"stage": "before-public-failover", "utc": "2026-11-06T13:36:04.201267+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "running", "started_at": "2026-11-06T13:26:47.744708442Z", "pid": 127588, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "running", "started_at": "2026-11-06T13:26:47.376065891Z", "pid": 127523, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:05:54.008269622Z", "pid": 54566, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:35:57.299909733Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T12:41:09.47886048Z", "pid": 20816, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:05:39.610516836Z", "pid": 53864, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "running", "started_at": "2026-11-06T13:26:38.817247404Z", "pid": 127088, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:35:56.807641478Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} +{"stage": "after-public-failover", "utc": "2026-11-06T13:36:23.137346+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "running", "started_at": "2026-11-06T13:26:47.744708442Z", "pid": 127588, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "exited", "started_at": "2026-11-06T13:26:47.376065891Z", "pid": 0, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:05:54.008269622Z", "pid": 54566, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:35:57.299909733Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T12:41:09.47886048Z", "pid": 20816, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:05:39.610516836Z", "pid": 53864, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "running", "started_at": "2026-11-06T13:26:38.817247404Z", "pid": 127088, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:35:56.807641478Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} +{"stage": "final-restored", "utc": "2026-11-06T13:36:37.599395+00:00", "containers": [{"id": "6a31e758ab31695155102d5ff184d17a3e68d9073bd432d89fd38715c54ca169", "name": "/messenger-lab4-api-1", "service": "api", "status": "running", "started_at": "2026-11-06T13:26:47.744708442Z", "pid": 127588, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "a3f3dcbed649d3785115042d7f9c2740f0505443badcfbd267365ad9bcc3d737", "name": "/messenger-lab4-api-2", "service": "api", "status": "running", "started_at": "2026-11-06T13:36:29.512778649Z", "pid": 140804, "image_id": "sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499", "volumes": []}, {"id": "5016c1318f8bf5d7c653da40f12ac6f12e2c8a9ff48432292efc8bb30b132d19", "name": "/messenger-lab4-lb-1", "service": "lb", "status": "running", "started_at": "2026-11-06T13:05:54.008269622Z", "pid": 54566, "image_id": "sha256:436a5691e6fd563ca4090e351048fda1066bec93b595cc8eaf26988c65eb1152", "volumes": []}, {"id": "266063e787b9270687163705e894614ae2e5bb5e143cd0720a8f74dc7421d670", "name": "/messenger-lab4-migrate-1", "service": "migrate", "status": "exited", "started_at": "2026-11-06T13:36:30.96778572Z", "pid": 0, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "3f30a9f0ca1255021049366c7689c679c196cea00d162b1ea92ef5e071508a16", "destination": "/var/lib/postgresql/data"}]}, {"id": "1d00ec06a5760f338a9fd1bfccd464024ced37f3d50f7ea1207ec852efacd1a5", "name": "/messenger-lab4-postgres-1", "service": "postgres", "status": "running", "started_at": "2026-11-06T12:41:09.47886048Z", "pid": 20816, "image_id": "sha256:47f917f7409eacd22fc5dfb1dee634e1b55cf0c01d1a7eb701be2227a03e0641", "volumes": [{"name": "messenger-lab4_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"id": "9ebc4e7f55faedb45a92d06a5301782731b772634449a60a34283e4ee2891bcb", "name": "/messenger-lab4-redis-1", "service": "redis", "status": "running", "started_at": "2026-11-06T13:05:39.610516836Z", "pid": 53864, "image_id": "sha256:858f009f9709ce576febc734aa78b8f6d624b82571f9ddb6bda4377c833b3499", "volumes": []}, {"id": "1fc6c2680ed90b14d0f7b057a81045481c20f01450a8a951da91a011a422ad78", "name": "/messenger-lab4-s3-1", "service": "s3", "status": "running", "started_at": "2026-11-06T13:26:38.817247404Z", "pid": 127088, "image_id": "sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942", "volumes": [{"name": "messenger-lab4_s3_lab4_data_v1", "destination": "/data"}]}, {"id": "fa34d67373614d131761f2214a66ac40116a2b449e99efeb4d0e467a72bd45f6", "name": "/messenger-lab4-s3-bootstrap-1", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T13:36:30.477275015Z", "pid": 0, "image_id": "sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2", "volumes": []}]} diff --git a/evidence/attachment-resilience-first-attempt.txt b/evidence/attachment-resilience-first-attempt.txt new file mode 100644 index 00000000..6457cced --- /dev/null +++ b/evidence/attachment-resilience-first-attempt.txt @@ -0,0 +1,2 @@ +PASS: public failover; one API stopped, in-flight messages/session/original preserved +FAIL: POST /api/v1/users: expected [201], received 503 diff --git a/evidence/attachment-resilience.txt b/evidence/attachment-resilience.txt new file mode 100644 index 00000000..0a834f6c --- /dev/null +++ b/evidence/attachment-resilience.txt @@ -0,0 +1,8 @@ +PASS: public failover; one API stopped, in-flight messages/session/original preserved +{"scenario": "uploader-failover", "replica": 1, "recovery_seconds_after_stop": 0.015, "transient_failures": 0, "checksum_match": true} +{"scenario": "uploader-failover", "replica": 2, "recovery_seconds_after_stop": 1.25, "transient_failures": 1, "checksum_match": true} +PASS: upload -> stop exact uploader -> other API downloads identical original, for both replicas +PASS: public persistence; API/LB/PostgreSQL/Redis/S3 restarted, named volumes unchanged +PASS: real S3 stop; both API ready=503, live=200, public download=503 (API or LB) +PASS: S3 restored; original/message/session preserved, new upload works, API processes did not restart +PASS: attachment resilience checks completed diff --git a/evidence/attachments-api-build.txt b/evidence/attachments-api-build.txt new file mode 100644 index 00000000..3e7435bf --- /dev/null +++ b/evidence/attachments-api-build.txt @@ -0,0 +1,209 @@ +#1 [internal] load local bake definitions +#1 reading from stdin 1.75kB 0.0s done +#1 DONE 0.0s + +#2 [s3 internal] load build definition from Dockerfile +#2 transferring dockerfile: 691B 0.0s done +#2 DONE 0.0s + +#3 [api internal] load build definition from Dockerfile +#3 transferring dockerfile: 727B 0.0s done +#3 DONE 0.0s + +#4 [api internal] load metadata for docker.io/library/golang:1.26.3 +#4 DONE 0.2s + +#5 [api internal] load .dockerignore +#5 transferring context: 216B done +#5 DONE 0.1s + +#6 [api internal] load build context +#6 DONE 0.0s + +#7 [api build 1/7] FROM docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a +#7 resolve docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a 0.0s done +#7 DONE 0.0s + +#8 [s3-bootstrap internal] load metadata for docker.io/library/alpine:3.22.2 +#8 ... + +#6 [api internal] load build context +#6 transferring context: 33.83kB 0.0s done +#6 DONE 0.1s + +#9 [api build 2/7] WORKDIR /src +#9 CACHED + +#10 [api build 3/7] COPY go.mod go.sum ./ +#10 DONE 0.1s + +#11 [api build 4/7] RUN go mod download +#11 ... + +#7 [s3-bootstrap build 1/7] FROM docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a +#7 resolve docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a 0.0s done +#7 DONE 0.0s + +#8 [s3 internal] load metadata for docker.io/library/alpine:3.22.2 +#8 DONE 1.3s + +#12 [s3 internal] load .dockerignore +#12 transferring context: 2B done +#12 DONE 0.0s + +#13 [s3 server 1/2] FROM docker.io/library/alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 +#13 resolve docker.io/library/alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 0.0s done +#13 DONE 0.0s + +#14 [s3 server-build 3/4] WORKDIR /src +#14 CACHED + +#15 [s3 server-build 2/4] RUN git clone --depth 1 --branch RELEASE.2025-04-22T22-12-26Z https://github.com/minio/minio.git /src +#15 CACHED + +#16 [s3 server-build 4/4] RUN go build -trimpath -o /out/minio . +#16 CACHED + +#17 [s3-bootstrap client-build 4/4] RUN go build -trimpath -o /out/mc . +#17 CACHED + +#18 [s3-bootstrap client-build 3/4] WORKDIR /src +#18 CACHED + +#19 [s3-bootstrap client-build 2/4] RUN git clone --depth 1 --branch RELEASE.2025-04-16T18-13-26Z https://github.com/minio/mc.git /src +#19 CACHED + +#20 [s3 server 2/2] COPY --from=server-build /out/minio /usr/local/bin/minio +#20 CACHED + +#21 [s3-bootstrap client 2/2] COPY --from=client-build /out/mc /usr/local/bin/mc +#21 CACHED + +#22 [s3] exporting to image +#22 exporting layers done +#22 exporting manifest sha256:8410921329eec1208439c3cd9dcb0b1ced8a8fb0a7348cea1d338f6f0a79c049 done +#22 exporting config sha256:350ec0fe229ab9c2b91b2c87cfb4e1e43d6dc3bbad5d7d9c0c7af1e5cf528867 done +#22 exporting attestation manifest sha256:d19086780e83f275095789825b65dc7752a86b0fc98210955e8ad83cc0bc0f4f 0.1s done +#22 exporting manifest list sha256:833b5fa7e3b09589c6fc8f80d24f097a0e637c5c0464c538fbe224d89c81aaaa 0.0s done +#22 naming to docker.io/library/messenger-lab4-s3:latest +#22 naming to docker.io/library/messenger-lab4-s3:latest 0.0s done +#22 unpacking to docker.io/library/messenger-lab4-s3:latest 0.0s done +#22 DONE 0.2s + +#23 [s3-bootstrap] exporting to image +#23 exporting layers done +#23 exporting manifest sha256:2103cc3b4036db5a1e5ee6580cef84645bc7465a14181fa62db60394f79fddfe done +#23 exporting config sha256:86a478d925e08843fe09c5d8f53e001d9daffdf9baab39f6bbfc48ce67db5b4d done +#23 exporting attestation manifest sha256:8f068af4ac5b233f51c015b83f72c2a98ae798610366a159be9f1191d6c7e4b2 0.1s done +#23 exporting manifest list sha256:7c9b4327dd63e7f07c6999132c3639a7c4ea4ca9aa57b467a9866f7b922ba782 0.0s done +#23 naming to docker.io/library/messenger-lab4-s3-bootstrap:latest done +#23 unpacking to docker.io/library/messenger-lab4-s3-bootstrap:latest 0.0s done +#23 DONE 0.2s + +#11 [api build 4/7] RUN go mod download +#11 ... + +#24 [s3] resolving provenance for metadata file +#24 DONE 0.0s + +#25 [s3-bootstrap] resolving provenance for metadata file +#25 DONE 0.0s + +#11 [api build 4/7] RUN go mod download +#11 DONE 6.4s + +#26 [api build 5/7] COPY cmd ./cmd +#26 DONE 0.1s + +#27 [api build 6/7] COPY internal ./internal +#27 DONE 0.1s + +#28 [api build 7/7] RUN go vet ./... && go test ./... && go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server +#28 34.26 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/cmd/server [no test files] +#28 34.26 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/cache [no test files] +#28 34.57 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/httpapi 0.298s +#28 34.57 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model [no test files] +#28 34.57 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/objectstore 0.015s +#28 35.17 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/password 0.900s +#28 35.17 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service 0.778s +#28 35.17 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store 0.011s +#28 35.17 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/upload 0.097s +#28 DONE 64.3s + +#29 [api stage-2 1/1] COPY --from=build /out/server /server +#29 DONE 0.1s + +#30 [api] exporting to image +#30 exporting layers +#30 exporting layers 0.9s done +#30 exporting manifest sha256:64fab80e083ae3ff9a4c38f344c1b7f6ef6fc9a18c2ca995574990859dcb85ee 0.0s done +#30 exporting config sha256:3d57c715a48afdfe0d1811e6ae2657e44301d4c0c3d43027aea8f1c3903f8b80 0.0s done +#30 exporting attestation manifest sha256:c6c8d445bf57b2027d62e24ea8b0c68252f5d98c9f594f7d2b1661ee8823072d 0.0s done +#30 exporting manifest list sha256:fb0e70af626d78c0bb95f7db932eba7085771ff072d71b233d48ac668be6f705 +#30 exporting manifest list sha256:fb0e70af626d78c0bb95f7db932eba7085771ff072d71b233d48ac668be6f705 0.0s done +#30 naming to docker.io/library/messenger-lab4-api:latest done +#30 unpacking to docker.io/library/messenger-lab4-api:latest 0.1s done +#30 DONE 1.2s + +#31 [api] resolving provenance for metadata file +#31 DONE 0.0s + messenger-lab4-s3 Built + messenger-lab4-s3-bootstrap Built + messenger-lab4-api Built + Container messenger-lab4-redis-1 Creating + Container messenger-lab4-postgres-1 Running + Container messenger-lab4-s3-1 Recreate + Container messenger-lab4-migrate-1 Creating + Container messenger-lab4-redis-1 Created + Container messenger-lab4-migrate-1 Created + Container messenger-lab4-s3-1 Recreated + Container messenger-lab4-s3-bootstrap-1 Recreate + Container messenger-lab4-s3-bootstrap-1 Recreated + Container messenger-lab4-api-2 Creating + Container messenger-lab4-api-1 Creating + Container messenger-lab4-api-2 Created + Container messenger-lab4-api-1 Created + Container messenger-lab4-lb-1 Creating + Container messenger-lab4-lb-1 Created + Container messenger-lab4-redis-1 Starting + Container messenger-lab4-postgres-1 Waiting + Container messenger-lab4-s3-1 Starting + Container messenger-lab4-postgres-1 Healthy + Container messenger-lab4-migrate-1 Starting + Container messenger-lab4-s3-1 Started + Container messenger-lab4-s3-bootstrap-1 Starting + Container messenger-lab4-redis-1 Started + Container messenger-lab4-migrate-1 Started + Container messenger-lab4-s3-bootstrap-1 Started + Container messenger-lab4-postgres-1 Waiting + Container messenger-lab4-migrate-1 Waiting + Container messenger-lab4-s3-bootstrap-1 Waiting + Container messenger-lab4-postgres-1 Healthy + Container messenger-lab4-migrate-1 Exited + Container messenger-lab4-s3-bootstrap-1 Exited + Container messenger-lab4-api-1 Starting + Container messenger-lab4-api-1 Started + Container messenger-lab4-api-2 Starting + Container messenger-lab4-api-2 Started + Container messenger-lab4-api-1 Waiting + Container messenger-lab4-api-2 Waiting + Container messenger-lab4-api-1 Healthy + Container messenger-lab4-api-2 Healthy + Container messenger-lab4-lb-1 Starting + Container messenger-lab4-lb-1 Started + Container messenger-lab4-migrate-1 Waiting + Container messenger-lab4-api-1 Waiting + Container messenger-lab4-api-2 Waiting + Container messenger-lab4-s3-1 Waiting + Container messenger-lab4-s3-bootstrap-1 Waiting + Container messenger-lab4-lb-1 Waiting + Container messenger-lab4-postgres-1 Waiting + Container messenger-lab4-redis-1 Waiting + Container messenger-lab4-s3-1 Healthy + Container messenger-lab4-redis-1 Healthy + Container messenger-lab4-s3-bootstrap-1 Exited + Container messenger-lab4-lb-1 Healthy + Container messenger-lab4-migrate-1 Exited + Container messenger-lab4-postgres-1 Healthy + Container messenger-lab4-api-1 Healthy + Container messenger-lab4-api-2 Healthy diff --git a/evidence/attachments-api-final.txt b/evidence/attachments-api-final.txt new file mode 100644 index 00000000..1e011959 --- /dev/null +++ b/evidence/attachments-api-final.txt @@ -0,0 +1,10 @@ +PASS: upload 201 ready; participant metadata/download; original and checksum match +PASS: unauthorized 401, outsider/unknown attachment 404, missing variant 404 +PASS: corrupt returns 415 +PASS: truncated returns 415 +PASS: spoofed MIME returns 415 +PASS: unsupported MIME returns 415 +PASS: excessive pixel dimensions return 413 +PASS: invalid filenames return 400 +PASS: declared byte length above 10 MiB returns 413 +PASS: attachment HTTP checks completed; message attachment_ids not tested yet diff --git a/evidence/attachments-api.txt b/evidence/attachments-api.txt new file mode 100644 index 00000000..1e011959 --- /dev/null +++ b/evidence/attachments-api.txt @@ -0,0 +1,10 @@ +PASS: upload 201 ready; participant metadata/download; original and checksum match +PASS: unauthorized 401, outsider/unknown attachment 404, missing variant 404 +PASS: corrupt returns 415 +PASS: truncated returns 415 +PASS: spoofed MIME returns 415 +PASS: unsupported MIME returns 415 +PASS: excessive pixel dimensions return 413 +PASS: invalid filenames return 400 +PASS: declared byte length above 10 MiB returns 413 +PASS: attachment HTTP checks completed; message attachment_ids not tested yet diff --git a/evidence/attachments-migration.txt b/evidence/attachments-migration.txt new file mode 100644 index 00000000..852e850a --- /dev/null +++ b/evidence/attachments-migration.txt @@ -0,0 +1,45 @@ + Container messenger-lab4-postgres-1 Running +BEGIN +SET +SET + pg_advisory_xact_lock +----------------------- + +(1 row) + +CREATE TABLE +CREATE TABLE +CREATE TABLE +CREATE TABLE +CREATE INDEX +CREATE TABLE +CREATE INDEX +CREATE TABLE +CREATE INDEX +INSERT 0 1 +Migration 000001 applied (commit follows). +CREATE TABLE +INSERT 0 1 +CREATE TABLE +CREATE INDEX +ALTER TABLE +CREATE TABLE +CREATE INDEX +INSERT 0 1 +Migration 000003 applied (commit follows). +COMMIT + Container messenger-lab4-postgres-1 Running +BEGIN +SET +SET + pg_advisory_xact_lock +----------------------- + +(1 row) + +CREATE TABLE +psql:/migrations/apply.psql:13: NOTICE: relation "schema_migrations" already exists, skipping +Migration 000001 already applied; skipping. +Migration 000002 already applied; skipping. +Migration 000003 already applied; skipping. +COMMIT diff --git a/evidence/attachments-schema.txt b/evidence/attachments-schema.txt new file mode 100644 index 00000000..fe69775e --- /dev/null +++ b/evidence/attachments-schema.txt @@ -0,0 +1,15 @@ +BEGIN +INSERT 0 1 +INSERT 0 2 +INSERT 0 2 +INSERT 0 1 +INSERT 0 2 +INSERT 0 1 +NOTICE: PASS: cross-chat attachment rejected +NOTICE: PASS: attachment position limited to 0..9 +NOTICE: PASS: oversized metadata rejected +NOTICE: PASS: filename path rejected +NOTICE: PASS: invalid checksum rejected +DO +ROLLBACK +PASS: attachment schema checks completed; fixtures rolled back. diff --git a/evidence/clean-attachment-messages.txt b/evidence/clean-attachment-messages.txt new file mode 100644 index 00000000..42cfce3b --- /dev/null +++ b/evidence/clean-attachment-messages.txt @@ -0,0 +1,7 @@ +PASS: attachment-only message, non-owner participant, list order, replay and changed-list 409 +PASS: text-only compatibility; omitted attachment_ids and [] replay identically +PASS: empty message, duplicates, >10 IDs, bad UUID/types/null/unknown fields return 400 +PASS: foreign-chat/unknown attachment and outsider return 404; rejected request does not consume key +PASS: exactly ten distinct attachments are accepted +PASS: 16 concurrent retries across two replicas return one message, history preserves attachment order +PASS: message attachment checks completed diff --git a/evidence/clean-attachments-api.txt b/evidence/clean-attachments-api.txt new file mode 100644 index 00000000..1e011959 --- /dev/null +++ b/evidence/clean-attachments-api.txt @@ -0,0 +1,10 @@ +PASS: upload 201 ready; participant metadata/download; original and checksum match +PASS: unauthorized 401, outsider/unknown attachment 404, missing variant 404 +PASS: corrupt returns 415 +PASS: truncated returns 415 +PASS: spoofed MIME returns 415 +PASS: unsupported MIME returns 415 +PASS: excessive pixel dimensions return 413 +PASS: invalid filenames return 400 +PASS: declared byte length above 10 MiB returns 413 +PASS: attachment HTTP checks completed; message attachment_ids not tested yet diff --git a/evidence/clean-bootstrap.txt b/evidence/clean-bootstrap.txt new file mode 100644 index 00000000..903d8d01 --- /dev/null +++ b/evidence/clean-bootstrap.txt @@ -0,0 +1,38 @@ +s3-bootstrap-1 | PASS: bucket configured private; API user policy applied. +migrate-1 | BEGIN +migrate-1 | SET +migrate-1 | SET +migrate-1 | pg_advisory_xact_lock +migrate-1 | ----------------------- +migrate-1 | +migrate-1 | (1 row) +migrate-1 | +migrate-1 | CREATE TABLE +migrate-1 | CREATE TABLE +migrate-1 | CREATE TABLE +migrate-1 | CREATE TABLE +migrate-1 | CREATE INDEX +migrate-1 | CREATE TABLE +migrate-1 | CREATE INDEX +migrate-1 | CREATE TABLE +migrate-1 | CREATE INDEX +migrate-1 | INSERT 0 1 +migrate-1 | Migration 000001 applied (commit follows). +migrate-1 | CREATE TABLE +migrate-1 | INSERT 0 1 +migrate-1 | CREATE TABLE +migrate-1 | CREATE INDEX +migrate-1 | ALTER TABLE +migrate-1 | CREATE TABLE +migrate-1 | CREATE INDEX +migrate-1 | INSERT 0 1 +migrate-1 | Migration 000003 applied (commit follows). +migrate-1 | ALTER TABLE +migrate-1 | ALTER TABLE +migrate-1 | INSERT 0 1 +migrate-1 | Migration 000004 applied (commit follows). +migrate-1 | CREATE TABLE +migrate-1 | CREATE INDEX +migrate-1 | INSERT 0 1 +migrate-1 | Migration 000005 applied (commit follows). +migrate-1 | COMMIT diff --git a/evidence/clean-build.txt b/evidence/clean-build.txt new file mode 100644 index 00000000..71e70e1f --- /dev/null +++ b/evidence/clean-build.txt @@ -0,0 +1,218 @@ +#1 [internal] load local bake definitions +#1 reading from stdin 1.90kB 0.0s done +#1 DONE 0.0s + +#2 [s3-bootstrap internal] load build definition from Dockerfile +#2 transferring dockerfile: 710B 0.0s done +#2 DONE 0.0s + +#3 [api internal] load build definition from Dockerfile +#3 transferring dockerfile: 871B 0.0s done +#3 DONE 0.1s + +#4 [s3-bootstrap internal] load metadata for docker.io/library/golang:1.26.3 +#4 DONE 0.1s + +#5 [api internal] load .dockerignore +#5 transferring context: 216B 0.0s done +#5 DONE 0.1s + +#6 [api internal] load build context +#6 DONE 0.0s + +#7 [api build 1/7] FROM docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a +#7 resolve docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a 0.0s done +#7 DONE 0.0s + +#6 [api internal] load build context +#6 transferring context: 144.75kB 0.0s done +#6 DONE 0.1s + +#8 [api build 2/7] WORKDIR /src +#8 CACHED + +#9 [api build 3/7] COPY go.mod go.sum ./ +#9 DONE 0.1s + +#10 [s3-bootstrap internal] load metadata for docker.io/library/alpine:3.22.2 +#10 DONE 1.3s + +#7 [s3-bootstrap build 1/7] FROM docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a +#7 resolve docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a 0.0s done +#7 DONE 0.0s + +#11 [s3-bootstrap internal] load .dockerignore +#11 transferring context: 2B done +#11 DONE 0.0s + +#12 [s3-bootstrap server 1/2] FROM docker.io/library/alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 +#12 resolve docker.io/library/alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 0.0s done +#12 DONE 0.0s + +#13 [s3 server-build 4/4] RUN go build -trimpath -o /out/minio . +#13 CACHED + +#14 [s3 server-build 3/4] WORKDIR /src +#14 CACHED + +#15 [s3 server-build 2/4] RUN git clone --depth 1 --branch RELEASE.2025-04-22T22-12-26Z https://github.com/minio/minio.git /src +#15 CACHED + +#16 [s3 server 2/2] COPY --from=server-build /out/minio /usr/local/bin/minio +#16 CACHED + +#17 [s3-bootstrap client-build 2/4] RUN git clone --depth 1 --branch RELEASE.2025-04-16T18-13-26Z https://github.com/minio/mc.git /src +#17 CACHED + +#18 [s3-bootstrap client-build 3/4] WORKDIR /src +#18 CACHED + +#19 [s3-bootstrap client-build 4/4] RUN go build -trimpath -o /out/mc . +#19 CACHED + +#20 [s3-bootstrap client 2/2] COPY --from=client-build /out/mc /usr/local/bin/mc +#20 CACHED + +#21 [s3-bootstrap] exporting to image +#21 exporting layers done +#21 exporting manifest sha256:fed62e4d3740a30c8e5fe7240a72b84e201bef30d0e5b044b17e5a75fffd3d0e 0.0s done +#21 exporting config sha256:97c8733577e743931782ee94435d027b4789c677f5511311bbb71021c8822702 +#21 exporting config sha256:97c8733577e743931782ee94435d027b4789c677f5511311bbb71021c8822702 0.0s done +#21 exporting attestation manifest sha256:a74f05c6fb3ac293f19d679176fd568d86ec300127b9347570613aab4fe53197 0.1s done +#21 exporting manifest list sha256:6cb567ec5a8d7104a637ff8749268606605acf2f9bb23a1a26e34d2bf4485c9d +#21 exporting manifest list sha256:6cb567ec5a8d7104a637ff8749268606605acf2f9bb23a1a26e34d2bf4485c9d 0.0s done +#21 naming to docker.io/library/messenger-lab4-clean-check-s3-bootstrap:latest 0.0s done +#21 unpacking to docker.io/library/messenger-lab4-clean-check-s3-bootstrap:latest 0.0s done +#21 DONE 0.3s + +#22 [s3] exporting to image +#22 exporting layers done +#22 exporting manifest sha256:b1198981eeff6339fd7123ceca6c19dc73f2827fa90aae1f9cb8de780ec2836d 0.0s done +#22 exporting config sha256:0b61384e4363ef4667e244183f9ef5413a570d0aa94be8f1142102764438b206 0.0s done +#22 exporting attestation manifest sha256:1f78deb5604f07aeec306eb35c36f7969ae56b386ae142b20e76e401677f8f91 0.1s done +#22 exporting manifest list sha256:a326b0526f5879239765d11aa086036678ad9217ccfc9c6c74cbf8807a8376e0 0.0s done +#22 naming to docker.io/library/messenger-lab4-clean-check-s3:latest done +#22 unpacking to docker.io/library/messenger-lab4-clean-check-s3:latest done +#22 DONE 0.3s + +#23 [api build 4/7] RUN go mod download +#23 ... + +#24 [s3-bootstrap] resolving provenance for metadata file +#24 DONE 0.0s + +#25 [s3] resolving provenance for metadata file +#25 DONE 0.0s + +#23 [api build 4/7] RUN go mod download +#23 DONE 8.3s + +#26 [api build 5/7] COPY cmd ./cmd +#26 DONE 0.1s + +#27 [api build 6/7] COPY internal ./internal +#27 DONE 0.1s + +#28 [api build 7/7] RUN go vet ./... && go test ./... && go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server && go build -trimpath -ldflags="-s -w" -o /out/resourceprobe ./cmd/resourceprobe +#28 31.43 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/cmd/resourceprobe [no test files] +#28 33.44 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/cmd/server [no test files] +#28 33.44 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/cache [no test files] +#28 33.72 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/httpapi 0.273s +#28 33.72 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model [no test files] +#28 33.72 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/objectstore 0.012s +#28 34.31 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/password 0.866s +#28 34.31 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service 0.741s +#28 34.31 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store 0.009s +#28 34.31 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/upload 0.088s +#28 DONE 62.5s + +#29 [api stage-2 1/2] COPY --from=build /out/server /server +#29 DONE 0.1s + +#30 [api stage-2 2/2] COPY --from=build /out/resourceprobe /resourceprobe +#30 DONE 0.1s + +#31 [api] exporting to image +#31 exporting layers +#31 exporting layers 0.9s done +#31 exporting manifest sha256:431efd4ed9f69a910f93101ba3a0d46e6b18b7d32b812666d829c27ca10d5435 0.0s done +#31 exporting config sha256:5b9f9c160b84b5fd1af1337f17c5350455e490bca87755a7b406d356e45ba6ba 0.0s done +#31 exporting attestation manifest sha256:92e0da465901aeead14d3b27fcfd2c1d138bba2c7c47f76325ea6220dc488d3f 0.0s done +#31 exporting manifest list sha256:314e951f6284741b1df2d79484039993208ad28c0eb378fc65ac4951f9359309 +#31 exporting manifest list sha256:314e951f6284741b1df2d79484039993208ad28c0eb378fc65ac4951f9359309 0.0s done +#31 naming to docker.io/library/messenger-lab4-clean-check-api:latest done +#31 unpacking to docker.io/library/messenger-lab4-clean-check-api:latest +#31 unpacking to docker.io/library/messenger-lab4-clean-check-api:latest 0.2s done +#31 DONE 1.3s + +#32 [api] resolving provenance for metadata file +#32 DONE 0.0s + messenger-lab4-clean-check-s3 Built + messenger-lab4-clean-check-s3-bootstrap Built + messenger-lab4-clean-check-api Built + Network messenger-lab4-clean-check_default Creating + Network messenger-lab4-clean-check_default Created + Volume messenger-lab4-clean-check_s3_lab4_data_v1 Creating + Volume messenger-lab4-clean-check_s3_lab4_data_v1 Created + Volume messenger-lab4-clean-check_postgres_lab4_data_v1 Creating + Volume messenger-lab4-clean-check_postgres_lab4_data_v1 Created + Container messenger-lab4-clean-check-postgres-1 Creating + Container messenger-lab4-clean-check-s3-1 Creating + Container messenger-lab4-clean-check-redis-1 Creating + Container messenger-lab4-clean-check-redis-1 Created + Container messenger-lab4-clean-check-s3-1 Created + Container messenger-lab4-clean-check-s3-bootstrap-1 Creating + Container messenger-lab4-clean-check-postgres-1 Created + Container messenger-lab4-clean-check-migrate-1 Creating + Container messenger-lab4-clean-check-s3-bootstrap-1 Created + Container messenger-lab4-clean-check-migrate-1 Created + Container messenger-lab4-clean-check-api-2 Creating + Container messenger-lab4-clean-check-api-1 Creating + Container messenger-lab4-clean-check-api-2 Created + Container messenger-lab4-clean-check-api-1 Created + Container messenger-lab4-clean-check-lb-1 Creating + Container messenger-lab4-clean-check-lb-1 Created + Container messenger-lab4-clean-check-s3-1 Starting + Container messenger-lab4-clean-check-postgres-1 Starting + Container messenger-lab4-clean-check-redis-1 Starting + Container messenger-lab4-clean-check-postgres-1 Started + Container messenger-lab4-clean-check-postgres-1 Waiting + Container messenger-lab4-clean-check-s3-1 Started + Container messenger-lab4-clean-check-s3-bootstrap-1 Starting + Container messenger-lab4-clean-check-redis-1 Started + Container messenger-lab4-clean-check-s3-bootstrap-1 Started + Container messenger-lab4-clean-check-postgres-1 Healthy + Container messenger-lab4-clean-check-migrate-1 Starting + Container messenger-lab4-clean-check-migrate-1 Started + Container messenger-lab4-clean-check-s3-bootstrap-1 Waiting + Container messenger-lab4-clean-check-postgres-1 Waiting + Container messenger-lab4-clean-check-migrate-1 Waiting + Container messenger-lab4-clean-check-postgres-1 Healthy + Container messenger-lab4-clean-check-migrate-1 Exited + Container messenger-lab4-clean-check-s3-bootstrap-1 Exited + Container messenger-lab4-clean-check-api-1 Starting + Container messenger-lab4-clean-check-api-1 Started + Container messenger-lab4-clean-check-api-2 Starting + Container messenger-lab4-clean-check-api-2 Started + Container messenger-lab4-clean-check-api-1 Waiting + Container messenger-lab4-clean-check-api-2 Waiting + Container messenger-lab4-clean-check-api-1 Healthy + Container messenger-lab4-clean-check-api-2 Healthy + Container messenger-lab4-clean-check-lb-1 Starting + Container messenger-lab4-clean-check-lb-1 Started + Container messenger-lab4-clean-check-s3-bootstrap-1 Waiting + Container messenger-lab4-clean-check-lb-1 Waiting + Container messenger-lab4-clean-check-postgres-1 Waiting + Container messenger-lab4-clean-check-redis-1 Waiting + Container messenger-lab4-clean-check-migrate-1 Waiting + Container messenger-lab4-clean-check-api-1 Waiting + Container messenger-lab4-clean-check-api-2 Waiting + Container messenger-lab4-clean-check-s3-1 Waiting + Container messenger-lab4-clean-check-redis-1 Healthy + Container messenger-lab4-clean-check-s3-1 Healthy + Container messenger-lab4-clean-check-postgres-1 Healthy + Container messenger-lab4-clean-check-lb-1 Healthy + Container messenger-lab4-clean-check-s3-bootstrap-1 Exited + Container messenger-lab4-clean-check-migrate-1 Exited + Container messenger-lab4-clean-check-api-1 Healthy + Container messenger-lab4-clean-check-api-2 Healthy diff --git a/evidence/clean-cleanup.txt b/evidence/clean-cleanup.txt new file mode 100644 index 00000000..21b4b0ed --- /dev/null +++ b/evidence/clean-cleanup.txt @@ -0,0 +1,8 @@ +PASS: real S3 PUT followed by injected INSERT failure returns 503; durable intent remains +PASS: cleanup preserves fresh failed upload during 10-minute grace period +PASS: unavailable cleanup endpoint reports error and preserves intent for retry +PASS: aged intent detected; dry-run preserves object; cleanup removes it and retains tombstone +PASS: late PUT removed on retry; repeat DELETE of absent object succeeds +PASS: published attachment referenced by message remains byte-identical +PASS: disconnected partial HTTP upload never reaches S3 intent stage +PASS: upload compensation checks completed; grace aging was explicit SQL simulation diff --git a/evidence/clean-containers.jsonl b/evidence/clean-containers.jsonl new file mode 100644 index 00000000..9ab7b4ed --- /dev/null +++ b/evidence/clean-containers.jsonl @@ -0,0 +1,5 @@ +{"stage": "before-failover", "containers": [{"name": "/messenger-lab4-clean-check-api-1", "id": "f185a8e6edfa4d47b465f55af27f78195369de35401bfbf1b81ce3ad767b1f98", "service": "api", "status": "running", "started_at": "2026-11-06T14:43:40.952183442Z", "pid": 287180, "volumes": []}, {"name": "/messenger-lab4-clean-check-api-2", "id": "c1f6e4ae125748a6388301aae36659298951cb557f5567a4c3d1d9a5071d1e70", "service": "api", "status": "running", "started_at": "2026-11-06T14:43:41.295547853Z", "pid": 287241, "volumes": []}, {"name": "/messenger-lab4-clean-check-lb-1", "id": "f7f376f9de1c3803199d0e5622f0b6cdd922f7233cc944187212a2c12103f393", "service": "lb", "status": "running", "started_at": "2026-11-06T14:43:47.159265539Z", "pid": 287544, "volumes": []}, {"name": "/messenger-lab4-clean-check-migrate-1", "id": "beab6364a02c2cacb0157ae3069f1e9ca56dd4d19b744622170403ec9ca45627", "service": "migrate", "status": "exited", "started_at": "2026-11-06T14:43:35.761096723Z", "pid": 0, "volumes": [{"name": "9f920f7bacb4191d9360166f99b838316246ef0d8f3ca0dd75d7eabdcdfe4810", "destination": "/var/lib/postgresql/data"}]}, {"name": "/messenger-lab4-clean-check-postgres-1", "id": "e81aca19327272869c335cebd2539bde579d9818819f6ca00c37b169c4685a10", "service": "postgres", "status": "running", "started_at": "2026-11-06T14:43:30.012783232Z", "pid": 286444, "volumes": [{"name": "messenger-lab4-clean-check_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"name": "/messenger-lab4-clean-check-redis-1", "id": "ea11db1b52092ee7efbd98251a804ceb1b1951e779cc678881625834c19f3ac3", "service": "redis", "status": "running", "started_at": "2026-11-06T14:43:30.01494547Z", "pid": 286445, "volumes": []}, {"name": "/messenger-lab4-clean-check-s3-1", "id": "a34da911af8a2b110fe0d8baaed00d316c87e5cc81bbe575a99824002967a851", "service": "s3", "status": "running", "started_at": "2026-11-06T14:46:28.89852597Z", "pid": 294851, "volumes": [{"name": "messenger-lab4-clean-check_s3_lab4_data_v1", "destination": "/data"}]}, {"name": "/messenger-lab4-clean-check-s3-bootstrap-1", "id": "35475d85960c16d720e4b4dd68a7166a8bab427792f2883530ca9c7fb0ba3aaa", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T14:43:30.498378692Z", "pid": 0, "volumes": []}]} +{"stage": "after-failover", "containers": [{"name": "/messenger-lab4-clean-check-api-1", "id": "f185a8e6edfa4d47b465f55af27f78195369de35401bfbf1b81ce3ad767b1f98", "service": "api", "status": "exited", "started_at": "2026-11-06T14:43:40.952183442Z", "pid": 0, "volumes": []}, {"name": "/messenger-lab4-clean-check-api-2", "id": "c1f6e4ae125748a6388301aae36659298951cb557f5567a4c3d1d9a5071d1e70", "service": "api", "status": "running", "started_at": "2026-11-06T14:43:41.295547853Z", "pid": 287241, "volumes": []}, {"name": "/messenger-lab4-clean-check-lb-1", "id": "f7f376f9de1c3803199d0e5622f0b6cdd922f7233cc944187212a2c12103f393", "service": "lb", "status": "running", "started_at": "2026-11-06T14:43:47.159265539Z", "pid": 287544, "volumes": []}, {"name": "/messenger-lab4-clean-check-migrate-1", "id": "beab6364a02c2cacb0157ae3069f1e9ca56dd4d19b744622170403ec9ca45627", "service": "migrate", "status": "exited", "started_at": "2026-11-06T14:43:35.761096723Z", "pid": 0, "volumes": [{"name": "9f920f7bacb4191d9360166f99b838316246ef0d8f3ca0dd75d7eabdcdfe4810", "destination": "/var/lib/postgresql/data"}]}, {"name": "/messenger-lab4-clean-check-postgres-1", "id": "e81aca19327272869c335cebd2539bde579d9818819f6ca00c37b169c4685a10", "service": "postgres", "status": "running", "started_at": "2026-11-06T14:43:30.012783232Z", "pid": 286444, "volumes": [{"name": "messenger-lab4-clean-check_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"name": "/messenger-lab4-clean-check-redis-1", "id": "ea11db1b52092ee7efbd98251a804ceb1b1951e779cc678881625834c19f3ac3", "service": "redis", "status": "running", "started_at": "2026-11-06T14:43:30.01494547Z", "pid": 286445, "volumes": []}, {"name": "/messenger-lab4-clean-check-s3-1", "id": "a34da911af8a2b110fe0d8baaed00d316c87e5cc81bbe575a99824002967a851", "service": "s3", "status": "running", "started_at": "2026-11-06T14:46:28.89852597Z", "pid": 294851, "volumes": [{"name": "messenger-lab4-clean-check_s3_lab4_data_v1", "destination": "/data"}]}, {"name": "/messenger-lab4-clean-check-s3-bootstrap-1", "id": "35475d85960c16d720e4b4dd68a7166a8bab427792f2883530ca9c7fb0ba3aaa", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T14:43:30.498378692Z", "pid": 0, "volumes": []}]} +{"stage": "before-persistence", "containers": [{"name": "/messenger-lab4-clean-check-api-1", "id": "f185a8e6edfa4d47b465f55af27f78195369de35401bfbf1b81ce3ad767b1f98", "service": "api", "status": "running", "started_at": "2026-11-06T14:48:48.218998987Z", "pid": 302020, "volumes": []}, {"name": "/messenger-lab4-clean-check-api-2", "id": "c1f6e4ae125748a6388301aae36659298951cb557f5567a4c3d1d9a5071d1e70", "service": "api", "status": "running", "started_at": "2026-11-06T14:43:41.295547853Z", "pid": 287241, "volumes": []}, {"name": "/messenger-lab4-clean-check-lb-1", "id": "f7f376f9de1c3803199d0e5622f0b6cdd922f7233cc944187212a2c12103f393", "service": "lb", "status": "running", "started_at": "2026-11-06T14:43:47.159265539Z", "pid": 287544, "volumes": []}, {"name": "/messenger-lab4-clean-check-migrate-1", "id": "beab6364a02c2cacb0157ae3069f1e9ca56dd4d19b744622170403ec9ca45627", "service": "migrate", "status": "exited", "started_at": "2026-11-06T14:43:35.761096723Z", "pid": 0, "volumes": [{"name": "9f920f7bacb4191d9360166f99b838316246ef0d8f3ca0dd75d7eabdcdfe4810", "destination": "/var/lib/postgresql/data"}]}, {"name": "/messenger-lab4-clean-check-postgres-1", "id": "e81aca19327272869c335cebd2539bde579d9818819f6ca00c37b169c4685a10", "service": "postgres", "status": "running", "started_at": "2026-11-06T14:43:30.012783232Z", "pid": 286444, "volumes": [{"name": "messenger-lab4-clean-check_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"name": "/messenger-lab4-clean-check-redis-1", "id": "ea11db1b52092ee7efbd98251a804ceb1b1951e779cc678881625834c19f3ac3", "service": "redis", "status": "running", "started_at": "2026-11-06T14:43:30.01494547Z", "pid": 286445, "volumes": []}, {"name": "/messenger-lab4-clean-check-s3-1", "id": "a34da911af8a2b110fe0d8baaed00d316c87e5cc81bbe575a99824002967a851", "service": "s3", "status": "running", "started_at": "2026-11-06T14:46:28.89852597Z", "pid": 294851, "volumes": [{"name": "messenger-lab4-clean-check_s3_lab4_data_v1", "destination": "/data"}]}, {"name": "/messenger-lab4-clean-check-s3-bootstrap-1", "id": "35475d85960c16d720e4b4dd68a7166a8bab427792f2883530ca9c7fb0ba3aaa", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T14:43:30.498378692Z", "pid": 0, "volumes": []}]} +{"stage": "after-persistence", "containers": [{"name": "/messenger-lab4-clean-check-api-1", "id": "f185a8e6edfa4d47b465f55af27f78195369de35401bfbf1b81ce3ad767b1f98", "service": "api", "status": "running", "started_at": "2026-11-06T14:49:04.105915997Z", "pid": 303224, "volumes": []}, {"name": "/messenger-lab4-clean-check-api-2", "id": "c1f6e4ae125748a6388301aae36659298951cb557f5567a4c3d1d9a5071d1e70", "service": "api", "status": "running", "started_at": "2026-11-06T14:49:04.480180664Z", "pid": 303299, "volumes": []}, {"name": "/messenger-lab4-clean-check-lb-1", "id": "f7f376f9de1c3803199d0e5622f0b6cdd922f7233cc944187212a2c12103f393", "service": "lb", "status": "running", "started_at": "2026-11-06T14:49:10.3978425Z", "pid": 303587, "volumes": []}, {"name": "/messenger-lab4-clean-check-migrate-1", "id": "beab6364a02c2cacb0157ae3069f1e9ca56dd4d19b744622170403ec9ca45627", "service": "migrate", "status": "exited", "started_at": "2026-11-06T14:49:01.526426905Z", "pid": 0, "volumes": [{"name": "9f920f7bacb4191d9360166f99b838316246ef0d8f3ca0dd75d7eabdcdfe4810", "destination": "/var/lib/postgresql/data"}]}, {"name": "/messenger-lab4-clean-check-postgres-1", "id": "e81aca19327272869c335cebd2539bde579d9818819f6ca00c37b169c4685a10", "service": "postgres", "status": "running", "started_at": "2026-11-06T14:48:55.334211957Z", "pid": 302596, "volumes": [{"name": "messenger-lab4-clean-check_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"name": "/messenger-lab4-clean-check-redis-1", "id": "ea11db1b52092ee7efbd98251a804ceb1b1951e779cc678881625834c19f3ac3", "service": "redis", "status": "running", "started_at": "2026-11-06T14:48:55.306182952Z", "pid": 302595, "volumes": []}, {"name": "/messenger-lab4-clean-check-s3-1", "id": "a34da911af8a2b110fe0d8baaed00d316c87e5cc81bbe575a99824002967a851", "service": "s3", "status": "running", "started_at": "2026-11-06T14:48:55.337424836Z", "pid": 302597, "volumes": [{"name": "messenger-lab4-clean-check_s3_lab4_data_v1", "destination": "/data"}]}, {"name": "/messenger-lab4-clean-check-s3-bootstrap-1", "id": "87cf07bfb87540767efa3259f16dd5e7e00493ec81d3cf741d11d31def61fd2e", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T14:48:55.695004829Z", "pid": 0, "volumes": []}]} +{"stage": "final-restored", "containers": [{"name": "/messenger-lab4-clean-check-api-1", "id": "f185a8e6edfa4d47b465f55af27f78195369de35401bfbf1b81ce3ad767b1f98", "service": "api", "status": "running", "started_at": "2026-11-06T14:49:04.105915997Z", "pid": 303224, "volumes": []}, {"name": "/messenger-lab4-clean-check-api-2", "id": "c1f6e4ae125748a6388301aae36659298951cb557f5567a4c3d1d9a5071d1e70", "service": "api", "status": "running", "started_at": "2026-11-06T14:49:04.480180664Z", "pid": 303299, "volumes": []}, {"name": "/messenger-lab4-clean-check-lb-1", "id": "f7f376f9de1c3803199d0e5622f0b6cdd922f7233cc944187212a2c12103f393", "service": "lb", "status": "running", "started_at": "2026-11-06T14:49:10.3978425Z", "pid": 303587, "volumes": []}, {"name": "/messenger-lab4-clean-check-migrate-1", "id": "beab6364a02c2cacb0157ae3069f1e9ca56dd4d19b744622170403ec9ca45627", "service": "migrate", "status": "exited", "started_at": "2026-11-06T14:49:01.526426905Z", "pid": 0, "volumes": [{"name": "9f920f7bacb4191d9360166f99b838316246ef0d8f3ca0dd75d7eabdcdfe4810", "destination": "/var/lib/postgresql/data"}]}, {"name": "/messenger-lab4-clean-check-postgres-1", "id": "e81aca19327272869c335cebd2539bde579d9818819f6ca00c37b169c4685a10", "service": "postgres", "status": "running", "started_at": "2026-11-06T14:48:55.334211957Z", "pid": 302596, "volumes": [{"name": "messenger-lab4-clean-check_postgres_lab4_data_v1", "destination": "/var/lib/postgresql/data"}]}, {"name": "/messenger-lab4-clean-check-redis-1", "id": "ea11db1b52092ee7efbd98251a804ceb1b1951e779cc678881625834c19f3ac3", "service": "redis", "status": "running", "started_at": "2026-11-06T14:48:55.306182952Z", "pid": 302595, "volumes": []}, {"name": "/messenger-lab4-clean-check-s3-1", "id": "a34da911af8a2b110fe0d8baaed00d316c87e5cc81bbe575a99824002967a851", "service": "s3", "status": "running", "started_at": "2026-11-06T14:48:55.337424836Z", "pid": 302597, "volumes": [{"name": "messenger-lab4-clean-check_s3_lab4_data_v1", "destination": "/data"}]}, {"name": "/messenger-lab4-clean-check-s3-bootstrap-1", "id": "87cf07bfb87540767efa3259f16dd5e7e00493ec81d3cf741d11d31def61fd2e", "service": "s3-bootstrap", "status": "exited", "started_at": "2026-11-06T14:48:55.695004829Z", "pid": 0, "volumes": []}]} diff --git a/evidence/clean-contract.txt b/evidence/clean-contract.txt new file mode 100644 index 00000000..b62af9c5 --- /dev/null +++ b/evidence/clean-contract.txt @@ -0,0 +1 @@ +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. diff --git a/evidence/clean-failover.txt b/evidence/clean-failover.txt new file mode 100644 index 00000000..85d7c592 --- /dev/null +++ b/evidence/clean-failover.txt @@ -0,0 +1,3 @@ +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 120, "failed_attempts": 2, "first_recovery_seconds": 2.234} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/clean-final-check.txt b/evidence/clean-final-check.txt new file mode 100644 index 00000000..df2368bd --- /dev/null +++ b/evidence/clean-final-check.txt @@ -0,0 +1 @@ +PASS: main stack remains ready on 8084; clean project stopped with volumes retained. diff --git a/evidence/clean-final-smoke.txt b/evidence/clean-final-smoke.txt new file mode 100644 index 00000000..c554fd6f --- /dev/null +++ b/evidence/clean-final-smoke.txt @@ -0,0 +1 @@ +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/clean-final-source.txt b/evidence/clean-final-source.txt new file mode 100644 index 00000000..7df6111a --- /dev/null +++ b/evidence/clean-final-source.txt @@ -0,0 +1,4 @@ +Verified implementation commit: +879463c947ae3480a82a7dac1873ae7782b0e277 +Final git status --porcelain (empty means tracked/untracked tree is clean; ignored local secrets excluded): +PASS: source files unchanged; runtime config/secrets are ignored by Git. diff --git a/evidence/clean-final-stack.txt b/evidence/clean-final-stack.txt new file mode 100644 index 00000000..9703081e --- /dev/null +++ b/evidence/clean-final-stack.txt @@ -0,0 +1,7 @@ +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +messenger-lab4-clean-check-api-1 messenger-lab4-clean-check-api "/server" api 7 minutes ago Up 2 minutes (healthy) 8080/tcp +messenger-lab4-clean-check-api-2 messenger-lab4-clean-check-api "/server" api 7 minutes ago Up 2 minutes (healthy) 8080/tcp +messenger-lab4-clean-check-lb-1 haproxy:3.2.4-alpine "docker-entrypoint.s…" lb 7 minutes ago Up About a minute 127.0.0.1:8094->8080/tcp +messenger-lab4-clean-check-postgres-1 postgres:17.9-bookworm "docker-entrypoint.s…" postgres 7 minutes ago Up 2 minutes (healthy) 5432/tcp +messenger-lab4-clean-check-redis-1 redis:7.4.11-alpine "docker-entrypoint.s…" redis 7 minutes ago Up 2 minutes (healthy) 6379/tcp +messenger-lab4-clean-check-s3-1 messenger-lab4-clean-check-s3 "minio server /data …" s3 4 minutes ago Up 2 minutes diff --git a/evidence/clean-init.txt b/evidence/clean-init.txt new file mode 100644 index 00000000..29a82d2d --- /dev/null +++ b/evidence/clean-init.txt @@ -0,0 +1,6 @@ +Local .env created (non-secret session TTL). +Local secret created: postgres_password (value not printed). +Local secret created: s3_root_user (value not printed). +Local secret created: s3_root_password (value not printed). +Local secret created: s3_access_key_id (value not printed). +Local secret created: s3_secret_access_key (value not printed). diff --git a/evidence/clean-initial-db.txt b/evidence/clean-initial-db.txt new file mode 100644 index 00000000..57c94f80 --- /dev/null +++ b/evidence/clean-initial-db.txt @@ -0,0 +1,14 @@ + version +--------- + 1 + 2 + 3 + 4 + 5 +(5 rows) + + users | messages | attachments +-------+----------+------------- + 0 | 0 | 0 +(1 row) + diff --git a/evidence/clean-isolation.txt b/evidence/clean-isolation.txt new file mode 100644 index 00000000..882bd017 --- /dev/null +++ b/evidence/clean-isolation.txt @@ -0,0 +1,8 @@ +Compose project: messenger-lab4-clean-check +LB port: 8094 +Credentials compared without printing values: +postgres_password is different from main stack: True +s3_root_user is different from main stack: True +s3_root_password is different from main stack: True +s3_access_key_id is different from main stack: True +s3_secret_access_key is different from main stack: True diff --git a/evidence/clean-original-stack-after.txt b/evidence/clean-original-stack-after.txt new file mode 100644 index 00000000..941d59c5 --- /dev/null +++ b/evidence/clean-original-stack-after.txt @@ -0,0 +1,7 @@ +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +messenger-lab4-api-3 messenger-lab4-api "/server" api 18 minutes ago Up 18 minutes (healthy) 8080/tcp +messenger-lab4-api-4 messenger-lab4-api "/server" api 18 minutes ago Up 18 minutes (healthy) 8080/tcp +messenger-lab4-lb-1 haproxy:3.2.4-alpine "docker-entrypoint.s…" lb 2 hours ago Up 25 minutes 127.0.0.1:8084->8080/tcp +messenger-lab4-postgres-1 postgres:17.9-bookworm "docker-entrypoint.s…" postgres 2 hours ago Up 25 minutes (healthy) 5432/tcp +messenger-lab4-redis-1 redis:7.4.11-alpine "docker-entrypoint.s…" redis 2 hours ago Up 25 minutes (healthy) 6379/tcp +messenger-lab4-s3-1 messenger-lab4-s3 "minio server /data …" s3 19 minutes ago Up 18 minutes diff --git a/evidence/clean-original-stack-before.txt b/evidence/clean-original-stack-before.txt new file mode 100644 index 00000000..c8b1a3ef --- /dev/null +++ b/evidence/clean-original-stack-before.txt @@ -0,0 +1,7 @@ +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +messenger-lab4-api-3 messenger-lab4-api "/server" api 7 minutes ago Up 7 minutes (healthy) 8080/tcp +messenger-lab4-api-4 messenger-lab4-api "/server" api 7 minutes ago Up 7 minutes (healthy) 8080/tcp +messenger-lab4-lb-1 haproxy:3.2.4-alpine "docker-entrypoint.s…" lb 2 hours ago Up 14 minutes 127.0.0.1:8084->8080/tcp +messenger-lab4-postgres-1 postgres:17.9-bookworm "docker-entrypoint.s…" postgres 2 hours ago Up 14 minutes (healthy) 5432/tcp +messenger-lab4-redis-1 redis:7.4.11-alpine "docker-entrypoint.s…" redis 2 hours ago Up 14 minutes (healthy) 6379/tcp +messenger-lab4-s3-1 messenger-lab4-s3 "minio server /data …" s3 8 minutes ago Up 7 minutes diff --git a/evidence/clean-persistence.txt b/evidence/clean-persistence.txt new file mode 100644 index 00000000..749fd29b --- /dev/null +++ b/evidence/clean-persistence.txt @@ -0,0 +1,2 @@ +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/clean-resilience-summary.txt b/evidence/clean-resilience-summary.txt new file mode 100644 index 00000000..705b1092 --- /dev/null +++ b/evidence/clean-resilience-summary.txt @@ -0,0 +1,9 @@ +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 120, "failed_attempts": 2, "first_recovery_seconds": 2.234} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. + +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. + +PASS: API/LB/PostgreSQL/Redis/S3 StartedAt changed; same volumes +PASS: clean clone resilience; git status clean diff --git a/evidence/clean-s3.txt b/evidence/clean-s3.txt new file mode 100644 index 00000000..f6480459 --- /dev/null +++ b/evidence/clean-s3.txt @@ -0,0 +1,120 @@ +Existing .env preserved. +Existing secret preserved: postgres_password +Existing secret preserved: s3_root_user +Existing secret preserved: s3_root_password +Existing secret preserved: s3_access_key_id +Existing secret preserved: s3_secret_access_key +COMMAND: docker compose --env-file .env build s3 s3-bootstrap +#1 [internal] load local bake definitions +#1 reading from stdin 1.34kB 0.0s done +#1 DONE 0.0s + +#2 [s3 internal] load build definition from Dockerfile +#2 DONE 0.0s + +#2 [s3-bootstrap internal] load build definition from Dockerfile +#2 transferring dockerfile: 710B done +#2 DONE 0.0s + +#3 [s3 internal] load metadata for docker.io/library/alpine:3.22.2 +#3 ... + +#4 [s3 internal] load metadata for docker.io/library/golang:1.26.3 +#4 DONE 0.2s + +#3 [s3 internal] load metadata for docker.io/library/alpine:3.22.2 +#3 DONE 0.7s + +#5 [s3-bootstrap internal] load .dockerignore +#5 transferring context: 2B done +#5 DONE 0.0s + +#6 [s3-bootstrap server 1/2] FROM docker.io/library/alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 +#6 resolve docker.io/library/alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 0.0s done +#6 DONE 0.0s + +#7 [s3 server-build 1/4] FROM docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a +#7 resolve docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a 0.0s done +#7 DONE 0.1s + +#8 [s3 server-build 2/4] RUN git clone --depth 1 --branch RELEASE.2025-04-22T22-12-26Z https://github.com/minio/minio.git /src +#8 CACHED + +#9 [s3 server-build 3/4] WORKDIR /src +#9 CACHED + +#10 [s3 server-build 4/4] RUN go build -trimpath -o /out/minio . +#10 CACHED + +#11 [s3 server 2/2] COPY --from=server-build /out/minio /usr/local/bin/minio +#11 CACHED + +#12 [s3-bootstrap client-build 2/4] RUN git clone --depth 1 --branch RELEASE.2025-04-16T18-13-26Z https://github.com/minio/mc.git /src +#12 CACHED + +#13 [s3-bootstrap client-build 3/4] WORKDIR /src +#13 CACHED + +#14 [s3-bootstrap client-build 4/4] RUN go build -trimpath -o /out/mc . +#14 CACHED + +#15 [s3-bootstrap client 2/2] COPY --from=client-build /out/mc /usr/local/bin/mc +#15 CACHED + +#16 [s3] exporting to image +#16 exporting layers done +#16 exporting manifest sha256:b1198981eeff6339fd7123ceca6c19dc73f2827fa90aae1f9cb8de780ec2836d done +#16 exporting config sha256:0b61384e4363ef4667e244183f9ef5413a570d0aa94be8f1142102764438b206 done +#16 exporting attestation manifest sha256:1540302c910f65c376a5713a29ae4942d110e89200a395a2758eb008e4d1e2f4 0.1s done +#16 exporting manifest list sha256:1f07b5ea243b9bc1a3c61732ac5b5a5d3b4d1d3ab6f1b604b0cc7da1cf7e1e48 +#16 exporting manifest list sha256:1f07b5ea243b9bc1a3c61732ac5b5a5d3b4d1d3ab6f1b604b0cc7da1cf7e1e48 0.0s done +#16 naming to docker.io/library/messenger-lab4-clean-check-s3:latest done +#16 unpacking to docker.io/library/messenger-lab4-clean-check-s3:latest done +#16 DONE 0.2s + +#17 [s3-bootstrap] exporting to image +#17 exporting layers done +#17 exporting manifest sha256:fed62e4d3740a30c8e5fe7240a72b84e201bef30d0e5b044b17e5a75fffd3d0e done +#17 exporting config sha256:97c8733577e743931782ee94435d027b4789c677f5511311bbb71021c8822702 done +#17 exporting attestation manifest sha256:d1a163f5216a4cc55a5cd55ee032b017c9e832cb3ac166aa5e0e6667d3dd7dcb 0.1s done +#17 exporting manifest list sha256:d0561db9f9e356a70899e5f3d88e83022cea23d2dba9f63bac6772a97f333a95 0.0s done +#17 naming to docker.io/library/messenger-lab4-clean-check-s3-bootstrap:latest done +#17 unpacking to docker.io/library/messenger-lab4-clean-check-s3-bootstrap:latest done +#17 DONE 0.2s + +#18 [s3] resolving provenance for metadata file +#18 DONE 0.0s + +#19 [s3-bootstrap] resolving provenance for metadata file +#19 DONE 0.0s + messenger-lab4-clean-check-s3-bootstrap Built + messenger-lab4-clean-check-s3 Built +COMMAND: docker compose --env-file .env up -d s3 + Container messenger-lab4-clean-check-s3-1 Recreate + Container messenger-lab4-clean-check-s3-1 Recreated + Container messenger-lab4-clean-check-s3-1 Starting + Container messenger-lab4-clean-check-s3-1 Started +COMMAND: docker compose --env-file .env run --rm s3-bootstrap + Container messenger-lab4-clean-check-s3-1 Running +PASS: bucket configured private; API user policy applied. +COMMAND: docker compose --env-file .env run --rm --entrypoint /bin/sh s3-bootstrap /bootstrap/verify.sh seed + Container messenger-lab4-clean-check-s3-1 Running +PASS: API credentials can upload fixture. +COMMAND: docker compose --env-file .env run --rm s3-bootstrap + Container messenger-lab4-clean-check-s3-1 Running +PASS: bucket configured private; API user policy applied. +COMMAND: docker compose --env-file .env restart s3 + Container messenger-lab4-clean-check-s3-1 Restarting + Container messenger-lab4-clean-check-s3-1 Started +COMMAND: docker compose --env-file .env run --rm s3-bootstrap + Container messenger-lab4-clean-check-s3-1 Running +PASS: bucket configured private; API user policy applied. +COMMAND: docker compose --env-file .env run --rm --entrypoint /bin/sh s3-bootstrap /bootstrap/verify.sh check + Container messenger-lab4-clean-check-s3-1 Running +PASS: API credentials read original bytes after bootstrap/restart. +PASS: unsigned GET of existing object returns HTTP 403. +PASS: API cannot write outside attachments prefix. +PASS: API cannot access administrative server information. +PASS: API can delete its test object; fixture removed. +PASS: S3 has no published host ports. +PASS: S3 infrastructure checks completed. diff --git a/evidence/clean-schema.txt b/evidence/clean-schema.txt new file mode 100644 index 00000000..248343ee --- /dev/null +++ b/evidence/clean-schema.txt @@ -0,0 +1 @@ +PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested. diff --git a/evidence/clean-shutdown.txt b/evidence/clean-shutdown.txt new file mode 100644 index 00000000..07331da5 --- /dev/null +++ b/evidence/clean-shutdown.txt @@ -0,0 +1,34 @@ + Container messenger-lab4-clean-check-lb-1 Stopping + Container messenger-lab4-clean-check-lb-1 Stopped + Container messenger-lab4-clean-check-lb-1 Removing + Container messenger-lab4-clean-check-lb-1 Removed + Container messenger-lab4-clean-check-api-2 Stopping + Container messenger-lab4-clean-check-api-1 Stopping + Container messenger-lab4-clean-check-api-1 Stopped + Container messenger-lab4-clean-check-api-1 Removing + Container messenger-lab4-clean-check-api-2 Stopped + Container messenger-lab4-clean-check-api-2 Removing + Container messenger-lab4-clean-check-api-1 Removed + Container messenger-lab4-clean-check-api-2 Removed + Container messenger-lab4-clean-check-redis-1 Stopping + Container messenger-lab4-clean-check-migrate-1 Stopping + Container messenger-lab4-clean-check-s3-bootstrap-1 Stopping + Container messenger-lab4-clean-check-s3-bootstrap-1 Stopped + Container messenger-lab4-clean-check-s3-bootstrap-1 Removing + Container messenger-lab4-clean-check-migrate-1 Stopped + Container messenger-lab4-clean-check-migrate-1 Removing + Container messenger-lab4-clean-check-migrate-1 Removed + Container messenger-lab4-clean-check-postgres-1 Stopping + Container messenger-lab4-clean-check-s3-bootstrap-1 Removed + Container messenger-lab4-clean-check-s3-1 Stopping + Container messenger-lab4-clean-check-redis-1 Stopped + Container messenger-lab4-clean-check-redis-1 Removing + Container messenger-lab4-clean-check-redis-1 Removed + Container messenger-lab4-clean-check-postgres-1 Stopped + Container messenger-lab4-clean-check-postgres-1 Removing + Container messenger-lab4-clean-check-s3-1 Stopped + Container messenger-lab4-clean-check-s3-1 Removing + Container messenger-lab4-clean-check-postgres-1 Removed + Container messenger-lab4-clean-check-s3-1 Removed + Network messenger-lab4-clean-check_default Removing + Network messenger-lab4-clean-check_default Removed diff --git a/evidence/clean-smoke.txt b/evidence/clean-smoke.txt new file mode 100644 index 00000000..c554fd6f --- /dev/null +++ b/evidence/clean-smoke.txt @@ -0,0 +1 @@ +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/clean-source.txt b/evidence/clean-source.txt new file mode 100644 index 00000000..2c621f2d --- /dev/null +++ b/evidence/clean-source.txt @@ -0,0 +1,6 @@ +Source: git clone from https://gitea.privcloud.tech/Daria_Sitnikova/mtusi2026lab4.git +Branch: solution/bvt2303-sitnikova +Commit: +879463c947ae3480a82a7dac1873ae7782b0e277 +Tracked changes immediately after clone: +Before init: .env exists = False; secrets exists = False diff --git a/evidence/clean-upload-boundaries.txt b/evidence/clean-upload-boundaries.txt new file mode 100644 index 00000000..4194d475 --- /dev/null +++ b/evidence/clean-upload-boundaries.txt @@ -0,0 +1,2 @@ +PASS: real PNG exactly 10 MiB accepted; original bytes/checksum preserved +PASS: chunked stream without Content-Length, 10 MiB+1 byte, returns 413 from API diff --git a/evidence/clean-volumes-retained.txt b/evidence/clean-volumes-retained.txt new file mode 100644 index 00000000..b1bdcbec --- /dev/null +++ b/evidence/clean-volumes-retained.txt @@ -0,0 +1,2 @@ +messenger-lab4-clean-check_postgres_lab4_data_v1 +messenger-lab4-clean-check_s3_lab4_data_v1 diff --git a/evidence/lab4-failover.txt b/evidence/lab4-failover.txt new file mode 100644 index 00000000..4f0c70f6 --- /dev/null +++ b/evidence/lab4-failover.txt @@ -0,0 +1,3 @@ +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 119, "failed_attempts": 2, "first_recovery_seconds": 2.344} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/lab4-final-smoke.txt b/evidence/lab4-final-smoke.txt new file mode 100644 index 00000000..c554fd6f --- /dev/null +++ b/evidence/lab4-final-smoke.txt @@ -0,0 +1 @@ +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/lab4-final-stack.txt b/evidence/lab4-final-stack.txt new file mode 100644 index 00000000..24bb171f --- /dev/null +++ b/evidence/lab4-final-stack.txt @@ -0,0 +1,7 @@ +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +messenger-lab4-api-1 messenger-lab4-api "/server" api 12 minutes ago Up 12 minutes (healthy) 8080/tcp +messenger-lab4-api-2 messenger-lab4-api "/server" api 12 minutes ago Up 12 minutes (healthy) 8080/tcp +messenger-lab4-lb-1 haproxy:3.2.4-alpine "docker-entrypoint.s…" lb About an hour ago Up 28 minutes 127.0.0.1:8084->8080/tcp +messenger-lab4-postgres-1 postgres:17.9-bookworm "docker-entrypoint.s…" postgres About an hour ago Up 29 minutes (healthy) 5432/tcp +messenger-lab4-redis-1 redis:7.4.11-alpine "docker-entrypoint.s…" redis About an hour ago Up 29 minutes (healthy) 6379/tcp +messenger-lab4-s3-1 messenger-lab4-s3 "minio server /data …" s3 14 minutes ago Up 14 minutes diff --git a/evidence/lab4-persistence.txt b/evidence/lab4-persistence.txt new file mode 100644 index 00000000..749fd29b --- /dev/null +++ b/evidence/lab4-persistence.txt @@ -0,0 +1,2 @@ +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/lab4-schema-final.txt b/evidence/lab4-schema-final.txt new file mode 100644 index 00000000..248343ee --- /dev/null +++ b/evidence/lab4-schema-final.txt @@ -0,0 +1 @@ +PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested. diff --git a/evidence/lab4-smoke-after-cleanup.txt b/evidence/lab4-smoke-after-cleanup.txt new file mode 100644 index 00000000..c554fd6f --- /dev/null +++ b/evidence/lab4-smoke-after-cleanup.txt @@ -0,0 +1 @@ +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/lab4-smoke-after-resilience.txt b/evidence/lab4-smoke-after-resilience.txt new file mode 100644 index 00000000..c554fd6f --- /dev/null +++ b/evidence/lab4-smoke-after-resilience.txt @@ -0,0 +1 @@ +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/lab4-smoke.txt b/evidence/lab4-smoke.txt new file mode 100644 index 00000000..c554fd6f --- /dev/null +++ b/evidence/lab4-smoke.txt @@ -0,0 +1 @@ +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/s3-config-checks.txt b/evidence/s3-config-checks.txt new file mode 100644 index 00000000..522423f4 --- /dev/null +++ b/evidence/s3-config-checks.txt @@ -0,0 +1,13 @@ +Дата: 2026-11-06 +Этап: конфигурация S3; приложение не поддерживает вложения. + +docker compose --env-file .env.example config --quiet +Результат: exit code 0. + +Python runtime Codex: python.exe -X utf8 scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. + +docker info --format '{{.ServerVersion}}' +Результат: Docker Engine недоступен: pipe dockerDesktopLinuxEngine не найден. +Образы не скачивались, S3/bootstrap не запускались. +Приватность, права API, повторный bootstrap и сохранность пока не проверены. diff --git a/evidence/s3-go-client.txt b/evidence/s3-go-client.txt new file mode 100644 index 00000000..7dab9e07 --- /dev/null +++ b/evidence/s3-go-client.txt @@ -0,0 +1,170 @@ +#1 [internal] load local bake definitions +#1 reading from stdin 1.24kB 0.0s done +#1 DONE 0.0s + +#2 [s3-bootstrap internal] load build definition from Dockerfile +#2 DONE 0.0s + +#2 [s3 internal] load build definition from Dockerfile +#2 transferring dockerfile: 691B done +#2 DONE 0.0s + +#3 [s3-bootstrap internal] load metadata for docker.io/library/golang:1.26.3 +#3 DONE 0.1s + +#4 [s3 internal] load metadata for docker.io/library/alpine:3.22.2 +#4 DONE 1.7s + +#5 [s3 internal] load .dockerignore +#5 transferring context: 2B done +#5 DONE 0.0s + +#6 [s3-bootstrap server 1/2] FROM docker.io/library/alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 +#6 resolve docker.io/library/alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 0.0s done +#6 DONE 0.0s + +#7 [s3 server-build 1/4] FROM docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a +#7 resolve docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a 0.0s done +#7 DONE 0.0s + +#8 [s3 server-build 2/4] RUN git clone --depth 1 --branch RELEASE.2025-04-22T22-12-26Z https://github.com/minio/minio.git /src +#8 CACHED + +#9 [s3 server-build 4/4] RUN go build -trimpath -o /out/minio . +#9 CACHED + +#10 [s3 server-build 3/4] WORKDIR /src +#10 CACHED + +#11 [s3 server 2/2] COPY --from=server-build /out/minio /usr/local/bin/minio +#11 CACHED + +#12 [s3-bootstrap client-build 2/4] RUN git clone --depth 1 --branch RELEASE.2025-04-16T18-13-26Z https://github.com/minio/mc.git /src +#12 CACHED + +#13 [s3-bootstrap client-build 3/4] WORKDIR /src +#13 CACHED + +#14 [s3-bootstrap client-build 4/4] RUN go build -trimpath -o /out/mc . +#14 CACHED + +#15 [s3-bootstrap client 2/2] COPY --from=client-build /out/mc /usr/local/bin/mc +#15 CACHED + +#16 [s3-bootstrap] exporting to image +#16 exporting layers done +#16 exporting manifest sha256:2103cc3b4036db5a1e5ee6580cef84645bc7465a14181fa62db60394f79fddfe done +#16 exporting config sha256:86a478d925e08843fe09c5d8f53e001d9daffdf9baab39f6bbfc48ce67db5b4d done +#16 exporting attestation manifest sha256:f21b0564052d04805a27a8149e389479ad14f7964c135743a638c4a910c39692 +#16 exporting attestation manifest sha256:f21b0564052d04805a27a8149e389479ad14f7964c135743a638c4a910c39692 0.1s done +#16 exporting manifest list sha256:3c496c1fe953cb1589491a64cdfa96f288269e9ba8cc29dc27259a69bf328da5 0.0s done +#16 naming to docker.io/library/messenger-lab4-s3-bootstrap:latest +#16 naming to docker.io/library/messenger-lab4-s3-bootstrap:latest done +#16 unpacking to docker.io/library/messenger-lab4-s3-bootstrap:latest done +#16 DONE 0.2s + +#17 [s3] exporting to image +#17 exporting layers done +#17 exporting manifest sha256:8410921329eec1208439c3cd9dcb0b1ced8a8fb0a7348cea1d338f6f0a79c049 done +#17 exporting config sha256:350ec0fe229ab9c2b91b2c87cfb4e1e43d6dc3bbad5d7d9c0c7af1e5cf528867 done +#17 exporting attestation manifest sha256:989cd11888443002732f8514d31bf9153f5ab890752b95d1392be8e14c6d19cb 0.1s done +#17 exporting manifest list sha256:595049d0d91bf70dc72556ea50d7eacf1ae50dddf18b58b519ff800b924231fd 0.0s done +#17 naming to docker.io/library/messenger-lab4-s3:latest done +#17 unpacking to docker.io/library/messenger-lab4-s3:latest done +#17 DONE 0.2s + +#18 [s3-bootstrap] resolving provenance for metadata file +#18 DONE 0.0s + +#19 [s3] resolving provenance for metadata file +#19 DONE 0.0s + messenger-lab4-s3-bootstrap Built + messenger-lab4-s3 Built + Container messenger-lab4-s3-1 Recreate + Container messenger-lab4-s3-1 Recreated + Container messenger-lab4-s3-bootstrap-1 Creating + Container messenger-lab4-s3-bootstrap-1 Created + Container messenger-lab4-s3-1 Starting + Container messenger-lab4-s3-1 Started + Container messenger-lab4-s3-bootstrap-1 Starting + Container messenger-lab4-s3-bootstrap-1 Started +#1 [internal] load local bake definitions +#1 reading from stdin 1.79kB 0.1s done +#1 DONE 0.1s + +#2 [internal] load build definition from Dockerfile +#2 transferring dockerfile: 727B 0.0s done +#2 DONE 0.1s + +#3 [internal] load metadata for docker.io/library/golang:1.26.3 +#3 DONE 0.0s + +#4 [internal] load .dockerignore +#4 transferring context: +#4 transferring context: 216B done +#4 DONE 0.0s + +#5 [build 1/7] FROM docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a +#5 resolve docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a 0.0s done +#5 DONE 0.0s + +#6 [internal] load build context +#6 transferring context: 120.57kB 0.0s done +#6 DONE 0.1s + +#7 [build 2/7] WORKDIR /src +#7 CACHED + +#8 [build 3/7] COPY go.mod go.sum ./ +#8 DONE 0.1s + +#9 [build 4/7] RUN go mod download +#9 DONE 6.2s + +#10 [build 5/7] COPY cmd ./cmd +#10 DONE 0.1s + +#11 [build 6/7] COPY internal ./internal +#11 DONE 0.1s + +#12 [build 7/7] RUN go vet ./... && go test ./... && go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server +#12 35.72 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/cmd/server [no test files] +#12 35.72 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/cache [no test files] +#12 35.96 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/httpapi 0.227s +#12 35.96 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model [no test files] +#12 35.99 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/objectstore 0.012s +#12 36.79 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/password 0.825s +#12 36.79 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service 0.710s +#12 36.79 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store 0.008s +#12 36.79 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/upload 0.080s +#12 DONE 54.8s + +#13 exporting to image +#13 exporting layers +#13 exporting layers 19.4s done +#13 exporting manifest sha256:9eeb24557750ef79f0c46edb77c7dfa2fca2f9d56d31217a07f7cb6442a12e7b 0.0s done +#13 exporting config sha256:d8594fc7694b0e43ca60bc74f94e8ad4ef0cd8295344582c85d0df0a388d98e5 0.0s done +#13 exporting attestation manifest sha256:eeed57355621175c12540f98e73923f054017323ade4daf13a05d37f86bfe747 0.0s done +#13 exporting manifest list sha256:09c4dbc8e4e7eebe9bf977bfcb4595bb5e2f64c03224a3b4ef7a58551b065948 +#13 exporting manifest list sha256:09c4dbc8e4e7eebe9bf977bfcb4595bb5e2f64c03224a3b4ef7a58551b065948 0.0s done +#13 naming to docker.io/library/messenger-lab4-s3-client-test:latest done +#13 unpacking to docker.io/library/messenger-lab4-s3-client-test:latest +#13 unpacking to docker.io/library/messenger-lab4-s3-client-test:latest 7.3s done +#13 DONE 26.9s + +#14 resolving provenance for metadata file +#14 DONE 0.0s +=== RUN TestObjectKey +--- PASS: TestObjectKey (0.00s) +=== RUN TestInvalidConfig +--- PASS: TestInvalidConfig (0.00s) +=== RUN TestS3Integration + s3_test.go:73: PASS: validated original uploaded with API credentials + s3_test.go:87: PASS: downloaded original bytes, MIME, size and SHA-256 match + s3_test.go:97: PASS: deleted object is not found; repeat delete succeeds + s3_test.go:103: PASS: caller cancellation propagated + s3_test.go:118: PASS: incorrect credentials yield sanitized unavailable error + s3_test.go:129: PASS: unreachable endpoint yields sanitized unavailable error +--- PASS: TestS3Integration (0.10s) +PASS +ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/objectstore 0.114s diff --git a/evidence/s3-runtime-checks.txt b/evidence/s3-runtime-checks.txt new file mode 100644 index 00000000..a28ea6e5 --- /dev/null +++ b/evidence/s3-runtime-checks.txt @@ -0,0 +1,115 @@ +Existing .env preserved. +Existing secret preserved: postgres_password +Existing secret preserved: s3_root_user +Existing secret preserved: s3_root_password +Existing secret preserved: s3_access_key_id +Existing secret preserved: s3_secret_access_key +COMMAND: docker compose --env-file .env build s3 s3-bootstrap +#1 [internal] load local bake definitions +#1 reading from stdin 1.24kB 0.0s done +#1 DONE 0.0s + +#2 [s3-bootstrap internal] load build definition from Dockerfile +#2 transferring dockerfile: 691B 0.0s done +#2 DONE 0.0s + +#3 [s3 internal] load metadata for docker.io/library/golang:1.26.3 +#3 DONE 0.1s + +#4 [s3-bootstrap internal] load metadata for docker.io/library/alpine:3.22.2 +#4 DONE 0.7s + +#5 [s3 internal] load .dockerignore +#5 transferring context: 2B done +#5 DONE 0.0s + +#6 [s3-bootstrap client-build 1/4] FROM docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a +#6 resolve docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a 0.0s done +#6 DONE 0.0s + +#7 [s3-bootstrap client 1/2] FROM docker.io/library/alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 +#7 resolve docker.io/library/alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 0.0s done +#7 DONE 0.0s + +#8 [s3-bootstrap client-build 4/4] RUN go build -trimpath -o /out/mc . +#8 CACHED + +#9 [s3-bootstrap client-build 3/4] WORKDIR /src +#9 CACHED + +#10 [s3-bootstrap client-build 2/4] RUN git clone --depth 1 --branch RELEASE.2025-04-16T18-13-26Z https://github.com/minio/mc.git /src +#10 CACHED + +#11 [s3-bootstrap client 2/2] COPY --from=client-build /out/mc /usr/local/bin/mc +#11 CACHED + +#12 [s3 server-build 2/4] RUN git clone --depth 1 --branch RELEASE.2025-04-22T22-12-26Z https://github.com/minio/minio.git /src +#12 CACHED + +#13 [s3 server-build 3/4] WORKDIR /src +#13 CACHED + +#14 [s3 server-build 4/4] RUN go build -trimpath -o /out/minio . +#14 CACHED + +#15 [s3 server 2/2] COPY --from=server-build /out/minio /usr/local/bin/minio +#15 CACHED + +#16 [s3] exporting to image +#16 exporting layers done +#16 exporting manifest sha256:8410921329eec1208439c3cd9dcb0b1ced8a8fb0a7348cea1d338f6f0a79c049 done +#16 exporting config sha256:350ec0fe229ab9c2b91b2c87cfb4e1e43d6dc3bbad5d7d9c0c7af1e5cf528867 done +#16 exporting attestation manifest sha256:027852aeca4680325e084357203825363c031f9f0b0565d3043f8c790b9362ab +#16 exporting attestation manifest sha256:027852aeca4680325e084357203825363c031f9f0b0565d3043f8c790b9362ab 0.1s done +#16 exporting manifest list sha256:8e2f50b577b8cb9a5b682c69d39b5ca540ac6749c2c9a761ea7e53456af803dc 0.0s done +#16 naming to docker.io/library/messenger-lab4-s3:latest +#16 naming to docker.io/library/messenger-lab4-s3:latest done +#16 unpacking to docker.io/library/messenger-lab4-s3:latest done +#16 DONE 0.2s + +#17 [s3-bootstrap] exporting to image +#17 exporting layers done +#17 exporting manifest sha256:2103cc3b4036db5a1e5ee6580cef84645bc7465a14181fa62db60394f79fddfe done +#17 exporting config sha256:86a478d925e08843fe09c5d8f53e001d9daffdf9baab39f6bbfc48ce67db5b4d done +#17 exporting attestation manifest sha256:a088e11c25a95e5e75300b6b0277d17f5dda2fd4c09084ec9f03d10ca9a25d58 0.1s done +#17 exporting manifest list sha256:c6f2770ebfdc87921c25caf81edb798da1221abb3372c8ecca8b60dd959e0c95 0.0s done +#17 naming to docker.io/library/messenger-lab4-s3-bootstrap:latest done +#17 unpacking to docker.io/library/messenger-lab4-s3-bootstrap:latest done +#17 DONE 0.2s + +#18 [s3] resolving provenance for metadata file +#18 DONE 0.0s + +#19 [s3-bootstrap] resolving provenance for metadata file +#19 DONE 0.0s + messenger-lab4-s3-bootstrap Built + messenger-lab4-s3 Built +COMMAND: docker compose --env-file .env up -d s3 + Container messenger-lab4-s3-1 Recreate + Container messenger-lab4-s3-1 Recreated + Container messenger-lab4-s3-1 Starting + Container messenger-lab4-s3-1 Started +COMMAND: docker compose --env-file .env run --rm s3-bootstrap + Container messenger-lab4-s3-1 Running +PASS: bucket configured private; API user policy applied. +COMMAND: docker compose --env-file .env run --rm --entrypoint /bin/sh s3-bootstrap /bootstrap/verify.sh seed + Container messenger-lab4-s3-1 Running +PASS: API credentials can upload fixture. +COMMAND: docker compose --env-file .env run --rm s3-bootstrap + Container messenger-lab4-s3-1 Running +PASS: bucket configured private; API user policy applied. +COMMAND: docker compose --env-file .env restart s3 + Container messenger-lab4-s3-1 Restarting + Container messenger-lab4-s3-1 Started +COMMAND: docker compose --env-file .env run --rm s3-bootstrap + Container messenger-lab4-s3-1 Running +PASS: bucket configured private; API user policy applied. +COMMAND: docker compose --env-file .env run --rm --entrypoint /bin/sh s3-bootstrap /bootstrap/verify.sh check + Container messenger-lab4-s3-1 Running +PASS: API credentials read original bytes after bootstrap/restart. +PASS: unsigned GET of existing object returns HTTP 403. +PASS: API cannot write outside attachments prefix. +PASS: API cannot access administrative server information. +PASS: API can delete its test object; fixture removed. +PASS: S3 has no published host ports. +PASS: S3 infrastructure checks completed. diff --git a/evidence/transfer-checks.txt b/evidence/transfer-checks.txt new file mode 100644 index 00000000..6ca6ce1c --- /dev/null +++ b/evidence/transfer-checks.txt @@ -0,0 +1,18 @@ +Дата: 2026-11-06 +Этап: перенос основы lab3 в lab4; S3 и вложения пока не реализованы. + +Команда: go test ./... +Результат: PASS; httpapi, password, service, store — ok; остальные пакеты без тестов. +Первый запуск в песочнице: HTTP-тесты получили отказ подключения к localhost. +Повторный запуск вне песочницы: exit code 0. + +Команда: go vet ./... +Результат: exit code 0, замечаний нет. + +Команда: C:/Users/homur/.cache/codex-runtimes/codex-primary-runtime/dependencies/python/python.exe -X utf8 scripts/check_contract.py +Результат: PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. + +Команда: docker compose --env-file .env.example config --quiet +Результат: exit code 0, замечаний нет. Для проверки использован пример переменных; локальные secrets не создавались. + +Стенд не запускался. Публичный smoke lab4, S3, сохранность и failover lab4 не проверялись. \ No newline at end of file diff --git a/evidence/upload-boundaries.txt b/evidence/upload-boundaries.txt new file mode 100644 index 00000000..4194d475 --- /dev/null +++ b/evidence/upload-boundaries.txt @@ -0,0 +1,2 @@ +PASS: real PNG exactly 10 MiB accepted; original bytes/checksum preserved +PASS: chunked stream without Content-Length, 10 MiB+1 byte, returns 413 from API diff --git a/evidence/upload-cleanup-build.txt b/evidence/upload-cleanup-build.txt new file mode 100644 index 00000000..30e1b841 --- /dev/null +++ b/evidence/upload-cleanup-build.txt @@ -0,0 +1,204 @@ +#1 [internal] load local bake definitions +#1 reading from stdin 1.75kB 0.1s done +#1 DONE 0.1s + +#2 [api internal] load build definition from Dockerfile +#2 DONE 0.0s + +#3 [s3-bootstrap internal] load build definition from Dockerfile +#3 transferring dockerfile: 691B 0.0s done +#3 DONE 0.0s + +#2 [api internal] load build definition from Dockerfile +#2 transferring dockerfile: 727B done +#2 DONE 0.0s + +#4 [s3 internal] load metadata for docker.io/library/golang:1.26.3 +#4 DONE 0.0s + +#5 [api internal] load .dockerignore +#5 transferring context: 216B done +#5 DONE 0.0s + +#6 [s3 internal] load metadata for docker.io/library/alpine:3.22.2 +#6 ... + +#7 [api build 1/7] FROM docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a +#7 resolve docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a 0.0s done +#7 DONE 0.0s + +#8 [api internal] load build context +#8 transferring context: 9.45kB 0.0s done +#8 DONE 0.0s + +#9 [api build 2/7] WORKDIR /src +#9 CACHED + +#10 [api build 3/7] COPY go.mod go.sum ./ +#10 CACHED + +#11 [api build 4/7] RUN go mod download +#11 CACHED + +#12 [api build 5/7] COPY cmd ./cmd +#12 CACHED + +#13 [api build 6/7] COPY internal ./internal +#13 DONE 0.1s + +#14 [api build 7/7] RUN go vet ./... && go test ./... && go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server +#14 ... + +#7 [s3-bootstrap build 1/7] FROM docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a +#7 resolve docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a 0.0s done +#7 DONE 0.0s + +#6 [s3 internal] load metadata for docker.io/library/alpine:3.22.2 +#6 DONE 0.6s + +#15 [s3-bootstrap internal] load .dockerignore +#15 transferring context: 2B done +#15 DONE 0.0s + +#16 [s3-bootstrap server 1/2] FROM docker.io/library/alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 +#16 resolve docker.io/library/alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 0.0s done +#16 DONE 0.0s + +#17 [s3 server-build 3/4] WORKDIR /src +#17 CACHED + +#18 [s3 server-build 4/4] RUN go build -trimpath -o /out/minio . +#18 CACHED + +#19 [s3 server-build 2/4] RUN git clone --depth 1 --branch RELEASE.2025-04-22T22-12-26Z https://github.com/minio/minio.git /src +#19 CACHED + +#20 [s3 server 2/2] COPY --from=server-build /out/minio /usr/local/bin/minio +#20 CACHED + +#21 [s3-bootstrap client-build 3/4] WORKDIR /src +#21 CACHED + +#22 [s3-bootstrap client-build 2/4] RUN git clone --depth 1 --branch RELEASE.2025-04-16T18-13-26Z https://github.com/minio/mc.git /src +#22 CACHED + +#23 [s3-bootstrap client-build 4/4] RUN go build -trimpath -o /out/mc . +#23 CACHED + +#24 [s3-bootstrap client 2/2] COPY --from=client-build /out/mc /usr/local/bin/mc +#24 CACHED + +#25 [s3-bootstrap] exporting to image +#25 exporting layers done +#25 exporting manifest sha256:2103cc3b4036db5a1e5ee6580cef84645bc7465a14181fa62db60394f79fddfe done +#25 exporting config sha256:86a478d925e08843fe09c5d8f53e001d9daffdf9baab39f6bbfc48ce67db5b4d done +#25 exporting attestation manifest sha256:29bb82a16ff582dfc854dc1aedaf27e1b076395e03981be2bd3958bd9cebee34 0.1s done +#25 exporting manifest list sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2 +#25 ... + +#26 [s3] exporting to image +#26 exporting layers done +#26 exporting manifest sha256:8410921329eec1208439c3cd9dcb0b1ced8a8fb0a7348cea1d338f6f0a79c049 done +#26 exporting config sha256:350ec0fe229ab9c2b91b2c87cfb4e1e43d6dc3bbad5d7d9c0c7af1e5cf528867 done +#26 exporting attestation manifest sha256:7c528edd7a322bdcb516dc4a3b30b5a42b2e2e224680ef98f96db49027ea491f 0.1s done +#26 exporting manifest list sha256:11438918d8cb9830154b41768790ac06a47ddf242fb8abf3fb76a8a4c4a92942 0.0s done +#26 naming to docker.io/library/messenger-lab4-s3:latest done +#26 unpacking to docker.io/library/messenger-lab4-s3:latest done +#26 DONE 0.3s + +#25 [s3-bootstrap] exporting to image +#25 exporting manifest list sha256:8450edd03a5de051f050d07e0e41aff5862f48b4c2953998c2a0583e9d0389a2 0.0s done +#25 naming to docker.io/library/messenger-lab4-s3-bootstrap:latest done +#25 unpacking to docker.io/library/messenger-lab4-s3-bootstrap:latest done +#25 DONE 0.3s + +#14 [api build 7/7] RUN go vet ./... && go test ./... && go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server +#14 ... + +#27 [s3] resolving provenance for metadata file +#27 DONE 0.0s + +#28 [s3-bootstrap] resolving provenance for metadata file +#28 DONE 0.0s + +#14 [api build 7/7] RUN go vet ./... && go test ./... && go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server +#14 33.01 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/cmd/server [no test files] +#14 33.01 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/cache [no test files] +#14 33.32 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/httpapi 0.288s +#14 33.32 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model [no test files] +#14 33.32 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/objectstore 0.013s +#14 34.03 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/password 1.006s +#14 34.03 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service 0.772s +#14 34.03 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store 0.009s +#14 34.03 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/upload 0.090s +#14 DONE 62.5s + +#29 [api stage-2 1/1] COPY --from=build /out/server /server +#29 DONE 0.1s + +#30 [api] exporting to image +#30 exporting layers +#30 exporting layers 0.9s done +#30 exporting manifest sha256:57acabbab3c5c97a3fce5c2a1156269f578357acde41b5945ce504d4f887c4ae 0.0s done +#30 exporting config sha256:d7382672a61853a97efb1515b69b13708723be9197109ad1a9fd19cb8d20347f 0.0s done +#30 exporting attestation manifest sha256:eb5762af4862809a6e93b0d263fa45e760ff7924b2ab19087aa321dbc9f9fc36 0.0s done +#30 exporting manifest list sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499 +#30 exporting manifest list sha256:224baf980cca9492b0bac5b09e3540844078c485d2690658a9152c73e2f40499 0.0s done +#30 naming to docker.io/library/messenger-lab4-api:latest done +#30 unpacking to docker.io/library/messenger-lab4-api:latest 0.1s done +#30 DONE 1.2s + +#31 [api] resolving provenance for metadata file +#31 DONE 0.0s + messenger-lab4-s3-bootstrap Built + messenger-lab4-api Built + messenger-lab4-s3 Built + Container messenger-lab4-redis-1 Running + Container messenger-lab4-postgres-1 Running + Container messenger-lab4-s3-1 Recreate + Container messenger-lab4-s3-1 Recreated + Container messenger-lab4-s3-bootstrap-1 Recreate + Container messenger-lab4-s3-bootstrap-1 Recreated + Container messenger-lab4-api-2 Recreate + Container messenger-lab4-api-1 Recreate + Container messenger-lab4-api-1 Recreated + Container messenger-lab4-api-2 Recreated + Container messenger-lab4-lb-1 Running + Container messenger-lab4-s3-1 Starting + Container messenger-lab4-postgres-1 Waiting + Container messenger-lab4-s3-1 Started + Container messenger-lab4-s3-bootstrap-1 Starting + Container messenger-lab4-postgres-1 Healthy + Container messenger-lab4-migrate-1 Starting + Container messenger-lab4-s3-bootstrap-1 Started + Container messenger-lab4-migrate-1 Started + Container messenger-lab4-s3-bootstrap-1 Waiting + Container messenger-lab4-postgres-1 Waiting + Container messenger-lab4-migrate-1 Waiting + Container messenger-lab4-migrate-1 Exited + Container messenger-lab4-postgres-1 Healthy + Container messenger-lab4-s3-bootstrap-1 Exited + Container messenger-lab4-api-2 Starting + Container messenger-lab4-api-2 Started + Container messenger-lab4-api-1 Starting + Container messenger-lab4-api-1 Started + Container messenger-lab4-api-2 Waiting + Container messenger-lab4-api-1 Waiting + Container messenger-lab4-api-2 Healthy + Container messenger-lab4-api-1 Healthy + Container messenger-lab4-s3-bootstrap-1 Waiting + Container messenger-lab4-lb-1 Waiting + Container messenger-lab4-postgres-1 Waiting + Container messenger-lab4-redis-1 Waiting + Container messenger-lab4-migrate-1 Waiting + Container messenger-lab4-api-2 Waiting + Container messenger-lab4-api-1 Waiting + Container messenger-lab4-s3-1 Waiting + Container messenger-lab4-redis-1 Healthy + Container messenger-lab4-lb-1 Healthy + Container messenger-lab4-s3-1 Healthy + Container messenger-lab4-postgres-1 Healthy + Container messenger-lab4-s3-bootstrap-1 Exited + Container messenger-lab4-migrate-1 Exited + Container messenger-lab4-api-2 Healthy + Container messenger-lab4-api-1 Healthy diff --git a/evidence/upload-cleanup-migration.txt b/evidence/upload-cleanup-migration.txt new file mode 100644 index 00000000..9137d5b3 --- /dev/null +++ b/evidence/upload-cleanup-migration.txt @@ -0,0 +1,67 @@ +migrate-1 | BEGIN +migrate-1 | SET +migrate-1 | SET +migrate-1 | pg_advisory_xact_lock +migrate-1 | ----------------------- +migrate-1 | +migrate-1 | (1 row) +migrate-1 | +migrate-1 | CREATE TABLE +migrate-1 | psql:/migrations/apply.psql:13: NOTICE: relation "schema_migrations" already exists, skipping +migrate-1 | Migration 000001 already applied; skipping. +migrate-1 | Migration 000002 already applied; skipping. +migrate-1 | Migration 000003 already applied; skipping. +migrate-1 | COMMIT +migrate-1 | BEGIN +migrate-1 | SET +migrate-1 | SET +migrate-1 | pg_advisory_xact_lock +migrate-1 | ----------------------- +migrate-1 | +migrate-1 | (1 row) +migrate-1 | +migrate-1 | CREATE TABLE +migrate-1 | psql:/migrations/apply.psql:13: NOTICE: relation "schema_migrations" already exists, skipping +migrate-1 | Migration 000001 already applied; skipping. +migrate-1 | Migration 000002 already applied; skipping. +migrate-1 | Migration 000003 already applied; skipping. +migrate-1 | ALTER TABLE +migrate-1 | ALTER TABLE +migrate-1 | INSERT 0 1 +migrate-1 | Migration 000004 applied (commit follows). +migrate-1 | COMMIT +migrate-1 | BEGIN +migrate-1 | SET +migrate-1 | SET +migrate-1 | pg_advisory_xact_lock +migrate-1 | ----------------------- +migrate-1 | +migrate-1 | (1 row) +migrate-1 | +migrate-1 | CREATE TABLE +migrate-1 | psql:/migrations/apply.psql:13: NOTICE: relation "schema_migrations" already exists, skipping +migrate-1 | Migration 000001 already applied; skipping. +migrate-1 | Migration 000002 already applied; skipping. +migrate-1 | Migration 000003 already applied; skipping. +migrate-1 | Migration 000004 already applied; skipping. +migrate-1 | CREATE TABLE +migrate-1 | CREATE INDEX +migrate-1 | INSERT 0 1 +migrate-1 | Migration 000005 applied (commit follows). +migrate-1 | COMMIT +migrate-1 | BEGIN +migrate-1 | SET +migrate-1 | SET +migrate-1 | pg_advisory_xact_lock +migrate-1 | ----------------------- +migrate-1 | +migrate-1 | (1 row) +migrate-1 | +migrate-1 | CREATE TABLE +migrate-1 | psql:/migrations/apply.psql:13: NOTICE: relation "schema_migrations" already exists, skipping +migrate-1 | Migration 000001 already applied; skipping. +migrate-1 | Migration 000002 already applied; skipping. +migrate-1 | Migration 000003 already applied; skipping. +migrate-1 | Migration 000004 already applied; skipping. +migrate-1 | Migration 000005 already applied; skipping. +migrate-1 | COMMIT diff --git a/evidence/upload-cleanup.txt b/evidence/upload-cleanup.txt new file mode 100644 index 00000000..21b4b0ed --- /dev/null +++ b/evidence/upload-cleanup.txt @@ -0,0 +1,8 @@ +PASS: real S3 PUT followed by injected INSERT failure returns 503; durable intent remains +PASS: cleanup preserves fresh failed upload during 10-minute grace period +PASS: unavailable cleanup endpoint reports error and preserves intent for retry +PASS: aged intent detected; dry-run preserves object; cleanup removes it and retains tombstone +PASS: late PUT removed on retry; repeat DELETE of absent object succeeds +PASS: published attachment referenced by message remains byte-identical +PASS: disconnected partial HTTP upload never reaches S3 intent stage +PASS: upload compensation checks completed; grace aging was explicit SQL simulation diff --git a/evidence/upload-hardware.json b/evidence/upload-hardware.json new file mode 100644 index 00000000..ce26ffe9 --- /dev/null +++ b/evidence/upload-hardware.json @@ -0,0 +1,13 @@ +{ + "cpu": [ + { + "Name": "AMD Ryzen 7 7745HX with Radeon Graphics ", + "NumberOfCores": 8, + "NumberOfLogicalProcessors": 16 + } + ], + "date": "2026-11-06", + "host_ram_bytes": 33592733696, + "manufacturer": "MECHREVO", + "model": "Jiaolong Series MRID6" +} diff --git a/evidence/upload-memory-audit.txt b/evidence/upload-memory-audit.txt new file mode 100644 index 00000000..468a8271 --- /dev/null +++ b/evidence/upload-memory-audit.txt @@ -0,0 +1 @@ +PASS: 4 profiles x 30 unique uploads; in-flight=10, two API, 120 verified downloads, no OOM/swap. diff --git a/evidence/upload-memory-before.json b/evidence/upload-memory-before.json new file mode 100644 index 00000000..6c57d53e --- /dev/null +++ b/evidence/upload-memory-before.json @@ -0,0 +1,310 @@ +{ + "utc": "2026-11-06T13:49:06.153967+00:00", + "python": "3.12.14", + "docker": { + "server": "29.1.2", + "cpus": 16, + "memory_bytes": 16389435392 + }, + "method": "synchronized finite batches, fresh API per profile, kernel peak since cgroup creation including warmup/probes; no upload retries", + "profiles": [ + { + "fixture": { + "name": "rgba16_wide_40mp", + "content_type": "image/png", + "width": 40000000, + "height": 1, + "bit_depth": 16, + "color_type": 6, + "size_bytes": 1707450, + "sha256": "d9c6f5873a4f6efe7464b4c7beeaba780a5b218534f66246717f89446fffb5ff" + }, + "concurrency": 1, + "max_client_in_flight": 1, + "repeats": 1, + "warmup": "2 sample.png uploads after fresh API start", + "batches": [ + { + "repeat": 1, + "seconds": 16.406, + "successes": 1, + "errors": 0 + } + ], + "successful_requests": 1, + "error_count": 0, + "error_fraction": 0.0, + "errors": [], + "http_upload_seconds": 16.406, + "successful_rps": 0.061, + "latency_success_seconds": { + "p50": 16.375, + "p95": 16.375, + "p99": 16.375 + }, + "serving_instances": 1, + "downloads_verified": 1, + "resources_before": [ + { + "container_id": "528e913d565021f778de9d0da28f6bfecff9d366540e581d503e91a5062c6b06", + "status": "running", + "oom_killed": false, + "anon_bytes": 140488704, + "cpu_throttled_usec": 808535, + "cpu_usage_usec": 1615964, + "file_bytes": 0, + "memory_current_bytes": 143003648, + "memory_limit_bytes": 402653184, + "memory_peak_bytes": 143003648, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0 + }, + { + "container_id": "062cd8efaf27cb09017456f06f1df8324011dcb82da8a026bc3cdb78d0e56eef", + "status": "running", + "oom_killed": false, + "anon_bytes": 140374016, + "cpu_throttled_usec": 815406, + "cpu_usage_usec": 1694309, + "file_bytes": 0, + "memory_current_bytes": 142893056, + "memory_limit_bytes": 402653184, + "memory_peak_bytes": 142893056, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0 + } + ], + "resources_after": [ + { + "container_id": "528e913d565021f778de9d0da28f6bfecff9d366540e581d503e91a5062c6b06", + "status": "running", + "oom_killed": false, + "anon_bytes": 398372864, + "cpu_throttled_usec": 1184935, + "cpu_usage_usec": 10371702, + "file_bytes": 0, + "memory_current_bytes": 402554880, + "memory_limit_bytes": 402653184, + "memory_peak_bytes": 402657280, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "cpu_mean_percent_one_core": 49.81 + }, + { + "container_id": "062cd8efaf27cb09017456f06f1df8324011dcb82da8a026bc3cdb78d0e56eef", + "status": "running", + "oom_killed": false, + "anon_bytes": 140439552, + "cpu_throttled_usec": 815406, + "cpu_usage_usec": 2023246, + "file_bytes": 0, + "memory_current_bytes": 143282176, + "memory_limit_bytes": 402653184, + "memory_peak_bytes": 143765504, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "cpu_mean_percent_one_core": 1.87 + } + ], + "resource_observation_seconds": 17.578, + "docker_stats_samples": [ + { + "BlockIO": "1.42MB / 61.9MB", + "CPUPerc": "97.68%", + "Container": "528e913d565021f778de9d0da28f6bfecff9d366540e581d503e91a5062c6b06", + "ID": "528e913d5650", + "MemPerc": "99.96%", + "MemUsage": "383.8MiB / 384MiB", + "Name": "messenger-lab4-api-1", + "NetIO": "1.86MB / 153kB", + "PIDs": "7", + "elapsed_seconds": 2.75 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.27%", + "Container": "062cd8efaf27cb09017456f06f1df8324011dcb82da8a026bc3cdb78d0e56eef", + "ID": "062cd8efaf27", + "MemPerc": "34.60%", + "MemUsage": "132.9MiB / 384MiB", + "Name": "messenger-lab4-api-2", + "NetIO": "141kB / 148kB", + "PIDs": "7", + "elapsed_seconds": 2.75 + }, + { + "BlockIO": "74.1MB / 455MB", + "CPUPerc": "60.48%", + "Container": "528e913d565021f778de9d0da28f6bfecff9d366540e581d503e91a5062c6b06", + "ID": "528e913d5650", + "MemPerc": "99.98%", + "MemUsage": "383.9MiB / 384MiB", + "Name": "messenger-lab4-api-1", + "NetIO": "1.86MB / 157kB", + "PIDs": "7", + "elapsed_seconds": 4.844 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.27%", + "Container": "062cd8efaf27cb09017456f06f1df8324011dcb82da8a026bc3cdb78d0e56eef", + "ID": "062cd8efaf27", + "MemPerc": "34.60%", + "MemUsage": "132.9MiB / 384MiB", + "Name": "messenger-lab4-api-2", + "NetIO": "143kB / 150kB", + "PIDs": "7", + "elapsed_seconds": 4.844 + }, + { + "BlockIO": "160MB / 541MB", + "CPUPerc": "48.56%", + "Container": "528e913d565021f778de9d0da28f6bfecff9d366540e581d503e91a5062c6b06", + "ID": "528e913d5650", + "MemPerc": "99.99%", + "MemUsage": "384MiB / 384MiB", + "Name": "messenger-lab4-api-1", + "NetIO": "1.86MB / 160kB", + "PIDs": "7", + "elapsed_seconds": 6.938 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "6.34%", + "Container": "062cd8efaf27cb09017456f06f1df8324011dcb82da8a026bc3cdb78d0e56eef", + "ID": "062cd8efaf27", + "MemPerc": "34.60%", + "MemUsage": "132.9MiB / 384MiB", + "Name": "messenger-lab4-api-2", + "NetIO": "146kB / 154kB", + "PIDs": "7", + "elapsed_seconds": 6.938 + }, + { + "BlockIO": "244MB / 626MB", + "CPUPerc": "42.42%", + "Container": "528e913d565021f778de9d0da28f6bfecff9d366540e581d503e91a5062c6b06", + "ID": "528e913d5650", + "MemPerc": "99.96%", + "MemUsage": "383.8MiB / 384MiB", + "Name": "messenger-lab4-api-1", + "NetIO": "1.86MB / 163kB", + "PIDs": "7", + "elapsed_seconds": 9.016 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.31%", + "Container": "062cd8efaf27cb09017456f06f1df8324011dcb82da8a026bc3cdb78d0e56eef", + "ID": "062cd8efaf27", + "MemPerc": "34.60%", + "MemUsage": "132.9MiB / 384MiB", + "Name": "messenger-lab4-api-2", + "NetIO": "148kB / 156kB", + "PIDs": "7", + "elapsed_seconds": 9.016 + }, + { + "BlockIO": "330MB / 712MB", + "CPUPerc": "42.05%", + "Container": "528e913d565021f778de9d0da28f6bfecff9d366540e581d503e91a5062c6b06", + "ID": "528e913d5650", + "MemPerc": "99.96%", + "MemUsage": "383.8MiB / 384MiB", + "Name": "messenger-lab4-api-1", + "NetIO": "1.86MB / 166kB", + "PIDs": "7", + "elapsed_seconds": 11.11 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.25%", + "Container": "062cd8efaf27cb09017456f06f1df8324011dcb82da8a026bc3cdb78d0e56eef", + "ID": "062cd8efaf27", + "MemPerc": "34.61%", + "MemUsage": "132.9MiB / 384MiB", + "Name": "messenger-lab4-api-2", + "NetIO": "150kB / 159kB", + "PIDs": "7", + "elapsed_seconds": 11.11 + }, + { + "BlockIO": "414MB / 797MB", + "CPUPerc": "46.65%", + "Container": "528e913d565021f778de9d0da28f6bfecff9d366540e581d503e91a5062c6b06", + "ID": "528e913d5650", + "MemPerc": "99.97%", + "MemUsage": "383.9MiB / 384MiB", + "Name": "messenger-lab4-api-1", + "NetIO": "1.87MB / 169kB", + "PIDs": "7", + "elapsed_seconds": 13.203 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.36%", + "Container": "062cd8efaf27cb09017456f06f1df8324011dcb82da8a026bc3cdb78d0e56eef", + "ID": "062cd8efaf27", + "MemPerc": "34.61%", + "MemUsage": "132.9MiB / 384MiB", + "Name": "messenger-lab4-api-2", + "NetIO": "153kB / 164kB", + "PIDs": "7", + "elapsed_seconds": 13.203 + }, + { + "BlockIO": "494MB / 879MB", + "CPUPerc": "39.05%", + "Container": "528e913d565021f778de9d0da28f6bfecff9d366540e581d503e91a5062c6b06", + "ID": "528e913d5650", + "MemPerc": "99.97%", + "MemUsage": "383.9MiB / 384MiB", + "Name": "messenger-lab4-api-1", + "NetIO": "1.87MB / 171kB", + "PIDs": "7", + "elapsed_seconds": 15.297 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.31%", + "Container": "062cd8efaf27cb09017456f06f1df8324011dcb82da8a026bc3cdb78d0e56eef", + "ID": "062cd8efaf27", + "MemPerc": "34.61%", + "MemUsage": "132.9MiB / 384MiB", + "Name": "messenger-lab4-api-2", + "NetIO": "155kB / 167kB", + "PIDs": "7", + "elapsed_seconds": 15.297 + }, + { + "BlockIO": "554MB / 939MB", + "CPUPerc": "29.69%", + "Container": "528e913d565021f778de9d0da28f6bfecff9d366540e581d503e91a5062c6b06", + "ID": "528e913d5650", + "MemPerc": "99.17%", + "MemUsage": "380.8MiB / 384MiB", + "Name": "messenger-lab4-api-1", + "NetIO": "1.88MB / 1.89MB", + "PIDs": "7", + "elapsed_seconds": 17.391 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "5.81%", + "Container": "062cd8efaf27cb09017456f06f1df8324011dcb82da8a026bc3cdb78d0e56eef", + "ID": "062cd8efaf27", + "MemPerc": "34.61%", + "MemUsage": "132.9MiB / 384MiB", + "Name": "messenger-lab4-api-2", + "NetIO": "158kB / 170kB", + "PIDs": "7", + "elapsed_seconds": 17.391 + } + ] + } + ] +} \ No newline at end of file diff --git a/evidence/upload-memory-before.txt b/evidence/upload-memory-before.txt new file mode 100644 index 00000000..d1dcadc4 --- /dev/null +++ b/evidence/upload-memory-before.txt @@ -0,0 +1,2 @@ +{"profile": "rgba16_wide_40mp", "successes": 1, "errors": 0, "peak_MiB": [384.0, 137.11], "oom_killed": [false, false]} +PASS: upload concurrency/memory experiment and original-byte verification diff --git a/evidence/upload-memory-build.txt b/evidence/upload-memory-build.txt new file mode 100644 index 00000000..d7fefef6 --- /dev/null +++ b/evidence/upload-memory-build.txt @@ -0,0 +1,198 @@ +#1 [internal] load local bake definitions +#1 reading from stdin 1.75kB 0.0s done +#1 DONE 0.0s + +#2 [s3-bootstrap internal] load build definition from Dockerfile +#2 transferring dockerfile: 691B 0.0s done +#2 DONE 0.0s + +#3 [api internal] load build definition from Dockerfile +#3 transferring dockerfile: 868B 0.0s done +#3 DONE 0.0s + +#4 [api internal] load metadata for docker.io/library/golang:1.26.3 +#4 DONE 0.2s + +#5 [api internal] load .dockerignore +#5 transferring context: 216B done +#5 DONE 0.0s + +#6 [api build 1/7] FROM docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a +#6 resolve docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a 0.0s done +#6 DONE 0.0s + +#7 [api internal] load build context +#7 transferring context: 8.66kB 0.0s done +#7 DONE 0.1s + +#8 [api build 2/7] WORKDIR /src +#8 CACHED + +#9 [api build 3/7] COPY go.mod go.sum ./ +#9 CACHED + +#10 [api build 4/7] RUN go mod download +#10 CACHED + +#11 [api build 5/7] COPY cmd ./cmd +#11 DONE 0.0s + +#12 [api build 6/7] COPY internal ./internal +#12 DONE 0.1s + +#13 [s3 internal] load metadata for docker.io/library/alpine:3.22.2 +#13 DONE 1.1s + +#6 [s3-bootstrap build 1/7] FROM docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a +#6 resolve docker.io/library/golang:1.26.3@sha256:2d6c80227255c3112a4d08e67ba98e58efd3846daf15d9d7d4c389565d881b1a 0.0s done +#6 DONE 0.0s + +#14 [s3-bootstrap internal] load .dockerignore +#14 transferring context: 2B done +#14 DONE 0.0s + +#15 [s3-bootstrap server 1/2] FROM docker.io/library/alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 +#15 resolve docker.io/library/alpine:3.22.2@sha256:4b7ce07002c69e8f3d704a9c5d6fd3053be500b7f1c69fc0d80990c2ad8dd412 0.0s done +#15 DONE 0.0s + +#16 [s3 server-build 3/4] WORKDIR /src +#16 CACHED + +#17 [s3 server-build 2/4] RUN git clone --depth 1 --branch RELEASE.2025-04-22T22-12-26Z https://github.com/minio/minio.git /src +#17 CACHED + +#18 [s3 server-build 4/4] RUN go build -trimpath -o /out/minio . +#18 CACHED + +#19 [s3 server 2/2] COPY --from=server-build /out/minio /usr/local/bin/minio +#19 CACHED + +#20 [s3-bootstrap client-build 4/4] RUN go build -trimpath -o /out/mc . +#20 CACHED + +#21 [s3-bootstrap client-build 2/4] RUN git clone --depth 1 --branch RELEASE.2025-04-16T18-13-26Z https://github.com/minio/mc.git /src +#21 CACHED + +#22 [s3-bootstrap client-build 3/4] WORKDIR /src +#22 CACHED + +#23 [s3-bootstrap client 2/2] COPY --from=client-build /out/mc /usr/local/bin/mc +#23 CACHED + +#24 [s3-bootstrap] exporting to image +#24 exporting layers done +#24 exporting manifest sha256:2103cc3b4036db5a1e5ee6580cef84645bc7465a14181fa62db60394f79fddfe done +#24 exporting config sha256:86a478d925e08843fe09c5d8f53e001d9daffdf9baab39f6bbfc48ce67db5b4d done +#24 exporting attestation manifest sha256:0f32c81310586d82fb4cbaba831e62d56ca9a0a81a533ad023d3a4486cd91ba1 +#24 exporting attestation manifest sha256:0f32c81310586d82fb4cbaba831e62d56ca9a0a81a533ad023d3a4486cd91ba1 0.1s done +#24 exporting manifest list sha256:84d0d287f4f31706f439e743a5b2286b09e4eb24ec082f0502e7c0a8e145683c 0.7s done +#24 naming to docker.io/library/messenger-lab4-s3-bootstrap:latest +#24 naming to docker.io/library/messenger-lab4-s3-bootstrap:latest 0.0s done +#24 unpacking to docker.io/library/messenger-lab4-s3-bootstrap:latest done +#24 DONE 0.9s + +#25 [s3] exporting to image +#25 exporting layers done +#25 exporting manifest sha256:8410921329eec1208439c3cd9dcb0b1ced8a8fb0a7348cea1d338f6f0a79c049 done +#25 exporting config sha256:350ec0fe229ab9c2b91b2c87cfb4e1e43d6dc3bbad5d7d9c0c7af1e5cf528867 done +#25 exporting attestation manifest sha256:bbcabaca9545a2f42aacc5d238cd936e72c5e026f0568d280b450645ae608530 0.7s done +#25 exporting manifest list sha256:085b50f32b837834e97ff7bc3947e42f7b5ff59a60868c77f76f7f9023610471 0.0s done +#25 naming to docker.io/library/messenger-lab4-s3:latest done +#25 unpacking to docker.io/library/messenger-lab4-s3:latest 0.0s done +#25 DONE 0.9s + +#26 [api build 7/7] RUN go vet ./... && go test ./... && go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server && go build -trimpath -ldflags="-s -w" -o /out/resourceprobe ./cmd/resourceprobe +#26 ... + +#27 [s3-bootstrap] resolving provenance for metadata file +#27 DONE 0.0s + +#28 [s3] resolving provenance for metadata file +#28 DONE 0.0s + +#26 [api build 7/7] RUN go vet ./... && go test ./... && go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server && go build -trimpath -ldflags="-s -w" -o /out/resourceprobe ./cmd/resourceprobe +#26 33.17 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/cmd/resourceprobe [no test files] +#26 35.07 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/cmd/server [no test files] +#26 35.07 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/cache [no test files] +#26 35.36 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/httpapi 0.277s +#26 35.36 ? gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model [no test files] +#26 35.36 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/objectstore 0.012s +#26 35.96 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/password 0.879s +#26 35.96 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service 0.872s +#26 35.96 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store 0.009s +#26 35.96 ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/upload 0.085s +#26 DONE 64.3s + +#29 [api stage-2 1/2] COPY --from=build /out/server /server +#29 CACHED + +#30 [api stage-2 2/2] COPY --from=build /out/resourceprobe /resourceprobe +#30 DONE 0.0s + +#31 [api] exporting to image +#31 exporting layers +#31 exporting layers 0.2s done +#31 exporting manifest sha256:3f391f60c88e2f481b5c16ce0da9b7baae6f12ed10377ddf0296fa5df08781a1 0.0s done +#31 exporting config sha256:347a82637a10c37668f37204f62bb39421f40f8576f0084bb2c908a6dceb7346 0.0s done +#31 exporting attestation manifest sha256:355cc515b363e7cb81258fb6b617f6e9fa34f5da9b1f3af660c49bd95e8caf6e 0.0s done +#31 exporting manifest list sha256:158cce21bb09f8697f8fe08d07bef3862a106142dfcd25dd63e8dc2413564bcb +#31 exporting manifest list sha256:158cce21bb09f8697f8fe08d07bef3862a106142dfcd25dd63e8dc2413564bcb 0.0s done +#31 naming to docker.io/library/messenger-lab4-api:latest done +#31 unpacking to docker.io/library/messenger-lab4-api:latest +#31 unpacking to docker.io/library/messenger-lab4-api:latest 0.2s done +#31 DONE 0.6s + +#32 [api] resolving provenance for metadata file +#32 DONE 0.0s + messenger-lab4-s3 Built + messenger-lab4-s3-bootstrap Built + messenger-lab4-api Built + Container messenger-lab4-redis-1 Running + Container messenger-lab4-postgres-1 Running + Container messenger-lab4-s3-1 Recreate + Container messenger-lab4-s3-1 Recreated + Container messenger-lab4-s3-bootstrap-1 Recreate + Container messenger-lab4-s3-bootstrap-1 Recreated + Container messenger-lab4-api-2 Recreate + Container messenger-lab4-api-1 Recreate + Container messenger-lab4-api-1 Recreated + Container messenger-lab4-api-2 Recreated + Container messenger-lab4-lb-1 Running + Container messenger-lab4-s3-1 Starting + Container messenger-lab4-postgres-1 Waiting + Container messenger-lab4-s3-1 Started + Container messenger-lab4-s3-bootstrap-1 Starting + Container messenger-lab4-postgres-1 Healthy + Container messenger-lab4-migrate-1 Starting + Container messenger-lab4-s3-bootstrap-1 Started + Container messenger-lab4-migrate-1 Started + Container messenger-lab4-migrate-1 Waiting + Container messenger-lab4-s3-bootstrap-1 Waiting + Container messenger-lab4-postgres-1 Waiting + Container messenger-lab4-migrate-1 Exited + Container messenger-lab4-postgres-1 Healthy + Container messenger-lab4-s3-bootstrap-1 Exited + Container messenger-lab4-api-2 Starting + Container messenger-lab4-api-2 Started + Container messenger-lab4-api-1 Starting + Container messenger-lab4-api-1 Started + Container messenger-lab4-api-2 Waiting + Container messenger-lab4-api-1 Waiting + Container messenger-lab4-api-2 Healthy + Container messenger-lab4-api-1 Healthy + Container messenger-lab4-postgres-1 Waiting + Container messenger-lab4-redis-1 Waiting + Container messenger-lab4-migrate-1 Waiting + Container messenger-lab4-api-2 Waiting + Container messenger-lab4-api-1 Waiting + Container messenger-lab4-s3-1 Waiting + Container messenger-lab4-s3-bootstrap-1 Waiting + Container messenger-lab4-lb-1 Waiting + Container messenger-lab4-redis-1 Healthy + Container messenger-lab4-migrate-1 Exited + Container messenger-lab4-lb-1 Healthy + Container messenger-lab4-s3-1 Healthy + Container messenger-lab4-postgres-1 Healthy + Container messenger-lab4-s3-bootstrap-1 Exited + Container messenger-lab4-api-2 Healthy + Container messenger-lab4-api-1 Healthy diff --git a/evidence/upload-memory-db-counts.txt b/evidence/upload-memory-db-counts.txt new file mode 100644 index 00000000..c56c9e9e --- /dev/null +++ b/evidence/upload-memory-db-counts.txt @@ -0,0 +1,8 @@ + filename | count | count +----------------------+-------+------- + rgb8_1mp.png | 30 | 30 + rgba16_40mp.png | 30 | 30 + rgba16_wide_40mp.png | 30 | 30 + synthetic.jpg | 30 | 30 +(4 rows) + diff --git a/evidence/upload-memory.json b/evidence/upload-memory.json new file mode 100644 index 00000000..9ecab46d --- /dev/null +++ b/evidence/upload-memory.json @@ -0,0 +1,1018 @@ +{ + "utc": "2026-11-06T13:54:53.647998+00:00", + "python": "3.12.14", + "docker": { + "server": "29.1.2", + "cpus": 16, + "memory_bytes": 16389435392 + }, + "method": "synchronized finite batches, fresh API per profile, kernel peak since cgroup creation including warmup/probes; no upload retries", + "profiles": [ + { + "fixture": { + "name": "rgb8_1mp", + "content_type": "image/png", + "width": 1024, + "height": 768, + "bit_depth": 8, + "color_type": 2, + "size_bytes": 2362575, + "sha256": "6f0348c904892473b0426fb45a2badf5711e3550d7343ce65dd4fbe2a9e208a5" + }, + "concurrency": 10, + "max_client_in_flight": 10, + "repeats": 3, + "warmup": "2 sample.png uploads after fresh API start", + "batches": [ + { + "repeat": 1, + "seconds": 0.719, + "successes": 10, + "errors": 0 + }, + { + "repeat": 2, + "seconds": 0.625, + "successes": 10, + "errors": 0 + }, + { + "repeat": 3, + "seconds": 0.672, + "successes": 10, + "errors": 0 + } + ], + "successful_requests": 30, + "error_count": 0, + "error_fraction": 0.0, + "errors": [], + "http_upload_seconds": 2.016, + "successful_rps": 14.881, + "latency_success_seconds": { + "p50": 0.3515, + "p95": 0.5016, + "p99": 0.5267 + }, + "serving_instances": 2, + "downloads_verified": 30, + "resources_before": [ + { + "container_id": "bdafbbf336b76356d2867ed37a0ac4d6a14609d69f2bd88202ae170ba1a11166", + "status": "running", + "oom_killed": false, + "anon_bytes": 139292672, + "cpu_throttled_usec": 930907, + "cpu_usage_usec": 1650765, + "file_bytes": 0, + "memory_current_bytes": 141615104, + "memory_limit_bytes": 1610612736, + "memory_peak_bytes": 142708736, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "swap_current_bytes": 0, + "swap_limit_bytes": 0 + }, + { + "container_id": "5945fbb4c5d3979f8a9c819268204bab10b012854c7ac4b0bafbcc1e8adeb6a6", + "status": "running", + "oom_killed": false, + "anon_bytes": 139272192, + "cpu_throttled_usec": 745922, + "cpu_usage_usec": 1582621, + "file_bytes": 0, + "memory_current_bytes": 142311424, + "memory_limit_bytes": 1610612736, + "memory_peak_bytes": 142598144, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "swap_current_bytes": 0, + "swap_limit_bytes": 0 + } + ], + "resources_after": [ + { + "container_id": "bdafbbf336b76356d2867ed37a0ac4d6a14609d69f2bd88202ae170ba1a11166", + "status": "running", + "oom_killed": false, + "anon_bytes": 139575296, + "cpu_throttled_usec": 930907, + "cpu_usage_usec": 2307584, + "file_bytes": 0, + "memory_current_bytes": 142278656, + "memory_limit_bytes": 1610612736, + "memory_peak_bytes": 145842176, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "swap_current_bytes": 0, + "swap_limit_bytes": 0, + "cpu_mean_percent_one_core": 21.34 + }, + { + "container_id": "5945fbb4c5d3979f8a9c819268204bab10b012854c7ac4b0bafbcc1e8adeb6a6", + "status": "running", + "oom_killed": false, + "anon_bytes": 139440128, + "cpu_throttled_usec": 745922, + "cpu_usage_usec": 2226442, + "file_bytes": 0, + "memory_current_bytes": 142082048, + "memory_limit_bytes": 1610612736, + "memory_peak_bytes": 146305024, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "swap_current_bytes": 0, + "swap_limit_bytes": 0, + "cpu_mean_percent_one_core": 20.92 + } + ], + "resource_observation_seconds": 3.078, + "docker_stats_samples": [ + { + "BlockIO": "0B / 0B", + "CPUPerc": "32.11%", + "Container": "bdafbbf336b76356d2867ed37a0ac4d6a14609d69f2bd88202ae170ba1a11166", + "ID": "bdafbbf336b7", + "MemPerc": "8.88%", + "MemUsage": "136.4MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "14.9MB / 14.4MB", + "PIDs": "8", + "elapsed_seconds": 2.719 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "30.57%", + "Container": "5945fbb4c5d3979f8a9c819268204bab10b012854c7ac4b0bafbcc1e8adeb6a6", + "ID": "5945fbb4c5d3", + "MemPerc": "8.70%", + "MemUsage": "133.6MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "35.8MB / 35.9MB", + "PIDs": "7", + "elapsed_seconds": 2.719 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.28%", + "Container": "bdafbbf336b76356d2867ed37a0ac4d6a14609d69f2bd88202ae170ba1a11166", + "ID": "bdafbbf336b7", + "MemPerc": "8.69%", + "MemUsage": "133.5MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "35.8MB / 35.9MB", + "PIDs": "8", + "elapsed_seconds": 4.797 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.31%", + "Container": "5945fbb4c5d3979f8a9c819268204bab10b012854c7ac4b0bafbcc1e8adeb6a6", + "ID": "5945fbb4c5d3", + "MemPerc": "8.68%", + "MemUsage": "133.3MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "35.8MB / 35.9MB", + "PIDs": "7", + "elapsed_seconds": 4.797 + } + ] + }, + { + "fixture": { + "name": "jpeg_1mp", + "content_type": "image/jpeg", + "width": 1024, + "height": 768, + "size_bytes": 342078, + "sha256": "f403c1df38756cdd48934fdd253fa39248c85fa93f59a1766e2ca6c5586f744a" + }, + "concurrency": 10, + "max_client_in_flight": 10, + "repeats": 3, + "warmup": "2 sample.png uploads after fresh API start", + "batches": [ + { + "repeat": 1, + "seconds": 0.453, + "successes": 10, + "errors": 0 + }, + { + "repeat": 2, + "seconds": 0.5, + "successes": 10, + "errors": 0 + }, + { + "repeat": 3, + "seconds": 0.453, + "successes": 10, + "errors": 0 + } + ], + "successful_requests": 30, + "error_count": 0, + "error_fraction": 0.0, + "errors": [], + "http_upload_seconds": 1.406, + "successful_rps": 21.337, + "latency_success_seconds": { + "p50": 0.172, + "p95": 0.281, + "p99": 0.3144 + }, + "serving_instances": 2, + "downloads_verified": 30, + "resources_before": [ + { + "container_id": "dfbab48c07032387f70fa876fdc63740ef403b2882651fb63e3840289443a095", + "status": "running", + "oom_killed": false, + "anon_bytes": 139509760, + "cpu_throttled_usec": 792708, + "cpu_usage_usec": 1578736, + "file_bytes": 0, + "memory_current_bytes": 142069760, + "memory_limit_bytes": 1610612736, + "memory_peak_bytes": 142802944, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "swap_current_bytes": 0, + "swap_limit_bytes": 0 + }, + { + "container_id": "34cfdc532bb473c85a848f19f551b57f5653018dc671123c1379440b5166b262", + "status": "running", + "oom_killed": false, + "anon_bytes": 139243520, + "cpu_throttled_usec": 647106, + "cpu_usage_usec": 1634895, + "file_bytes": 0, + "memory_current_bytes": 141639680, + "memory_limit_bytes": 1610612736, + "memory_peak_bytes": 142622720, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "swap_current_bytes": 0, + "swap_limit_bytes": 0 + } + ], + "resources_after": [ + { + "container_id": "dfbab48c07032387f70fa876fdc63740ef403b2882651fb63e3840289443a095", + "status": "running", + "oom_killed": false, + "anon_bytes": 139694080, + "cpu_throttled_usec": 884167, + "cpu_usage_usec": 2231629, + "file_bytes": 0, + "memory_current_bytes": 142241792, + "memory_limit_bytes": 1610612736, + "memory_peak_bytes": 145375232, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "swap_current_bytes": 0, + "swap_limit_bytes": 0, + "cpu_mean_percent_one_core": 26.62 + }, + { + "container_id": "34cfdc532bb473c85a848f19f551b57f5653018dc671123c1379440b5166b262", + "status": "running", + "oom_killed": false, + "anon_bytes": 139481088, + "cpu_throttled_usec": 647106, + "cpu_usage_usec": 2275796, + "file_bytes": 0, + "memory_current_bytes": 142204928, + "memory_limit_bytes": 1610612736, + "memory_peak_bytes": 143245312, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "swap_current_bytes": 0, + "swap_limit_bytes": 0, + "cpu_mean_percent_one_core": 26.13 + } + ], + "resource_observation_seconds": 2.453, + "docker_stats_samples": [ + { + "BlockIO": "0B / 0B", + "CPUPerc": "22.46%", + "Container": "dfbab48c07032387f70fa876fdc63740ef403b2882651fb63e3840289443a095", + "ID": "dfbab48c0703", + "MemPerc": "8.69%", + "MemUsage": "133.4MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "5.36MB / 5.38MB", + "PIDs": "7", + "elapsed_seconds": 2.703 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "41.65%", + "Container": "34cfdc532bb473c85a848f19f551b57f5653018dc671123c1379440b5166b262", + "ID": "34cfdc532bb4", + "MemPerc": "8.69%", + "MemUsage": "133.5MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "3.62MB / 3.64MB", + "PIDs": "7", + "elapsed_seconds": 2.703 + } + ] + }, + { + "fixture": { + "name": "rgba16_40mp", + "content_type": "image/png", + "width": 8000, + "height": 5000, + "bit_depth": 16, + "color_type": 6, + "size_bytes": 1716094, + "sha256": "45c5e622f416bd35846b31597bf6ee46d31279b5d32f15f09287e1e527874ad8" + }, + "concurrency": 10, + "max_client_in_flight": 10, + "repeats": 3, + "warmup": "2 sample.png uploads after fresh API start", + "batches": [ + { + "repeat": 1, + "seconds": 6.094, + "successes": 10, + "errors": 0 + }, + { + "repeat": 2, + "seconds": 4.125, + "successes": 10, + "errors": 0 + }, + { + "repeat": 3, + "seconds": 4.015, + "successes": 10, + "errors": 0 + } + ], + "successful_requests": 30, + "error_count": 0, + "error_fraction": 0.0, + "errors": [], + "http_upload_seconds": 14.234, + "successful_rps": 2.108, + "latency_success_seconds": { + "p50": 3.062, + "p95": 5.5607, + "p99": 5.9024 + }, + "serving_instances": 2, + "downloads_verified": 30, + "resources_before": [ + { + "container_id": "5c014f5a488efa61e8dd37bd59800dfb0a39c575b264d579160de802515760e7", + "status": "running", + "oom_killed": false, + "anon_bytes": 139333632, + "cpu_throttled_usec": 792190, + "cpu_usage_usec": 1572654, + "file_bytes": 0, + "memory_current_bytes": 142147584, + "memory_limit_bytes": 1610612736, + "memory_peak_bytes": 142700544, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "swap_current_bytes": 0, + "swap_limit_bytes": 0 + }, + { + "container_id": "50051d780dd6f07b92f69da60977f7e623604c91f8b6fa183b05caa8cbe38d4a", + "status": "running", + "oom_killed": false, + "anon_bytes": 139382784, + "cpu_throttled_usec": 577905, + "cpu_usage_usec": 1453518, + "file_bytes": 0, + "memory_current_bytes": 142049280, + "memory_limit_bytes": 1610612736, + "memory_peak_bytes": 142741504, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "swap_current_bytes": 0, + "swap_limit_bytes": 0 + } + ], + "resources_after": [ + { + "container_id": "5c014f5a488efa61e8dd37bd59800dfb0a39c575b264d579160de802515760e7", + "status": "running", + "oom_killed": false, + "anon_bytes": 709836800, + "cpu_throttled_usec": 1436976, + "cpu_usage_usec": 14357043, + "file_bytes": 0, + "memory_current_bytes": 713990144, + "memory_limit_bytes": 1610612736, + "memory_peak_bytes": 776294400, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "swap_current_bytes": 0, + "swap_limit_bytes": 0, + "cpu_mean_percent_one_core": 83.49 + }, + { + "container_id": "50051d780dd6f07b92f69da60977f7e623604c91f8b6fa183b05caa8cbe38d4a", + "status": "running", + "oom_killed": false, + "anon_bytes": 708927488, + "cpu_throttled_usec": 1356129, + "cpu_usage_usec": 14109676, + "file_bytes": 0, + "memory_current_bytes": 712798208, + "memory_limit_bytes": 1610612736, + "memory_peak_bytes": 776699904, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "swap_current_bytes": 0, + "swap_limit_bytes": 0, + "cpu_mean_percent_one_core": 82.65 + } + ], + "resource_observation_seconds": 15.313, + "docker_stats_samples": [ + { + "BlockIO": "0B / 0B", + "CPUPerc": "98.30%", + "Container": "5c014f5a488efa61e8dd37bd59800dfb0a39c575b264d579160de802515760e7", + "ID": "5c014f5a488e", + "MemPerc": "28.65%", + "MemUsage": "440.1MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "4.01MB / 1.9MB", + "PIDs": "8", + "elapsed_seconds": 2.719 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "81.67%", + "Container": "50051d780dd6f07b92f69da60977f7e623604c91f8b6fa183b05caa8cbe38d4a", + "ID": "50051d780dd6", + "MemPerc": "28.46%", + "MemUsage": "437.2MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "2.38MB / 164kB", + "PIDs": "7", + "elapsed_seconds": 2.719 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "98.92%", + "Container": "5c014f5a488efa61e8dd37bd59800dfb0a39c575b264d579160de802515760e7", + "ID": "5c014f5a488e", + "MemPerc": "44.25%", + "MemUsage": "679.7MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "5.61MB / 3.63MB", + "PIDs": "8", + "elapsed_seconds": 4.813 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "100.34%", + "Container": "50051d780dd6f07b92f69da60977f7e623604c91f8b6fa183b05caa8cbe38d4a", + "ID": "50051d780dd6", + "MemPerc": "44.31%", + "MemUsage": "680.6MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "5.59MB / 3.63MB", + "PIDs": "7", + "elapsed_seconds": 4.813 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "81.43%", + "Container": "5c014f5a488efa61e8dd37bd59800dfb0a39c575b264d579160de802515760e7", + "ID": "5c014f5a488e", + "MemPerc": "44.20%", + "MemUsage": "679MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "9.32MB / 8.82MB", + "PIDs": "8", + "elapsed_seconds": 6.906 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "74.23%", + "Container": "50051d780dd6f07b92f69da60977f7e623604c91f8b6fa183b05caa8cbe38d4a", + "ID": "50051d780dd6", + "MemPerc": "44.15%", + "MemUsage": "678.1MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "9.51MB / 8.82MB", + "PIDs": "8", + "elapsed_seconds": 6.906 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "98.89%", + "Container": "5c014f5a488efa61e8dd37bd59800dfb0a39c575b264d579160de802515760e7", + "ID": "5c014f5a488e", + "MemPerc": "44.29%", + "MemUsage": "680.2MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "14.3MB / 12.3MB", + "PIDs": "8", + "elapsed_seconds": 8.984 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "99.03%", + "Container": "50051d780dd6f07b92f69da60977f7e623604c91f8b6fa183b05caa8cbe38d4a", + "ID": "50051d780dd6", + "MemPerc": "44.24%", + "MemUsage": "679.6MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "14.2MB / 12.3MB", + "PIDs": "8", + "elapsed_seconds": 8.984 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "69.82%", + "Container": "5c014f5a488efa61e8dd37bd59800dfb0a39c575b264d579160de802515760e7", + "ID": "5c014f5a488e", + "MemPerc": "44.30%", + "MemUsage": "680.5MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "19.5MB / 17.5MB", + "PIDs": "8", + "elapsed_seconds": 11.094 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "74.14%", + "Container": "50051d780dd6f07b92f69da60977f7e623604c91f8b6fa183b05caa8cbe38d4a", + "ID": "50051d780dd6", + "MemPerc": "44.27%", + "MemUsage": "679.9MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "19.7MB / 17.5MB", + "PIDs": "8", + "elapsed_seconds": 11.094 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "97.05%", + "Container": "5c014f5a488efa61e8dd37bd59800dfb0a39c575b264d579160de802515760e7", + "ID": "5c014f5a488e", + "MemPerc": "44.31%", + "MemUsage": "680.6MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "22.9MB / 20.9MB", + "PIDs": "8", + "elapsed_seconds": 13.172 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "99.17%", + "Container": "50051d780dd6f07b92f69da60977f7e623604c91f8b6fa183b05caa8cbe38d4a", + "ID": "50051d780dd6", + "MemPerc": "44.27%", + "MemUsage": "679.9MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "22.9MB / 20.9MB", + "PIDs": "8", + "elapsed_seconds": 13.172 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "58.16%", + "Container": "5c014f5a488efa61e8dd37bd59800dfb0a39c575b264d579160de802515760e7", + "ID": "5c014f5a488e", + "MemPerc": "44.17%", + "MemUsage": "678.4MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "26.1MB / 26.1MB", + "PIDs": "8", + "elapsed_seconds": 15.266 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "41.13%", + "Container": "50051d780dd6f07b92f69da60977f7e623604c91f8b6fa183b05caa8cbe38d4a", + "ID": "50051d780dd6", + "MemPerc": "44.11%", + "MemUsage": "677.5MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "26.1MB / 26.1MB", + "PIDs": "8", + "elapsed_seconds": 15.266 + } + ] + }, + { + "fixture": { + "name": "rgba16_wide_40mp", + "content_type": "image/png", + "width": 40000000, + "height": 1, + "bit_depth": 16, + "color_type": 6, + "size_bytes": 1707450, + "sha256": "d9c6f5873a4f6efe7464b4c7beeaba780a5b218534f66246717f89446fffb5ff" + }, + "concurrency": 10, + "max_client_in_flight": 10, + "repeats": 3, + "warmup": "2 sample.png uploads after fresh API start", + "batches": [ + { + "repeat": 1, + "seconds": 8.39, + "successes": 10, + "errors": 0 + }, + { + "repeat": 2, + "seconds": 6.672, + "successes": 10, + "errors": 0 + }, + { + "repeat": 3, + "seconds": 6.516, + "successes": 10, + "errors": 0 + } + ], + "successful_requests": 30, + "error_count": 0, + "error_fraction": 0.0, + "errors": [], + "http_upload_seconds": 21.578, + "successful_rps": 1.39, + "latency_success_seconds": { + "p50": 4.1795, + "p95": 7.5697, + "p99": 8.1507 + }, + "serving_instances": 2, + "downloads_verified": 30, + "resources_before": [ + { + "container_id": "c83812f6504672907472a4c79cb07389874fba69dae5aff684de1908c081e1c4", + "status": "running", + "oom_killed": false, + "anon_bytes": 139276288, + "cpu_throttled_usec": 736718, + "cpu_usage_usec": 1587908, + "file_bytes": 0, + "memory_current_bytes": 142000128, + "memory_limit_bytes": 1610612736, + "memory_peak_bytes": 142598144, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "swap_current_bytes": 0, + "swap_limit_bytes": 0 + }, + { + "container_id": "9172f3eadae78761c5f96fd8f0c9ea7a0513dbe157419b31073b0f073fbf4dab", + "status": "running", + "oom_killed": false, + "anon_bytes": 139395072, + "cpu_throttled_usec": 958549, + "cpu_usage_usec": 1658406, + "file_bytes": 0, + "memory_current_bytes": 142180352, + "memory_limit_bytes": 1610612736, + "memory_peak_bytes": 142790656, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "swap_current_bytes": 0, + "swap_limit_bytes": 0 + } + ], + "resources_after": [ + { + "container_id": "c83812f6504672907472a4c79cb07389874fba69dae5aff684de1908c081e1c4", + "status": "running", + "oom_killed": false, + "anon_bytes": 1049526272, + "cpu_throttled_usec": 2856851, + "cpu_usage_usec": 21291942, + "file_bytes": 0, + "memory_current_bytes": 1054314496, + "memory_limit_bytes": 1610612736, + "memory_peak_bytes": 1291698176, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "swap_current_bytes": 0, + "swap_limit_bytes": 0, + "cpu_mean_percent_one_core": 86.85 + }, + { + "container_id": "9172f3eadae78761c5f96fd8f0c9ea7a0513dbe157419b31073b0f073fbf4dab", + "status": "running", + "oom_killed": false, + "anon_bytes": 1040797696, + "cpu_throttled_usec": 3317042, + "cpu_usage_usec": 21343346, + "file_bytes": 0, + "memory_current_bytes": 1046347776, + "memory_limit_bytes": 1610612736, + "memory_peak_bytes": 1292255232, + "oom_events": 0, + "oom_kills": 0, + "shmem_bytes": 0, + "swap_current_bytes": 0, + "swap_limit_bytes": 0, + "cpu_mean_percent_one_core": 86.76 + } + ], + "resource_observation_seconds": 22.688, + "docker_stats_samples": [ + { + "BlockIO": "0B / 0B", + "CPUPerc": "75.88%", + "Container": "c83812f6504672907472a4c79cb07389874fba69dae5aff684de1908c081e1c4", + "ID": "c83812f65046", + "MemPerc": "28.73%", + "MemUsage": "441.3MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "2.38MB / 164kB", + "PIDs": "7", + "elapsed_seconds": 2.734 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "99.42%", + "Container": "9172f3eadae78761c5f96fd8f0c9ea7a0513dbe157419b31073b0f073fbf4dab", + "ID": "9172f3eadae7", + "MemPerc": "46.81%", + "MemUsage": "718.9MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "2.37MB / 164kB", + "PIDs": "7", + "elapsed_seconds": 2.734 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "99.32%", + "Container": "c83812f6504672907472a4c79cb07389874fba69dae5aff684de1908c081e1c4", + "ID": "c83812f65046", + "MemPerc": "63.05%", + "MemUsage": "968.5MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "3.97MB / 1.89MB", + "PIDs": "7", + "elapsed_seconds": 4.813 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "99.58%", + "Container": "9172f3eadae78761c5f96fd8f0c9ea7a0513dbe157419b31073b0f073fbf4dab", + "ID": "9172f3eadae7", + "MemPerc": "63.51%", + "MemUsage": "975.4MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "3.96MB / 1.89MB", + "PIDs": "7", + "elapsed_seconds": 4.813 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "102.32%", + "Container": "c83812f6504672907472a4c79cb07389874fba69dae5aff684de1908c081e1c4", + "ID": "c83812f65046", + "MemPerc": "66.49%", + "MemUsage": "1021MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "7.15MB / 5.33MB", + "PIDs": "7", + "elapsed_seconds": 6.906 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "98.80%", + "Container": "9172f3eadae78761c5f96fd8f0c9ea7a0513dbe157419b31073b0f073fbf4dab", + "ID": "9172f3eadae7", + "MemPerc": "63.26%", + "MemUsage": "971.7MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "7.15MB / 5.33MB", + "PIDs": "7", + "elapsed_seconds": 6.906 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "80.92%", + "Container": "c83812f6504672907472a4c79cb07389874fba69dae5aff684de1908c081e1c4", + "ID": "c83812f65046", + "MemPerc": "66.36%", + "MemUsage": "1019MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "8.74MB / 8.77MB", + "PIDs": "7", + "elapsed_seconds": 9.063 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "92.71%", + "Container": "9172f3eadae78761c5f96fd8f0c9ea7a0513dbe157419b31073b0f073fbf4dab", + "ID": "9172f3eadae7", + "MemPerc": "63.38%", + "MemUsage": "973.5MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "8.74MB / 8.77MB", + "PIDs": "7", + "elapsed_seconds": 9.063 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "99.19%", + "Container": "c83812f6504672907472a4c79cb07389874fba69dae5aff684de1908c081e1c4", + "ID": "c83812f65046", + "MemPerc": "63.41%", + "MemUsage": "974MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "12.6MB / 10.5MB", + "PIDs": "7", + "elapsed_seconds": 11.094 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "98.89%", + "Container": "9172f3eadae78761c5f96fd8f0c9ea7a0513dbe157419b31073b0f073fbf4dab", + "ID": "9172f3eadae7", + "MemPerc": "60.03%", + "MemUsage": "922.1MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "12.6MB / 10.5MB", + "PIDs": "7", + "elapsed_seconds": 11.094 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "99.63%", + "Container": "c83812f6504672907472a4c79cb07389874fba69dae5aff684de1908c081e1c4", + "ID": "c83812f65046", + "MemPerc": "60.30%", + "MemUsage": "926.3MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "14.2MB / 12.2MB", + "PIDs": "7", + "elapsed_seconds": 13.172 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "99.86%", + "Container": "9172f3eadae78761c5f96fd8f0c9ea7a0513dbe157419b31073b0f073fbf4dab", + "ID": "9172f3eadae7", + "MemPerc": "62.40%", + "MemUsage": "958.4MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "15.7MB / 13.9MB", + "PIDs": "7", + "elapsed_seconds": 13.172 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "98.98%", + "Container": "c83812f6504672907472a4c79cb07389874fba69dae5aff684de1908c081e1c4", + "ID": "c83812f65046", + "MemPerc": "63.41%", + "MemUsage": "974MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "17.3MB / 15.7MB", + "PIDs": "7", + "elapsed_seconds": 15.266 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "99.21%", + "Container": "9172f3eadae78761c5f96fd8f0c9ea7a0513dbe157419b31073b0f073fbf4dab", + "ID": "9172f3eadae7", + "MemPerc": "64.87%", + "MemUsage": "996.4MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "17.3MB / 15.7MB", + "PIDs": "7", + "elapsed_seconds": 15.266 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "99.45%", + "Container": "c83812f6504672907472a4c79cb07389874fba69dae5aff684de1908c081e1c4", + "ID": "c83812f65046", + "MemPerc": "68.55%", + "MemUsage": "1.028GiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "21.2MB / 19.1MB", + "PIDs": "7", + "elapsed_seconds": 17.359 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "98.81%", + "Container": "9172f3eadae78761c5f96fd8f0c9ea7a0513dbe157419b31073b0f073fbf4dab", + "ID": "9172f3eadae7", + "MemPerc": "69.35%", + "MemUsage": "1.04GiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "21.2MB / 19.1MB", + "PIDs": "7", + "elapsed_seconds": 17.359 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "98.57%", + "Container": "c83812f6504672907472a4c79cb07389874fba69dae5aff684de1908c081e1c4", + "ID": "c83812f65046", + "MemPerc": "66.23%", + "MemUsage": "1017MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "22.8MB / 20.9MB", + "PIDs": "7", + "elapsed_seconds": 19.438 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "98.71%", + "Container": "9172f3eadae78761c5f96fd8f0c9ea7a0513dbe157419b31073b0f073fbf4dab", + "ID": "9172f3eadae7", + "MemPerc": "66.49%", + "MemUsage": "1021MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "22.8MB / 20.8MB", + "PIDs": "8", + "elapsed_seconds": 19.438 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "97.65%", + "Container": "c83812f6504672907472a4c79cb07389874fba69dae5aff684de1908c081e1c4", + "ID": "c83812f65046", + "MemPerc": "65.42%", + "MemUsage": "1005MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "25.9MB / 24.3MB", + "PIDs": "7", + "elapsed_seconds": 21.531 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "98.30%", + "Container": "9172f3eadae78761c5f96fd8f0c9ea7a0513dbe157419b31073b0f073fbf4dab", + "ID": "9172f3eadae7", + "MemPerc": "65.14%", + "MemUsage": "1001MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "25.9MB / 24.3MB", + "PIDs": "8", + "elapsed_seconds": 21.531 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.22%", + "Container": "c83812f6504672907472a4c79cb07389874fba69dae5aff684de1908c081e1c4", + "ID": "c83812f65046", + "MemPerc": "65.07%", + "MemUsage": "999.5MiB / 1.5GiB", + "Name": "messenger-lab4-api-1", + "NetIO": "26MB / 26MB", + "PIDs": "7", + "elapsed_seconds": 23.625 + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "4.80%", + "Container": "9172f3eadae78761c5f96fd8f0c9ea7a0513dbe157419b31073b0f073fbf4dab", + "ID": "9172f3eadae7", + "MemPerc": "64.80%", + "MemUsage": "995.4MiB / 1.5GiB", + "Name": "messenger-lab4-api-2", + "NetIO": "25.9MB / 26MB", + "PIDs": "8", + "elapsed_seconds": 23.625 + } + ] + } + ] +} \ No newline at end of file diff --git a/evidence/upload-memory.txt b/evidence/upload-memory.txt new file mode 100644 index 00000000..5185f82e --- /dev/null +++ b/evidence/upload-memory.txt @@ -0,0 +1,5 @@ +{"profile": "rgb8_1mp", "successes": 30, "errors": 0, "peak_MiB": [139.09, 139.53], "oom_killed": [false, false]} +{"profile": "jpeg_1mp", "successes": 30, "errors": 0, "peak_MiB": [138.64, 136.61], "oom_killed": [false, false]} +{"profile": "rgba16_40mp", "successes": 30, "errors": 0, "peak_MiB": [740.33, 740.72], "oom_killed": [false, false]} +{"profile": "rgba16_wide_40mp", "successes": 30, "errors": 0, "peak_MiB": [1231.86, 1232.39], "oom_killed": [false, false]} +PASS: upload concurrency/memory experiment and original-byte verification diff --git a/evidence/upload-validation.txt b/evidence/upload-validation.txt new file mode 100644 index 00000000..53cbaa5a --- /dev/null +++ b/evidence/upload-validation.txt @@ -0,0 +1,28 @@ +=== RUN TestOriginalPreserved +=== RUN TestOriginalPreserved/png +=== RUN TestOriginalPreserved/jpeg +--- PASS: TestOriginalPreserved (0.01s) + --- PASS: TestOriginalPreserved/png (0.00s) + --- PASS: TestOriginalPreserved/jpeg (0.00s) +=== RUN TestInvalidFilesAndCleanup +=== RUN TestInvalidFilesAndCleanup/empty +=== RUN TestInvalidFilesAndCleanup/text +=== RUN TestInvalidFilesAndCleanup/spoofed_MIME +=== RUN TestInvalidFilesAndCleanup/unsupported_MIME +=== RUN TestInvalidFilesAndCleanup/truncated_after_header +=== RUN TestInvalidFilesAndCleanup/pixel_limit_before_decode +--- PASS: TestInvalidFilesAndCleanup (0.02s) + --- PASS: TestInvalidFilesAndCleanup/empty (0.00s) + --- PASS: TestInvalidFilesAndCleanup/text (0.00s) + --- PASS: TestInvalidFilesAndCleanup/spoofed_MIME (0.00s) + --- PASS: TestInvalidFilesAndCleanup/unsupported_MIME (0.00s) + --- PASS: TestInvalidFilesAndCleanup/truncated_after_header (0.00s) + --- PASS: TestInvalidFilesAndCleanup/pixel_limit_before_decode (0.00s) +=== RUN TestByteLimitStopsInfiniteStream +--- PASS: TestByteLimitStopsInfiniteStream (0.02s) +=== RUN TestFilename +--- PASS: TestFilename (0.00s) +=== RUN TestCancellationAndReadFailure +--- PASS: TestCancellationAndReadFailure (0.00s) +PASS +ok gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/upload 0.946s diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..3ac2e6ab --- /dev/null +++ b/go.mod @@ -0,0 +1,31 @@ +module gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork + +go 1.26.3 + +require ( + github.com/google/uuid v1.6.0 + github.com/jackc/pgx/v5 v5.7.6 + github.com/minio/minio-go/v7 v7.0.90 + github.com/redis/go-redis/v9 v9.7.3 + golang.org/x/crypto v0.37.0 +) + +require ( + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/go-ini/ini v1.67.0 // indirect + github.com/goccy/go-json v0.10.5 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect + github.com/klauspost/compress v1.18.0 // indirect + github.com/klauspost/cpuid/v2 v2.2.10 // indirect + github.com/minio/crc64nvme v1.0.1 // indirect + github.com/minio/md5-simd v1.1.2 // indirect + github.com/rs/xid v1.6.0 // indirect + golang.org/x/net v0.38.0 // indirect + golang.org/x/sync v0.13.0 // indirect + golang.org/x/sys v0.32.0 // indirect + golang.org/x/text v0.24.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..214c5a1b --- /dev/null +++ b/go.sum @@ -0,0 +1,63 @@ +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= +github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= +github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.7.6 h1:rWQc5FwZSPX58r1OQmkuaNicxdmExaEz5A2DO2hUuTk= +github.com/jackc/pgx/v5 v5.7.6/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= +github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= +github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE= +github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= +github.com/minio/crc64nvme v1.0.1 h1:DHQPrYPdqK7jQG/Ls5CTBZWeex/2FMS3G5XGkycuFrY= +github.com/minio/crc64nvme v1.0.1/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg= +github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= +github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= +github.com/minio/minio-go/v7 v7.0.90 h1:TmSj1083wtAD0kEYTx7a5pFsv3iRYMsOJ6A4crjA1lE= +github.com/minio/minio-go/v7 v7.0.90/go.mod h1:uvMUcGrpgeSAAI6+sD3818508nUyMULw94j2Nxku/Go= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/redis/go-redis/v9 v9.7.3 h1:YpPyAayJV+XErNsatSElgRZZVCwXX9QzkKYNvO7x0wM= +github.com/redis/go-redis/v9 v9.7.3/go.mod h1:bGUrSggJ9X9GUmZpZNEOQKaANxSGgOEBRltRTZHSvrA= +github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU= +github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE= +golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc= +golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8= +golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8= +golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610= +golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20= +golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0= +golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/infra/haproxy.cfg b/infra/haproxy.cfg new file mode 100644 index 00000000..18d7c1e3 --- /dev/null +++ b/infra/haproxy.cfg @@ -0,0 +1,35 @@ +global + log stdout format raw local0 + maxconn 512 + +defaults + log global + mode http + option httplog + timeout connect 1s + timeout client 65s + timeout server 65s + timeout check 1s + retries 0 + +resolvers docker + nameserver dns 127.0.0.11:53 + resolve_retries 3 + timeout resolve 1s + timeout retry 1s + hold valid 1s + hold obsolete 1s + hold nx 1s + hold timeout 1s + hold refused 1s + +frontend public + bind :8080 + default_backend api + +backend api + balance roundrobin + option httpchk + http-check send meth GET uri /health/ready ver HTTP/1.1 hdr Host api + http-check expect status 200 + server-template replica 10 api:8080 check inter 1s fall 2 rise 2 resolvers docker resolve-prefer ipv4 init-addr none diff --git a/infra/s3/Dockerfile b/infra/s3/Dockerfile new file mode 100644 index 00000000..3a7a1b19 --- /dev/null +++ b/infra/s3/Dockerfile @@ -0,0 +1,19 @@ +FROM golang:1.26.3 AS server-build +ENV CGO_ENABLED=0 GOTOOLCHAIN=local +RUN git clone --depth 1 --branch RELEASE.2025-04-22T22-12-26Z https://github.com/minio/minio.git /src +WORKDIR /src +RUN go build -trimpath -o /out/minio . + +FROM golang:1.26.3 AS client-build +ENV CGO_ENABLED=0 GOTOOLCHAIN=local +RUN git clone --depth 1 --branch RELEASE.2025-04-16T18-13-26Z https://github.com/minio/mc.git /src +WORKDIR /src +RUN go build -trimpath -o /out/mc . + +FROM alpine:3.22.2 AS server +COPY --from=server-build /out/minio /usr/local/bin/minio +ENTRYPOINT ["minio"] + +FROM alpine:3.22.2 AS client +COPY --from=client-build /out/mc /usr/local/bin/mc +ENTRYPOINT ["mc"] diff --git a/infra/s3/api-policy.json b/infra/s3/api-policy.json new file mode 100644 index 00000000..2c13eefc --- /dev/null +++ b/infra/s3/api-policy.json @@ -0,0 +1,15 @@ +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": ["s3:GetBucketLocation", "s3:ListBucket"], + "Resource": ["arn:aws:s3:::BUCKET_NAME"] + }, + { + "Effect": "Allow", + "Action": ["s3:PutObject", "s3:GetObject", "s3:DeleteObject"], + "Resource": ["arn:aws:s3:::BUCKET_NAME/attachments/*"] + } + ] +} diff --git a/infra/s3/bootstrap.sh b/infra/s3/bootstrap.sh new file mode 100644 index 00000000..842da2c4 --- /dev/null +++ b/infra/s3/bootstrap.sh @@ -0,0 +1,31 @@ +#!/bin/sh +set -eu +# Do not enable shell tracing: arguments below contain credentials. +bucket="${S3_BUCKET:?S3_BUCKET is required}" +case "$bucket" in + ''|*[!a-z0-9.-]*) echo 'Invalid bucket name' >&2; exit 1 ;; +esac +root_user="$(cat /run/secrets/s3_root_user)" +root_password="$(cat /run/secrets/s3_root_password)" +api_user="$(cat /run/secrets/s3_access_key_id)" +api_password="$(cat /run/secrets/s3_secret_access_key)" + +# Bound startup wait to 60 attempts. Also retries if the server is not ready +# when Docker reports that its process has started. +attempt=0 +until mc alias set local http://s3:9000 "$root_user" "$root_password" >/dev/null 2>&1; do + attempt=$((attempt + 1)) + if [ "$attempt" -ge 60 ]; then + echo 'S3 bootstrap: server unavailable' >&2 + exit 1 + fi + sleep 2 +done +mc mb --ignore-existing "local/$bucket" >/dev/null +mc anonymous set none "local/$bucket" >/dev/null +sed "s/BUCKET_NAME/$bucket/g" /bootstrap/api-policy.json > /tmp/api-policy.json +mc admin policy create local attachments-api /tmp/api-policy.json >/dev/null +# Reapplying with the same local credentials preserves the user identity. +mc admin user add local "$api_user" "$api_password" >/dev/null +mc admin policy attach local attachments-api --user "$api_user" >/dev/null +echo 'PASS: bucket configured private; API user policy applied.' diff --git a/infra/s3/verify.sh b/infra/s3/verify.sh new file mode 100644 index 00000000..6044b5d2 --- /dev/null +++ b/infra/s3/verify.sh @@ -0,0 +1,35 @@ +#!/bin/sh +set -eu +# Synthetic fixture only. Credentials are not printed. +bucket="${S3_BUCKET:?}" +key=attachments/infrastructure-check.txt +mc alias set api http://s3:9000 "$(cat /run/secrets/s3_access_key_id)" "$(cat /run/secrets/s3_secret_access_key)" >/dev/null 2>&1 +printf 'lab4-private-storage-check\n' > /tmp/expected +if [ "${1:-check}" = seed ]; then + mc cp /tmp/expected "api/$bucket/$key" >/dev/null + echo 'PASS: API credentials can upload fixture.' + exit 0 +fi +mc cat "api/$bucket/$key" > /tmp/actual +cmp /tmp/expected /tmp/actual +echo 'PASS: API credentials read original bytes after bootstrap/restart.' + +# BusyBox wget makes a real unsigned HTTP request, bypassing the application. +if wget -S -O /tmp/unsigned "http://s3:9000/$bucket/$key" 2>/tmp/http-status; then + echo 'FAIL: anonymous download succeeded' >&2; exit 1 +fi +grep -q '403 Forbidden' /tmp/http-status +echo 'PASS: unsigned GET of existing object returns HTTP 403.' + +if mc --json cp /tmp/expected "api/$bucket/outside-api-prefix.txt" >/tmp/denied 2>&1; then + echo 'FAIL: API wrote outside permitted prefix' >&2; exit 1 +fi +grep -qi 'AccessDenied\|Access Denied\|Insufficient permissions' /tmp/denied +echo 'PASS: API cannot write outside attachments prefix.' +# mc admin info can emit a JSON error while returning exit code 0. +mc --json admin info api >/tmp/denied 2>&1 || true +grep -q '"status":"error"' /tmp/denied +grep -qi 'AccessDenied\|Access Denied' /tmp/denied +echo 'PASS: API cannot access administrative server information.' +mc rm "api/$bucket/$key" >/dev/null +echo 'PASS: API can delete its test object; fixture removed.' diff --git a/internal/cache/profiles.go b/internal/cache/profiles.go new file mode 100644 index 00000000..a540318b --- /dev/null +++ b/internal/cache/profiles.go @@ -0,0 +1,56 @@ +package cache + +import ( + "context" + "encoding/json" + "time" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" + "github.com/redis/go-redis/v9" +) + +const ProfileTTL = 60 * time.Second + +type Profiles struct{ client *redis.Client } + +func NewProfiles(address string) *Profiles { + return &Profiles{client: redis.NewClient(&redis.Options{ + Addr: address, + Protocol: 2, + MaxRetries: -1, + DialTimeout: 200 * time.Millisecond, + ReadTimeout: 200 * time.Millisecond, + WriteTimeout: 200 * time.Millisecond, + PoolTimeout: 200 * time.Millisecond, + ContextTimeoutEnabled: true, + PoolSize: 10, + MaxActiveConns: 10, + MinIdleConns: 0, + })} +} + +func (p *Profiles) Close() error { return p.client.Close() } + +func (p *Profiles) Get(ctx context.Context, id string) (model.User, bool) { + ctx, cancel := context.WithTimeout(ctx, 250*time.Millisecond) + defer cancel() + raw, err := p.client.Get(ctx, "profile:v1:"+id).Bytes() + if err != nil { + return model.User{}, false + } + var user model.User + if json.Unmarshal(raw, &user) != nil || user.ID != id || user.Username == "" || user.DisplayName == "" || user.CreatedAt.IsZero() { + return model.User{}, false + } + return user, true +} + +func (p *Profiles) Set(ctx context.Context, user model.User) { + raw, err := json.Marshal(user) + if err != nil { + return + } + ctx, cancel := context.WithTimeout(ctx, 250*time.Millisecond) + defer cancel() + _ = p.client.Set(ctx, "profile:v1:"+user.ID, raw, ProfileTTL).Err() +} diff --git a/internal/httpapi/attachments.go b/internal/httpapi/attachments.go new file mode 100644 index 00000000..3ee57cdd --- /dev/null +++ b/internal/httpapi/attachments.go @@ -0,0 +1,92 @@ +package httpapi + +import ( + "context" + "io" + "log" + "mime" + "net/http" + "net/url" + "strconv" + "strings" + "time" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" +) + +func createAttachment(w http.ResponseWriter, r *http.Request, attachments *service.Attachments, chatID string) { + if !requireMethod(w, r, http.MethodPost) { + return + } + if attachments == nil { + writeAppError(w, store.ErrUnavailable) + return + } + query, err := url.ParseQuery(r.URL.RawQuery) + if err != nil || len(query["filename"]) != 1 { + writeAppError(w, service.ErrInvalidRequest) + return + } + ctx, cancel := context.WithTimeout(r.Context(), 45*time.Second) + defer cancel() + a, err := attachments.Create(ctx, userFromContext(ctx).ID, chatID, query.Get("filename"), r.Header.Get("Content-Type"), r.Body, r.ContentLength) + if err != nil { + writeAppError(w, err) + return + } + writeJSON(w, http.StatusCreated, a) +} + +func attachmentRoutes(w http.ResponseWriter, r *http.Request, attachments *service.Attachments) { + if !requireMethod(w, r, http.MethodGet) { + return + } + if attachments == nil { + writeAppError(w, store.ErrUnavailable) + return + } + parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v1/attachments/"), "/") + if parts[0] == "" || len(parts) > 2 || (len(parts) == 2 && parts[1] != "content") { + writeAppError(w, store.ErrNotFound) + return + } + a, err := attachments.Get(r.Context(), userFromContext(r.Context()).ID, parts[0]) + if err != nil { + writeAppError(w, err) + return + } + if len(parts) == 1 { + writeJSON(w, http.StatusOK, a) + return + } + query, err := url.ParseQuery(r.URL.RawQuery) + if err != nil { + writeAppError(w, service.ErrInvalidRequest) + return + } + if len(query["variant"]) > 1 { + writeAppError(w, service.ErrInvalidRequest) + return + } + if variant := query.Get("variant"); variant != "" && variant != "original" { + writeAppError(w, store.ErrNotFound) + return + } + original, err := attachments.Open(r.Context(), a) + if err != nil { + writeAppError(w, err) + return + } + defer original.Body.Close() + w.Header().Set("Content-Type", a.ContentType) + w.Header().Set("Content-Length", strconv.FormatInt(a.SizeBytes, 10)) + w.Header().Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": a.Filename})) + w.Header().Set("X-Content-Type-Options", "nosniff") + w.Header().Set("Cache-Control", "private, no-store") + if _, err := io.CopyBuffer(w, original.Body, make([]byte, 32*1024)); err != nil { + log.Print("attachment download stream interrupted") + // Headers may already be sent: abort rather than append a JSON error to bytes. + panic(http.ErrAbortHandler) + } +} diff --git a/internal/httpapi/auth.go b/internal/httpapi/auth.go new file mode 100644 index 00000000..4cf0fb80 --- /dev/null +++ b/internal/httpapi/auth.go @@ -0,0 +1,27 @@ +package httpapi + +import ( + "context" + "net/http" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" +) + +type userContextKey struct{} + +func (api *API) authenticate(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + user, err := api.messenger.CurrentUser(r.Header.Get("X-User-Id")) + if err != nil { + writeAppError(w, err) + return + } + + ctx := context.WithValue(r.Context(), userContextKey{}, user) + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} + +func userFromContext(ctx context.Context) model.User { + return ctx.Value(userContextKey{}).(model.User) +} diff --git a/internal/httpapi/chats.go b/internal/httpapi/chats.go new file mode 100644 index 00000000..2176542b --- /dev/null +++ b/internal/httpapi/chats.go @@ -0,0 +1,170 @@ +package httpapi + +import ( + "net/http" + "strconv" + "strings" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service" +) + +type createChatRequest struct { + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` +} + +type createMessageRequest struct { + Text string `json:"text"` + AttachmentIDs []string `json:"attachment_ids"` +} + +func (api *API) chatRoutes(w http.ResponseWriter, r *http.Request) { + actor := userFromContext(r.Context()) + + if r.URL.Path == "/api/v1/chats" { + switch r.Method { + case http.MethodPost: + var body createChatRequest + if !readJSON(w, r, &body, "title", "member_ids") { + return + } + + chat, err := api.messenger.CreateChat( + actor.ID, body.Title, body.MemberIDs, + ) + if err != nil { + writeAppError(w, err) + return + } + + writeJSON(w, http.StatusCreated, chat) + + case http.MethodGet: + limit, cursor, ok := pageParams(w, r) + if !ok { + return + } + + page, err := api.messenger.ListChats(actor.ID, limit, cursor) + if err != nil { + writeAppError(w, err) + return + } + + writeJSON(w, http.StatusOK, page) + + default: + requireMethod(w, r, "GET, POST") + } + return + } + + path := strings.TrimPrefix(r.URL.Path, "/api/v1/chats/") + parts := strings.Split(path, "/") + + if len(parts) == 1 && parts[0] != "" { + if !requireMethod(w, r, http.MethodGet) { + return + } + + chat, err := api.messenger.GetChat(actor.ID, parts[0]) + if err != nil { + writeAppError(w, err) + return + } + + writeJSON(w, http.StatusOK, chat) + return + } + + if len(parts) != 2 || parts[0] == "" || parts[1] != "messages" { + writeError(w, http.StatusNotFound, "not_found", "Resource not found") + return + } + + chat, err := api.messenger.GetChat(actor.ID, parts[0]) + if err != nil { + writeAppError(w, err) + return + } + + switch r.Method { + case http.MethodPost: + var body createMessageRequest + if !readJSON(w, r, &body, "text") { + return + } + + message, err := api.messenger.CreateMessage( + actor.ID, chat.ID, body.Text, + ) + if err != nil { + writeAppError(w, err) + return + } + + writeJSON(w, http.StatusCreated, message) + + case http.MethodGet: + limit, cursor, ok := pageParams(w, r) + if !ok { + return + } + + page, err := api.messenger.ListMessages( + actor.ID, chat.ID, limit, cursor, + ) + if err != nil { + writeAppError(w, err) + return + } + + writeJSON(w, http.StatusOK, page) + + default: + requireMethod(w, r, "GET, POST") + } +} + +func pageParams( + w http.ResponseWriter, + r *http.Request, +) (int, string, bool) { + invalid := func() (int, string, bool) { + writeAppError(w, service.ErrInvalidRequest) + return 0, "", false + } + + query := r.URL.Query() + limit := 50 + + if values, exists := query["limit"]; exists { + if len(values) != 1 || values[0] == "" { + return invalid() + } + + for _, char := range values[0] { + if char < '0' || char > '9' { + return invalid() + } + } + + value, err := strconv.Atoi(values[0]) + if err != nil || value < 1 || value > 100 { + return invalid() + } + + limit = value + } + + cursor := "" + + if values, exists := query["cursor"]; exists { + if len(values) != 1 || values[0] == "" { + return invalid() + } + cursor = values[0] + } + + return limit, cursor, true +} diff --git a/internal/httpapi/chats_test.go b/internal/httpapi/chats_test.go new file mode 100644 index 00000000..109a6b00 --- /dev/null +++ b/internal/httpapi/chats_test.go @@ -0,0 +1,503 @@ +package httpapi_test + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "net/url" + "reflect" + "sync" + "testing" + "time" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service" +) + +type chatTestClient struct { + base string + client *http.Client +} + +func newChatTestClient(t *testing.T) *chatTestClient { + t.Helper() + + server := httptest.NewServer(newAPI()) + t.Cleanup(server.Close) + + client := server.Client() + client.Timeout = 10 * time.Second + + return &chatTestClient{ + base: server.URL, + client: client, + } +} + +func (c *chatTestClient) call( + method string, + path string, + actorID string, + body any, + wantStatus int, + result any, +) error { + var encoded []byte + var err error + + if body != nil { + encoded, err = json.Marshal(body) + if err != nil { + return err + } + } + + req, err := http.NewRequest( + method, + c.base+path, + bytes.NewReader(encoded), + ) + if err != nil { + return err + } + + if body != nil { + req.Header.Set("Content-Type", "application/json") + } + if actorID != "" { + req.Header.Set("X-User-Id", actorID) + } + + response, err := c.client.Do(req) + if err != nil { + return err + } + defer response.Body.Close() + + data, err := io.ReadAll(response.Body) + if err != nil { + return err + } + + if response.StatusCode != wantStatus { + return fmt.Errorf( + "%s %s: status=%d, want=%d, body=%s", + method, path, response.StatusCode, wantStatus, data, + ) + } + + requestID := response.Header.Get("X-Request-Id") + if requestID == "" { + return fmt.Errorf("%s %s: missing request ID", method, path) + } + + if wantStatus >= 400 { + var problem struct { + Error struct { + Code string `json:"code"` + RequestID string `json:"request_id"` + } `json:"error"` + } + + if err := json.Unmarshal(data, &problem); err != nil { + return err + } + + if problem.Error.Code == "" || + problem.Error.RequestID != requestID { + return fmt.Errorf("invalid error response: %s", data) + } + } + + if result != nil { + return json.Unmarshal(data, result) + } + + return nil +} + +func mustCall( + t *testing.T, + c *chatTestClient, + method string, + path string, + actorID string, + body any, + status int, + result any, +) { + t.Helper() + + if err := c.call(method, path, actorID, body, status, result); err != nil { + t.Fatal(err) + } +} + +func createTestUser( + t *testing.T, + c *chatTestClient, + username string, +) model.User { + t.Helper() + + var user model.User + mustCall( + t, c, http.MethodPost, "/api/v1/users", "", + map[string]any{ + "username": username, + "display_name": username, + }, + http.StatusCreated, &user, + ) + + return user +} + +func createTestChat( + t *testing.T, + c *chatTestClient, + actorID string, + members []string, +) model.Chat { + t.Helper() + + var chat model.Chat + mustCall( + t, c, http.MethodPost, "/api/v1/chats", actorID, + map[string]any{ + "title": "Test chat", + "member_ids": members, + }, + http.StatusCreated, &chat, + ) + + return chat +} + +func readAllMessages( + t *testing.T, + c *chatTestClient, + actorID string, + chatID string, + limit int, +) ([]model.Message, int) { + t.Helper() + + var messages []model.Message + cursor := "" + pages := 0 + seenCursors := make(map[string]bool) + + for { + path := fmt.Sprintf( + "/api/v1/chats/%s/messages?limit=%d", + chatID, limit, + ) + + if cursor != "" { + path += "&cursor=" + url.QueryEscape(cursor) + } + + var page service.Page[model.Message] + mustCall( + t, c, http.MethodGet, path, actorID, + nil, http.StatusOK, &page, + ) + + pages++ + + if page.Items == nil { + t.Fatal("items must be an array, not null") + } + if len(page.Items) > limit { + t.Fatal("page exceeds requested limit") + } + + messages = append(messages, page.Items...) + + if page.NextCursor == nil { + break + } + + cursor = *page.NextCursor + if cursor == "" || seenCursors[cursor] || pages > 100 { + t.Fatal("invalid or repeating pagination cursor") + } + seenCursors[cursor] = true + } + + return messages, pages +} + +func TestChatAccessAndValidation(t *testing.T) { + c := newChatTestClient(t) + + alice := createTestUser(t, c, "alice") + bob := createTestUser(t, c, "bob") + eve := createTestUser(t, c, "eve") + + chat := createTestChat( + t, c, alice.ID, []string{alice.ID, bob.ID}, + ) + path := "/api/v1/chats/" + chat.ID + + mustCall( + t, c, http.MethodGet, path, eve.ID, + nil, http.StatusNotFound, nil, + ) + mustCall( + t, c, http.MethodGet, path+"/messages", eve.ID, + nil, http.StatusNotFound, nil, + ) + mustCall( + t, c, http.MethodPost, path+"/messages", eve.ID, + map[string]any{"text": "not allowed"}, + http.StatusNotFound, nil, + ) + + var hidden service.Page[model.Chat] + mustCall( + t, c, http.MethodGet, "/api/v1/chats", eve.ID, + nil, http.StatusOK, &hidden, + ) + if len(hidden.Items) != 0 { + t.Fatal("outsider sees a private chat") + } + + for _, text := range []string{"", " \t\n"} { + mustCall( + t, c, http.MethodPost, path+"/messages", alice.ID, + map[string]any{"text": text}, + http.StatusBadRequest, nil, + ) + } + + mustCall( + t, c, http.MethodPost, path+"/messages", alice.ID, + map[string]any{ + "text": "hello", + "sender_id": bob.ID, + }, + http.StatusBadRequest, nil, + ) + + for _, members := range [][]string{ + {alice.ID, alice.ID}, + {bob.ID}, + {alice.ID, "00000000-0000-4000-8000-000000000000"}, + } { + mustCall( + t, c, http.MethodPost, "/api/v1/chats", alice.ID, + map[string]any{ + "title": "Invalid chat", + "member_ids": members, + }, + http.StatusBadRequest, nil, + ) + } + + var chats service.Page[model.Chat] + mustCall( + t, c, http.MethodGet, "/api/v1/chats", alice.ID, + nil, http.StatusOK, &chats, + ) + if len(chats.Items) != 1 { + t.Fatalf("expected one chat, got %d", len(chats.Items)) + } + + messages, _ := readAllMessages(t, c, alice.ID, chat.ID, 10) + if len(messages) != 0 { + t.Fatal("rejected messages were saved") + } +} + +func TestMessagePagination(t *testing.T) { + c := newChatTestClient(t) + alice := createTestUser(t, c, "alice") + + chat := createTestChat(t, c, alice.ID, []string{alice.ID}) + other := createTestChat(t, c, alice.ID, []string{alice.ID}) + path := "/api/v1/chats/" + chat.ID + "/messages" + + var sent []model.Message + + for i := 0; i < 7; i++ { + var message model.Message + mustCall( + t, c, http.MethodPost, path, alice.ID, + map[string]any{"text": fmt.Sprintf("Сообщение %d 👋", i)}, + http.StatusCreated, &message, + ) + sent = append(sent, message) + } + + got, pages := readAllMessages(t, c, alice.ID, chat.ID, 2) + + if len(got) != len(sent) || pages != 4 { + t.Fatalf("got %d messages on %d pages", len(got), pages) + } + + for i := range sent { + if !reflect.DeepEqual(got[i], sent[i]) { + t.Fatalf("message %d differs or is out of order", i) + } + } + + var first service.Page[model.Message] + mustCall( + t, c, http.MethodGet, path+"?limit=2", alice.ID, + nil, http.StatusOK, &first, + ) + + if first.NextCursor == nil { + t.Fatal("expected next cursor") + } + + cursor := url.QueryEscape(*first.NextCursor) + + var next service.Page[model.Message] + mustCall( + t, c, http.MethodGet, + path+"?limit=3&cursor="+cursor, alice.ID, + nil, http.StatusOK, &next, + ) + + if len(next.Items) != 3 || next.Items[0].ID != sent[2].ID { + t.Fatal("changing page size lost or repeated a message") + } + + mustCall( + t, c, http.MethodGet, + "/api/v1/chats/"+other.ID+"/messages?cursor="+cursor, + alice.ID, nil, http.StatusBadRequest, nil, + ) + + for _, query := range []string{ + "limit=0", + "limit=101", + "limit=abc", + "cursor=invalid", + } { + mustCall( + t, c, http.MethodGet, path+"?"+query, alice.ID, + nil, http.StatusBadRequest, nil, + ) + } +} + +func TestConcurrentMessages100From10Clients(t *testing.T) { + c := newChatTestClient(t) + + const clients = 10 + const messagesPerClient = 10 + const total = clients * messagesPerClient + + users := make([]model.User, clients) + memberIDs := make([]string, clients) + + for i := range users { + users[i] = createTestUser(t, c, fmt.Sprintf("user_%02d", i)) + memberIDs[i] = users[i].ID + } + + chat := createTestChat(t, c, users[0].ID, memberIDs) + path := "/api/v1/chats/" + chat.ID + "/messages" + + type outcome struct { + message model.Message + err error + } + + results := make(chan outcome, total) + start := make(chan struct{}) + var wg sync.WaitGroup + + for clientIndex := 0; clientIndex < clients; clientIndex++ { + wg.Add(1) + + go func(index int) { + defer wg.Done() + <-start + + for j := 0; j < messagesPerClient; j++ { + text := fmt.Sprintf("client-%d-message-%d", index, j) + var message model.Message + + err := c.call( + http.MethodPost, + path, + users[index].ID, + map[string]any{"text": text}, + http.StatusCreated, + &message, + ) + + if err == nil && + (message.SenderID != users[index].ID || + message.Text != text || + message.ChatID != chat.ID) { + err = fmt.Errorf("incorrect message response") + } + + results <- outcome{message: message, err: err} + } + }(clientIndex) + } + + close(start) + wg.Wait() + close(results) + + expected := make(map[string]model.Message, total) + + for result := range results { + if result.err != nil { + t.Errorf("send failed: %v", result.err) + continue + } + + if _, exists := expected[result.message.ID]; exists { + t.Errorf("duplicate response ID: %s", result.message.ID) + } + + expected[result.message.ID] = result.message + } + + if t.Failed() { + t.FailNow() + } + + if len(expected) != total { + t.Fatalf("expected %d unique responses, got %d", total, len(expected)) + } + + history, pages := readAllMessages( + t, c, users[0].ID, chat.ID, 17, + ) + + if len(history) != total { + t.Fatalf("expected %d stored messages, got %d", total, len(history)) + } + + seen := make(map[string]bool, total) + + for _, message := range history { + if seen[message.ID] { + t.Fatalf("duplicate history ID: %s", message.ID) + } + seen[message.ID] = true + + want, exists := expected[message.ID] + if !exists || !reflect.DeepEqual(want, message) { + t.Fatalf("missing or changed message: %s", message.ID) + } + } + + t.Logf( + "clients=%d sent=%d unique_responses=%d stored=%d unique_stored=%d pages=%d", + clients, total, len(expected), len(history), len(seen), pages, + ) +} diff --git a/internal/httpapi/errors.go b/internal/httpapi/errors.go new file mode 100644 index 00000000..aaa4cf70 --- /dev/null +++ b/internal/httpapi/errors.go @@ -0,0 +1,102 @@ +package httpapi + +import ( + "context" + "errors" + "log" + "net/http" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/objectstore" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/upload" +) + +type errorResponse struct { + Error errorDetails `json:"error"` +} + +type errorDetails struct { + Code string `json:"code"` + Message string `json:"message"` + RequestID string `json:"request_id"` +} + +func writeAppError(w http.ResponseWriter, err error) { + switch { + case errors.Is(err, store.ErrAttachmentNotReady): + writeError(w, http.StatusConflict, "conflict", "Attachment is not ready") + case errors.Is(err, upload.ErrTooLarge): + writeError(w, http.StatusRequestEntityTooLarge, "payload_too_large", "Attachment limit exceeded") + case errors.Is(err, upload.ErrMediaType): + writeError(w, http.StatusUnsupportedMediaType, "unsupported_media_type", "Valid PNG or JPEG required") + case errors.Is(err, upload.ErrFilename), errors.Is(err, upload.ErrInterrupted): + writeError(w, http.StatusBadRequest, "invalid_request", "Invalid attachment request") + case errors.Is(err, objectstore.ErrNotFound): + writeError(w, http.StatusNotFound, "not_found", "Resource not found") + case errors.Is(err, objectstore.ErrUnavailable), errors.Is(err, context.DeadlineExceeded), errors.Is(err, context.Canceled): + writeError(w, http.StatusServiceUnavailable, "unavailable", "Service temporarily unavailable") + case errors.Is(err, store.ErrIdempotencyConflict): + writeError(w, http.StatusConflict, "conflict", "Idempotency key already used for another message") + + case errors.Is(err, store.ErrUnavailable): + writeError(w, http.StatusServiceUnavailable, "unavailable", "Service temporarily unavailable") + + case errors.Is(err, service.ErrInvalidRequest): + writeError( + w, http.StatusBadRequest, + "invalid_request", "Invalid request fields", + ) + + case errors.Is(err, service.ErrUnauthorized): + writeError( + w, http.StatusUnauthorized, + "unauthorized", "Valid X-User-Id is required", + ) + + case errors.Is(err, store.ErrUsernameTaken): + writeError( + w, http.StatusConflict, + "conflict", "Username already exists", + ) + + case errors.Is(err, store.ErrInvalidMembers): + writeError( + w, http.StatusBadRequest, + "invalid_request", "Invalid chat members", + ) + + case errors.Is(err, store.ErrNotFound): + writeError( + w, http.StatusNotFound, + "not_found", "Resource not found", + ) + + default: + log.Printf( + "internal error request_id=%s: %v", + w.Header().Get("X-Request-Id"), + err, + ) + + writeError( + w, http.StatusInternalServerError, + "internal_error", "Internal server error", + ) + } +} + +func writeError( + w http.ResponseWriter, + status int, + code string, + message string, +) { + writeJSON(w, status, errorResponse{ + Error: errorDetails{ + Code: code, + Message: message, + RequestID: w.Header().Get("X-Request-Id"), + }, + }) +} diff --git a/internal/httpapi/health.go b/internal/httpapi/health.go new file mode 100644 index 00000000..f3a4fb21 --- /dev/null +++ b/internal/httpapi/health.go @@ -0,0 +1,23 @@ +package httpapi + +import "net/http" + +func handleHealth(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + w.Header().Set("Allow", http.MethodGet) + + writeError( + w, + http.StatusMethodNotAllowed, + "invalid_request", + "Method not allowed", + ) + return + } + + writeJSON(w, http.StatusOK, struct { + Status string `json:"status"` + }{ + Status: "ok", + }) +} diff --git a/internal/httpapi/instance_test.go b/internal/httpapi/instance_test.go new file mode 100644 index 00000000..a4a340c7 --- /dev/null +++ b/internal/httpapi/instance_test.go @@ -0,0 +1,48 @@ +package httpapi_test + +import ( + "net/http" + "sync/atomic" + "testing" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/httpapi" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" +) + +func TestInstanceIDOnResponses(t *testing.T) { + var stopping atomic.Bool + handler, err := httpapi.NewPostgresHandler(&store.PostgresStore{}, &stopping, 3600) + if err != nil { + t.Fatal(err) + } + var instanceID string + for _, tc := range []struct { + path string + stopping bool + status int + }{ + {"/health/live", false, http.StatusOK}, + {"/missing", false, http.StatusNotFound}, + {"/api/v1/chats", false, http.StatusUnauthorized}, + {"/health/ready", true, http.StatusServiceUnavailable}, + {"/health/live", true, http.StatusOK}, + } { + stopping.Store(tc.stopping) + response := request(handler, http.MethodGet, tc.path, "", "") + if response.Code != tc.status { + t.Fatalf("%s: status=%d, want %d", tc.path, response.Code, tc.status) + } + got := response.Header().Get("X-Instance-Id") + if got == "" { + t.Fatal("missing X-Instance-Id") + } + if instanceID != "" && got != instanceID { + t.Fatalf("instance changed: %q -> %q", instanceID, got) + } + instanceID = got + } + response := request(newAPI(), http.MethodGet, "/health/live", "", "") + if got := response.Header().Get("X-Instance-Id"); got != instanceID { + t.Fatalf("different handlers in one process: %q != %q", got, instanceID) + } +} diff --git a/internal/httpapi/json.go b/internal/httpapi/json.go new file mode 100644 index 00000000..efe2380e --- /dev/null +++ b/internal/httpapi/json.go @@ -0,0 +1,104 @@ +package httpapi + +import ( + "bytes" + "encoding/json" + "io" + "log" + "mime" + "net/http" + "slices" + "unicode/utf8" +) + +const maxJSONBody = 1 << 20 // 1 MiB + +func readJSON( + w http.ResponseWriter, + r *http.Request, + dst any, + requiredFields ...string, +) bool { + return readJSONFields(w, r, dst, requiredFields, nil) +} + +func readJSONFields(w http.ResponseWriter, r *http.Request, dst any, requiredFields, optionalFields []string) bool { + mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type")) + if err != nil || mediaType != "application/json" { + writeError( + w, + http.StatusUnsupportedMediaType, + "unsupported_media_type", + "Content-Type must be application/json", + ) + return false + } + + defer r.Body.Close() + + body, err := io.ReadAll(io.LimitReader(r.Body, maxJSONBody+1)) + if err != nil { + writeError(w, http.StatusBadRequest, "invalid_request", "Cannot read body") + return false + } + + if len(body) > maxJSONBody { + writeError( + w, + http.StatusRequestEntityTooLarge, + "payload_too_large", + "JSON body is too large", + ) + return false + } + + invalid := func() bool { + writeError( + w, + http.StatusBadRequest, + "invalid_request", + "Invalid JSON body", + ) + return false + } + + if !utf8.Valid(body) { + return invalid() + } + + var fields map[string]json.RawMessage + if err := json.Unmarshal(body, &fields); err != nil || fields == nil { + return invalid() + } + + for name, value := range fields { + if (!slices.Contains(requiredFields, name) && !slices.Contains(optionalFields, name)) || bytes.Equal(bytes.TrimSpace(value), []byte("null")) { + return invalid() + } + } + + for _, name := range requiredFields { + value, exists := fields[name] + if !exists || bytes.Equal(bytes.TrimSpace(value), []byte("null")) { + return invalid() + } + } + + decoder := json.NewDecoder(bytes.NewReader(body)) + decoder.DisallowUnknownFields() + + if err := decoder.Decode(dst); err != nil { + return invalid() + } + + return true +} + +func writeJSON(w http.ResponseWriter, status int, body any) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + + if err := json.NewEncoder(w).Encode(body); err != nil { + log.Printf("cannot write JSON response: %v", err) + } +} diff --git a/internal/httpapi/json_test.go b/internal/httpapi/json_test.go new file mode 100644 index 00000000..f6d20f0a --- /dev/null +++ b/internal/httpapi/json_test.go @@ -0,0 +1,99 @@ +package httpapi_test + +import ( + "encoding/json" + "fmt" + "net/http" + "testing" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service" +) + +func TestJSONFieldNamesAreCaseSensitive(t *testing.T) { + handler := newAPI() + + t.Run("registration", func(t *testing.T) { + for _, body := range []string{ + `{"username":"alice","display_name":"Original","Display_Name":"Changed"}`, + `{"Display_Name":"Changed","username":"alice","display_name":"Original"}`, + `{"username":"alice","display_name":"Original","Username":"bob"}`, + `{"username":"alice","Display_Name":"Changed"}`, + } { + checkError(t, request(handler, http.MethodPost, "/api/v1/users", body, ""), + http.StatusBadRequest, "invalid_request") + } + }) + + created := request(handler, http.MethodPost, "/api/v1/users", + `{"username":"alice","display_name":"Original"}`, "") + if created.Code != http.StatusCreated { + t.Fatalf("valid registration: %d %s", created.Code, created.Body.String()) + } + var user model.User + if err := json.Unmarshal(created.Body.Bytes(), &user); err != nil { + t.Fatal(err) + } + if user.DisplayName != "Original" { + t.Fatalf("display name changed: %q", user.DisplayName) + } + + t.Run("chat", func(t *testing.T) { + for _, body := range []string{ + fmt.Sprintf(`{"title":"Original","Title":"Changed","member_ids":[%q]}`, user.ID), + fmt.Sprintf(`{"Title":"Changed","title":"Original","member_ids":[%q]}`, user.ID), + fmt.Sprintf(`{"title":"Original","member_ids":[%q],"Member_Ids":[]}`, user.ID), + fmt.Sprintf(`{"Title":"Changed","member_ids":[%q]}`, user.ID), + } { + checkError(t, request(handler, http.MethodPost, "/api/v1/chats", body, user.ID), + http.StatusBadRequest, "invalid_request") + } + response := request(handler, http.MethodGet, "/api/v1/chats", "", user.ID) + var page service.Page[model.Chat] + if err := json.Unmarshal(response.Body.Bytes(), &page); err != nil { + t.Fatal(err) + } + if response.Code != http.StatusOK || len(page.Items) != 0 { + t.Fatalf("rejected chats were saved: %d %s", response.Code, response.Body.String()) + } + }) + + created = request(handler, http.MethodPost, "/api/v1/chats", + fmt.Sprintf(`{"title":"Original","member_ids":[%q]}`, user.ID), user.ID) + if created.Code != http.StatusCreated { + t.Fatalf("valid chat: %d %s", created.Code, created.Body.String()) + } + var chat model.Chat + if err := json.Unmarshal(created.Body.Bytes(), &chat); err != nil { + t.Fatal(err) + } + path := "/api/v1/chats/" + chat.ID + "/messages" + + t.Run("message", func(t *testing.T) { + for _, body := range []string{ + `{"text":"Original","Text":"Changed"}`, + `{"Text":"Changed","text":"Original"}`, + `{"text":"Original","TEXT":"Changed"}`, + `{"Text":"Changed"}`, + } { + checkError(t, request(handler, http.MethodPost, path, body, user.ID), + http.StatusBadRequest, "invalid_request") + } + response := request(handler, http.MethodGet, path, "", user.ID) + var page service.Page[model.Message] + if err := json.Unmarshal(response.Body.Bytes(), &page); err != nil { + t.Fatal(err) + } + if response.Code != http.StatusOK || len(page.Items) != 0 { + t.Fatalf("rejected messages were saved: %d %s", response.Code, response.Body.String()) + } + response = request(handler, http.MethodPost, path, `{"text":"Original"}`, user.ID) + var message model.Message + if err := json.Unmarshal(response.Body.Bytes(), &message); err != nil { + t.Fatal(err) + } + if response.Code != http.StatusCreated || message.Text != "Original" { + t.Fatalf("valid message: %d %s", response.Code, response.Body.String()) + } + }) +} diff --git a/internal/httpapi/middleware.go b/internal/httpapi/middleware.go new file mode 100644 index 00000000..a3f626a0 --- /dev/null +++ b/internal/httpapi/middleware.go @@ -0,0 +1,102 @@ +package httpapi + +import ( + "crypto/rand" + "encoding/hex" + "log" + "net/http" + "time" +) + +var processInstanceID = rand.Text() + +func withInstanceID(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("X-Instance-Id", processInstanceID) + next.ServeHTTP(w, r) + }) +} + +func withRequestID(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var randomBytes [16]byte + + if _, err := rand.Read(randomBytes[:]); err != nil { + panic("cannot generate request ID") + } + + requestID := hex.EncodeToString(randomBytes[:]) + w.Header().Set("X-Request-Id", requestID) + + next.ServeHTTP(w, r) + }) +} + +type statusWriter struct { + http.ResponseWriter + status int +} + +func (w *statusWriter) WriteHeader(status int) { + if w.status != 0 { + return + } + + w.status = status + w.ResponseWriter.WriteHeader(status) +} + +func (w *statusWriter) Write(body []byte) (int, error) { + if w.status == 0 { + w.WriteHeader(http.StatusOK) + } + + return w.ResponseWriter.Write(body) +} + +func (w *statusWriter) Unwrap() http.ResponseWriter { + return w.ResponseWriter +} + +func withAccessLog(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + started := time.Now() + writer := &statusWriter{ResponseWriter: w} + + defer func() { + if recovered := recover(); recovered != nil { + log.Printf( + "handler panic request_id=%s", + writer.Header().Get("X-Request-Id"), + ) + + if writer.status == 0 { + writeError( + writer, + http.StatusInternalServerError, + "internal_error", + "Internal server error", + ) + } else { + panic(http.ErrAbortHandler) + } + } + + status := writer.status + if status == 0 { + status = http.StatusOK + } + + log.Printf( + "request_id=%s method=%s path=%q status=%d duration_ms=%d", + writer.Header().Get("X-Request-Id"), + r.Method, + r.URL.Path, + status, + time.Since(started).Milliseconds(), + ) + }() + + next.ServeHTTP(writer, r) + }) +} diff --git a/internal/httpapi/postgres.go b/internal/httpapi/postgres.go new file mode 100644 index 00000000..8f3bdbae --- /dev/null +++ b/internal/httpapi/postgres.go @@ -0,0 +1,93 @@ +package httpapi + +import ( + "context" + "net/http" + "strings" + "sync/atomic" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" +) + +func NewPostgresHandler(db *store.PostgresStore, stopping *atomic.Bool, ttl int64, attachmentServices ...*service.Attachments) (http.Handler, error) { + registration := service.NewRegistration(db) + sessions, err := service.NewSessions(db, registration, ttl) + if err != nil { + return nil, err + } + chats := &persistentAPI{messenger: service.NewPersistentMessenger(db)} + if len(attachmentServices) > 0 { + chats.attachments = attachmentServices[0] + } + return withInstanceID(withRequestID(withAccessLog(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if stopping.Load() && r.URL.Path != "/health/live" { + writeAppError(w, store.ErrUnavailable) + return + } + if r.URL.Path == "/api/v1/chats" || strings.HasPrefix(r.URL.Path, "/api/v1/chats/") || strings.HasPrefix(r.URL.Path, "/api/v1/attachments/") { + token, err := bearerToken(r) + if err != nil { + sessionError(w, err, false) + return + } + user, err := sessions.CurrentUser(r.Context(), token) + if err != nil { + sessionError(w, err, false) + return + } + ctx := context.WithValue(r.Context(), userContextKey{}, user) + if strings.HasPrefix(r.URL.Path, "/api/v1/attachments/") { + attachmentRoutes(w, r.WithContext(ctx), chats.attachments) + return + } + chats.chatRoutes(w, r.WithContext(ctx)) + return + } + switch r.URL.Path { + case "/api/v1/auth/sessions": + loginSession(w, r, sessions) + case "/api/v1/auth/sessions/current": + currentSession(w, r, sessions, true) + case "/api/v1/users/me": + currentSession(w, r, sessions, false) + case "/health/live": + handleHealth(w, r) + case "/health/ready": + if !requireMethod(w, r, http.MethodGet) { + return + } + if err := db.Ping(r.Context()); err != nil { + writeAppError(w, err) + return + } + if chats.attachments != nil { + if err := chats.attachments.Ready(r.Context()); err != nil { + writeAppError(w, err) + return + } + } + handleHealth(w, r) + case "/api/v1/users": + if !requireMethod(w, r, http.MethodPost) { + return + } + var body struct { + Username string `json:"username"` + DisplayName string `json:"display_name"` + Password string `json:"password"` + } + if !readJSON(w, r, &body, "username", "display_name", "password") { + return + } + user, err := registration.Register(r.Context(), body.Username, body.DisplayName, body.Password) + if err != nil { + writeAppError(w, err) + return + } + writeJSON(w, http.StatusCreated, user) + default: + writeError(w, http.StatusNotFound, "not_found", "Resource not found") + } + })))), nil +} diff --git a/internal/httpapi/postgres_chats.go b/internal/httpapi/postgres_chats.go new file mode 100644 index 00000000..d406ff69 --- /dev/null +++ b/internal/httpapi/postgres_chats.go @@ -0,0 +1,126 @@ +package httpapi + +import ( + "net/http" + + "strings" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service" +) + +type persistentAPI struct { + messenger *service.PersistentMessenger + attachments *service.Attachments +} + +func (api *persistentAPI) chatRoutes(w http.ResponseWriter, r *http.Request) { + actor := userFromContext(r.Context()) + + if r.URL.Path == "/api/v1/chats" { + switch r.Method { + case http.MethodPost: + var body createChatRequest + if !readJSON(w, r, &body, "title", "member_ids") { + return + } + + chat, err := api.messenger.CreateChat(r.Context(), + actor.ID, body.Title, body.MemberIDs, + ) + if err != nil { + writeAppError(w, err) + return + } + + writeJSON(w, http.StatusCreated, chat) + + case http.MethodGet: + limit, cursor, ok := pageParams(w, r) + if !ok { + return + } + + page, err := api.messenger.ListChats(r.Context(), actor.ID, limit, cursor) + if err != nil { + writeAppError(w, err) + return + } + + writeJSON(w, http.StatusOK, page) + + default: + requireMethod(w, r, "GET, POST") + } + return + } + + path := strings.TrimPrefix(r.URL.Path, "/api/v1/chats/") + parts := strings.Split(path, "/") + if len(parts) == 2 && parts[0] != "" && parts[1] == "attachments" { + createAttachment(w, r, api.attachments, parts[0]) + return + } + + if len(parts) == 1 && parts[0] != "" { + if !requireMethod(w, r, http.MethodGet) { + return + } + + chat, err := api.messenger.GetChat(r.Context(), actor.ID, parts[0]) + if err != nil { + writeAppError(w, err) + return + } + + writeJSON(w, http.StatusOK, chat) + return + } + + if len(parts) != 2 || parts[0] == "" || parts[1] != "messages" { + writeError(w, http.StatusNotFound, "not_found", "Resource not found") + return + } + + chat, err := api.messenger.GetChat(r.Context(), actor.ID, parts[0]) + if err != nil { + writeAppError(w, err) + return + } + + switch r.Method { + case http.MethodPost: + var body createMessageRequest + if !readJSONFields(w, r, &body, []string{"text"}, []string{"attachment_ids"}) { + return + } + + message, err := api.messenger.CreateMessage(r.Context(), + actor.ID, chat.ID, body.Text, r.Header.Get("Idempotency-Key"), body.AttachmentIDs..., + ) + if err != nil { + writeAppError(w, err) + return + } + + writeJSON(w, http.StatusCreated, message) + + case http.MethodGet: + limit, cursor, ok := pageParams(w, r) + if !ok { + return + } + + page, err := api.messenger.ListMessages(r.Context(), + actor.ID, chat.ID, limit, cursor, + ) + if err != nil { + writeAppError(w, err) + return + } + + writeJSON(w, http.StatusOK, page) + + default: + requireMethod(w, r, "GET, POST") + } +} diff --git a/internal/httpapi/router.go b/internal/httpapi/router.go new file mode 100644 index 00000000..3051b0ae --- /dev/null +++ b/internal/httpapi/router.go @@ -0,0 +1,90 @@ +package httpapi + +import ( + "net/http" + "strings" + "sync/atomic" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service" +) + +type API struct { + messenger *service.Messenger +} + +func NewHandler( + messenger *service.Messenger, + stoppingFlags ...*atomic.Bool, +) http.Handler { + api := &API{messenger: messenger} + + var stopping *atomic.Bool + + if len(stoppingFlags) > 0 { + stopping = stoppingFlags[0] + } + + if stopping == nil { + stopping = &atomic.Bool{} + } + + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if stopping.Load() && r.URL.Path != "/health/live" { + writeError( + w, + http.StatusServiceUnavailable, + "unavailable", + "Server is shutting down", + ) + return + } + + api.route(w, r) + }) + + return withInstanceID(withRequestID(withAccessLog(handler))) +} + +func (api *API) route(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/api/v1/chats" || + strings.HasPrefix(r.URL.Path, "/api/v1/chats/") { + api.authenticate(http.HandlerFunc(api.chatRoutes)).ServeHTTP(w, r) + return + } + + switch r.URL.Path { + case "/health/live", "/health/ready": + handleHealth(w, r) + + case "/api/v1/users": + if !requireMethod(w, r, http.MethodPost) { + return + } + api.createUser(w, r) + + case "/api/v1/users/me": + if !requireMethod(w, r, http.MethodGet) { + return + } + api.authenticate(http.HandlerFunc(api.currentUser)).ServeHTTP(w, r) + + default: + writeError(w, http.StatusNotFound, "not_found", "Resource not found") + } +} + +func requireMethod(w http.ResponseWriter, r *http.Request, method string) bool { + if r.Method == method { + return true + } + + w.Header().Set("Allow", method) + writeError( + w, + http.StatusMethodNotAllowed, + "invalid_request", + "Method not allowed", + ) + + return false +} diff --git a/internal/httpapi/sessions.go b/internal/httpapi/sessions.go new file mode 100644 index 00000000..ca411466 --- /dev/null +++ b/internal/httpapi/sessions.go @@ -0,0 +1,87 @@ +package httpapi + +import ( + "errors" + "io" + "net/http" + "strings" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service" +) + +func sessionError(w http.ResponseWriter, err error, basic bool) { + if errors.Is(err, service.ErrUnauthorized) { + challenge := `Bearer realm="messenger"` + if basic { + challenge = `Basic realm="messenger", charset="UTF-8"` + } + w.Header().Set("WWW-Authenticate", challenge) + writeError(w, http.StatusUnauthorized, "unauthorized", "Invalid credentials") + return + } + writeAppError(w, err) +} + +func loginSession(w http.ResponseWriter, r *http.Request, sessions *service.Sessions) { + if !requireMethod(w, r, http.MethodPost) { + return + } + username, secret, ok := r.BasicAuth() + if !ok || len(r.Header.Values("Authorization")) != 1 { + sessionError(w, service.ErrUnauthorized, true) + return + } + body, err := io.ReadAll(io.LimitReader(r.Body, 1)) + if err != nil || len(body) != 0 { + writeAppError(w, service.ErrInvalidRequest) + return + } + result, err := sessions.Login(r.Context(), username, secret) + if err != nil { + sessionError(w, err, true) + return + } + w.Header().Set("Cache-Control", "no-store") + writeJSON(w, http.StatusCreated, result) +} + +func bearerToken(r *http.Request) (string, error) { + if len(r.Header.Values("Authorization")) != 1 { + return "", service.ErrUnauthorized + } + fields := strings.Fields(r.Header.Get("Authorization")) + if len(fields) != 2 || !strings.EqualFold(fields[0], "Bearer") { + return "", service.ErrUnauthorized + } + return fields[1], nil +} + +func currentSession(w http.ResponseWriter, r *http.Request, sessions *service.Sessions, logout bool) { + method := http.MethodGet + if logout { + method = http.MethodDelete + } + if !requireMethod(w, r, method) { + return + } + token, err := bearerToken(r) + if err != nil { + sessionError(w, err, false) + return + } + if logout { + if err := sessions.Logout(r.Context(), token); err != nil { + sessionError(w, err, false) + return + } + w.WriteHeader(http.StatusNoContent) + return + } + user, err := sessions.CurrentUser(r.Context(), token) + if err != nil { + sessionError(w, err, false) + return + } + w.Header().Set("Cache-Control", "no-store") + writeJSON(w, http.StatusOK, user) +} diff --git a/internal/httpapi/users.go b/internal/httpapi/users.go new file mode 100644 index 00000000..7bbe659b --- /dev/null +++ b/internal/httpapi/users.go @@ -0,0 +1,29 @@ +package httpapi + +import "net/http" + +type createUserRequest struct { + Username string `json:"username"` + DisplayName string `json:"display_name"` +} + +func (api *API) createUser(w http.ResponseWriter, r *http.Request) { + var body createUserRequest + + if !readJSON(w, r, &body, "username", "display_name") { + return + } + + user, err := api.messenger.RegisterUser(body.Username, body.DisplayName) + if err != nil { + writeAppError(w, err) + return + } + + writeJSON(w, http.StatusCreated, user) +} + +func (api *API) currentUser(w http.ResponseWriter, r *http.Request) { + user := userFromContext(r.Context()) + writeJSON(w, http.StatusOK, user) +} diff --git a/internal/httpapi/users_test.go b/internal/httpapi/users_test.go new file mode 100644 index 00000000..3802bc0a --- /dev/null +++ b/internal/httpapi/users_test.go @@ -0,0 +1,171 @@ +package httpapi_test + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/httpapi" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" +) + +func newAPI() http.Handler { + return httpapi.NewHandler( + service.NewMessenger(store.NewMemoryStore()), + ) +} + +func request( + handler http.Handler, + method string, + path string, + body string, + userID string, +) *httptest.ResponseRecorder { + req := httptest.NewRequest(method, path, strings.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + + if userID != "" { + req.Header.Set("X-User-Id", userID) + } + + response := httptest.NewRecorder() + handler.ServeHTTP(response, req) + return response +} + +func checkError( + t *testing.T, + response *httptest.ResponseRecorder, + status int, + code string, +) { + t.Helper() + + if response.Code != status { + t.Fatalf( + "status: got %d, want %d; body=%s", + response.Code, status, response.Body.String(), + ) + } + + var body struct { + Error struct { + Code string `json:"code"` + RequestID string `json:"request_id"` + } `json:"error"` + } + + if err := json.Unmarshal(response.Body.Bytes(), &body); err != nil { + t.Fatalf("decode error response: %v", err) + } + + if body.Error.Code != code { + t.Errorf("error code: got %q, want %q", body.Error.Code, code) + } + + id := response.Header().Get("X-Request-Id") + if id == "" || body.Error.RequestID != id { + t.Error("missing or mismatched request ID") + } +} + +func TestUserHTTPFlow(t *testing.T) { + handler := newAPI() + + body := `{"username":"alice","display_name":"Алиса 👋"}` + + created := request(handler, http.MethodPost, "/api/v1/users", body, "") + if created.Code != http.StatusCreated { + t.Fatalf("registration: %d %s", created.Code, created.Body.String()) + } + + if created.Header().Get("X-Request-Id") == "" { + t.Fatal("registration response has no request ID") + } + + var user model.User + if err := json.Unmarshal(created.Body.Bytes(), &user); err != nil { + t.Fatal(err) + } + + if user.ID == "" || user.CreatedAt.IsZero() || + user.Username != "alice" || user.DisplayName != "Алиса 👋" { + t.Fatalf("invalid user: %+v", user) + } + + me := request(handler, http.MethodGet, "/api/v1/users/me", "", user.ID) + if me.Code != http.StatusOK { + t.Fatalf("current user: %d %s", me.Code, me.Body.String()) + } + + var current model.User + if err := json.Unmarshal(me.Body.Bytes(), ¤t); err != nil { + t.Fatal(err) + } + + if current != user { + t.Fatalf("current user differs: %+v", current) + } + + duplicate := request(handler, http.MethodPost, "/api/v1/users", body, "") + checkError(t, duplicate, http.StatusConflict, "conflict") + + for _, id := range []string{ + "", + "invalid", + "00000000-0000-4000-8000-000000000000", + } { + response := request( + handler, http.MethodGet, "/api/v1/users/me", "", id, + ) + checkError(t, response, http.StatusUnauthorized, "unauthorized") + } +} + +func TestRegistrationInvalidJSON(t *testing.T) { + cases := []string{ + ``, + `{`, + `null`, + `[]`, + `{}`, + `{"username":"alice"}`, + `{"username":null,"display_name":"Alice"}`, + `{"username":123,"display_name":"Alice"}`, + `{"username":"alice","display_name":"Alice","extra":true}`, + `{"username":"alice","display_name":"Alice"} {}`, + `{"username":"Alice","display_name":"Alice"}`, + `{"username":"alice","display_name":" "}`, + } + + for _, body := range cases { + t.Run(body, func(t *testing.T) { + response := request( + newAPI(), http.MethodPost, "/api/v1/users", body, "", + ) + checkError(t, response, http.StatusBadRequest, "invalid_request") + }) + } +} + +func TestRegistrationContentType(t *testing.T) { + req := httptest.NewRequest( + http.MethodPost, + "/api/v1/users", + strings.NewReader(`{"username":"alice","display_name":"Alice"}`), + ) + req.Header.Set("Content-Type", "text/plain") + + response := httptest.NewRecorder() + newAPI().ServeHTTP(response, req) + + checkError( + t, response, + http.StatusUnsupportedMediaType, "unsupported_media_type", + ) +} diff --git a/internal/model/attachment.go b/internal/model/attachment.go new file mode 100644 index 00000000..c7103fd2 --- /dev/null +++ b/internal/model/attachment.go @@ -0,0 +1,27 @@ +package model + +import "time" + +type Attachment struct { + ID string `json:"id"` + ChatID string `json:"chat_id"` + OwnerID string `json:"owner_id"` + Filename string `json:"filename"` + ContentType string `json:"content_type"` + SizeBytes int64 `json:"size_bytes"` + SHA256 string `json:"sha256"` + Status string `json:"status"` + Variants []Variant `json:"variants"` + ErrorCode *string `json:"error_code"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` +} + +type Variant struct { + Name string `json:"name"` + ContentType string `json:"content_type"` + SizeBytes int64 `json:"size_bytes"` + Width int `json:"width"` + Height int `json:"height"` + SHA256 string `json:"sha256"` +} diff --git a/internal/model/model.go b/internal/model/model.go new file mode 100644 index 00000000..35081dca --- /dev/null +++ b/internal/model/model.go @@ -0,0 +1,26 @@ +package model + +import "time" + +type User struct { + ID string `json:"id"` + Username string `json:"username"` + DisplayName string `json:"display_name"` + CreatedAt time.Time `json:"created_at"` +} + +type Chat struct { + ID string `json:"id"` + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` + CreatedAt time.Time `json:"created_at"` +} + +type Message struct { + AttachmentIDs []string `json:"attachment_ids"` + ID string `json:"id"` + ChatID string `json:"chat_id"` + SenderID string `json:"sender_id"` + Text string `json:"text"` + CreatedAt time.Time `json:"created_at"` +} diff --git a/internal/objectstore/s3.go b/internal/objectstore/s3.go new file mode 100644 index 00000000..4cd7f79a --- /dev/null +++ b/internal/objectstore/s3.go @@ -0,0 +1,178 @@ +// Package objectstore stores original bytes; chat authorization belongs to the service. +package objectstore + +import ( + "context" + "encoding/hex" + "errors" + "io" + "net" + "net/http" + "net/url" + "os" + "strings" + "sync" + "time" + + "github.com/google/uuid" + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" + "github.com/minio/minio-go/v7/pkg/s3utils" +) + +var ( + ErrConfig = errors.New("invalid S3 configuration") + ErrInput = errors.New("invalid object input") + ErrNotFound = errors.New("S3 object not found") + ErrUnavailable = errors.New("S3 unavailable") +) + +const operationTimeout = 30 * time.Second + +type Config struct { + Endpoint, Region, Bucket string + AccessKeyFile, SecretKeyFile string +} + +type S3 struct { + core *minio.Core + bucket string + transport *http.Transport +} + +func New(config Config) (*S3, error) { + u, err := url.Parse(config.Endpoint) + if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" || u.User != nil || (u.Path != "" && u.Path != "/") || u.RawQuery != "" || u.Fragment != "" || config.Region == "" { + return nil, ErrConfig + } + if err := s3utils.CheckValidBucketNameStrict(config.Bucket); err != nil { + return nil, ErrConfig + } + access, err := readSecret(config.AccessKeyFile) + if err != nil { + return nil, err + } + secret, err := readSecret(config.SecretKeyFile) + if err != nil { + return nil, err + } + transport := &http.Transport{ + DialContext: (&net.Dialer{Timeout: 5 * time.Second, KeepAlive: 30 * time.Second}).DialContext, + TLSHandshakeTimeout: 5 * time.Second, ResponseHeaderTimeout: 5 * time.Second, + IdleConnTimeout: 60 * time.Second, MaxIdleConns: 10, MaxIdleConnsPerHost: 10, + } + core, err := minio.NewCore(u.Host, &minio.Options{ + Creds: credentials.NewStaticV4(access, secret, ""), Secure: u.Scheme == "https", + Region: config.Region, BucketLookup: minio.BucketLookupPath, + Transport: transport, MaxRetries: 1, + }) + if err != nil { + return nil, ErrConfig + } + return &S3{core: core, bucket: config.Bucket, transport: transport}, nil +} + +func readSecret(path string) (string, error) { + data, err := os.ReadFile(path) + if err != nil { + return "", ErrConfig + } + value := strings.TrimRight(string(data), "\r\n") + if strings.TrimSpace(value) == "" { + return "", ErrConfig + } + return value, nil +} + +func (s *S3) Close() { s.transport.CloseIdleConnections() } + +func (s *S3) Check(ctx context.Context) error { + ctx, cancel := context.WithTimeout(ctx, 1500*time.Millisecond) + defer cancel() + exists, err := s.core.BucketExists(ctx, s.bucket) + if err != nil { + return storageError(ctx, err) + } + if !exists { + return ErrUnavailable + } + return nil +} + +func ObjectKey(id string) (string, error) { + parsed, err := uuid.Parse(id) + if err != nil || parsed.String() != id { + return "", ErrInput + } + return "attachments/" + id, nil +} + +func (s *S3) Put(ctx context.Context, id string, source io.Reader, size int64, contentType, sha256 string) error { + key, err := ObjectKey(id) + checksum, checksumErr := hex.DecodeString(sha256) + if err != nil || source == nil || size < 1 || size > 10485760 || (contentType != "image/png" && contentType != "image/jpeg") || checksumErr != nil || len(checksum) != 32 || strings.ToLower(sha256) != sha256 { + return ErrInput + } + ctx, cancel := context.WithTimeout(ctx, operationTimeout) + defer cancel() + _, err = s.core.PutObject(ctx, s.bucket, key, source, size, "", sha256, minio.PutObjectOptions{ContentType: contentType}) + return storageError(ctx, err) +} + +type Original struct { + Body io.ReadCloser + SizeBytes int64 + ContentType string +} + +func (s *S3) Open(ctx context.Context, id string) (Original, error) { + key, err := ObjectKey(id) + if err != nil { + return Original{}, err + } + ctx, cancel := context.WithTimeout(ctx, operationTimeout) + body, info, _, err := s.core.GetObject(ctx, s.bucket, key, minio.GetObjectOptions{}) + if err != nil { + mapped := storageError(ctx, err) + cancel() + return Original{}, mapped + } + return Original{Body: &originalBody{ReadCloser: body, cancel: cancel}, SizeBytes: info.Size, ContentType: info.ContentType}, nil +} + +func (s *S3) Delete(ctx context.Context, id string) error { + key, err := ObjectKey(id) + if err != nil { + return err + } + ctx, cancel := context.WithTimeout(ctx, operationTimeout) + defer cancel() + return storageError(ctx, s.core.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{})) +} + +func storageError(ctx context.Context, err error) error { + if err == nil { + return nil + } + if ctx.Err() != nil { + return ctx.Err() + } + switch minio.ToErrorResponse(err).Code { + case "NoSuchKey", "NoSuchObject": + return ErrNotFound + default: + return ErrUnavailable + } +} + +type originalBody struct { + io.ReadCloser + cancel context.CancelFunc + once sync.Once + err error +} + +func (b *originalBody) Close() error { + b.once.Do(func() { b.err = b.ReadCloser.Close(); b.cancel() }) + return b.err +} diff --git a/internal/objectstore/s3_test.go b/internal/objectstore/s3_test.go new file mode 100644 index 00000000..44164d5a --- /dev/null +++ b/internal/objectstore/s3_test.go @@ -0,0 +1,130 @@ +package objectstore + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "image" + "image/png" + "io" + "os" + "path/filepath" + "testing" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/upload" + "github.com/google/uuid" +) + +func TestObjectKey(t *testing.T) { + for _, bad := range []string{"", "../x", "photo.png", "550E8400-E29B-41D4-A716-446655440000"} { + if _, err := ObjectKey(bad); !errors.Is(err, ErrInput) { + t.Fatalf("accepted key %q", bad) + } + } + id := uuid.NewString() + if key, err := ObjectKey(id); err != nil || key != "attachments/"+id { + t.Fatal("invalid generated key") + } +} + +func TestInvalidConfig(t *testing.T) { + for _, endpoint := range []string{"", "ftp://s3:9000", "http://user:password@s3:9000", "http://s3:9000/path", "http://s3:9000?secret=hidden"} { + _, err := New(Config{Endpoint: endpoint, Region: "us-east-1", Bucket: "messenger-attachments"}) + if !errors.Is(err, ErrConfig) { + t.Fatal("invalid config accepted") + } + } +} + +func TestS3Integration(t *testing.T) { + if os.Getenv("S3_INTEGRATION") != "1" { + t.Skip("explicit S3 integration mode required") + } + config := Config{ + Endpoint: os.Getenv("S3_ENDPOINT"), Region: os.Getenv("S3_REGION"), Bucket: os.Getenv("S3_BUCKET"), + AccessKeyFile: os.Getenv("S3_ACCESS_KEY_ID_FILE"), SecretKeyFile: os.Getenv("S3_SECRET_ACCESS_KEY_FILE"), + } + client, err := New(config) + if err != nil { + t.Fatal(err) + } + defer client.Close() + ctx := context.Background() + id := uuid.NewString() + defer func() { + if err := client.Delete(context.Background(), id); err != nil { + t.Errorf("fixture cleanup: %v", err) + } + }() + var encoded bytes.Buffer + if err := png.Encode(&encoded, image.NewNRGBA(image.Rect(0, 0, 3, 2))); err != nil { + t.Fatal(err) + } + p, err := upload.NewValidator(t.TempDir()).Prepare(ctx, bytes.NewReader(encoded.Bytes()), "image/png", "original.png") + if err != nil { + t.Fatal(err) + } + defer p.Close() + if err := client.Put(ctx, id, p.File, p.SizeBytes, p.ContentType, p.SHA256); err != nil { + t.Fatal(err) + } + t.Log("PASS: validated original uploaded with API credentials") + original, err := client.Open(ctx, id) + if err != nil { + t.Fatal(err) + } + data, readErr := io.ReadAll(original.Body) + closeErr := original.Body.Close() + if readErr != nil || closeErr != nil { + t.Fatalf("download: %v %v", readErr, closeErr) + } + sum := sha256.Sum256(data) + if !bytes.Equal(data, encoded.Bytes()) || original.SizeBytes != p.SizeBytes || original.ContentType != "image/png" || hex.EncodeToString(sum[:]) != p.SHA256 { + t.Fatal("original or metadata mismatch") + } + t.Log("PASS: downloaded original bytes, MIME, size and SHA-256 match") + if err := client.Delete(ctx, id); err != nil { + t.Fatal(err) + } + if _, err := client.Open(ctx, id); !errors.Is(err, ErrNotFound) { + t.Fatalf("missing object: %v", err) + } + if err := client.Delete(ctx, id); err != nil { + t.Fatal("repeat delete failed") + } + t.Log("PASS: deleted object is not found; repeat delete succeeds") + cancelled, cancel := context.WithCancel(ctx) + cancel() + if _, err := client.Open(cancelled, id); !errors.Is(err, context.Canceled) { + t.Fatalf("cancellation: %v", err) + } + t.Log("PASS: caller cancellation propagated") + badSecret := filepath.Join(t.TempDir(), "secret") + if err := os.WriteFile(badSecret, []byte("deliberately-invalid-secret"), 0600); err != nil { + t.Fatal(err) + } + wrongConfig := config + wrongConfig.SecretKeyFile = badSecret + wrongClient, err := New(wrongConfig) + if err != nil { + t.Fatal(err) + } + defer wrongClient.Close() + if _, err := wrongClient.Open(ctx, id); !errors.Is(err, ErrUnavailable) { + t.Fatalf("credential failure: %v", err) + } + t.Log("PASS: incorrect credentials yield sanitized unavailable error") + unavailableConfig := config + unavailableConfig.Endpoint = "http://127.0.0.1:1" + unavailableClient, err := New(unavailableConfig) + if err != nil { + t.Fatal(err) + } + defer unavailableClient.Close() + if _, err := unavailableClient.Open(ctx, id); !errors.Is(err, ErrUnavailable) { + t.Fatalf("unreachable endpoint: %v", err) + } + t.Log("PASS: unreachable endpoint yields sanitized unavailable error") +} diff --git a/internal/password/password.go b/internal/password/password.go new file mode 100644 index 00000000..331cc1a4 --- /dev/null +++ b/internal/password/password.go @@ -0,0 +1,76 @@ +package password + +import ( + "crypto/rand" + "crypto/subtle" + "encoding/base64" + "errors" + "fmt" + "strings" + "unicode/utf8" + + "golang.org/x/crypto/argon2" +) + +const ( + memoryKiB uint32 = 64 * 1024 + iterations uint32 = 3 + parallelism uint8 = 4 + saltLength = 16 + keyLength uint32 = 32 +) + +var ( + ErrInvalidPassword = errors.New("password must contain 12 to 128 UTF-8 code points and a non-whitespace character") + ErrInvalidHash = errors.New("invalid or unsupported password hash") + encoding = base64.RawStdEncoding.Strict() + profile = fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$", + argon2.Version, memoryKiB, iterations, parallelism) +) + +func Validate(value string) error { + if !utf8.ValidString(value) { + return ErrInvalidPassword + } + length := utf8.RuneCountInString(value) + if length < 12 || length > 128 || strings.TrimSpace(value) == "" { + return ErrInvalidPassword + } + return nil +} + +func Hash(value string) (string, error) { + if err := Validate(value); err != nil { + return "", err + } + salt := make([]byte, saltLength) + if _, err := rand.Read(salt); err != nil { + return "", fmt.Errorf("generate password salt: %w", err) + } + key := argon2.IDKey([]byte(value), salt, iterations, memoryKiB, parallelism, keyLength) + return profile + encoding.EncodeToString(salt) + "$" + encoding.EncodeToString(key), nil +} + +func Verify(value, encoded string) (bool, error) { + if len(encoded) != len(profile)+encoding.EncodedLen(saltLength)+1+encoding.EncodedLen(int(keyLength)) || + !strings.HasPrefix(encoded, profile) { + return false, ErrInvalidHash + } + parts := strings.Split(strings.TrimPrefix(encoded, profile), "$") + if len(parts) != 2 { + return false, ErrInvalidHash + } + salt, err := encoding.DecodeString(parts[0]) + if err != nil || len(salt) != saltLength { + return false, ErrInvalidHash + } + expected, err := encoding.DecodeString(parts[1]) + if err != nil || len(expected) != int(keyLength) { + return false, ErrInvalidHash + } + if err := Validate(value); err != nil { + return false, nil + } + actual := argon2.IDKey([]byte(value), salt, iterations, memoryKiB, parallelism, keyLength) + return subtle.ConstantTimeCompare(actual, expected) == 1, nil +} diff --git a/internal/password/password_test.go b/internal/password/password_test.go new file mode 100644 index 00000000..2620b01b --- /dev/null +++ b/internal/password/password_test.go @@ -0,0 +1,98 @@ +package password + +import ( + "errors" + "strings" + "testing" +) + +func TestValidate(t *testing.T) { + for _, tc := range []struct { + name string + value string + valid bool + }{ + {"empty", "", false}, + {"11 characters", strings.Repeat("a", 11), false}, + {"12 characters", strings.Repeat("a", 12), true}, + {"128 Unicode characters", strings.Repeat("я", 128), true}, + {"129 Unicode characters", strings.Repeat("я", 129), false}, + {"invalid UTF-8", strings.Repeat("a", 12) + string([]byte{0xff}), false}, + {"only spaces", strings.Repeat(" ", 12), false}, + {"only Unicode whitespace", strings.Repeat("\u00a0\u2003\t\n", 3), false}, + {"spaces preserved", " пароль:секрет ", true}, + {"colon allowed", "пароль:секрет", true}, + } { + t.Run(tc.name, func(t *testing.T) { + err := Validate(tc.value) + if (err == nil) != tc.valid { + t.Fatalf("valid=%v, error=%v", tc.valid, err) + } + if !tc.valid { + if _, err := Hash(tc.value); !errors.Is(err, ErrInvalidPassword) { + t.Fatalf("Hash error=%v", err) + } + } + }) + } +} + +func TestHashAndVerify(t *testing.T) { + value := " пароль:секрет " + first, err := Hash(value) + if err != nil { + t.Fatal(err) + } + second, err := Hash(value) + if err != nil { + t.Fatal(err) + } + if first == second { + t.Fatal("independent salts produced identical records") + } + for _, encoded := range []string{first, second} { + ok, err := Verify(value, encoded) + if err != nil || !ok { + t.Fatalf("correct password: match=%v error=%v", ok, err) + } + } + for _, wrong := range []string{"другой:пароль", strings.TrimSpace(value), "short"} { + ok, err := Verify(wrong, first) + if err != nil || ok { + t.Fatalf("wrong password: match=%v error=%v", ok, err) + } + } +} + +func TestLongUnicodePasswordIsNotTruncated(t *testing.T) { + value := strings.Repeat("я", 128) + encoded, err := Hash(value) + if err != nil { + t.Fatal(err) + } + ok, err := Verify(value, encoded) + if err != nil || !ok { + t.Fatalf("match=%v error=%v", ok, err) + } + ok, err = Verify(strings.Repeat("я", 127)+"ю", encoded) + if err != nil || ok { + t.Fatalf("last character ignored: match=%v error=%v", ok, err) + } +} + +func TestVerifyRejectsMalformedHash(t *testing.T) { + encoded := profile + encoding.EncodeToString(make([]byte, saltLength)) + "$" + encoding.EncodeToString(make([]byte, keyLength)) + for _, broken := range []string{ + "", "plaintext", encoded[:len(encoded)-1], encoded + "$extra", + strings.Replace(encoded, "v=19", "v=18", 1), + strings.Replace(encoded, "m=65536", "m=999999999", 1), + strings.Replace(encoded, "t=3", "t=0", 1), + strings.Replace(encoded, "p=4", "p=0", 1), + profile + "!" + encoded[len(profile)+1:], + } { + ok, err := Verify("пароль:секрет", broken) + if ok || !errors.Is(err, ErrInvalidHash) { + t.Fatalf("match=%v error=%v", ok, err) + } + } +} diff --git a/internal/service/attachments.go b/internal/service/attachments.go new file mode 100644 index 00000000..093a316e --- /dev/null +++ b/internal/service/attachments.go @@ -0,0 +1,123 @@ +package service + +import ( + "context" + "io" + "log" + "strings" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/objectstore" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/upload" + "github.com/google/uuid" +) + +type Attachments struct { + db *store.PostgresStore + objects *objectstore.S3 + validator *upload.Validator +} + +func NewAttachments(db *store.PostgresStore, objects *objectstore.S3) *Attachments { + return &Attachments{db: db, objects: objects, validator: upload.NewValidator("")} +} + +func (s *Attachments) Ready(ctx context.Context) error { return s.objects.Check(ctx) } + +func (s *Attachments) Create(ctx context.Context, actor, chatID, filename, mime string, body io.Reader, length int64) (model.Attachment, error) { + if !uuidPattern.MatchString(chatID) { + return model.Attachment{}, ErrInvalidRequest + } + chatID = strings.ToLower(chatID) + if _, err := s.db.GetChat(ctx, actor, chatID); err != nil { + return model.Attachment{}, err + } + if length > upload.MaxBytes { + return model.Attachment{}, upload.ErrTooLarge + } + p, err := s.validator.Prepare(ctx, body, mime, filename) + if err != nil { + return model.Attachment{}, err + } + defer func() { + if err := p.Close(); err != nil { + log.Print("attachment staging cleanup failed") + } + }() + id, err := uuid.NewRandom() + if err != nil { + return model.Attachment{}, store.ErrUnavailable + } + a := model.Attachment{ID: id.String(), ChatID: chatID, OwnerID: actor, Filename: filename, + ContentType: p.ContentType, SizeBytes: p.SizeBytes, SHA256: p.SHA256, Status: "ready", Variants: []model.Variant{}} + if err := s.db.BeginUpload(ctx, a.ID, chatID, actor); err != nil { + return model.Attachment{}, err + } + if err := s.objects.Put(ctx, a.ID, p.File, p.SizeBytes, p.ContentType, p.SHA256); err != nil { + return model.Attachment{}, err + } + key, _ := objectstore.ObjectKey(a.ID) + // Publication and intent removal commit together; failed/unknown outcomes + // are resolved by cleanup under the same DB row lock after the grace period. + return s.db.SaveAttachment(ctx, a, key) +} + +type CleanupSummary struct { + DryRun bool `json:"dry_run"` + Candidates int `json:"candidates"` + Deleted int `json:"deleted"` + Protected int `json:"protected"` + WouldDelete int `json:"would_delete"` + Skipped int `json:"skipped"` + Errors int `json:"errors"` +} + +func (s *Attachments) Cleanup(ctx context.Context, dryRun bool) (CleanupSummary, error) { + result := CleanupSummary{DryRun: dryRun} + ids, err := s.db.CleanupCandidates(ctx) + if err != nil { + return result, err + } + result.Candidates = len(ids) + for _, id := range ids { + action, err := s.db.CleanupUpload(ctx, id, dryRun, s.objects.Delete) + if err != nil { + result.Errors++ + continue + } + switch action { + case "deleted": + result.Deleted++ + case "protected": + result.Protected++ + case "would_delete": + result.WouldDelete++ + case "skipped": + result.Skipped++ + } + } + if result.Errors > 0 { + return result, store.ErrUnavailable + } + return result, nil +} + +func (s *Attachments) Get(ctx context.Context, actor, id string) (model.Attachment, error) { + if !uuidPattern.MatchString(id) { + return model.Attachment{}, ErrInvalidRequest + } + return s.db.GetAttachment(ctx, actor, strings.ToLower(id)) +} + +func (s *Attachments) Open(ctx context.Context, a model.Attachment) (objectstore.Original, error) { + original, err := s.objects.Open(ctx, a.ID) + if err != nil { + return objectstore.Original{}, err + } + if original.SizeBytes != a.SizeBytes || original.ContentType != a.ContentType { + original.Body.Close() + return objectstore.Original{}, store.ErrUnavailable + } + return original, nil +} diff --git a/internal/service/chats.go b/internal/service/chats.go new file mode 100644 index 00000000..b5e7082e --- /dev/null +++ b/internal/service/chats.go @@ -0,0 +1,111 @@ +package service + +import ( + "strings" + "unicode/utf8" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" +) + +func validText(value string, maxLength int) bool { + return utf8.ValidString(value) && + strings.TrimSpace(value) != "" && + utf8.RuneCountInString(value) <= maxLength +} + +func (m *Messenger) CreateChat( + actorID string, + title string, + memberIDs []string, +) (model.Chat, error) { + if !validText(title, 100) || + len(memberIDs) < 1 || + len(memberIDs) > 100 { + return model.Chat{}, ErrInvalidRequest + } + + normalized := make([]string, len(memberIDs)) + + for i, id := range memberIDs { + if !uuidPattern.MatchString(id) { + return model.Chat{}, ErrInvalidRequest + } + normalized[i] = strings.ToLower(id) + } + + return m.store.CreateChat(actorID, title, normalized) +} + +func (m *Messenger) GetChat( + actorID string, + chatID string, +) (model.Chat, error) { + if !uuidPattern.MatchString(chatID) { + return model.Chat{}, ErrInvalidRequest + } + + return m.store.GetChat(actorID, strings.ToLower(chatID)) +} + +func (m *Messenger) ListChats( + actorID string, + limit int, + cursor string, +) (Page[model.Chat], error) { + chats := m.store.ListChats(actorID) + + return paginate( + chats, + "chats:"+actorID, + limit, + cursor, + func(chat model.Chat) position { + return position{CreatedAt: chat.CreatedAt, ID: chat.ID} + }, + ) +} + +func (m *Messenger) CreateMessage( + actorID string, + chatID string, + text string, +) (model.Message, error) { + chat, err := m.GetChat(actorID, chatID) + if err != nil { + return model.Message{}, err + } + + if !validText(text, 4000) { + return model.Message{}, ErrInvalidRequest + } + + return m.store.CreateMessage(actorID, chat.ID, text) +} + +func (m *Messenger) ListMessages( + actorID string, + chatID string, + limit int, + cursor string, +) (Page[model.Message], error) { + if !uuidPattern.MatchString(chatID) { + return Page[model.Message]{}, ErrInvalidRequest + } + + chatID = strings.ToLower(chatID) + + messages, err := m.store.ListMessages(actorID, chatID) + if err != nil { + return Page[model.Message]{}, err + } + + return paginate( + messages, + "messages:"+chatID, + limit, + cursor, + func(message model.Message) position { + return position{CreatedAt: message.CreatedAt, ID: message.ID} + }, + ) +} diff --git a/internal/service/idempotency_test.go b/internal/service/idempotency_test.go new file mode 100644 index 00000000..48e4d1b8 --- /dev/null +++ b/internal/service/idempotency_test.go @@ -0,0 +1,25 @@ +package service + +import ( + "context" + "errors" + "strings" + "testing" +) + +func TestIdempotencyKeyValidation(t *testing.T) { + for _, key := range []string{"a", "retry-1._:ABC09", strings.Repeat("a", 128)} { + if !idempotencyKeyPattern.MatchString(key) { + t.Fatalf("valid key rejected: %q", key) + } + } + for _, key := range []string{"", " ", "a b", "ключ", "a\n", strings.Repeat("a", 129)} { + // Nil DB ensures invalid input is rejected before any storage call. + m := NewPersistentMessenger(nil) + _, err := m.CreateMessage(context.Background(), "actor", + "12345678-1234-4234-8234-123456789abc", "hello", key) + if !errors.Is(err, ErrInvalidRequest) { + t.Fatalf("key %q: got %v, want invalid request", key, err) + } + } +} diff --git a/internal/service/messenger.go b/internal/service/messenger.go new file mode 100644 index 00000000..539ce982 --- /dev/null +++ b/internal/service/messenger.go @@ -0,0 +1,76 @@ +package service + +import ( + "errors" + "regexp" + "strings" + "unicode/utf8" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" +) + +var ( + ErrInvalidRequest = errors.New("invalid request") + ErrUnauthorized = errors.New("unauthorized") +) + +var ( + usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`) + uuidPattern = regexp.MustCompile( + `^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`, + ) +) + +type Messenger struct { + store *store.MemoryStore +} + +func NewMessenger(memory *store.MemoryStore) *Messenger { + return &Messenger{ + store: memory, + } +} + +func (m *Messenger) RegisterUser( + username string, + displayName string, +) (model.User, error) { + if !usernamePattern.MatchString(username) { + return model.User{}, ErrInvalidRequest + } + + if !utf8.ValidString(displayName) { + return model.User{}, ErrInvalidRequest + } + + length := utf8.RuneCountInString(displayName) + + if length < 1 || length > 100 { + return model.User{}, ErrInvalidRequest + } + + if strings.TrimSpace(displayName) == "" { + return model.User{}, ErrInvalidRequest + } + + return m.store.CreateUser(username, displayName) +} + +func (m *Messenger) CurrentUser(id string) (model.User, error) { + if !uuidPattern.MatchString(id) { + return model.User{}, ErrUnauthorized + } + + user, err := m.store.GetUser(strings.ToLower(id)) + + if errors.Is(err, store.ErrUserNotFound) { + return model.User{}, ErrUnauthorized + } + + if err != nil { + return model.User{}, err + } + + return user, nil +} diff --git a/internal/service/messenger_test.go b/internal/service/messenger_test.go new file mode 100644 index 00000000..dcb79bc9 --- /dev/null +++ b/internal/service/messenger_test.go @@ -0,0 +1,143 @@ +package service_test + +import ( + "errors" + "strings" + "testing" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" +) + +func TestRegisterUserValidation(t *testing.T) { + tests := []struct { + name string + username string + displayName string + valid bool + }{ + { + name: "valid Unicode name", + username: "alice_01", + displayName: "Алиса 👋", + valid: true, + }, + { + name: "100 Unicode code points", + username: "alice", + displayName: strings.Repeat("👋", 100), + valid: true, + }, + { + name: "101 Unicode code points", + username: "alice", + displayName: strings.Repeat("👋", 101), + }, + { + name: "short username", + username: "ab", + displayName: "Алиса", + }, + { + name: "long username", + username: strings.Repeat("a", 33), + displayName: "Алиса", + }, + { + name: "uppercase username", + username: "Alice", + displayName: "Алиса", + }, + { + name: "non ASCII username", + username: "алиса", + displayName: "Алиса", + }, + { + name: "empty display name", + username: "alice", + displayName: "", + }, + { + name: "whitespace display name", + username: "alice", + displayName: " \t\n", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := service.NewMessenger(store.NewMemoryStore()) + + user, err := m.RegisterUser(tt.username, tt.displayName) + + if !tt.valid { + if !errors.Is(err, service.ErrInvalidRequest) { + t.Fatalf("expected ErrInvalidRequest, got %v", err) + } + return + } + + if err != nil { + t.Fatalf("RegisterUser: %v", err) + } + + if user.Username != tt.username || + user.DisplayName != tt.displayName { + t.Fatalf("user fields changed: %+v", user) + } + }) + } +} + +func TestRegisterDuplicateUsername(t *testing.T) { + m := service.NewMessenger(store.NewMemoryStore()) + + if _, err := m.RegisterUser("alice", "Алиса"); err != nil { + t.Fatalf("first registration: %v", err) + } + + _, err := m.RegisterUser("alice", "Другое имя") + if !errors.Is(err, store.ErrUsernameTaken) { + t.Fatalf("expected ErrUsernameTaken, got %v", err) + } +} + +func TestCurrentUser(t *testing.T) { + m := service.NewMessenger(store.NewMemoryStore()) + + created, err := m.RegisterUser("alice", "Алиса") + if err != nil { + t.Fatalf("RegisterUser: %v", err) + } + + for _, id := range []string{ + created.ID, + strings.ToUpper(created.ID), + } { + found, err := m.CurrentUser(id) + if err != nil { + t.Fatalf("CurrentUser(%q): %v", id, err) + } + + if found != created { + t.Fatalf("got %+v, want %+v", found, created) + } + } + + for _, id := range []string{ + "", + "not-a-uuid", + "00000000-0000-4000-8000-000000000000", + } { + _, err := m.CurrentUser(id) + + if !errors.Is(err, service.ErrUnauthorized) { + t.Errorf( + "CurrentUser(%q): expected ErrUnauthorized, got %v", + id, + err, + ) + } + } +} diff --git a/internal/service/pagination.go b/internal/service/pagination.go new file mode 100644 index 00000000..55232267 --- /dev/null +++ b/internal/service/pagination.go @@ -0,0 +1,98 @@ +package service + +import ( + "encoding/base64" + "encoding/json" + "sort" + "time" +) + +type Page[T any] struct { + Items []T `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +type position struct { + CreatedAt time.Time `json:"created_at"` + ID string `json:"id"` +} + +type cursorData struct { + Scope string `json:"scope"` + After position `json:"after"` +} + +func less(a, b position) bool { + if a.CreatedAt.Equal(b.CreatedAt) { + return a.ID < b.ID + } + return a.CreatedAt.Before(b.CreatedAt) +} + +func paginate[T any]( + items []T, + scope string, + limit int, + cursor string, + key func(T) position, +) (Page[T], error) { + if limit < 1 || limit > 100 { + return Page[T]{}, ErrInvalidRequest + } + + var after *position + + if cursor != "" { + raw, err := base64.RawURLEncoding.DecodeString(cursor) + if err != nil { + return Page[T]{}, ErrInvalidRequest + } + + var decoded cursorData + if err := json.Unmarshal(raw, &decoded); err != nil { + return Page[T]{}, ErrInvalidRequest + } + + if decoded.Scope != scope || + decoded.After.CreatedAt.IsZero() || + !uuidPattern.MatchString(decoded.After.ID) { + return Page[T]{}, ErrInvalidRequest + } + + after = &decoded.After + } + + sort.Slice(items, func(i, j int) bool { + return less(key(items[i]), key(items[j])) + }) + + start := 0 + + if after != nil { + start = sort.Search(len(items), func(i int) bool { + return less(*after, key(items[i])) + }) + } + + end := min(start+limit, len(items)) + + page := Page[T]{ + Items: make([]T, end-start), + } + copy(page.Items, items[start:end]) + + if end < len(items) { + raw, err := json.Marshal(cursorData{ + Scope: scope, + After: key(items[end-1]), + }) + if err != nil { + return Page[T]{}, err + } + + next := base64.RawURLEncoding.EncodeToString(raw) + page.NextCursor = &next + } + + return page, nil +} diff --git a/internal/service/persistent.go b/internal/service/persistent.go new file mode 100644 index 00000000..5d081082 --- /dev/null +++ b/internal/service/persistent.go @@ -0,0 +1,134 @@ +package service + +import ( + "context" + "encoding/base64" + "encoding/json" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" + "regexp" + "strings" + "unicode/utf8" +) + +type PersistentMessenger struct{ db *store.PostgresStore } + +func NewPersistentMessenger(db *store.PostgresStore) *PersistentMessenger { + return &PersistentMessenger{db: db} +} + +func (m *PersistentMessenger) CreateChat(ctx context.Context, actor, title string, members []string) (model.Chat, error) { + if !validText(title, 100) || len(members) < 1 || len(members) > 100 { + return model.Chat{}, ErrInvalidRequest + } + normalized := make([]string, len(members)) + seen := make(map[string]bool) + for i, id := range members { + if !uuidPattern.MatchString(id) { + return model.Chat{}, ErrInvalidRequest + } + id = strings.ToLower(id) + if seen[id] { + return model.Chat{}, store.ErrInvalidMembers + } + seen[id] = true + normalized[i] = id + } + if !seen[actor] { + return model.Chat{}, store.ErrInvalidMembers + } + return m.db.CreateChat(ctx, title, normalized) +} +func (m *PersistentMessenger) GetChat(ctx context.Context, actor, id string) (model.Chat, error) { + if !uuidPattern.MatchString(id) { + return model.Chat{}, ErrInvalidRequest + } + return m.db.GetChat(ctx, actor, strings.ToLower(id)) +} +func decodePosition(cursor, scope string, limit int) (*store.Position, error) { + if limit < 1 || limit > 100 { + return nil, ErrInvalidRequest + } + if cursor == "" { + return nil, nil + } + if len(cursor) > 2048 { + return nil, ErrInvalidRequest + } + raw, err := base64.RawURLEncoding.DecodeString(cursor) + if err != nil { + return nil, ErrInvalidRequest + } + var data cursorData + if json.Unmarshal(raw, &data) != nil || data.Scope != scope || data.After.CreatedAt.IsZero() || !uuidPattern.MatchString(data.After.ID) { + return nil, ErrInvalidRequest + } + if data.After.CreatedAt.Year() < 1 || data.After.CreatedAt.Year() > 9999 { + return nil, ErrInvalidRequest + } + return &store.Position{CreatedAt: data.After.CreatedAt, ID: strings.ToLower(data.After.ID)}, nil +} +func databasePage[T any](items []T, limit int, scope string, key func(T) position) (Page[T], error) { + page := Page[T]{Items: items} + if len(items) <= limit { + return page, nil + } + page.Items = items[:limit] + raw, err := json.Marshal(cursorData{Scope: scope, After: key(page.Items[limit-1])}) + if err != nil { + return Page[T]{}, err + } + cursor := base64.RawURLEncoding.EncodeToString(raw) + page.NextCursor = &cursor + return page, nil +} +func (m *PersistentMessenger) ListChats(ctx context.Context, actor string, limit int, cursor string) (Page[model.Chat], error) { + scope := "chats:" + actor + after, err := decodePosition(cursor, scope, limit) + if err != nil { + return Page[model.Chat]{}, err + } + items, err := m.db.ChatPage(ctx, actor, limit+1, after) + if err != nil { + return Page[model.Chat]{}, err + } + return databasePage(items, limit, scope, func(c model.Chat) position { return position{CreatedAt: c.CreatedAt, ID: c.ID} }) +} + +var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`) + +func (m *PersistentMessenger) CreateMessage(ctx context.Context, actor, chatID, text, key string, attachmentIDs ...string) (model.Message, error) { + if !uuidPattern.MatchString(chatID) || !utf8.ValidString(text) || utf8.RuneCountInString(text) > 4000 || (strings.TrimSpace(text) == "" && len(attachmentIDs) == 0) || len(attachmentIDs) > 10 || !idempotencyKeyPattern.MatchString(key) { + return model.Message{}, ErrInvalidRequest + } + normalized := make([]string, len(attachmentIDs)) + seen := make(map[string]bool, len(attachmentIDs)) + for i, id := range attachmentIDs { + if !uuidPattern.MatchString(id) { + return model.Message{}, ErrInvalidRequest + } + id = strings.ToLower(id) + if seen[id] { + return model.Message{}, ErrInvalidRequest + } + seen[id] = true + normalized[i] = id + } + return m.db.CreateMessage(ctx, actor, strings.ToLower(chatID), text, key, normalized...) +} +func (m *PersistentMessenger) ListMessages(ctx context.Context, actor, chatID string, limit int, cursor string) (Page[model.Message], error) { + chat, err := m.GetChat(ctx, actor, chatID) + if err != nil { + return Page[model.Message]{}, err + } + scope := "messages:" + chat.ID + after, err := decodePosition(cursor, scope, limit) + if err != nil { + return Page[model.Message]{}, err + } + items, err := m.db.MessagePage(ctx, chat.ID, limit+1, after) + if err != nil { + return Page[model.Message]{}, err + } + return databasePage(items, limit, scope, func(message model.Message) position { return position{CreatedAt: message.CreatedAt, ID: message.ID} }) +} diff --git a/internal/service/registration.go b/internal/service/registration.go new file mode 100644 index 00000000..3f4c7764 --- /dev/null +++ b/internal/service/registration.go @@ -0,0 +1,45 @@ +package service + +import ( + "context" + "time" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/password" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" +) + +type UserWriter interface { + CreateUser(context.Context, string, string, string) (model.User, error) +} + +type Registration struct { + users UserWriter + hashSlots chan struct{} +} + +func NewRegistration(users UserWriter) *Registration { + return &Registration{users: users, hashSlots: make(chan struct{}, 2)} +} + +func (s *Registration) Register(ctx context.Context, username, displayName, secret string) (model.User, error) { + if !usernamePattern.MatchString(username) || !validText(displayName, 100) || password.Validate(secret) != nil { + return model.User{}, ErrInvalidRequest + } + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + select { + case s.hashSlots <- struct{}{}: + case <-ctx.Done(): + return model.User{}, store.ErrUnavailable + } + hash, err := password.Hash(secret) + <-s.hashSlots + if err != nil { + return model.User{}, err + } + if ctx.Err() != nil { + return model.User{}, store.ErrUnavailable + } + return s.users.CreateUser(ctx, username, displayName, hash) +} diff --git a/internal/service/registration_test.go b/internal/service/registration_test.go new file mode 100644 index 00000000..79c071bf --- /dev/null +++ b/internal/service/registration_test.go @@ -0,0 +1,55 @@ +package service + +import ( + "context" + "errors" + "testing" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/password" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" +) + +type registrationWriter struct { + calls int + hash string + err error +} + +func (w *registrationWriter) CreateUser(_ context.Context, username, displayName, hash string) (model.User, error) { + w.calls++ + w.hash = hash + return model.User{Username: username, DisplayName: displayName}, w.err +} + +func TestRegistrationHashesBeforeStorage(t *testing.T) { + w := ®istrationWriter{} + s := NewRegistration(w) + user, err := s.Register(context.Background(), "alice", "Алиса", "учебный:пароль") + if err != nil || user.Username != "alice" || w.calls != 1 { + t.Fatalf("registration failed: %v", err) + } + ok, err := password.Verify("учебный:пароль", w.hash) + if err != nil || !ok { + t.Fatalf("stored value is not a valid password hash: %v", err) + } + w.err = store.ErrUsernameTaken + _, err = s.Register(context.Background(), "alice", "Алиса", "учебный:пароль") + if !errors.Is(err, store.ErrUsernameTaken) { + t.Fatalf("conflict lost: %v", err) + } +} + +func TestRegistrationRejectsBeforeStorage(t *testing.T) { + w := ®istrationWriter{} + s := NewRegistration(w) + for _, fields := range [][3]string{{"Alice", "Алиса", "учебный:пароль"}, {"alice", " ", "учебный:пароль"}, {"alice", "Алиса", "short"}} { + _, err := s.Register(context.Background(), fields[0], fields[1], fields[2]) + if !errors.Is(err, ErrInvalidRequest) { + t.Fatalf("invalid input accepted: %v", err) + } + } + if w.calls != 0 { + t.Fatal("invalid data reached storage") + } +} diff --git a/internal/service/sessions.go b/internal/service/sessions.go new file mode 100644 index 00000000..6a55f1a1 --- /dev/null +++ b/internal/service/sessions.go @@ -0,0 +1,125 @@ +package service + +import ( + "context" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "errors" + "time" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/password" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" +) + +type SessionStore interface { + Credentials(context.Context, string) (model.User, string, error) + CreateSession(context.Context, []byte, string, int64) (time.Time, error) + SessionUser(context.Context, []byte) (model.User, error) + DeleteSession(context.Context, []byte) error +} + +type Sessions struct { + db SessionStore + ttl int64 + hashSlots chan struct{} + dummyHash string +} + +type SessionResponse struct { + Token string `json:"token"` + TokenType string `json:"token_type"` + ExpiresAt time.Time `json:"expires_at"` + User model.User `json:"user"` +} + +func NewSessions(db SessionStore, registration *Registration, ttl int64) (*Sessions, error) { + if ttl < 1 || ttl > 31536000 { + return nil, errors.New("SESSION_TTL_SECONDS must be between 1 and 31536000") + } + dummy, err := password.Hash("dummy-password-for-missing-user") + if err != nil { + return nil, err + } + return &Sessions{db: db, ttl: ttl, hashSlots: registration.hashSlots, dummyHash: dummy}, nil +} + +func (s *Sessions) Login(ctx context.Context, username, secret string) (SessionResponse, error) { + if !usernamePattern.MatchString(username) || password.Validate(secret) != nil { + return SessionResponse{}, ErrUnauthorized + } + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + user, hash, err := s.db.Credentials(ctx, username) + missing := errors.Is(err, store.ErrUserNotFound) + if err != nil && !missing { + return SessionResponse{}, err + } + if missing { + hash = s.dummyHash + } + select { + case s.hashSlots <- struct{}{}: + case <-ctx.Done(): + return SessionResponse{}, store.ErrUnavailable + } + matches, err := password.Verify(secret, hash) + <-s.hashSlots + if err != nil { + return SessionResponse{}, errors.New("stored password hash is invalid") + } + if ctx.Err() != nil { + return SessionResponse{}, store.ErrUnavailable + } + if missing || !matches { + return SessionResponse{}, ErrUnauthorized + } + raw := make([]byte, 32) + if _, err := rand.Read(raw); err != nil { + return SessionResponse{}, errors.New("cannot generate session token") + } + token := base64.RawURLEncoding.EncodeToString(raw) + digest := sha256.Sum256([]byte(token)) + expires, err := s.db.CreateSession(ctx, digest[:], user.ID, s.ttl) + if err != nil { + return SessionResponse{}, err + } + return SessionResponse{Token: token, TokenType: "Bearer", ExpiresAt: expires, User: user}, nil +} + +func tokenDigest(token string) ([]byte, error) { + if len(token) != 43 { + return nil, ErrUnauthorized + } + raw, err := base64.RawURLEncoding.Strict().DecodeString(token) + if err != nil || len(raw) != 32 { + return nil, ErrUnauthorized + } + digest := sha256.Sum256([]byte(token)) + return digest[:], nil +} + +func (s *Sessions) CurrentUser(ctx context.Context, token string) (model.User, error) { + digest, err := tokenDigest(token) + if err != nil { + return model.User{}, err + } + user, err := s.db.SessionUser(ctx, digest) + if errors.Is(err, store.ErrSessionNotFound) { + return model.User{}, ErrUnauthorized + } + return user, err +} + +func (s *Sessions) Logout(ctx context.Context, token string) error { + digest, err := tokenDigest(token) + if err != nil { + return err + } + err = s.db.DeleteSession(ctx, digest) + if errors.Is(err, store.ErrSessionNotFound) { + return ErrUnauthorized + } + return err +} diff --git a/internal/service/sessions_test.go b/internal/service/sessions_test.go new file mode 100644 index 00000000..45dbcabf --- /dev/null +++ b/internal/service/sessions_test.go @@ -0,0 +1,96 @@ +package service + +import ( + "context" + "crypto/sha256" + "errors" + "testing" + "time" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/password" + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" +) + +type sessionFake struct { + hash string + stored []byte + writes int + unavailable bool +} + +func (f *sessionFake) Credentials(_ context.Context, username string) (model.User, string, error) { + if f.unavailable { + return model.User{}, "", store.ErrUnavailable + } + if username != "alice" { + return model.User{}, "", store.ErrUserNotFound + } + return model.User{ID: "user-id", Username: username}, f.hash, nil +} +func (f *sessionFake) CreateSession(_ context.Context, hash []byte, _ string, _ int64) (time.Time, error) { + f.writes++ + f.stored = append([]byte(nil), hash...) + return time.Now().UTC().Add(time.Hour), nil +} +func (f *sessionFake) SessionUser(_ context.Context, hash []byte) (model.User, error) { + if f.unavailable { + return model.User{}, store.ErrUnavailable + } + if string(hash) != string(f.stored) || f.stored == nil { + return model.User{}, store.ErrSessionNotFound + } + return model.User{ID: "user-id", Username: "alice"}, nil +} +func (f *sessionFake) DeleteSession(_ context.Context, hash []byte) error { + if _, err := f.SessionUser(context.Background(), hash); err != nil { + return err + } + f.stored = nil + return nil +} + +func TestSessionLifecycle(t *testing.T) { + hash, err := password.Hash("учебный:пароль") + if err != nil { + t.Fatal(err) + } + db := &sessionFake{hash: hash} + s, err := NewSessions(db, NewRegistration(nil), 3600) + if err != nil { + t.Fatal(err) + } + for _, username := range []string{"alice", "missing"} { + if _, err := s.Login(context.Background(), username, "неверный:пароль"); !errors.Is(err, ErrUnauthorized) { + t.Fatalf("bad credentials: %v", err) + } + } + if db.writes != 0 { + t.Fatal("invalid login created a session") + } + session, err := s.Login(context.Background(), "alice", "учебный:пароль") + if err != nil { + t.Fatal(err) + } + digest := sha256.Sum256([]byte(session.Token)) + if len(session.Token) != 43 || string(db.stored) != string(digest[:]) { + t.Fatal("must store digest of random token") + } + if _, err := s.CurrentUser(context.Background(), session.Token); err != nil { + t.Fatal(err) + } + db.unavailable = true + if _, err := s.CurrentUser(context.Background(), session.Token); !errors.Is(err, store.ErrUnavailable) { + t.Fatalf("outage: %v", err) + } + db.unavailable = false + if err := s.Logout(context.Background(), session.Token); err != nil { + t.Fatal(err) + } + if _, err := s.CurrentUser(context.Background(), session.Token); !errors.Is(err, ErrUnauthorized) { + t.Fatalf("revoked: %v", err) + } + if err := s.Logout(context.Background(), session.Token); !errors.Is(err, ErrUnauthorized) { + t.Fatalf("repeated logout: %v", err) + } +} diff --git a/internal/store/chats.go b/internal/store/chats.go new file mode 100644 index 00000000..feea5c88 --- /dev/null +++ b/internal/store/chats.go @@ -0,0 +1,172 @@ +package store + +import ( + "errors" + "slices" + "time" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" +) + +var ( + ErrNotFound = errors.New("resource not found") + ErrInvalidMembers = errors.New("invalid chat members") +) + +func cloneChat(chat model.Chat) model.Chat { + chat.MemberIDs = slices.Clone(chat.MemberIDs) + return chat +} + +func (s *MemoryStore) CreateChat( + actorID string, + title string, + memberIDs []string, +) (model.Chat, error) { + s.mu.Lock() + defer s.mu.Unlock() + + seen := make(map[string]bool, len(memberIDs)) + + for _, id := range memberIDs { + if seen[id] { + return model.Chat{}, ErrInvalidMembers + } + + if _, exists := s.usersByID[id]; !exists { + return model.Chat{}, ErrInvalidMembers + } + + seen[id] = true + } + + if !seen[actorID] { + return model.Chat{}, ErrInvalidMembers + } + + id, err := newUUID() + if err != nil { + return model.Chat{}, err + } + + for { + if _, exists := s.chatsByID[id]; !exists { + break + } + id, err = newUUID() + if err != nil { + return model.Chat{}, err + } + } + + chat := model.Chat{ + ID: id, + Title: title, + MemberIDs: slices.Clone(memberIDs), + CreatedAt: time.Now().UTC(), + } + + s.chatsByID[id] = chat + s.messagesByChat[id] = []model.Message{} + + return cloneChat(chat), nil +} + +func (s *MemoryStore) GetChat( + actorID string, + chatID string, +) (model.Chat, error) { + s.mu.RLock() + defer s.mu.RUnlock() + + chat, exists := s.chatsByID[chatID] + if !exists || !slices.Contains(chat.MemberIDs, actorID) { + return model.Chat{}, ErrNotFound + } + + return cloneChat(chat), nil +} + +func (s *MemoryStore) ListChats(actorID string) []model.Chat { + s.mu.RLock() + defer s.mu.RUnlock() + + result := make([]model.Chat, 0) + + for _, chat := range s.chatsByID { + if slices.Contains(chat.MemberIDs, actorID) { + result = append(result, cloneChat(chat)) + } + } + + return result +} + +func (s *MemoryStore) CreateMessage( + actorID string, + chatID string, + text string, +) (model.Message, error) { + s.mu.Lock() + defer s.mu.Unlock() + + chat, exists := s.chatsByID[chatID] + if !exists || !slices.Contains(chat.MemberIDs, actorID) { + return model.Message{}, ErrNotFound + } + + id, err := newUUID() + if err != nil { + return model.Message{}, err + } + + history := s.messagesByChat[chatID] + + for slices.ContainsFunc(history, func(message model.Message) bool { + return message.ID == id + }) { + id, err = newUUID() + if err != nil { + return model.Message{}, err + } + } + + createdAt := time.Now().UTC() + + if len(history) > 0 { + last := history[len(history)-1].CreatedAt + if !createdAt.After(last) { + createdAt = last.Add(time.Nanosecond) + } + } + + message := model.Message{ + AttachmentIDs: []string{}, + ID: id, + ChatID: chatID, + SenderID: actorID, + Text: text, + CreatedAt: createdAt, + } + + s.messagesByChat[chatID] = append(history, message) + return message, nil +} + +func (s *MemoryStore) ListMessages( + actorID string, + chatID string, +) ([]model.Message, error) { + s.mu.RLock() + defer s.mu.RUnlock() + + chat, exists := s.chatsByID[chatID] + if !exists || !slices.Contains(chat.MemberIDs, actorID) { + return nil, ErrNotFound + } + + result := make([]model.Message, len(s.messagesByChat[chatID])) + copy(result, s.messagesByChat[chatID]) + + return result, nil +} diff --git a/internal/store/memory.go b/internal/store/memory.go new file mode 100644 index 00000000..851027b7 --- /dev/null +++ b/internal/store/memory.go @@ -0,0 +1,106 @@ +package store + +import ( + "crypto/rand" + "errors" + "fmt" + "sync" + "time" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" +) + +var ( + ErrUsernameTaken = errors.New("username already exists") + ErrUserNotFound = errors.New("user not found") +) + +type MemoryStore struct { + mu sync.RWMutex + + usersByID map[string]model.User + userIDsByName map[string]string + chatsByID map[string]model.Chat + messagesByChat map[string][]model.Message +} + +func NewMemoryStore() *MemoryStore { + return &MemoryStore{ + usersByID: make(map[string]model.User), + userIDsByName: make(map[string]string), + chatsByID: make(map[string]model.Chat), + messagesByChat: make(map[string][]model.Message), + } +} + +func (s *MemoryStore) CreateUser( + username string, + displayName string, +) (model.User, error) { + s.mu.Lock() + defer s.mu.Unlock() + + if _, exists := s.userIDsByName[username]; exists { + return model.User{}, ErrUsernameTaken + } + + id, err := newUUID() + if err != nil { + return model.User{}, fmt.Errorf("generate user ID: %w", err) + } + + for { + if _, exists := s.usersByID[id]; !exists { + break + } + + id, err = newUUID() + if err != nil { + return model.User{}, fmt.Errorf("generate user ID: %w", err) + } + } + + user := model.User{ + ID: id, + Username: username, + DisplayName: displayName, + CreatedAt: time.Now().UTC(), + } + + s.usersByID[user.ID] = user + s.userIDsByName[user.Username] = user.ID + + return user, nil +} + +func (s *MemoryStore) GetUser(id string) (model.User, error) { + s.mu.RLock() + defer s.mu.RUnlock() + + user, exists := s.usersByID[id] + if !exists { + return model.User{}, ErrUserNotFound + } + + return user, nil +} + +func newUUID() (string, error) { + var value [16]byte + + if _, err := rand.Read(value[:]); err != nil { + return "", err + } + + value[6] = (value[6] & 0x0f) | 0x40 + value[8] = (value[8] & 0x3f) | 0x80 + + return fmt.Sprintf( + "%x-%x-%x-%x-%x", + value[0:4], + value[4:6], + value[6:8], + value[8:10], + value[10:16], + ), nil +} diff --git a/internal/store/memory_test.go b/internal/store/memory_test.go new file mode 100644 index 00000000..4d1c67a6 --- /dev/null +++ b/internal/store/memory_test.go @@ -0,0 +1,89 @@ +package store_test + +import ( + "errors" + "sync" + "testing" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" +) + +func TestCreateAndGetUser(t *testing.T) { + s := store.NewMemoryStore() + + created, err := s.CreateUser("alice", "Алиса") + if err != nil { + t.Fatalf("CreateUser: %v", err) + } + + if created.ID == "" { + t.Fatal("user ID is empty") + } + + if created.CreatedAt.IsZero() { + t.Fatal("creation time is empty") + } + + found, err := s.GetUser(created.ID) + if err != nil { + t.Fatalf("GetUser: %v", err) + } + + if found != created { + t.Fatalf("stored user differs: got %+v, want %+v", found, created) + } + + _, err = s.GetUser("00000000-0000-4000-8000-000000000000") + if !errors.Is(err, store.ErrUserNotFound) { + t.Fatalf("expected ErrUserNotFound, got %v", err) + } +} + +func TestConcurrentDuplicateUsername(t *testing.T) { + s := store.NewMemoryStore() + + const clients = 10 + + start := make(chan struct{}) + results := make(chan error, clients) + + var wg sync.WaitGroup + + for i := 0; i < clients; i++ { + wg.Add(1) + + go func() { + defer wg.Done() + + <-start + _, err := s.CreateUser("alice", "Алиса") + results <- err + }() + } + + close(start) + wg.Wait() + close(results) + + var created, conflicts int + + for err := range results { + switch { + case err == nil: + created++ + case errors.Is(err, store.ErrUsernameTaken): + conflicts++ + default: + t.Errorf("unexpected error: %v", err) + } + } + + if created != 1 || conflicts != clients-1 { + t.Fatalf( + "expected 1 creation and %d conflicts; got %d creations and %d conflicts", + clients-1, + created, + conflicts, + ) + } +} diff --git a/internal/store/postgres.go b/internal/store/postgres.go new file mode 100644 index 00000000..08aff21e --- /dev/null +++ b/internal/store/postgres.go @@ -0,0 +1,51 @@ +package store + +import ( + "context" + "errors" + "os" + "strings" + "time" + + "github.com/jackc/pgx/v5/pgxpool" +) + +func OpenPostgres(ctx context.Context, passwordFile string) (*pgxpool.Pool, error) { + password, err := os.ReadFile(passwordFile) + if err != nil { + return nil, errors.New("cannot read PostgreSQL password file") + } + secret := strings.TrimRight(string(password), "\r\n") + if secret == "" { + return nil, errors.New("PostgreSQL password file is empty") + } + + config, err := pgxpool.ParseConfig( + "host=postgres port=5432 dbname=messenger user=messenger sslmode=disable", + ) + if err != nil { + return nil, errors.New("invalid PostgreSQL configuration") + } + config.ConnConfig.Password = secret + config.ConnConfig.ConnectTimeout = 3 * time.Second + config.ConnConfig.RuntimeParams["statement_timeout"] = "3000" + config.ConnConfig.RuntimeParams["lock_timeout"] = "2000" + config.ConnConfig.RuntimeParams["application_name"] = "messenger-lab2" + config.MaxConns = 10 + config.MinConns = 0 + config.MaxConnLifetime = 30 * time.Minute + config.MaxConnIdleTime = 5 * time.Minute + + connectCtx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + + pool, err := pgxpool.NewWithConfig(connectCtx, config) + if err != nil { + return nil, errors.New("cannot create PostgreSQL pool") + } + if err := pool.Ping(connectCtx); err != nil { + pool.Close() + return nil, errors.New("PostgreSQL connection check failed") + } + return pool, nil +} diff --git a/internal/store/postgres_attachments.go b/internal/store/postgres_attachments.go new file mode 100644 index 00000000..5db8f290 --- /dev/null +++ b/internal/store/postgres_attachments.go @@ -0,0 +1,56 @@ +package store + +import ( + "context" + "errors" + "time" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" + "github.com/jackc/pgx/v5" +) + +func (s *PostgresStore) SaveAttachment(ctx context.Context, a model.Attachment, objectKey string) (model.Attachment, error) { + ctx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + tx, err := s.pool.Begin(ctx) + if err != nil { + return model.Attachment{}, ErrUnavailable + } + defer tx.Rollback(context.Background()) + var id string + err = tx.QueryRow(ctx, `SELECT id::text FROM upload_intents WHERE id=$1 AND state='pending' AND expires_at>clock_timestamp() FOR UPDATE`, a.ID).Scan(&id) + if err != nil { + return model.Attachment{}, ErrUnavailable + } + err = tx.QueryRow(ctx, `INSERT INTO attachments(id,chat_id,owner_id,object_key,filename,content_type,size_bytes,sha256) + VALUES($1,$2,$3,$4,$5,$6,$7,$8) RETURNING created_at,updated_at`, + a.ID, a.ChatID, a.OwnerID, objectKey, a.Filename, a.ContentType, a.SizeBytes, a.SHA256).Scan(&a.CreatedAt, &a.UpdatedAt) + if err != nil { + return model.Attachment{}, ErrUnavailable + } + if _, err := tx.Exec(ctx, `DELETE FROM upload_intents WHERE id=$1`, a.ID); err != nil { + return model.Attachment{}, ErrUnavailable + } + if err := tx.Commit(ctx); err != nil { + return model.Attachment{}, ErrUnavailable + } + a.CreatedAt, a.UpdatedAt = a.CreatedAt.UTC(), a.UpdatedAt.UTC() + return a, nil +} + +func (s *PostgresStore) GetAttachment(ctx context.Context, actor, id string) (model.Attachment, error) { + ctx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + a := model.Attachment{Variants: []model.Variant{}} + err := s.pool.QueryRow(ctx, `SELECT a.id::text,a.chat_id::text,a.owner_id::text,a.filename,a.content_type,a.size_bytes,a.sha256,a.status,a.created_at,a.updated_at + FROM attachments a WHERE a.id=$1 AND EXISTS(SELECT 1 FROM chat_members m WHERE m.chat_id=a.chat_id AND m.user_id=$2)`, id, actor). + Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType, &a.SizeBytes, &a.SHA256, &a.Status, &a.CreatedAt, &a.UpdatedAt) + if errors.Is(err, pgx.ErrNoRows) { + return model.Attachment{}, ErrNotFound + } + if err != nil { + return model.Attachment{}, ErrUnavailable + } + a.CreatedAt, a.UpdatedAt = a.CreatedAt.UTC(), a.UpdatedAt.UTC() + return a, nil +} diff --git a/internal/store/postgres_chats.go b/internal/store/postgres_chats.go new file mode 100644 index 00000000..c0086b38 --- /dev/null +++ b/internal/store/postgres_chats.go @@ -0,0 +1,213 @@ +package store + +import ( + "context" + "errors" + "slices" + "time" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" +) + +type Position struct { + CreatedAt time.Time + ID string +} + +func (s *PostgresStore) CreateChat(ctx context.Context, title string, members []string) (model.Chat, error) { + ctx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + id, err := newUUID() + if err != nil { + return model.Chat{}, ErrUnavailable + } + tx, err := s.pool.Begin(ctx) + if err != nil { + return model.Chat{}, ErrUnavailable + } + defer tx.Rollback(context.Background()) + chat := model.Chat{ID: id, Title: title, MemberIDs: members} + err = tx.QueryRow(ctx, `INSERT INTO chats(id,title) VALUES($1,$2) RETURNING created_at`, id, title).Scan(&chat.CreatedAt) + if err != nil { + return model.Chat{}, ErrUnavailable + } + for _, member := range members { + _, err = tx.Exec(ctx, `INSERT INTO chat_members(chat_id,user_id) VALUES($1,$2)`, id, member) + if err != nil { + var pgErr *pgconn.PgError + if errors.As(err, &pgErr) && (pgErr.Code == "23503" || pgErr.Code == "23505") { + return model.Chat{}, ErrInvalidMembers + } + return model.Chat{}, ErrUnavailable + } + } + if err := tx.Commit(ctx); err != nil { + return model.Chat{}, ErrUnavailable + } + chat.CreatedAt = chat.CreatedAt.UTC() + return chat, nil +} + +const chatColumns = `c.id::text,c.title,c.created_at,ARRAY(SELECT m.user_id::text FROM chat_members m WHERE m.chat_id=c.id ORDER BY m.user_id)` + +func (s *PostgresStore) GetChat(ctx context.Context, actor, id string) (model.Chat, error) { + ctx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + var chat model.Chat + err := s.pool.QueryRow(ctx, `SELECT `+chatColumns+` FROM chats c WHERE c.id=$1 AND EXISTS(SELECT 1 FROM chat_members WHERE chat_id=c.id AND user_id=$2)`, id, actor).Scan(&chat.ID, &chat.Title, &chat.CreatedAt, &chat.MemberIDs) + if errors.Is(err, pgx.ErrNoRows) { + return model.Chat{}, ErrNotFound + } + if err != nil { + return model.Chat{}, ErrUnavailable + } + chat.CreatedAt = chat.CreatedAt.UTC() + return chat, nil +} + +func (s *PostgresStore) ChatPage(ctx context.Context, actor string, limit int, after *Position) ([]model.Chat, error) { + ctx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + query := `SELECT ` + chatColumns + ` FROM chats c WHERE EXISTS(SELECT 1 FROM chat_members WHERE chat_id=c.id AND user_id=$1)` + args := []any{actor, limit} + if after != nil { + query += ` AND (c.created_at,c.id)>($3,$4::uuid)` + args = append(args, after.CreatedAt, after.ID) + } + query += ` ORDER BY c.created_at,c.id LIMIT $2` + rows, err := s.pool.Query(ctx, query, args...) + if err != nil { + return nil, ErrUnavailable + } + defer rows.Close() + items := make([]model.Chat, 0) + for rows.Next() { + var chat model.Chat + if err := rows.Scan(&chat.ID, &chat.Title, &chat.CreatedAt, &chat.MemberIDs); err != nil { + return nil, ErrUnavailable + } + chat.CreatedAt = chat.CreatedAt.UTC() + items = append(items, chat) + } + if rows.Err() != nil { + return nil, ErrUnavailable + } + return items, nil +} + +var ErrIdempotencyConflict = errors.New("idempotency key already used for another message") +var ErrAttachmentNotReady = errors.New("attachment is not ready") + +const messageColumns = `m.id::text,m.chat_id::text,m.sender_id::text,m.text,m.created_at, + ARRAY(SELECT ma.attachment_id::text FROM message_attachments ma WHERE ma.message_id=m.id ORDER BY ma.position)` + +func (s *PostgresStore) CreateMessage(ctx context.Context, actor, chatID, text, key string, attachmentIDs ...string) (model.Message, error) { + ctx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + id, err := newUUID() + if err != nil { + return model.Message{}, ErrUnavailable + } + tx, err := s.pool.Begin(ctx) + if err != nil { + return model.Message{}, ErrUnavailable + } + defer tx.Rollback(context.Background()) + + var locked string + err = tx.QueryRow(ctx, `SELECT c.id::text FROM chats c WHERE c.id=$1 AND EXISTS(SELECT 1 FROM chat_members WHERE chat_id=c.id AND user_id=$2) FOR UPDATE OF c`, chatID, actor).Scan(&locked) + if errors.Is(err, pgx.ErrNoRows) { + return model.Message{}, ErrNotFound + } + if err != nil { + return model.Message{}, ErrUnavailable + } + var message model.Message + + err = tx.QueryRow(ctx, `SELECT `+messageColumns+` + FROM message_keys k JOIN messages m ON m.id=k.message_id + WHERE k.user_id=$1 AND k.chat_id=$2 AND k.key=$3 AND k.expires_at>clock_timestamp()`, + actor, chatID, key).Scan(&message.ID, &message.ChatID, &message.SenderID, &message.Text, &message.CreatedAt, &message.AttachmentIDs) + if err == nil { + if message.Text != text || !slices.Equal(message.AttachmentIDs, attachmentIDs) { + return model.Message{}, ErrIdempotencyConflict + } + message.CreatedAt = message.CreatedAt.UTC() + return message, nil + } + if !errors.Is(err, pgx.ErrNoRows) { + return model.Message{}, ErrUnavailable + } + for _, attachmentID := range attachmentIDs { + var status string + err := tx.QueryRow(ctx, `SELECT status FROM attachments WHERE id=$1 AND chat_id=$2 FOR SHARE`, attachmentID, chatID).Scan(&status) + if errors.Is(err, pgx.ErrNoRows) { + return model.Message{}, ErrNotFound + } + if err != nil { + return model.Message{}, ErrUnavailable + } + if status != "ready" { + return model.Message{}, ErrAttachmentNotReady + } + } + err = tx.QueryRow(ctx, `INSERT INTO messages(id,chat_id,sender_id,text,created_at) + VALUES($1,$2,$3,$4,GREATEST(clock_timestamp(), + COALESCE((SELECT created_at + INTERVAL '1 microsecond' FROM messages WHERE chat_id=$2 ORDER BY created_at DESC,id DESC LIMIT 1),clock_timestamp()))) + RETURNING id::text,chat_id::text,sender_id::text,text,created_at`, id, chatID, actor, text).Scan(&message.ID, &message.ChatID, &message.SenderID, &message.Text, &message.CreatedAt) + if err != nil { + return model.Message{}, ErrUnavailable + } + message.AttachmentIDs = make([]string, len(attachmentIDs)) + copy(message.AttachmentIDs, attachmentIDs) + for position, attachmentID := range attachmentIDs { + _, err := tx.Exec(ctx, `INSERT INTO message_attachments(message_id,attachment_id,chat_id,position) VALUES($1,$2,$3,$4)`, message.ID, attachmentID, chatID, position) + if err != nil { + return model.Message{}, ErrUnavailable + } + } + _, err = tx.Exec(ctx, `INSERT INTO message_keys(user_id,chat_id,key,message_id,expires_at) + VALUES($1,$2,$3,$4,clock_timestamp()+INTERVAL '24 hours') + ON CONFLICT(user_id,chat_id,key) DO UPDATE + SET message_id=EXCLUDED.message_id,expires_at=EXCLUDED.expires_at`, actor, chatID, key, message.ID) + if err != nil { + return model.Message{}, ErrUnavailable + } + if err := tx.Commit(ctx); err != nil { + return model.Message{}, ErrUnavailable + } + message.CreatedAt = message.CreatedAt.UTC() + return message, nil +} + +func (s *PostgresStore) MessagePage(ctx context.Context, chatID string, limit int, after *Position) ([]model.Message, error) { + ctx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + query := `SELECT ` + messageColumns + ` FROM messages m WHERE m.chat_id=$1` + args := []any{chatID, limit} + if after != nil { + query += ` AND (m.created_at,m.id)>($3,$4::uuid)` + args = append(args, after.CreatedAt, after.ID) + } + query += ` ORDER BY m.created_at,m.id LIMIT $2` + rows, err := s.pool.Query(ctx, query, args...) + if err != nil { + return nil, ErrUnavailable + } + defer rows.Close() + items := make([]model.Message, 0) + for rows.Next() { + var m model.Message + if err := rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt, &m.AttachmentIDs); err != nil { + return nil, ErrUnavailable + } + m.CreatedAt = m.CreatedAt.UTC() + items = append(items, m) + } + if rows.Err() != nil { + return nil, ErrUnavailable + } + return items, nil +} diff --git a/internal/store/postgres_sessions.go b/internal/store/postgres_sessions.go new file mode 100644 index 00000000..4bc4dfc0 --- /dev/null +++ b/internal/store/postgres_sessions.go @@ -0,0 +1,69 @@ +package store + +import ( + "context" + "errors" + "time" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" + "github.com/jackc/pgx/v5" +) + +var ErrSessionNotFound = errors.New("session not found") + +func (s *PostgresStore) Credentials(ctx context.Context, username string) (model.User, string, error) { + ctx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + var user model.User + var hash string + err := s.pool.QueryRow(ctx, `SELECT id::text, username, display_name, created_at, password_hash FROM users WHERE username=$1`, username).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt, &hash) + if errors.Is(err, pgx.ErrNoRows) { + return model.User{}, "", ErrUserNotFound + } + if err != nil { + return model.User{}, "", ErrUnavailable + } + user.CreatedAt = user.CreatedAt.UTC() + return user, hash, nil +} + +func (s *PostgresStore) CreateSession(ctx context.Context, hash []byte, userID string, ttlSeconds int64) (time.Time, error) { + ctx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + var expires time.Time + err := s.pool.QueryRow(ctx, `INSERT INTO sessions (token_hash, user_id, expires_at) + VALUES ($1, $2, CURRENT_TIMESTAMP + $3::bigint * INTERVAL '1 second') RETURNING expires_at`, hash, userID, ttlSeconds).Scan(&expires) + if err != nil { + return time.Time{}, ErrUnavailable + } + return expires.UTC(), nil +} + +func (s *PostgresStore) SessionUser(ctx context.Context, hash []byte) (model.User, error) { + ctx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + var userID string + + err := s.pool.QueryRow(ctx, `SELECT user_id::text FROM sessions + WHERE token_hash=$1 AND expires_at > clock_timestamp()`, hash).Scan(&userID) + if errors.Is(err, pgx.ErrNoRows) { + return model.User{}, ErrSessionNotFound + } + if err != nil { + return model.User{}, ErrUnavailable + } + return s.profile(ctx, userID) +} + +func (s *PostgresStore) DeleteSession(ctx context.Context, hash []byte) error { + ctx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + tag, err := s.pool.Exec(ctx, `DELETE FROM sessions WHERE token_hash=$1 AND expires_at > clock_timestamp()`, hash) + if err != nil { + return ErrUnavailable + } + if tag.RowsAffected() == 0 { + return ErrSessionNotFound + } + return nil +} diff --git a/internal/store/postgres_users.go b/internal/store/postgres_users.go new file mode 100644 index 00000000..c5e0d9fe --- /dev/null +++ b/internal/store/postgres_users.go @@ -0,0 +1,86 @@ +package store + +import ( + "context" + "errors" + "time" + + "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgxpool" +) + +var ErrUnavailable = errors.New("storage unavailable") + +type ProfileCache interface { + Get(context.Context, string) (model.User, bool) + Set(context.Context, model.User) +} + +type PostgresStore struct { + pool *pgxpool.Pool + profiles ProfileCache +} + +func NewPostgresStore(pool *pgxpool.Pool) *PostgresStore { + return &PostgresStore{pool: pool} +} + +func (s *PostgresStore) WithProfiles(profiles ProfileCache) *PostgresStore { + s.profiles = profiles + return s +} + +func (s *PostgresStore) profile(ctx context.Context, id string) (model.User, error) { + if s.profiles != nil { + if user, ok := s.profiles.Get(ctx, id); ok { + return user, nil + } + } + var user model.User + err := s.pool.QueryRow(ctx, `SELECT id::text, username, display_name, created_at FROM users WHERE id=$1`, id).Scan( + &user.ID, &user.Username, &user.DisplayName, &user.CreatedAt, + ) + if err != nil { + return model.User{}, ErrUnavailable + } + user.CreatedAt = user.CreatedAt.UTC() + if s.profiles != nil { + s.profiles.Set(ctx, user) + } + return user, nil +} + +func (s *PostgresStore) CreateUser(ctx context.Context, username, displayName, passwordHash string) (model.User, error) { + id, err := newUUID() + if err != nil { + return model.User{}, errors.New("cannot generate user ID") + } + ctx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + var user model.User + err = s.pool.QueryRow(ctx, ` + INSERT INTO users (id, username, display_name, password_hash) + VALUES ($1, $2, $3, $4) + RETURNING id::text, username, display_name, created_at`, + id, username, displayName, passwordHash, + ).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt) + if err != nil { + var pgErr *pgconn.PgError + if errors.As(err, &pgErr) && pgErr.Code == "23505" && pgErr.ConstraintName == "users_username_key" { + return model.User{}, ErrUsernameTaken + } + return model.User{}, ErrUnavailable + } + user.CreatedAt = user.CreatedAt.UTC() + return user, nil +} + +func (s *PostgresStore) Ping(ctx context.Context) error { + ctx, cancel := context.WithTimeout(ctx, time.Second) + defer cancel() + if err := s.pool.Ping(ctx); err != nil { + return ErrUnavailable + } + return nil +} diff --git a/internal/store/upload_intents.go b/internal/store/upload_intents.go new file mode 100644 index 00000000..fadc14b7 --- /dev/null +++ b/internal/store/upload_intents.go @@ -0,0 +1,95 @@ +package store + +import ( + "context" + "errors" + "time" + + "github.com/jackc/pgx/v5" +) + +func (s *PostgresStore) BeginUpload(ctx context.Context, id, chatID, ownerID string) error { + ctx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + if _, err := s.pool.Exec(ctx, `INSERT INTO upload_intents(id,chat_id,owner_id) VALUES($1,$2,$3)`, id, chatID, ownerID); err != nil { + return ErrUnavailable + } + return nil +} + +// CleanupCandidates is bounded. Each candidate is locked and rechecked later. +func (s *PostgresStore) CleanupCandidates(ctx context.Context) ([]string, error) { + ctx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + rows, err := s.pool.Query(ctx, `SELECT id::text FROM upload_intents + WHERE expires_at<=clock_timestamp() AND next_cleanup_at<=clock_timestamp() + ORDER BY next_cleanup_at,id LIMIT 100`) + if err != nil { + return nil, ErrUnavailable + } + defer rows.Close() + ids := []string{} + for rows.Next() { + var id string + if err := rows.Scan(&id); err != nil { + return nil, ErrUnavailable + } + ids = append(ids, id) + } + if rows.Err() != nil { + return nil, ErrUnavailable + } + return ids, nil +} + +// CleanupUpload serializes with publication using the same intent row lock. +// No deletion is attempted if the DB is unavailable or a row is still fresh. +func (s *PostgresStore) CleanupUpload(ctx context.Context, id string, dryRun bool, remove func(context.Context, string) error) (string, error) { + ctx, cancel := context.WithTimeout(ctx, 35*time.Second) + defer cancel() + tx, err := s.pool.Begin(ctx) + if err != nil { + return "", ErrUnavailable + } + defer tx.Rollback(context.Background()) + var locked string + err = tx.QueryRow(ctx, `SELECT id::text FROM upload_intents + WHERE id=$1 AND expires_at<=clock_timestamp() AND next_cleanup_at<=clock_timestamp() + FOR UPDATE SKIP LOCKED`, id).Scan(&locked) + if errors.Is(err, pgx.ErrNoRows) { + return "skipped", nil + } + if err != nil { + return "", ErrUnavailable + } + var published bool + if err := tx.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM attachments WHERE id=$1)`, id).Scan(&published); err != nil { + return "", ErrUnavailable + } + if published { + if dryRun { + return "protected", nil + } + if _, err := tx.Exec(ctx, `DELETE FROM upload_intents WHERE id=$1`, id); err != nil { + return "", ErrUnavailable + } + if err := tx.Commit(ctx); err != nil { + return "", ErrUnavailable + } + return "protected", nil + } + if dryRun { + return "would_delete", nil + } + if err := remove(ctx, id); err != nil { + return "", err + } + // Retain a tombstone: repeat deletion catches a late PUT after a crashed API. + if _, err := tx.Exec(ctx, `UPDATE upload_intents SET state='abandoned',next_cleanup_at=clock_timestamp()+INTERVAL '10 minutes' WHERE id=$1`, id); err != nil { + return "", ErrUnavailable + } + if err := tx.Commit(ctx); err != nil { + return "", ErrUnavailable + } + return "deleted", nil +} diff --git a/internal/upload/image.go b/internal/upload/image.go new file mode 100644 index 00000000..ed74257b --- /dev/null +++ b/internal/upload/image.go @@ -0,0 +1,156 @@ +package upload + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "fmt" + "image" + _ "image/jpeg" + _ "image/png" + "io" + "mime" + "os" + "strings" + "sync" + "unicode" + "unicode/utf8" +) + +const ( + MaxBytes int64 = 10 * 1024 * 1024 + MaxPixels = 40_000_000 +) + +var ( + ErrFilename = errors.New("invalid attachment filename") + ErrMediaType = errors.New("unsupported or corrupt image") + ErrTooLarge = errors.New("attachment limit exceeded") + ErrInterrupted = errors.New("upload interrupted") +) + +type Validator struct { + tempDir string + slot chan struct{} +} + +func NewValidator(tempDir string) *Validator { + return &Validator{tempDir: tempDir, slot: make(chan struct{}, 1)} +} + +type Prepared struct { + File *os.File + SizeBytes int64 + SHA256 string + ContentType string + Width int + Height int + release func() + closeOnce sync.Once + closeErr error +} + +func (p *Prepared) Close() error { + p.closeOnce.Do(func() { + p.closeErr = errors.Join(p.File.Close(), os.Remove(p.File.Name())) + p.release() + }) + return p.closeErr +} + +func ValidFilename(name string) bool { + if !utf8.ValidString(name) || strings.TrimSpace(name) == "" || utf8.RuneCountInString(name) > 255 { + return false + } + for _, r := range name { + if r == '/' || r == '\\' || unicode.IsControl(r) { + return false + } + } + return true +} + +func (v *Validator) Prepare(ctx context.Context, source io.Reader, contentType, filename string) (_ *Prepared, err error) { + if !ValidFilename(filename) { + return nil, ErrFilename + } + mediaType, _, parseErr := mime.ParseMediaType(contentType) + if parseErr != nil || (mediaType != "image/png" && mediaType != "image/jpeg") { + return nil, ErrMediaType + } + select { + case <-ctx.Done(): + return nil, ctx.Err() + case v.slot <- struct{}{}: + } + defer func() { + if err != nil { + <-v.slot + } + }() + file, err := os.CreateTemp(v.tempDir, "attachment-*") + if err != nil { + return nil, fmt.Errorf("create upload staging: %w", err) + } + defer func() { + if err != nil { + file.Close() + os.Remove(file.Name()) + } + }() + hash := sha256.New() + size, err := io.CopyBuffer(io.MultiWriter(file, hash), io.LimitReader(contextReader{ctx, source}, MaxBytes+1), make([]byte, 32*1024)) + if err != nil { + return nil, fmt.Errorf("stage upload: %w", err) + } + if size > MaxBytes { + return nil, ErrTooLarge + } + if _, err := file.Seek(0, io.SeekStart); err != nil { + return nil, err + } + config, format, err := image.DecodeConfig(file) + if err != nil || "image/"+format != mediaType { + return nil, ErrMediaType + } + if config.Width <= 0 || config.Height <= 0 || config.Width > MaxPixels/config.Height { + return nil, ErrTooLarge + } + if _, err := file.Seek(0, io.SeekStart); err != nil { + return nil, err + } + if _, _, err := image.Decode(contextReader{ctx, file}); err != nil { + if ctx.Err() != nil { + return nil, ctx.Err() + } + return nil, ErrMediaType + } + if err := ctx.Err(); err != nil { + return nil, err + } + if _, err := file.Seek(0, io.SeekStart); err != nil { + return nil, err + } + return &Prepared{ + File: file, SizeBytes: size, SHA256: hex.EncodeToString(hash.Sum(nil)), + ContentType: mediaType, Width: config.Width, Height: config.Height, + release: func() { <-v.slot }, + }, nil +} + +type contextReader struct { + ctx context.Context + src io.Reader +} + +func (r contextReader) Read(p []byte) (int, error) { + if err := r.ctx.Err(); err != nil { + return 0, err + } + n, err := r.src.Read(p) + if err != nil && !errors.Is(err, io.EOF) { + return n, fmt.Errorf("%w: %w", ErrInterrupted, err) + } + return n, err +} diff --git a/internal/upload/image_test.go b/internal/upload/image_test.go new file mode 100644 index 00000000..81c44ed8 --- /dev/null +++ b/internal/upload/image_test.go @@ -0,0 +1,142 @@ +package upload + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/binary" + "encoding/hex" + "errors" + "hash/crc32" + "image" + "image/jpeg" + "image/png" + "io" + "os" + "strings" + "testing" +) + +func fixture(t *testing.T, format string) []byte { + t.Helper() + var data bytes.Buffer + img := image.NewNRGBA(image.Rect(0, 0, 3, 2)) + var err error + if format == "png" { + err = png.Encode(&data, img) + } else { + err = jpeg.Encode(&data, img, nil) + } + if err != nil { + t.Fatal(err) + } + return data.Bytes() +} + +func TestOriginalPreserved(t *testing.T) { + for _, format := range []string{"png", "jpeg"} { + t.Run(format, func(t *testing.T) { + data := fixture(t, format) + p, err := NewValidator(t.TempDir()).Prepare(context.Background(), bytes.NewReader(data), "image/"+format, "фото."+format) + if err != nil { + t.Fatal(err) + } + defer p.Close() + original, err := io.ReadAll(p.File) + if err != nil { + t.Fatal(err) + } + hash := sha256.Sum256(data) + if !bytes.Equal(original, data) || p.SizeBytes != int64(len(data)) || p.SHA256 != hex.EncodeToString(hash[:]) || p.Width != 3 || p.Height != 2 { + t.Fatal("original bytes or metadata changed") + } + }) + } +} + +func TestInvalidFilesAndCleanup(t *testing.T) { + valid := fixture(t, "png") + // A valid IHDR advertises excessive dimensions, with a corrected CRC. + huge := bytes.Clone(valid) + binary.BigEndian.PutUint32(huge[16:20], 40001) + binary.BigEndian.PutUint32(huge[20:24], 1000) + binary.BigEndian.PutUint32(huge[29:33], crc32.ChecksumIEEE(huge[12:29])) + for _, tc := range []struct { + name, mime string + data []byte + want error + }{ + {"empty", "image/png", nil, ErrMediaType}, + {"text", "image/png", []byte("not an image"), ErrMediaType}, + {"spoofed MIME", "image/jpeg", valid, ErrMediaType}, + {"unsupported MIME", "text/plain", valid, ErrMediaType}, + {"truncated after header", "image/png", valid[:33], ErrMediaType}, + {"pixel limit before decode", "image/png", huge, ErrTooLarge}, + } { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + _, err := NewValidator(dir).Prepare(context.Background(), bytes.NewReader(tc.data), tc.mime, "photo.png") + if !errors.Is(err, tc.want) { + t.Fatalf("got %v; want %v", err, tc.want) + } + files, err := os.ReadDir(dir) + if err != nil || len(files) != 0 { + t.Fatalf("staging not cleaned: %v %v", files, err) + } + }) + } +} + +type countingStream struct{ read int64 } + +func (s *countingStream) Read(p []byte) (int, error) { + clear(p) + s.read += int64(len(p)) + return len(p), nil +} + +func TestByteLimitStopsInfiniteStream(t *testing.T) { + stream := &countingStream{} + dir := t.TempDir() + _, err := NewValidator(dir).Prepare(context.Background(), stream, "image/png", "photo.png") + if !errors.Is(err, ErrTooLarge) || stream.read != MaxBytes+1 { + t.Fatalf("got %v after reading %d bytes", err, stream.read) + } + files, _ := os.ReadDir(dir) + if len(files) != 0 { + t.Fatal("oversized staging file remains") + } +} + +func TestFilename(t *testing.T) { + for _, name := range []string{"", " ", "../x.png", `a\b.png`, "x\x00.png", "x\n.png", string([]byte{0xff}), strings.Repeat("я", 256)} { + if ValidFilename(name) { + t.Errorf("accepted filename %q", name) + } + } + if !ValidFilename(strings.Repeat("я", 255)) { + t.Fatal("Unicode length counted as bytes") + } +} + +func TestCancellationAndReadFailure(t *testing.T) { + v := NewValidator(t.TempDir()) + v.slot <- struct{}{} + ctx, cancel := context.WithCancel(context.Background()) + cancel() + if _, err := v.Prepare(ctx, bytes.NewReader(nil), "image/png", "x.png"); !errors.Is(err, context.Canceled) { + t.Fatalf("cancelled admission: %v", err) + } + <-v.slot + if _, err := v.Prepare(context.Background(), brokenReader{}, "image/png", "x.png"); !errors.Is(err, ErrInterrupted) { + t.Fatalf("interrupted upload: %v", err) + } + files, _ := os.ReadDir(v.tempDir) + if len(files) != 0 { + t.Fatal("interrupted staging file remains") + } +} + +type brokenReader struct{} + +func (brokenReader) Read([]byte) (int, error) { return 0, io.ErrUnexpectedEOF } diff --git a/migrations/000001_initial.up.sql b/migrations/000001_initial.up.sql new file mode 100644 index 00000000..f1629c57 --- /dev/null +++ b/migrations/000001_initial.up.sql @@ -0,0 +1,49 @@ +CREATE TABLE users ( + id UUID PRIMARY KEY, + username TEXT NOT NULL UNIQUE, + display_name TEXT NOT NULL, + password_hash TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'), + CONSTRAINT users_display_name_length CHECK (char_length(display_name) BETWEEN 1 AND 100), + CONSTRAINT users_password_hash_not_empty CHECK (password_hash <> '') +); + +CREATE TABLE chats ( + id UUID PRIMARY KEY, + title TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT chats_title_length CHECK (char_length(title) BETWEEN 1 AND 100) +); + +CREATE TABLE chat_members ( + chat_id UUID NOT NULL REFERENCES chats(id), + user_id UUID NOT NULL REFERENCES users(id), + PRIMARY KEY (chat_id, user_id) +); + +CREATE INDEX chat_members_user_chat_idx ON chat_members (user_id, chat_id); + +CREATE TABLE messages ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL, + sender_id UUID NOT NULL, + text TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT messages_sender_membership_fk + FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id), + CONSTRAINT messages_text_length CHECK (char_length(text) BETWEEN 1 AND 4000) +); + +CREATE INDEX messages_chat_history_idx ON messages (chat_id, created_at, id); + +CREATE TABLE sessions ( + token_hash BYTEA PRIMARY KEY, + user_id UUID NOT NULL REFERENCES users(id), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + expires_at TIMESTAMPTZ NOT NULL, + CONSTRAINT sessions_token_hash_length CHECK (octet_length(token_hash) = 32), + CONSTRAINT sessions_expiry_after_creation CHECK (expires_at > created_at) +); + +CREATE INDEX sessions_expiry_idx ON sessions (expires_at); diff --git a/migrations/000002_message_keys.up.sql b/migrations/000002_message_keys.up.sql new file mode 100644 index 00000000..74bca7e4 --- /dev/null +++ b/migrations/000002_message_keys.up.sql @@ -0,0 +1,8 @@ +CREATE TABLE message_keys ( + user_id UUID NOT NULL REFERENCES users(id), + chat_id UUID NOT NULL REFERENCES chats(id), + key TEXT NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'), + message_id UUID NOT NULL REFERENCES messages(id), + expires_at TIMESTAMPTZ NOT NULL, + PRIMARY KEY (user_id, chat_id, key) +); diff --git a/migrations/000003_attachments.up.sql b/migrations/000003_attachments.up.sql new file mode 100644 index 00000000..fe32b1ef --- /dev/null +++ b/migrations/000003_attachments.up.sql @@ -0,0 +1,44 @@ +-- Only completed, ready attachments are published in this table in lab4. +-- Interrupted upload tracking/compensation is implemented separately. +CREATE TABLE attachments ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL REFERENCES chats(id), + owner_id UUID NOT NULL, + object_key TEXT NOT NULL UNIQUE, + filename TEXT NOT NULL, + content_type TEXT NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')), + size_bytes BIGINT NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760), + sha256 TEXT NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'), + status TEXT NOT NULL DEFAULT 'ready' CHECK (status = 'ready'), + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT attachments_owner_membership_fk + FOREIGN KEY (chat_id, owner_id) REFERENCES chat_members(chat_id, user_id), + CONSTRAINT attachments_filename_valid CHECK ( + char_length(filename) BETWEEN 1 AND 255 + AND filename !~ '[[:cntrl:]]' + AND position('/' IN filename) = 0 + AND position(chr(92) IN filename) = 0 + ), + CONSTRAINT attachments_object_key_valid CHECK (object_key = 'attachments/' || id::text), + CONSTRAINT attachments_updated_at_valid CHECK (updated_at >= created_at), + CONSTRAINT attachments_id_chat_unique UNIQUE (id, chat_id) +); + +CREATE INDEX attachments_created_idx ON attachments (created_at, id); + +-- Composite FKs enforce that message and attachment share the same chat. +ALTER TABLE messages ADD CONSTRAINT messages_id_chat_unique UNIQUE (id, chat_id); + +CREATE TABLE message_attachments ( + message_id UUID NOT NULL, + attachment_id UUID NOT NULL, + chat_id UUID NOT NULL, + position SMALLINT NOT NULL CHECK (position BETWEEN 0 AND 9), + PRIMARY KEY (message_id, attachment_id), + UNIQUE (message_id, position), + FOREIGN KEY (message_id, chat_id) REFERENCES messages(id, chat_id), + FOREIGN KEY (attachment_id, chat_id) REFERENCES attachments(id, chat_id) +); + +CREATE INDEX message_attachments_attachment_idx ON message_attachments (attachment_id); diff --git a/migrations/000004_attachment_messages.up.sql b/migrations/000004_attachment_messages.up.sql new file mode 100644 index 00000000..6ddf4cbc --- /dev/null +++ b/migrations/000004_attachment_messages.up.sql @@ -0,0 +1,5 @@ +-- Row CHECK cannot inspect message_attachments. The service enforces +-- nonblank text OR at least one attachment, inserted in the same transaction. +ALTER TABLE messages DROP CONSTRAINT messages_text_length; +ALTER TABLE messages ADD CONSTRAINT messages_text_length + CHECK (char_length(text) BETWEEN 0 AND 4000); diff --git a/migrations/000005_upload_intents.up.sql b/migrations/000005_upload_intents.up.sql new file mode 100644 index 00000000..5a80b0c4 --- /dev/null +++ b/migrations/000005_upload_intents.up.sql @@ -0,0 +1,13 @@ +CREATE TABLE upload_intents ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL, + owner_id UUID NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + expires_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP + INTERVAL '10 minutes', + state TEXT NOT NULL DEFAULT 'pending' CHECK (state IN ('pending', 'abandoned')), + next_cleanup_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP + INTERVAL '10 minutes', + CHECK (expires_at > created_at), + FOREIGN KEY (chat_id, owner_id) REFERENCES chat_members(chat_id, user_id) +); + +CREATE INDEX upload_intents_cleanup_idx ON upload_intents (next_cleanup_at, id); diff --git a/migrations/README.md b/migrations/README.md new file mode 100644 index 00000000..b5ca70c6 --- /dev/null +++ b/migrations/README.md @@ -0,0 +1,21 @@ +# Миграции PostgreSQL lab4 + +`apply.psql` применяет версии 1–5 в одной транзакции под advisory lock (2026, 2). Запись версии и изменение схемы атомарны. Уже применённые версии пропускаются; новые изменения оформляются новой миграцией. + +- 000001: пользователи, чаты, участники, сообщения, сессии. +- 000002: ключи идемпотентности сообщений. +- 000003: метаданные вложений и связи с сообщениями. +- 000004: допускает пустой текст для сообщений с вложениями; правило «непустой текст или вложение» проверяет сервис. +- 000005: durable-журнал намерений загрузки, grace period и abandoned-отметки для повторной очистки. + +Запуск из корня lab4: + +```powershell +docker compose --env-file .env run --rm migrate +``` + +Первое применение на новом volume lab4 и повторный запуск проверены: [вывод](../evidence/attachments-migration.txt). Ограничения вложений проверены транзакционным SQL-сценарием: [вывод](../evidence/attachments-schema.txt). Тестовые строки откатываются. + +UUID и object key назначает приложение. БД хранит только метаданные, байты находятся в S3. Составные внешние ключи message_attachments гарантируют совпадение чата сообщения и вложения. CHECK не заменяет проверку содержимого изображения и авторизацию в Go. + +Между PostgreSQL и S3 нет общей транзакции. Перед PUT сохраняется upload_intent; публикация ready-метаданных и удаление intent атомарны. Просроченные намерения обрабатывает очистка с grace 10 минут; механизм и ограничения описаны в [документе](../docs/attachment-consistency.md). Публикуются только ready-вложения. HTTP-путь подключён. Сообщение, связи с вложениями и ключ идемпотентности записываются в одной транзакции. Межчатовые связи запрещены составными внешними ключами; пустое сообщение без вложений отвергается сервисом. CHECK одной строки не обращается к таблице связей. \ No newline at end of file diff --git a/migrations/apply.psql b/migrations/apply.psql new file mode 100644 index 00000000..8e464e43 --- /dev/null +++ b/migrations/apply.psql @@ -0,0 +1,74 @@ +\set ON_ERROR_STOP on + +BEGIN; +SET LOCAL lock_timeout = '10s'; +SET LOCAL statement_timeout = '30s'; + +-- All migration runners for this application must use this same lock key. +SELECT pg_advisory_xact_lock(2026, 2); + +CREATE TABLE IF NOT EXISTS schema_migrations ( + version BIGINT PRIMARY KEY, + applied_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +SELECT EXISTS ( + SELECT 1 FROM schema_migrations WHERE version = 1 +) AS initial_applied \gset + +\if :initial_applied + \echo 'Migration 000001 already applied; skipping.' +\else + \ir 000001_initial.up.sql + INSERT INTO schema_migrations (version) VALUES (1); + \echo 'Migration 000001 applied (commit follows).' +\endif + +SELECT EXISTS ( + SELECT 1 FROM schema_migrations WHERE version = 2 +) AS message_keys_applied \gset + +\if :message_keys_applied + \echo 'Migration 000002 already applied; skipping.' +\else + \ir 000002_message_keys.up.sql + INSERT INTO schema_migrations (version) VALUES (2); +\endif + +SELECT EXISTS ( + SELECT 1 FROM schema_migrations WHERE version = 3 +) AS attachments_applied \gset + +\if :attachments_applied + \echo 'Migration 000003 already applied; skipping.' +\else + \ir 000003_attachments.up.sql + INSERT INTO schema_migrations (version) VALUES (3); + \echo 'Migration 000003 applied (commit follows).' +\endif + +SELECT EXISTS ( + SELECT 1 FROM schema_migrations WHERE version = 4 +) AS attachment_messages_applied \gset + +\if :attachment_messages_applied + \echo 'Migration 000004 already applied; skipping.' +\else + \ir 000004_attachment_messages.up.sql + INSERT INTO schema_migrations (version) VALUES (4); + \echo 'Migration 000004 applied (commit follows).' +\endif + +SELECT EXISTS ( + SELECT 1 FROM schema_migrations WHERE version = 5 +) AS upload_intents_applied \gset + +\if :upload_intents_applied + \echo 'Migration 000005 already applied; skipping.' +\else + \ir 000005_upload_intents.up.sql + INSERT INTO schema_migrations (version) VALUES (5); + \echo 'Migration 000005 applied (commit follows).' +\endif + +COMMIT; diff --git a/scripts/check-s3.ps1 b/scripts/check-s3.ps1 new file mode 100644 index 00000000..6125b13b --- /dev/null +++ b/scripts/check-s3.ps1 @@ -0,0 +1,31 @@ +$ErrorActionPreference = 'Stop' +Push-Location (Split-Path -Parent $PSScriptRoot) +try { + function Invoke-Compose { + Write-Output ('COMMAND: docker compose --env-file .env ' + ($args -join ' ')) + & docker compose --env-file .env @args + if ($LASTEXITCODE -ne 0) { throw "Compose failed with exit code $LASTEXITCODE" } + } + ./scripts/init-local.ps1 + Invoke-Compose build s3 s3-bootstrap + Invoke-Compose up -d s3 + Invoke-Compose run --rm s3-bootstrap + Invoke-Compose run --rm --entrypoint /bin/sh s3-bootstrap /bootstrap/verify.sh seed + # Repeat setup after a real object exists, then restart its storage process. + Invoke-Compose run --rm s3-bootstrap + Invoke-Compose restart s3 + Invoke-Compose run --rm s3-bootstrap + Invoke-Compose run --rm --entrypoint /bin/sh s3-bootstrap /bootstrap/verify.sh check + $container = & docker compose ps -q s3 + if ($LASTEXITCODE -ne 0 -or -not $container) { throw 'S3 container not found' } + $portsJson = & docker inspect --format '{{json .NetworkSettings.Ports}}' $container + if ($LASTEXITCODE -ne 0) { throw 'Cannot inspect published ports' } + $ports = $portsJson | ConvertFrom-Json + foreach ($property in $ports.PSObject.Properties) { + if ($null -ne $property.Value) { throw 'Unexpected published S3 port' } + } + Write-Output 'PASS: S3 has no published host ports.' + Write-Output 'PASS: S3 infrastructure checks completed.' +} finally { + Pop-Location +} diff --git a/scripts/compose_actions.py b/scripts/compose_actions.py new file mode 100644 index 00000000..428c1fcd --- /dev/null +++ b/scripts/compose_actions.py @@ -0,0 +1,38 @@ +"""Explicit local Compose hooks. No credentials or session tokens are printed.""" +import os +from pathlib import Path +import subprocess +import sys + +ROOT = Path(__file__).resolve().parent.parent + + +def docker(*args, capture=False): + return subprocess.run(["docker", *args], cwd=ROOT, check=True, + text=True, capture_output=capture) + + +def main(): + action = sys.argv[1] + if action == "restart": + # Stop all running application components before restarting shared stores. + docker("compose", "--env-file", ".env", "stop", "lb", "api", "redis", "postgres", "s3") + docker("compose", "--env-file", ".env", "up", "-d", "--wait", "--wait-timeout", "60") + elif action == "stop-one": + target = os.environ["TARGET_INSTANCE_ID"] + containers = docker("compose", "ps", "-q", "api", capture=True).stdout.split() + matches = [] + for container in containers: + alias = docker("exec", container, "/server", "instance-id", capture=True).stdout.strip() + if alias == target: + matches.append(container) + if len(matches) != 1: + raise RuntimeError("Expected exactly one API container for target alias") + # One second grace keeps this explicit failure hook within the test budget. + docker("stop", "--time", "1", matches[0]) + else: + raise ValueError("Unknown action") + + +if __name__ == "__main__": + main() diff --git a/scripts/init-local.ps1 b/scripts/init-local.ps1 new file mode 100644 index 00000000..78d6744b --- /dev/null +++ b/scripts/init-local.ps1 @@ -0,0 +1,36 @@ +$ErrorActionPreference = 'Stop' + +$repoRoot = Split-Path -Parent $PSScriptRoot +$envPath = Join-Path $repoRoot '.env' +if (-not (Test-Path -LiteralPath $envPath)) { + Copy-Item -LiteralPath (Join-Path $repoRoot '.env.example') -Destination $envPath + Write-Output 'Local .env created (non-secret session TTL).' +} else { + Write-Output 'Existing .env preserved.' +} + +$secretsDirectory = Join-Path $repoRoot 'secrets' +New-Item -ItemType Directory -Path $secretsDirectory -Force | Out-Null + +function Ensure-LocalSecret([string] $name, [string] $prefix = '', [int] $byteCount = 32) { + $secretPath = Join-Path $secretsDirectory "$name.txt" + if (Test-Path -LiteralPath $secretPath) { + if ([string]::IsNullOrWhiteSpace([IO.File]::ReadAllText($secretPath))) { + throw "Existing secret file is empty: $name" + } + Write-Output "Existing secret preserved: $name" + return + } + $randomBytes = New-Object byte[] $byteCount + $generator = [System.Security.Cryptography.RandomNumberGenerator]::Create() + try { $generator.GetBytes($randomBytes) } finally { $generator.Dispose() } + $secretValue = $prefix + ([BitConverter]::ToString($randomBytes).Replace('-', '').ToLowerInvariant()) + [IO.File]::WriteAllText($secretPath, $secretValue, [Text.Encoding]::ASCII) + Write-Output "Local secret created: $name (value not printed)." +} + +Ensure-LocalSecret 'postgres_password' +Ensure-LocalSecret 's3_root_user' 'root' 8 +Ensure-LocalSecret 's3_root_password' +Ensure-LocalSecret 's3_access_key_id' 'api-' 8 +Ensure-LocalSecret 's3_secret_access_key' diff --git a/scripts/restart.cmd b/scripts/restart.cmd new file mode 100644 index 00000000..05bd011d --- /dev/null +++ b/scripts/restart.cmd @@ -0,0 +1,6 @@ +@echo off +if defined LAB_PYTHON ( + "%LAB_PYTHON%" -X utf8 "%~dp0compose_actions.py" restart +) else ( + py -X utf8 "%~dp0compose_actions.py" restart +) diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100644 index 00000000..5718796b --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +exec python3 scripts/compose_actions.py restart diff --git a/scripts/stop-one.cmd b/scripts/stop-one.cmd new file mode 100644 index 00000000..0a2d6a32 --- /dev/null +++ b/scripts/stop-one.cmd @@ -0,0 +1,6 @@ +@echo off +if defined LAB_PYTHON ( + "%LAB_PYTHON%" -X utf8 "%~dp0compose_actions.py" stop-one +) else ( + py -X utf8 "%~dp0compose_actions.py" stop-one +) diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100644 index 00000000..5d9e98cf --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +exec python3 scripts/compose_actions.py stop-one diff --git a/student-tests/attachment_messages.py b/student-tests/attachment_messages.py new file mode 100644 index 00000000..b1b35d5e --- /dev/null +++ b/student-tests/attachment_messages.py @@ -0,0 +1,83 @@ +"""Message attachment ACL, validation and idempotency. Synthetic data only.""" +from concurrent.futures import ThreadPoolExecutor +from pathlib import Path +import sys +import uuid + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client, arguments, check, run + + +def main(): + args = arguments('Messages with attachments') + base = args.base_url or 'http://127.0.0.1:8084' + c = Client(base, args.ca_file) + alice, bob, eve, chat = c.bootstrap() + first, _ = c.attachment(alice, chat) + second, _ = c.attachment(alice, chat) + path = '/api/v1/chats/' + chat['id'] + '/messages' + ids = [second['id'], first['id']] + key = uuid.uuid4().hex + created = c.message(bob, chat, '', key=key, attachments=ids)['json'] + check(created['attachment_ids'] == ids and created['text'] == '', 'attachment-only message/order lost') + replay = c.call('POST', path, 201, actor=bob, + body={'attachment_ids': ids, 'text': ''}, headers={'Idempotency-Key': key}, schema='Message')['json'] + check(replay == created, 'JSON field order changed replay') + c.call('POST', path, 409, actor=bob, body={'text': '', 'attachment_ids': list(reversed(ids))}, headers={'Idempotency-Key': key}) + c.call('POST', path, 409, actor=bob, body={'text': '', 'attachment_ids': ids[:1]}, headers={'Idempotency-Key': key}) + print('PASS: attachment-only message, non-owner participant, list order, replay and changed-list 409') + text_key = uuid.uuid4().hex + text = c.message(alice, chat, 'text-only', key=text_key)['json'] + check(text['attachment_ids'] == [], 'text-only attachment_ids must be []') + check(c.message(alice, chat, 'text-only', key=text_key, attachments=[])['json'] == text, + 'omitted attachment_ids differs from []') + print('PASS: text-only compatibility; omitted attachment_ids and [] replay identically') + invalid_bodies = [ + {'text': ''}, {'text': ' \t\n', 'attachment_ids': []}, + {'text': '', 'attachment_ids': [first['id'], first['id']]}, + {'text': '', 'attachment_ids': [str(uuid.uuid4()) for _ in range(11)]}, + {'text': '', 'attachment_ids': ['not-uuid']}, + {'text': 'x', 'attachment_ids': None}, {'text': 'x', 'attachment_ids': 'wrong'}, + {'text': 'x', 'attachment_ids': [None]}, {'text': 'x', 'unexpected': True}, + ] + for body in invalid_bodies: + c.call('POST', path, 400, actor=alice, body=body, headers={'Idempotency-Key': uuid.uuid4().hex}) + print('PASS: empty message, duplicates, >10 IDs, bad UUID/types/null/unknown fields return 400') + other = c.call('POST', '/api/v1/chats', 201, actor=alice, + body={'title': 'Other chat', 'member_ids': [alice['id'], eve['id']]}, schema='Chat')['json'] + foreign, _ = c.attachment(alice, other) + retry_key = uuid.uuid4().hex + for attachment_id in (foreign['id'], str(uuid.uuid4())): + c.call('POST', path, 404, actor=alice, body={'text': '', 'attachment_ids': [attachment_id]}, + headers={'Idempotency-Key': retry_key}) + c.message(alice, chat, 'corrected retry', key=retry_key, attachments=[first['id']]) + c.call('POST', path, 404, actor=eve, body={'text': '', 'attachment_ids': [first['id']]}, + headers={'Idempotency-Key': uuid.uuid4().hex}) + print('PASS: foreign-chat/unknown attachment and outsider return 404; rejected request does not consume key') + ten_ids = ids + [c.attachment(alice, chat)[0]['id'] for _ in range(8)] + ten = c.message(alice, chat, '', attachments=ten_ids)['json'] + check(ten['attachment_ids'] == ten_ids, 'ten attachments were not preserved') + print('PASS: exactly ten distinct attachments are accepted') + concurrent_key = uuid.uuid4().hex + + def send(_): + local = Client(base, args.ca_file) + return local.message(alice, chat, 'concurrent', key=concurrent_key, attachments=[first['id']]) + + with ThreadPoolExecutor(max_workers=8) as executor: + results = list(executor.map(send, range(16))) + check(all(result['json'] == results[0]['json'] for result in results), 'concurrent replies differ') + instances = {result['headers']['X-Instance-Id'] for result in results} + check(len(instances) >= 2, 'concurrent replay did not reach two replicas') + history = c.call('GET', path, actor=bob, schema='MessagePage')['json']['items'] + check(len(history) == 5, 'rejected requests or retries left extra messages') + matching = [message for message in history if message['id'] == results[0]['json']['id']] + check(len(matching) == 1, 'concurrent retry created duplicates in history') + check(next(message for message in history if message['id'] == created['id']) == created, 'history lost attachments') + print('PASS: 16 concurrent retries across two replicas return one message, history preserves attachment order') + print('PASS: message attachment checks completed') + + +if __name__ == '__main__': + run(main) diff --git a/student-tests/attachment_resilience.py b/student-tests/attachment_resilience.py new file mode 100644 index 00000000..b94f7ae4 --- /dev/null +++ b/student-tests/attachment_resilience.py @@ -0,0 +1,216 @@ +"""Explicit local failures; preserves volumes and restores API/S3 in finally.""" +import datetime as dt +import hashlib +import json +import os +from pathlib import Path +import subprocess +import sys +import time +import urllib.error +import urllib.request + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client, Failure, arguments, check, run + + +def docker(*args, check_code=True): + result = subprocess.run(['docker', *args], cwd=ROOT, text=True, capture_output=True, timeout=120) + if check_code: + check(result.returncode == 0, 'Docker operation failed: ' + args[0]) + return result + + +def compose(*args, **kwargs): + return docker('compose', '--env-file', '.env', *args, **kwargs) + + +def snapshot(label): + ids = compose('ps', '-a', '-q').stdout.split() + template = ('{"id":{{json .Id}},"name":{{json .Name}},' + '"service":{{json (index .Config.Labels "com.docker.compose.service")}},' + '"status":{{json .State.Status}},"started_at":{{json .State.StartedAt}},' + '"pid":{{json .State.Pid}},"image_id":{{json .Image}},"mounts":{{json .Mounts}}}') + records = [] + for container in ids: + item = json.loads(docker('inspect', '--format', template, container).stdout) + item['volumes'] = [{'name': m['Name'], 'destination': m['Destination']} + for m in item.pop('mounts') if m['Type'] == 'volume'] + records.append(item) + record = {'stage': label, 'utc': dt.datetime.now(dt.timezone.utc).isoformat(), 'containers': records} + with (ROOT / 'evidence' / 'attachment-resilience-containers.jsonl').open('a', encoding='utf-8') as out: + out.write(json.dumps(record, ensure_ascii=False) + '\n') + return records + + +def wait_ready(c, seconds=60): + deadline = time.monotonic() + seconds + while True: + try: + c.call('GET', '/health/ready', schema='Health') + return + except Failure: + check(time.monotonic() < deadline, 'readiness did not recover') + time.sleep(0.3) + + +def restore(c): + # Compose start also visits depends_on (bootstrap/migrate). Restore only + # the stopped processes, without reapplying credentials during the test. + for container in compose('ps', '-a', '-q', 's3', 'api').stdout.split(): + if docker('inspect', '--format', '{{.State.Running}}', container).stdout.strip() != 'true': + docker('start', container) + deadline = time.monotonic() + 60 + observed, consecutive = set(), 0 + while True: + try: + response = c.call('GET', '/health/ready', schema='Health') + observed.add(response['headers']['X-Instance-Id']) + consecutive += 1 + if len(observed) == 2 and consecutive >= 8: + return + except Failure: + observed, consecutive = set(), 0 + check(time.monotonic() < deadline, 'two API replicas did not return to stable LB rotation') + time.sleep(0.2) + + +def download(c, actor, attachment, original): + response = c.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content', actor=actor) + check(response['bytes'] == original, 'original bytes changed') + check(hashlib.sha256(response['bytes']).hexdigest() == attachment['sha256'], 'checksum changed') + return response + + +def public_test(c, mode): + env = dict(os.environ, LAB_PYTHON=sys.executable) + hook = 'scripts/restart.cmd' if mode == 'persistence' else 'scripts/stop-one.cmd' + before = snapshot('before-public-' + mode) + result = subprocess.run([sys.executable, '-X', 'utf8', 'tests/resilience.py', '--mode', mode, + '--action-script', hook, '--base-url', c.base], + cwd=ROOT, env=env, text=True, capture_output=True, timeout=200) + (ROOT / 'evidence' / ('lab4-' + mode + '.txt')).write_text(result.stdout + result.stderr, encoding='utf-8') + after = snapshot('after-public-' + mode) + check(result.returncode == 0, 'public ' + mode + ' failed; inspect its evidence file') + if mode == 'persistence': + previous = {item['name']: item for item in before} + for item in after: + if item['service'] in ('api', 'lb', 'postgres', 'redis', 's3'): + check(item['started_at'] != previous[item['name']]['started_at'], 'component was not restarted: ' + item['service']) + check(item['volumes'] == previous[item['name']]['volumes'], 'persistent volume changed') + print('PASS: public persistence; API/LB/PostgreSQL/Redis/S3 restarted, named volumes unchanged') + else: + check(sum(item['service'] == 'api' and item['status'] == 'exited' for item in after) == 1, + 'failover hook did not leave exactly one API stopped') + print('PASS: public failover; one API stopped, in-flight messages/session/original preserved') + restore(c) + + +def uploader_failovers(c): + alice, bob, _, chat = c.bootstrap() + data = (ROOT / 'tests/fixtures/sample.png').read_bytes() + containers = compose('ps', '-q', 'api').stdout.split() + check(len(containers) == 2, 'expected exactly two running API replicas') + aliases = {container: docker('exec', container, '/server', 'instance-id').stdout.strip() for container in containers} + for index, target in enumerate(containers, 1): + expected = aliases[target] + uploaded = None + for _ in range(40): + response = c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=failover.png', + 201, actor=alice, raw=data, headers={'Content-Type': 'image/png'}, schema='Attachment') + if response['headers']['X-Instance-Id'] == expected: + uploaded = response['json'] + break + check(uploaded is not None, 'target API did not receive upload') + c.message(alice, chat, '', attachments=[uploaded['id']]) + snapshot('before-uploader-stop-' + str(index)) + docker('stop', '--time', '1', target) + snapshot('after-uploader-stop-' + str(index)) + started = time.monotonic() + failures = 0 + while True: + try: + response = download(c, bob, uploaded, data) + check(response['headers']['X-Instance-Id'] != expected, 'stopped uploader still served download') + break + except Failure: + failures += 1 + check(time.monotonic() - started < 10, 'download did not recover within 10 seconds') + time.sleep(0.2) + print(json.dumps({'scenario': 'uploader-failover', 'replica': index, + 'recovery_seconds_after_stop': round(time.monotonic()-started, 3), + 'transient_failures': failures, 'checksum_match': True})) + restore(c) + # Ensure the restored process is ready before selecting it as the next target. + deadline = time.monotonic() + 30 + while True: + result = docker('exec', target, '/server', 'healthcheck', check_code=False) + if result.returncode == 0: + break + check(time.monotonic() < deadline, 'restored API did not become ready') + time.sleep(0.2) + aliases[target] = docker('exec', target, '/server', 'instance-id').stdout.strip() + print('PASS: upload -> stop exact uploader -> other API downloads identical original, for both replicas') + + +def s3_outage(c): + alice, bob, _, chat = c.bootstrap() + attachment, data = c.attachment(alice, chat) + original_message = c.message(alice, chat, 'survive S3 outage', attachments=[attachment['id']])['json'] + before = snapshot('before-s3-outage') + compose('stop', '--timeout', '1', 's3') + snapshot('during-s3-outage') + try: + api_containers = compose('ps', '-q', 'api').stdout.split() + for container in api_containers: + probe = docker('exec', container, '/server', 'healthcheck', check_code=False) + check(probe.returncode != 0 and 'status=503' in probe.stderr, 'API readiness did not return 503') + live = compose('run', '--rm', '--no-deps', '--entrypoint', '/bin/sh', 's3-bootstrap', + '-c', 'wget -qO- http://api:8080/health/live').stdout + check(json.loads(live)['status'] == 'ok', 'API liveness failed during S3 outage') + # LB may withdraw all API after readiness failure and generate its own 503. + req = urllib.request.Request(c.base + '/api/v1/attachments/' + attachment['id'] + '/content', + headers={'Authorization': 'Bearer ' + bob['token']}) + try: + response = c.opener.open(req, timeout=10) + except urllib.error.HTTPError as error: + response = error + with response: + check(response.code == 503, 'download did not fail with 503 during S3 outage') + print('PASS: real S3 stop; both API ready=503, live=200, public download=503 (API or LB)') + finally: + compose('start', 's3') + wait_ready(c) + download(c, bob, attachment, data) + page = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages', actor=bob, schema='MessagePage')['json'] + check(original_message in page['items'], 'message lost during S3 outage') + c.attachment(alice, chat) + after = snapshot('after-s3-outage') + previous = {item['name']: item for item in before} + for item in after: + if item['service'] == 'api': + check(item['started_at'] == previous[item['name']]['started_at'], 'API restarted during S3 outage') + print('PASS: S3 restored; original/message/session preserved, new upload works, API processes did not restart') + + +def main(): + args = arguments('Explicit attachment failover, restart and S3 outage') + c = Client(args.base_url or 'http://127.0.0.1:8084', args.ca_file, timeout=3) + evidence = ROOT / 'evidence/attachment-resilience-containers.jsonl' + evidence.write_text('', encoding='utf-8') + try: + restore(c) + snapshot('initial') + public_test(c, 'failover') + uploader_failovers(c) + public_test(c, 'persistence') + s3_outage(c) + print('PASS: attachment resilience checks completed') + finally: + restore(c) + snapshot('final-restored') + + +if __name__ == '__main__': + run(main) diff --git a/student-tests/attachments-schema.sql b/student-tests/attachments-schema.sql new file mode 100644 index 00000000..5fe4a98d --- /dev/null +++ b/student-tests/attachments-schema.sql @@ -0,0 +1,62 @@ +\set ON_ERROR_STOP on +BEGIN; +-- All synthetic rows are rolled back, including on test failure. +INSERT INTO users(id, username, display_name, password_hash) +VALUES ('10000000-0000-4000-8000-000000000001', 'schema_attachment_test', 'Test', 'not-a-login-fixture'); +INSERT INTO chats(id, title) VALUES +('20000000-0000-4000-8000-000000000001', 'First'), +('20000000-0000-4000-8000-000000000002', 'Second'); +INSERT INTO chat_members(chat_id, user_id) +SELECT id, '10000000-0000-4000-8000-000000000001'::uuid FROM chats +WHERE id IN ('20000000-0000-4000-8000-000000000001', '20000000-0000-4000-8000-000000000002'); +INSERT INTO messages(id, chat_id, sender_id, text) VALUES +('30000000-0000-4000-8000-000000000001', '20000000-0000-4000-8000-000000000001', + '10000000-0000-4000-8000-000000000001', 'schema test'); +INSERT INTO attachments(id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256) +SELECT id, chat_id, '10000000-0000-4000-8000-000000000001'::uuid, + 'attachments/' || id::text, 'photo.png', 'image/png', 10, repeat('a', 64) +FROM (VALUES +('40000000-0000-4000-8000-000000000001'::uuid, '20000000-0000-4000-8000-000000000001'::uuid), +('40000000-0000-4000-8000-000000000002'::uuid, '20000000-0000-4000-8000-000000000002'::uuid) +) AS fixture(id, chat_id); +INSERT INTO message_attachments VALUES +('30000000-0000-4000-8000-000000000001', '40000000-0000-4000-8000-000000000001', + '20000000-0000-4000-8000-000000000001', 0); + +DO $$ +BEGIN + BEGIN + INSERT INTO message_attachments VALUES + ('30000000-0000-4000-8000-000000000001', '40000000-0000-4000-8000-000000000002', + '20000000-0000-4000-8000-000000000001', 1); + RAISE EXCEPTION 'FAIL: cross-chat attachment accepted'; + EXCEPTION WHEN foreign_key_violation THEN + RAISE NOTICE 'PASS: cross-chat attachment rejected'; + END; + BEGIN + UPDATE message_attachments SET position = 10; + RAISE EXCEPTION 'FAIL: attachment position 10 accepted'; + EXCEPTION WHEN check_violation THEN + RAISE NOTICE 'PASS: attachment position limited to 0..9'; + END; + BEGIN + UPDATE attachments SET size_bytes = 10485761; + RAISE EXCEPTION 'FAIL: oversized metadata accepted'; + EXCEPTION WHEN check_violation THEN + RAISE NOTICE 'PASS: oversized metadata rejected'; + END; + BEGIN + UPDATE attachments SET filename = '../photo.png'; + RAISE EXCEPTION 'FAIL: path filename accepted'; + EXCEPTION WHEN check_violation THEN + RAISE NOTICE 'PASS: filename path rejected'; + END; + BEGIN + UPDATE attachments SET sha256 = 'wrong'; + RAISE EXCEPTION 'FAIL: invalid checksum accepted'; + EXCEPTION WHEN check_violation THEN + RAISE NOTICE 'PASS: invalid checksum rejected'; + END; +END $$; +ROLLBACK; +\echo 'PASS: attachment schema checks completed; fixtures rolled back.' diff --git a/student-tests/attachments_api.py b/student-tests/attachments_api.py new file mode 100644 index 00000000..3037a60d --- /dev/null +++ b/student-tests/attachments_api.py @@ -0,0 +1,63 @@ +"""Attachment endpoints only; synthetic users and one PNG, no tokens in output.""" +import hashlib +from pathlib import Path +import struct +import sys +from urllib.parse import quote +import uuid +import zlib + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client, arguments, check, run + + +def main(): + args = arguments('Attachment upload, metadata, content and validation') + c = Client(args.base_url or 'http://127.0.0.1:8084', args.ca_file) + alice, bob, eve, chat = c.bootstrap() + attachment, content = c.attachment(alice, chat) + path = '/api/v1/attachments/' + attachment['id'] + check(attachment['variants'] == [] and attachment['error_code'] is None, 'invalid ready metadata') + fetched = c.call('GET', path, actor=bob, schema='Attachment')['json'] + check(fetched == attachment, 'metadata changed between replicas') + download = c.call('GET', path + '/content', actor=bob) + check(download['bytes'] == content, 'original changed') + check(download['headers'].get_content_type() == 'image/png', 'wrong MIME') + check(hashlib.sha256(download['bytes']).hexdigest() == attachment['sha256'], 'checksum mismatch') + print('PASS: upload 201 ready; participant metadata/download; original and checksum match') + for suffix in ('', '/content'): + c.call('GET', path + suffix, 404, actor=eve) + c.call('GET', path + suffix, 401) + c.call('GET', '/api/v1/attachments/' + str(uuid.uuid4()), 404, actor=alice) + c.call('GET', path + '/content?variant=thumbnail', 404, actor=alice) + print('PASS: unauthorized 401, outsider/unknown attachment 404, missing variant 404') + upload = '/api/v1/chats/' + chat['id'] + '/attachments?filename=photo.png' + c.call('POST', upload, 404, actor=eve, raw=content, headers={'Content-Type': 'image/png'}) + c.call('POST', upload, 401, raw=content, headers={'Content-Type': 'image/png'}) + for label, raw, mime, expected in ( + ('corrupt', b'not an image', 'image/png', 415), + ('truncated', content[:33], 'image/png', 415), + ('spoofed MIME', content, 'image/jpeg', 415), + ('unsupported MIME', content, 'text/plain', 415), + ): + c.call('POST', upload, expected, actor=alice, raw=raw, headers={'Content-Type': mime}) + print('PASS:', label, 'returns', expected) + huge = bytearray(content) + huge[16:24] = struct.pack('>II', 40001, 1000) + huge[29:33] = struct.pack('>I', zlib.crc32(huge[12:29])) + c.call('POST', upload, 413, actor=alice, raw=bytes(huge), headers={'Content-Type': 'image/png'}) + print('PASS: excessive pixel dimensions return 413') + for name in ('', '../photo.png', 'a\\photo.png', 'a\x00.png', 'a\n.png', 'я' * 256): + c.call('POST', upload.split('?')[0] + '?filename=' + quote(name, safe=''), 400, + actor=alice, raw=content, headers={'Content-Type': 'image/png'}) + print('PASS: invalid filenames return 400') + # Declared length is rejected before attempting an unbounded body read. + c.call('POST', upload, 413, actor=alice, raw=b'', + headers={'Content-Type': 'image/png', 'Content-Length': str(10485761)}) + print('PASS: declared byte length above 10 MiB returns 413') + print('PASS: attachment HTTP checks completed; message attachment_ids not tested yet') + + +if __name__ == '__main__': + run(main) diff --git a/student-tests/compose.s3-client.yaml b/student-tests/compose.s3-client.yaml new file mode 100644 index 00000000..917adc24 --- /dev/null +++ b/student-tests/compose.s3-client.yaml @@ -0,0 +1,19 @@ +services: + s3-client-test: + profiles: [checks] + build: + context: . + target: s3-client-test + environment: + S3_INTEGRATION: "1" + S3_ENDPOINT: http://s3:9000 + S3_REGION: ${S3_REGION:-us-east-1} + S3_BUCKET: ${S3_BUCKET:-messenger-attachments} + S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id + S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key + secrets: [s3_access_key_id, s3_secret_access_key] + cpus: 1.0 + mem_limit: 512m + depends_on: + s3-bootstrap: + condition: service_completed_successfully diff --git a/student-tests/contract_checks.py b/student-tests/contract_checks.py new file mode 100644 index 00000000..4fbadb25 --- /dev/null +++ b/student-tests/contract_checks.py @@ -0,0 +1,95 @@ +"""Additional Lab3 contract checks against the running stack. + +Only synthetic data are used. --database-checks additionally expires one key +created by this test using local Compose psql, to avoid waiting 24 hours. +""" +import argparse +from concurrent.futures import ThreadPoolExecutor +import json +from pathlib import Path +import subprocess +import sys +import uuid + +ROOT = Path(__file__).resolve().parent.parent +sys.path.insert(0, str(ROOT / "tests")) +from client import Client, check + + +def sql(statement): + return subprocess.run([ + "docker", "compose", "exec", "-T", "postgres", "psql", "-X", "-A", "-t", + "-v", "ON_ERROR_STOP=1", "-U", "messenger", "-d", "messenger", "-c", statement, + ], cwd=ROOT, check=True, capture_output=True, text=True).stdout.strip() + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--base-url", default="http://127.0.0.1:8084") + parser.add_argument("--database-checks", action="store_true") + args = parser.parse_args() + c = Client(args.base_url) + alice, bob, eve, chat = c.bootstrap() + path = "/api/v1/chats/" + chat["id"] + "/messages" + key = uuid.uuid4().hex + text = "Привет, мир" + first = c.message(alice, chat, text, key=key)["json"] + raw = (" " + json.dumps({"text": text}, ensure_ascii=True, indent=2) + "\n").encode() + replay = c.call("POST", path, 201, actor=alice, raw=raw, + headers={"Content-Type": "application/json", "Idempotency-Key": key}, + schema="Message")["json"] + check(first == replay, "Equivalent JSON changed replay result") + for invalid in ["", "a b", "\xe9", "a" * 129]: + c.call("POST", path, 400, actor=alice, body={"text": text}, + headers={"Idempotency-Key": invalid}) + c.message(alice, chat, "Maximum length key", key="a" * 128) + print("PASS: decoded JSON equality; invalid keys -> 400; 128-character key accepted") + + by_bob = c.message(bob, chat, text, key=key)["json"] + check(by_bob["id"] != first["id"], "Different users share an idempotency result") + other_chat = c.call("POST", "/api/v1/chats", 201, actor=alice, + body={"title": "Scope test", "member_ids": [alice["id"]]}, + schema="Chat")["json"] + by_chat = c.message(alice, other_chat, text, key=key)["json"] + check(by_chat["id"] != first["id"], "Different chats share an idempotency result") + c.call("POST", path, 404, actor=eve, body={"text": text}, headers={"Idempotency-Key": key}) + print("PASS: key scope is per user/chat; unauthorized chat replay -> 404") + + race_key = uuid.uuid4().hex + def conflicting(index): + local = Client(args.base_url) + return local.call("POST", path, [201, 409], actor=alice, + body={"text": "race-" + str(index % 2)}, + headers={"Idempotency-Key": race_key}, schema=None) + with ThreadPoolExecutor(max_workers=8) as pool: + results = list(pool.map(conflicting, range(16))) + check(sum(item["status"] == 201 for item in results) == 8, "Wrong number of winning replays") + check(sum(item["status"] == 409 for item in results) == 8, "Wrong number of conflicts") + check(len({item["headers"]["X-Instance-Id"] for item in results}) >= 2, + "Conflicting race did not span replicas") + history = c.call("GET", path + "?limit=100", actor=alice, schema="MessagePage")["json"]["items"] + check(sum(item["text"].startswith("race-") for item in history) == 1, "Conflicting race created duplicates") + print("PASS: concurrent different bodies -> eight 201 and eight 409; one new message") + + if args.database_checks: + # UUID conversion prevents server-returned IDs from becoming SQL syntax. + actor_id = str(uuid.UUID(alice["id"])) + chat_id = str(uuid.UUID(chat["id"])) + where = f"user_id='{actor_id}' AND chat_id='{chat_id}' AND key='{key}'" + check(sql("SELECT count(*) FROM message_keys WHERE " + where + + " AND expires_at>clock_timestamp()+INTERVAL '23 hours'") == "1", "Key TTL too short") + sql("UPDATE message_keys SET expires_at=clock_timestamp()-INTERVAL '1 second' WHERE " + where) + after_expiry = c.message(alice, chat, "New operation after expiry", key=key)["json"] + check(after_expiry["id"] != first["id"], "Expired key was not reusable") + check(c.message(alice, chat, "New operation after expiry", key=key)["json"] == after_expiry, + "New key cycle is not idempotent") + check(sql("SELECT count(*) FROM message_keys WHERE " + where) == "1", "Key replacement duplicated row") + print("PASS: TTL persisted; simulated expiry permits a new idempotent operation") + + c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=alice) + c.call("POST", path, 401, actor=alice, body={"text": text}, headers={"Idempotency-Key": key}) + print("PASS: revoked session cannot replay a previous operation") + + +if __name__ == "__main__": + main() diff --git a/student-tests/dependency_checks.py b/student-tests/dependency_checks.py new file mode 100644 index 00000000..d3efb191 --- /dev/null +++ b/student-tests/dependency_checks.py @@ -0,0 +1,71 @@ +"""Verify shared-store failures or short session TTL; restores stopped stores.""" +import argparse +import json +from pathlib import Path +import subprocess +import sys +import time +import urllib.error +import urllib.request + +ROOT = Path(__file__).resolve().parent.parent +sys.path.insert(0, str(ROOT / "tests")) +from client import Client, check + + +def docker(*args): + return subprocess.run(["docker", *args], cwd=ROOT, check=True, + capture_output=True, text=True).stdout.strip() + + +def status(url): + try: + response = urllib.request.urlopen(url, timeout=5) + except urllib.error.HTTPError as error: + response = error + with response: + return response.status + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--base-url", default="http://127.0.0.1:8084") + parser.add_argument("--expiry", action="store_true", + help="Requires API SESSION_TTL_SECONDS=2, configured by the operator") + args = parser.parse_args() + client = Client(args.base_url) + actor = client.user("dependencies") + client.call("GET", "/api/v1/users/me", actor=actor, schema="User") + if args.expiry: + time.sleep(2.3) + aliases = set() + for _ in range(12): + response = client.call("GET", "/api/v1/users/me", 401, actor=actor) + aliases.add(response["headers"]["X-Instance-Id"]) + check(len(aliases) >= 2, "Expiry not observed through two replicas") + print("PASS: TTL=2 session rejected by both replicas without re-login") + return + + try: + docker("compose", "stop", "redis") + client.call("GET", "/api/v1/users/me", actor=actor, schema="User") + client.call("GET", "/health/ready", schema="Health") + print("PASS: Redis stopped; profile and readiness still return 200 via LB") + finally: + docker("compose", "up", "-d", "--wait", "--wait-timeout", "60", "redis") + + try: + docker("compose", "stop", "postgres") + check(status(args.base_url + "/health/ready") == 503, "PostgreSQL failure did not disable readiness") + # LB may exclude all APIs, so inspect liveness inside the Docker network. + live = docker("compose", "exec", "-T", "redis", "wget", "-q", "-O", "-", "http://api:8080/health/live") + check(json.loads(live) == {"status": "ok"}, "API process liveness failed") + print("PASS: PostgreSQL stopped; LB readiness=503, API process liveness=200") + finally: + docker("compose", "up", "-d", "--wait", "--wait-timeout", "60") + client.call("GET", "/api/v1/users/me", actor=actor, schema="User") + print("PASS: PostgreSQL restored; original session still valid") + + +if __name__ == "__main__": + main() diff --git a/student-tests/fixturegen/main.go b/student-tests/fixturegen/main.go new file mode 100644 index 00000000..6d96c6a4 --- /dev/null +++ b/student-tests/fixturegen/main.go @@ -0,0 +1,32 @@ +// Generate a deterministic synthetic JPEG without third-party dependencies. +package main + +import ( + "image" + "image/color" + "image/jpeg" + "log" + "os" +) + +func main() { + if err := os.MkdirAll("data/upload-fixtures", 0755); err != nil { + log.Fatal(err) + } + file, err := os.Create("data/upload-fixtures/synthetic.jpg") + if err != nil { + log.Fatal(err) + } + img := image.NewRGBA(image.Rect(0, 0, 1024, 768)) + for y := 0; y < 768; y++ { + for x := 0; x < 1024; x++ { + img.SetRGBA(x, y, color.RGBA{uint8(x*7 + y), uint8(y*11 + x), uint8(x ^ y), 255}) + } + } + if err := jpeg.Encode(file, img, &jpeg.Options{Quality: 90}); err != nil { + log.Fatal(err) + } + if err := file.Close(); err != nil { + log.Fatal(err) + } +} diff --git a/student-tests/operations_load.py b/student-tests/operations_load.py new file mode 100644 index 00000000..4acfcb06 --- /dev/null +++ b/student-tests/operations_load.py @@ -0,0 +1,192 @@ +"""Run the unchanged public load generator during explicit API operations. + +Usage: python -X utf8 student-tests/operations_load.py baseline|stop1|stop2|rolling +Only synthetic HTTP data are created. Volumes and shared stores are preserved. +""" +import argparse +import json +from pathlib import Path +import subprocess +import sys +import time +import urllib.error +import urllib.request +import uuid + +ROOT = Path(__file__).resolve().parent.parent +BASE = "http://127.0.0.1:8084" +sys.path.insert(0, str(ROOT / "tests")) +from client import Client, Failure + + +def docker(*args): + return subprocess.run(["docker", *args], cwd=ROOT, check=True, + text=True, capture_output=True).stdout.strip() + + +def containers(): + ids = docker("compose", "ps", "-q", "api").split() + return sorted(ids, key=lambda item: docker("inspect", "--format", "{{.Name}}", item)) + + +def wait_visible(container): + deadline = time.monotonic() + 40 + alias = None + while time.monotonic() < deadline: + try: + if alias is None: + alias = docker("exec", container, "/server", "instance-id") + with urllib.request.urlopen(BASE + "/health/ready", timeout=2) as response: + if response.headers.get("X-Instance-Id") == alias: + return alias + except (subprocess.CalledProcessError, urllib.error.URLError, TimeoutError): + pass + time.sleep(0.2) + raise RuntimeError("API did not become visible through LB within 40 seconds") + + +def check_data(client, actor, chat, confirmed, action_started): + """Verify real history reads and writes after stop, not just health.""" + deadline = action_started + 10 + key = uuid.uuid4().hex + first_read = first_write = None + message = None + while time.monotonic() < deadline: + try: + history = client.call("GET", "/api/v1/chats/" + chat["id"] + "/messages?limit=100", + actor=actor, schema="MessagePage")["json"]["items"] + if any(item not in history for item in confirmed): + raise RuntimeError("Confirmed probe message lost") + if first_read is None: + first_read = time.monotonic() - action_started + except Failure: + pass + if message is None: + try: + message = client.message(actor, chat, "operation-probe-" + key, key=key)["json"] + first_write = time.monotonic() - action_started + except Failure: + pass + if first_read is not None and message is not None: + confirmed.append(message) + return {"read_recovery_seconds": round(first_read, 3), + "write_recovery_seconds": round(first_write, 3)} + time.sleep(0.1) + raise RuntimeError("History/read or message/write did not recover within 10 seconds") + + +def main(): + global BASE + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("mode", choices=["baseline", "stop1", "stop2", "rolling"]) + parser.add_argument("--base-url", default=BASE) + parser.add_argument("--output-dir", default="evidence") + parser.add_argument("--force-address-change", action="store_true", + help="During rolling, briefly reserve the removed IP to verify Docker DNS updates") + args = parser.parse_args() + mode = args.mode + BASE = args.base_url.rstrip("/") + evidence_dir = ROOT / args.output_dir + evidence_dir.mkdir(parents=True, exist_ok=True) + original = containers() + if len(original) != 2: + raise RuntimeError("Exactly two running APIs required") + for container in original: + wait_visible(container) + probe = Client(BASE, timeout=1) + actor, _, _, probe_chat = probe.bootstrap() + confirmed = [probe.message(actor, probe_chat, "confirmed-before-operation")["json"]] + duration = 50 if mode == "rolling" else 25 + output = evidence_dir / ("load-" + mode + ".json") + log = evidence_dir / ("load-" + mode + ".txt") + operations = [] + # Capture timestamps independently of load.py, whose elapsed interval starts + # after its own bootstrap. These are separate timing domains. + started = time.monotonic() + with log.open("w", encoding="utf-8") as stream: + process = subprocess.Popen([ + sys.executable, "-X", "utf8", "tests/load.py", "--base-url", BASE, + "--duration", str(duration), "--concurrency", "4", "--output", str(output), + ], cwd=ROOT, stdout=stream, stderr=subprocess.STDOUT) + try: + time.sleep(5) + if process.poll() is not None: + raise RuntimeError("Load generator stopped before operation") + if mode.startswith("stop"): + target = original[int(mode[-1]) - 1] + action_started = time.monotonic() + docker("stop", "--time", "1", target) + recovery = check_data(probe, actor, probe_chat, confirmed, action_started) + # Probe API availability after Docker has confirmed the stop. + survivor = next(item for item in original if item != target) + wait_visible(survivor) + operations.append({"action": "stop", "container": target, + "offset_seconds": round(action_started - started, 3), + "stop_and_ready_seconds": round(time.monotonic() - action_started, 3), + **recovery}) + elif mode == "rolling": + for target in original: + survivor = next(item for item in containers() if item != target) + networks = json.loads(docker("inspect", "--format", "{{json .NetworkSettings.Networks}}", target)) + network_name, network = next(iter(networks.items())) + old_ip = network["IPAddress"] + action_started = time.monotonic() + docker("stop", "--time", "15", target) + recovery = check_data(probe, actor, probe_chat, confirmed, action_started) + docker("rm", target) + holder = None + try: + if args.force_address_change: + holder = docker("run", "--rm", "-d", "--network", network_name, + "--ip", old_ip, "--cpus", "0.05", "--memory", "16m", + "--entrypoint", "/bin/sh", "redis:7.4.11-alpine", "-c", "sleep 120") + # Keep the survivor intact, create only the missing replica. + docker("compose", "up", "-d", "--no-deps", "--no-recreate", "--scale", "api=2", "api") + current = containers() + if survivor not in current or target in current or len(current) != 2: + raise RuntimeError("Replacement changed survivor or did not replace target") + replacement = next(item for item in current if item != survivor) + new_networks = json.loads(docker("inspect", "--format", "{{json .NetworkSettings.Networks}}", replacement)) + new_ip = new_networks[network_name]["IPAddress"] + if args.force_address_change and new_ip == old_ip: + raise RuntimeError("Replacement did not change IP") + alias = wait_visible(replacement) + finally: + if holder is not None: + docker("rm", "-f", holder) + operations.append({"action": "replace", "old_container": target, + "new_container": replacement, "new_instance_alias": alias, + "survivor_container": survivor, + "old_ip": old_ip, "new_ip": new_ip, + "offset_seconds": round(action_started - started, 3), + "replacement_ready_seconds": round(time.monotonic() - action_started, 3), + **recovery}) + if process.poll() is not None: + raise RuntimeError("Load did not span both replacements") + time.sleep(3) + code = process.wait(timeout=duration + 20) + if code != 0: + raise RuntimeError("Load failed; inspect " + str(log)) + history = probe.call("GET", "/api/v1/chats/" + probe_chat["id"] + "/messages?limit=100", + actor=actor, schema="MessagePage")["json"]["items"] + if len(history) != len(confirmed) or any(item not in history for item in confirmed): + raise RuntimeError("Final probe history lost messages or contains duplicates") + finally: + if process.poll() is None: + process.terminate() + process.wait(timeout=10) + docker("compose", "up", "-d", "--wait", "--wait-timeout", "60") + metadata = { + "mode": mode, "operations": operations, + "docker": json.loads(docker("info", "--format", '{"cpus":{{.NCPU}},"memory_bytes":{{.MemTotal}},"version":"{{.ServerVersion}}"}')), + "confirmed_probe_messages": len(confirmed), + "forced_address_change": args.force_address_change, + "note": "One run per mode, concurrency 4, shared load chat, no load POST retries; separate history/write probes retry with a stable key and validate confirmed data. Replacement uses the same image and no migrations.", + } + (evidence_dir / ("operations-" + mode + ".json")).write_text( + json.dumps(metadata, indent=2) + "\n", encoding="utf-8") + print(mode + ": PASS; metrics in " + str(output.relative_to(ROOT)), flush=True) + + +if __name__ == "__main__": + main() diff --git a/student-tests/replicas.py b/student-tests/replicas.py new file mode 100644 index 00000000..292859bf --- /dev/null +++ b/student-tests/replicas.py @@ -0,0 +1,77 @@ +"""Check shared sessions and concurrent idempotency through the public LB only. + +Creates synthetic data; never prints credentials or tokens. +""" +import argparse +import base64 +import concurrent.futures +import json +import urllib.error +import urllib.request +import uuid + +BASE = "http://127.0.0.1:8084" + + +def request(method, path, body=None, token=None, key=None, basic=None): + headers = {"Content-Type": "application/json"} + if token: + headers["Authorization"] = "Bearer " + token + if basic: + headers["Authorization"] = "Basic " + base64.b64encode(basic.encode()).decode() + if key is not None: + headers["Idempotency-Key"] = key + raw = None if body is None else json.dumps(body).encode() + req = urllib.request.Request(BASE + path, data=raw, headers=headers, method=method) + try: + response = urllib.request.urlopen(req, timeout=15) + except urllib.error.HTTPError as error: + response = error + with response: + return response.status, json.load(response), response.headers.get("X-Instance-Id") + + +def main(): + global BASE + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--base-url", default=BASE) + BASE = parser.parse_args().base_url.rstrip("/") + username = "r_" + uuid.uuid4().hex[:20] + password = uuid.uuid4().hex + status, user, _ = request("POST", "/api/v1/users", { + "username": username, "display_name": "Replica test", "password": password, + }) + assert status == 201, status + status, session, _ = request("POST", "/api/v1/auth/sessions", basic=username + ":" + password) + assert status == 201, status + token = session["token"] + instances = set() + for _ in range(20): + status, current, instance = request("GET", "/api/v1/users/me", token=token) + assert status == 200 and current["id"] == user["id"] + assert instance + instances.add(instance) + assert len(instances) >= 2, "Fewer than two API instances observed" + print("PASS: one session accepted by", len(instances), "API instances") + + status, chat, _ = request("POST", "/api/v1/chats", { + "title": "Concurrent retries", "member_ids": [user["id"]], + }, token=token) + assert status == 201, status + path = "/api/v1/chats/" + chat["id"] + "/messages" + key = uuid.uuid4().hex + with concurrent.futures.ThreadPoolExecutor(max_workers=8) as pool: + results = list(pool.map(lambda _: request("POST", path, {"text": "hello"}, token, key), range(16))) + original = results[0][1] + assert all(status == 201 and body == original for status, body, _ in results) + assert len({instance for _, _, instance in results}) >= 2 + assert request("POST", path, {"text": "different"}, token, key)[0] == 409 + assert request("POST", path, {"text": "hello"}, token)[0] == 400 + status, history, _ = request("GET", path, token=token) + assert status == 200 and history["items"] == [original] + print("PASS: 16 parallel retries across replicas returned the same Message; history has one item") + print("PASS: changed text -> 409; missing key -> 400") + + +if __name__ == "__main__": + main() diff --git a/student-tests/upload_boundaries.py b/student-tests/upload_boundaries.py new file mode 100644 index 00000000..10d97aa5 --- /dev/null +++ b/student-tests/upload_boundaries.py @@ -0,0 +1,58 @@ +"""Exact byte limit and chunked overflow using a valid PNG ancillary chunk.""" +import hashlib +import http.client +import json +from pathlib import Path +import struct +import sys +from urllib.parse import urlsplit +import zlib + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client, arguments, check, run, shape + + +def main(): + args = arguments('Exact 10 MiB and chunked overflow') + c = Client(args.base_url or 'http://127.0.0.1:8084', args.ca_file, timeout=65) + alice, bob, _, chat = c.bootstrap() + base = (ROOT / 'tests/fixtures/sample.png').read_bytes() + limit = 10485760 + # Insert a real tEXt chunk before IEND; no trailing bytes after the PNG end. + size = limit - len(base) - 12 + text = b'padding\0' + b'x' * (size - 8) + chunk = struct.pack('>I', size) + b'tEXt' + text + struct.pack('>I', zlib.crc32(b'tEXt' + text)) + data = base[:-12] + chunk + base[-12:] + check(len(data) == limit, 'incorrect boundary fixture size') + path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=limit.png' + a = c.call('POST', path, 201, actor=alice, raw=data, headers={'Content-Type': 'image/png'}, schema='Attachment')['json'] + check(a['size_bytes'] == limit and a['sha256'] == hashlib.sha256(data).hexdigest(), 'boundary metadata mismatch') + downloaded = c.call('GET', '/api/v1/attachments/' + a['id'] + '/content', actor=bob) + check(downloaded['bytes'] == data, '10 MiB original changed') + print('PASS: real PNG exactly 10 MiB accepted; original bytes/checksum preserved') + target = urlsplit(c.base) + check(target.scheme == 'http', 'chunked boundary scenario is local HTTP only') + + def pieces(): + for offset in range(0, len(data), 32768): + yield data[offset:offset+32768] + yield b'x' + + connection = http.client.HTTPConnection(target.hostname, target.port or 80, timeout=65) + try: + connection.request('POST', path, body=pieces(), encode_chunked=True, + headers={'Authorization': 'Bearer ' + alice['token'], 'Content-Type': 'image/png'}) + response = connection.getresponse() + body = json.loads(response.read()) + check(response.status == 413, 'chunked size overflow did not return 413') + shape(body, 'Error') + check(body['error']['request_id'] == response.headers.get('X-Request-Id'), 'request ID mismatch') + check(bool(response.headers.get('X-Instance-Id')), 'missing API instance ID') + finally: + connection.close() + print('PASS: chunked stream without Content-Length, 10 MiB+1 byte, returns 413 from API') + + +if __name__ == '__main__': + run(main) diff --git a/student-tests/upload_cleanup.py b/student-tests/upload_cleanup.py new file mode 100644 index 00000000..4452e813 --- /dev/null +++ b/student-tests/upload_cleanup.py @@ -0,0 +1,144 @@ +"""Explicit local fault injection. Ages only its own synthetic upload intent.""" +import json +from pathlib import Path +import socket +import subprocess +import sys +import uuid +from urllib.parse import urlsplit + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client, arguments, check, run + + +def compose(*args, input=None): + result = subprocess.run(['docker', 'compose', '--env-file', '.env', *args], cwd=ROOT, + input=input, text=True, capture_output=True, timeout=90) + check(result.returncode == 0, 'Compose operation failed: ' + args[0]) + return result.stdout.strip() + + +def sql(query): + return compose('exec', '-T', 'postgres', 'psql', '-X', '-qAt', '-v', 'ON_ERROR_STOP=1', + '-U', 'messenger', '-d', 'messenger', input=query) + + +def s3_exists(object_id): + # Shell text contains no credential values; they are read inside the container. + script = ('mc alias set api http://s3:9000 "$(cat /run/secrets/s3_access_key_id)" ' + '"$(cat /run/secrets/s3_secret_access_key)" >/dev/null 2>&1; ' + 'mc --json stat "api/$S3_BUCKET/attachments/' + object_id + '"') + result = subprocess.run(['docker', 'compose', '--env-file', '.env', 'run', '--rm', + '--entrypoint', '/bin/sh', 's3-bootstrap', '-c', script], + cwd=ROOT, text=True, capture_output=True, timeout=30) + lines = [json.loads(line) for line in result.stdout.splitlines() if line.startswith('{')] + check(bool(lines), 'S3 stat returned no JSON') + item = lines[-1] + if item.get('status') == 'success': + return True + # Missing and authorization/network errors must not be confused. + message = json.dumps(item).lower() + check('does not exist' in message or 'specified key' in message or 'nosuchkey' in message, + 'unexpected S3 stat error') + return False + + +def age(object_id): + sql("UPDATE upload_intents SET created_at=clock_timestamp()-INTERVAL '11 minutes', " + "expires_at=clock_timestamp()-INTERVAL '1 minute', next_cleanup_at=clock_timestamp()-INTERVAL '1 minute' " + "WHERE id='" + object_id + "';") + + +def cleanup(dry=False): + options = ('--dry-run',) if dry else () + return json.loads(compose('exec', '-T', 'api', '/server', 'cleanup', *options)) + + +def main(): + args = arguments('Upload compensation / grace period checks') + c = Client(args.base_url or 'http://127.0.0.1:8084', args.ca_file) + alice, bob, _, chat = c.bootstrap() + ready, data = c.attachment(alice, chat) + c.message(alice, chat, '', attachments=[ready['id']]) + check(sql("SELECT count(*) FROM upload_intents WHERE id='" + ready['id'] + "';") == '0', + 'successful publication left an intent') + marker = 'cleanup_test_' + uuid.uuid4().hex + filename = marker + '.png' + create_trigger = f""" +CREATE FUNCTION {marker}() RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + IF NEW.filename='{filename}' THEN RAISE EXCEPTION 'synthetic metadata failure'; END IF; + RETURN NEW; +END $$; +CREATE TRIGGER {marker} BEFORE INSERT ON attachments FOR EACH ROW EXECUTE FUNCTION {marker}(); +""" + sql(create_trigger) + failed_id = None + try: + c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + filename, + 503, actor=alice, raw=data, headers={'Content-Type': 'image/png'}) + failed_id = sql("SELECT id::text FROM upload_intents WHERE owner_id='" + alice['id'] + "';") + uuid.UUID(failed_id) + check(sql("SELECT count(*) FROM attachments WHERE id='" + failed_id + "';") == '0', 'failed publication committed metadata') + check(s3_exists(failed_id), 'fault was not injected after successful S3 PUT') + print('PASS: real S3 PUT followed by injected INSERT failure returns 503; durable intent remains') + cleanup() + check(s3_exists(failed_id), 'fresh upload removed before grace period') + print('PASS: cleanup preserves fresh failed upload during 10-minute grace period') + age(failed_id) + preview = cleanup(True) + check(preview['would_delete'] >= 1 and s3_exists(failed_id), 'dry-run modified object or missed intent') + unavailable = subprocess.run(['docker', 'compose', '--env-file', '.env', 'exec', '-T', + '-e', 'S3_ENDPOINT=http://127.0.0.1:1', 'api', '/server', 'cleanup'], + cwd=ROOT, text=True, capture_output=True, timeout=30) + check(unavailable.returncode != 0 and json.loads(unavailable.stdout)['errors'] >= 1, + 'cleanup failed to report unavailable S3') + check(sql("SELECT state FROM upload_intents WHERE id='" + failed_id + "';") == 'pending', + 'failed deletion discarded pending intent') + print('PASS: unavailable cleanup endpoint reports error and preserves intent for retry') + cleanup() + check(not s3_exists(failed_id), 'expired failed upload remains in S3') + check(sql("SELECT state FROM upload_intents WHERE id='" + failed_id + "';") == 'abandoned', 'tombstone missing') + print('PASS: aged intent detected; dry-run preserves object; cleanup removes it and retains tombstone') + # Simulate a late PUT arriving after the first deletion. + script = ('mc alias set api http://s3:9000 "$(cat /run/secrets/s3_access_key_id)" ' + '"$(cat /run/secrets/s3_secret_access_key)" >/dev/null 2>&1; ' + 'printf late | mc pipe "api/$S3_BUCKET/attachments/' + failed_id + '" >/dev/null') + compose('run', '--rm', '--entrypoint', '/bin/sh', 's3-bootstrap', '-c', script) + age(failed_id) + cleanup() + check(not s3_exists(failed_id), 'late PUT not cleaned') + age(failed_id) + cleanup() + print('PASS: late PUT removed on retry; repeat DELETE of absent object succeeds') + check(c.call('GET', '/api/v1/attachments/' + ready['id'] + '/content', actor=bob)['bytes'] == data, + 'cleanup damaged published attachment') + sql("INSERT INTO upload_intents(id,chat_id,owner_id) VALUES('" + ready['id'] + "','" + chat['id'] + "','" + alice['id'] + "');") + age(ready['id']) + cleanup() + check(sql("SELECT count(*) FROM upload_intents WHERE id='" + ready['id'] + "';") == '0', 'stale ready intent not removed') + check(c.call('GET', '/api/v1/attachments/' + ready['id'] + '/content', actor=bob)['bytes'] == data, + 'stale intent caused published object deletion') + print('PASS: published attachment referenced by message remains byte-identical') + # A client disconnects before a complete image is received: no S3 PUT starts. + target = urlsplit(c.base) + check(target.scheme == 'http', 'disconnect check is local HTTP only') + request = (f'POST /api/v1/chats/{chat["id"]}/attachments?filename=interrupted.png HTTP/1.1\r\n' + f'Host: {target.netloc}\r\nAuthorization: Bearer {alice["token"]}\r\n' + 'Content-Type: image/png\r\nContent-Length: 100000\r\n\r\n').encode() + data[:20] + with socket.create_connection((target.hostname, target.port or 80), timeout=5) as sock: + sock.sendall(request) + sock.shutdown(socket.SHUT_WR) + while sock.recv(4096): + pass + check(sql("SELECT count(*) FROM upload_intents WHERE owner_id='" + alice['id'] + "' AND state='pending';") == '0', + 'interrupted client created a pending S3 upload') + print('PASS: disconnected partial HTTP upload never reaches S3 intent stage') + finally: + sql(f'DROP TRIGGER IF EXISTS {marker} ON attachments; DROP FUNCTION IF EXISTS {marker}();') + print('PASS: upload compensation checks completed; grace aging was explicit SQL simulation') + + +if __name__ == '__main__': + run(main) diff --git a/student-tests/upload_fixtures.py b/student-tests/upload_fixtures.py new file mode 100644 index 00000000..c92c2fa5 --- /dev/null +++ b/student-tests/upload_fixtures.py @@ -0,0 +1,60 @@ +"""Deterministic streaming PNG generation; no third-party Python packages.""" +import hashlib +from pathlib import Path +import random +import struct +import zlib + +ROOT = Path(__file__).resolve().parents[1] +DIRECTORY = ROOT / 'data/upload-fixtures' + + +def chunk(file, kind, data): + file.write(struct.pack('>I', len(data)) + kind + data + struct.pack('>I', zlib.crc32(kind + data))) + + +def png(name, width, height, bit_depth, color_type, noisy=False): + path = DIRECTORY / (name + '.png') + channels = {2: 3, 6: 4}[color_type] + row_bytes = width * channels * (bit_depth // 8) + compressor = zlib.compressobj(level=1) + random_source = random.Random(20261106) + pattern = b'\x12\x34\x56\x78\x9a\xbc\xde\xf0' * 8192 + with path.open('wb') as file: + file.write(b'\x89PNG\r\n\x1a\n') + chunk(file, b'IHDR', struct.pack('>IIBBBBB', width, height, bit_depth, color_type, 0, 0, 0)) + for _ in range(height): + compressed = compressor.compress(b'\0') + if compressed: + chunk(file, b'IDAT', compressed) + remaining = row_bytes + while remaining: + size = min(remaining, 65536) + block = random_source.randbytes(size) if noisy else pattern[:size] + compressed = compressor.compress(block) + if compressed: + chunk(file, b'IDAT', compressed) + remaining -= size + chunk(file, b'IDAT', compressor.flush()) + chunk(file, b'IEND', b'') + return {'name': name, 'path': str(path), 'content_type': 'image/png', 'width': width, + 'height': height, 'bit_depth': bit_depth, 'color_type': color_type, + 'size_bytes': path.stat().st_size, 'sha256': hashlib.sha256(path.read_bytes()).hexdigest()} + + +def generate(): + DIRECTORY.mkdir(parents=True, exist_ok=True) + fixtures = [png('rgb8_1mp', 1024, 768, 8, 2, noisy=True), + png('rgba16_40mp', 8000, 5000, 16, 6), + png('rgba16_wide_40mp', 40000000, 1, 16, 6)] + jpeg = DIRECTORY / 'synthetic.jpg' + if jpeg.exists(): + fixtures.append({'name': 'jpeg_1mp', 'path': str(jpeg), 'content_type': 'image/jpeg', + 'width': 1024, 'height': 768, 'size_bytes': jpeg.stat().st_size, + 'sha256': hashlib.sha256(jpeg.read_bytes()).hexdigest()}) + return fixtures + + +if __name__ == '__main__': + import json + print(json.dumps(generate(), indent=2)) diff --git a/student-tests/upload_memory.py b/student-tests/upload_memory.py new file mode 100644 index 00000000..43b4efdc --- /dev/null +++ b/student-tests/upload_memory.py @@ -0,0 +1,211 @@ +"""Bounded upload experiment. Recreates only API; preserves all data volumes.""" +import argparse +from concurrent.futures import ThreadPoolExecutor +import datetime as dt +import hashlib +import json +from pathlib import Path +import subprocess +import sys +import threading +import time + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client, Failure, check +from upload_fixtures import generate + + +def docker(*args, required=True): + result = subprocess.run(['docker', *args], cwd=ROOT, capture_output=True, text=True, timeout=120) + if required: + check(result.returncode == 0, 'Docker operation failed: ' + args[0]) + return result + + +def compose(*args): + return docker('compose', '--env-file', '.env', *args).stdout.strip() + + +def wait_two(c, ids): + aliases = {docker('exec', cid, '/server', 'instance-id').stdout.strip() for cid in ids} + deadline = time.monotonic() + 60 + seen, stable = set(), 0 + while True: + try: + alias = c.call('GET', '/health/ready', schema='Health')['headers']['X-Instance-Id'] + check(alias in aliases, 'old API still in LB') + seen.add(alias) + stable += 1 + if seen == aliases and stable >= 8: + return + except Failure: + seen, stable = set(), 0 + check(time.monotonic() < deadline, 'two new API did not become ready') + time.sleep(0.2) + + +def resources(ids): + result = [] + for cid in ids: + state = json.loads(docker('inspect', '--format', '{{json .State}}', cid).stdout) + item = {'container_id': cid, 'status': state['Status'], 'oom_killed': state['OOMKilled']} + probe = docker('exec', cid, '/resourceprobe', required=False) + if probe.returncode == 0: + item.update(json.loads(probe.stdout)) + else: + item['cgroup_unavailable'] = True + result.append(item) + return result + + +def percentile(values, percentile): + ordered = sorted(values) + if not ordered: + return None + index = (len(ordered)-1) * percentile / 100 + low = int(index) + return round(ordered[low] + (ordered[min(low+1, len(ordered)-1)] - ordered[low]) * (index-low), 4) + + +def experiment(args, fixture): + c = Client(args.base_url, timeout=65) + compose('up', '-d', '--force-recreate', '--no-deps', '--wait', '--wait-timeout', '60', 'api') + ids = compose('ps', '-q', 'api').split() + check(len(ids) == 2, 'expected two API') + wait_two(c, ids) + alice, bob, _, chat = c.bootstrap() + for _ in range(2): + c.attachment(alice, chat) # Same small warmup for every profile. + payload = Path(fixture['path']).read_bytes() + path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + Path(fixture['path']).name + samples, stop = [], threading.Event() + + def monitor(): + while not stop.is_set(): + observation = docker('stats', '--no-stream', '--format', '{{json .}}', *ids, required=False) + if observation.returncode == 0: + for line in observation.stdout.splitlines(): + item = json.loads(line) + item['elapsed_seconds'] = round(time.monotonic() - observation_start, 3) + samples.append(item) + stop.wait(0.2) + + observation_start = time.monotonic() + before = resources(ids) + check(all('memory_peak_bytes' in item for item in before), 'cgroup v2 measurement unavailable') + thread = threading.Thread(target=monitor, daemon=True) + thread.start() + results, batches = [], [] + lock = threading.Lock() + active = maximum_active = 0 + try: + for repeat in range(args.repeats): + barrier = threading.Barrier(args.concurrency) + + def send(_): + nonlocal active, maximum_active + local = Client(args.base_url, timeout=65) + barrier.wait(timeout=15) + started = time.monotonic() + with lock: + active += 1 + maximum_active = max(maximum_active, active) + try: + response = local.call('POST', path, 201, actor=alice, raw=payload, + headers={'Content-Type': fixture['content_type']}, schema='Attachment') + return {'ok': True, 'seconds': time.monotonic()-started, 'attachment': response['json'], + 'instance': response['headers']['X-Instance-Id']} + except (Failure, OSError, TimeoutError) as exc: + return {'ok': False, 'seconds': time.monotonic()-started, 'error': str(exc)[:200]} + finally: + with lock: + active -= 1 + + started = time.monotonic() + with ThreadPoolExecutor(max_workers=args.concurrency) as executor: + batch = list(executor.map(send, range(args.concurrency))) + batches.append({'repeat': repeat+1, 'seconds': round(time.monotonic()-started, 4), + 'successes': sum(item['ok'] for item in batch), + 'errors': sum(not item['ok'] for item in batch)}) + results.extend(batch) + after = resources(ids) + observation_seconds = time.monotonic() - observation_start + finally: + stop.set() + thread.join(timeout=5) + successes = [item for item in results if item['ok']] + verified = 0 + for item in successes: + a = item['attachment'] + check(a['sha256'] == fixture['sha256'] and a['size_bytes'] == len(payload), 'upload checksum/size mismatch') + response = c.call('GET', '/api/v1/attachments/' + a['id'] + '/content', actor=bob) + check(response['bytes'] == payload and hashlib.sha256(response['bytes']).hexdigest() == fixture['sha256'], 'download mismatch') + check(response['headers'].get_content_type() == fixture['content_type'], 'download MIME mismatch') + verified += 1 + timed_seconds = sum(batch['seconds'] for batch in batches) + result = { + 'fixture': {key: value for key, value in fixture.items() if key != 'path'}, + 'concurrency': args.concurrency, 'max_client_in_flight': maximum_active, 'repeats': args.repeats, + 'warmup': '2 sample.png uploads after fresh API start', 'batches': batches, + 'successful_requests': len(successes), 'error_count': len(results)-len(successes), + 'error_fraction': (len(results)-len(successes))/len(results), + 'errors': [item['error'] for item in results if not item['ok']], + 'http_upload_seconds': round(timed_seconds, 4), + 'successful_rps': round(len(successes)/timed_seconds, 3), + 'latency_success_seconds': {f'p{p}': percentile([item['seconds'] for item in successes], p) for p in (50, 95, 99)}, + 'serving_instances': len({item['instance'] for item in successes}), 'downloads_verified': verified, + 'resources_before': before, 'resources_after': after, + 'resource_observation_seconds': round(observation_seconds, 4), 'docker_stats_samples': samples, + } + for first, last in zip(before, after): + if 'cpu_usage_usec' in last: + last['cpu_mean_percent_one_core'] = round((last['cpu_usage_usec']-first['cpu_usage_usec']) / (observation_seconds*10000), 2) + return result + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--base-url', default='http://127.0.0.1:8084') + parser.add_argument('--profiles', nargs='+', default=['rgb8_1mp', 'jpeg_1mp', 'rgba16_40mp', 'rgba16_wide_40mp']) + parser.add_argument('--concurrency', type=int, default=10) + parser.add_argument('--repeats', type=int, default=3) + parser.add_argument('--output', default='evidence/upload-memory.json') + args = parser.parse_args() + check(1 <= args.concurrency <= 32 and 1 <= args.repeats <= 5, 'invalid experiment bounds') + fixtures = {item['name']: item for item in generate()} + check(all(name in fixtures for name in args.profiles), 'missing fixture; run go run ./student-tests/fixturegen first') + engine = json.loads(docker('info', '--format', '{{json .}}').stdout) + report = {'utc': dt.datetime.now(dt.timezone.utc).isoformat(), 'python': sys.version.split()[0], + 'docker': {'server': engine['ServerVersion'], 'cpus': engine['NCPU'], 'memory_bytes': engine['MemTotal']}, + 'method': 'synchronized finite batches, fresh API per profile, kernel peak since cgroup creation including warmup/probes; no upload retries', + 'profiles': []} + output = ROOT / args.output + for name in args.profiles: + result = experiment(args, fixtures[name]) + report['profiles'].append(result) + output.write_text(json.dumps(report, indent=2), encoding='utf-8') + print(json.dumps({'profile': name, 'successes': result['successful_requests'], 'errors': result['error_count'], + 'peak_MiB': [round(item.get('memory_peak_bytes', 0)/1048576, 2) for item in result['resources_after']], + 'oom_killed': [item['oom_killed'] for item in result['resources_after']]}), flush=True) + # Restore stopped API after a diagnostic failure without changing data volumes. + stopped = docker('compose', '--env-file', '.env', 'ps', '-a', '-q', 'api').stdout.split() + for cid in stopped: + if docker('inspect', '--format', '{{.State.Running}}', cid).stdout.strip() != 'true': + docker('start', cid) + check(all(profile['error_count'] == 0 for profile in report['profiles']), 'upload failures recorded; inspect JSON') + for profile in report['profiles']: + check(all(not item['oom_killed'] and item.get('oom_kills', 0) == 0 and item.get('swap_limit_bytes') == 0 + for item in profile['resources_after']), 'OOM or unexpected swap configuration') + if args.concurrency >= 10: + check(profile['max_client_in_flight'] >= 10 and profile['serving_instances'] == 2, + 'experiment did not reach ten overlapping uploads and both API') + print('PASS: upload concurrency/memory experiment and original-byte verification') + + +if __name__ == '__main__': + try: + main() + except (Failure, OSError, ValueError) as exc: + print('FAIL:', str(exc)) + raise SystemExit(1)