From 8e1f96b70b1dca91ab1d014e0692ca8bdec27f48 Mon Sep 17 00:00:00 2001 From: dima gus Date: Fri, 20 Nov 2026 22:48:28 +0300 Subject: [PATCH] lab5 --- .dockerignore | 7 + .env.example | 33 ++- .github/pull_request_template.md | 16 +- .gitignore | 1 + Dockerfile | 20 ++ Makefile | 10 +- REPORT.md | 81 ++++-- build.gradle | 35 +++ compose.yaml | 138 ++++++++++ docker/Healthcheck.java | 20 ++ gradle.lockfile | 121 +++++++++ gradle.properties | 2 + gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 43764 bytes gradle/wrapper/gradle-wrapper.properties | 8 + gradlew | 251 ++++++++++++++++++ infra/bootstrap-s3.sh | 7 + infra/haproxy.cfg | 29 ++ infra/s3-policy.json | 15 ++ scripts/replay-failed.sh | 16 ++ scripts/restart.sh | 5 + scripts/rolling-update.sh | 14 + scripts/stop-one.sh | 14 + settings.gradle | 1 + .../java/ru/mtusi/messenger/ApiErrors.java | 74 ++++++ .../java/ru/mtusi/messenger/ApiException.java | 24 ++ .../messenger/AttachmentImageProcessor.java | 61 +++++ .../ru/mtusi/messenger/AttachmentService.java | 93 +++++++ .../ru/mtusi/messenger/AttachmentWorker.java | 119 +++++++++ .../java/ru/mtusi/messenger/CursorCodec.java | 66 +++++ .../ru/mtusi/messenger/HealthController.java | 39 +++ src/main/java/ru/mtusi/messenger/Input.java | 125 +++++++++ .../java/ru/mtusi/messenger/JobBroker.java | 46 ++++ .../mtusi/messenger/MessengerApplication.java | 26 ++ .../mtusi/messenger/MessengerController.java | 130 +++++++++ .../ru/mtusi/messenger/MessengerStore.java | 215 +++++++++++++++ src/main/java/ru/mtusi/messenger/Models.java | 31 +++ .../ru/mtusi/messenger/OutboxPublisher.java | 31 +++ .../ru/mtusi/messenger/RequestFilter.java | 106 ++++++++ .../java/ru/mtusi/messenger/S3Storage.java | 72 +++++ .../ru/mtusi/messenger/SessionService.java | 102 +++++++ src/main/resources/application.properties | 32 +++ .../resources/db/migration/V1__messenger.sql | 30 +++ .../db/migration/V2__idempotency.sql | 13 + .../db/migration/V3__attachments.sql | 24 ++ .../db/migration/V4__async_attachments.sql | 33 +++ student-tests/lab5_grade4.py | 58 ++++ 46 files changed, 2345 insertions(+), 49 deletions(-) create mode 100644 Dockerfile create mode 100644 build.gradle create mode 100644 compose.yaml create mode 100644 docker/Healthcheck.java create mode 100644 gradle.lockfile create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100755 gradlew create mode 100755 infra/bootstrap-s3.sh create mode 100644 infra/haproxy.cfg create mode 100644 infra/s3-policy.json create mode 100755 scripts/replay-failed.sh create mode 100755 scripts/restart.sh create mode 100755 scripts/rolling-update.sh create mode 100755 scripts/stop-one.sh create mode 100644 settings.gradle create mode 100644 src/main/java/ru/mtusi/messenger/ApiErrors.java create mode 100644 src/main/java/ru/mtusi/messenger/ApiException.java create mode 100644 src/main/java/ru/mtusi/messenger/AttachmentImageProcessor.java create mode 100644 src/main/java/ru/mtusi/messenger/AttachmentService.java create mode 100644 src/main/java/ru/mtusi/messenger/AttachmentWorker.java create mode 100644 src/main/java/ru/mtusi/messenger/CursorCodec.java create mode 100644 src/main/java/ru/mtusi/messenger/HealthController.java create mode 100644 src/main/java/ru/mtusi/messenger/Input.java create mode 100644 src/main/java/ru/mtusi/messenger/JobBroker.java create mode 100644 src/main/java/ru/mtusi/messenger/MessengerApplication.java create mode 100644 src/main/java/ru/mtusi/messenger/MessengerController.java create mode 100644 src/main/java/ru/mtusi/messenger/MessengerStore.java create mode 100644 src/main/java/ru/mtusi/messenger/Models.java create mode 100644 src/main/java/ru/mtusi/messenger/OutboxPublisher.java create mode 100644 src/main/java/ru/mtusi/messenger/RequestFilter.java create mode 100644 src/main/java/ru/mtusi/messenger/S3Storage.java create mode 100644 src/main/java/ru/mtusi/messenger/SessionService.java create mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/db/migration/V1__messenger.sql create mode 100644 src/main/resources/db/migration/V2__idempotency.sql create mode 100644 src/main/resources/db/migration/V3__attachments.sql create mode 100644 src/main/resources/db/migration/V4__async_attachments.sql create mode 100755 student-tests/lab5_grade4.py diff --git a/.dockerignore b/.dockerignore index bc16e556..31d8f200 100644 --- a/.dockerignore +++ b/.dockerignore @@ -18,3 +18,10 @@ __pycache__ node_modules evidence .DS_Store +target +.idea +.vscode +*.log +.m2 +.gradle +build diff --git a/.env.example b/.env.example index 119384b5..d65c60cd 100644 --- a/.env.example +++ b/.env.example @@ -1,25 +1,24 @@ # Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. APP_HOST=0.0.0.0 APP_PORT=8080 -APP_ENV=lab SESSION_TTL_SECONDS=3600 -# Choose credentials locally. File paths contain no secret values. -DATABASE_URL_FILE=/run/secrets/database_url -# Optional on 3, required use on 4 in lab2; Redis OR Valkey: -SESSION_STORE=postgres -CACHE_URL_FILE=/run/secrets/cache_url -API_REPLICAS=2 +POSTGRES_DB=messenger +POSTGRES_USER=messenger +POSTGRES_PASSWORD=change-me-local-only +DB_REQUEST_TIMEOUT_MS=2000 IDEMPOTENCY_TTL_SECONDS=86400 -S3_ENDPOINT=http://s3:9000 -S3_REGION=us-east-1 +# Generate with: openssl rand -base64 32 | tr '+/' '-_' | tr -d '=' +CURSOR_SECRET=replace-with-url-safe-base64-secret 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 +S3_ROOT_USER=minioadmin +S3_ROOT_PASSWORD=change-me-minio-root +S3_ACCESS_KEY_ID=messenger-api +S3_SECRET_ACCESS_KEY=change-me-api-secret MAX_UPLOAD_BYTES=10485760 MAX_IMAGE_PIXELS=40000000 -BROKER_URL_FILE=/run/secrets/broker_url -WORKER_CONCURRENCY=2 -JOB_MAX_ATTEMPTS=5 -JOB_TIMEOUT_SECONDS=60 -STAGING_RETENTION_SECONDS=86400 +WORKER_ONLY=true +WORKER_POLL_MS=500 +WORKER_POLL_INITIAL_DELAY_MS=500 +WORKER_LEASE_SCAN_MS=1000 +OUTBOX_PUBLISH_INTERVAL_MS=500 +WORKER_MAX_ATTEMPTS=5 diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 7d1ff4a4..9f442d5e 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,14 +1,14 @@ ## Лабораторная 5 -Автор / группа: TODO -Целевая оценка (3/4/5): TODO -Трек 5, если заявлен: TODO +Автор / группа: Гусак Дмитрий / БПИ2303 +Целевая оценка (3/4/5): 4 +Трек 5, если заявлен: не заявлен -Что реализовано и что перенесено: TODO -Команды запуска из чистого clone: TODO -Результаты проверок и ссылка на REPORT.md: TODO -Ограничения / известные ошибки: TODO -Источники и использование ИИ: TODO +Что реализовано и что перенесено: сохранён минимум lab1–lab4; добавлены Redis Streams, transactional outbox, отдельный worker, queued/processing/ready/failed, retry/lease/DLQ, thumbnail/crop/WebP и идемпотентная фиксация вариантов. +Команды запуска из чистого clone: `cp .env.example .env`, заполнить `CURSOR_SECRET`, `docker compose --env-file .env up -d --build --scale api=2`. +Результаты проверок и ссылка на REPORT.md: `make check`, `make test`, `make test-grade4`, `make validate`; подробности в `REPORT.md`. +Ограничения / известные ошибки: at-least-once вместо exactly-once; production janitor/метрики lag не входят в цель 4. +Источники и использование ИИ: README/контракты и официальная документация Redis Streams, Spring Boot и MinIO; ИИ использовался для разбора требований и ревью. - [ ] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами. - [ ] Контракт и публичные тесты не ослаблены. diff --git a/.gitignore b/.gitignore index b5a71896..556778da 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ volumes/ data/ backups/ # Runtime/build artifacts +.gradle/ .venv/ __pycache__/ *.py[cod] diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..20dd9ec3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM eclipse-temurin:21.0.10_7-jdk-jammy@sha256:25d1276565738d3c805e632a4542c3a7598866ef967f4def6544c15de3a74b14 AS build +WORKDIR /build +COPY gradlew build.gradle settings.gradle gradle.properties gradle.lockfile ./ +COPY gradle ./gradle +RUN chmod +x gradlew && ./gradlew --no-daemon --console=plain resolveDependencies +COPY src ./src +COPY docker/Healthcheck.java ./docker/Healthcheck.java +RUN ./gradlew --no-daemon --console=plain build && javac -d /healthcheck docker/Healthcheck.java + +FROM eclipse-temurin:21.0.10_7-jre-jammy@sha256:fa4854e6057665066cb79953616671c20f32b96a85d6dd8b78db0762203924c4 +RUN groupadd --gid 10001 messenger && useradd --uid 10001 --gid messenger --no-create-home --shell /usr/sbin/nologin messenger +WORKDIR /app +COPY --from=build /build/build/libs/messenger.jar /app/messenger.jar +COPY --from=build /healthcheck /app/healthcheck +ENV APP_HOST=0.0.0.0 APP_PORT=8080 JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=60 -XX:+ExitOnOutOfMemoryError" +USER 10001:10001 +EXPOSE 8080 +STOPSIGNAL SIGTERM +HEALTHCHECK --interval=5s --timeout=3s --start-period=30s --retries=3 CMD ["java", "-cp", "/app/healthcheck", "Healthcheck"] +ENTRYPOINT ["java", "-jar", "/app/messenger.jar"] diff --git a/Makefile b/Makefile index 5d86d171..c31946e3 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,10 @@ CA_FILE ?= ACTION_SCRIPT ?= TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) -.PHONY: help check validate test load test-persistence test-failover +.PHONY: help check validate test test-grade4 load test-persistence test-failover compose-config help: - @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" + @echo "check: offline structure; validate: OpenAPI; test: public API; test-grade4: async image pipeline; load: synthetic writes" check: $(PYTHON) scripts/check_contract.py @@ -18,6 +18,12 @@ validate: test: $(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS) +test-grade4: + $(PYTHON) student-tests/lab5_grade4.py --base-url "$(BASE_URL)" $(TLS_ARGS) + +compose-config: + docker compose --env-file .env config --quiet + load: $(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS) diff --git a/REPORT.md b/REPORT.md index e98f75e9..55c02490 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,74 @@ # Отчёт — лабораторная 5 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Гусак Дмитрий, БПИ2303 +- Целевая оценка: 4 +- Трек 5: не заявлен + +## Что реализовано + +Сохранен минимум лабораторных 1–4: регистрация и Basic/Bearer-аутентификация, PostgreSQL, Redis-сессии, HAProxy, idempotency для сообщений, MinIO и ACL чата. + +В этой работе загрузка изображения стала асинхронной. API сначала потоково принимает файл во временный файл, проверяет MIME/signature, декодирование, размер (10 MiB) и число пикселей (40 млн), затем кладёт оригинал в приватный staging-prefix MinIO и одной транзакцией PostgreSQL сохраняет `attachments` и `attachment_jobs`. Только после этого отвечает `202 queued`. + +`OutboxPublisher` регулярно публикует в Redis Stream только `job_id`, тип и версию схемы. Отдельный контейнер `worker` читает durable stream с consumer group, получает staging-объект, создаёт конечный original, JPEG thumbnail 256×256 fit без увеличения, JPEG crop 128×128 и WebP optimized до 1280 px. В PostgreSQL записываются варианты с размерами, MIME и SHA-256, затем attachment переводится в `ready`, а staging удаляется. + +Повторная доставка безопасна: job имеет уникальный `job_id`, варианты имеют первичный ключ `(attachment_id, name)`, записи используют `ON CONFLICT DO NOTHING`, а object keys детерминированы. Есть lease 30 секунд, ограничение пяти попыток, exponential backoff с jitter, безопасные `temporary_error`/`retry_exhausted` и восстановление outbox-записей после разрыва между COMMIT и publish. Redis работает с AOF `everysec`, PostgreSQL и MinIO — с именованными volumes. + +Для финального failed-состояния предусмотрен явный операторский replay без создания новой attachment: `ATTACHMENT_ID=... scripts/replay-failed.sh`. Он переводит только `failed` job обратно в `queued`, после чего outbox публикует тот же `job_id`. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Нужны Docker Desktop, Java 21/Gradle Wrapper и Python 3.9+. Секреты не коммитятся: -## Архитектура и решения +```sh +cp .env.example .env +openssl rand -base64 32 | tr '+/' '-_' | tr -d '=' +# вставить результат в CURSOR_SECRET в .env +docker compose --env-file .env config --quiet +docker compose --env-file .env up -d --build --scale api=2 +``` -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab4, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +Ожидать успешного healthcheck у `api` и `lb`, затем использовать порт `APP_PORT` (по умолчанию 8080). Остановка без удаления данных: -## Доказательства критериев +```sh +docker compose --env-file .env stop +``` -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +Намеренное удаление учебных данных (не выполнять для обычной остановки): -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +```sh +docker compose --env-file .env down -v +``` -## Эксперименты и отказы +## Проверки -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +```sh +make check +make test BASE_URL=http://localhost:8080 +make test-grade4 BASE_URL=http://localhost:8080 +make validate +``` -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +Фактически проверено на локальном стенде с `APP_PORT=8084`: -## Ограничения и следующие шаги +```text +make check PASS: offline skeleton structure... +make test BASE_URL=http://localhost:8084 +PASS: lab5 public API scenario; infrastructure and grading evidence remain separate. +make test-grade4 BASE_URL=http://localhost:8084 +PASS: grade-4 async attachment pipeline; queue, worker, crop, WebP and idempotent metadata verified. +make test-persistence ACTION_SCRIPT=./scripts/restart.sh BASE_URL=http://localhost:8084 +PASS: persistence preserved confirmed data and existing session. +make test-failover ACTION_SCRIPT=./scripts/stop-one.sh BASE_URL=http://localhost:8084 +PASS: failover preserved confirmed data and existing session. +``` -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +Публичный smoke проверяет `202 queued`, связь queued-вложения с сообщением, переход в `ready`, оригинал и thumbnail. Собственный `student-tests/lab5_grade4.py` дополнительно проверяет crop, WebP-сигнатуру, фактические JPEG-размеры, SHA-256 всех вариантов и стабильность повторного GET. Отказ worker воспроизводится так: `docker compose stop worker`, выполнить upload (получить 202 и 409 на content), затем `docker compose start worker` и дождаться `ready`; данные не теряются. + +## Ограничения + +Это учебный single-process worker с Redis consumer group и bounded polling; production-масштабирование требует отдельной настройки числа consumers, мониторинга lag/DLQ и janitor для очень старых staging-объектов. WebP-библиотека `webp4j` содержит native backend для Linux x64/ARM64; при смене платформы образ должен соответствовать архитектуре. Exactly-once доставка не заявляется: гарантируется at-least-once и идемпотентный эффект. + +## Источники и ИИ + +Использованы README/контракты лаборатории, официальная документация Redis Streams, Spring Boot scheduling/JDBC и MinIO S3 API. При подготовке кода использовалась помощь ИИ для разбора требований и ревью; все решения, команды запуска и результаты проверок воспроизведены локально. diff --git a/build.gradle b/build.gradle new file mode 100644 index 00000000..a05f40a8 --- /dev/null +++ b/build.gradle @@ -0,0 +1,35 @@ +plugins { + id 'java' + id 'org.springframework.boot' version '3.5.16' +} + +group = 'ru.mtusi' +version = '2.0.0' +description = 'Lab 5 messenger with durable asynchronous image processing' + +java { toolchain { languageVersion = JavaLanguageVersion.of(21) } } +repositories { mavenCentral() } + +dependencies { + implementation platform('org.springframework.boot:spring-boot-dependencies:3.5.16') + implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-jdbc' + implementation 'org.springframework.boot:spring-boot-starter-data-redis' + implementation 'org.flywaydb:flyway-core' + implementation 'org.flywaydb:flyway-database-postgresql' + implementation 'org.postgresql:postgresql' + implementation 'org.springframework.security:spring-security-crypto' + implementation 'org.bouncycastle:bcprov-jdk18on:1.80' + implementation 'io.minio:minio:8.5.17' + implementation 'io.github.lajczik:webp4j:2.7.2' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' +} + +dependencyLocking { lockAllConfigurations(); lockMode = LockMode.STRICT } +tasks.withType(JavaCompile).configureEach { options.release = 21; options.encoding = 'UTF-8'; options.compilerArgs.add('-parameters') } +tasks.withType(AbstractArchiveTask).configureEach { preserveFileTimestamps = false; reproducibleFileOrder = true } +tasks.named('bootJar') { archiveFileName = 'messenger.jar' } +tasks.named('jar') { enabled = false } +tasks.named('test') { useJUnitPlatform(); maxHeapSize = '256m'; testLogging { events 'passed','skipped','failed'; exceptionFormat = 'full' } } +tasks.register('resolveDependencies') { doLast { configurations.findAll { it.canBeResolved }.each { it.resolve() } } } diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..b6dc7f49 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,138 @@ +services: + postgres: + image: postgres:16.4-alpine + environment: + POSTGRES_DB: ${POSTGRES_DB:-messenger} + POSTGRES_USER: ${POSTGRES_USER:-messenger} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-messenger-dev-only} + volumes: [postgres_data:/var/lib/postgresql/data] + healthcheck: + test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"] + interval: 2s + timeout: 3s + retries: 15 + cpus: "1.0" + mem_limit: 512m + + redis: + image: redis:7.4.2-alpine + command: ["redis-server", "--appendonly", "yes", "--appendfsync", "everysec"] + volumes: [redis_data:/data] + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 2s + timeout: 3s + retries: 15 + cpus: "0.5" + mem_limit: 256m + + minio: + image: minio/minio:RELEASE.2024-06-13T22-53-53Z + command: server /data --console-address ":9001" + environment: + MINIO_ROOT_USER: ${S3_ROOT_USER:-minioadmin} + MINIO_ROOT_PASSWORD: ${S3_ROOT_PASSWORD:-minioadmin123} + volumes: [s3_data:/data] + healthcheck: + test: ["CMD-SHELL", "curl -fsS http://127.0.0.1:9000/minio/health/live >/dev/null"] + interval: 2s + timeout: 3s + retries: 20 + cpus: "1.0" + mem_limit: 512m + + s3-bootstrap: + image: minio/mc:RELEASE.2024-06-13T15-53-12Z + depends_on: {minio: {condition: service_healthy}} + environment: + S3_ROOT_USER: ${S3_ROOT_USER:-minioadmin} + S3_ROOT_PASSWORD: ${S3_ROOT_PASSWORD:-minioadmin123} + S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:?Set S3_ACCESS_KEY_ID in .env} + S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:?Set S3_SECRET_ACCESS_KEY in .env} + S3_BUCKET: ${S3_BUCKET:-messenger-attachments} + volumes: + - ./infra/s3-policy.json:/tmp/s3-policy.json:ro + - ./infra/bootstrap-s3.sh:/bootstrap-s3.sh:ro + entrypoint: ["/bin/sh", "/bootstrap-s3.sh"] + restart: "no" + + migrate: + image: mtusi-messenger:lab5 + environment: &app-env + APP_HOST: 0.0.0.0 + APP_PORT: 8080 + DB_HOST: postgres + DB_PORT: 5432 + DB_NAME: ${POSTGRES_DB:-messenger} + DB_USERNAME: ${POSTGRES_USER:-messenger} + DB_PASSWORD: ${POSTGRES_PASSWORD:-messenger-dev-only} + REDIS_HOST: redis + REDIS_PORT: 6379 + CURSOR_SECRET: ${CURSOR_SECRET:?Set CURSOR_SECRET in .env} + S3_ENDPOINT: http://minio:9000 + S3_BUCKET: ${S3_BUCKET:-messenger-attachments} + S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:?Set S3_ACCESS_KEY_ID in .env} + S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:?Set S3_SECRET_ACCESS_KEY in .env} + MIGRATION_ONLY: "true" + SPRING_MAIN_WEB_APPLICATION_TYPE: none + depends_on: + postgres: {condition: service_healthy} + s3-bootstrap: {condition: service_completed_successfully} + restart: "no" + + api: + build: . + image: mtusi-messenger:lab5 + environment: + <<: *app-env + MIGRATION_ONLY: "false" + SPRING_MAIN_WEB_APPLICATION_TYPE: servlet + depends_on: + migrate: {condition: service_completed_successfully} + postgres: {condition: service_healthy} + redis: {condition: service_healthy} + minio: {condition: service_healthy} + healthcheck: + test: ["CMD", "java", "-cp", "/app/healthcheck", "Healthcheck"] + interval: 1s + timeout: 2s + start_period: 20s + retries: 3 + stop_grace_period: 10s + cpus: "1.0" + mem_limit: 512m + + worker: + image: mtusi-messenger:lab5 + environment: + <<: *app-env + WORKER_ONLY: "true" + SPRING_MAIN_WEB_APPLICATION_TYPE: none + MIGRATION_ONLY: "false" + depends_on: + migrate: {condition: service_completed_successfully} + postgres: {condition: service_healthy} + redis: {condition: service_healthy} + minio: {condition: service_healthy} + stop_grace_period: 10s + cpus: "1.0" + mem_limit: 768m + healthcheck: + disable: true + + lb: + image: haproxy:3.1.8-alpine + ports: ["127.0.0.1:${APP_PORT:-8080}:8080"] + volumes: [./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro] + depends_on: {api: {condition: service_healthy}} + healthcheck: + test: ["CMD-SHELL", "wget -q -O - http://127.0.0.1:8080/health/live >/dev/null"] + interval: 2s + timeout: 2s + retries: 5 + stop_grace_period: 10s + +volumes: + postgres_data: + redis_data: + s3_data: diff --git a/docker/Healthcheck.java b/docker/Healthcheck.java new file mode 100644 index 00000000..0ff9be9e --- /dev/null +++ b/docker/Healthcheck.java @@ -0,0 +1,20 @@ +import java.net.HttpURLConnection; +import java.net.URI; + +/** Compiled in the build stage; works in a JRE without curl, shell parsing or extra packages. */ +public class Healthcheck { + public static void main(String[] args) { + try { + String port = System.getenv().getOrDefault("APP_PORT", "8080"); + HttpURLConnection connection = (HttpURLConnection) URI.create( + "http://127.0.0.1:" + port + "/health/ready").toURL().openConnection(); + connection.setConnectTimeout(1000); + connection.setReadTimeout(1000); + int status = connection.getResponseCode(); + connection.disconnect(); + System.exit(status == 200 ? 0 : 1); + } catch (Exception exception) { + System.exit(1); + } + } +} diff --git a/gradle.lockfile b/gradle.lockfile new file mode 100644 index 00000000..3d16e500 --- /dev/null +++ b/gradle.lockfile @@ -0,0 +1,121 @@ +# This is a Gradle generated file for dependency locking. +# Manual edits can break the build and are not advised. +# This file is expected to be part of source control. +ch.qos.logback:logback-classic:1.5.34=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +ch.qos.logback:logback-core:1.5.34=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.carrotsearch.thirdparty:simple-xml-safe:2.7.1=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.fasterxml.jackson.core:jackson-annotations:2.21=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.fasterxml.jackson.core:jackson-core:2.21.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.fasterxml.jackson.core:jackson-databind:2.21.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.fasterxml.jackson.dataformat:jackson-dataformat-toml:2.21.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.21.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.21.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.fasterxml.jackson.module:jackson-module-parameter-names:2.21.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.fasterxml.jackson:jackson-bom:2.21.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.google.code.findbugs:jsr305:3.0.2=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.google.errorprone:error_prone_annotations:2.28.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.google.guava:failureaccess:1.0.2=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.google.guava:guava:33.3.1-jre=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.google.j2objc:j2objc-annotations:3.0.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.jayway.jsonpath:json-path:2.9.0=testCompileClasspath,testRuntimeClasspath +com.squareup.okhttp3:okhttp:4.12.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.squareup.okio:okio-jvm:3.6.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.squareup.okio:okio:3.6.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +com.vaadin.external.google:android-json:0.0.20131108.vaadin1=testCompileClasspath,testRuntimeClasspath +com.zaxxer:HikariCP:6.3.3=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +commons-codec:commons-codec:1.18.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +commons-io:commons-io:2.16.1=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +io.github.lajczik:webp4j:2.7.2=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +io.lettuce:lettuce-core:6.6.0.RELEASE=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +io.micrometer:micrometer-commons:1.15.12=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +io.micrometer:micrometer-observation:1.15.12=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +io.minio:minio:8.5.17=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +io.netty:netty-buffer:4.1.135.Final=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +io.netty:netty-codec:4.1.135.Final=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +io.netty:netty-common:4.1.135.Final=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +io.netty:netty-handler:4.1.135.Final=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +io.netty:netty-resolver:4.1.135.Final=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +io.netty:netty-transport-native-unix-common:4.1.135.Final=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +io.netty:netty-transport:4.1.135.Final=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +io.projectreactor:reactor-core:3.7.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +jakarta.activation:jakarta.activation-api:2.1.4=productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +jakarta.annotation:jakarta.annotation-api:2.1.1=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +jakarta.xml.bind:jakarta.xml.bind-api:4.0.5=productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +net.bytebuddy:byte-buddy-agent:1.17.8=testCompileClasspath,testRuntimeClasspath +net.bytebuddy:byte-buddy:1.18.3=testCompileClasspath,testRuntimeClasspath +net.minidev:accessors-smart:2.5.2=testCompileClasspath,testRuntimeClasspath +net.minidev:json-smart:2.5.2=testCompileClasspath,testRuntimeClasspath +org.apache.commons:commons-compress:1.27.1=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.apache.commons:commons-lang3:3.17.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.apache.logging.log4j:log4j-api:2.24.3=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.apache.logging.log4j:log4j-to-slf4j:2.24.3=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.apache.tomcat.embed:tomcat-embed-core:10.1.55=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.apache.tomcat.embed:tomcat-embed-el:10.1.55=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.apache.tomcat.embed:tomcat-embed-websocket:10.1.55=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.apiguardian:apiguardian-api:1.1.2=testCompileClasspath +org.assertj:assertj-core:3.27.7=testCompileClasspath,testRuntimeClasspath +org.awaitility:awaitility:4.2.2=testCompileClasspath,testRuntimeClasspath +org.bouncycastle:bcprov-jdk18on:1.80=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.checkerframework:checker-qual:3.43.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.flywaydb:flyway-core:11.7.2=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.flywaydb:flyway-database-postgresql:11.7.2=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.hamcrest:hamcrest:3.0=testCompileClasspath,testRuntimeClasspath +org.jetbrains.kotlin:kotlin-stdlib-common:1.9.25=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.9.25=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.9.25=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.jetbrains.kotlin:kotlin-stdlib:1.9.25=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.jetbrains:annotations:13.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.junit.jupiter:junit-jupiter-api:5.12.2=testCompileClasspath,testRuntimeClasspath +org.junit.jupiter:junit-jupiter-engine:5.12.2=testRuntimeClasspath +org.junit.jupiter:junit-jupiter-params:5.12.2=testCompileClasspath,testRuntimeClasspath +org.junit.jupiter:junit-jupiter:5.12.2=testCompileClasspath,testRuntimeClasspath +org.junit.platform:junit-platform-commons:1.12.2=testCompileClasspath,testRuntimeClasspath +org.junit.platform:junit-platform-engine:1.12.2=testRuntimeClasspath +org.junit.platform:junit-platform-launcher:1.12.2=testRuntimeClasspath +org.junit:junit-bom:5.12.2=testCompileClasspath,testRuntimeClasspath +org.mockito:mockito-core:5.17.0=testCompileClasspath,testRuntimeClasspath +org.mockito:mockito-junit-jupiter:5.17.0=testCompileClasspath,testRuntimeClasspath +org.objenesis:objenesis:3.3=testRuntimeClasspath +org.opentest4j:opentest4j:1.3.0=testCompileClasspath,testRuntimeClasspath +org.ow2.asm:asm:9.7.1=testCompileClasspath,testRuntimeClasspath +org.postgresql:postgresql:42.7.11=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.reactivestreams:reactive-streams:1.0.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.skyscreamer:jsonassert:1.5.3=testCompileClasspath,testRuntimeClasspath +org.slf4j:jul-to-slf4j:2.0.18=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.slf4j:slf4j-api:2.0.18=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework.boot:spring-boot-autoconfigure:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework.boot:spring-boot-dependencies:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework.boot:spring-boot-starter-data-redis:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework.boot:spring-boot-starter-jdbc:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework.boot:spring-boot-starter-json:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework.boot:spring-boot-starter-logging:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework.boot:spring-boot-starter-test:3.5.16=testCompileClasspath,testRuntimeClasspath +org.springframework.boot:spring-boot-starter-tomcat:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework.boot:spring-boot-starter-web:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework.boot:spring-boot-starter:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework.boot:spring-boot-test-autoconfigure:3.5.16=testCompileClasspath,testRuntimeClasspath +org.springframework.boot:spring-boot-test:3.5.16=testCompileClasspath,testRuntimeClasspath +org.springframework.boot:spring-boot:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework.data:spring-data-commons:3.5.13=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework.data:spring-data-keyvalue:3.5.13=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework.data:spring-data-redis:3.5.13=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework.security:spring-security-crypto:6.5.11=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework:spring-aop:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework:spring-beans:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework:spring-context-support:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework:spring-context:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework:spring-core:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework:spring-expression:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework:spring-jcl:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework:spring-jdbc:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework:spring-oxm:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework:spring-test:6.2.19=testCompileClasspath,testRuntimeClasspath +org.springframework:spring-tx:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework:spring-web:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.springframework:spring-webmvc:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.xerial.snappy:snappy-java:1.1.10.7=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.xmlunit:xmlunit-core:2.10.4=testCompileClasspath,testRuntimeClasspath +org.yaml:snakeyaml:2.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +redis.clients.authentication:redis-authx-core:0.1.1-beta2=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +empty=annotationProcessor,developmentOnly,testAndDevelopmentOnly,testAnnotationProcessor diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 00000000..14595c8a --- /dev/null +++ b/gradle.properties @@ -0,0 +1,2 @@ +org.gradle.jvmargs=-Xmx512m -Dfile.encoding=UTF-8 +org.gradle.workers.max=2 diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000000000000000000000000000000000..1b33c55baabb587c669f562ae36f953de2481846 GIT binary patch literal 43764 zcma&OWmKeVvL#I6?i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0Nn(EKk@5^qO4@l@! z6O0lr%tzGC`D^)8xU3FnMZVm0kX1sBWhaQyzVoXFWwr%Ny?=2M{5s#5i7fTu3gEkG zc{(Pr$v=;`Y#&`y*J}#M9ux>0?xu!`$9cUKm#Bdd_&S#LPTS?ZPV6zN6>W6JTS~-LfjL{mB=b(KMk3 z2HjBSlJeyUVqDd=Mt!=hpYsvby2GL&3~zm;0{^nZJq+4vb?5HH4wufvr}IX42sHeK zm@x?HN$8TsTavXs)tLDFJtY9b)y~Tl@7z4^I8oUQq4JckH@~CVQ;FoK(+e0XAM>1O z(ei}h?)JQp>)d=6ng-BZF1Z5hsAKW@mXq+hU?r8I(*%`tnIIOXw7V6ZK(T9RFJJe@ zZS!aC+p)Gf2Ujc=a6hx4!A1Th%YH!Lb^xpI!Eu` zmJO{9rw){B1Ql18d%F%da+Tbu1()?o(zT7StYqK6_w`e+fjXq5L^y(0 z09QA6H4oFj59c2wR~{~>jUoDzDdKz}5#onYPJRwa`SUO)Pd4)?(ENBaFVLJr6Kvz= zhTtXqbx09C1z~~iZt;g^9_2nCZ{};-b4dQJbv8HsWHXPVg^@(*!@xycp#R?a|L!+` zY5w))JWV`Gls(=}shH0#r*;~>_+-P5Qc978+QUd>J%`fyn{*TsiG-dWMiJXNgwBaT zJ=wgYFt+1ACW)XwtNx)Q9tA2LPoB&DkL16P)ERWQlY4%Y`-5aM9mZ{eKPUgI!~J3Z zkMd5A_p&v?V-o-6TUa8BndiX?ooviev(DKw=*bBVOW|=zps9=Yl|-R5@yJe*BPzN}a0mUsLn{4LfjB_oxpv(mwq# zSY*%E{iB)sNvWfzg-B!R!|+x(Q|b@>{-~cFvdDHA{F2sFGA5QGiIWy#3?P2JIpPKg6ncI^)dvqe`_|N=8 '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH="\\\"\\\"" + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='-Dfile.encoding=UTF-8 "-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/infra/bootstrap-s3.sh b/infra/bootstrap-s3.sh new file mode 100755 index 00000000..5827b93b --- /dev/null +++ b/infra/bootstrap-s3.sh @@ -0,0 +1,7 @@ +#!/bin/sh +set -eu +mc alias set local http://minio:9000 "$S3_ROOT_USER" "$S3_ROOT_PASSWORD" +mc mb --ignore-existing "local/$S3_BUCKET" +mc admin policy create local messenger-api-policy-v2 /tmp/s3-policy.json || true +mc admin user add local "$S3_ACCESS_KEY_ID" "$S3_SECRET_ACCESS_KEY" || true +mc admin policy attach local messenger-api-policy-v2 --user "$S3_ACCESS_KEY_ID" diff --git a/infra/haproxy.cfg b/infra/haproxy.cfg new file mode 100644 index 00000000..ce156a19 --- /dev/null +++ b/infra/haproxy.cfg @@ -0,0 +1,29 @@ +global + log stdout format raw local0 + maxconn 2048 + +defaults + mode http + option httplog + timeout http-request 5s + timeout connect 1s + timeout client 30s + timeout server 30s + option httpchk GET /health/ready + http-check expect status 200 + +resolvers docker + nameserver dns 127.0.0.11:53 + resolve_retries 3 + timeout resolve 1s + timeout retry 1s + hold valid 5s + +frontend public + bind :8080 + default_backend api_backend + +backend api_backend + balance roundrobin + option httpchk GET /health/ready + server-template api 4 api:8080 check inter 1s fall 2 rise 2 resolvers docker init-addr last,libc,none diff --git a/infra/s3-policy.json b/infra/s3-policy.json new file mode 100644 index 00000000..57613fd5 --- /dev/null +++ b/infra/s3-policy.json @@ -0,0 +1,15 @@ +{ + "Version": "2012-10-17", + "Statement": [ + { + "Action": ["s3:ListBucket", "s3:GetBucketLocation"], + "Effect": "Allow", + "Resource": ["arn:aws:s3:::messenger-attachments"] + }, + { + "Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"], + "Effect": "Allow", + "Resource": ["arn:aws:s3:::messenger-attachments/*"] + } + ] +} diff --git a/scripts/replay-failed.sh b/scripts/replay-failed.sh new file mode 100755 index 00000000..b6574d65 --- /dev/null +++ b/scripts/replay-failed.sh @@ -0,0 +1,16 @@ +#!/bin/sh +set -eu + +: "${ATTACHMENT_ID:?Set ATTACHMENT_ID to the failed attachment UUID}" + +# Explicit operator action: only a failed job is moved back to the durable queue. +docker compose --env-file .env exec -T postgres psql -U "${POSTGRES_USER:-messenger}" -d "${POSTGRES_DB:-messenger}" -v ON_ERROR_STOP=1 \ + -v attachment_id="$ATTACHMENT_ID" <<'SQL' +UPDATE attachment_jobs +SET status = 'queued', attempt = 0, next_attempt_at = now(), lease_until = NULL, + published_at = NULL, error_code = NULL, updated_at = now() +WHERE attachment_id = :'attachment_id' AND status = 'failed'; +UPDATE attachments +SET status = 'queued', error_code = NULL, updated_at = now() +WHERE id = :'attachment_id' AND status = 'failed'; +SQL diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..41630ab7 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,5 @@ +#!/bin/sh +set -eu + +# Restart API replicas and shared stores without removing named volumes. +docker compose --env-file .env restart api worker postgres redis minio lb diff --git a/scripts/rolling-update.sh b/scripts/rolling-update.sh new file mode 100755 index 00000000..136d716f --- /dev/null +++ b/scripts/rolling-update.sh @@ -0,0 +1,14 @@ +#!/bin/sh +set -eu + +# Restart one API container at a time and wait for its healthcheck. +for container in $(docker compose --env-file .env ps -q api); do + docker restart "$container" >/dev/null + deadline=$(( $(date +%s) + 60 )) + while :; do + status=$(docker inspect --format '{{.State.Health.Status}}' "$container" 2>/dev/null || true) + [ "$status" = "healthy" ] && break + [ "$(date +%s)" -lt "$deadline" ] || exit 1 + sleep 1 + done +done diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..340b83db --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,14 @@ +#!/bin/sh +set -eu + +: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}" + +for container in $(docker compose --env-file .env ps -q api); do + hostname=$(docker inspect --format '{{.Config.Hostname}}' "$container") + if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then + docker stop "$container" >/dev/null + exit 0 + fi +done + +exit 2 diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 00000000..ab50d253 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'messenger' diff --git a/src/main/java/ru/mtusi/messenger/ApiErrors.java b/src/main/java/ru/mtusi/messenger/ApiErrors.java new file mode 100644 index 00000000..b0546870 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/ApiErrors.java @@ -0,0 +1,74 @@ +package ru.mtusi.messenger; + +import jakarta.servlet.http.HttpServletRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.ResponseEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.dao.DataAccessException; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.web.HttpMediaTypeNotSupportedException; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.bind.MissingServletRequestParameterException; +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; +import org.springframework.web.servlet.resource.NoResourceFoundException; +import static ru.mtusi.messenger.Models.*; + +@RestControllerAdvice +public class ApiErrors { + private static final Logger LOG = LoggerFactory.getLogger(ApiErrors.class); + + @ExceptionHandler(ApiException.class) + ResponseEntity application(ApiException error, HttpServletRequest request) { + return response(error.status(), error.code(), error.getMessage(), request); + } + + @ExceptionHandler({HttpMessageNotReadableException.class, MethodArgumentTypeMismatchException.class, + MissingServletRequestParameterException.class}) + ResponseEntity invalid(Exception error, HttpServletRequest request) { + return response(400, "invalid_request", "Некорректный JSON или параметры запроса", request); + } + + @ExceptionHandler(HttpMediaTypeNotSupportedException.class) + ResponseEntity media(Exception error, HttpServletRequest request) { + return response(415, "unsupported_media_type", "Ожидается Content-Type: application/json", request); + } + + @ExceptionHandler(DataAccessException.class) + ResponseEntity database(DataAccessException error, HttpServletRequest request) { + LOG.error("request_id={} error_type={}", request.getAttribute(RequestFilter.REQUEST_ID), error.getClass().getSimpleName()); + return response(503, "unavailable", "Хранилище данных недоступно", request); + } + + @ExceptionHandler({NoResourceFoundException.class, HttpRequestMethodNotSupportedException.class}) + ResponseEntity missing(Exception error, HttpServletRequest request) { + return response(404, "not_found", "Объект не найден", request); + } + + @ExceptionHandler(Exception.class) + ResponseEntity unexpected(Exception error, HttpServletRequest request) { + for (Throwable cause = error; cause != null; cause = cause.getCause()) { + if (cause instanceof DataAccessException) { + LOG.error("request_id={} error_type={}", request.getAttribute(RequestFilter.REQUEST_ID), cause.getClass().getSimpleName()); + return response(503, "unavailable", "Хранилище данных недоступно", request); + } + } + LOG.error("request_id={} error_type={}", request.getAttribute(RequestFilter.REQUEST_ID), + error.getClass().getSimpleName()); + return response(500, "internal_error", "Внутренняя ошибка сервера", request); + } + + private ResponseEntity response(int status, String code, String message, HttpServletRequest request) { + String id = (String) request.getAttribute(RequestFilter.REQUEST_ID); + ResponseEntity.BodyBuilder builder = ResponseEntity.status(status); + if (status == 401) { + String challenge = request.getRequestURI().equals("/api/v1/auth/sessions") + ? "Basic realm=\"messenger\", charset=\"UTF-8\"" + : "Bearer realm=\"messenger\""; + builder.header(HttpHeaders.WWW_AUTHENTICATE, challenge); + } + return builder.body(new ErrorBody(new ErrorDetail(code, message, id))); + } +} diff --git a/src/main/java/ru/mtusi/messenger/ApiException.java b/src/main/java/ru/mtusi/messenger/ApiException.java new file mode 100644 index 00000000..31a0ff05 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/ApiException.java @@ -0,0 +1,24 @@ +package ru.mtusi.messenger; + +public class ApiException extends RuntimeException { + private final int status; + private final String code; + + public ApiException(int status, String code, String message) { + super(message); + this.status = status; + this.code = code; + } + + public int status() { return status; } + public String code() { return code; } + public static ApiException invalid(String message) { + return new ApiException(400, "invalid_request", message); + } + public static ApiException unauthorized() { + return new ApiException(401, "unauthorized", "Требуется действующая Bearer-сессия"); + } + public static ApiException notFound() { + return new ApiException(404, "not_found", "Объект не найден"); + } +} diff --git a/src/main/java/ru/mtusi/messenger/AttachmentImageProcessor.java b/src/main/java/ru/mtusi/messenger/AttachmentImageProcessor.java new file mode 100644 index 00000000..d41e1f50 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/AttachmentImageProcessor.java @@ -0,0 +1,61 @@ +package ru.mtusi.messenger; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import xyz.lychee.webp4j.WebPCodec; + +import static ru.mtusi.messenger.Models.*; + +public final class AttachmentImageProcessor { + private AttachmentImageProcessor() {} + + public static List render(Path source) throws IOException { + BufferedImage image = ImageIO.read(source.toFile()); + if (image == null) throw new ApiException(415, "unsupported_media_type", "Повреждённое изображение"); + List result = new ArrayList<>(); + result.add(renderFit("thumbnail", image, 256, 256, "jpg")); + result.add(renderCrop(image, 128)); + result.add(renderFit("optimized", image, 1280, 1280, "webp")); + return result; + } + + private static Rendered renderFit(String name, BufferedImage source, int maxWidth, int maxHeight, String format) throws IOException { + double scale = Math.min(1.0, Math.min((double) maxWidth / source.getWidth(), (double) maxHeight / source.getHeight())); + int width = Math.max(1, (int) Math.floor(source.getWidth() * scale)); + int height = Math.max(1, (int) Math.floor(source.getHeight() * scale)); + BufferedImage target = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics2D graphics = target.createGraphics(); + graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, width, height); + graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + graphics.drawImage(source, 0, 0, width, height, null); graphics.dispose(); + return encode(name, target, format); + } + + private static Rendered renderCrop(BufferedImage source, int size) throws IOException { + int side = Math.min(source.getWidth(), source.getHeight()); + int x = (source.getWidth() - side) / 2, y = (source.getHeight() - side) / 2; + BufferedImage target = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB); + Graphics2D graphics = target.createGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, size, size); + graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + graphics.drawImage(source, 0, 0, size, size, x, y, x + side, y + side, null); graphics.dispose(); + return encode("crop", target, "jpg"); + } + + private static Rendered encode(String name, BufferedImage image, String format) throws IOException { + if (format.equals("webp")) { + byte[] bytes = WebPCodec.encodeImage(image, 0.82f); + return new Rendered(name, "image/webp", bytes, image.getWidth(), image.getHeight()); + } + ByteArrayOutputStream output = new ByteArrayOutputStream(); + if (!ImageIO.write(image, format, output)) throw new IOException("No ImageIO writer for " + format); + return new Rendered(name, "image/jpeg", output.toByteArray(), image.getWidth(), image.getHeight()); + } + + public record Rendered(String name, String contentType, byte[] bytes, int width, int height) {} +} diff --git a/src/main/java/ru/mtusi/messenger/AttachmentService.java b/src/main/java/ru/mtusi/messenger/AttachmentService.java new file mode 100644 index 00000000..6ea1c827 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/AttachmentService.java @@ -0,0 +1,93 @@ +package ru.mtusi.messenger; + +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; +import org.springframework.transaction.support.TransactionTemplate; +import javax.imageio.ImageIO; +import javax.imageio.ImageReader; +import javax.imageio.stream.ImageInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.MessageDigest; +import java.sql.Timestamp; +import java.time.Instant; +import java.util.Iterator; +import java.util.List; +import java.util.UUID; +import static ru.mtusi.messenger.Models.*; + +@Service +public class AttachmentService { + public static final long MAX_BYTES = 10_485_760L; + public static final long MAX_PIXELS = 40_000_000L; + private final JdbcTemplate jdbc; + private final MessengerStore store; + private final S3Storage s3; + private final TransactionTemplate transaction; + + public AttachmentService(JdbcTemplate jdbc, MessengerStore store, S3Storage s3, + org.springframework.transaction.PlatformTransactionManager manager) { + this.jdbc = jdbc; this.store = store; this.s3 = s3; this.transaction = new TransactionTemplate(manager); + } + + public Attachment upload(UUID actor, UUID chatId, String filename, String contentType, InputStream input) throws IOException { + store.chat(actor, chatId); validateFilename(filename); + String normalizedType = contentType == null ? "" : contentType.split(";", 2)[0].trim().toLowerCase(); + if (!normalizedType.equals("image/png") && !normalizedType.equals("image/jpeg")) throw new ApiException(415, "unsupported_media_type", "Поддерживаются только PNG и JPEG"); + Path temp = Files.createTempFile("messenger-upload-", ".bin"); UUID id = UUID.randomUUID(); String stagingKey = "staging/" + id; boolean uploaded = false; + try { + long size = copyWithLimit(input, temp, MAX_BYTES); String actualType = detectType(temp); + if (!normalizedType.equals(actualType)) throw new ApiException(415, "unsupported_media_type", "MIME не совпадает с содержимым"); + validateImage(temp); String sha256 = sha256(temp); + try (InputStream file = Files.newInputStream(temp)) { s3.put(stagingKey, file, size, actualType); uploaded = true; } + Instant now = Instant.now(); UUID jobId = UUID.randomUUID(); + try { + transaction.executeWithoutResult(status -> { + jdbc.update(""" + INSERT INTO attachments(id, chat_id, owner_id, filename, content_type, size_bytes, sha256, object_key, staging_key, status, error_code, pipeline_version, created_at, updated_at) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'queued', NULL, 1, ?, ?) + """, id, chatId, actor, filename, actualType, size, sha256, "original/" + id, stagingKey, Timestamp.from(now), Timestamp.from(now)); + jdbc.update(""" + INSERT INTO attachment_jobs(job_id, attachment_id, pipeline_version, attempt, status, next_attempt_at, lease_until, error_code, published_at, created_at, updated_at) + VALUES (?, ?, 1, 0, 'queued', ?, NULL, NULL, NULL, ?, ?) + """, jobId, id, Timestamp.from(now), Timestamp.from(now), Timestamp.from(now)); + }); + } catch (RuntimeException exception) { if (uploaded) s3.delete(stagingKey); throw exception; } + return get(actor, id); + } finally { Files.deleteIfExists(temp); } + } + + public Attachment get(UUID actor, UUID id) { + try { + return jdbc.queryForObject(""" + SELECT a.id, a.chat_id, a.owner_id, a.filename, a.content_type, a.size_bytes, a.sha256, a.status, a.error_code, a.created_at, a.updated_at + FROM attachments a JOIN chat_members m ON m.chat_id=a.chat_id WHERE a.id=? AND m.user_id=? + """, (rs, row) -> new Attachment(rs.getObject("id", UUID.class), rs.getObject("chat_id", UUID.class), rs.getObject("owner_id", UUID.class), rs.getString("filename"), rs.getString("content_type"), rs.getLong("size_bytes"), rs.getString("sha256"), rs.getString("status"), loadVariants(rs.getObject("id", UUID.class)), rs.getString("error_code"), rs.getTimestamp("created_at").toInstant(), rs.getTimestamp("updated_at").toInstant()), id, actor); + } catch (EmptyResultDataAccessException exception) { throw ApiException.notFound(); } + } + + public Download download(UUID actor, UUID id, String variant) { + if (!List.of("original", "thumbnail", "crop", "optimized").contains(variant)) throw ApiException.notFound(); + Attachment metadata = get(actor, id); + if (metadata.status().equals("queued") || metadata.status().equals("processing")) throw new ApiException(409, "conflict", "Вложение ещё обрабатывается"); + if (metadata.status().equals("failed")) throw new ApiException(409, "conflict", "Обработка вложения завершилась ошибкой"); + try { + String key; String type; + if (variant.equals("original")) { key = jdbc.queryForObject("SELECT object_key FROM attachments WHERE id=?", String.class, id); type = metadata.contentType(); } + else { Variant v = metadata.variants().stream().filter(x -> x.name().equals(variant)).findFirst().orElseThrow(ApiException::notFound); key = jdbc.queryForObject("SELECT object_key FROM attachment_variants WHERE attachment_id=? AND name=?", String.class, id, variant); type = v.contentType(); } + return new Download(s3.get(key), type); + } catch (EmptyResultDataAccessException exception) { throw ApiException.notFound(); } + } + + private List loadVariants(UUID id) { return jdbc.query("SELECT name, content_type, size_bytes, width, height, sha256 FROM attachment_variants WHERE attachment_id=? ORDER BY name", (rs, row) -> new Variant(rs.getString("name"), rs.getString("content_type"), rs.getLong("size_bytes"), rs.getInt("width"), rs.getInt("height"), rs.getString("sha256")), id); } + private static long copyWithLimit(InputStream input, Path target, long max) throws IOException { long total = 0; byte[] buffer = new byte[8192]; try (var output = Files.newOutputStream(target)) { int read; while ((read = input.read(buffer)) != -1) { total += read; if (total > max) throw new ApiException(413, "payload_too_large", "Файл слишком большой"); output.write(buffer, 0, read); } } if (total == 0) throw new ApiException(400, "invalid_request", "Пустой файл"); return total; } + private static String detectType(Path path) throws IOException { byte[] h = new byte[12]; int n; try (InputStream in = Files.newInputStream(path)) { n = in.read(h); } if (n >= 8 && (h[0] & 255) == 0x89 && h[1] == 0x50 && h[2] == 0x4e && h[3] == 0x47 && h[4] == 0x0d && h[5] == 0x0a && h[6] == 0x1a && h[7] == 0x0a) return "image/png"; if (n >= 3 && (h[0] & 255) == 0xff && (h[1] & 255) == 0xd8 && (h[2] & 255) == 0xff) return "image/jpeg"; throw new ApiException(415, "unsupported_media_type", "Файл не является PNG или JPEG"); } + static Dimensions validateImage(Path path) throws IOException { try (ImageInputStream stream = ImageIO.createImageInputStream(path.toFile())) { Iterator readers = ImageIO.getImageReaders(stream); if (!readers.hasNext()) throw new ApiException(415, "unsupported_media_type", "Повреждённое изображение"); ImageReader reader = readers.next(); try { reader.setInput(stream, true, true); int width = reader.getWidth(0), height = reader.getHeight(0); if ((long) width * height > MAX_PIXELS) throw new ApiException(413, "payload_too_large", "Слишком много пикселей"); if (ImageIO.read(path.toFile()) == null) throw new ApiException(415, "unsupported_media_type", "Повреждённое изображение"); return new Dimensions(width, height); } finally { reader.dispose(); } } } + static String sha256(Path path) throws IOException { try (InputStream input = Files.newInputStream(path)) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] buffer = new byte[8192]; int n; while ((n = input.read(buffer)) != -1) digest.update(buffer, 0, n); StringBuilder result = new StringBuilder(); for (byte value : digest.digest()) result.append(String.format("%02x", value)); return result.toString(); } catch (java.security.NoSuchAlgorithmException exception) { throw new IllegalStateException(exception); } } } + private static void validateFilename(String value) { if (value == null || value.isEmpty() || value.codePointCount(0, value.length()) > 255 || value.indexOf('/') >= 0 || value.indexOf('\\') >= 0 || value.indexOf('\0') >= 0 || value.codePoints().anyMatch(cp -> cp < 32 || cp == 127)) throw ApiException.invalid("Некорректное имя файла"); } + record Dimensions(int width, int height) {} + public record Download(InputStream stream, String contentType) {} +} diff --git a/src/main/java/ru/mtusi/messenger/AttachmentWorker.java b/src/main/java/ru/mtusi/messenger/AttachmentWorker.java new file mode 100644 index 00000000..24fdb326 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/AttachmentWorker.java @@ -0,0 +1,119 @@ +package ru.mtusi.messenger; + +import jakarta.annotation.PostConstruct; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.data.redis.connection.stream.MapRecord; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.sql.Timestamp; +import java.time.Instant; +import java.util.List; +import java.util.UUID; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Component +@ConditionalOnProperty(name = "WORKER_ONLY", havingValue = "true") +public class AttachmentWorker { + private static final Logger LOG = LoggerFactory.getLogger(AttachmentWorker.class); + private final JdbcTemplate jdbc; + private final JobBroker broker; + private final S3Storage s3; + private final String consumer = "worker-" + UUID.randomUUID(); + + public AttachmentWorker(JdbcTemplate jdbc, JobBroker broker, S3Storage s3) { this.jdbc = jdbc; this.broker = broker; this.s3 = s3; } + + @PostConstruct + void start() { LOG.info("attachment worker started consumer={}", consumer); } + + @Scheduled(fixedDelayString = "${WORKER_POLL_MS:500}", initialDelayString = "${WORKER_POLL_INITIAL_DELAY_MS:500}") + public void poll() { + try { + MapRecord record = broker.read(consumer); + if (record != null) { + UUID jobId = UUID.fromString(String.valueOf(record.getValue().get("job_id"))); + LOG.info("attachment job received job_id={}", jobId); + try { process(jobId); } finally { broker.ack(record.getId()); } + } else { + processQueuedFallback(); + } + recoverLeases(); + } catch (Exception exception) { + LOG.warn("worker poll error", exception); + } + } + + private void processQueuedFallback() { + List jobs = jdbc.query("SELECT job_id FROM attachment_jobs WHERE status='queued' AND next_attempt_at <= now() ORDER BY created_at LIMIT 1", (rs, row) -> rs.getObject("job_id", UUID.class)); + if (!jobs.isEmpty()) { LOG.info("processing queued fallback job_id={}", jobs.getFirst()); process(jobs.getFirst()); } + } + + @Scheduled(fixedDelayString = "${WORKER_LEASE_SCAN_MS:1000}") + public void recoverLeases() { + jdbc.update("UPDATE attachment_jobs SET status='queued', published_at=NULL, lease_until=NULL, updated_at=now() WHERE status='processing' AND lease_until < now() AND attempt < 5"); + } + + private void process(UUID jobId) { + Job job = claim(jobId); + if (job == null) return; + LOG.info("processing attachment job_id={} attempt={}", job.jobId(), job.attempt()); + Path temp = null; + try { + temp = Files.createTempFile("messenger-worker-", ".bin"); + try (InputStream input = s3.get(job.stagingKey()); var output = Files.newOutputStream(temp)) { input.transferTo(output); } + AttachmentService.Dimensions dimensions = AttachmentService.validateImage(temp); + List variants = AttachmentImageProcessor.render(temp); + try (InputStream original = Files.newInputStream(temp)) { + s3.put(job.originalKey(), original, Files.size(temp), job.contentType()); + } + for (AttachmentImageProcessor.Rendered variant : variants) { + String key = "variants/" + job.attachmentId() + "/" + variant.name(); + s3.put(key, new ByteArrayInputStream(variant.bytes()), variant.bytes().length, variant.contentType()); + } + Instant now = Instant.now(); + for (AttachmentImageProcessor.Rendered variant : variants) { + String key = "variants/" + job.attachmentId() + "/" + variant.name(); + jdbc.update(""" + INSERT INTO attachment_variants(attachment_id, name, content_type, size_bytes, width, height, sha256, object_key) + VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT (attachment_id, name) DO NOTHING + """, job.attachmentId(), variant.name(), variant.contentType(), variant.bytes().length, + variant.width(), variant.height(), sha256(variant.bytes()), key); + } + jdbc.update("UPDATE attachments SET status='ready', error_code=NULL, updated_at=? WHERE id=?", Timestamp.from(now), job.attachmentId()); + jdbc.update("UPDATE attachment_jobs SET status='ready', lease_until=NULL, updated_at=? WHERE job_id=?", Timestamp.from(now), job.jobId()); + s3.delete(job.stagingKey()); + LOG.info("attachment ready job_id={} attachment_id={}", job.jobId(), job.attachmentId()); + } catch (Exception exception) { + LOG.warn("attachment processing failed job_id={} attempt={}", job.jobId(), job.attempt(), exception); + failOrRetry(job, exception); + } finally { if (temp != null) try { Files.deleteIfExists(temp); } catch (Exception ignored) {} LOG.info("attachment job finished job_id={}", job.jobId()); } + } + + private Job claim(UUID jobId) { + int changed = jdbc.update("UPDATE attachment_jobs SET status='processing', attempt=attempt+1, lease_until=now()+interval '30 seconds', updated_at=now() WHERE job_id=? AND (status='queued' OR (status='processing' AND lease_until < now())) AND attempt < 5", jobId); + if (changed == 0) return null; + return jdbc.queryForObject("SELECT j.job_id, j.attachment_id, a.staging_key, a.object_key, a.content_type, j.attempt FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE j.job_id=?", (rs, row) -> new Job(rs.getObject("job_id", UUID.class), rs.getObject("attachment_id", UUID.class), rs.getString("staging_key"), rs.getString("object_key"), rs.getString("content_type"), rs.getInt("attempt")), jobId); + } + + private void failOrRetry(Job job, Exception exception) { + boolean permanent = exception instanceof ApiException api && (api.status() == 413 || api.status() == 415); + if (permanent || job.attempt() >= 5) { + String code = permanent ? "invalid_image" : "retry_exhausted"; + jdbc.update("UPDATE attachments SET status='failed', error_code=?, updated_at=now() WHERE id=?", code, job.attachmentId()); + jdbc.update("UPDATE attachment_jobs SET status='failed', error_code=?, lease_until=NULL, updated_at=now() WHERE job_id=?", code, job.jobId()); + } else { + long delay = Math.min(5000L, 200L * (1L << Math.min(job.attempt(), 4))) + (long) (Math.random() * 300); + jdbc.update("UPDATE attachment_jobs SET status='queued', published_at=NULL, lease_until=NULL, next_attempt_at=now() + (? * interval '1 millisecond'), error_code=?, updated_at=now() WHERE job_id=?", delay, "temporary_error", job.jobId()); + jdbc.update("UPDATE attachments SET status='queued', error_code=NULL, updated_at=now() WHERE id=?", job.attachmentId()); + } + } + + private static String sha256(byte[] bytes) throws Exception { var digest = java.security.MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(bytes); StringBuilder s = new StringBuilder(); for (byte b : hash) s.append(String.format("%02x", b)); return s.toString(); } + private record Job(UUID jobId, UUID attachmentId, String stagingKey, String originalKey, String contentType, int attempt) {} + +} diff --git a/src/main/java/ru/mtusi/messenger/CursorCodec.java b/src/main/java/ru/mtusi/messenger/CursorCodec.java new file mode 100644 index 00000000..ff2bbf20 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/CursorCodec.java @@ -0,0 +1,66 @@ +package ru.mtusi.messenger; + +import org.springframework.stereotype.Component; +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.time.Instant; +import java.util.Arrays; +import java.util.Base64; +import java.util.UUID; + +@Component +public class CursorCodec { + private final byte[] secret; + private static final Base64.Encoder ENCODER = Base64.getUrlEncoder().withoutPadding(); + private static final Base64.Decoder DECODER = Base64.getUrlDecoder(); + + public CursorCodec() { + String configured = System.getenv("CURSOR_SECRET"); + if (configured == null || configured.isBlank()) { + throw new IllegalStateException("CURSOR_SECRET must be configured for all API replicas"); + } + try { + byte[] decoded = DECODER.decode(configured); + if (decoded.length < 32) throw new IllegalArgumentException("secret is too short"); + this.secret = Arrays.copyOf(decoded, 32); + } catch (IllegalArgumentException exception) { + throw new IllegalStateException("CURSOR_SECRET must be URL-safe base64 with at least 32 bytes", exception); + } + } + + public record Position(Instant createdAt, UUID id) {} + + public String encode(String scope, Position position) { + String payload = scope + "|" + position.createdAt() + "|" + position.id(); + String encoded = ENCODER.encodeToString(payload.getBytes(StandardCharsets.UTF_8)); + return encoded + "." + ENCODER.encodeToString(sign(encoded)); + } + + public Position decode(String cursor, String scope) { + if (cursor == null) return null; + try { + if (cursor.length() > 1024) throw new IllegalArgumentException(); + String[] parts = cursor.split("\\.", -1); + if (parts.length != 2 || !MessageDigest.isEqual(sign(parts[0]), DECODER.decode(parts[1]))) { + throw new IllegalArgumentException(); + } + String[] fields = new String(DECODER.decode(parts[0]), StandardCharsets.UTF_8).split("\\|", -1); + if (fields.length != 3 || !scope.equals(fields[0])) throw new IllegalArgumentException(); + return new Position(Instant.parse(fields[1]), Input.uuid(fields[2])); + } catch (RuntimeException exception) { + throw ApiException.invalid("Некорректный cursor или cursor от другого ресурса"); + } + } + + private byte[] sign(String payload) { + try { + Mac mac = Mac.getInstance("HmacSHA256"); + mac.init(new SecretKeySpec(secret, "HmacSHA256")); + return mac.doFinal(payload.getBytes(StandardCharsets.UTF_8)); + } catch (java.security.GeneralSecurityException exception) { + throw new IllegalStateException("HMAC is unavailable", exception); + } + } +} diff --git a/src/main/java/ru/mtusi/messenger/HealthController.java b/src/main/java/ru/mtusi/messenger/HealthController.java new file mode 100644 index 00000000..7a51dcb4 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/HealthController.java @@ -0,0 +1,39 @@ +package ru.mtusi.messenger; + +import org.springframework.boot.availability.ApplicationAvailability; +import org.springframework.boot.availability.ReadinessState; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; + +@RestController +public class HealthController { + private final ApplicationAvailability availability; + private final JdbcTemplate jdbc; + private final SessionService sessions; + private final S3Storage s3; + + public HealthController(ApplicationAvailability availability, JdbcTemplate jdbc, SessionService sessions, S3Storage s3) { + this.availability = availability; this.jdbc = jdbc; this.sessions = sessions; this.s3 = s3; + } + + @GetMapping("/health/live") + Map live() { return Map.of("status", "ok"); } + + @GetMapping("/health/ready") + Map ready() { + if (availability.getReadinessState() != ReadinessState.ACCEPTING_TRAFFIC) throw unavailable(); + try { + jdbc.queryForObject("SELECT 1", Integer.class); + sessions.check(); + s3.check(); + return Map.of("status", "ok"); + } catch (RuntimeException exception) { + throw unavailable(); + } + } + + private ApiException unavailable() { return new ApiException(503, "unavailable", "Зависимость недоступна"); } +} diff --git a/src/main/java/ru/mtusi/messenger/Input.java b/src/main/java/ru/mtusi/messenger/Input.java new file mode 100644 index 00000000..328fe4fe --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/Input.java @@ -0,0 +1,125 @@ +package ru.mtusi.messenger; + +import com.fasterxml.jackson.databind.JsonNode; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.UUID; + +final class Input { + private Input() {} + + static void fields(JsonNode body, String... allowed) { + Set names = Set.of(allowed); + if (body == null || !body.isObject() || body.size() != names.size()) { + throw ApiException.invalid("Неверный набор полей JSON"); + } + body.fieldNames().forEachRemaining(name -> { + if (!names.contains(name)) throw ApiException.invalid("Неизвестное поле JSON"); + }); + } + + static String text(JsonNode body, String field, int max) { + JsonNode node = body.get(field); + if (node == null || !node.isTextual()) throw ApiException.invalid("Поле должно быть строкой: " + field); + String value = node.textValue(); + if (value.codePointCount(0, value.length()) > max || value.codePoints().allMatch(Input::isUnicodeWhitespace)) { + throw ApiException.invalid("Недопустимая длина или пустая строка: " + field); + } + return value; + } + + static String password(JsonNode body) { + JsonNode node = body.get("password"); + if (node == null || !node.isTextual()) throw ApiException.invalid("Пароль должен быть строкой"); + String value = node.textValue(); + int length = value.codePointCount(0, value.length()); + if (length < 12 || length > 128 || value.codePoints().allMatch(Input::isUnicodeWhitespace)) { + throw ApiException.invalid("Пароль должен содержать от 12 до 128 непробельных символов"); + } + return value; + } + + private static boolean isUnicodeWhitespace(int codePoint) { + return codePoint == 0x0085 + || Character.isWhitespace(codePoint) + || Character.isSpaceChar(codePoint); + } + + static UUID uuid(String value) { + if (value == null || !value.matches("(?i)[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) { + throw ApiException.invalid("Некорректный UUID"); + } + return UUID.fromString(value); + } + + static List members(JsonNode body) { + JsonNode array = body.get("member_ids"); + if (array == null || !array.isArray() || array.isEmpty() || array.size() > 100) { + throw ApiException.invalid("member_ids должен содержать от 1 до 100 участников"); + } + List ids = new ArrayList<>(); + for (JsonNode item : array) { + if (!item.isTextual()) throw ApiException.invalid("Участник должен быть UUID-строкой"); + ids.add(uuid(item.textValue())); + } + if (new HashSet<>(ids).size() != ids.size()) throw ApiException.invalid("Участники повторяются"); + return ids; + } + + static int limit(String value) { + if (value == null) return 50; + try { + if (!value.matches("[0-9]{1,3}")) throw new NumberFormatException(); + int limit = Integer.parseInt(value); + if (limit < 1 || limit > 100) throw new NumberFormatException(); + return limit; + } catch (NumberFormatException exception) { + throw ApiException.invalid("limit должен быть целым числом от 1 до 100"); + } + } + + static String idempotencyKey(String value) { + if (value == null || !value.matches("[A-Za-z0-9._:-]{1,128}")) { + throw ApiException.invalid("Некорректный Idempotency-Key"); + } + return value; + } + + static void messageFields(JsonNode body) { + if (body == null || !body.isObject() || !body.has("text") || body.size() > 2 + || (body.has("attachment_ids") && !body.get("attachment_ids").isArray())) { + throw ApiException.invalid("Неверный набор полей JSON сообщения"); + } + body.fieldNames().forEachRemaining(name -> { + if (!name.equals("text") && !name.equals("attachment_ids")) { + throw ApiException.invalid("Неизвестное поле JSON"); + } + }); + } + + static List attachmentIds(JsonNode body) { + JsonNode array = body.get("attachment_ids"); + if (array == null) return List.of(); + if (!array.isArray() || array.size() > 10) throw ApiException.invalid("Слишком много вложений"); + List ids = new ArrayList<>(); + for (JsonNode item : array) { + if (!item.isTextual()) throw ApiException.invalid("attachment_ids должны быть UUID-строками"); + ids.add(uuid(item.textValue())); + } + if (new HashSet<>(ids).size() != ids.size()) throw ApiException.invalid("Вложения повторяются"); + return ids; + } + + static String messageText(JsonNode body, boolean hasAttachments) { + JsonNode node = body.get("text"); + if (node == null || !node.isTextual()) throw ApiException.invalid("Поле должно быть строкой: text"); + String value = node.textValue(); + if (value.codePointCount(0, value.length()) > 4000 + || (!hasAttachments && (value.isEmpty() || value.codePoints().allMatch(Input::isUnicodeWhitespace)))) { + throw ApiException.invalid("Сообщение должно содержать текст или вложение"); + } + return value; + } +} diff --git a/src/main/java/ru/mtusi/messenger/JobBroker.java b/src/main/java/ru/mtusi/messenger/JobBroker.java new file mode 100644 index 00000000..5ef4ecd5 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/JobBroker.java @@ -0,0 +1,46 @@ +package ru.mtusi.messenger; + +import jakarta.annotation.PostConstruct; +import org.springframework.data.redis.connection.stream.Consumer; +import org.springframework.data.redis.connection.stream.MapRecord; +import org.springframework.data.redis.connection.stream.ReadOffset; +import org.springframework.data.redis.connection.stream.RecordId; +import org.springframework.data.redis.connection.stream.StreamRecords; +import org.springframework.data.redis.connection.stream.StreamOffset; +import org.springframework.data.redis.connection.stream.StreamReadOptions; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Component; +import java.time.Duration; +import java.util.List; +import java.util.Map; + +@Component +public class JobBroker { + public static final String STREAM = "messenger:attachment-jobs"; + public static final String GROUP = "attachment-workers"; + private final StringRedisTemplate redis; + + public JobBroker(StringRedisTemplate redis) { this.redis = redis; } + + @PostConstruct + void ensureGroup() { + try { redis.opsForStream().createGroup(STREAM, ReadOffset.from("0-0"), GROUP); } + catch (RuntimeException ignored) { /* BUSYGROUP means it already exists. */ } + } + + public void publish(String jobId) { + MapRecord record = StreamRecords.newRecord() + .in(STREAM).ofMap(Map.of("job_id", jobId, "schema_version", "1", "type", "attachment.process.v1")); + redis.opsForStream().add(record); + } + + public MapRecord read(String consumer) { + List> records = redis.opsForStream().read( + Consumer.from(GROUP, consumer), + StreamReadOptions.empty().count(1).block(Duration.ofSeconds(1)), + StreamOffset.create(STREAM, ReadOffset.lastConsumed())); + return records == null || records.isEmpty() ? null : records.getFirst(); + } + + public void ack(RecordId id) { redis.opsForStream().acknowledge(STREAM, GROUP, id); } +} diff --git a/src/main/java/ru/mtusi/messenger/MessengerApplication.java b/src/main/java/ru/mtusi/messenger/MessengerApplication.java new file mode 100644 index 00000000..65a17a2d --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/MessengerApplication.java @@ -0,0 +1,26 @@ +package ru.mtusi.messenger; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.context.ApplicationContext; +import org.springframework.core.env.Environment; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; + +@SpringBootApplication +@EnableScheduling +public class MessengerApplication { + public static void main(String[] args) { + SpringApplication.run(MessengerApplication.class, args); + } + + @org.springframework.context.annotation.Bean + CommandLineRunner migrationOnly(ApplicationContext context, Environment environment) { + return args -> { + if (environment.getProperty("MIGRATION_ONLY", Boolean.class, false)) { + int exitCode = SpringApplication.exit(context, () -> 0); + System.exit(exitCode); + } + }; + } +} diff --git a/src/main/java/ru/mtusi/messenger/MessengerController.java b/src/main/java/ru/mtusi/messenger/MessengerController.java new file mode 100644 index 00000000..bccb648b --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/MessengerController.java @@ -0,0 +1,130 @@ +package ru.mtusi.messenger; + +import com.fasterxml.jackson.databind.JsonNode; +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; + +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.UUID; +import java.util.List; +import org.springframework.core.io.InputStreamResource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody; + +import static ru.mtusi.messenger.Models.*; + +@RestController +@RequestMapping("/api/v1") +public class MessengerController { + private final MessengerStore store; + private final SessionService sessions; + private final AttachmentService attachments; + + public MessengerController(MessengerStore store, SessionService sessions, AttachmentService attachments) { + this.store = store; this.sessions = sessions; this.attachments = attachments; + } + + @PostMapping(value = "/users", consumes = "application/json") + @ResponseStatus(HttpStatus.CREATED) + User register(@RequestBody JsonNode body) { + Input.fields(body, "username", "display_name", "password"); + String username = Input.text(body, "username", 32); + if (!username.matches("[a-z0-9_]{3,32}")) throw ApiException.invalid("Некорректный username"); + return store.createUser(username, Input.text(body, "display_name", 100), sessions.hash(Input.password(body))); + } + + @PostMapping(value = "/auth/sessions", consumes = "*/*") + @ResponseStatus(HttpStatus.CREATED) + Session login(HttpServletRequest request) throws java.io.IOException { + if (request.getInputStream().read() != -1) { + throw ApiException.invalid("Вход не принимает тело запроса"); + } + String header = request.getHeader("Authorization"); + if (header == null || !header.startsWith("Basic ")) throw ApiException.unauthorized(); + try { + String decoded = new String(Base64.getDecoder().decode(header.substring(6)), StandardCharsets.UTF_8); + int colon = decoded.indexOf(':'); + if (colon <= 0) throw new IllegalArgumentException(); + String username = decoded.substring(0, colon); + String password = decoded.substring(colon + 1); + if (!username.matches("[a-z0-9_]{3,32}") || password.codePointCount(0, password.length()) < 12 + || password.codePointCount(0, password.length()) > 128) throw new IllegalArgumentException(); + return sessions.login(username, password); + } catch (IllegalArgumentException exception) { + throw ApiException.unauthorized(); + } + } + + @DeleteMapping("/auth/sessions/current") + @ResponseStatus(HttpStatus.NO_CONTENT) + void logout(@RequestAttribute(RequestFilter.TOKEN) String token) { sessions.revoke(token); } + + @GetMapping("/users/me") + User me(@RequestAttribute(RequestFilter.ACTOR) User actor) { return actor; } + + @PostMapping(value = "/chats", consumes = "application/json") + @ResponseStatus(HttpStatus.CREATED) + Chat createChat(@RequestAttribute(RequestFilter.ACTOR) User actor, @RequestBody JsonNode body) { + Input.fields(body, "title", "member_ids"); + return store.createChat(actor.id(), Input.text(body, "title", 100), Input.members(body)); + } + + @GetMapping("/chats") + Page chats(@RequestAttribute(RequestFilter.ACTOR) User actor, + @RequestParam(required = false) String limit, @RequestParam(required = false) String cursor) { + return store.chats(actor.id(), Input.limit(limit), cursor); + } + + @GetMapping("/chats/{chatId}") + Chat chat(@RequestAttribute(RequestFilter.ACTOR) User actor, @PathVariable String chatId) { + return store.chat(actor.id(), Input.uuid(chatId)); + } + + @PostMapping(value = "/chats/{chatId}/messages", consumes = "application/json") + @ResponseStatus(HttpStatus.CREATED) + Message send(@RequestAttribute(RequestFilter.ACTOR) User actor, @PathVariable String chatId, + @RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey, + HttpServletRequest request, @RequestBody JsonNode body) { + UUID id = Input.uuid(chatId); + store.chat(actor.id(), id); + String key = Input.idempotencyKey(idempotencyKey); + Input.messageFields(body); + List attachmentIds = Input.attachmentIds(body); + String text = Input.messageText(body, !attachmentIds.isEmpty()); + return store.sendIdempotent(actor.id(), id, text, attachmentIds, request.getMethod(), request.getRequestURI(), key); + } + + @GetMapping("/chats/{chatId}/messages") + Page history(@RequestAttribute(RequestFilter.ACTOR) User actor, @PathVariable String chatId, + @RequestParam(required = false) String limit, @RequestParam(required = false) String cursor) { + UUID id = Input.uuid(chatId); + return store.history(actor.id(), id, Input.limit(limit), cursor); + } + + @PostMapping(value = "/chats/{chatId}/attachments", consumes = {"image/png", "image/jpeg"}) + @ResponseStatus(HttpStatus.ACCEPTED) + Attachment upload(@RequestAttribute(RequestFilter.ACTOR) User actor, @PathVariable String chatId, + @RequestParam String filename, HttpServletRequest request) throws java.io.IOException { + return attachments.upload(actor.id(), Input.uuid(chatId), filename, request.getContentType(), request.getInputStream()); + } + + @GetMapping("/attachments/{attachmentId}") + Attachment attachment(@RequestAttribute(RequestFilter.ACTOR) User actor, @PathVariable String attachmentId) { + return attachments.get(actor.id(), Input.uuid(attachmentId)); + } + + @GetMapping("/attachments/{attachmentId}/content") + ResponseEntity content(@RequestAttribute(RequestFilter.ACTOR) User actor, + @PathVariable String attachmentId, + @RequestParam(defaultValue = "original") String variant) { + AttachmentService.Download download = attachments.download(actor.id(), Input.uuid(attachmentId), variant); + StreamingResponseBody body = output -> { + try (java.io.InputStream input = download.stream()) { input.transferTo(output); } + }; + return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, download.contentType()).body(body); + } +} diff --git a/src/main/java/ru/mtusi/messenger/MessengerStore.java b/src/main/java/ru/mtusi/messenger/MessengerStore.java new file mode 100644 index 00000000..e2f1c697 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/MessengerStore.java @@ -0,0 +1,215 @@ +package ru.mtusi.messenger; + +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.sql.Timestamp; +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import static ru.mtusi.messenger.Models.*; + +@Service +public class MessengerStore { + private final JdbcTemplate jdbc; + private final CursorCodec cursors; + + public MessengerStore(JdbcTemplate jdbc, CursorCodec cursors) { this.jdbc = jdbc; this.cursors = cursors; } + + @Transactional + public User createUser(String username, String displayName, String passwordHash) { + UUID id = UUID.randomUUID(); + Instant created = Instant.now(); + try { + jdbc.update("INSERT INTO users(id, username, display_name, password_hash, created_at) VALUES (?, ?, ?, ?, ?)", + id, username, displayName, passwordHash, Timestamp.from(created)); + } catch (DuplicateKeyException exception) { + throw new ApiException(409, "conflict", "username уже занят"); + } + return findUser(id); + } + + public Credentials credentials(String username) { + try { + return jdbc.queryForObject("SELECT id, username, display_name, created_at, password_hash FROM users WHERE username = ?", + (rs, row) -> new Credentials(new User(rs.getObject("id", UUID.class), rs.getString("username"), + rs.getString("display_name"), rs.getTimestamp("created_at").toInstant()), + rs.getString("password_hash")), username); + } catch (EmptyResultDataAccessException exception) { return null; } + } + + public User findUser(UUID id) { + try { + return jdbc.queryForObject("SELECT id, username, display_name, created_at FROM users WHERE id = ?", + (rs, row) -> user(rs), id); + } catch (EmptyResultDataAccessException exception) { return null; } + } + + @Transactional + public Chat createChat(UUID actor, String title, List memberIds) { + if (!memberIds.contains(actor)) throw ApiException.invalid("Участники должны существовать и включать создателя"); + for (UUID member : memberIds) { + if (findUser(member) == null) throw ApiException.invalid("Участники должны существовать и включать создателя"); + } + UUID id = UUID.randomUUID(); + Instant created = Instant.now(); + jdbc.update("INSERT INTO chats(id, title, created_at) VALUES (?, ?, ?)", id, title, Timestamp.from(created)); + for (UUID member : memberIds) jdbc.update("INSERT INTO chat_members(chat_id, user_id) VALUES (?, ?)", id, member); + return chat(actor, id); + } + + public Chat chat(UUID actor, UUID id) { + try { + Chat chat = jdbc.queryForObject("SELECT c.id, c.title, c.created_at FROM chats c JOIN chat_members m ON m.chat_id=c.id WHERE c.id=? AND m.user_id=?", + (rs, row) -> new Chat(rs.getObject("id", UUID.class), rs.getString("title"), + loadMembers(rs.getObject("id", UUID.class)), rs.getTimestamp("created_at").toInstant()), id, actor); + if (chat == null) throw ApiException.notFound(); + return chat; + } catch (EmptyResultDataAccessException exception) { throw ApiException.notFound(); } + } + + @Transactional + public Message send(UUID actor, UUID chatId, String text) { + chat(actor, chatId); + UUID id = UUID.randomUUID(); + Instant created = Instant.now(); + jdbc.update("INSERT INTO messages(id, chat_id, sender_id, text, created_at) VALUES (?, ?, ?, ?, ?)", + id, chatId, actor, text, Timestamp.from(created)); + return findMessage(id); + } + + @Transactional + public Message sendIdempotent(UUID actor, UUID chatId, String text, + List attachmentIds, + String method, String requestPath, String idempotencyKey) { + chat(actor, chatId); + verifyAttachments(actor, chatId, attachmentIds); + String requestHash = hashRequest(text, attachmentIds); + Instant created = Instant.now(); + int inserted = jdbc.update(""" + INSERT INTO message_idempotency(actor_id, method, request_path, idempotency_key, + request_hash, message_id, created_at) + VALUES (?, ?, ?, ?, ?, NULL, ?) + ON CONFLICT (actor_id, method, request_path, idempotency_key) DO NOTHING + """, actor, method, requestPath, idempotencyKey, requestHash, Timestamp.from(created)); + + if (inserted == 0) { + IdempotencyRecord previous = jdbc.queryForObject(""" + SELECT request_hash, message_id + FROM message_idempotency + WHERE actor_id=? AND method=? AND request_path=? AND idempotency_key=? + """, (rs, row) -> new IdempotencyRecord(rs.getString("request_hash"), + rs.getObject("message_id", UUID.class)), actor, method, requestPath, idempotencyKey); + if (!MessageDigest.isEqual(previous.requestHash().getBytes(StandardCharsets.US_ASCII), + requestHash.getBytes(StandardCharsets.US_ASCII))) { + throw new ApiException(409, "conflict", "Idempotency-Key уже использован для другого сообщения"); + } + if (previous.messageId() == null) { + throw new ApiException(503, "unavailable", "Результат идемпотентного запроса ещё не готов"); + } + return findMessage(previous.messageId()); + } + + UUID id = UUID.randomUUID(); + jdbc.update("INSERT INTO messages(id, chat_id, sender_id, text, created_at) VALUES (?, ?, ?, ?, ?)", + id, chatId, actor, text, Timestamp.from(created)); + for (UUID attachmentId : attachmentIds) { + jdbc.update("INSERT INTO message_attachments(message_id, attachment_id) VALUES (?, ?)", id, attachmentId); + } + jdbc.update(""" + UPDATE message_idempotency + SET message_id=? + WHERE actor_id=? AND method=? AND request_path=? AND idempotency_key=? + """, id, actor, method, requestPath, idempotencyKey); + return findMessage(id); + } + + public Page chats(UUID actor, int limit, String cursor) { + CursorCodec.Position after = cursors.decode(cursor, "chats:" + actor); + String sql = "SELECT c.id, c.title, c.created_at FROM chats c JOIN chat_members m ON m.chat_id=c.id WHERE m.user_id=?"; + List args = new ArrayList<>(List.of(actor)); + if (after != null) { sql += " AND (c.created_at, c.id) > (?, ?)"; args.add(Timestamp.from(after.createdAt())); args.add(after.id()); } + sql += " ORDER BY c.created_at, c.id LIMIT ?"; + args.add(limit + 1); + List rows = jdbc.query(sql, (rs, row) -> { + UUID id = rs.getObject("id", UUID.class); + return new Chat(id, rs.getString("title"), loadMembers(id), rs.getTimestamp("created_at").toInstant()); + }, args.toArray()); + return page(rows, limit, "chats:" + actor, c -> new CursorCodec.Position(c.createdAt(), c.id())); + } + + public Page history(UUID actor, UUID chatId, int limit, String cursor) { + chat(actor, chatId); + CursorCodec.Position after = cursors.decode(cursor, "messages:" + chatId); + String sql = "SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id=?"; + List args = new ArrayList<>(List.of(chatId)); + if (after != null) { sql += " AND (created_at, id) > (?, ?)"; args.add(Timestamp.from(after.createdAt())); args.add(after.id()); } + sql += " ORDER BY created_at, id LIMIT ?"; + args.add(limit + 1); + List rows = jdbc.query(sql, (rs, row) -> new Message(rs.getObject("id", UUID.class), + rs.getObject("chat_id", UUID.class), rs.getObject("sender_id", UUID.class), rs.getString("text"), + rs.getTimestamp("created_at").toInstant(), loadAttachmentIds(rs.getObject("id", UUID.class))), args.toArray()); + return page(rows, limit, "messages:" + chatId, m -> new CursorCodec.Position(m.createdAt(), m.id())); + } + + private List loadMembers(UUID chatId) { + return jdbc.query("SELECT user_id FROM chat_members WHERE chat_id=? ORDER BY user_id", + (rs, row) -> rs.getObject("user_id", UUID.class), chatId); + } + + private Message findMessage(UUID id) { + try { + return jdbc.queryForObject("SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE id=?", + (rs, row) -> new Message(rs.getObject("id", UUID.class), rs.getObject("chat_id", UUID.class), + rs.getObject("sender_id", UUID.class), rs.getString("text"), rs.getTimestamp("created_at").toInstant(), + loadAttachmentIds(rs.getObject("id", UUID.class))), id); + } catch (EmptyResultDataAccessException exception) { + throw new ApiException(500, "internal_error", "Созданное сообщение не найдено"); + } + } + + private String hashRequest(String text, List attachmentIds) { + try { + byte[] digest = MessageDigest.getInstance("SHA-256") + .digest((text + "|" + attachmentIds.stream().map(UUID::toString).sorted().toList()).getBytes(StandardCharsets.UTF_8)); + StringBuilder result = new StringBuilder(digest.length * 2); + for (byte value : digest) result.append(String.format("%02x", value)); + return result.toString(); + } catch (java.security.NoSuchAlgorithmException exception) { + throw new IllegalStateException("SHA-256 is unavailable", exception); + } + } + + private void verifyAttachments(UUID actor, UUID chatId, List ids) { + for (UUID id : ids) { + Integer count = jdbc.queryForObject("SELECT count(*) FROM attachments a JOIN chat_members m ON m.chat_id=a.chat_id WHERE a.id=? AND a.chat_id=? AND m.user_id=? AND a.status <> 'failed'", Integer.class, id, chatId, actor); + if (count == null || count != 1) throw ApiException.notFound(); + } + } + + private List loadAttachmentIds(UUID messageId) { + return jdbc.query("SELECT attachment_id FROM message_attachments WHERE message_id=? ORDER BY attachment_id", + (rs, row) -> rs.getObject("attachment_id", UUID.class), messageId); + } + + private record IdempotencyRecord(String requestHash, UUID messageId) {} + + private User user(java.sql.ResultSet rs) throws java.sql.SQLException { + return new User(rs.getObject("id", UUID.class), rs.getString("username"), rs.getString("display_name"), + rs.getTimestamp("created_at").toInstant()); + } + + private Page page(List rows, int limit, String scope, java.util.function.Function position) { + boolean more = rows.size() > limit; + List result = more ? rows.subList(0, limit) : rows; + String next = more ? cursors.encode(scope, position.apply(result.getLast())) : null; + return new Page<>(result, next); + } +} diff --git a/src/main/java/ru/mtusi/messenger/Models.java b/src/main/java/ru/mtusi/messenger/Models.java new file mode 100644 index 00000000..af4e91c2 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/Models.java @@ -0,0 +1,31 @@ +package ru.mtusi.messenger; + +import java.time.Instant; +import java.util.List; +import java.util.UUID; + +public final class Models { + private Models() {} + + public record User(UUID id, String username, String displayName, Instant createdAt) {} + public record Chat(UUID id, String title, List memberIds, Instant createdAt) { + public Chat { memberIds = List.copyOf(memberIds); } + } + public record Message(UUID id, UUID chatId, UUID senderId, String text, Instant createdAt, + List attachmentIds) { + public Message { attachmentIds = List.copyOf(attachmentIds); } + } + public record Attachment(UUID id, UUID chatId, UUID ownerId, String filename, String contentType, + long sizeBytes, String sha256, String status, List variants, + String errorCode, Instant createdAt, Instant updatedAt) { + public Attachment { variants = List.copyOf(variants); } + } + public record Variant(String name, String contentType, long sizeBytes, int width, int height, String sha256) {} + public record Credentials(User user, String passwordHash) {} + public record Session(String token, String tokenType, Instant expiresAt, User user) {} + public record Page(List items, String nextCursor) { + public Page { items = List.copyOf(items); } + } + public record ErrorBody(ErrorDetail error) {} + public record ErrorDetail(String code, String message, String requestId) {} +} diff --git a/src/main/java/ru/mtusi/messenger/OutboxPublisher.java b/src/main/java/ru/mtusi/messenger/OutboxPublisher.java new file mode 100644 index 00000000..62e149ca --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/OutboxPublisher.java @@ -0,0 +1,31 @@ +package ru.mtusi.messenger; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import java.sql.Timestamp; +import java.time.Instant; +import java.util.List; +import java.util.UUID; + +@Component +@ConditionalOnProperty(name = "WORKER_ONLY", havingValue = "false", matchIfMissing = true) +public class OutboxPublisher { + private final JdbcTemplate jdbc; + private final JobBroker broker; + public OutboxPublisher(JdbcTemplate jdbc, JobBroker broker) { this.jdbc = jdbc; this.broker = broker; } + + @Scheduled(fixedDelayString = "${OUTBOX_PUBLISH_INTERVAL_MS:500}") + public void publishPending() { + List jobs = jdbc.query("SELECT job_id FROM attachment_jobs WHERE status='queued' AND published_at IS NULL AND next_attempt_at <= now() ORDER BY created_at LIMIT 20", + (rs, row) -> new Job(rs.getObject("job_id", UUID.class))); + for (Job job : jobs) { + try { + broker.publish(job.id().toString()); + jdbc.update("UPDATE attachment_jobs SET published_at=now(), updated_at=now() WHERE job_id=? AND published_at IS NULL", job.id()); + } catch (RuntimeException ignored) { /* retry on the next poll */ } + } + } + private record Job(UUID id) {} +} diff --git a/src/main/java/ru/mtusi/messenger/RequestFilter.java b/src/main/java/ru/mtusi/messenger/RequestFilter.java new file mode 100644 index 00000000..a223c212 --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/RequestFilter.java @@ -0,0 +1,106 @@ +package ru.mtusi.messenger; + +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; +import org.springframework.web.filter.OncePerRequestFilter; +import org.springframework.web.servlet.HandlerMapping; +import java.io.IOException; +import java.sql.SQLException; +import java.util.UUID; +import org.springframework.dao.DataAccessException; +import static ru.mtusi.messenger.Models.*; + +@Component +@Order(Ordered.HIGHEST_PRECEDENCE) +public class RequestFilter extends OncePerRequestFilter { + public static final String ACTOR = "messenger.actor"; + public static final String REQUEST_ID = "messenger.requestId"; + public static final String TOKEN = "messenger.token"; + public static final String INSTANCE_ID = "messenger.instanceId"; + private static final String INSTANCE = instanceId(); + private static final Logger LOG = LoggerFactory.getLogger(RequestFilter.class); + private final SessionService sessions; + private final ObjectMapper mapper; + + public RequestFilter(SessionService sessions, ObjectMapper mapper) { + this.sessions = sessions; + this.mapper = mapper; + } + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, + FilterChain chain) throws ServletException, IOException { + String requestId = UUID.randomUUID().toString(); + request.setAttribute(REQUEST_ID, requestId); + response.setHeader("X-Request-Id", requestId); + request.setAttribute(INSTANCE_ID, INSTANCE); + response.setHeader("X-Instance-Id", INSTANCE); + long start = System.nanoTime(); + try { + String path = request.getRequestURI(); + boolean publicEndpoint = (path.equals("/api/v1/users") && request.getMethod().equals("POST")) + || (path.equals("/api/v1/auth/sessions") && request.getMethod().equals("POST")); + if (path.startsWith("/api/v1/") && !publicEndpoint) { + String header = request.getHeader("Authorization"); + if (header == null || !header.startsWith("Bearer ")) throw ApiException.unauthorized(); + String token = header.substring("Bearer ".length()).trim(); + request.setAttribute(TOKEN, token); + request.setAttribute(ACTOR, sessions.authenticate(token)); + } + chain.doFilter(request, response); + } catch (ApiException exception) { + writeError(response, exception.status(), exception.code(), exception.getMessage(), requestId, request); + } catch (Exception exception) { + if (unavailable(exception)) { + writeError(response, 503, "unavailable", "Хранилище данных недоступно", requestId, request); + } else if (exception instanceof ServletException servlet) { + throw servlet; + } else if (exception instanceof IOException io) { + throw io; + } else { + throw new ServletException(exception); + } + } finally { + Object route = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); + LOG.info("request_id={} method={} route={} status={} duration_ms={}", requestId, + request.getMethod(), route == null ? "unmapped" : route, response.getStatus(), + (System.nanoTime() - start) / 1_000_000); + } + } + + private void writeError(HttpServletResponse response, int status, String code, String message, + String requestId, HttpServletRequest request) throws IOException { + response.setStatus(status); + if (status == 401) { + String challenge = request.getRequestURI().equals("/api/v1/auth/sessions") + ? "Basic realm=\"messenger\", charset=\"UTF-8\"" + : "Bearer realm=\"messenger\""; + response.setHeader("WWW-Authenticate", challenge); + } + response.setContentType("application/json"); + response.setCharacterEncoding("UTF-8"); + mapper.writeValue(response.getOutputStream(), new ErrorBody(new ErrorDetail(code, message, requestId))); + } + + private boolean unavailable(Throwable error) { + for (Throwable cause = error; cause != null; cause = cause.getCause()) { + if (cause instanceof DataAccessException || cause instanceof SQLException) return true; + } + return false; + } + + private static String instanceId() { + String configured = System.getenv("API_INSTANCE_ID"); + if (configured != null && !configured.isBlank()) return configured; + String hostname = System.getenv("HOSTNAME"); + return hostname == null || hostname.isBlank() ? UUID.randomUUID().toString() : hostname; + } +} diff --git a/src/main/java/ru/mtusi/messenger/S3Storage.java b/src/main/java/ru/mtusi/messenger/S3Storage.java new file mode 100644 index 00000000..00d0b69e --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/S3Storage.java @@ -0,0 +1,72 @@ +package ru.mtusi.messenger; + +import io.minio.GetObjectArgs; +import io.minio.MakeBucketArgs; +import io.minio.MinioClient; +import io.minio.PutObjectArgs; +import io.minio.RemoveObjectArgs; +import io.minio.BucketExistsArgs; +import org.springframework.stereotype.Component; + +import java.io.InputStream; + +@Component +public class S3Storage { + private final MinioClient client; + private final String bucket; + + public S3Storage() { + String endpoint = env("S3_ENDPOINT", "http://minio:9000"); + String access = env("S3_ACCESS_KEY_ID", "messenger-api"); + String secret = env("S3_SECRET_ACCESS_KEY", "messenger-api-secret"); + this.bucket = env("S3_BUCKET", "messenger-attachments"); + this.client = MinioClient.builder().endpoint(endpoint).credentials(access, secret).build(); + try { + if (!client.bucketExists(BucketExistsArgs.builder().bucket(bucket).build())) { + client.makeBucket(MakeBucketArgs.builder().bucket(bucket).build()); + } + } catch (Exception exception) { + throw new IllegalStateException("S3 bucket is unavailable", exception); + } + } + + public void put(String key, InputStream input, long size, String contentType) { + try { + client.putObject(PutObjectArgs.builder().bucket(bucket).object(key).stream(input, size, -1) + .contentType(contentType).build()); + } catch (Exception exception) { + throw new ApiException(503, "unavailable", "S3-хранилище недоступно"); + } + } + + public void check() { + try { + if (!client.bucketExists(BucketExistsArgs.builder().bucket(bucket).build())) { + throw new IllegalStateException("bucket is missing"); + } + } catch (Exception exception) { + throw new ApiException(503, "unavailable", "S3-хранилище недоступно"); + } + } + + public InputStream get(String key) { + try { + return client.getObject(GetObjectArgs.builder().bucket(bucket).object(key).build()); + } catch (Exception exception) { + throw new ApiException(503, "unavailable", "S3-хранилище недоступно"); + } + } + + public void delete(String key) { + try { + client.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(key).build()); + } catch (Exception ignored) { + // Best-effort compensation; orphan cleanup is documented in REPORT.md. + } + } + + private static String env(String name, String fallback) { + String value = System.getenv(name); + return value == null || value.isBlank() ? fallback : value; + } +} diff --git a/src/main/java/ru/mtusi/messenger/SessionService.java b/src/main/java/ru/mtusi/messenger/SessionService.java new file mode 100644 index 00000000..41986b2e --- /dev/null +++ b/src/main/java/ru/mtusi/messenger/SessionService.java @@ -0,0 +1,102 @@ +package ru.mtusi.messenger; + +import jakarta.annotation.PreDestroy; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder; +import org.springframework.stereotype.Service; + +import java.security.SecureRandom; +import java.time.Duration; +import java.time.Instant; +import java.util.Base64; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static ru.mtusi.messenger.Models.*; + +@Service +public class SessionService { + private static final Base64.Encoder TOKEN_ENCODER = Base64.getUrlEncoder().withoutPadding(); + private final MessengerStore store; + private final StringRedisTemplate redis; + private final SCryptPasswordEncoder passwords = SCryptPasswordEncoder.defaultsForSpringSecurity_v5_8(); + private final SecureRandom random = new SecureRandom(); + private final long ttlSeconds; + private final long databaseTimeoutMillis; + private final ExecutorService databaseExecutor = Executors.newFixedThreadPool(4); + + public SessionService(MessengerStore store, StringRedisTemplate redis, + @Value("${SESSION_TTL_SECONDS:3600}") long ttlSeconds, + @Value("${DB_REQUEST_TIMEOUT_MS:2000}") long databaseTimeoutMillis) { + this.store = store; + this.redis = redis; + this.ttlSeconds = Math.max(2, ttlSeconds); + this.databaseTimeoutMillis = Math.max(250, databaseTimeoutMillis); + } + + public String hash(String password) { return passwords.encode(password); } + + public Session login(String username, String password) { + Credentials credentials = database(() -> store.credentials(username)); + if (credentials == null || !passwords.matches(password, credentials.passwordHash())) throw ApiException.unauthorized(); + byte[] bytes = new byte[32]; + random.nextBytes(bytes); + String token = TOKEN_ENCODER.encodeToString(bytes); + Instant expires = Instant.now().plusSeconds(ttlSeconds); + try { + redis.opsForValue().set(key(token), credentials.user().id().toString(), Duration.ofSeconds(ttlSeconds)); + } catch (RuntimeException exception) { + throw new ApiException(503, "unavailable", "Сервис сессий недоступен"); + } + return new Session(token, "Bearer", expires, credentials.user()); + } + + public User authenticate(String token) { + if (token == null || token.isBlank()) throw ApiException.unauthorized(); + String value; + try { value = redis.opsForValue().get(key(token)); } + catch (RuntimeException exception) { throw new ApiException(503, "unavailable", "Сервис сессий недоступен"); } + if (value == null) throw ApiException.unauthorized(); + User user = database(() -> store.findUser(UUID.fromString(value))); + if (user == null) throw ApiException.unauthorized(); + return user; + } + + public void revoke(String token) { + if (token == null) throw ApiException.unauthorized(); + try { redis.delete(key(token)); } + catch (RuntimeException exception) { throw new ApiException(503, "unavailable", "Сервис сессий недоступен"); } + } + + public void check() { redis.opsForValue().get("messenger:readiness"); } + + private String key(String token) { return "messenger:session:" + token; } + + private T database(java.util.concurrent.Callable operation) { + Future future = databaseExecutor.submit(operation); + try { + return future.get(databaseTimeoutMillis, TimeUnit.MILLISECONDS); + } catch (TimeoutException exception) { + future.cancel(true); + throw new ApiException(503, "unavailable", "Хранилище данных недоступно"); + } catch (InterruptedException exception) { + Thread.currentThread().interrupt(); + throw new ApiException(503, "unavailable", "Хранилище данных недоступно"); + } catch (ExecutionException exception) { + Throwable cause = exception.getCause(); + if (cause instanceof RuntimeException runtime) throw runtime; + throw new IllegalStateException(cause); + } + } + + @PreDestroy + void shutdownDatabaseExecutor() { + databaseExecutor.shutdownNow(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 00000000..340f030f --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,32 @@ +spring.application.name=messenger-lab5 +server.address=${APP_HOST:0.0.0.0} +server.port=${APP_PORT:8080} +server.shutdown=graceful +spring.lifecycle.timeout-per-shutdown-phase=5s + +spring.datasource.url=${DATABASE_URL:jdbc:postgresql://${DB_HOST:postgres}:${DB_PORT:5432}/${DB_NAME:messenger}?socketTimeout=1&options=-c%20statement_timeout%3D1000} +spring.datasource.username=${DB_USERNAME:messenger} +spring.datasource.password=${DB_PASSWORD:messenger} +spring.datasource.hikari.maximum-pool-size=${DB_POOL_SIZE:10} +spring.datasource.hikari.connection-timeout=${DB_CONNECTION_TIMEOUT_MS:3000} +spring.datasource.hikari.validation-timeout=250 +spring.datasource.hikari.transaction-isolation=TRANSACTION_READ_COMMITTED +# Connection timeout only limits waiting for a free pool slot. These settings +# also bound an already running PostgreSQL statement and socket read. +spring.datasource.hikari.data-source-properties.[socketTimeout]=${DB_SOCKET_TIMEOUT_SECONDS:1} +spring.datasource.hikari.data-source-properties.[options]=-c statement_timeout=${DB_STATEMENT_TIMEOUT_MS:1000} +spring.jdbc.template.query-timeout=${DB_QUERY_TIMEOUT_SECONDS:1}s + +spring.data.redis.host=${REDIS_HOST:redis} +spring.data.redis.port=${REDIS_PORT:6379} +spring.data.redis.timeout=${REDIS_TIMEOUT:2s} +spring.data.redis.connect-timeout=${REDIS_CONNECT_TIMEOUT:2s} + +spring.flyway.enabled=true +spring.jackson.property-naming-strategy=SNAKE_CASE +spring.jackson.deserialization.fail-on-trailing-tokens=true +spring.jackson.parser.strict-duplicate-detection=true +server.error.include-message=never +server.error.include-stacktrace=never +spring.main.banner-mode=off +logging.level.root=INFO diff --git a/src/main/resources/db/migration/V1__messenger.sql b/src/main/resources/db/migration/V1__messenger.sql new file mode 100644 index 00000000..4381acaf --- /dev/null +++ b/src/main/resources/db/migration/V1__messenger.sql @@ -0,0 +1,30 @@ +CREATE TABLE users ( + id UUID PRIMARY KEY, + username VARCHAR(32) NOT NULL UNIQUE, + display_name VARCHAR(100) NOT NULL, + password_hash TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +CREATE TABLE chats ( + id UUID PRIMARY KEY, + title VARCHAR(100) NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +CREATE TABLE chat_members ( + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + PRIMARY KEY (chat_id, user_id) +); + +CREATE TABLE messages ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + sender_id UUID NOT NULL REFERENCES users(id), + text VARCHAR(4000) NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +CREATE INDEX idx_messages_chat_created_id ON messages(chat_id, created_at, id); +CREATE INDEX idx_chat_members_user_chat ON chat_members(user_id, chat_id); diff --git a/src/main/resources/db/migration/V2__idempotency.sql b/src/main/resources/db/migration/V2__idempotency.sql new file mode 100644 index 00000000..2991aedc --- /dev/null +++ b/src/main/resources/db/migration/V2__idempotency.sql @@ -0,0 +1,13 @@ +CREATE TABLE message_idempotency ( + actor_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + method VARCHAR(10) NOT NULL, + request_path VARCHAR(200) NOT NULL, + idempotency_key VARCHAR(128) NOT NULL, + request_hash CHAR(64) NOT NULL, + message_id UUID REFERENCES messages(id) ON DELETE CASCADE, + created_at TIMESTAMPTZ NOT NULL, + PRIMARY KEY (actor_id, method, request_path, idempotency_key) +); + +CREATE INDEX idx_message_idempotency_created + ON message_idempotency(created_at); diff --git a/src/main/resources/db/migration/V3__attachments.sql b/src/main/resources/db/migration/V3__attachments.sql new file mode 100644 index 00000000..46ef2156 --- /dev/null +++ b/src/main/resources/db/migration/V3__attachments.sql @@ -0,0 +1,24 @@ +CREATE TABLE attachments ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + owner_id UUID NOT NULL REFERENCES users(id), + filename VARCHAR(255) NOT NULL, + content_type VARCHAR(32) NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')), + size_bytes BIGINT NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760), + sha256 CHAR(64) NOT NULL, + object_key TEXT NOT NULL UNIQUE, + status VARCHAR(16) NOT NULL CHECK (status IN ('ready')), + error_code VARCHAR(64), + created_at TIMESTAMPTZ NOT NULL, + updated_at TIMESTAMPTZ NOT NULL +); + +CREATE INDEX idx_attachments_chat ON attachments(chat_id, created_at, id); + +CREATE TABLE message_attachments ( + message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE, + attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE RESTRICT, + PRIMARY KEY (message_id, attachment_id) +); + +CREATE INDEX idx_message_attachments_attachment ON message_attachments(attachment_id); diff --git a/src/main/resources/db/migration/V4__async_attachments.sql b/src/main/resources/db/migration/V4__async_attachments.sql new file mode 100644 index 00000000..fdd76ca1 --- /dev/null +++ b/src/main/resources/db/migration/V4__async_attachments.sql @@ -0,0 +1,33 @@ +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_check; +ALTER TABLE attachments ADD COLUMN IF NOT EXISTS staging_key TEXT; +ALTER TABLE attachments ADD COLUMN IF NOT EXISTS pipeline_version INTEGER NOT NULL DEFAULT 1; +ALTER TABLE attachments ADD CONSTRAINT attachments_status_check CHECK (status IN ('queued', 'processing', 'ready', 'failed')); + +CREATE TABLE attachment_jobs ( + job_id UUID PRIMARY KEY, + attachment_id UUID NOT NULL UNIQUE REFERENCES attachments(id) ON DELETE CASCADE, + pipeline_version INTEGER NOT NULL DEFAULT 1, + attempt INTEGER NOT NULL DEFAULT 0, + status VARCHAR(16) NOT NULL CHECK (status IN ('queued', 'processing', 'ready', 'failed')), + next_attempt_at TIMESTAMPTZ NOT NULL, + lease_until TIMESTAMPTZ, + error_code VARCHAR(64), + published_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL, + updated_at TIMESTAMPTZ NOT NULL +); + +CREATE INDEX idx_attachment_jobs_publish ON attachment_jobs(status, published_at, next_attempt_at); +CREATE INDEX idx_attachment_jobs_lease ON attachment_jobs(status, lease_until); + +CREATE TABLE attachment_variants ( + attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE, + name VARCHAR(16) NOT NULL CHECK (name IN ('thumbnail', 'crop', 'optimized')), + content_type VARCHAR(32) NOT NULL, + size_bytes BIGINT NOT NULL, + width INTEGER NOT NULL, + height INTEGER NOT NULL, + sha256 CHAR(64) NOT NULL, + object_key TEXT NOT NULL, + PRIMARY KEY (attachment_id, name) +); diff --git a/student-tests/lab5_grade4.py b/student-tests/lab5_grade4.py new file mode 100755 index 00000000..a9e2ec1c --- /dev/null +++ b/student-tests/lab5_grade4.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +"""Student grade-4 checks for the asynchronous attachment pipeline.""" +import hashlib +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) +from client import Client, arguments, check, run +from image_probe import jpeg_size + + +def main(): + args = arguments('Lab 5 grade-4 attachment pipeline') + client = Client(args.base_url, args.ca_file, timeout=10) + alice, bob, _, chat = client.bootstrap() + + attachment, original = client.attachment(alice, chat) + check(attachment['status'] == 'queued', 'upload must be acknowledged as queued') + check(attachment['variants'] == [], 'queued attachment must not expose ready variants') + + linked = client.message(alice, chat, '', attachments=[attachment['id']])['json'] + check(linked['attachment_ids'] == [attachment['id']], 'queued attachment must be linkable') + + ready = client.wait_ready(bob, attachment) + check(ready['status'] == 'ready' and ready['error_code'] is None, 'attachment did not become ready') + variants = {item['name']: item for item in ready['variants']} + check(set(variants) == {'thumbnail', 'crop', 'optimized'}, 'grade-4 variants are incomplete') + check((variants['thumbnail']['width'], variants['thumbnail']['height']) == (256, 160), 'thumbnail dimensions') + check((variants['crop']['width'], variants['crop']['height']) == (128, 128), 'crop dimensions') + check(variants['optimized']['width'] <= 1280 and variants['optimized']['height'] <= 1280, 'optimized dimensions') + + original_response = client.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content', actor=bob) + check(original_response['bytes'] == original, 'original object was changed') + check(hashlib.sha256(original_response['bytes']).hexdigest() == attachment['sha256'], 'original checksum') + + thumbnail = client.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content?variant=thumbnail', actor=bob) + check(thumbnail['headers'].get_content_type() == 'image/jpeg', 'thumbnail MIME') + check(jpeg_size(thumbnail['bytes']) == (256, 160), 'thumbnail bytes dimensions') + check(hashlib.sha256(thumbnail['bytes']).hexdigest() == variants['thumbnail']['sha256'], 'thumbnail checksum') + + crop = client.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content?variant=crop', actor=bob) + check(crop['headers'].get_content_type() == 'image/jpeg', 'crop MIME') + check(jpeg_size(crop['bytes']) == (128, 128), 'crop bytes dimensions') + check(hashlib.sha256(crop['bytes']).hexdigest() == variants['crop']['sha256'], 'crop checksum') + + optimized = client.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content?variant=optimized', actor=bob) + check(optimized['headers'].get_content_type() == 'image/webp', 'optimized MIME') + check(optimized['bytes'][:4] == b'RIFF' and optimized['bytes'][8:12] == b'WEBP', 'optimized is WebP') + check(hashlib.sha256(optimized['bytes']).hexdigest() == variants['optimized']['sha256'], 'optimized checksum') + + # A second metadata read proves that the idempotent worker result is stable. + stable = client.call('GET', '/api/v1/attachments/' + attachment['id'], actor=bob, schema='Attachment')['json'] + check(stable['variants'] == ready['variants'], 'repeated metadata read changed variants') + print('PASS: grade-4 async attachment pipeline; queue, worker, crop, WebP and idempotent metadata verified.') + + +if __name__ == '__main__': + run(main)