From a7bedda1837e57afc31f9a7bf7c9e9cf07d36428 Mon Sep 17 00:00:00 2001 From: Khakimov Dmitrij Date: Wed, 28 Oct 2026 21:48:04 +0300 Subject: [PATCH] lab3 done --- .dockerignore | 15 +- Dockerfile | 57 +++ Makefile | 61 ++- PORTING.md | 110 ++++++ README.md | 2 +- cmd/main.go | 100 +++++ compose.yaml | 101 +++++ contracts/embed.go | 9 + dto/create_chat.go | 13 + dto/create_message.go | 14 + dto/create_user.go | 14 + dto/current_user.go | 10 + dto/error.go | 11 + dto/get_chat.go | 12 + dto/list_chats.go | 18 + dto/list_messages.go | 20 + dto/liveness.go | 7 + dto/readiness.go | 7 + dto/session.go | 8 + dto/timestamp.go | 11 + dto/timestamp_test.go | 15 + go.mod | 31 ++ go.sum | 67 ++++ infra/README.md | 4 + infra/haproxy.cfg | 36 ++ infra/migrate.sh | 8 + infra/redis.conf | 9 + infra/schema.md | 25 ++ internal/app/chat/access.go | 31 ++ internal/app/chat/create_chat.go | 33 ++ internal/app/chat/create_message.go | 51 +++ internal/app/chat/create_user.go | 32 ++ internal/app/chat/current_user.go | 21 ++ internal/app/chat/cursor.go | 75 ++++ internal/app/chat/errors.go | 91 +++++ internal/app/chat/get_chat.go | 32 ++ internal/app/chat/list_chats.go | 53 +++ internal/app/chat/list_messages.go | 70 ++++ internal/app/chat/liveness.go | 13 + internal/app/chat/middleware.go | 43 +++ internal/app/chat/readiness.go | 20 + internal/app/chat/request.go | 74 ++++ internal/app/chat/service.go | 86 +++++ internal/app/chat/service_test.go | 353 ++++++++++++++++++ internal/app/chat/session.go | 45 +++ internal/app/chat/swagger.go | 38 ++ internal/app/chat/usecase.go | 21 ++ internal/config/config.go | 100 +++++ internal/pkg/domain/chat.go | 10 + internal/pkg/domain/errors.go | 32 ++ internal/pkg/domain/message.go | 12 + internal/pkg/domain/pagination.go | 23 ++ internal/pkg/domain/session.go | 14 + internal/pkg/domain/user.go | 10 + internal/pkg/password/password.go | 65 ++++ internal/pkg/password/password_test.go | 35 ++ internal/pkg/repository/memory.go | 202 ++++++++++ internal/pkg/repository/postgres/chats.go | 78 ++++ internal/pkg/repository/postgres/messages.go | 61 +++ internal/pkg/repository/postgres/storage.go | 51 +++ .../pkg/repository/postgres/storage_test.go | 282 ++++++++++++++ internal/pkg/repository/postgres/users.go | 46 +++ internal/pkg/session/redis.go | 71 ++++ internal/usecase/chat/repository.go | 28 ++ internal/usecase/chat/service.go | 158 ++++++++ internal/usecase/chat/session.go | 102 +++++ internal/usecase/chat/session_test.go | 19 + internal/usecase/chat/validation.go | 37 ++ lab.json | 2 +- migrations/001_initial.sql | 54 +++ migrations/002_idempotency.sql | 10 + scripts/bootstrap.sh | 15 + scripts/compose.sh | 7 + scripts/measure_load.py | 54 +++ scripts/restart.sh | 4 + scripts/stop-one.sh | 4 + scripts/swarm.py | 228 +++++++++++ stack.yaml | 121 ++++++ tests/own/README.md | 15 + tests/own/grade4.py | 120 ++++++ tests/own/swarm_grade4.py | 165 ++++++++ 81 files changed, 4202 insertions(+), 10 deletions(-) create mode 100644 Dockerfile create mode 100644 PORTING.md create mode 100644 cmd/main.go create mode 100644 compose.yaml create mode 100644 contracts/embed.go create mode 100644 dto/create_chat.go create mode 100644 dto/create_message.go create mode 100644 dto/create_user.go create mode 100644 dto/current_user.go create mode 100644 dto/error.go create mode 100644 dto/get_chat.go create mode 100644 dto/list_chats.go create mode 100644 dto/list_messages.go create mode 100644 dto/liveness.go create mode 100644 dto/readiness.go create mode 100644 dto/session.go create mode 100644 dto/timestamp.go create mode 100644 dto/timestamp_test.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 infra/haproxy.cfg create mode 100755 infra/migrate.sh create mode 100644 infra/redis.conf create mode 100644 infra/schema.md create mode 100644 internal/app/chat/access.go create mode 100644 internal/app/chat/create_chat.go create mode 100644 internal/app/chat/create_message.go create mode 100644 internal/app/chat/create_user.go create mode 100644 internal/app/chat/current_user.go create mode 100644 internal/app/chat/cursor.go create mode 100644 internal/app/chat/errors.go create mode 100644 internal/app/chat/get_chat.go create mode 100644 internal/app/chat/list_chats.go create mode 100644 internal/app/chat/list_messages.go create mode 100644 internal/app/chat/liveness.go create mode 100644 internal/app/chat/middleware.go create mode 100644 internal/app/chat/readiness.go create mode 100644 internal/app/chat/request.go create mode 100644 internal/app/chat/service.go create mode 100644 internal/app/chat/service_test.go create mode 100644 internal/app/chat/session.go create mode 100644 internal/app/chat/swagger.go create mode 100644 internal/app/chat/usecase.go create mode 100644 internal/config/config.go create mode 100644 internal/pkg/domain/chat.go create mode 100644 internal/pkg/domain/errors.go create mode 100644 internal/pkg/domain/message.go create mode 100644 internal/pkg/domain/pagination.go create mode 100644 internal/pkg/domain/session.go create mode 100644 internal/pkg/domain/user.go create mode 100644 internal/pkg/password/password.go create mode 100644 internal/pkg/password/password_test.go create mode 100644 internal/pkg/repository/memory.go create mode 100644 internal/pkg/repository/postgres/chats.go create mode 100644 internal/pkg/repository/postgres/messages.go create mode 100644 internal/pkg/repository/postgres/storage.go create mode 100644 internal/pkg/repository/postgres/storage_test.go create mode 100644 internal/pkg/repository/postgres/users.go create mode 100644 internal/pkg/session/redis.go create mode 100644 internal/usecase/chat/repository.go create mode 100644 internal/usecase/chat/service.go create mode 100644 internal/usecase/chat/session.go create mode 100644 internal/usecase/chat/session_test.go create mode 100644 internal/usecase/chat/validation.go create mode 100644 migrations/001_initial.sql create mode 100644 migrations/002_idempotency.sql create mode 100755 scripts/bootstrap.sh create mode 100755 scripts/compose.sh create mode 100644 scripts/measure_load.py create mode 100755 scripts/restart.sh create mode 100755 scripts/stop-one.sh create mode 100644 scripts/swarm.py create mode 100644 stack.yaml create mode 100644 tests/own/README.md create mode 100644 tests/own/grade4.py create mode 100644 tests/own/swarm_grade4.py diff --git a/.dockerignore b/.dockerignore index bc16e556..ef48c580 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,8 +1,8 @@ .git .github +.gitignore .env .env.* -!.env.example secrets certs volumes @@ -17,4 +17,17 @@ backups __pycache__ node_modules evidence +examples +scripts +tests +*.md +Dockerfile* +Makefile +requirements-dev.txt +lab.json .DS_Store + +.idea +.vscode +.agents +.codex diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..24d8a7f2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,57 @@ +FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS goose-builder +RUN GOBIN=/out CGO_ENABLED=0 go install \ + -tags='no_clickhouse no_libsql no_mssql no_mysql no_sqlite3 no_vertica no_ydb no_spanner' \ + github.com/pressly/goose/v3/cmd/goose@v3.27.1 + +FROM alpine:3.23@sha256:075c2c1a4068c1e251228ca2b0ea8b163f835ca5d66e758b93c7fc89b1995616 AS migrations +COPY --from=goose-builder /out/goose /usr/local/bin/goose +COPY migrations /migrations +COPY infra/migrate.sh /usr/local/bin/migrate +USER 10001:10001 +ENTRYPOINT ["/usr/local/bin/migrate"] +CMD ["up"] + +FROM golang:1.26.4-alpine3.23@sha256:18b460dd17542c2ba43299a633cf6ebfc1115101509531471d7cfce1019af083 AS builder + +WORKDIR /src + +RUN apk add --no-cache gcc musl-dev + +COPY go.mod go.sum ./ +RUN go mod download + +COPY cmd ./cmd +COPY contracts ./contracts +COPY dto ./dto +COPY internal ./internal +COPY migrations ./migrations + +RUN CGO_ENABLED=0 GOOS=linux go build \ + -trimpath \ + -ldflags="-s -w" \ + -o /out/messenger ./cmd + +FROM builder AS tests +COPY --from=goose-builder /out/goose /usr/local/bin/goose +ENV GOOSE_MIGRATION_DIR=/src/migrations +CMD ["go", "test", "-p=1", "-race", "-count=1", "-v", "./..."] + +FROM alpine:3.23@sha256:075c2c1a4068c1e251228ca2b0ea8b163f835ca5d66e758b93c7fc89b1995616 + +RUN addgroup -S messenger \ + && adduser -S -D -H -u 10001 -G messenger messenger + +COPY --from=builder --chown=messenger:messenger /out/messenger /usr/local/bin/messenger + +ENV PORT=8080 + +EXPOSE 8080 + +USER messenger:messenger + +HEALTHCHECK --interval=10s --timeout=3s --start-period=3s --retries=3 \ + CMD wget -q -O /dev/null "http://127.0.0.1:${PORT}/health/ready" || exit 1 + +STOPSIGNAL SIGTERM + +ENTRYPOINT ["/usr/local/bin/messenger"] diff --git a/Makefile b/Makefile index 5d86d171..074d47f5 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,20 @@ -PYTHON ?= python3 -BASE_URL ?= http://localhost:8080 +PYTHON ?= $(if $(wildcard .venv/bin/python),.venv/bin/python,python3) +GO ?= go +PORT ?= 8080 +BASE_URL ?= http://localhost:$(PORT) +IMAGE ?= mtusi-messenger:lab4 CA_FILE ?= -ACTION_SCRIPT ?= +ACTION_SCRIPT ?= scripts/restart.sh +FAILOVER_SCRIPT ?= scripts/stop-one.sh +export PYTHONDONTWRITEBYTECODE=1 TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) -.PHONY: help check validate test load test-persistence test-failover +.PHONY: help run docker-build docker-run check validate test test-own test-all load test-persistence bootstrap up down migrate test-integration test-grade4 measure-load migrate-status rolling-update test-failover wait + +export PORT BASE_URL help: - @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" + @echo "up: build and deploy Swarm stack; down: stop with volumes retained; test-integration: PostgreSQL race/rollback/EXPLAIN; test-grade4: cross-replica idempotency, both failures and rolling update; check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" check: $(PYTHON) scripts/check_contract.py @@ -18,6 +25,11 @@ validate: test: $(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS) +test-own: + $(GO) test -race ./... + +test-all: check validate test-own + load: $(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS) @@ -25,6 +37,41 @@ test-persistence: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2) $(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) +bootstrap: + ./scripts/bootstrap.sh + +up: + $(PYTHON) scripts/swarm.py up + +down: + $(PYTHON) scripts/swarm.py down + +migrate: + $(PYTHON) scripts/swarm.py migrate + +migrate-status: + $(PYTHON) scripts/swarm.py migrate-status + +rolling-update: + $(PYTHON) scripts/swarm.py update + +run: + PORT="$(PORT)" $(GO) run ./cmd + +docker-build: + docker build -t "$(IMAGE)" . + +test-integration: + $(PYTHON) scripts/swarm.py test-integration + +test-grade4: + $(PYTHON) tests/own/swarm_grade4.py + +measure-load: + $(PYTHON) scripts/measure_load.py --base-url "$(BASE_URL)" + test-failover: - @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2) - $(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + $(PYTHON) tests/resilience.py --mode failover --action-script "$(if $(filter command line,$(origin ACTION_SCRIPT)),$(ACTION_SCRIPT),$(FAILOVER_SCRIPT))" --base-url "$(BASE_URL)" $(TLS_ARGS) + +wait: + $(PYTHON) scripts/swarm.py wait diff --git a/PORTING.md b/PORTING.md new file mode 100644 index 00000000..6e9092bb --- /dev/null +++ b/PORTING.md @@ -0,0 +1,110 @@ +# Перенос реализации lab3 + +В репозиторий перенесена логика из `mtusi2026lab3`. Исходный проект сохранён без изменений. + +## Состав + +- `cmd/`, `internal/`, `dto/`: HTTP API, пользователи, чаты, сообщения, права доступа и пагинация. +- `internal/pkg/session/`: общие сессии в Redis. +- `internal/pkg/repository/postgres/`: PostgreSQL, транзакции и идемпотентная отправка сообщений. +- `migrations/`: исходные миграции goose `001_initial.sql` и `002_idempotency.sql` без изменений. +- `contracts/embed.go`: встраивание текущего OpenAPI в бинарный файл для Swagger UI. +- `Dockerfile`: сборка API, миграций и контейнера интеграционных тестов. +- `stack.yaml`, `infra/`: две API-реплики, HAProxy, PostgreSQL и Redis AOF. +- `scripts/`: запуск, миграции, перезапуск, остановка реплики, последовательное обновление API и измерение нагрузки. +- `tests/own/` и Go-тесты: собственные проверки предыдущих лабораторных. + +Контракт, примеры запросов и публичные проверки lab4 сохранены. Swagger UI показывает контракт lab4. +S3, вложения и поле `attachment_ids` пока не реализованы. Публичные HTTP-проверки lab4 требуют этих возможностей. +Результаты экспериментов lab3 не перенесены: они относятся к другому стенду. + +## Запуск + +Установите Docker Engine с поддержкой Swarm, Python 3.9+ и Make. +Для локальных Go-тестов установите Go 1.26.4 или новее. +Dockerfile использует Go 1.26.4 и goose 3.27.1. + +Запустите стенд на одном Docker-узле: + +```sh +make up +make wait +``` + +Команда создаёт локальные секреты, собирает образы и применяет миграции перед запуском API. +Повторный запуск сохраняет данные и применяет новые миграции перед обновлением API. +Секреты создаются отдельно для этого репозитория; копирование секретов lab3 не требуется. + +API доступен по адресу , Swagger UI — . +По умолчанию используется стек `mtusi-lab4`, образы с тегом `lab4` и метка узла `mtusi_lab4_storage`. +PostgreSQL и Redis используют отдельные volumes этого стека. Go module path сохранён для совместимости исходных импортов. + +Если порт 8080 занят, задайте другой порт: + +```sh +PORT=8081 make up +PORT=8081 make wait +``` + +Передавайте одинаковые `PORT`, `STACK_NAME`, `API_IMAGE` и `API_REPLICAS` всем командам, если меняете их значения. +Параметры передаются через переменные процесса. Скрипты не загружают файлы окружения автоматически. + +## Проверки + +Выполните проверки исходников: + +```sh +make check +make test-own +goose -env /dev/null -dir migrations validate +``` + +Для полной проверки контракта установите зависимости из `requirements-dev.txt` в виртуальное окружение. +Затем выполните `make validate PYTHON=.venv/bin/python`. + +После запуска стенда выполните интеграционные Go-тесты: + +```sh +make test-integration +``` + +Без `INTEGRATION_TESTS=1` Go-тесты пропускают сценарии с PostgreSQL. +Команда `make test-integration` запускает их в отдельном Swarm job с временной схемой БД. +Она проверяет миграции, транзакции, ограничения, пагинацию и конкурентные запросы. + +Команды `make test`, `make test-persistence` и `make test-failover` используют публичный профиль lab4. +Для их полного прохождения добавьте S3 и вложения. +Команда `make test-grade4` перенесена из lab3 и проверяет масштабирование; она не подтверждает критерии оценки lab4. + +## Управление стендом + +Проверьте состояние: + +```sh +python3 scripts/swarm.py status +make migrate-status +``` + +Перезапустите API, HAProxy, PostgreSQL и Redis с сохранением данных: + +```sh +./scripts/restart.sh +``` + +Выполните последовательное обновление API: + +```sh +make rolling-update +``` + +Остановите стенд: + +```sh +make down +``` + +Команда сохраняет volumes и Swarm secrets. Стенд выдерживает отказ API-реплики в пределах одного узла. +Отказ узла, PostgreSQL, Redis или HAProxy приводит к недоступности соответствующих операций. + +`compose.yaml` и `scripts/compose.sh` сохраняют вспомогательный вариант из предыдущих лабораторных. +Он запускает одну API-реплику без HAProxy. Основной стенд использует Swarm и `stack.yaml`. diff --git a/README.md b/README.md index 947094c3..48f55366 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Разделить метаданные и бинарное содержимое, добавить изображения в сообщения и сохранить доступ к ним при переключении API-реплики. -Это **skeleton**, а не готовый сервер. Здесь есть задание, контракт и публичные проверки. Реализацию приложения, Dockerfile и требуемую инфраструктуру пишет студент. Язык и framework свободные; UI не обязателен. +В проект перенесена реализация lab3: Go API, PostgreSQL, Redis, HAProxy, Docker Swarm и собственные проверки. Инструкция запуска находится в [PORTING.md](PORTING.md). S3 и вложения из задания lab4 пока не реализованы. Задание, контракт и публичные проверки lab4 сохранены. [Карта курса](COURSE.md) · [Процесс fork/PR](CONTRIBUTING.md) · [OpenAPI](contracts/openapi.json) · [Семантика API](contracts/README.md) · [Проверки](tests/README.md) · [Отчёт](REPORT.md) · [Примеры JSON](contracts/examples.json) · [Памятка преподавателю](MAINTAINERS.md) diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 00000000..aa05fec6 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,100 @@ +package main + +import ( + "context" + "errors" + "fmt" + "log" + "net/http" + "os" + "os/signal" + "syscall" + "time" + + "github.com/jackc/pgx/v5/pgxpool" + "github.com/redis/go-redis/v9" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/app/chat" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/config" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/repository/postgres" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/session" + chatusecase "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat" +) + +const shutdownTimeout = 8 * time.Second + +func main() { + cfg, err := config.Load() + if err != nil { + log.Fatal(err) + } + poolConfig, err := pgxpool.ParseConfig("sslmode=disable") + if err != nil { + log.Fatal("invalid PostgreSQL config") + } + poolConfig.ConnConfig.Host = cfg.PostgresHost + poolConfig.ConnConfig.Port = cfg.PostgresPort + poolConfig.ConnConfig.User = cfg.PostgresUser + poolConfig.ConnConfig.Database = cfg.PostgresDatabase + poolConfig.ConnConfig.Password = cfg.PostgresPassword + poolConfig.ConnConfig.ConnectTimeout = cfg.QueryTimeout + poolConfig.ConnConfig.RuntimeParams["timezone"] = "UTC" + poolConfig.ConnConfig.RuntimeParams["statement_timeout"] = fmt.Sprint(cfg.QueryTimeout.Milliseconds()) + poolConfig.MaxConns = cfg.PostgresMaxConns + poolConfig.MinConns = 1 + poolConfig.MaxConnLifetime = time.Hour + poolConfig.MaxConnIdleTime = 5 * time.Minute + pool, err := pgxpool.NewWithConfig(context.Background(), poolConfig) + if err != nil { + log.Fatal("cannot create PostgreSQL pool") + } + defer pool.Close() + redisClient := redis.NewClient(&redis.Options{ + Addr: cfg.RedisAddr, PoolSize: 10, MaxActiveConns: 10, PoolTimeout: cfg.QueryTimeout, + DialTimeout: cfg.QueryTimeout, ReadTimeout: cfg.QueryTimeout, WriteTimeout: cfg.QueryTimeout, + MaxRetries: -1, ContextTimeoutEnabled: true, + }) + defer redisClient.Close() + repo := postgres.New(pool, cfg.QueryTimeout) + sessions := session.NewRedis(redisClient, cfg.QueryTimeout) + useCase := chatusecase.New(repo, sessions, cfg.SessionTTL) + handler := chat.NewChat(useCase, cfg.CursorKey) + port := cfg.Port + e := handler.Router() + e.Server.ReadHeaderTimeout = 5 * time.Second + e.Server.ReadTimeout = 10 * time.Second + e.Server.WriteTimeout = 15 * time.Second + e.Server.IdleTimeout = 60 * time.Second + + serverErrors := make(chan error, 1) + go func() { + log.Printf("HTTP server is listening on :%s", port) + serverErrors <- e.Start(":" + port) + }() + + stop := make(chan os.Signal, 1) + signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM) + select { + case sig := <-stop: + log.Printf("received %s, shutting down", sig) + case err := <-serverErrors: + if !errors.Is(err, http.ErrServerClosed) { + log.Fatalf("HTTP server failed: %v", err) + } + return + } + + handler.Drain() + // Allow two 1-second LB checks to remove this backend before closing the listener. + time.Sleep(3 * time.Second) + ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout) + defer cancel() + if err := e.Shutdown(ctx); err != nil { + log.Printf("graceful shutdown failed: %v", err) + if closeErr := e.Close(); closeErr != nil { + log.Printf("forced shutdown failed: %v", closeErr) + } + return + } + log.Printf("shutdown complete") +} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..59c63714 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,101 @@ +name: mtusi2026lab4 +services: + postgres: + image: postgres:17.6-alpine3.22 + environment: + POSTGRES_USER: messenger + POSTGRES_DB: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + secrets: + - postgres_password + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: [CMD-SHELL, 'pg_isready -U messenger -d messenger'] + interval: 2s + timeout: 2s + retries: 30 + cpus: 1.0 + mem_limit: 512m + redis: + image: redis:7.4.5-alpine3.21 + command: [redis-server, /usr/local/etc/redis/redis.conf] + volumes: + - redis_data:/data + - ./infra/redis.conf:/usr/local/etc/redis/redis.conf:ro + healthcheck: + test: [CMD, redis-cli, ping] + interval: 2s + timeout: 2s + retries: 30 + cpus: 0.5 + mem_limit: 128m + migrations: + image: mtusi-migrations:lab4 + build: + context: . + target: migrations + environment: + GOOSE_DRIVER: postgres + GOOSE_DBSTRING: host=postgres user=messenger dbname=messenger sslmode=disable connect_timeout=5 + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + secrets: + - postgres_password + depends_on: + postgres: + condition: service_healthy + cpus: 0.5 + mem_limit: 128m + api: + build: . + image: mtusi-messenger:lab4 + environment: &api_environment + POSTGRES_HOST: postgres + POSTGRES_USER: messenger + POSTGRES_DB: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + POSTGRES_MAX_CONNS: '10' + REDIS_ADDR: redis:6379 + CURSOR_KEY_FILE: /run/secrets/cursor_key + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + QUERY_TIMEOUT_MS: '2000' + secrets: &api_secrets + - postgres_password + - cursor_key + ports: + - '127.0.0.1:${PORT:-8080}:8080' + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + migrations: + condition: service_completed_successfully + cpus: 1.0 + mem_limit: 256m + stop_grace_period: 10s + tests: + image: mtusi-messenger-tests:lab4 + profiles: [test] + build: + context: . + target: tests + environment: + <<: *api_environment + INTEGRATION_TESTS: '1' + secrets: *api_secrets + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + cpus: 1.0 + mem_limit: 1g +secrets: + postgres_password: + file: ./secrets/postgres_password + cursor_key: + file: ./secrets/cursor_key +volumes: + postgres_data: + redis_data: diff --git a/contracts/embed.go b/contracts/embed.go new file mode 100644 index 00000000..ef4b85ae --- /dev/null +++ b/contracts/embed.go @@ -0,0 +1,9 @@ +package contracts + +import _ "embed" + +// OpenAPI is embedded so Swagger UI works from a compiled binary and does not +// depend on the process working directory. +// +//go:embed openapi.json +var OpenAPI []byte diff --git a/dto/create_chat.go b/dto/create_chat.go new file mode 100644 index 00000000..25c42a03 --- /dev/null +++ b/dto/create_chat.go @@ -0,0 +1,13 @@ +package dto + +type CreateChatRequest struct { + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` +} + +type CreateChatResponse struct { + ID string `json:"id"` + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` + CreatedAt Timestamp `json:"created_at"` +} diff --git a/dto/create_message.go b/dto/create_message.go new file mode 100644 index 00000000..7cfeb14c --- /dev/null +++ b/dto/create_message.go @@ -0,0 +1,14 @@ +package dto + +type CreateMessageRequest struct { + ChatID string `json:"-"` + Text string `json:"text"` +} + +type CreateMessageResponse struct { + ID string `json:"id"` + ChatID string `json:"chat_id"` + SenderID string `json:"sender_id"` + Text string `json:"text"` + CreatedAt Timestamp `json:"created_at"` +} diff --git a/dto/create_user.go b/dto/create_user.go new file mode 100644 index 00000000..af41015a --- /dev/null +++ b/dto/create_user.go @@ -0,0 +1,14 @@ +package dto + +type CreateUserRequest struct { + Password string `json:"password"` + Username string `json:"username"` + DisplayName string `json:"display_name"` +} + +type CreateUserResponse struct { + ID string `json:"id"` + Username string `json:"username"` + DisplayName string `json:"display_name"` + CreatedAt Timestamp `json:"created_at"` +} diff --git a/dto/current_user.go b/dto/current_user.go new file mode 100644 index 00000000..b82a032d --- /dev/null +++ b/dto/current_user.go @@ -0,0 +1,10 @@ +package dto + +type CurrentUserRequest struct{} + +type CurrentUserResponse struct { + ID string `json:"id"` + Username string `json:"username"` + DisplayName string `json:"display_name"` + CreatedAt Timestamp `json:"created_at"` +} diff --git a/dto/error.go b/dto/error.go new file mode 100644 index 00000000..c8c17c4d --- /dev/null +++ b/dto/error.go @@ -0,0 +1,11 @@ +package dto + +type ErrorResponse struct { + Error ErrorDetails `json:"error"` +} + +type ErrorDetails struct { + Code string `json:"code"` + Message string `json:"message"` + RequestID string `json:"request_id"` +} diff --git a/dto/get_chat.go b/dto/get_chat.go new file mode 100644 index 00000000..6ed9e1ef --- /dev/null +++ b/dto/get_chat.go @@ -0,0 +1,12 @@ +package dto + +type GetChatRequest struct { + ChatID string `json:"-"` +} + +type GetChatResponse struct { + ID string `json:"id"` + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` + CreatedAt Timestamp `json:"created_at"` +} diff --git a/dto/list_chats.go b/dto/list_chats.go new file mode 100644 index 00000000..43f9eb16 --- /dev/null +++ b/dto/list_chats.go @@ -0,0 +1,18 @@ +package dto + +type ListChatsRequest struct { + Limit int `json:"-"` + Cursor string `json:"-"` +} + +type ListChatsResponse struct { + Items []ListChatsItem `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +type ListChatsItem struct { + ID string `json:"id"` + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` + CreatedAt Timestamp `json:"created_at"` +} diff --git a/dto/list_messages.go b/dto/list_messages.go new file mode 100644 index 00000000..a7d82f63 --- /dev/null +++ b/dto/list_messages.go @@ -0,0 +1,20 @@ +package dto + +type ListMessagesRequest struct { + ChatID string `json:"-"` + Limit int `json:"-"` + Cursor string `json:"-"` +} + +type ListMessagesResponse struct { + Items []ListMessagesItem `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +type ListMessagesItem struct { + ID string `json:"id"` + ChatID string `json:"chat_id"` + SenderID string `json:"sender_id"` + Text string `json:"text"` + CreatedAt Timestamp `json:"created_at"` +} diff --git a/dto/liveness.go b/dto/liveness.go new file mode 100644 index 00000000..830955e9 --- /dev/null +++ b/dto/liveness.go @@ -0,0 +1,7 @@ +package dto + +type LivenessRequest struct{} + +type LivenessResponse struct { + Status string `json:"status"` +} diff --git a/dto/readiness.go b/dto/readiness.go new file mode 100644 index 00000000..21573b78 --- /dev/null +++ b/dto/readiness.go @@ -0,0 +1,7 @@ +package dto + +type ReadinessRequest struct{} + +type ReadinessResponse struct { + Status string `json:"status"` +} diff --git a/dto/session.go b/dto/session.go new file mode 100644 index 00000000..bd2c8807 --- /dev/null +++ b/dto/session.go @@ -0,0 +1,8 @@ +package dto + +type CreateSessionResponse struct { + Token string `json:"token"` + TokenType string `json:"token_type"` + ExpiresAt Timestamp `json:"expires_at"` + User CreateUserResponse `json:"user"` +} diff --git a/dto/timestamp.go b/dto/timestamp.go new file mode 100644 index 00000000..9dab3e29 --- /dev/null +++ b/dto/timestamp.go @@ -0,0 +1,11 @@ +package dto + +import "time" + +// Timestamp preserves PostgreSQL microseconds and is readable by Python 3.9 clients. +// time.Time's variable-length fractional part is not supported by those clients. +type Timestamp time.Time + +func (t Timestamp) MarshalJSON() ([]byte, error) { + return []byte(`"` + time.Time(t).UTC().Format("2006-01-02T15:04:05.000000Z") + `"`), nil +} diff --git a/dto/timestamp_test.go b/dto/timestamp_test.go new file mode 100644 index 00000000..1b48033a --- /dev/null +++ b/dto/timestamp_test.go @@ -0,0 +1,15 @@ +package dto + +import ( + "encoding/json" + "testing" + "time" +) + +func TestTimestampFixedMicroseconds(t *testing.T) { + timestamp := Timestamp(time.Date(2026, 1, 2, 3, 4, 5, 123400000, time.UTC)) + encoded, err := json.Marshal(timestamp) + if err != nil || string(encoded) != `"2026-01-02T03:04:05.123400Z"` { + t.Fatalf("timestamp=%s err=%v", encoded, err) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..f5cadee8 --- /dev/null +++ b/go.mod @@ -0,0 +1,31 @@ +module gitflic.ru/akselbant/mtusi2026lab1-fork + +go 1.26.4 + +require ( + github.com/google/uuid v1.6.0 + github.com/jackc/pgx/v5 v5.7.6 + github.com/labstack/echo/v4 v4.13.4 + github.com/redis/go-redis/v9 v9.12.1 + github.com/swaggest/swgui v1.8.9 + golang.org/x/crypto v0.38.0 +) + +require ( + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect + github.com/labstack/gommon v0.4.2 // indirect + github.com/mattn/go-colorable v0.1.14 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasttemplate v1.2.2 // indirect + github.com/vearutop/statigz v1.4.0 // indirect + golang.org/x/net v0.40.0 // indirect + golang.org/x/sync v0.14.0 // indirect + golang.org/x/sys v0.33.0 // indirect + golang.org/x/text v0.25.0 // indirect + golang.org/x/time v0.11.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..bbb344c1 --- /dev/null +++ b/go.sum @@ -0,0 +1,67 @@ +github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= +github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/bool64/dev v0.2.45 h1:3nLKhAS/6Oklk3Mt2lHYSN/Cb4tdAD77KLwzeP+6eYE= +github.com/bool64/dev v0.2.45/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg= +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.7.6 h1:rWQc5FwZSPX58r1OQmkuaNicxdmExaEz5A2DO2hUuTk= +github.com/jackc/pgx/v5 v5.7.6/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/labstack/echo/v4 v4.13.4 h1:oTZZW+T3s9gAu5L8vmzihV7/lkXGZuITzTQkTEhcXEA= +github.com/labstack/echo/v4 v4.13.4/go.mod h1:g63b33BZ5vZzcIUF8AtRH40DrTlXnx4UMC8rBdndmjQ= +github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= +github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= +github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= +github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/redis/go-redis/v9 v9.12.1 h1:k5iquqv27aBtnTm2tIkROUDp8JBXhXZIVu1InSgvovg= +github.com/redis/go-redis/v9 v9.12.1/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/swaggest/swgui v1.8.9 h1:cxAgIwouPpZPlvX68jY5fpwarzLbkc8/IL6DMj+H460= +github.com/swaggest/swgui v1.8.9/go.mod h1:eTJfgwudbyw9xMwqO26vs82ei2u6//JnUAofx2vGB3M= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= +github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= +github.com/vearutop/statigz v1.4.0 h1:RQL0KG3j/uyA/PFpHeZ/L6l2ta920/MxlOAIGEOuwmU= +github.com/vearutop/statigz v1.4.0/go.mod h1:LYTolBLiz9oJISwiVKnOQoIwhO1LWX1A7OECawGS8XE= +golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8= +golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw= +golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY= +golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds= +golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ= +golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= +golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4= +golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA= +golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0= +golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/infra/README.md b/infra/README.md index 5a39e862..cb4f6fa9 100644 --- a/infra/README.md +++ b/infra/README.md @@ -1,5 +1,9 @@ # Инфраструктура студента +Из lab3 перенесены HAProxy, Redis AOF и скрипт запуска goose. +Основной стенд использует `stack.yaml`: HAProxy → две API-реплики → PostgreSQL и Redis. +Инструкция запуска находится в [PORTING.md](../PORTING.md). S3 пока не добавлено. + Здесь разместите конфигурации выбранных сервисов; основную топологию — в корневом compose.yaml/stack.yaml. `compose.example.yaml` — только worksheet ролей и намеренно не запускается. `contracts/infrastructure.json` задаёт минимальные роли, не готовые образы/пароли. В REPORT.md сопоставьте роль → service name → версия/digest → health/readiness → volume → сеть/порт → способ передачи credentials. Приведите bootstrap (schema, bucket, broker topology, а с lab6 provisioning) и безопасное повторение этих команд. diff --git a/infra/haproxy.cfg b/infra/haproxy.cfg new file mode 100644 index 00000000..3c378e8f --- /dev/null +++ b/infra/haproxy.cfg @@ -0,0 +1,36 @@ +global + log stdout format raw local0 + maxconn 1024 + +defaults + log global + mode http + timeout connect 1s + timeout client 20s + timeout server 20s + timeout check 1s + retries 0 + option http-server-close + +resolvers docker + nameserver dns 127.0.0.11:53 + resolve_retries 3 + timeout resolve 1s + timeout retry 1s + hold valid 1s + hold obsolete 1s + hold nx 1s + hold refused 1s + hold timeout 1s + +frontend public + bind :8080 + default_backend api + +backend api + balance roundrobin + option httpchk + http-check send meth GET uri /health/ready ver HTTP/1.1 hdr Host api + http-check expect status 200 + default-server inter 1s fastinter 500ms downinter 1s fall 2 rise 1 + server-template replica 1-8 tasks.api:8080 check resolvers docker init-addr none diff --git a/infra/migrate.sh b/infra/migrate.sh new file mode 100755 index 00000000..70a11b2f --- /dev/null +++ b/infra/migrate.sh @@ -0,0 +1,8 @@ +#!/bin/sh +set -eu +# Keep the password out of argv and disable automatic dotenv loading. +if [ -n "${POSTGRES_PASSWORD_FILE:-}" ]; then + PGPASSWORD=$(cat "$POSTGRES_PASSWORD_FILE") + export PGPASSWORD +fi +exec goose -env /dev/null -dir /migrations -timeout 30s "$@" diff --git a/infra/redis.conf b/infra/redis.conf new file mode 100644 index 00000000..c9e470c6 --- /dev/null +++ b/infra/redis.conf @@ -0,0 +1,9 @@ +bind 0.0.0.0 +protected-mode no +port 6379 +dir /data +appendonly yes +appendfsync always +save "" +maxmemory 64mb +maxmemory-policy noeviction diff --git a/infra/schema.md b/infra/schema.md new file mode 100644 index 00000000..9b214069 --- /dev/null +++ b/infra/schema.md @@ -0,0 +1,25 @@ +# Схема и транзакционные границы + +PostgreSQL — источник истины пользователей, чатов, участников и сообщений. +Redis — источник истины сессий: SHA-256 токена → user_id, expires_at; TTL до +абсолютного expires_at, без продления при чтении. Logout удаляет ключ. +Redis использует AOF с appendfsync always и отдельный именованный volume. + +- users: UUID PK, UNIQUE(username), password_hash (Argon2id), display_name, created_at. +- chats: UUID PK, title, created_at; индекс (created_at, id). +- chat_members: PK(chat_id, user_id), FK к chats/users; ordinal сохраняет порядок + участников; UNIQUE(chat_id, ordinal); индекс (user_id, chat_id). +- messages: UUID PK, FK chat_id/sender_id через членство; text, created_at. + Индекс (chat_id, created_at, id) отвечает равенству по чату и keyset-предикату + (created_at, id) > ($timestamp, $id), ORDER BY created_at, id LIMIT $limit. +- goose_db_version: журнал goose CLI; SQL-миграция и запись её версии + коммитятся в одной транзакции. Сервис migrations завершается до старта API. + +Регистрация — один INSERT, гонка разрешается UNIQUE, проигравшие получают 409. +Создание чата — одна транзакция: INSERT чата, затем участников; любой неверный +FK откатывает всю операцию, включая уже вставленных участников. +Отправка сообщения — один INSERT с FK на членство. Чтения идут в PostgreSQL, +кэша истории нет, подтверждённый COMMIT виден следующему запросу. +При конкурентной записи keyset не даёт snapshot: поздний COMMIT с ключом до +уже прочитанного cursor может быть пропущен. created_at задаёт PostgreSQL, +UUID разрешает совпадения времени; часы не используются как уникальный ID. diff --git a/internal/app/chat/access.go b/internal/app/chat/access.go new file mode 100644 index 00000000..36f71a5b --- /dev/null +++ b/internal/app/chat/access.go @@ -0,0 +1,31 @@ +package chat + +import ( + "errors" + + "github.com/labstack/echo/v4" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" +) + +func (h *Chat) authorizedChat(c echo.Context, requestedChat domain.Chat) (domain.Chat, bool, error) { + chat, err := h.useCase.GetChat(c.Request().Context(), authenticatedUser(c).ID, requestedChat) + if err != nil { + return domain.Chat{}, false, h.useCaseError(c, err) + } + return chat, true, nil +} + +func (h *Chat) cursorPosition(raw string, present bool, scope, owner string) (*domain.Position, error) { + if !present { + return nil, nil + } + if raw == "" { + return nil, errors.New("empty cursor") + } + createdAt, id, err := h.cursor.decode(raw, scope, owner) + if err != nil { + return nil, err + } + return &domain.Position{CreatedAt: createdAt, ID: id}, nil +} diff --git a/internal/app/chat/create_chat.go b/internal/app/chat/create_chat.go new file mode 100644 index 00000000..7e57b2e5 --- /dev/null +++ b/internal/app/chat/create_chat.go @@ -0,0 +1,33 @@ +package chat + +import ( + "net/http" + + "github.com/labstack/echo/v4" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/dto" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" +) + +func (h *Chat) createChat(c echo.Context) error { + var request dto.CreateChatRequest + if err := decodeJSON(c, &request); err != nil { + return h.jsonError(c, err) + } + callerID := authenticatedUser(c).ID + chat, err := h.useCase.CreateChat(c.Request().Context(), callerID, createChatRequestToDomain(request)) + if err != nil { + return h.useCaseError(c, err) + } + return c.JSON(http.StatusCreated, createChatResponseFromDomain(chat)) +} + +func createChatRequestToDomain(request dto.CreateChatRequest) domain.Chat { + return domain.Chat{Title: request.Title, MemberIDs: request.MemberIDs} +} + +func createChatResponseFromDomain(chat domain.Chat) dto.CreateChatResponse { + return dto.CreateChatResponse{ + ID: chat.ID, Title: chat.Title, MemberIDs: chat.MemberIDs, CreatedAt: dto.Timestamp(chat.CreatedAt), + } +} diff --git a/internal/app/chat/create_message.go b/internal/app/chat/create_message.go new file mode 100644 index 00000000..3fcdebfe --- /dev/null +++ b/internal/app/chat/create_message.go @@ -0,0 +1,51 @@ +package chat + +import ( + "net/http" + "regexp" + + "github.com/labstack/echo/v4" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/dto" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" +) + +var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`) + +func (h *Chat) createMessage(c echo.Context) error { + request := dto.CreateMessageRequest{ChatID: c.Param("chat_id")} + chat, ok, err := h.authorizedChat(c, domain.Chat{ID: request.ChatID}) + if err != nil { + return err + } + if !ok { + return nil + } + if err := decodeJSON(c, &request); err != nil { + return h.jsonError(c, err) + } + request.ChatID = chat.ID + + keys := c.Request().Header.Values("Idempotency-Key") + if len(keys) != 1 || !idempotencyKeyPattern.MatchString(keys[0]) { + return h.invalid(c, "Требуется Idempotency-Key: ASCII [A-Za-z0-9._:-]{1,128}") + } + input := createMessageRequestToDomain(request) + input.IdempotencyKey = keys[0] + message, err := h.useCase.CreateMessage(c.Request().Context(), authenticatedUser(c).ID, input) + if err != nil { + return h.useCaseError(c, err) + } + return c.JSON(http.StatusCreated, createMessageResponseFromDomain(message)) +} + +func createMessageRequestToDomain(request dto.CreateMessageRequest) domain.Message { + return domain.Message{ChatID: request.ChatID, Text: request.Text} +} + +func createMessageResponseFromDomain(message domain.Message) dto.CreateMessageResponse { + return dto.CreateMessageResponse{ + ID: message.ID, ChatID: message.ChatID, SenderID: message.SenderID, + Text: message.Text, CreatedAt: dto.Timestamp(message.CreatedAt), + } +} diff --git a/internal/app/chat/create_user.go b/internal/app/chat/create_user.go new file mode 100644 index 00000000..f6078178 --- /dev/null +++ b/internal/app/chat/create_user.go @@ -0,0 +1,32 @@ +package chat + +import ( + "net/http" + + "github.com/labstack/echo/v4" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/dto" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" +) + +func (h *Chat) createUser(c echo.Context) error { + var request dto.CreateUserRequest + if err := decodeJSON(c, &request); err != nil { + return h.jsonError(c, err) + } + user, err := h.useCase.CreateUser(c.Request().Context(), createUserRequestToDomain(request), request.Password) + if err != nil { + return h.useCaseError(c, err) + } + return c.JSON(http.StatusCreated, createUserResponseFromDomain(user)) +} + +func createUserRequestToDomain(request dto.CreateUserRequest) domain.User { + return domain.User{Username: request.Username, DisplayName: request.DisplayName} +} + +func createUserResponseFromDomain(user domain.User) dto.CreateUserResponse { + return dto.CreateUserResponse{ + ID: user.ID, Username: user.Username, DisplayName: user.DisplayName, CreatedAt: dto.Timestamp(user.CreatedAt), + } +} diff --git a/internal/app/chat/current_user.go b/internal/app/chat/current_user.go new file mode 100644 index 00000000..23277dea --- /dev/null +++ b/internal/app/chat/current_user.go @@ -0,0 +1,21 @@ +package chat + +import ( + "net/http" + + "github.com/labstack/echo/v4" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/dto" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" +) + +func (h *Chat) currentUser(c echo.Context) error { + user := authenticatedUser(c) + return c.JSON(http.StatusOK, currentUserResponseFromDomain(user)) +} + +func currentUserResponseFromDomain(user domain.User) dto.CurrentUserResponse { + return dto.CurrentUserResponse{ + ID: user.ID, Username: user.Username, DisplayName: user.DisplayName, CreatedAt: dto.Timestamp(user.CreatedAt), + } +} diff --git a/internal/app/chat/cursor.go b/internal/app/chat/cursor.go new file mode 100644 index 00000000..83836f11 --- /dev/null +++ b/internal/app/chat/cursor.go @@ -0,0 +1,75 @@ +package chat + +import ( + "crypto/hmac" + "crypto/sha256" + "encoding/base64" + "encoding/json" + "errors" + "strings" + "time" + + "github.com/google/uuid" +) + +type cursorPayload struct { + Scope string `json:"scope"` + Owner string `json:"owner"` + CreatedAt string `json:"created_at"` + ID string `json:"id"` +} + +type cursorCodec struct { + secret [32]byte +} + +func (c cursorCodec) encode(scope, owner string, createdAt time.Time, id string) string { + payload, err := json.Marshal(cursorPayload{ + Scope: scope, + Owner: owner, + CreatedAt: createdAt.UTC().Format(time.RFC3339Nano), + ID: id, + }) + if err != nil { + panic("cannot encode cursor: " + err.Error()) + } + signature := c.sign(payload) + return base64.RawURLEncoding.EncodeToString(payload) + "." + base64.RawURLEncoding.EncodeToString(signature) +} + +func (c cursorCodec) decode(raw, scope, owner string) (time.Time, string, error) { + parts := strings.Split(raw, ".") + if len(parts) != 2 { + return time.Time{}, "", errors.New("malformed cursor") + } + payload, err := base64.RawURLEncoding.DecodeString(parts[0]) + if err != nil { + return time.Time{}, "", errors.New("malformed cursor") + } + signature, err := base64.RawURLEncoding.DecodeString(parts[1]) + if err != nil || !hmac.Equal(signature, c.sign(payload)) { + return time.Time{}, "", errors.New("invalid cursor signature") + } + var value cursorPayload + if err := json.Unmarshal(payload, &value); err != nil { + return time.Time{}, "", errors.New("malformed cursor payload") + } + if value.Scope != scope || value.Owner != owner { + return time.Time{}, "", errors.New("cursor belongs to another resource") + } + parsedID, err := uuid.Parse(value.ID) + if err != nil || parsedID.String() != value.ID { + return time.Time{}, "", errors.New("malformed cursor id") + } + createdAt, err := time.Parse(time.RFC3339Nano, value.CreatedAt) + if err != nil { + return time.Time{}, "", errors.New("malformed cursor timestamp") + } + return createdAt, value.ID, nil +} + +func (c cursorCodec) sign(payload []byte) []byte { + mac := hmac.New(sha256.New, c.secret[:]) + _, _ = mac.Write(payload) + return mac.Sum(nil) +} diff --git a/internal/app/chat/errors.go b/internal/app/chat/errors.go new file mode 100644 index 00000000..a8bd10b8 --- /dev/null +++ b/internal/app/chat/errors.go @@ -0,0 +1,91 @@ +package chat + +import ( + "errors" + "net/http" + + "github.com/google/uuid" + "github.com/labstack/echo/v4" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/dto" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" +) + +func (h *Chat) invalid(c echo.Context, message string) error { + return h.writeError(c, http.StatusBadRequest, "invalid_request", message) +} + +func (h *Chat) jsonError(c echo.Context, err error) error { + if errors.Is(err, errUnsupportedMediaType) { + return h.writeError(c, http.StatusUnsupportedMediaType, "unsupported_media_type", "Content-Type должен быть application/json") + } + var tooLarge *http.MaxBytesError + if errors.As(err, &tooLarge) { + return h.writeError(c, http.StatusRequestEntityTooLarge, "payload_too_large", "Тело запроса слишком большое") + } + return h.invalid(c, err.Error()) +} + +func (h *Chat) useCaseError(c echo.Context, err error) error { + applicationError, ok := domain.AsApplicationError(err) + if !ok { + return err + } + status := http.StatusInternalServerError + switch applicationError.Code { + case domain.ErrorUnavailable: + status = http.StatusServiceUnavailable + case domain.ErrorInvalid: + status = http.StatusBadRequest + case domain.ErrorUnauthorized: + status = http.StatusUnauthorized + challenge := `Bearer realm="messenger"` + if c.Path() == "/api/v1/auth/sessions" { + challenge = `Basic realm="messenger", charset="UTF-8"` + } + c.Response().Header().Set("WWW-Authenticate", challenge) + case domain.ErrorNotFound: + status = http.StatusNotFound + case domain.ErrorConflict: + status = http.StatusConflict + } + return h.writeError(c, status, string(applicationError.Code), applicationError.Message) +} + +func (h *Chat) writeError(c echo.Context, status int, code, message string) error { + return c.JSON(status, dto.ErrorResponse{Error: dto.ErrorDetails{ + Code: code, + Message: message, + RequestID: requestID(c), + }}) +} + +func (h *Chat) httpErrorHandler(err error, c echo.Context) { + if c.Response().Committed { + return + } + status := http.StatusInternalServerError + code := "internal_error" + message := "Внутренняя ошибка сервера" + var httpError *echo.HTTPError + if errors.As(err, &httpError) { + status = httpError.Code + switch status { + case http.StatusNotFound: + code, message = "not_found", "Объект не найден" + case http.StatusMethodNotAllowed, http.StatusBadRequest: + code, message = "invalid_request", "Некорректный запрос" + } + } + _ = h.writeError(c, status, code, message) +} + +func requestID(c echo.Context) string { + value, _ := c.Get(requestIDKey).(string) + if value == "" { + value = uuid.NewString() + c.Set(requestIDKey, value) + c.Response().Header().Set("X-Request-Id", value) + } + return value +} diff --git a/internal/app/chat/get_chat.go b/internal/app/chat/get_chat.go new file mode 100644 index 00000000..eaddd9eb --- /dev/null +++ b/internal/app/chat/get_chat.go @@ -0,0 +1,32 @@ +package chat + +import ( + "net/http" + + "github.com/labstack/echo/v4" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/dto" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" +) + +func (h *Chat) getChat(c echo.Context) error { + request := dto.GetChatRequest{ChatID: c.Param("chat_id")} + chat, ok, err := h.authorizedChat(c, getChatRequestToDomain(request)) + if err != nil { + return err + } + if !ok { + return nil + } + return c.JSON(http.StatusOK, getChatResponseFromDomain(chat)) +} + +func getChatRequestToDomain(request dto.GetChatRequest) domain.Chat { + return domain.Chat{ID: request.ChatID} +} + +func getChatResponseFromDomain(chat domain.Chat) dto.GetChatResponse { + return dto.GetChatResponse{ + ID: chat.ID, Title: chat.Title, MemberIDs: chat.MemberIDs, CreatedAt: dto.Timestamp(chat.CreatedAt), + } +} diff --git a/internal/app/chat/list_chats.go b/internal/app/chat/list_chats.go new file mode 100644 index 00000000..1a02d5fa --- /dev/null +++ b/internal/app/chat/list_chats.go @@ -0,0 +1,53 @@ +package chat + +import ( + "net/http" + + "github.com/labstack/echo/v4" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/dto" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" +) + +func (h *Chat) listChats(c echo.Context) error { + limit, err := parseLimit(c) + if err != nil { + return h.invalid(c, err.Error()) + } + rawCursor, cursorPresent, err := oneQueryValue(c, "cursor") + if err != nil { + return h.invalid(c, "cursor должен быть указан один раз") + } + request := dto.ListChatsRequest{Limit: limit, Cursor: rawCursor} + userID := authenticatedUser(c).ID + position, err := h.cursorPosition(request.Cursor, cursorPresent, "chats", userID) + if err != nil { + return h.invalid(c, "Некорректный cursor") + } + + page, err := h.useCase.ListChats(c.Request().Context(), userID, listChatsRequestToDomain(request, position)) + if err != nil { + return h.useCaseError(c, err) + } + var next *string + if page.HasMore { + last := page.Items[len(page.Items)-1] + encoded := h.cursor.encode("chats", userID, last.CreatedAt, last.ID) + next = &encoded + } + return c.JSON(http.StatusOK, listChatsResponseFromDomain(page, next)) +} + +func listChatsRequestToDomain(request dto.ListChatsRequest, position *domain.Position) domain.Pagination { + return domain.Pagination{After: position, Limit: request.Limit} +} + +func listChatsResponseFromDomain(page domain.ChatPage, nextCursor *string) dto.ListChatsResponse { + items := make([]dto.ListChatsItem, 0, len(page.Items)) + for _, chat := range page.Items { + items = append(items, dto.ListChatsItem{ + ID: chat.ID, Title: chat.Title, MemberIDs: chat.MemberIDs, CreatedAt: dto.Timestamp(chat.CreatedAt), + }) + } + return dto.ListChatsResponse{Items: items, NextCursor: nextCursor} +} diff --git a/internal/app/chat/list_messages.go b/internal/app/chat/list_messages.go new file mode 100644 index 00000000..05cb861e --- /dev/null +++ b/internal/app/chat/list_messages.go @@ -0,0 +1,70 @@ +package chat + +import ( + "net/http" + + "github.com/labstack/echo/v4" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/dto" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" +) + +func (h *Chat) listMessages(c echo.Context) error { + request := dto.ListMessagesRequest{ChatID: c.Param("chat_id")} + chat, ok, err := h.authorizedChat(c, domain.Chat{ID: request.ChatID}) + if err != nil { + return err + } + if !ok { + return nil + } + limit, err := parseLimit(c) + if err != nil { + return h.invalid(c, err.Error()) + } + rawCursor, cursorPresent, err := oneQueryValue(c, "cursor") + if err != nil { + return h.invalid(c, "cursor должен быть указан один раз") + } + request.ChatID = chat.ID + request.Limit = limit + request.Cursor = rawCursor + position, err := h.cursorPosition(request.Cursor, cursorPresent, "messages", request.ChatID) + if err != nil { + return h.invalid(c, "Некорректный cursor") + } + + page, err := h.useCase.ListMessages( + c.Request().Context(), authenticatedUser(c).ID, + listMessagesChatToDomain(request), listMessagesPaginationToDomain(request, position), + ) + if err != nil { + return h.useCaseError(c, err) + } + var next *string + if page.HasMore { + last := page.Items[len(page.Items)-1] + encoded := h.cursor.encode("messages", request.ChatID, last.CreatedAt, last.ID) + next = &encoded + } + return c.JSON(http.StatusOK, listMessagesResponseFromDomain(page, next)) +} + +func listMessagesChatToDomain(request dto.ListMessagesRequest) domain.Chat { + return domain.Chat{ID: request.ChatID} +} + +func listMessagesPaginationToDomain(request dto.ListMessagesRequest, position *domain.Position) domain.Pagination { + return domain.Pagination{After: position, Limit: request.Limit} +} + +func listMessagesResponseFromDomain(page domain.MessagePage, nextCursor *string) dto.ListMessagesResponse { + items := make([]dto.ListMessagesItem, 0, len(page.Items)) + for _, message := range page.Items { + items = append(items, dto.ListMessagesItem{ + ID: message.ID, ChatID: message.ChatID, SenderID: message.SenderID, + Text: message.Text, CreatedAt: dto.Timestamp(message.CreatedAt), + }) + } + return dto.ListMessagesResponse{Items: items, NextCursor: nextCursor} +} diff --git a/internal/app/chat/liveness.go b/internal/app/chat/liveness.go new file mode 100644 index 00000000..6b6029f9 --- /dev/null +++ b/internal/app/chat/liveness.go @@ -0,0 +1,13 @@ +package chat + +import ( + "net/http" + + "github.com/labstack/echo/v4" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/dto" +) + +func (h *Chat) liveness(c echo.Context) error { + return c.JSON(http.StatusOK, dto.LivenessResponse{Status: "ok"}) +} diff --git a/internal/app/chat/middleware.go b/internal/app/chat/middleware.go new file mode 100644 index 00000000..353b517c --- /dev/null +++ b/internal/app/chat/middleware.go @@ -0,0 +1,43 @@ +package chat + +import ( + "net/http" + + "github.com/google/uuid" + "github.com/labstack/echo/v4" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" +) + +const ( + requestIDKey = "request_id" + userKey = "current_user" +) + +func (h *Chat) requestIDMiddleware(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + requestID := uuid.NewString() + c.Set(requestIDKey, requestID) + c.Response().Header().Set("X-Request-Id", requestID) + c.Response().Header().Set("X-Instance-Id", h.instanceID) + if h.draining.Load() && c.Request().URL.Path != "/health/live" { + return h.writeError(c, http.StatusServiceUnavailable, "unavailable", "Реплика завершает работу") + } + return next(c) + } +} + +func (h *Chat) authenticate(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + user, err := h.useCase.Authenticate(c.Request().Context(), bearerToken(c)) + if err != nil { + return h.useCaseError(c, err) + } + c.Set(userKey, user) + return next(c) + } +} + +func authenticatedUser(c echo.Context) domain.User { + return c.Get(userKey).(domain.User) +} diff --git a/internal/app/chat/readiness.go b/internal/app/chat/readiness.go new file mode 100644 index 00000000..d8ee0644 --- /dev/null +++ b/internal/app/chat/readiness.go @@ -0,0 +1,20 @@ +package chat + +import ( + "context" + "net/http" + "time" + + "github.com/labstack/echo/v4" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/dto" +) + +func (h *Chat) readiness(c echo.Context) error { + ctx, cancel := context.WithTimeout(c.Request().Context(), 2*time.Second) + defer cancel() + if err := h.useCase.Ready(ctx); err != nil { + return h.useCaseError(c, err) + } + return c.JSON(http.StatusOK, dto.ReadinessResponse{Status: "ok"}) +} diff --git a/internal/app/chat/request.go b/internal/app/chat/request.go new file mode 100644 index 00000000..2cb05d4d --- /dev/null +++ b/internal/app/chat/request.go @@ -0,0 +1,74 @@ +package chat + +import ( + "encoding/json" + "errors" + "fmt" + "io" + "mime" + "net/http" + "strconv" + + "github.com/labstack/echo/v4" +) + +const ( + defaultLimit = 50 + maxLimit = 100 + maxJSONBody = 1 << 20 +) + +var errUnsupportedMediaType = errors.New("unsupported media type") + +func decodeJSON(c echo.Context, target any) error { + mediaType, _, err := mime.ParseMediaType(c.Request().Header.Get(echo.HeaderContentType)) + if err != nil || mediaType != echo.MIMEApplicationJSON { + return errUnsupportedMediaType + } + reader := http.MaxBytesReader(c.Response(), c.Request().Body, maxJSONBody) + decoder := json.NewDecoder(reader) + decoder.DisallowUnknownFields() + if err := decoder.Decode(target); err != nil { + var tooLarge *http.MaxBytesError + if errors.As(err, &tooLarge) { + return err + } + return errors.New("Некорректное JSON-тело") + } + var extra any + err = decoder.Decode(&extra) + var tooLarge *http.MaxBytesError + if errors.As(err, &tooLarge) { + return err + } + if !errors.Is(err, io.EOF) { + return errors.New("JSON-тело должно содержать ровно один объект") + } + return nil +} + +func parseLimit(c echo.Context) (int, error) { + raw, present, err := oneQueryValue(c, "limit") + if err != nil { + return 0, errors.New("limit должен быть указан один раз") + } + if !present { + return defaultLimit, nil + } + limit, err := strconv.Atoi(raw) + if err != nil || limit < 1 || limit > maxLimit { + return 0, fmt.Errorf("limit должен быть целым числом от 1 до %d", maxLimit) + } + return limit, nil +} + +func oneQueryValue(c echo.Context, name string) (string, bool, error) { + values, present := c.QueryParams()[name] + if !present { + return "", false, nil + } + if len(values) != 1 { + return "", true, errors.New("query parameter must occur once") + } + return values[0], true, nil +} diff --git a/internal/app/chat/service.go b/internal/app/chat/service.go new file mode 100644 index 00000000..5fd2a286 --- /dev/null +++ b/internal/app/chat/service.go @@ -0,0 +1,86 @@ +package chat + +import ( + "log" + "os" + "sync/atomic" + + "github.com/google/uuid" + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" + "github.com/swaggest/swgui/v5emb" +) + +// Chat is the HTTP handler and depends only on the application boundary. +type Chat struct { + instanceID string + draining atomic.Bool + useCase UseCase + cursor cursorCodec +} + +func NewChat(useCase UseCase, cursorKey [32]byte) *Chat { + instanceID := os.Getenv("INSTANCE_ID") + if instanceID == "" { + instanceID = uuid.NewString() + } + return &Chat{useCase: useCase, cursor: cursorCodec{secret: cursorKey}, instanceID: instanceID} +} + +func (h *Chat) Drain() { h.draining.Store(true) } + +func (h *Chat) Router() *echo.Echo { + e := echo.New() + e.HideBanner = true + e.HidePort = true + e.HTTPErrorHandler = h.httpErrorHandler + e.Use(h.requestIDMiddleware) + e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{ + HandleError: true, + LogLatency: true, + LogMethod: true, + LogRoutePath: true, + LogURIPath: true, + LogRequestID: true, + LogStatus: true, + LogResponseSize: true, + LogValuesFunc: func(_ echo.Context, values middleware.RequestLoggerValues) error { + path := values.RoutePath + if path == "" { + path = values.URIPath + } + log.Printf( + "request_id=%s method=%s path=%s status=%d response_bytes=%d latency=%s", + values.RequestID, values.Method, path, values.Status, values.ResponseSize, values.Latency, + ) + return nil + }, + })) + e.Use(middleware.Recover()) + + e.GET("/health/live", h.liveness) + e.GET("/health/ready", h.readiness) + swaggerUI := echo.WrapHandler(v5emb.New( + "MTUSI Messenger", + "/swagger/openapi.json", + "/swagger/", + )) + e.GET("/swagger", h.swagger) + e.GET("/swagger/", swaggerUI) + e.GET("/swagger/openapi.json", h.swaggerSpec) + e.GET("/swagger/*", swaggerUI) + e.POST("/api/v1/users", h.createUser) + + e.POST("/api/v1/auth/sessions", h.createSession) + + api := e.Group("/api/v1", h.authenticate) + api.GET("/users/me", h.currentUser) + api.DELETE("/auth/sessions/current", h.deleteSession) + api.POST("/chats", h.createChat) + api.GET("/chats", h.listChats) + api.GET("/chats/:chat_id", h.getChat) + api.POST("/chats/:chat_id/messages", h.createMessage) + api.GET("/chats/:chat_id/messages", h.listMessages) + + return e +} diff --git a/internal/app/chat/service_test.go b/internal/app/chat/service_test.go new file mode 100644 index 00000000..bf582c2c --- /dev/null +++ b/internal/app/chat/service_test.go @@ -0,0 +1,353 @@ +package chat_test + +import ( + "bytes" + "context" + "encoding/base64" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "net/url" + "sync" + "testing" + "time" + + "github.com/google/uuid" + + chathandler "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/app/chat" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/repository" + chatusecase "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat" +) + +type testAPI struct { + server *httptest.Server + tokens sync.Map +} + +type userResponse struct { + ID string `json:"id"` +} + +type chatResponse struct { + ID string `json:"id"` +} + +type messageResponse struct { + ID string `json:"id"` + Text string `json:"text"` +} + +type messagePageResponse struct { + Items []messageResponse `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +type errorResponse struct { + Error struct { + Code string `json:"code"` + } `json:"error"` +} + +func newTestAPI(t *testing.T) *testAPI { + t.Helper() + useCase := chatusecase.New(repository.NewMemory(), &testSessions{values: map[string]domain.Session{}}, time.Hour) + server := httptest.NewServer(chathandler.NewChat(useCase, [32]byte{1}).Router()) + t.Cleanup(server.Close) + return &testAPI{server: server} +} + +func (a *testAPI) request(t *testing.T, client *http.Client, method, path, actorID, body string, wantStatus int, target any) { + t.Helper() + request, err := http.NewRequest(method, a.server.URL+path, bytes.NewBufferString(body)) + if err != nil { + t.Fatalf("create request: %v", err) + } + if body != "" { + request.Header.Set("Content-Type", "application/json") + request.Header.Set("Idempotency-Key", uuid.NewString()) + } + if actorID != "" { + token, _ := a.tokens.Load(actorID) + request.Header.Set("Authorization", "Bearer "+token.(string)) + } + response, err := client.Do(request) + if err != nil { + t.Fatalf("%s %s: %v", method, path, err) + } + defer response.Body.Close() + responseBody, err := io.ReadAll(response.Body) + if err != nil { + t.Fatalf("read response: %v", err) + } + if response.StatusCode != wantStatus { + t.Fatalf("%s %s: status=%d, want=%d, body=%s", method, path, response.StatusCode, wantStatus, responseBody) + } + if response.Header.Get("X-Request-Id") == "" { + t.Fatalf("%s %s: missing X-Request-Id", method, path) + } + if target != nil { + if err := json.Unmarshal(responseBody, target); err != nil { + t.Fatalf("decode response %q: %v", responseBody, err) + } + } +} + +func (a *testAPI) createUser(t *testing.T, username string) userResponse { + t.Helper() + var user userResponse + a.request(t, http.DefaultClient, http.MethodPost, "/api/v1/users", "", + fmt.Sprintf(`{"username":%q,"display_name":%q,"password":"test-password-12345"}`, username, username), http.StatusCreated, &user) + req, err := http.NewRequest(http.MethodPost, a.server.URL+"/api/v1/auth/sessions", nil) + if err != nil { + t.Fatal(err) + } + req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":test-password-12345"))) + response, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatal(err) + } + defer response.Body.Close() + var login struct { + Token string `json:"token"` + } + if response.StatusCode != http.StatusCreated { + t.Fatalf("login status: %d", response.StatusCode) + } + if err = json.NewDecoder(response.Body).Decode(&login); err != nil { + t.Fatal(err) + } + a.tokens.Store(user.ID, login.Token) + return user +} + +func (a *testAPI) requestError(t *testing.T, method, path, actorID, body string, wantStatus int, wantCode string) { + t.Helper() + var response errorResponse + a.request(t, http.DefaultClient, method, path, actorID, body, wantStatus, &response) + if response.Error.Code != wantCode { + t.Fatalf("%s %s: error code=%q, want=%q", method, path, response.Error.Code, wantCode) + } +} + +func (a *testAPI) createChat(t *testing.T, ownerID string, memberIDs []string) chatResponse { + t.Helper() + body, err := json.Marshal(map[string]any{"title": "test chat", "member_ids": memberIDs}) + if err != nil { + t.Fatalf("encode chat: %v", err) + } + var chat chatResponse + a.request(t, http.DefaultClient, http.MethodPost, "/api/v1/chats", ownerID, string(body), http.StatusCreated, &chat) + return chat +} + +func (a *testAPI) createMessage(t *testing.T, client *http.Client, actorID, chatID, text string) messageResponse { + t.Helper() + body, err := json.Marshal(map[string]string{"text": text}) + if err != nil { + t.Fatalf("encode message: %v", err) + } + var message messageResponse + a.request(t, client, http.MethodPost, "/api/v1/chats/"+chatID+"/messages", actorID, string(body), http.StatusCreated, &message) + return message +} + +func TestRejectsMalformedJSON(t *testing.T) { + api := newTestAPI(t) + api.requestError(t, http.MethodPost, "/api/v1/users", "", + `{"username":`, http.StatusBadRequest, "invalid_request") +} + +func TestHidesForeignChat(t *testing.T) { + api := newTestAPI(t) + owner := api.createUser(t, "owner_user") + outsider := api.createUser(t, "outsider_user") + chat := api.createChat(t, owner.ID, []string{owner.ID}) + api.requestError(t, http.MethodGet, "/api/v1/chats/"+chat.ID, outsider.ID, + "", http.StatusNotFound, "not_found") +} + +func TestRejectsEmptyMessage(t *testing.T) { + api := newTestAPI(t) + user := api.createUser(t, "empty_message_user") + chat := api.createChat(t, user.ID, []string{user.ID}) + api.requestError(t, http.MethodPost, "/api/v1/chats/"+chat.ID+"/messages", user.ID, + `{"text":" "}`, http.StatusBadRequest, "invalid_request") +} + +func TestRejectsDuplicateUsername(t *testing.T) { + api := newTestAPI(t) + api.createUser(t, "duplicate_user") + api.requestError(t, http.MethodPost, "/api/v1/users", "", + `{"username":"duplicate_user","display_name":"Duplicate","password":"test-password-12345"}`, http.StatusConflict, "conflict") +} + +func TestPaginatesMessagesAcrossMultiplePages(t *testing.T) { + api := newTestAPI(t) + user := api.createUser(t, "pagination_user") + chat := api.createChat(t, user.ID, []string{user.ID}) + for index := 0; index < 7; index++ { + api.createMessage(t, http.DefaultClient, user.ID, chat.ID, fmt.Sprintf("message-%d", index)) + } + + seen := make(map[string]struct{}) + cursor := "" + pageSizes := make([]int, 0, 4) + for { + path := "/api/v1/chats/" + chat.ID + "/messages?limit=2" + if cursor != "" { + path += "&cursor=" + url.QueryEscape(cursor) + } + var page messagePageResponse + api.request(t, http.DefaultClient, http.MethodGet, path, user.ID, "", http.StatusOK, &page) + pageSizes = append(pageSizes, len(page.Items)) + for _, message := range page.Items { + if _, duplicate := seen[message.ID]; duplicate { + t.Fatalf("message %s appeared on multiple pages", message.ID) + } + seen[message.ID] = struct{}{} + } + if page.NextCursor == nil { + break + } + cursor = *page.NextCursor + } + if len(seen) != 7 { + t.Fatalf("received %d unique messages, want 7", len(seen)) + } + wantPageSizes := []int{2, 2, 2, 1} + if fmt.Sprint(pageSizes) != fmt.Sprint(wantPageSizes) { + t.Fatalf("page sizes=%v, want=%v", pageSizes, wantPageSizes) + } +} + +func TestConcurrentMessagesAreUnique(t *testing.T) { + api := newTestAPI(t) + + users := make([]userResponse, 10) + memberIDs := make([]string, 10) + for index := range users { + users[index] = api.createUser(t, fmt.Sprintf("parallel_user_%02d", index)) + memberIDs[index] = users[index].ID + } + chat := api.createChat(t, users[0].ID, memberIDs) + + start := make(chan struct{}) + errors := make(chan error, 10) + var waitGroup sync.WaitGroup + + for clientIndex := 0; clientIndex < 10; clientIndex++ { + waitGroup.Add(1) + go func(clientIndex int) { + defer waitGroup.Done() + client := &http.Client{Transport: &http.Transport{}, Timeout: 5 * time.Second} + defer client.CloseIdleConnections() + <-start + for messageIndex := 0; messageIndex < 10; messageIndex++ { + body, err := json.Marshal(map[string]string{ + "text": fmt.Sprintf("client-%02d-message-%02d", clientIndex, messageIndex), + }) + if err != nil { + errors <- err + return + } + request, err := http.NewRequest(http.MethodPost, + api.server.URL+"/api/v1/chats/"+chat.ID+"/messages", bytes.NewReader(body)) + if err != nil { + errors <- err + return + } + request.Header.Set("Content-Type", "application/json") + request.Header.Set("Idempotency-Key", uuid.NewString()) + token, _ := api.tokens.Load(users[clientIndex].ID) + request.Header.Set("Authorization", "Bearer "+token.(string)) + response, err := client.Do(request) + if err != nil { + errors <- err + return + } + _, readErr := io.Copy(io.Discard, response.Body) + closeErr := response.Body.Close() + if readErr != nil { + errors <- readErr + return + } + if closeErr != nil { + errors <- closeErr + return + } + if response.StatusCode != http.StatusCreated { + errors <- fmt.Errorf("client %d message %d: status=%d", clientIndex, messageIndex, response.StatusCode) + return + } + } + }(clientIndex) + } + close(start) + waitGroup.Wait() + close(errors) + for err := range errors { + if err != nil { + t.Fatal(err) + } + } + + seenIDs := make(map[string]struct{}, 10*10) + seenTexts := make(map[string]struct{}, 10*10) + cursor := "" + for { + path := "/api/v1/chats/" + chat.ID + "/messages?limit=17" + if cursor != "" { + path += "&cursor=" + url.QueryEscape(cursor) + } + var page messagePageResponse + api.request(t, http.DefaultClient, http.MethodGet, path, users[0].ID, "", http.StatusOK, &page) + for _, message := range page.Items { + if _, duplicate := seenIDs[message.ID]; duplicate { + t.Fatalf("duplicate message ID %s", message.ID) + } + if _, duplicate := seenTexts[message.Text]; duplicate { + t.Fatalf("duplicate message text %q", message.Text) + } + seenIDs[message.ID] = struct{}{} + seenTexts[message.Text] = struct{}{} + } + if page.NextCursor == nil { + break + } + cursor = *page.NextCursor + } + if len(seenIDs) != 10*10 || len(seenTexts) != 10*10 { + t.Fatalf("received %d unique IDs and %d unique texts, want %d of each", + len(seenIDs), len(seenTexts), 10*10) + } +} + +// Session fake is only used by HTTP unit tests; the server always uses Redis. +type testSessions struct { + mu sync.Mutex + values map[string]domain.Session +} + +func (s *testSessions) Ready(context.Context) error { return nil } +func (s *testSessions) Put(_ context.Context, key string, value domain.Session) error { + s.mu.Lock() + defer s.mu.Unlock() + s.values[key] = value + return nil +} +func (s *testSessions) Get(_ context.Context, key string) (domain.Session, bool, error) { + s.mu.Lock() + defer s.mu.Unlock() + value, ok := s.values[key] + return value, ok, nil +} +func (s *testSessions) Delete(_ context.Context, key string) (bool, error) { + s.mu.Lock() + defer s.mu.Unlock() + _, ok := s.values[key] + delete(s.values, key) + return ok, nil +} diff --git a/internal/app/chat/session.go b/internal/app/chat/session.go new file mode 100644 index 00000000..29ce755c --- /dev/null +++ b/internal/app/chat/session.go @@ -0,0 +1,45 @@ +package chat + +import ( + "io" + "net/http" + "strings" + + "github.com/labstack/echo/v4" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/dto" +) + +func bearerToken(c echo.Context) string { + parts := strings.Fields(c.Request().Header.Get("Authorization")) + if len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") { + return "" + } + return parts[1] +} + +func (h *Chat) createSession(c echo.Context) error { + username, password, ok := c.Request().BasicAuth() + if !ok { + c.Response().Header().Set("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`) + return h.writeError(c, http.StatusUnauthorized, "unauthorized", "Неверные учётные данные") + } + body, err := io.ReadAll(io.LimitReader(c.Request().Body, 1)) + if err != nil || len(body) != 0 { + return h.invalid(c, "Тело запроса входа должно быть пустым") + } + login, err := h.useCase.CreateSession(c.Request().Context(), username, password) + if err != nil { + return h.useCaseError(c, err) + } + return c.JSON(http.StatusCreated, dto.CreateSessionResponse{ + Token: login.Token, TokenType: "Bearer", ExpiresAt: dto.Timestamp(login.ExpiresAt), User: createUserResponseFromDomain(login.User), + }) +} + +func (h *Chat) deleteSession(c echo.Context) error { + if err := h.useCase.DeleteSession(c.Request().Context(), bearerToken(c)); err != nil { + return h.useCaseError(c, err) + } + return c.NoContent(http.StatusNoContent) +} diff --git a/internal/app/chat/swagger.go b/internal/app/chat/swagger.go new file mode 100644 index 00000000..fc87c65e --- /dev/null +++ b/internal/app/chat/swagger.go @@ -0,0 +1,38 @@ +package chat + +import ( + "encoding/json" + "net/http" + + "github.com/labstack/echo/v4" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/contracts" +) + +var swaggerOpenAPI = makeSwaggerOpenAPI() + +func (h *Chat) swagger(c echo.Context) error { + return c.Redirect(http.StatusTemporaryRedirect, "/swagger/") +} + +func (h *Chat) swaggerSpec(c echo.Context) error { + return c.Blob(http.StatusOK, echo.MIMEApplicationJSON, swaggerOpenAPI) +} + +func makeSwaggerOpenAPI() []byte { + var document map[string]any + if err := json.Unmarshal(contracts.OpenAPI, &document); err != nil { + panic("cannot decode embedded OpenAPI contract: " + err.Error()) + } + // A relative URL keeps Swagger requests on the same scheme, host and port + // as the UI. This also works behind a reverse proxy and on a custom PORT. + document["servers"] = []map[string]string{{ + "url": "/", + "description": "Текущий адрес Swagger UI", + }} + encoded, err := json.Marshal(document) + if err != nil { + panic("cannot encode Swagger OpenAPI contract: " + err.Error()) + } + return encoded +} diff --git a/internal/app/chat/usecase.go b/internal/app/chat/usecase.go new file mode 100644 index 00000000..811a6ef0 --- /dev/null +++ b/internal/app/chat/usecase.go @@ -0,0 +1,21 @@ +package chat + +import ( + "context" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" +) + +// UseCase is the application boundary required by the HTTP handler. +type UseCase interface { + Ready(context.Context) error + CreateSession(context.Context, string, string) (domain.Login, error) + DeleteSession(context.Context, string) error + Authenticate(ctx context.Context, token string) (domain.User, error) + CreateUser(ctx context.Context, user domain.User, password string) (domain.User, error) + CreateChat(ctx context.Context, callerID string, chat domain.Chat) (domain.Chat, error) + GetChat(ctx context.Context, callerID string, chat domain.Chat) (domain.Chat, error) + ListChats(ctx context.Context, callerID string, pagination domain.Pagination) (domain.ChatPage, error) + CreateMessage(ctx context.Context, callerID string, message domain.Message) (domain.Message, error) + ListMessages(ctx context.Context, callerID string, chat domain.Chat, pagination domain.Pagination) (domain.MessagePage, error) +} diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 00000000..84b4dcb7 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,100 @@ +package config + +import ( + "encoding/hex" + "fmt" + "os" + "strconv" + "strings" + "time" +) + +type Config struct { + Port string + PostgresHost string + PostgresPort uint16 + PostgresUser string + PostgresDatabase string + PostgresPassword string + PostgresMaxConns int32 + RedisAddr string + SessionTTL time.Duration + QueryTimeout time.Duration + CursorKey [32]byte +} + +func value(name, fallback string) string { + if v := os.Getenv(name); v != "" { + return v + } + return fallback +} +func positive(name string, fallback, max int) (int, error) { + n, err := strconv.Atoi(value(name, strconv.Itoa(fallback))) + if err != nil || n < 1 || n > max { + return 0, fmt.Errorf("invalid %s", name) + } + return n, nil +} +func secret(name string) (string, error) { + if file := os.Getenv(name + "_FILE"); file != "" { + data, err := os.ReadFile(file) + if err != nil { + return "", fmt.Errorf("cannot read %s_FILE", name) + } + if s := strings.TrimSpace(string(data)); s != "" { + return s, nil + } + } + if s := os.Getenv(name); s != "" { + return s, nil + } + return "", fmt.Errorf("%s or %s_FILE is required", name, name) +} + +func Load() (Config, error) { + var c Config + port, err := positive("PORT", 8080, 65535) + if err != nil { + return c, err + } + c.Port = strconv.Itoa(port) + pgPort, err := positive("POSTGRES_PORT", 5432, 65535) + if err != nil { + return c, err + } + c.PostgresPort = uint16(pgPort) + max, err := positive("POSTGRES_MAX_CONNS", 10, 100) + if err != nil { + return c, err + } + c.PostgresMaxConns = int32(max) + ttl, err := positive("SESSION_TTL_SECONDS", 3600, 86400*30) + if err != nil { + return c, err + } + c.SessionTTL = time.Duration(ttl) * time.Second + timeout, err := positive("QUERY_TIMEOUT_MS", 2000, 10000) + if err != nil { + return c, err + } + c.QueryTimeout = time.Duration(timeout) * time.Millisecond + c.PostgresHost = value("POSTGRES_HOST", "localhost") + c.PostgresUser = value("POSTGRES_USER", "messenger") + c.PostgresDatabase = value("POSTGRES_DB", "messenger") + c.RedisAddr = value("REDIS_ADDR", "localhost:6379") + c.PostgresPassword, err = secret("POSTGRES_PASSWORD") + if err != nil { + return c, err + } + key, err := secret("CURSOR_KEY") + if err != nil { + return c, err + } + decoded, err := hex.DecodeString(key) + if err != nil || len(decoded) != 32 { + return c, fmt.Errorf("CURSOR_KEY must be 32 random bytes encoded as hex") + } + copy(c.CursorKey[:], decoded) + return c, nil +} diff --git a/internal/pkg/domain/chat.go b/internal/pkg/domain/chat.go new file mode 100644 index 00000000..2de7174e --- /dev/null +++ b/internal/pkg/domain/chat.go @@ -0,0 +1,10 @@ +package domain + +import "time" + +type Chat struct { + ID string + Title string + MemberIDs []string + CreatedAt time.Time +} diff --git a/internal/pkg/domain/errors.go b/internal/pkg/domain/errors.go new file mode 100644 index 00000000..a8447d22 --- /dev/null +++ b/internal/pkg/domain/errors.go @@ -0,0 +1,32 @@ +package domain + +import "errors" + +type ErrorCode string + +const ( + ErrorUnavailable ErrorCode = "unavailable" + ErrorInvalid ErrorCode = "invalid_request" + ErrorUnauthorized ErrorCode = "unauthorized" + ErrorNotFound ErrorCode = "not_found" + ErrorConflict ErrorCode = "conflict" +) + +type ApplicationError struct { + Code ErrorCode + Message string +} + +func (e *ApplicationError) Error() string { + return e.Message +} + +func NewApplicationError(code ErrorCode, message string) error { + return &ApplicationError{Code: code, Message: message} +} + +func AsApplicationError(err error) (*ApplicationError, bool) { + var applicationError *ApplicationError + ok := errors.As(err, &applicationError) + return applicationError, ok +} diff --git a/internal/pkg/domain/message.go b/internal/pkg/domain/message.go new file mode 100644 index 00000000..41b93ace --- /dev/null +++ b/internal/pkg/domain/message.go @@ -0,0 +1,12 @@ +package domain + +import "time" + +type Message struct { + IdempotencyKey string + ID string + ChatID string + SenderID string + Text string + CreatedAt time.Time +} diff --git a/internal/pkg/domain/pagination.go b/internal/pkg/domain/pagination.go new file mode 100644 index 00000000..d3ab68f4 --- /dev/null +++ b/internal/pkg/domain/pagination.go @@ -0,0 +1,23 @@ +package domain + +import "time" + +type Position struct { + CreatedAt time.Time + ID string +} + +type Pagination struct { + After *Position + Limit int +} + +type ChatPage struct { + Items []Chat + HasMore bool +} + +type MessagePage struct { + Items []Message + HasMore bool +} diff --git a/internal/pkg/domain/session.go b/internal/pkg/domain/session.go new file mode 100644 index 00000000..50029b96 --- /dev/null +++ b/internal/pkg/domain/session.go @@ -0,0 +1,14 @@ +package domain + +import "time" + +type Session struct { + UserID string `json:"user_id"` + ExpiresAt time.Time `json:"expires_at"` +} + +type Login struct { + Token string + ExpiresAt time.Time + User User +} diff --git a/internal/pkg/domain/user.go b/internal/pkg/domain/user.go new file mode 100644 index 00000000..34d52353 --- /dev/null +++ b/internal/pkg/domain/user.go @@ -0,0 +1,10 @@ +package domain + +import "time" + +type User struct { + ID string + Username string + DisplayName string + CreatedAt time.Time +} diff --git a/internal/pkg/password/password.go b/internal/pkg/password/password.go new file mode 100644 index 00000000..1280dca7 --- /dev/null +++ b/internal/pkg/password/password.go @@ -0,0 +1,65 @@ +package password + +import ( + "context" + "crypto/rand" + "crypto/subtle" + "encoding/base64" + "errors" + "strings" + + "golang.org/x/crypto/argon2" +) + +// Bound simultaneous KDF work to 4 * 19 MiB, including concurrent registrations. +var slots = make(chan struct{}, 4) + +const prefix = "$argon2id$v=19$m=19456,t=2,p=1$" + +func derive(ctx context.Context, password string, salt []byte) ([]byte, error) { + select { + case slots <- struct{}{}: + case <-ctx.Done(): + return nil, ctx.Err() + } + defer func() { <-slots }() + if err := ctx.Err(); err != nil { + return nil, err + } + return argon2.IDKey([]byte(password), salt, 2, 19456, 1, 32), nil +} + +func Hash(ctx context.Context, password string) (string, error) { + salt := make([]byte, 16) + if _, err := rand.Read(salt); err != nil { + return "", err + } + key, err := derive(ctx, password, salt) + if err != nil { + return "", err + } + return prefix + base64.RawStdEncoding.EncodeToString(salt) + "$" + base64.RawStdEncoding.EncodeToString(key), nil +} + +func Verify(ctx context.Context, password, encoded string) (bool, error) { + if !strings.HasPrefix(encoded, prefix) { + return false, errors.New("unsupported password hash") + } + parts := strings.Split(strings.TrimPrefix(encoded, prefix), "$") + if len(parts) != 2 { + return false, errors.New("invalid password hash") + } + salt, err := base64.RawStdEncoding.DecodeString(parts[0]) + if err != nil || len(salt) != 16 { + return false, errors.New("invalid salt") + } + expected, err := base64.RawStdEncoding.DecodeString(parts[1]) + if err != nil || len(expected) != 32 { + return false, errors.New("invalid hash") + } + actual, err := derive(ctx, password, salt) + return subtle.ConstantTimeCompare(actual, expected) == 1, err +} + +// A valid fixed encoding for equal-cost verification when username does not exist. +const DummyHash = prefix + "AAAAAAAAAAAAAAAAAAAAAA$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" diff --git a/internal/pkg/password/password_test.go b/internal/pkg/password/password_test.go new file mode 100644 index 00000000..6a0ef969 --- /dev/null +++ b/internal/pkg/password/password_test.go @@ -0,0 +1,35 @@ +package password_test + +import ( + "context" + "strings" + "testing" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/password" +) + +func TestUnicodeAndSalt(t *testing.T) { + ctx := context.Background() + secret := strings.Repeat("🔑", 128) + first, err := password.Hash(ctx, secret) + if err != nil { + t.Fatal(err) + } + second, err := password.Hash(ctx, secret) + if err != nil { + t.Fatal(err) + } + if first == second { + t.Fatal("password hashes reused salt") + } + for _, hash := range []string{first, second} { + ok, err := password.Verify(ctx, secret, hash) + if err != nil || !ok { + t.Fatal("128-code-point UTF-8 password rejected") + } + ok, err = password.Verify(ctx, strings.Repeat("🔑", 127)+"x", hash) + if err != nil || ok { + t.Fatal("password suffix was ignored") + } + } +} diff --git a/internal/pkg/repository/memory.go b/internal/pkg/repository/memory.go new file mode 100644 index 00000000..1c269015 --- /dev/null +++ b/internal/pkg/repository/memory.go @@ -0,0 +1,202 @@ +package repository + +import ( + "context" + "sort" + "sync" + "time" + + "github.com/google/uuid" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" + chatusecase "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat" +) + +// Memory is the lab1 in-memory Repository implementation. +type Memory struct { + mu sync.RWMutex + users map[string]domain.User + hashes map[string]string + userByUsername map[string]string + chats map[string]domain.Chat + messages map[string][]domain.Message +} + +var _ chatusecase.Repository = (*Memory)(nil) + +func NewMemory() *Memory { + return &Memory{ + users: make(map[string]domain.User), + hashes: make(map[string]string), + userByUsername: make(map[string]string), + chats: make(map[string]domain.Chat), + messages: make(map[string][]domain.Message), + } +} + +func (m *Memory) CreateUser(_ context.Context, username, displayName, hash string) (domain.User, error) { + m.mu.Lock() + defer m.mu.Unlock() + + if _, exists := m.userByUsername[username]; exists { + return domain.User{}, chatusecase.ErrUsernameTaken + } + user := domain.User{ + ID: uuid.NewString(), + Username: username, + DisplayName: displayName, + CreatedAt: now(), + } + m.users[user.ID] = user + m.hashes[user.ID] = hash + m.userByUsername[user.Username] = user.ID + return user, nil +} + +func (m *Memory) User(_ context.Context, id string) (domain.User, bool, error) { + m.mu.RLock() + defer m.mu.RUnlock() + user, ok := m.users[id] + return user, ok, nil +} + +func (m *Memory) CreateChat(_ context.Context, title string, memberIDs []string) (domain.Chat, error) { + m.mu.Lock() + defer m.mu.Unlock() + + for _, memberID := range memberIDs { + if _, exists := m.users[memberID]; !exists { + return domain.Chat{}, chatusecase.ErrUserNotFound + } + } + chat := domain.Chat{ + ID: uuid.NewString(), + Title: title, + MemberIDs: append([]string(nil), memberIDs...), + CreatedAt: now(), + } + m.chats[chat.ID] = chat + return cloneChat(chat), nil +} + +func (m *Memory) ChatForUser(_ context.Context, chatID, userID string) (domain.Chat, error) { + m.mu.RLock() + defer m.mu.RUnlock() + chat, exists := m.chats[chatID] + if !exists { + return domain.Chat{}, chatusecase.ErrChatNotFound + } + if !contains(chat.MemberIDs, userID) { + return domain.Chat{}, chatusecase.ErrNotMember + } + return cloneChat(chat), nil +} + +func (m *Memory) ListChats(_ context.Context, userID string, after *domain.Position, limit int) ([]domain.Chat, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + result := make([]domain.Chat, 0) + for _, chat := range m.chats { + if contains(chat.MemberIDs, userID) && isAfter(chat.CreatedAt, chat.ID, after) { + result = append(result, cloneChat(chat)) + } + } + sort.Slice(result, func(i, j int) bool { + return less(result[i].CreatedAt, result[i].ID, result[j].CreatedAt, result[j].ID) + }) + if len(result) > limit { + result = result[:limit] + } + return result, nil +} + +func (m *Memory) CreateMessage(_ context.Context, chatID, senderID, text, key string) (domain.Message, error) { + m.mu.Lock() + defer m.mu.Unlock() + + chat, exists := m.chats[chatID] + if !exists { + return domain.Message{}, chatusecase.ErrChatNotFound + } + if !contains(chat.MemberIDs, senderID) { + return domain.Message{}, chatusecase.ErrNotMember + } + if key != "" { + for _, previous := range m.messages[chatID] { + if previous.SenderID == senderID && previous.IdempotencyKey == key { + if previous.Text != text { + return domain.Message{}, domain.NewApplicationError(domain.ErrorConflict, "Idempotency-Key уже использован с другим телом") + } + return previous, nil + } + } + } + message := domain.Message{ + IdempotencyKey: key, + ID: uuid.NewString(), + ChatID: chatID, + SenderID: senderID, + Text: text, + CreatedAt: now(), + } + m.messages[chatID] = append(m.messages[chatID], message) + return message, nil +} + +func (m *Memory) ListMessages(_ context.Context, chatID string, after *domain.Position, limit int) ([]domain.Message, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + values := m.messages[chatID] + result := make([]domain.Message, 0, min(len(values), limit)) + for _, message := range values { + if isAfter(message.CreatedAt, message.ID, after) { + result = append(result, message) + } + } + sort.Slice(result, func(i, j int) bool { + return less(result[i].CreatedAt, result[i].ID, result[j].CreatedAt, result[j].ID) + }) + if len(result) > limit { + result = result[:limit] + } + return result, nil +} + +func cloneChat(chat domain.Chat) domain.Chat { + chat.MemberIDs = append([]string(nil), chat.MemberIDs...) + return chat +} + +func now() time.Time { + return time.Now().UTC().Truncate(time.Second) +} + +func contains(values []string, target string) bool { + for _, value := range values { + if value == target { + return true + } + } + return false +} + +func isAfter(createdAt time.Time, id string, after *domain.Position) bool { + return after == nil || createdAt.After(after.CreatedAt) || createdAt.Equal(after.CreatedAt) && id > after.ID +} + +func less(leftTime time.Time, leftID string, rightTime time.Time, rightID string) bool { + if leftTime.Equal(rightTime) { + return leftID < rightID + } + return leftTime.Before(rightTime) +} + +func (m *Memory) Ready(context.Context) error { return nil } +func (m *Memory) UserByUsername(_ context.Context, username string) (domain.User, string, bool, error) { + m.mu.RLock() + defer m.mu.RUnlock() + id, ok := m.userByUsername[username] + return m.users[id], m.hashes[id], ok, nil +} diff --git a/internal/pkg/repository/postgres/chats.go b/internal/pkg/repository/postgres/chats.go new file mode 100644 index 00000000..60c9051c --- /dev/null +++ b/internal/pkg/repository/postgres/chats.go @@ -0,0 +1,78 @@ +package postgres + +import ( + "context" + "errors" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" + chatusecase "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat" +) + +func (s *Storage) CreateChat(ctx context.Context, title string, members []string) (domain.Chat, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + tx, err := s.pool.Begin(ctx) + if err != nil { + return domain.Chat{}, dbError(err) + } + defer tx.Rollback(context.WithoutCancel(ctx)) + chat := domain.Chat{ID: uuid.NewString(), Title: title, MemberIDs: append([]string(nil), members...)} + err = tx.QueryRow(ctx, `INSERT INTO chats(id,title) VALUES($1,$2) RETURNING created_at`, chat.ID, title).Scan(&chat.CreatedAt) + if err != nil { + return domain.Chat{}, dbError(err) + } + for i, member := range members { + if _, err = tx.Exec(ctx, `INSERT INTO chat_members(chat_id,user_id,ordinal) VALUES($1,$2,$3)`, chat.ID, member, i); err != nil { + return domain.Chat{}, dbError(err) + } + } + if err = tx.Commit(ctx); err != nil { + return domain.Chat{}, dbError(err) + } + return chat, nil +} + +const chatColumns = `c.id::text,c.title,c.created_at,ARRAY(SELECT m.user_id::text FROM chat_members m WHERE m.chat_id=c.id ORDER BY m.ordinal)` + +func (s *Storage) ChatForUser(ctx context.Context, chatID, userID string) (domain.Chat, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + var chat domain.Chat + err := s.pool.QueryRow(ctx, `SELECT `+chatColumns+` FROM chats c + WHERE c.id=$1 AND EXISTS(SELECT 1 FROM chat_members m WHERE m.chat_id=c.id AND m.user_id=$2)`, chatID, userID). + Scan(&chat.ID, &chat.Title, &chat.CreatedAt, &chat.MemberIDs) + if errors.Is(err, pgx.ErrNoRows) { + return chat, chatusecase.ErrChatNotFound + } + return chat, dbError(err) +} + +func (s *Storage) ListChats(ctx context.Context, userID string, after *domain.Position, limit int) ([]domain.Chat, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + query := `SELECT ` + chatColumns + ` FROM chats c JOIN chat_members member ON member.chat_id=c.id + WHERE member.user_id=$1` + args := []any{userID, limit} + if after != nil { + query += ` AND (c.created_at,c.id)>($3,$4)` + args = append(args, after.CreatedAt, after.ID) + } + query += ` ORDER BY c.created_at,c.id LIMIT $2` + rows, err := s.pool.Query(ctx, query, args...) + if err != nil { + return nil, dbError(err) + } + defer rows.Close() + chats := make([]domain.Chat, 0) + for rows.Next() { + var chat domain.Chat + if err = rows.Scan(&chat.ID, &chat.Title, &chat.CreatedAt, &chat.MemberIDs); err != nil { + return nil, dbError(err) + } + chats = append(chats, chat) + } + return chats, dbError(rows.Err()) +} diff --git a/internal/pkg/repository/postgres/messages.go b/internal/pkg/repository/postgres/messages.go new file mode 100644 index 00000000..ec997624 --- /dev/null +++ b/internal/pkg/repository/postgres/messages.go @@ -0,0 +1,61 @@ +package postgres + +import ( + "context" + "errors" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" +) + +func (s *Storage) CreateMessage(ctx context.Context, chatID, senderID, text, key string) (domain.Message, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + message := domain.Message{ID: uuid.NewString(), ChatID: chatID, SenderID: senderID, Text: text} + var storedKey *string + if key != "" { + storedKey = &key + } + // One row contains both the side effect and its durable replay result. + err := s.pool.QueryRow(ctx, `INSERT INTO messages(id,chat_id,sender_id,text,idempotency_key) + VALUES($1,$2,$3,$4,$5) ON CONFLICT (sender_id,chat_id,idempotency_key) DO NOTHING RETURNING created_at`, + message.ID, chatID, senderID, text, storedKey).Scan(&message.CreatedAt) + if errors.Is(err, pgx.ErrNoRows) { + // A new READ COMMITTED statement sees the winner after the unique-index wait. + err = s.pool.QueryRow(ctx, `SELECT id::text,text,created_at FROM messages + WHERE sender_id=$1 AND chat_id=$2 AND idempotency_key=$3`, senderID, chatID, key). + Scan(&message.ID, &message.Text, &message.CreatedAt) + if err == nil && message.Text != text { + return domain.Message{}, domain.NewApplicationError(domain.ErrorConflict, "Idempotency-Key уже использован с другим телом") + } + } + return message, dbError(err) +} + +func (s *Storage) ListMessages(ctx context.Context, chatID string, after *domain.Position, limit int) ([]domain.Message, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + query := `SELECT m.id::text,m.chat_id::text,m.sender_id::text,m.text,m.created_at FROM messages m WHERE m.chat_id=$1` + args := []any{chatID, limit} + if after != nil { + query += ` AND (m.created_at,m.id)>($3,$4)` + args = append(args, after.CreatedAt, after.ID) + } + query += ` ORDER BY m.created_at,m.id LIMIT $2` + rows, err := s.pool.Query(ctx, query, args...) + if err != nil { + return nil, dbError(err) + } + defer rows.Close() + messages := make([]domain.Message, 0) + for rows.Next() { + var m domain.Message + if err = rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt); err != nil { + return nil, dbError(err) + } + messages = append(messages, m) + } + return messages, dbError(rows.Err()) +} diff --git a/internal/pkg/repository/postgres/storage.go b/internal/pkg/repository/postgres/storage.go new file mode 100644 index 00000000..22a93b14 --- /dev/null +++ b/internal/pkg/repository/postgres/storage.go @@ -0,0 +1,51 @@ +package postgres + +import ( + "context" + "errors" + "log" + "time" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgxpool" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" + chatusecase "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat" +) + +type Storage struct { + pool *pgxpool.Pool + timeout time.Duration +} + +var _ chatusecase.Repository = (*Storage)(nil) + +func New(pool *pgxpool.Pool, timeout time.Duration) *Storage { + return &Storage{pool: pool, timeout: timeout} +} + +func (s *Storage) Ready(ctx context.Context) error { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + return dbError(s.pool.Ping(ctx)) +} + +func dbError(err error) error { + if err == nil { + return nil + } + var pgErr *pgconn.PgError + if errors.As(err, &pgErr) { + switch pgErr.Code { + case "23505": + return chatusecase.ErrUsernameTaken + case "23503": + return chatusecase.ErrUserNotFound + } + log.Printf("postgres operation failed: sqlstate=%s", pgErr.Code) + } else if !errors.Is(err, pgx.ErrNoRows) { + log.Print("postgres operation failed") + } + return domain.NewApplicationError(domain.ErrorUnavailable, "Хранилище недоступно") +} diff --git a/internal/pkg/repository/postgres/storage_test.go b/internal/pkg/repository/postgres/storage_test.go new file mode 100644 index 00000000..78ad5ab9 --- /dev/null +++ b/internal/pkg/repository/postgres/storage_test.go @@ -0,0 +1,282 @@ +package postgres_test + +import ( + "context" + "errors" + "net" + "net/url" + "os" + "os/exec" + "path/filepath" + "strconv" + "strings" + "sync" + "testing" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgxpool" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/config" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/repository/postgres" + chatusecase "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat" +) + +func TestIntegration(t *testing.T) { + if os.Getenv("INTEGRATION_TESTS") != "1" { + t.Skip("run make test-integration against the Compose stack") + } + ctx := context.Background() + cfg, err := config.Load() + if err != nil { + t.Fatal(err) + } + pc, err := pgxpool.ParseConfig("sslmode=disable") + if err != nil { + t.Fatal(err) + } + pc.ConnConfig.Host = cfg.PostgresHost + pc.ConnConfig.Port = cfg.PostgresPort + pc.ConnConfig.User = cfg.PostgresUser + pc.ConnConfig.Database = cfg.PostgresDatabase + pc.ConnConfig.Password = cfg.PostgresPassword + pc.ConnConfig.RuntimeParams["timezone"] = "UTC" + pc.MaxConns = 4 + admin, err := pgxpool.NewWithConfig(ctx, pc.Copy()) + if err != nil { + t.Fatal("connect failed") + } + defer admin.Close() + // Identifier is generated by the test, never from an API request. + schema := "test_" + uuid.New().String()[:8] + if _, err = admin.Exec(ctx, `CREATE SCHEMA `+schema); err != nil { + t.Fatal(err) + } + defer admin.Exec(ctx, `DROP SCHEMA `+schema+` CASCADE`) + pc.ConnConfig.RuntimeParams["search_path"] = schema + pool, err := pgxpool.NewWithConfig(ctx, pc) + if err != nil { + t.Fatal(err) + } + defer pool.Close() + runGoose := func(t *testing.T, command string) { + t.Helper() + migrationDir := os.Getenv("GOOSE_MIGRATION_DIR") + if migrationDir == "" { + migrationDir, err = filepath.Abs("../../../../migrations") + if err != nil { + t.Fatal(err) + } + } + connection := url.URL{Scheme: "postgres", User: url.User(cfg.PostgresUser), Host: net.JoinHostPort(cfg.PostgresHost, strconv.Itoa(int(cfg.PostgresPort))), Path: cfg.PostgresDatabase} + connection.RawQuery = url.Values{"sslmode": {"disable"}, "search_path": {schema}, "connect_timeout": {"5"}}.Encode() + commandCtx, cancel := context.WithTimeout(ctx, 45*time.Second) + defer cancel() + commandExec := exec.CommandContext(commandCtx, "goose", "-env", "/dev/null", "-dir", migrationDir, "-timeout", "30s", command) + commandExec.Env = append(os.Environ(), "GOOSE_DRIVER=postgres", "GOOSE_DBSTRING="+connection.String(), "PGPASSWORD="+cfg.PostgresPassword) + output, err := commandExec.CombinedOutput() + if err != nil { + t.Fatalf("goose %s: %v: %s", command, err, strings.ReplaceAll(string(output), cfg.PostgresPassword, "[redacted]")) + } + t.Logf("goose %s: %s", command, output) + } + runGoose(t, "up") + runGoose(t, "up") + var versions int + if err = pool.QueryRow(ctx, `SELECT count(*) FROM goose_db_version WHERE version_id=1 AND is_applied`).Scan(&versions); err != nil || versions != 1 { + t.Fatalf("migration versions=%d error=%v", versions, err) + } + repo := postgres.New(pool, 2*time.Second) + t.Run("registration_race", func(t *testing.T) { + const count = 12 + results := make(chan error, count) + var wg sync.WaitGroup + start := make(chan struct{}) + for i := 0; i < count; i++ { + wg.Add(1) + go func() { + defer wg.Done() + <-start + _, err := repo.CreateUser(ctx, "same_username", "Same", "test-hash") + results <- err + }() + } + close(start) + wg.Wait() + close(results) + successes, conflicts := 0, 0 + for err := range results { + if err == nil { + successes++ + } else if errors.Is(err, chatusecase.ErrUsernameTaken) { + conflicts++ + } else { + t.Fatal(err) + } + } + if successes != 1 || conflicts != count-1 { + t.Fatalf("successes=%d conflicts=%d", successes, conflicts) + } + t.Logf("12 concurrent INSERTs: %d success, %d unique conflicts", successes, conflicts) + }) + user, err := repo.CreateUser(ctx, "history_owner", "Owner", "test-hash") + if err != nil { + t.Fatal(err) + } + t.Run("rollback", func(t *testing.T) { + _, err := repo.CreateChat(ctx, "must rollback", []string{user.ID, uuid.NewString()}) + if !errors.Is(err, chatusecase.ErrUserNotFound) { + t.Fatalf("unexpected error %v", err) + } + var chats, members int + if err = pool.QueryRow(ctx, `SELECT (SELECT count(*) FROM chats),(SELECT count(*) FROM chat_members)`).Scan(&chats, &members); err != nil { + t.Fatal(err) + } + if chats != 0 || members != 0 { + t.Fatalf("partial transaction: chats=%d members=%d", chats, members) + } + t.Log("failed second member INSERT left 0 chats and 0 members") + }) + chat, err := repo.CreateChat(ctx, "History", []string{user.ID}) + if err != nil { + t.Fatal(err) + } + _, err = pool.Exec(ctx, `INSERT INTO messages(id,chat_id,sender_id,text,created_at) + SELECT md5(i::text)::uuid,$1,$2,'synthetic message '||i, + timestamptz '2026-01-01 00:00:00+00' + (i / 10) * interval '1 millisecond' + FROM generate_series(1,10000) i`, chat.ID, user.ID) + if err != nil { + t.Fatal(err) + } + if _, err = pool.Exec(ctx, `ANALYZE messages`); err != nil { + t.Fatal(err) + } + t.Run("keyset_and_explain", func(t *testing.T) { + seen := make(map[string]bool) + var after *domain.Position + for { + page, err := repo.ListMessages(ctx, chat.ID, after, 97) + if err != nil { + t.Fatal(err) + } + if len(page) == 0 { + break + } + for _, message := range page { + if seen[message.ID] { + t.Fatal("duplicate on page boundary") + } + seen[message.ID] = true + } + last := page[len(page)-1] + after = &domain.Position{CreatedAt: last.CreatedAt, ID: last.ID} + } + if len(seen) != 10000 { + t.Fatalf("history count=%d", len(seen)) + } + rows, err := pool.Query(ctx, `EXPLAIN (ANALYZE, BUFFERS) + SELECT m.id::text,m.chat_id::text,m.sender_id::text,m.text,m.created_at FROM messages m + WHERE m.chat_id=$1 AND (m.created_at,m.id)>($2,$3) ORDER BY m.created_at,m.id LIMIT $4`, + chat.ID, time.Date(2026, 1, 1, 0, 0, 0, 900000000, time.UTC), "00000000-0000-0000-0000-000000000000", 51) + if err != nil { + t.Fatal(err) + } + defer rows.Close() + t.Log("EXPLAIN on 10,000 messages, cursor near the end (9,000):") + for rows.Next() { + var line string + if err = rows.Scan(&line); err != nil { + t.Fatal(err) + } + t.Log(line) + } + if err = rows.Err(); err != nil { + t.Fatal(err) + } + t.Log("keyset traversal returned 10,000 distinct messages, including equal timestamps") + }) + t.Run("idempotency_race", func(t *testing.T) { + isolated, err := repo.CreateChat(ctx, "Idempotency", []string{user.ID}) + if err != nil { + t.Fatal(err) + } + const count = 16 + results := make(chan domain.Message, count) + failures := make(chan error, count) + start := make(chan struct{}) + var wg sync.WaitGroup + for i := 0; i < count; i++ { + wg.Add(1) + go func() { + defer wg.Done() + <-start + message, err := repo.CreateMessage(ctx, isolated.ID, user.ID, "exactly once", "shared-key") + if err != nil { + failures <- err + return + } + results <- message + }() + } + close(start) + wg.Wait() + close(results) + close(failures) + for err := range failures { + t.Fatal(err) + } + var first domain.Message + for message := range results { + if first.ID == "" { + first = message + } + if message.ID != first.ID || !message.CreatedAt.Equal(first.CreatedAt) || message.Text != first.Text { + t.Fatal("concurrent replay changed result") + } + } + var rows int + if err := pool.QueryRow(ctx, `SELECT count(*) FROM messages WHERE chat_id=$1`, isolated.ID).Scan(&rows); err != nil || rows != 1 { + t.Fatalf("expected one durable effect, got %d: %v", rows, err) + } + _, err = repo.CreateMessage(ctx, isolated.ID, user.ID, "different", "shared-key") + applicationError, ok := domain.AsApplicationError(err) + if !ok || applicationError.Code != domain.ErrorConflict { + t.Fatalf("expected conflict, got %v", err) + } + t.Log("16 concurrent PostgreSQL requests: one effect, identical replay, changed body conflicts") + }) + t.Run("goose_legacy_and_down", func(t *testing.T) { + // This is the disposable test schema, never the application's public schema. + const digestSQL = `SELECT md5(string_agg(ROW(id,chat_id,sender_id,text,created_at)::text,',' ORDER BY id)) FROM messages` + var before, after string + if err = pool.QueryRow(ctx, digestSQL).Scan(&before); err != nil { + t.Fatal(err) + } + if _, err = pool.Exec(ctx, `DROP TABLE goose_db_version; + CREATE TABLE schema_migrations(version text PRIMARY KEY,applied_at timestamptz NOT NULL DEFAULT now()); + INSERT INTO schema_migrations(version) VALUES('001_initial.sql')`); err != nil { + t.Fatal(err) + } + runGoose(t, "up") + runGoose(t, "up") + if err = pool.QueryRow(ctx, digestSQL).Scan(&after); err != nil { + t.Fatal(err) + } + if before != after { + t.Fatal("legacy migration changed message data") + } + t.Log("adopted legacy schema: all existing messages are unchanged") + runGoose(t, "reset") + var tables int + if err = pool.QueryRow(ctx, `SELECT count(*) FROM information_schema.tables WHERE table_schema=$1 AND table_name IN ('users','chats','chat_members','messages','schema_migrations')`, schema).Scan(&tables); err != nil || tables != 0 { + t.Fatalf("down left %d application tables: %v", tables, err) + } + runGoose(t, "up") + if err = pool.QueryRow(ctx, `SELECT count(*) FROM messages`).Scan(&tables); err != nil || tables != 0 { + t.Fatalf("up after down failed: %v", err) + } + t.Log("down and up succeeded in isolated schema") + }) + +} diff --git a/internal/pkg/repository/postgres/users.go b/internal/pkg/repository/postgres/users.go new file mode 100644 index 00000000..ad3f8c15 --- /dev/null +++ b/internal/pkg/repository/postgres/users.go @@ -0,0 +1,46 @@ +package postgres + +import ( + "context" + "errors" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" +) + +func (s *Storage) CreateUser(ctx context.Context, username, displayName, hash string) (domain.User, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + var user domain.User + err := s.pool.QueryRow(ctx, `INSERT INTO users(id,username,display_name,password_hash) VALUES($1,$2,$3,$4) + RETURNING id::text,username,display_name,created_at`, uuid.NewString(), username, displayName, hash). + Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt) + return user, dbError(err) +} + +func (s *Storage) User(ctx context.Context, id string) (domain.User, bool, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + var user domain.User + err := s.pool.QueryRow(ctx, `SELECT id::text,username,display_name,created_at FROM users WHERE id=$1`, id). + Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt) + if errors.Is(err, pgx.ErrNoRows) { + return user, false, nil + } + return user, err == nil, dbError(err) +} + +func (s *Storage) UserByUsername(ctx context.Context, username string) (domain.User, string, bool, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + var user domain.User + var hash string + err := s.pool.QueryRow(ctx, `SELECT id::text,username,display_name,created_at,password_hash FROM users WHERE username=$1`, username). + Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt, &hash) + if errors.Is(err, pgx.ErrNoRows) { + return user, "", false, nil + } + return user, hash, err == nil, dbError(err) +} diff --git a/internal/pkg/session/redis.go b/internal/pkg/session/redis.go new file mode 100644 index 00000000..13721b4b --- /dev/null +++ b/internal/pkg/session/redis.go @@ -0,0 +1,71 @@ +package session + +import ( + "context" + "encoding/json" + "errors" + "log" + "time" + + "github.com/redis/go-redis/v9" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" +) + +type Redis struct { + client *redis.Client + timeout time.Duration +} + +func NewRedis(client *redis.Client, timeout time.Duration) *Redis { + return &Redis{client: client, timeout: timeout} +} + +func unavailable(err error) error { + if err == nil { + return nil + } + log.Print("redis operation failed") + return domain.NewApplicationError(domain.ErrorUnavailable, "Хранилище сессий недоступно") +} + +func (s *Redis) Ready(ctx context.Context) error { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + return unavailable(s.client.Ping(ctx).Err()) +} + +func (s *Redis) Put(ctx context.Context, key string, value domain.Session) error { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + data, err := json.Marshal(value) + if err != nil { + return err + } + // Absolute expiry prevents TTL extension by network delay or restart. + return unavailable(s.client.Do(ctx, "SET", "session:"+key, data, "PXAT", value.ExpiresAt.UnixMilli()).Err()) +} + +func (s *Redis) Get(ctx context.Context, key string) (domain.Session, bool, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + var value domain.Session + data, err := s.client.Get(ctx, "session:"+key).Bytes() + if errors.Is(err, redis.Nil) { + return value, false, nil + } + if err != nil { + return value, false, unavailable(err) + } + if err = json.Unmarshal(data, &value); err != nil { + return value, false, unavailable(err) + } + return value, time.Now().Before(value.ExpiresAt), nil +} + +func (s *Redis) Delete(ctx context.Context, key string) (bool, error) { + ctx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + count, err := s.client.Del(ctx, "session:"+key).Result() + return count > 0, unavailable(err) +} diff --git a/internal/usecase/chat/repository.go b/internal/usecase/chat/repository.go new file mode 100644 index 00000000..004d1f67 --- /dev/null +++ b/internal/usecase/chat/repository.go @@ -0,0 +1,28 @@ +package chat + +import ( + "context" + "errors" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" +) + +var ( + ErrUsernameTaken = errors.New("username is already taken") + ErrUserNotFound = errors.New("user not found") + ErrChatNotFound = errors.New("chat not found") + ErrNotMember = errors.New("user is not a chat member") +) + +// Repository is the persistence port required by the chat use case. +type Repository interface { + Ready(ctx context.Context) error + UserByUsername(ctx context.Context, username string) (domain.User, string, bool, error) + CreateUser(ctx context.Context, username, displayName, passwordHash string) (domain.User, error) + User(ctx context.Context, id string) (domain.User, bool, error) + CreateChat(ctx context.Context, title string, memberIDs []string) (domain.Chat, error) + ChatForUser(ctx context.Context, chatID, userID string) (domain.Chat, error) + ListChats(ctx context.Context, userID string, after *domain.Position, limit int) ([]domain.Chat, error) + CreateMessage(ctx context.Context, chatID, senderID, text, key string) (domain.Message, error) + ListMessages(ctx context.Context, chatID string, after *domain.Position, limit int) ([]domain.Message, error) +} diff --git a/internal/usecase/chat/service.go b/internal/usecase/chat/service.go new file mode 100644 index 00000000..da6460f3 --- /dev/null +++ b/internal/usecase/chat/service.go @@ -0,0 +1,158 @@ +package chat + +import ( + "context" + "errors" + "time" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/password" +) + +type UseCase struct { + repository Repository + sessions Sessions + sessionTTL time.Duration +} + +func New(repository Repository, sessions Sessions, ttl time.Duration) *UseCase { + return &UseCase{repository: repository, sessions: sessions, sessionTTL: ttl} +} + +func (u *UseCase) CreateUser(ctx context.Context, user domain.User, secret string) (domain.User, error) { + if !validUsername(user.Username) { + return domain.User{}, invalid("username должен соответствовать [a-z0-9_]{3,32}") + } + if !validRequiredText(user.DisplayName, 100) { + return domain.User{}, invalid("display_name должен содержать от 1 до 100 символов") + } + if !validPassword(secret) { + return domain.User{}, invalid("password должен содержать от 12 до 128 символов") + } + hash, err := password.Hash(ctx, secret) + if err != nil { + return domain.User{}, err + } + created, err := u.repository.CreateUser(ctx, user.Username, user.DisplayName, hash) + if errors.Is(err, ErrUsernameTaken) { + return domain.User{}, domain.NewApplicationError(domain.ErrorConflict, "Пользователь с таким username уже существует") + } + return created, err +} + +func (u *UseCase) CreateChat(ctx context.Context, callerID string, chat domain.Chat) (domain.Chat, error) { + if !validRequiredText(chat.Title, 100) { + return domain.Chat{}, invalid("title должен содержать от 1 до 100 символов") + } + if len(chat.MemberIDs) < 1 || len(chat.MemberIDs) > 100 { + return domain.Chat{}, invalid("member_ids должен содержать от 1 до 100 участников") + } + + normalizedIDs := make([]string, len(chat.MemberIDs)) + seen := make(map[string]struct{}, len(chat.MemberIDs)) + callerIncluded := false + for index, memberID := range chat.MemberIDs { + normalized, valid := normalizeUUID(memberID) + if !valid { + return domain.Chat{}, invalid("member_ids содержит некорректный UUID") + } + if _, duplicate := seen[normalized]; duplicate { + return domain.Chat{}, invalid("member_ids содержит повторяющиеся UUID") + } + normalizedIDs[index] = normalized + seen[normalized] = struct{}{} + callerIncluded = callerIncluded || normalized == callerID + } + if !callerIncluded { + return domain.Chat{}, invalid("текущий пользователь должен входить в member_ids") + } + + created, err := u.repository.CreateChat(ctx, chat.Title, normalizedIDs) + if errors.Is(err, ErrUserNotFound) { + return domain.Chat{}, invalid("member_ids содержит неизвестного пользователя") + } + return created, err +} + +func (u *UseCase) GetChat(ctx context.Context, callerID string, chat domain.Chat) (domain.Chat, error) { + chatID, valid := normalizeUUID(chat.ID) + if !valid { + return domain.Chat{}, invalid("Некорректный chat_id") + } + return u.chatForUser(ctx, chatID, callerID) +} + +func (u *UseCase) ListChats(ctx context.Context, callerID string, pagination domain.Pagination) (domain.ChatPage, error) { + if pagination.Limit < 1 || pagination.Limit > 100 { + return domain.ChatPage{}, invalid("limit должен быть целым числом от 1 до 100") + } + items, err := u.repository.ListChats(ctx, callerID, pagination.After, pagination.Limit+1) + if err != nil { + return domain.ChatPage{}, err + } + page := domain.ChatPage{Items: items, HasMore: len(items) > pagination.Limit} + if page.HasMore { + page.Items = page.Items[:pagination.Limit] + } + return page, nil +} + +func (u *UseCase) CreateMessage(ctx context.Context, callerID string, message domain.Message) (domain.Message, error) { + chatID, valid := normalizeUUID(message.ChatID) + if !valid { + return domain.Message{}, invalid("Некорректный chat_id") + } + if _, err := u.chatForUser(ctx, chatID, callerID); err != nil { + return domain.Message{}, err + } + if !validRequiredText(message.Text, 4000) { + return domain.Message{}, invalid("text должен содержать от 1 до 4000 символов") + } + created, err := u.repository.CreateMessage(ctx, chatID, callerID, message.Text, message.IdempotencyKey) + if errors.Is(err, ErrChatNotFound) || errors.Is(err, ErrNotMember) { + return domain.Message{}, notFound() + } + return created, err +} + +func (u *UseCase) ListMessages(ctx context.Context, callerID string, chat domain.Chat, pagination domain.Pagination) (domain.MessagePage, error) { + chatID, valid := normalizeUUID(chat.ID) + if !valid { + return domain.MessagePage{}, invalid("Некорректный chat_id") + } + if _, err := u.chatForUser(ctx, chatID, callerID); err != nil { + return domain.MessagePage{}, err + } + if pagination.Limit < 1 || pagination.Limit > 100 { + return domain.MessagePage{}, invalid("limit должен быть целым числом от 1 до 100") + } + items, err := u.repository.ListMessages(ctx, chatID, pagination.After, pagination.Limit+1) + if err != nil { + return domain.MessagePage{}, err + } + page := domain.MessagePage{Items: items, HasMore: len(items) > pagination.Limit} + if page.HasMore { + page.Items = page.Items[:pagination.Limit] + } + return page, nil +} + +func (u *UseCase) chatForUser(ctx context.Context, chatID, callerID string) (domain.Chat, error) { + chat, err := u.repository.ChatForUser(ctx, chatID, callerID) + if errors.Is(err, ErrChatNotFound) || errors.Is(err, ErrNotMember) { + return domain.Chat{}, notFound() + } + return chat, err +} + +func invalid(message string) error { + return domain.NewApplicationError(domain.ErrorInvalid, message) +} + +func unauthorized() error { + return domain.NewApplicationError(domain.ErrorUnauthorized, "Требуется действительная сессия") +} + +func notFound() error { + return domain.NewApplicationError(domain.ErrorNotFound, "Объект не найден") +} diff --git a/internal/usecase/chat/session.go b/internal/usecase/chat/session.go new file mode 100644 index 00000000..07d16684 --- /dev/null +++ b/internal/usecase/chat/session.go @@ -0,0 +1,102 @@ +package chat + +import ( + "context" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "strings" + "time" + "unicode/utf8" + + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" + "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/password" +) + +type Sessions interface { + Put(context.Context, string, domain.Session) error + Get(context.Context, string) (domain.Session, bool, error) + Delete(context.Context, string) (bool, error) + Ready(context.Context) error +} + +func validPassword(value string) bool { + n := utf8.RuneCountInString(value) + return utf8.ValidString(value) && n >= 12 && n <= 128 && strings.TrimSpace(value) != "" +} + +func tokenKey(token string) string { + sum := sha256.Sum256([]byte(token)) + return hex.EncodeToString(sum[:]) +} + +func (u *UseCase) Authenticate(ctx context.Context, token string) (domain.User, error) { + decoded, err := base64.RawURLEncoding.DecodeString(token) + if err != nil || len(decoded) != 32 { + return domain.User{}, unauthorized() + } + session, exists, err := u.sessions.Get(ctx, tokenKey(token)) + if err != nil { + return domain.User{}, err + } + if !exists || !time.Now().Before(session.ExpiresAt) { + return domain.User{}, unauthorized() + } + user, exists, err := u.repository.User(ctx, session.UserID) + if err != nil { + return domain.User{}, err + } + if !exists { + return domain.User{}, unauthorized() + } + return user, nil +} + +func (u *UseCase) CreateSession(ctx context.Context, username, secret string) (domain.Login, error) { + if !validUsername(username) || !validPassword(secret) { + return domain.Login{}, unauthorized() + } + user, hash, exists, err := u.repository.UserByUsername(ctx, username) + if err != nil { + return domain.Login{}, err + } + if !exists { + hash = password.DummyHash + } + valid, err := password.Verify(ctx, secret, hash) + if err != nil { + return domain.Login{}, err + } + if !valid || !exists { + return domain.Login{}, unauthorized() + } + var raw [32]byte + if _, err = rand.Read(raw[:]); err != nil { + return domain.Login{}, err + } + token := base64.RawURLEncoding.EncodeToString(raw[:]) + expires := time.Now().UTC().Add(u.sessionTTL) + if err = u.sessions.Put(ctx, tokenKey(token), domain.Session{UserID: user.ID, ExpiresAt: expires}); err != nil { + return domain.Login{}, err + } + return domain.Login{Token: token, ExpiresAt: expires, User: user}, nil +} + +func (u *UseCase) DeleteSession(ctx context.Context, token string) error { + deleted, err := u.sessions.Delete(ctx, tokenKey(token)) + if err != nil { + return err + } + if !deleted { + return unauthorized() + } + return nil +} + +func (u *UseCase) Ready(ctx context.Context) error { + if err := u.repository.Ready(ctx); err != nil { + return err + } + return u.sessions.Ready(ctx) +} diff --git a/internal/usecase/chat/session_test.go b/internal/usecase/chat/session_test.go new file mode 100644 index 00000000..5096e1ba --- /dev/null +++ b/internal/usecase/chat/session_test.go @@ -0,0 +1,19 @@ +package chat + +import ( + "strings" + "testing" +) + +func TestPasswordContract(t *testing.T) { + for _, value := range []string{"", "too-short", strings.Repeat(" ", 12), strings.Repeat("x", 129), strings.Repeat("x", 12) + string([]byte{0xff})} { + if validPassword(value) { + t.Fatal("invalid password accepted") + } + } + for _, value := range []string{strings.Repeat("🔑", 128), "valid:password", " password with spaces "} { + if !validPassword(value) { + t.Fatal("valid Unicode password rejected") + } + } +} diff --git a/internal/usecase/chat/validation.go b/internal/usecase/chat/validation.go new file mode 100644 index 00000000..5fe43df1 --- /dev/null +++ b/internal/usecase/chat/validation.go @@ -0,0 +1,37 @@ +package chat + +import ( + "strings" + "unicode/utf8" + + "github.com/google/uuid" +) + +func normalizeUUID(value string) (string, bool) { + parsed, err := uuid.Parse(value) + if err != nil { + return "", false + } + normalized := parsed.String() + if !strings.EqualFold(value, normalized) { + return "", false + } + return normalized, true +} + +func validUsername(value string) bool { + if len(value) < 3 || len(value) > 32 { + return false + } + for _, char := range value { + if char != '_' && (char < 'a' || char > 'z') && (char < '0' || char > '9') { + return false + } + } + return true +} + +func validRequiredText(value string, maxRunes int) bool { + length := utf8.RuneCountInString(value) + return utf8.ValidString(value) && length >= 1 && length <= maxRunes && strings.TrimSpace(value) != "" +} diff --git a/lab.json b/lab.json index e9ffc9f7..cc86272e 100644 --- a/lab.json +++ b/lab.json @@ -4,5 +4,5 @@ "contract_version": "1.4.0", "default_base_url": "http://localhost:8080", "grading": "3; 4 includes 3; 5 includes 4 plus one completed research track", - "scaffold_has_application": false + "scaffold_has_application": true } diff --git a/migrations/001_initial.sql b/migrations/001_initial.sql new file mode 100644 index 00000000..2433b274 --- /dev/null +++ b/migrations/001_initial.sql @@ -0,0 +1,54 @@ +-- +goose Up +-- +goose StatementBegin +DO $$ +BEGIN + -- Adopt the schema created by the previous runner without changing its data. + IF to_regclass('schema_migrations') IS NOT NULL THEN + IF NOT EXISTS (SELECT 1 FROM schema_migrations WHERE version = '001_initial.sql') + OR to_regclass('users') IS NULL OR to_regclass('chats') IS NULL + OR to_regclass('chat_members') IS NULL OR to_regclass('messages') IS NULL THEN + RAISE EXCEPTION 'incomplete legacy schema: cannot adopt migration 1'; + END IF; + RETURN; + END IF; + CREATE TABLE users ( + id uuid PRIMARY KEY, + username text NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'), + display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100), + password_hash text NOT NULL, + created_at timestamptz NOT NULL DEFAULT clock_timestamp() + ); + CREATE TABLE chats ( + id uuid PRIMARY KEY, + title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100), + created_at timestamptz NOT NULL DEFAULT clock_timestamp() + ); + CREATE INDEX chats_history_idx ON chats (created_at, id); + CREATE TABLE chat_members ( + chat_id uuid NOT NULL REFERENCES chats(id), + user_id uuid NOT NULL REFERENCES users(id), + ordinal integer NOT NULL CHECK (ordinal >= 0), + PRIMARY KEY (chat_id, user_id), + UNIQUE (chat_id, ordinal) + ); + CREATE INDEX chat_members_user_idx ON chat_members (user_id, chat_id); + CREATE TABLE messages ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL, + sender_id uuid NOT NULL, + text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id) + ); + CREATE INDEX messages_history_idx ON messages (chat_id, created_at, id); +END; +$$; +-- +goose StatementEnd + +-- +goose Down +DROP TABLE messages; +DROP TABLE chat_members; +DROP TABLE chats; +DROP TABLE users; +-- The old journal must not claim that the dropped schema is still applied. +DROP TABLE IF EXISTS schema_migrations; diff --git a/migrations/002_idempotency.sql b/migrations/002_idempotency.sql new file mode 100644 index 00000000..fb02f93c --- /dev/null +++ b/migrations/002_idempotency.sql @@ -0,0 +1,10 @@ +-- +goose Up +-- Additive: old lab2 writers can still insert NULL keys during migration. +ALTER TABLE messages ADD COLUMN IF NOT EXISTS idempotency_key text + CHECK (idempotency_key ~ '^[A-Za-z0-9._:-]{1,128}$'); +CREATE UNIQUE INDEX IF NOT EXISTS messages_idempotency_idx + ON messages(sender_id, chat_id, idempotency_key); + +-- +goose Down +DROP INDEX messages_idempotency_idx; +ALTER TABLE messages DROP COLUMN idempotency_key; diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh new file mode 100755 index 00000000..ae4321a6 --- /dev/null +++ b/scripts/bootstrap.sh @@ -0,0 +1,15 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +umask 077 +mkdir -p secrets +chmod 700 secrets +for name in postgres_password cursor_key; do + if [ ! -s "secrets/$name" ]; then + openssl rand -hex 32 > "secrets/$name" + fi +done +# Compose mounts files directly on Linux; the unprivileged API must read them. +# The host directory remains private (0700). +chmod 444 secrets/postgres_password secrets/cursor_key +printf '%s\n' 'Local secrets are ready.' diff --git a/scripts/compose.sh b/scripts/compose.sh new file mode 100755 index 00000000..078e4f0d --- /dev/null +++ b/scripts/compose.sh @@ -0,0 +1,7 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +if docker compose version >/dev/null 2>&1; then + exec docker compose --env-file /dev/null -f compose.yaml "$@" +fi +exec docker-compose --env-file /dev/null -f compose.yaml "$@" diff --git a/scripts/measure_load.py b/scripts/measure_load.py new file mode 100644 index 00000000..82481f07 --- /dev/null +++ b/scripts/measure_load.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +"""Reproducible local benchmark with resource samples; no credentials in output.""" +import argparse +import json +import os +from pathlib import Path +import subprocess +import sys +import time + +ROOT = Path(__file__).resolve().parents[1] +STACK = os.environ.get('STACK_NAME', 'mtusi-lab4') + +def containers(service=None): + label = 'com.docker.swarm.service.name='+STACK+'_'+service if service else 'com.docker.stack.namespace='+STACK + return subprocess.check_output(['docker','ps','--filter','label='+label,'-q'],text=True).split() + + +def counts(): + sql = 'SELECT json_build_object(\'users\',(SELECT count(*) FROM users),\'chats\',(SELECT count(*) FROM chats),\'messages\',(SELECT count(*) FROM messages))' + return json.loads(subprocess.check_output(['docker', 'exec', containers('postgres')[0], 'psql', '-U', 'messenger', '-d', 'messenger', '-Atc', sql], text=True)) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--base-url', default=os.environ.get('BASE_URL', 'http://localhost:8080')) + parser.add_argument('--duration', type=int, default=30) + parser.add_argument('--concurrency', type=int, default=4) + args = parser.parse_args() + command = [sys.executable, str(ROOT / 'tests/load.py'), '--base-url', args.base_url, '--concurrency', str(args.concurrency)] + subprocess.run(command + ['--duration', '3'], check=True, stdout=subprocess.DEVNULL) + before = counts() + ids = containers() + samples = [] + process = subprocess.Popen(command + ['--duration', str(args.duration), '--output', str(ROOT / 'evidence/swarm-load.json')], stdout=subprocess.DEVNULL) + try: + while process.poll() is None: + stats = subprocess.check_output(['docker', 'stats', '--no-stream', '--format', '{{json .}}', *ids], text=True) + samples.append({'elapsed_sample': len(samples), 'containers': [json.loads(line) for line in stats.splitlines()]}) + time.sleep(1) + finally: + if process.poll() is None: + process.terminate() + code = process.wait() + if code: + raise SystemExit(code) + report = {'warmup_seconds': 3, 'dataset_before': before, 'dataset_after': counts(), 'samples': samples} + (ROOT / 'evidence/swarm-resources.json').write_text(json.dumps(report, indent=2) + '\n') + print((ROOT / 'evidence/swarm-load.json').read_text()) + print('Resource samples and dataset counts: evidence/swarm-resources.json') + + +if __name__ == '__main__': + main() diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..211f35b4 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +exec python3 scripts/swarm.py restart diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..8a93f7f0 --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +exec python3 scripts/swarm.py stop-one diff --git a/scripts/swarm.py b/scripts/swarm.py new file mode 100644 index 00000000..8d5f444d --- /dev/null +++ b/scripts/swarm.py @@ -0,0 +1,228 @@ +#!/usr/bin/env python3 +"""Single-node Swarm lab lifecycle; never deletes volumes or prints credentials.""" +import json +import os +from pathlib import Path +import subprocess +import sys +import time +import urllib.request + +ROOT = Path(__file__).resolve().parents[1] +os.chdir(ROOT) +STACK = os.environ.get('STACK_NAME', 'mtusi-lab4') +API = os.environ.get('API_IMAGE', 'mtusi-messenger:lab4') +MIGRATIONS = os.environ.get('MIGRATIONS_IMAGE', 'mtusi-migrations:lab4') +TESTS = os.environ.get('TESTS_IMAGE', 'mtusi-messenger-tests:lab4') +PORT = os.environ.get('PORT', '8080') + + +def docker(*args, capture=False, **kwargs): + return subprocess.run(['docker', *args], check=True, text=True, + stdout=subprocess.PIPE if capture else None, **kwargs).stdout + + +def inspect(kind, name): + return json.loads(docker(kind, 'inspect', name, capture=True))[0] + + +def tasks(service): + ids = docker('service', 'ps', '--filter', 'desired-state=running', '-q', STACK+'_'+service, capture=True).split() + return [json.loads(docker('inspect', task, capture=True))[0] for task in ids] + + +def wait_service(service, count=1, timeout=120): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + active = tasks(service) + if len(active) == count and all(t['Status']['State'] == 'running' for t in active): + # This lab pins storage to this manager and runs on one Docker engine. + healthy = True + for task in active: + container = task['Status'].get('ContainerStatus', {}).get('ContainerID') + state = json.loads(docker('inspect', container, capture=True))[0]['State'] + healthy &= state.get('Health', {}).get('Status', 'healthy') == 'healthy' + if healthy: + return + time.sleep(1) + raise RuntimeError('service did not become healthy: '+service) + + +def wait_api(): + wait_service('api', int(os.environ.get('API_REPLICAS', '2'))) + deadline = time.monotonic()+45 + while time.monotonic() < deadline: + try: + with urllib.request.urlopen('http://localhost:'+PORT+'/health/ready', timeout=3) as response: + if response.status == 200: + return + except OSError: + pass + time.sleep(.5) + raise RuntimeError('LB readiness timeout') + + +def job(kind, image, command, integration=False): + name = STACK+'_'+kind + # Fixed service name is a deployment lock: concurrent runners fail to create it. + args = ['service', 'create', '--detach', '--name', name, '--mode', 'replicated-job', + '--replicas', '1', '--restart-condition', 'none', '--network', STACK+'_data', + '--constraint', 'node.labels.mtusi_lab4_storage==true', + '--limit-cpu', '1', '--limit-memory', '1g' if integration else '128m', + '--secret', 'source='+STACK+'_postgres_password,target=postgres_password', + '--env', 'POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password'] + if integration: + for value in ['INTEGRATION_TESTS=1', 'POSTGRES_HOST=postgres', 'POSTGRES_USER=messenger', + 'POSTGRES_DB=messenger', 'REDIS_ADDR=redis:6379', 'CURSOR_KEY_FILE=/run/secrets/cursor_key']: + args += ['--env', value] + args += ['--secret', 'source='+STACK+'_cursor_key,target=cursor_key'] + else: + args += ['--env', 'GOOSE_DRIVER=postgres', '--env', + 'GOOSE_DBSTRING=host=postgres user=messenger dbname=messenger sslmode=disable connect_timeout=5'] + docker(*args, image, *command) + try: + deadline = time.monotonic()+(300 if integration else 90) + while time.monotonic() < deadline: + ids = docker('service', 'ps', '-q', name, capture=True).split() + states = [json.loads(docker('inspect', i, capture=True))[0]['Status']['State'] for i in ids] + if 'complete' in states: + docker('service', 'logs', name) + return + if any(s in ('failed', 'rejected') for s in states): + docker('service', 'logs', name) + raise RuntimeError(kind+' failed') + time.sleep(1) + raise RuntimeError(kind+' timed out') + finally: + docker('service', 'rm', name) + + +def build(): + docker('build', '-t', API, '.') + docker('build', '--target', 'migrations', '-t', MIGRATIONS, '.') + + +def up(): + swarm = json.loads(docker('info', '--format', '{{json .Swarm}}', capture=True)) + if swarm['LocalNodeState'] == 'inactive': + docker('swarm', 'init', capture=True) # join token stays out of logs + swarm = json.loads(docker('info', '--format', '{{json .Swarm}}', capture=True)) + if not swarm['ControlAvailable']: + raise RuntimeError('run on the Swarm manager') + if len(docker('node', 'ls', '-q', capture=True).split()) != 1: + raise RuntimeError('automatic lifecycle supports one Swarm node; see REPORT.md for image distribution') + docker('node', 'update', '--label-add', 'mtusi_lab4_storage=true', swarm['NodeID']) + subprocess.run(['./scripts/bootstrap.sh'], check=True) + for secret in ('postgres_password', 'cursor_key'): + known = docker('secret', 'ls', '--format', '{{.Name}}', capture=True).split() + if STACK+'_'+secret not in known: + docker('secret', 'create', STACK+'_'+secret, 'secrets/'+secret) + build() + existing = docker('service', 'ls', '--format', '{{.Name}}', capture=True).split() + # Upgrade the shared schema before replacing any running API image. + fresh = STACK+'_api' not in existing + if not fresh: + job('migrations', MIGRATIONS, ['up']) + # Initial deployment keeps API at zero until the one-shot migration succeeds. + env = dict(os.environ) + env['STACK_NAME'] = STACK + if fresh: + env['API_REPLICAS'] = '0' + docker('stack', 'deploy', '--resolve-image', 'never', '-c', 'stack.yaml', STACK, env=env) + wait_service('postgres') + wait_service('redis') + if fresh: + job('migrations', MIGRATIONS, ['up']) + docker('service', 'scale', '--detach', STACK+'_api='+os.environ.get('API_REPLICAS', '2')) + if fresh: + wait_api() + else: + update() + print('PASS: Swarm stack ready at http://localhost:'+PORT) + + +def restart(): + # Force replaces every task while retaining the same Swarm secrets and volumes. + services = ('postgres', 'redis', 'api', 'lb') + previous = {name: {t['ID'] for t in tasks(name)} for name in services} + for service in services: + docker('service', 'update', '--detach', '--force', STACK+'_'+service) + deadline = time.monotonic()+75 + while any(previous[name].intersection(t['ID'] for t in tasks(name)) for name in services): + if time.monotonic() > deadline: + raise RuntimeError('restart did not replace every original task') + time.sleep(1) + wait_service('lb') + wait_service('postgres') + wait_service('redis') + wait_api() + + +def stop_one(): + target = os.environ['TARGET_INSTANCE_ID'] + matches = [t for t in tasks('api') if t['ID'] == target] + if len(matches) != 1: + raise RuntimeError('TARGET_INSTANCE_ID is not a running API task in this stack') + task = matches[0] + container = task['Status']['ContainerStatus']['ContainerID'] + print('Stopping task '+target+' container '+container, flush=True) + docker('kill', container) # Swarm replaces it with a different task/instance ID. + + +def update(): + previous = {t['ID'] for t in tasks('api')} + docker('service', 'update', '--detach', '--force', '--image', API, STACK+'_api') + deadline = time.monotonic()+180 + while time.monotonic() < deadline: + state = inspect('service', STACK+'_api').get('UpdateStatus', {}).get('State') + if state == 'completed' and previous.isdisjoint(t['ID'] for t in tasks('api')): + wait_api() + print('PASS: rolling update completed') + return + if state in ('paused', 'rollback_started', 'rollback_paused', 'rollback_completed'): + raise RuntimeError('rolling update: '+state) + time.sleep(1) + raise RuntimeError('rolling update timed out') + + +def status(): + result = {'stack': STACK, 'docker': docker('version','--format','{{.Server.Version}}',capture=True).strip(), 'services': {}} + for name in ('lb','api','postgres','redis'): + service = inspect('service', STACK+'_'+name) + spec = service['Spec'] + template = spec['TaskTemplate'] + result['services'][name] = { + 'image': template['ContainerSpec']['Image'], + 'limits': template.get('Resources',{}).get('Limits',{}), + 'published_ports': spec.get('EndpointSpec',{}).get('Ports',[]), + 'secret_names': [x['SecretName'] for x in template['ContainerSpec'].get('Secrets',[])], + 'mounts': template['ContainerSpec'].get('Mounts',[]), + 'tasks': [{'id':t['ID'],'state':t['Status']['State'], + 'container':t['Status'].get('ContainerStatus',{}).get('ContainerID')} + for t in tasks(name)]} + print(json.dumps(result,indent=2)) + +def main(): + action = sys.argv[1] + if action == 'status': status() + elif action == 'up': up() + elif action == 'build': build() + elif action == 'wait': wait_api() + elif action == 'restart': restart() + elif action == 'stop-one': stop_one() + elif action == 'update': update() + elif action == 'migrate': job('migrations', MIGRATIONS, ['up']) + elif action == 'migrate-status': job('migrations', MIGRATIONS, ['status']) + elif action == 'test-integration': + docker('build', '--target', 'tests', '-t', TESTS, '.') + job('tests', TESTS, [], integration=True) + elif action == 'down': docker('stack', 'rm', STACK) + else: raise RuntimeError('unknown action: '+action) + + +if __name__ == '__main__': + try: + main() + except (RuntimeError, subprocess.CalledProcessError) as error: + print('FAIL: '+str(error), file=sys.stderr) + sys.exit(1) diff --git a/stack.yaml b/stack.yaml new file mode 100644 index 00000000..dc0d0c26 --- /dev/null +++ b/stack.yaml @@ -0,0 +1,121 @@ +version: '3.8' +services: + postgres: + image: postgres:17.6-alpine3.22 + environment: + POSTGRES_USER: messenger + POSTGRES_DB: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + secrets: [postgres_password] + volumes: [postgres_data:/var/lib/postgresql/data] + networks: [data] + healthcheck: + test: [CMD-SHELL, 'pg_isready -U messenger -d messenger'] + interval: 2s + timeout: 2s + retries: 15 + stop_grace_period: 20s + deploy: + replicas: 1 + placement: + constraints: [node.labels.mtusi_lab4_storage == true] + resources: + limits: {cpus: '1.0', memory: 512M} + update_config: {parallelism: 1, order: stop-first} + redis: + image: redis:7.4.5-alpine3.21 + command: [redis-server, /usr/local/etc/redis/redis.conf] + configs: + - source: redis + target: /usr/local/etc/redis/redis.conf + volumes: [redis_data:/data] + networks: [data] + healthcheck: + test: [CMD, redis-cli, ping] + interval: 2s + timeout: 1s + retries: 15 + stop_grace_period: 20s + deploy: + replicas: 1 + placement: + constraints: [node.labels.mtusi_lab4_storage == true] + resources: + limits: {cpus: '0.5', memory: 128M} + update_config: {parallelism: 1, order: stop-first} + api: + image: ${API_IMAGE:-mtusi-messenger:lab4} + environment: + INSTANCE_ID: '{{.Task.ID}}' + POSTGRES_HOST: postgres + POSTGRES_USER: messenger + POSTGRES_DB: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + CURSOR_KEY_FILE: /run/secrets/cursor_key + POSTGRES_MAX_CONNS: '10' + REDIS_ADDR: redis:6379 + SESSION_TTL_SECONDS: '3600' + QUERY_TIMEOUT_MS: '1000' + secrets: [postgres_password, cursor_key] + networks: [backend, data] + stop_grace_period: 15s + healthcheck: + test: [CMD, wget, -q, -T, '2', -O, /dev/null, 'http://127.0.0.1:8080/health/ready'] + interval: 2s + timeout: 3s + start_period: 10s + retries: 3 + deploy: + replicas: ${API_REPLICAS:-2} + endpoint_mode: dnsrr + resources: + limits: {cpus: '1.0', memory: 256M} + restart_policy: {condition: any, delay: 1s} + update_config: + parallelism: 1 + order: start-first + delay: 5s + monitor: 15s + failure_action: rollback + rollback_config: {parallelism: 1, order: start-first} + lb: + image: haproxy:3.2.6-alpine + configs: + - source: haproxy + target: /usr/local/etc/haproxy/haproxy.cfg + networks: [backend] + ports: + - target: 8080 + published: ${PORT:-8080} + protocol: tcp + mode: host + healthcheck: + test: [CMD, haproxy, -c, -f, /usr/local/etc/haproxy/haproxy.cfg] + interval: 5s + timeout: 2s + retries: 3 + deploy: + replicas: 1 + resources: + limits: {cpus: '0.5', memory: 128M} + update_config: {parallelism: 1, order: stop-first} +networks: + backend: + driver: overlay + data: + driver: overlay +volumes: + postgres_data: + redis_data: +configs: + haproxy: + file: ./infra/haproxy.cfg + redis: + file: ./infra/redis.conf +secrets: + postgres_password: + external: true + name: ${STACK_NAME:-mtusi-lab4}_postgres_password + cursor_key: + external: true + name: ${STACK_NAME:-mtusi-lab4}_cursor_key diff --git a/tests/own/README.md b/tests/own/README.md new file mode 100644 index 00000000..5a7390e0 --- /dev/null +++ b/tests/own/README.md @@ -0,0 +1,15 @@ +# Проверки базы из lab3 + +Эти проверки перенесены вместе с реализацией lab3. Они не подтверждают критерии оценки lab4. +Публичные HTTP-проверки используют текущий контракт lab4 и требуют реализации вложений. + +Публичные tests/*.py и обязательный контракт сохранены. + +- `make test-own`: Go tests с race detector. Без INTEGRATION_TESTS=1 PostgreSQL-сценарий пропускается. +- `make test-integration`: отдельный Swarm job, Go 1.26.4 и goose. Временная схема, миграции с нуля/повторно/legacy/reset/up, регистрационная гонка, rollback, 10 000 сообщений с одинаковыми timestamps, EXPLAIN и 16 конкурентных запросов одного ключа. +- `make test-grade4`: `swarm_grade4.py`, ровно две API tasks. Проверяет общий ключ через LB, одинаковый результат, 409, границы ключа, область пользователя/чата, права и logout. По очереди останавливает обе исходные tasks с восстановлением, затем выполняет rolling update под нагрузкой. Проверяет историю, отсутствие дублей и повтор исходного ключа после воздействия. Сохраняет `evidence/swarm-grade4.json`. +- `make test-persistence`: перезапуск всех API, PostgreSQL, Redis и LB; volumes/secrets сохраняются. +- `make test-failover ACTION_SCRIPT=scripts/stop-one.sh`: публичный сценарий отказа; `make wait` дожидается восстановления двух реплик. +- `make measure-load`: 3s прогрева + 30s, concurrency 4, метрики и Docker stats в `evidence/swarm-*`. + +Сценарии отказов, persistence и rolling update выполняйте **последовательно**, на локальном учебном стенде. Все данные синтетические; пароль и токены в результаты не записываются. Старый `grade4.py` относится к Compose-стенду lab2 и не является entrypoint текущей работы. diff --git a/tests/own/grade4.py b/tests/own/grade4.py new file mode 100644 index 00000000..60f89b87 --- /dev/null +++ b/tests/own/grade4.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python3 +"""Additional lab2 acceptance checks. Never print credentials or session tokens.""" +import base64 +from concurrent.futures import ThreadPoolExecutor +import os +from pathlib import Path +import subprocess +import sys +import time +import uuid + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) +from client import Client, Failure, check, run + +ROOT = Path(__file__).resolve().parents[2] + + +def compose(*args, ttl=None): + env = dict(os.environ) + if ttl is not None: + env['SESSION_TTL_SECONDS'] = str(ttl) + result = subprocess.run([str(ROOT / 'scripts/compose.sh'), *args], env=env, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=180) + check(result.returncode == 0, 'compose operation failed: ' + args[0]) + + +def login(c, username, password, expected=201): + basic = base64.b64encode((username + ':' + password).encode()).decode() + return c.call('POST', '/api/v1/auth/sessions', expected, + headers={'Authorization': 'Basic ' + basic}, schema='Session' if expected == 201 else None) + + +def main(): + c = Client(timeout=5) + alice, bob, eve, chat = c.bootstrap() + credentials = alice['registration'] + login(c, credentials['username'], 'incorrect-password', 401) + login(c, 'unknown_' + uuid.uuid4().hex[:12], 'incorrect-password', 401) + c.call('GET', '/api/v1/users/me', 401, headers={'X-User-Id': alice['id']}) + basic = base64.b64encode((credentials['username'] + ':' + credentials['password']).encode()).decode() + c.call('GET', '/api/v1/users/me', 401, headers={'Authorization': 'Basic ' + basic}) + response = c.call('GET', '/api/v1/users/me', actor=alice, headers={'X-User-Id': bob['id']}, schema='User') + check(response['json']['id'] == alice['id'], 'X-User-Id changed session owner') + c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice) + c.call('GET', '/api/v1/users/me', 401, actor=alice) + c.call('DELETE', '/api/v1/auth/sessions/current', 401, actor=alice) + print('PASS: wrong password, unknown user, Bearer-only access, X-User-Id ignored, logout') + + # Maximum UTF-8 password length and a colon are supported without truncation. + username = 'unicode_' + uuid.uuid4().hex[:12] + password = '🔑' * 127 + ':' + c.call('POST', '/api/v1/users', 201, body={'username': username, 'display_name': 'Unicode', 'password': password}, schema='User') + login(c, username, password) + login(c, username, '🔑' * 127 + 'x', 401) + print('PASS: 128-code-point Unicode password including colon; suffix verified') + + username = 'race_' + uuid.uuid4().hex[:12] + def register(_): + return Client().call('POST', '/api/v1/users', [201, 409], body={ + 'username': username, 'display_name': 'Concurrent', 'password': 'synthetic-password-123' + })['status'] + with ThreadPoolExecutor(max_workers=8) as pool: + statuses = list(pool.map(register, range(8))) + check(statuses.count(201) == 1 and statuses.count(409) == 7, 'registration race broken') + print('PASS: HTTP registration race: one 201, seven 409') + + # Revoke before restart: absence must also survive AOF recovery. + for service in ('redis', 'postgres'): + compose('stop', service) + try: + c.call('GET', '/health/live', schema='Health') + start = time.monotonic() + c.call('GET', '/health/ready', 503) + check(time.monotonic() - start < 4, 'readiness error exceeded timeout budget') + c.call('GET', '/api/v1/users/me', 503, actor=bob) + c.call('GET', '/api/v1/users/me', 401, headers={'X-User-Id': bob['id']}) + login(c, bob['registration']['username'], bob['registration']['password'], 503) + c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 503, actor=bob, body={'text': 'must fail'}) + finally: + compose('up', '-d', '--wait', service) + c.call('GET', '/health/ready', schema='Health') + c.call('GET', '/api/v1/users/me', actor=bob, schema='User') + c.call('GET', '/api/v1/users/me', 401, actor=alice) + print('PASS: ' + service + ' outage bounded; live=200, ready=503, protected=503, anonymous=401; session recovered, logout retained') + + # The same signed cursor remains valid across an API process restart. + c.message(bob, chat, 'cursor before restart 1') + c.message(bob, chat, 'cursor before restart 2') + page = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?limit=1', actor=bob, schema='MessagePage')['json'] + check(page['next_cursor'] is not None, 'missing cursor') + compose('restart', 'api') + deadline = time.monotonic() + 30 + while True: + try: + c.call('GET', '/health/ready', schema='Health') + break + except Failure: + check(time.monotonic() < deadline, 'API did not recover') + time.sleep(.2) + from urllib.parse import quote + c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?cursor=' + quote(page['next_cursor']), actor=bob, schema='MessagePage') + print('PASS: cursor and existing session survive API restart') + + try: + compose('up', '-d', '--wait', '--force-recreate', 'api', ttl=2) + short = c.user('expiry') + c.call('GET', '/api/v1/users/me', actor=short, schema='User') + time.sleep(2.2) + c.call('GET', '/api/v1/users/me', 401, actor=short) + compose('restart', 'redis') + compose('up', '-d', '--wait', 'redis') + c.call('GET', '/api/v1/users/me', 401, actor=short) + print('PASS: SESSION_TTL_SECONDS=2 expires; Redis restart does not revive expired session') + finally: + compose('up', '-d', '--wait', '--force-recreate', 'api', ttl=3600) + print('PASS: all grade-4 HTTP checks; normal TTL restored to 3600') + + +if __name__ == '__main__': + run(main) diff --git a/tests/own/swarm_grade4.py b/tests/own/swarm_grade4.py new file mode 100644 index 00000000..fd37a5bd --- /dev/null +++ b/tests/own/swarm_grade4.py @@ -0,0 +1,165 @@ +#!/usr/bin/env python3 +"""Grade-4 acceptance through the public LB; writes only synthetic test data.""" +from concurrent.futures import ThreadPoolExecutor +import json +import os +from pathlib import Path +import subprocess +import sys +import threading +import time +import uuid + +ROOT = Path(__file__).resolve().parents[2] +sys.path.insert(0, str(ROOT/'tests')) +from client import Client, Failure, check, run +from resilience import history + +STACK = os.environ.get('STACK_NAME', 'mtusi-lab4') + + +def docker_json(*args): + return json.loads(subprocess.check_output(['docker', *args], text=True)) + + +def task_ids(): + ids = subprocess.check_output(['docker','service','ps','--filter','desired-state=running','-q',STACK+'_api'], text=True).split() + return {i for i in ids if docker_json('inspect',i)[0]['Status']['State']=='running'} + + +def wait_ready(): + subprocess.run([sys.executable, str(ROOT/'scripts/swarm.py'), 'wait'], check=True, stdout=subprocess.DEVNULL) + time.sleep(3) # DNS and HAProxy rise interval + + +def idempotency(c, alice, bob, eve, chat): + key = uuid.uuid4().hex + barrier = threading.Barrier(16) + def send(_): + local = Client(timeout=5) + barrier.wait() + return local.message(alice, chat, 'one concurrent effect', key=key) + with ThreadPoolExecutor(max_workers=16) as pool: + results = list(pool.map(send, range(16))) + expected = results[0]['json'] + check(all(r['json']==expected and r['status']==201 for r in results), 'concurrent replay changed result') + instances = {r['headers']['X-Instance-Id'] for r in results} + check(len(instances)>=2, 'same-key race did not reach two replicas') + path = '/api/v1/chats/'+chat['id']+'/messages' + c.call('POST', path, 409, actor=alice, body={'text':'changed'}, headers={'Idempotency-Key':key}) + c.call('POST', path, 404, actor=eve, body={'text':expected['text']}, headers={'Idempotency-Key':key}) + other = c.message(bob, chat, expected['text'], key=key)['json'] + check(other['id']!=expected['id'], 'key leaked across users') + second_chat = c.call('POST','/api/v1/chats',201,actor=alice,body={'title':'Key scope','member_ids':[alice['id'],bob['id']]},schema='Chat')['json'] + separate = c.message(alice,second_chat,expected['text'],key=key)['json'] + check(separate['id']!=expected['id'], 'key leaked across chats') + for bad in ['', 'x'*129, 'bad key', 'bad,key', 'bad/key']: + c.call('POST', path, 400, actor=alice, body={'text':'invalid key'}, headers={'Idempotency-Key':bad}) + c.message(alice,chat,'128 ASCII characters',key='x'*128) + retry = uuid.uuid4().hex + c.call('POST',path,400,actor=alice,body={'text':''},headers={'Idempotency-Key':retry}) + c.message(alice,chat,'valid after invalid',key=retry) + check(sum(m['id']==expected['id'] for m in history(c,bob,chat))==1, 'race created duplicate') + # Revocation must be rechecked even for an already committed key. + c.call('DELETE','/api/v1/auth/sessions/current',204,actor=alice) + c.call('POST',path,401,actor=alice,body={'text':expected['text']},headers={'Idempotency-Key':key}) + print('PASS: shared-key race on two replicas; replay/conflict/scope/validation/access/logout', flush=True) + return {'parallel_requests':16, 'instances':sorted(instances), 'unique_message_ids':1} + + +def exercise(action, target=None): + c = Client(timeout=2) + alice,bob,eve,chat = c.bootstrap() + original_key = uuid.uuid4().hex + original = c.message(alice,chat,'confirmed before '+action,key=original_key)['json'] + before = task_ids() + stop = threading.Event() + events = [] + confirmations = [] + lock = threading.Lock() + def worker(number): + local = Client(timeout=2) + key = uuid.uuid4().hex + body = 'load-'+key + while not stop.is_set(): + started = time.monotonic() + try: + result = local.message(alice,chat,body,key=key) + with lock: + confirmations.append(result['json']) + local.call('GET','/api/v1/users/me',actor=bob,schema='User') + event = (started,time.monotonic(),result['json'],result['headers']['X-Instance-Id'],None) + key = uuid.uuid4().hex + body = 'load-'+key + except Failure as exc: + event = (started,time.monotonic(),None,None,str(exc)) + with lock: + events.append(event) + time.sleep(.03) + threads = [threading.Thread(target=worker,args=(i,)) for i in range(4)] + for thread in threads: thread.start() + time.sleep(2) + env = dict(os.environ) + if target: env['TARGET_INSTANCE_ID'] = target + started = time.monotonic() + proc = subprocess.Popen([sys.executable,str(ROOT/'scripts/swarm.py'),action],env=env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) + try: + code = proc.wait(timeout=180) + check(code==0, action+' command failed') + completed = time.monotonic() + time.sleep(15 if target else 5) + finally: + stop.set() + for thread in threads: thread.join(timeout=10) + if proc.poll() is None: + proc.kill(); proc.wait() + ended = time.monotonic() + wait_ready() + window = [e for e in events if e[0]>=started] + successes = [e for e in window if e[2] is not None] + failures = [e for e in window if e[2] is None] + check(bool(successes), 'no writes during '+action) + recovery = min(e[1] for e in successes if e[0]>=completed)-started if target else None + if target: + check(recovery<=10, 'failure recovery exceeded 10 seconds') + check(target not in [e[3] for e in successes[-20:]], 'stopped instance returned') + check(target not in task_ids(), 'old task still running') + else: + check(before.isdisjoint(task_ids()), 'rolling update did not replace every replica') + replay = c.message(alice,chat,original['text'],key=original_key)['json'] + check(replay==original, 'committed idempotency result lost after '+action) + all_messages = history(c,bob,chat) + by_id = {m['id']:m for m in all_messages} + check(by_id.get(original['id'])==original, 'pre-action confirmation lost') + check(all(by_id.get(m['id'])==m for m in confirmations), 'confirmation lost/changed') + texts = [m['text'] for m in all_messages] + check(len(texts)==len(set(texts)), 'uncertain retry created duplicate') + times = [started]+sorted(e[1] for e in successes)+[ended] + max_gap = max(b-a for a,b in zip(times,times[1:])) + check(max_gap<=10, 'writes stalled for more than 10 seconds') + report = {'action':action,'target':target,'duration_seconds':round(ended-started,3), + 'successful_writes':len(successes),'failed_attempts':len(failures), + 'successful_rps':round(len(successes)/(ended-started),3), + 'first_recovery_seconds':round(recovery,3) if recovery is not None else None, + 'max_success_gap_seconds':round(max_gap,3), + 'instances_before':sorted(before),'instances_after':sorted(task_ids()), + 'confirmed_messages_checked':len(confirmations)+1, + 'duplicate_bodies':0} + print(json.dumps(report),flush=True) + return report + + +def main(): + wait_ready() + c = Client() + results = {'idempotency':idempotency(c,*c.bootstrap())} + originals = sorted(task_ids()) + check(len(originals)==2,'grade4 scenario expects exactly two API replicas') + results['failovers'] = [exercise('stop-one',target) for target in originals] + results['rolling_update'] = exercise('update') + (ROOT/'evidence/swarm-grade4.json').write_text(json.dumps(results,indent=2)+'\n') + print('PASS: each original replica stopped and replaced; rolling update under load; confirmations and sessions preserved') + + +if __name__=='__main__': + run(main)