diff --git a/.gitignore b/.gitignore index b5a71896..a085fb7a 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,5 @@ coverage/ .vscode/ # Keep small, sanitized evidence summaries in evidence/ under version control. # Inspect git diff --cached: ignore rules are not a secret scanner. + +.ruff_cache/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..3146321d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +FROM python:3.13.0-slim-bookworm + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 + +WORKDIR /app + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt \ + && python -m pip uninstall --yes pip \ + && addgroup --system app \ + && adduser --system --ingroup app app + +COPY --chown=app:app src ./src +COPY --chown=app:app migrations ./migrations +COPY --chown=app:app alembic.ini ./alembic.ini + +USER app +EXPOSE 8080 + +HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \ + CMD python -c "import urllib.request; from src.core.settings import settings; urllib.request.urlopen(f'http://127.0.0.1:{settings.app_port}/health/ready', timeout=settings.healthcheck_timeout_seconds)" + +CMD ["python", "-m", "src"] diff --git a/Makefile b/Makefile index 5d86d171..ba293d25 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,17 @@ PYTHON ?= python3 -BASE_URL ?= http://localhost:8080 +ifndef BASE_URL +BASE_URL := $(shell $(PYTHON) scripts/runtime_config.py) +endif +export BASE_URL CA_FILE ?= +API_CONTAINER = $(shell docker compose ps -q api | head -n 1) ACTION_SCRIPT ?= TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) -.PHONY: help check validate test load test-persistence test-failover +.PHONY: help check validate test load test-persistence help: - @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" + @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; test-lab3: concurrency; verify-lab3: full acceptance; research-lab3: scaling study; load: synthetic writes" check: $(PYTHON) scripts/check_contract.py @@ -25,6 +29,99 @@ 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) +.PHONY: test-persistence-proof +test-persistence-proof: + $(PYTHON) scripts/test-persistence.py --action-script "scripts/restart.sh" --base-url "$(BASE_URL)" $(TLS_ARGS) + +.PHONY: up down migrate test-extra +up: + docker compose up -d --build --wait + +down: + docker compose down + +migrate: + docker compose run --rm migration + +test-extra: + $(PYTHON) tests/extra.py --base-url "$(BASE_URL)" $(TLS_ARGS) + +.PHONY: test-regressions +test-regressions: + $(PYTHON) tests/regressions.py --base-url "$(BASE_URL)" $(TLS_ARGS) + +.PHONY: test-production test-production-limits audit-dependencies +test-production: + $(PYTHON) tests/production.py --base-url "$(BASE_URL)" $(TLS_ARGS) + +test-production-limits: + docker exec -i "$(API_CONTAINER)" python < tests/production_limits.py + +audit-dependencies: + $(PYTHON) scripts/audit-dependencies.py $(TLS_ARGS) + +.PHONY: test-db test-expiry test-dependencies +test-db: + docker exec -i "$(API_CONTAINER)" python < tests/database.py + +test-expiry: + PYTHON="$(PYTHON)" scripts/test-expiry.sh + +test-dependencies: + $(PYTHON) tests/dependencies.py --base-url "$(BASE_URL)" $(TLS_ARGS) + +.PHONY: test-passwords +test-passwords: + docker exec -i "$(API_CONTAINER)" python < tests/passwords.py + +.PHONY: lint format +lint: + $(PYTHON) -m ruff check src + $(PYTHON) -m ruff format --check src + +format: + $(PYTHON) -m ruff check --fix src + $(PYTHON) -m ruff format src + +.PHONY: test-auth-logs +test-auth-logs: + $(PYTHON) tests/auth_logging.py --base-url "$(BASE_URL)" $(TLS_ARGS) + +.PHONY: test-settings +test-settings: + docker exec -i "$(API_CONTAINER)" python < tests/settings.py + +.PHONY: test-callbacks +test-callbacks: + docker exec -i "$(API_CONTAINER)" python < tests/callbacks.py + +.PHONY: test-failover test-lab3 rolling-update test-failover: - @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2) + @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT=scripts/stop-one.sh"; exit 2) $(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +test-lab3: + $(PYTHON) tests/lab3.py --base-url "$(BASE_URL)" + +rolling-update: + $(PYTHON) scripts/rolling-update.py + +.PHONY: verify-lab3 research-lab3 +verify-lab3: + $(PYTHON) scripts/verify-lab3.py + +research-lab3: + $(PYTHON) scripts/research-lab3.py --full + $(PYTHON) scripts/plot-lab3.py + +.PHONY: audit-lab3 +audit-lab3: + $(PYTHON) scripts/audit-lab3.py + +.PHONY: audit-edges-lab3 +audit-edges-lab3: + $(PYTHON) scripts/edge-audit-lab3.py + +.PHONY: review-lab3 +review-lab3: + $(PYTHON) scripts/review-lab3.py diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 00000000..d3b7004f --- /dev/null +++ b/alembic.ini @@ -0,0 +1,3 @@ +[alembic] +script_location = %(here)s/migrations +prepend_sys_path = . diff --git a/compose.env.example b/compose.env.example new file mode 100644 index 00000000..ed69c080 --- /dev/null +++ b/compose.env.example @@ -0,0 +1,31 @@ +APP_HOST=0.0.0.0 +APP_PORT=8080 +APP_ENV=lab +POSTGRES_DB=messenger +POSTGRES_USER=messenger +# Задай свой локальный пароль перед запуском. Не добавляй .env в Git. +POSTGRES_PASSWORD= +SESSION_TTL_SECONDS=3600 +# REDIS_URL задаётся в compose.yaml; Redis хранит сессии с TTL и кэш профилей на 60 секунд. +LOG_LEVEL=INFO +# DATABASE_URL и REDIS_URL передаёт Compose. При ручном запуске задай их в окружении. +# Остальные параметры и значения по умолчанию: src/core/settings.py (AppSettings). + +# Стартовый администратор; повторный запуск не меняет существующий пароль. +ADMIN_USERNAME=admin +ADMIN_PASSWORD=admin-password-123 +ADMIN_DISPLAY_NAME=Администратор + +# APP_ENV=production запрещает учебный ADMIN_PASSWORD. Пароль существующего +# администратора должен совпадать с настройкой; автоматического сброса нет. +# HTTP_MAX_BODY_BYTES=65536 +# HTTP_BODY_TIMEOUT_SECONDS=5 +# HTTP_CONCURRENCY_LIMIT=64 +# PASSWORD_HASH_CONCURRENCY=2 + +# Lab3: число реплик и лимиты каждой API-реплики. Наружу доступен только LB. +API_REPLICAS=2 +API_CPUS=0.5 +API_MEMORY=256m +DATABASE_POOL_SIZE=10 +POSTGRES_CPUS=1.0 diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..8fd5e2e9 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,117 @@ +x-logging: &logging + driver: json-file + options: + max-size: "10m" + max-file: "3" + +x-app: &app + build: . + image: mtusi-messenger:lab3 + env_file: ${ENV_FILE:-.env} + logging: *logging + +services: + postgres: + logging: *logging + image: postgres:16.6-bookworm + cpus: ${POSTGRES_CPUS:-1.0} + mem_limit: 512m + environment: + POSTGRES_DB: ${POSTGRES_DB:-messenger} + POSTGRES_USER: ${POSTGRES_USER:-messenger} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Задай POSTGRES_PASSWORD в .env} + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"] + interval: 2s + timeout: 3s + retries: 30 + + redis: + logging: *logging + image: redis:7.4.2-bookworm + cpus: 0.25 + mem_limit: 128m + command: ["redis-server", "--appendonly", "yes", "--appendfsync", "always", "--maxmemory", "64mb", "--maxmemory-policy", "noeviction"] + volumes: + - redis_data:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 2s + timeout: 3s + retries: 30 + + migration: + <<: *app + command: ["alembic", "upgrade", "head"] + cpus: 0.5 + mem_limit: 256m + environment: + REDIS_URL: &redis_url redis://redis:6379/0 + DATABASE_URL: &database_url postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:?Задай POSTGRES_PASSWORD в .env}@postgres:5432/${POSTGRES_DB:-messenger} + depends_on: + postgres: + condition: service_healthy + healthcheck: + disable: true + + api: + <<: *app + cpus: ${API_CPUS:-0.5} + mem_limit: ${API_MEMORY:-256m} + stop_grace_period: 15s + deploy: + replicas: ${API_REPLICAS:-2} + read_only: true + tmpfs: + - /tmp:rw,noexec,nosuid,size=16m + cap_drop: [ALL] + security_opt: ["no-new-privileges:true"] + environment: + APP_HOST: 0.0.0.0 + APP_PORT: 8080 + DATABASE_URL: *database_url + REDIS_URL: *redis_url + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + LOG_LEVEL: ${LOG_LEVEL:-INFO} + APP_WORKERS: 1 + DATABASE_POOL_SIZE: ${DATABASE_POOL_SIZE:-10} + DATABASE_POOL_TIMEOUT_SECONDS: 2 + DATABASE_CONNECT_TIMEOUT_SECONDS: 1 + DATABASE_COMMAND_TIMEOUT_SECONDS: 3 + DATABASE_STATEMENT_TIMEOUT_MS: 2500 + SHUTDOWN_TIMEOUT_SECONDS: 10 + expose: ["8080"] + depends_on: + migration: + condition: service_completed_successfully + redis: + condition: service_healthy + healthcheck: + interval: 2s + timeout: 3s + retries: 30 + start_period: 10s + + lb: + image: haproxy:3.0.12-alpine + logging: *logging + cpus: 0.5 + mem_limit: 128m + read_only: true + tmpfs: ["/tmp:rw,noexec,nosuid,size=4m"] + security_opt: ["no-new-privileges:true"] + ports: + - "127.0.0.1:${APP_PORT:-8080}:8080" + volumes: + - ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro + depends_on: + api: + condition: service_healthy + stop_signal: SIGUSR1 + stop_grace_period: 15s + +volumes: + redis_data: + postgres_data: diff --git a/evidence/lab3-500-headers.txt b/evidence/lab3-500-headers.txt new file mode 100644 index 00000000..f4ee4f35 --- /dev/null +++ b/evidence/lab3-500-headers.txt @@ -0,0 +1,49 @@ +PASS: 500 has X-Request-Id/X-Instance-Id and hides internal exception details +2026-11-06 15:29:19,207 ERROR src.main: Необработанная ошибка запроса +Traceback (most recent call last): + File "/app/src/main.py", line 37, in add_request_id + response = await call_next(request) + ^^^^^^^^^^^^^^^^^^^^^^^^ + File "/usr/local/lib/python3.13/site-packages/starlette/middleware/base.py", line 173, in call_next + raise app_exc from app_exc.__cause__ or app_exc.__context__ + File "/usr/local/lib/python3.13/site-packages/starlette/middleware/base.py", line 149, in coro + await self.app(scope, receive_or_disconnect, send_no_error) + File "/app/src/core/request_limits.py", line 34, in __call__ + await self.app(scope, receive, send) + File "/usr/local/lib/python3.13/site-packages/starlette/middleware/exceptions.py", line 63, in __call__ + await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) + File "/usr/local/lib/python3.13/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app + raise exc + File "/usr/local/lib/python3.13/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app + await app(scope, receive, sender) + File "/usr/local/lib/python3.13/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__ + await self.app(scope, receive, send) + File "/usr/local/lib/python3.13/site-packages/starlette/routing.py", line 676, in __call__ + await self.middleware_stack(scope, receive, send) + File "/usr/local/lib/python3.13/site-packages/fastapi/routing.py", line 2734, in app + await route.handle(scope, receive, send) + File "/usr/local/lib/python3.13/site-packages/fastapi/routing.py", line 1281, in handle + await super().handle(scope, receive, send) + File "/usr/local/lib/python3.13/site-packages/starlette/routing.py", line 282, in handle + await self.app(scope, receive, send) + File "/usr/local/lib/python3.13/site-packages/fastapi/routing.py", line 158, in app + await wrap_app_handling_exceptions(app, request)(scope, receive, send) + File "/usr/local/lib/python3.13/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app + raise exc + File "/usr/local/lib/python3.13/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app + await app(scope, receive, sender) + File "/usr/local/lib/python3.13/site-packages/fastapi/routing.py", line 144, in app + response = await f(request) + ^^^^^^^^^^^^^^^^ + File "/usr/local/lib/python3.13/site-packages/fastapi/routing.py", line 706, in app + raw_response = await run_endpoint_function( + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + ...<3 lines>... + ) + ^ + File "/usr/local/lib/python3.13/site-packages/fastapi/routing.py", line 352, in run_endpoint_function + return await dependant.call(**values) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "", line 13, in unexpected +RuntimeError: audit-internal-detail +2026-11-06 15:29:19,287 INFO src.main: ид_запроса=78e3b0b2-f02d-4294-9dc7-24ec66167566 метод=GET путь='/_audit_unexpected_error' статус=500 длительность_мс=83.705 diff --git a/evidence/lab3-audit-before.json b/evidence/lab3-audit-before.json new file mode 100644 index 00000000..4d2d8dca --- /dev/null +++ b/evidence/lab3-audit-before.json @@ -0,0 +1,5 @@ +{ + "nul_message_status": 400, + "missing_idempotency_table_ready_status": 200, + "missing_idempotency_table_business_status": 200 +} diff --git a/evidence/lab3-audit-changing-ips.json b/evidence/lab3-audit-changing-ips.json new file mode 100644 index 00000000..1bd3b6c5 --- /dev/null +++ b/evidence/lab3-audit-changing-ips.json @@ -0,0 +1,32 @@ +{ + "result": "PASS", + "events": [ + { + "old_container": "70d88d4f5925", + "old_ip": "172.25.0.5", + "old_instance": "4cff52af-d57e-4dad-82b4-1b9797fb0aac", + "new_container": "4856ec40e6db", + "new_ip": "172.25.0.7", + "new_instance": "a916210d-1bd0-4086-9e21-7c13158a9ee0", + "discovery_after_ready_seconds": 0.138 + }, + { + "old_container": "0b6ad97c4510", + "old_ip": "172.25.0.4", + "old_instance": "a2200766-fb19-4863-889b-4a38019c180e", + "new_container": "6205d0db9981", + "new_ip": "172.25.0.8", + "new_instance": "f9491c25-70ac-4b45-935d-f742415b6366", + "discovery_after_ready_seconds": 0.151 + } + ], + "observed_instances_after": [ + "a916210d-1bd0-4086-9e21-7c13158a9ee0", + "f9491c25-70ac-4b45-935d-f742415b6366" + ], + "lb_restarted": false, + "old_ips_kept_occupied_during_test": [ + "172.25.0.5", + "172.25.0.4" + ] +} diff --git a/evidence/lab3-audit-contract.json b/evidence/lab3-audit-contract.json new file mode 100644 index 00000000..dea6d32d --- /dev/null +++ b/evidence/lab3-audit-contract.json @@ -0,0 +1,22 @@ +{ + "result": "PASS", + "simultaneous_requests": 16, + "observed_instances": [ + "4cff52af-d57e-4dad-82b4-1b9797fb0aac", + "a2200766-fb19-4863-889b-4a38019c180e" + ], + "database_rows_for_first_key": 1, + "headers_413": "present", + "nul_roundtrip": "201, replay and history; 4000 code points supported", + "checks": [ + "invalid cursor types/limit", + "UTF-8 Basic", + "login body rejected", + "unknown member 400", + "key scope/format", + "foreign cursor", + "fresh history limit=7 and other limits", + "outsider hidden", + "raw invalid JSON rejected" + ] +} diff --git a/evidence/lab3-audit-first-run.txt b/evidence/lab3-audit-first-run.txt new file mode 100644 index 00000000..46cea78b --- /dev/null +++ b/evidence/lab3-audit-first-run.txt @@ -0,0 +1,30 @@ +Checking contract +contract PASS +Checking readiness +readiness PASS +Checking frozen-stores + Container mtusi2026lab1-postgres-1 Paused + Container mtusi2026lab1-postgres-1 Unpaused +Traceback (most recent call last): + File "/Users/andrejkamenskij/PycharmProjects/mtusi2026lab1/scripts/audit-lab3.py", line 494, in + main() + ~~~~^^ + File "/Users/andrejkamenskij/PycharmProjects/mtusi2026lab1/scripts/audit-lab3.py", line 482, in main + result = check() + File "/Users/andrejkamenskij/PycharmProjects/mtusi2026lab1/scripts/audit-lab3.py", line 284, in frozen_stores + transition = wire("GET", "/health/ready") + File "/Users/andrejkamenskij/PycharmProjects/mtusi2026lab1/scripts/audit-lab3.py", line 65, in wire + response = conn.getresponse() + File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/http/client.py", line 1428, in getresponse + response.begin() + ~~~~~~~~~~~~~~^^ + File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/http/client.py", line 331, in begin + version, status, reason = self._read_status() + ~~~~~~~~~~~~~~~~~^^ + File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/http/client.py", line 292, in _read_status + line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") + ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^ + File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/socket.py", line 719, in readinto + return self._sock.recv_into(b) + ~~~~~~~~~~~~~~~~~~~~^^^ +TimeoutError: timed out diff --git a/evidence/lab3-audit-frozen-stores.json b/evidence/lab3-audit-frozen-stores.json new file mode 100644 index 00000000..8f1b3daa --- /dev/null +++ b/evidence/lab3-audit-frozen-stores.json @@ -0,0 +1,43 @@ +{ + "result": "PASS", + "experiments": [ + { + "service": "postgres", + "initial_ready_request": { + "status": 503, + "seconds": 2.006, + "instance": "a2200766-fb19-4863-889b-4a38019c180e" + }, + "business": { + "status": 503, + "seconds": 0.004, + "instance": null + }, + "registration": { + "status": 503, + "seconds": 0.003, + "instance": null + }, + "elapsed_seconds": 6.132 + }, + { + "service": "redis", + "initial_ready_request": { + "status": 503, + "seconds": 0.311, + "instance": "a2200766-fb19-4863-889b-4a38019c180e" + }, + "business": { + "status": 503, + "seconds": 0.005, + "instance": null + }, + "registration": { + "status": 503, + "seconds": 0.005, + "instance": null + }, + "elapsed_seconds": 4.433 + } + ] +} diff --git a/evidence/lab3-audit-limits-evidence.json b/evidence/lab3-audit-limits-evidence.json new file mode 100644 index 00000000..ffd3e83d --- /dev/null +++ b/evidence/lab3-audit-limits-evidence.json @@ -0,0 +1,52 @@ +{ + "result": "PASS", + "limits": { + "api": [ + { + "container": "4856ec40e6db", + "cpu": 0.5, + "memory_bytes": 268435456 + }, + { + "container": "6205d0db9981", + "cpu": 0.5, + "memory_bytes": 268435456 + } + ], + "postgres": [ + { + "container": "ec54dbe8e8dd", + "cpu": 1.0, + "memory_bytes": 536870912 + } + ], + "redis": [ + { + "container": "f71bc2ef45c8", + "cpu": 0.25, + "memory_bytes": 134217728 + } + ], + "lb": [ + { + "container": "279562a6ff00", + "cpu": 0.5, + "memory_bytes": 134217728 + } + ], + "migration": [ + { + "container": "5a2ccb851cbb", + "cpu": 0.5, + "memory_bytes": 268435456 + } + ] + }, + "fixed_api_budget": { + "cpu": 2, + "memory_mib": 1024 + }, + "research_runs": 18, + "rolling_command": "python3 scripts/rolling-update.py", + "shell_hooks_executable": true +} diff --git a/evidence/lab3-audit-readiness.json b/evidence/lab3-audit-readiness.json new file mode 100644 index 00000000..025346b9 --- /dev/null +++ b/evidence/lab3-audit-readiness.json @@ -0,0 +1,275 @@ +{ + "result": "PASS", + "experiments": [ + { + "missing_table": "users", + "direct_readiness": { + "70d88d4f5925": 503, + "0b6ad97c4510": 503 + }, + "business_requests": [ + { + "status": 503, + "seconds": 0.004, + "instance": "a2200766-fb19-4863-889b-4a38019c180e" + }, + { + "status": 503, + "seconds": 0.004, + "instance": "4cff52af-d57e-4dad-82b4-1b9797fb0aac" + }, + { + "status": 503, + "seconds": 0.003, + "instance": "a2200766-fb19-4863-889b-4a38019c180e" + }, + { + "status": 503, + "seconds": 0.003, + "instance": "4cff52af-d57e-4dad-82b4-1b9797fb0aac" + }, + { + "status": 503, + "seconds": 0.003, + "instance": "a2200766-fb19-4863-889b-4a38019c180e" + }, + { + "status": 503, + "seconds": 0.004, + "instance": "4cff52af-d57e-4dad-82b4-1b9797fb0aac" + }, + { + "status": 503, + "seconds": 0.003, + "instance": "a2200766-fb19-4863-889b-4a38019c180e" + }, + { + "status": 503, + "seconds": 0.003, + "instance": "4cff52af-d57e-4dad-82b4-1b9797fb0aac" + } + ], + "registration": { + "status": 503, + "seconds": 0.156, + "instance": "a2200766-fb19-4863-889b-4a38019c180e" + } + }, + { + "missing_table": "chats", + "direct_readiness": { + "70d88d4f5925": 503, + "0b6ad97c4510": 503 + }, + "business_requests": [ + { + "status": 503, + "seconds": 0.002, + "instance": null + }, + { + "status": 503, + "seconds": 0.002, + "instance": null + }, + { + "status": 503, + "seconds": 0.002, + "instance": null + }, + { + "status": 503, + "seconds": 0.002, + "instance": null + }, + { + "status": 503, + "seconds": 0.002, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + } + ], + "registration": { + "status": 503, + "seconds": 0.001, + "instance": null + } + }, + { + "missing_table": "chat_members", + "direct_readiness": { + "70d88d4f5925": 503, + "0b6ad97c4510": 503 + }, + "business_requests": [ + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + } + ], + "registration": { + "status": 503, + "seconds": 0.001, + "instance": null + } + }, + { + "missing_table": "messages", + "direct_readiness": { + "70d88d4f5925": 503, + "0b6ad97c4510": 503 + }, + "business_requests": [ + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.002, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + } + ], + "registration": { + "status": 503, + "seconds": 0.002, + "instance": null + } + }, + { + "missing_table": "message_requests", + "direct_readiness": { + "70d88d4f5925": 503, + "0b6ad97c4510": 503 + }, + "business_requests": [ + { + "status": 503, + "seconds": 0.002, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.002, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.001, + "instance": null + }, + { + "status": 503, + "seconds": 0.002, + "instance": null + } + ], + "registration": { + "status": 503, + "seconds": 0.001, + "instance": null + } + } + ] +} diff --git a/evidence/lab3-audit-run.txt b/evidence/lab3-audit-run.txt new file mode 100644 index 00000000..e06b0edb --- /dev/null +++ b/evidence/lab3-audit-run.txt @@ -0,0 +1,30 @@ +Checking contract +contract PASS +Checking readiness +readiness PASS +Checking frozen-stores + Container mtusi2026lab1-postgres-1 Paused + Container mtusi2026lab1-postgres-1 Unpaused + Container mtusi2026lab1-redis-1 Paused + Container mtusi2026lab1-redis-1 Unpaused +frozen-stores PASS +Checking changing-ips + Container mtusi2026lab1-api-14 Running + Container mtusi2026lab1-api-15 Creating + Container mtusi2026lab1-api-15 Created + Container mtusi2026lab1-api-15 Starting + Container mtusi2026lab1-api-15 Started + Container mtusi2026lab1-api-15 Running + Container mtusi2026lab1-api-16 Creating + Container mtusi2026lab1-api-16 Created + Container mtusi2026lab1-api-16 Starting + Container mtusi2026lab1-api-16 Started + Container mtusi2026lab1-api-15 Running + Container mtusi2026lab1-api-16 Running + Container mtusi2026lab1-api-16 Waiting + Container mtusi2026lab1-api-15 Waiting + Container mtusi2026lab1-api-16 Healthy + Container mtusi2026lab1-api-15 Healthy +changing-ips PASS +Checking limits-evidence +limits-evidence PASS diff --git a/evidence/lab3-audit-summary-all.json b/evidence/lab3-audit-summary-all.json new file mode 100644 index 00000000..78a62910 --- /dev/null +++ b/evidence/lab3-audit-summary-all.json @@ -0,0 +1,7 @@ +{ + "contract": "PASS", + "readiness": "PASS", + "frozen-stores": "PASS", + "changing-ips": "PASS", + "limits-evidence": "PASS" +} diff --git a/evidence/lab3-cache-edge-before.json b/evidence/lab3-cache-edge-before.json new file mode 100644 index 00000000..14edf7a0 --- /dev/null +++ b/evidence/lab3-cache-edge-before.json @@ -0,0 +1,9 @@ +{ + "surrogate": { + "status": 500 + }, + "extreme_timezone": { + "status": 200, + "created_at": "0001-01-01T00:00:00+23:59" + } +} diff --git a/evidence/lab3-clean-start.json b/evidence/lab3-clean-start.json new file mode 100644 index 00000000..3fbde009 --- /dev/null +++ b/evidence/lab3-clean-start.json @@ -0,0 +1,8 @@ +{ + "result": "PASS", + "project": "lab3-audit-clean-f4312125", + "schema_revision": "006", + "public_smoke": "PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.", + "empty_volumes": true, + "temporary_project_removed": true +} diff --git a/evidence/lab3-clean-start.txt b/evidence/lab3-clean-start.txt new file mode 100644 index 00000000..3fbde009 --- /dev/null +++ b/evidence/lab3-clean-start.txt @@ -0,0 +1,8 @@ +{ + "result": "PASS", + "project": "lab3-audit-clean-f4312125", + "schema_revision": "006", + "public_smoke": "PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.", + "empty_volumes": true, + "temporary_project_removed": true +} diff --git a/evidence/lab3-control/method.json b/evidence/lab3-control/method.json new file mode 100644 index 00000000..5210785d --- /dev/null +++ b/evidence/lab3-control/method.json @@ -0,0 +1,81 @@ +{ + "host": "macOS-15.6.1-arm64-arm-64bit-Mach-O", + "architecture": "arm64", + "docker": { + "ServerVersion": "28.5.1", + "NCPU": 10, + "MemTotal": 8218034176, + "KernelVersion": "6.10.14-linuxkit", + "OperatingSystem": "Docker Desktop", + "Architecture": "aarch64" + }, + "application": { + "schema_revision": "006", + "message_storage": "UTF-8 bytea", + "source_sha256": { + "src/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "src/__main__.py": "b1b472fa858b203911db455eb5c25965b961510985ec4d2c94f627d378aaa9f8", + "src/api/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "src/api/dependencies.py": "73c3f94d7c14d6c34031214933340124ebc4e22730b2b1bf8a2eb89a0d42e986", + "src/api/router.py": "6e78524634e76036418867e6f8a4fa83874213122d39728441a64ef174da2152", + "src/api/v1/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "src/api/v1/routers/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "src/api/v1/routers/auth/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "src/api/v1/routers/auth/callback.py": "69a2cde6ac37f445e64d9afdea55437240846d158460e6fc5601ff0ce2045d2c", + "src/api/v1/routers/auth/router.py": "075648602d66bdc3497c4714d9789b2ea5661501478b05ba845b65c55a725fd8", + "src/api/v1/routers/auth/schemas.py": "ba3345570c35b8a9e55751202f9c8a27f7d338cfcd85b300c196a43ac03aa311", + "src/api/v1/routers/chats/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "src/api/v1/routers/chats/router.py": "85cac2d5ade6917c99e4e6a82053f681e1d253eca4fe3eee726e9f7621635bce", + "src/api/v1/routers/chats/schemas.py": "00e0fc283069cca6c7929ef8d704aa1e577e6bfb555a496eb1b8170231c3b240", + "src/api/v1/routers/messages/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "src/api/v1/routers/messages/router.py": "4e53d6fe338518c165194b9de319ce9d725eb1808567d68c1a6abb002d6edd19", + "src/api/v1/routers/messages/schemas.py": "c0f4d7f3052d725ee4ca1bc093359be90a8a857e436df706ec179171114cc644", + "src/api/v1/routers/users/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "src/api/v1/routers/users/router.py": "fedd9cb7da2fb21caccd8ae2080d013719b4f13382dc795de880fb8ca87f857f", + "src/api/v1/routers/users/schemas.py": "c30356333a3f6bd1b80b8cf6f4208d550201adfaaa0667e1db074c4aecc70dd7", + "src/api/v1/schemas.py": "403d5821588d8abcc33a285cd2689be5eba0e7ab0373d1e17856ff0464934766", + "src/core/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "src/core/cache.py": "1973038f948f2eff50b18ba9c0ea3aa1eedd712932a41045ba6dd18dee358966", + "src/core/callback.py": "9046a504d8324a32e428ff8f4d159d452160828d470e3205400473394d380fee", + "src/core/db/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "src/core/db/models.py": "4597db6b0170e14873046bdeadd544bc75909dc0e242e4896f1ce3bce533850a", + "src/core/db/repo/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "src/core/db/repo/auth.py": "63fb17b55cf05a4b05c5f0b08d3e4f6dc6c83981c7f9d89a0d798b0d270283f2", + "src/core/db/repo/chat.py": "fa1042b689e1e2242360d1432ca84ca49f35a6a2de03810135907333466e76fd", + "src/core/db/repo/message.py": "8589f68ab991126ad48c32f4629db96dac0f5b1ba8203ab750ca317ef696b1ac", + "src/core/db/repo/user.py": "f1d36be8530772e6a969687e78bc7bb9bfe7daf8be67d1e079acb1e6c9d7017b", + "src/core/db/session.py": "9d4c07446ccc0990843c7f978c487a9976f9d926bf1e69a57f88021f742c7c5f", + "src/core/db/types.py": "1e8890cfa43c31ce77ce0c95f0a8a535d0730835db16f33b086226bb189ad7aa", + "src/core/errors.py": "5f93764110b969832766be8a6302515edf2eecc44de4d43c4d350c9c20d31432", + "src/core/pagination.py": "3e1c7e5232dc27688f20c622f312ea10d5ce45e9d3960a4c1b61af3e885079c6", + "src/core/readiness.py": "4bffdfbfa4eda45ac4c065251c99bd62cb18280120b32d4b74360519c87b0d98", + "src/core/request_limits.py": "68bc5406f7d867280c96ebce1c798fd5a34fe7853298dcf1e2a286a9e6c547ac", + "src/core/security.py": "64d6769b36090ab2824f19668f99d23669f838e82d6a785636c339b7c1876663", + "src/core/session_store.py": "874b3ffa5b52b5b6a4587dc785784f41b6958d1b3be5a6a62470623f2182a6c1", + "src/core/settings.py": "cc5892ad7c7900c0642630cfc286f865b944631f2a213240376e0ee115d0b848", + "src/main.py": "89bdd35baef98621e9c287db7911ff72bec8768604617b525f7afd948fd877ff", + "src/service/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "src/service/auth.py": "c979b1bdf157d7468ec584093e56a16abbb6b3fb25da0a1cce6eecac792db5ce", + "src/service/chat.py": "baa2b0eb570fa0965a0e4b929313ecb1c96464997b592872f4ed193e22c48489", + "src/service/dto.py": "ce785b26adc2f807aecb742d392867e0fdd11603d5a8058ce6d9dc0bf0a7d6a2", + "src/service/exceptions.py": "32a232596795b28c1ca5ebbd19ed938cc6086263ceaf476716e9ae310da9f685", + "src/service/message.py": "e00315c45b11b8cf1d05ad1960afe59d0ebf8992efbb6d8d431a12dfc07ebadb", + "src/service/user.py": "44e561c97aff49d358e5f9ed02cfe6ae85abdd384423fe9dcc7b9b05f13f4bbb" + }, + "configuration_sha256": { + "compose.yaml": "704f9d6ab2bd183e38353b8de74e918f414c673fe45b54de3fd307fe949adce8", + "infra/haproxy.cfg": "6640795207cee655e5c22d685d73b2a57ae1a987156783ad5226d57d7ed6d59c", + "scripts/benchmark.py": "d93a933bea29400700c1ad9825929a7031406134eeffe704b7b615844549b5f5" + } + }, + "method": { + "warmup_seconds": 3, + "repeats": 1, + "duration_seconds": 8, + "concurrency": 32, + "profile": "80% POST unique keys, 20% GET first page limit20, 20 seed messages; fresh chat per run", + "generator": "Python http.client, one persistent connection per thread, closed-loop, no retries", + "limits": "PostgreSQL 0.25 CPU/512MiB baseline, LB 0.5CPU/128MiB, Redis 0.25CPU/128MiB", + "randomization_seed": 20261106 + } +} diff --git a/evidence/lab3-control/results.json b/evidence/lab3-control/results.json new file mode 100644 index 00000000..a8449ed7 --- /dev/null +++ b/evidence/lab3-control/results.json @@ -0,0 +1,280 @@ +[ + { + "job": { + "series": "growing", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 8, + "elapsed_seconds": 8.103, + "concurrency": 32, + "successes": { + "write": 1302, + "read": 308 + }, + "errors": {}, + "rps": 198.697, + "all_attempt_latency_ms": { + "p50": 155.692, + "p95": 302.563, + "p99": 403.351 + }, + "successful_latency_ms": { + "p50": 155.692, + "p95": 302.563, + "p99": 403.351 + }, + "instances": [ + "339b0a87-f7b9-4f08-9660-689a35b5361f", + "5321e930-7ae6-4b67-841f-2a98b114585d", + "bf0dbeaf-b05a-4b05-aa10-5854c055d688", + "dd38948b-a08a-49c2-9b4e-c03c727d7d7f" + ], + "seconds": { + "0": { + "success": 201, + "error": 0 + }, + "1": { + "success": 192, + "error": 0 + }, + "2": { + "success": 178, + "error": 0 + }, + "3": { + "success": 203, + "error": 0 + }, + "4": { + "success": 194, + "error": 0 + }, + "5": { + "success": 246, + "error": 0 + }, + "6": { + "success": 176, + "error": 0 + }, + "7": { + "success": 220, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "d58b8e8ce26e": { + "service": "api", + "memory_current_mib": 65.02, + "memory_peak_lifetime_mib": 125.65, + "cpu_cores_used": 0.554, + "throttled_seconds": 0.456, + "throttled_period_ratio": 0.163 + }, + "29dc6359d3e7": { + "service": "api", + "memory_current_mib": 64.79, + "memory_peak_lifetime_mib": 126.68, + "cpu_cores_used": 0.569, + "throttled_seconds": 0.565, + "throttled_period_ratio": 0.186 + }, + "d9839970a49e": { + "service": "api", + "memory_current_mib": 65.02, + "memory_peak_lifetime_mib": 126.87, + "cpu_cores_used": 0.535, + "throttled_seconds": 0.63, + "throttled_period_ratio": 0.185 + }, + "28df70fb850b": { + "service": "api", + "memory_current_mib": 65.38, + "memory_peak_lifetime_mib": 125.32, + "cpu_cores_used": 0.46, + "throttled_seconds": 0.417, + "throttled_period_ratio": 0.128 + }, + "49bc2fa29e8a": { + "service": "postgres", + "memory_current_mib": 124.34, + "memory_peak_lifetime_mib": 128.11, + "cpu_cores_used": 0.265, + "throttled_seconds": 38.655, + "throttled_period_ratio": 0.903 + }, + "f71bc2ef45c8": { + "service": "redis", + "memory_current_mib": 10.61, + "memory_peak_lifetime_mib": 15.91, + "cpu_cores_used": 0.024, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "memory_current_mib": 17.48, + "memory_peak_lifetime_mib": 19.04, + "cpu_cores_used": 0.055, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 1573, + "rollbacks": 1655, + "blocks_read": 375, + "blocks_hit": 114030, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "improved-db", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 1, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 8, + "elapsed_seconds": 8.115, + "concurrency": 32, + "successes": { + "write": 3854, + "read": 941 + }, + "errors": { + "503": 10 + }, + "rps": 590.851, + "all_attempt_latency_ms": { + "p50": 17.076, + "p95": 169.569, + "p99": 263.091 + }, + "successful_latency_ms": { + "p50": 17.025, + "p95": 169.721, + "p99": 264.503 + }, + "instances": [ + "339b0a87-f7b9-4f08-9660-689a35b5361f", + "5321e930-7ae6-4b67-841f-2a98b114585d", + "bf0dbeaf-b05a-4b05-aa10-5854c055d688", + "dd38948b-a08a-49c2-9b4e-c03c727d7d7f" + ], + "seconds": { + "0": { + "success": 668, + "error": 1 + }, + "1": { + "success": 637, + "error": 0 + }, + "2": { + "success": 433, + "error": 0 + }, + "3": { + "success": 506, + "error": 0 + }, + "4": { + "success": 738, + "error": 3 + }, + "5": { + "success": 647, + "error": 3 + }, + "6": { + "success": 519, + "error": 3 + }, + "7": { + "success": 647, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "d58b8e8ce26e": { + "service": "api", + "memory_current_mib": 68.0, + "memory_peak_lifetime_mib": 130.5, + "cpu_cores_used": 0.881, + "throttled_seconds": 2.226, + "throttled_period_ratio": 0.349 + }, + "29dc6359d3e7": { + "service": "api", + "memory_current_mib": 69.26, + "memory_peak_lifetime_mib": 126.68, + "cpu_cores_used": 0.899, + "throttled_seconds": 2.23, + "throttled_period_ratio": 0.489 + }, + "d9839970a49e": { + "service": "api", + "memory_current_mib": 71.18, + "memory_peak_lifetime_mib": 142.96, + "cpu_cores_used": 0.87, + "throttled_seconds": 2.278, + "throttled_period_ratio": 0.333 + }, + "28df70fb850b": { + "service": "api", + "memory_current_mib": 71.35, + "memory_peak_lifetime_mib": 125.32, + "cpu_cores_used": 0.948, + "throttled_seconds": 2.153, + "throttled_period_ratio": 0.385 + }, + "2bf17307d821": { + "service": "postgres", + "memory_current_mib": 129.41, + "memory_peak_lifetime_mib": 133.3, + "cpu_cores_used": 0.578, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "f71bc2ef45c8": { + "service": "redis", + "memory_current_mib": 10.71, + "memory_peak_lifetime_mib": 16.09, + "cpu_cores_used": 0.058, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "memory_current_mib": 17.3, + "memory_peak_lifetime_mib": 19.23, + "cpu_cores_used": 0.118, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 4573, + "rollbacks": 4657, + "blocks_read": 30, + "blocks_hit": 378497, + "temp_bytes": 0 + } + } +] diff --git a/evidence/lab3-control/run-01.json b/evidence/lab3-control/run-01.json new file mode 100644 index 00000000..816d64de --- /dev/null +++ b/evidence/lab3-control/run-01.json @@ -0,0 +1,138 @@ +{ + "job": { + "series": "growing", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 8, + "elapsed_seconds": 8.103, + "concurrency": 32, + "successes": { + "write": 1302, + "read": 308 + }, + "errors": {}, + "rps": 198.697, + "all_attempt_latency_ms": { + "p50": 155.692, + "p95": 302.563, + "p99": 403.351 + }, + "successful_latency_ms": { + "p50": 155.692, + "p95": 302.563, + "p99": 403.351 + }, + "instances": [ + "339b0a87-f7b9-4f08-9660-689a35b5361f", + "5321e930-7ae6-4b67-841f-2a98b114585d", + "bf0dbeaf-b05a-4b05-aa10-5854c055d688", + "dd38948b-a08a-49c2-9b4e-c03c727d7d7f" + ], + "seconds": { + "0": { + "success": 201, + "error": 0 + }, + "1": { + "success": 192, + "error": 0 + }, + "2": { + "success": 178, + "error": 0 + }, + "3": { + "success": 203, + "error": 0 + }, + "4": { + "success": 194, + "error": 0 + }, + "5": { + "success": 246, + "error": 0 + }, + "6": { + "success": 176, + "error": 0 + }, + "7": { + "success": 220, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "d58b8e8ce26e": { + "service": "api", + "memory_current_mib": 65.02, + "memory_peak_lifetime_mib": 125.65, + "cpu_cores_used": 0.554, + "throttled_seconds": 0.456, + "throttled_period_ratio": 0.163 + }, + "29dc6359d3e7": { + "service": "api", + "memory_current_mib": 64.79, + "memory_peak_lifetime_mib": 126.68, + "cpu_cores_used": 0.569, + "throttled_seconds": 0.565, + "throttled_period_ratio": 0.186 + }, + "d9839970a49e": { + "service": "api", + "memory_current_mib": 65.02, + "memory_peak_lifetime_mib": 126.87, + "cpu_cores_used": 0.535, + "throttled_seconds": 0.63, + "throttled_period_ratio": 0.185 + }, + "28df70fb850b": { + "service": "api", + "memory_current_mib": 65.38, + "memory_peak_lifetime_mib": 125.32, + "cpu_cores_used": 0.46, + "throttled_seconds": 0.417, + "throttled_period_ratio": 0.128 + }, + "49bc2fa29e8a": { + "service": "postgres", + "memory_current_mib": 124.34, + "memory_peak_lifetime_mib": 128.11, + "cpu_cores_used": 0.265, + "throttled_seconds": 38.655, + "throttled_period_ratio": 0.903 + }, + "f71bc2ef45c8": { + "service": "redis", + "memory_current_mib": 10.61, + "memory_peak_lifetime_mib": 15.91, + "cpu_cores_used": 0.024, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "memory_current_mib": 17.48, + "memory_peak_lifetime_mib": 19.04, + "cpu_cores_used": 0.055, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 1573, + "rollbacks": 1655, + "blocks_read": 375, + "blocks_hit": 114030, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-control/run-02.json b/evidence/lab3-control/run-02.json new file mode 100644 index 00000000..52c9479e --- /dev/null +++ b/evidence/lab3-control/run-02.json @@ -0,0 +1,140 @@ +{ + "job": { + "series": "improved-db", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 1, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 8, + "elapsed_seconds": 8.115, + "concurrency": 32, + "successes": { + "write": 3854, + "read": 941 + }, + "errors": { + "503": 10 + }, + "rps": 590.851, + "all_attempt_latency_ms": { + "p50": 17.076, + "p95": 169.569, + "p99": 263.091 + }, + "successful_latency_ms": { + "p50": 17.025, + "p95": 169.721, + "p99": 264.503 + }, + "instances": [ + "339b0a87-f7b9-4f08-9660-689a35b5361f", + "5321e930-7ae6-4b67-841f-2a98b114585d", + "bf0dbeaf-b05a-4b05-aa10-5854c055d688", + "dd38948b-a08a-49c2-9b4e-c03c727d7d7f" + ], + "seconds": { + "0": { + "success": 668, + "error": 1 + }, + "1": { + "success": 637, + "error": 0 + }, + "2": { + "success": 433, + "error": 0 + }, + "3": { + "success": 506, + "error": 0 + }, + "4": { + "success": 738, + "error": 3 + }, + "5": { + "success": 647, + "error": 3 + }, + "6": { + "success": 519, + "error": 3 + }, + "7": { + "success": 647, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "d58b8e8ce26e": { + "service": "api", + "memory_current_mib": 68.0, + "memory_peak_lifetime_mib": 130.5, + "cpu_cores_used": 0.881, + "throttled_seconds": 2.226, + "throttled_period_ratio": 0.349 + }, + "29dc6359d3e7": { + "service": "api", + "memory_current_mib": 69.26, + "memory_peak_lifetime_mib": 126.68, + "cpu_cores_used": 0.899, + "throttled_seconds": 2.23, + "throttled_period_ratio": 0.489 + }, + "d9839970a49e": { + "service": "api", + "memory_current_mib": 71.18, + "memory_peak_lifetime_mib": 142.96, + "cpu_cores_used": 0.87, + "throttled_seconds": 2.278, + "throttled_period_ratio": 0.333 + }, + "28df70fb850b": { + "service": "api", + "memory_current_mib": 71.35, + "memory_peak_lifetime_mib": 125.32, + "cpu_cores_used": 0.948, + "throttled_seconds": 2.153, + "throttled_period_ratio": 0.385 + }, + "2bf17307d821": { + "service": "postgres", + "memory_current_mib": 129.41, + "memory_peak_lifetime_mib": 133.3, + "cpu_cores_used": 0.578, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "f71bc2ef45c8": { + "service": "redis", + "memory_current_mib": 10.71, + "memory_peak_lifetime_mib": 16.09, + "cpu_cores_used": 0.058, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "memory_current_mib": 17.3, + "memory_peak_lifetime_mib": 19.23, + "cpu_cores_used": 0.118, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 4573, + "rollbacks": 4657, + "blocks_read": 30, + "blocks_hit": 378497, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-control/summary.csv b/evidence/lab3-control/summary.csv new file mode 100644 index 00000000..727bc457 --- /dev/null +++ b/evidence/lab3-control/summary.csv @@ -0,0 +1,3 @@ +series,replicas,cpus,memory,db,concurrency,repeat,rps,p95_ms,errors,postgres_cores +growing,4,1,256m,0.25,32,0,198.697,302.563,0,0.265 +improved-db,4,1,256m,1,32,0,590.851,169.569,10,0.578 diff --git a/evidence/lab3-db-idempotency.txt b/evidence/lab3-db-idempotency.txt new file mode 100644 index 00000000..b913f834 --- /dev/null +++ b/evidence/lab3-db-idempotency.txt @@ -0,0 +1 @@ +PASS: rollback removes both message/key; committed result replays; expired key can be reused diff --git a/evidence/lab3-edge-audit.json b/evidence/lab3-edge-audit.json new file mode 100644 index 00000000..31b7c89e --- /dev/null +++ b/evidence/lab3-edge-audit.json @@ -0,0 +1,18 @@ +{ + "result": "PASS", + "checks": [ + "duplicate Idempotency-Key and Content-Type rejected", + "strict integer and repeated query parameters", + "NUL/emoji user and chat Unicode roundtrip", + "100 code point names and 4000 code point messages", + "no text trim or silent Unicode normalization", + "16 conflicting simultaneous requests: 8 replayed winners, 8 conflicts, one DB row", + "DB failure after message INSERT rolls back message and key; same key succeeds after recovery", + "invalid cached Unicode/timezone falls back to DB with TTL", + "one TCP connection reaches both API" + ], + "keepalive_instances": [ + "1117b014-edbb-473e-ba6f-107991a36b0c", + "cf4df4a1-2ff5-4d04-a979-42a2425d3b80" + ] +} diff --git a/evidence/lab3-edge-audit.txt b/evidence/lab3-edge-audit.txt new file mode 100644 index 00000000..ba7e29cb --- /dev/null +++ b/evidence/lab3-edge-audit.txt @@ -0,0 +1,18 @@ +{ + "result": "PASS", + "checks": [ + "duplicate Idempotency-Key and Content-Type rejected", + "strict integer and repeated query parameters", + "NUL/emoji user and chat Unicode roundtrip", + "100 code point names and 4000 code point messages", + "no text trim or silent Unicode normalization", + "16 conflicting simultaneous requests: 8 replayed winners, 8 conflicts, one DB row", + "DB failure after message INSERT rolls back message and key; same key succeeds after recovery", + "invalid cached Unicode/timezone falls back to DB with TTL", + "one TCP connection reaches both API" + ], + "keepalive_instances": [ + "a916210d-1bd0-4086-9e21-7c13158a9ee0", + "f9491c25-70ac-4b45-935d-f742415b6366" + ] +} diff --git a/evidence/lab3-extra-audit-before.json b/evidence/lab3-extra-audit-before.json new file mode 100644 index 00000000..8bdf0884 --- /dev/null +++ b/evidence/lab3-extra-audit-before.json @@ -0,0 +1,6 @@ +{ + "duplicate_idempotency_header_status": 201, + "decimal_limit_status": 200, + "nul_display_name_status": 400, + "nul_chat_title_status": 400 +} diff --git a/evidence/lab3-extra.txt b/evidence/lab3-extra.txt new file mode 100644 index 00000000..119d0eb9 --- /dev/null +++ b/evidence/lab3-extra.txt @@ -0,0 +1 @@ +{"результат": "ПРОЙДЕНО", "гонка_регистрации": "1 создан, 5 конфликтов", "unicode_пароль_и_logout_с_кэшем": "ПРОЙДЕНО", "параллельных_клиентов": 10, "создано_сообщений": 100, "уникальных_сообщений": 100} diff --git a/evidence/lab3-failures.json b/evidence/lab3-failures.json new file mode 100644 index 00000000..0c1fc5ce --- /dev/null +++ b/evidence/lab3-failures.json @@ -0,0 +1,182 @@ +[ + { + "duration_seconds": 12, + "elapsed_seconds": 12.038, + "concurrency": 8, + "successes": { + "write": 1158, + "read": 286 + }, + "errors": { + "connection": 16, + "503": 7 + }, + "rps": 119.958, + "all_attempt_latency_ms": { + "p50": 49.938, + "p95": 192.584, + "p99": 204.952 + }, + "successful_latency_ms": { + "p50": 49.17, + "p95": 191.682, + "p99": 201.502 + }, + "instances": [ + "87e7909e-5d06-4d0e-9314-5bdc86b2f617", + "f9491c25-70ac-4b45-935d-f742415b6366" + ], + "seconds": { + "0": { + "success": 285, + "error": 0 + }, + "1": { + "success": 167, + "error": 0 + }, + "2": { + "success": 149, + "error": 0 + }, + "3": { + "success": 22, + "error": 23 + }, + "4": { + "success": 135, + "error": 0 + }, + "5": { + "success": 64, + "error": 0 + }, + "6": { + "success": 48, + "error": 0 + }, + "7": { + "success": 96, + "error": 0 + }, + "8": { + "success": 216, + "error": 0 + }, + "9": { + "success": 149, + "error": 0 + }, + "10": { + "success": 41, + "error": 0 + }, + "11": { + "success": 72, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries", + "first_success_after_action_seconds": { + "read": 0.002, + "write": 0.004 + }, + "stopped_container": "4856ec40e6db", + "stopped_instance": "87e7909e-5d06-4d0e-9314-5bdc86b2f617", + "exit_code": 137, + "all_acknowledged_messages_replayed": 1158, + "failure_started_at_load_second": 3, + "service_after_second_5": "reads/writes continue", + "note": "hard SIGKILL, no automatic retries in load, replay confirmations after failure" + }, + { + "duration_seconds": 12, + "elapsed_seconds": 12.054, + "concurrency": 8, + "successes": { + "write": 1312, + "read": 326 + }, + "errors": { + "connection": 16, + "503": 7 + }, + "rps": 135.894, + "all_attempt_latency_ms": { + "p50": 21.79, + "p95": 180.369, + "p99": 211.697 + }, + "successful_latency_ms": { + "p50": 21.606, + "p95": 179.273, + "p99": 203.16 + }, + "instances": [ + "26994c32-3a09-4940-b937-2ccdb77cb027", + "f9491c25-70ac-4b45-935d-f742415b6366" + ], + "seconds": { + "0": { + "success": 315, + "error": 0 + }, + "1": { + "success": 130, + "error": 0 + }, + "2": { + "success": 114, + "error": 8 + }, + "3": { + "success": 15, + "error": 15 + }, + "4": { + "success": 158, + "error": 0 + }, + "5": { + "success": 54, + "error": 0 + }, + "6": { + "success": 100, + "error": 0 + }, + "7": { + "success": 232, + "error": 0 + }, + "8": { + "success": 200, + "error": 0 + }, + "9": { + "success": 64, + "error": 0 + }, + "10": { + "success": 48, + "error": 0 + }, + "11": { + "success": 208, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries", + "first_success_after_action_seconds": { + "read": 0.076, + "write": 0.078 + }, + "stopped_container": "6205d0db9981", + "stopped_instance": "f9491c25-70ac-4b45-935d-f742415b6366", + "exit_code": 137, + "all_acknowledged_messages_replayed": 1312, + "failure_started_at_load_second": 3, + "service_after_second_5": "reads/writes continue", + "note": "hard SIGKILL, no automatic retries in load, replay confirmations after failure" + } +] diff --git a/evidence/lab3-final-callbacks.txt b/evidence/lab3-final-callbacks.txt new file mode 100644 index 00000000..b737ca5a --- /dev/null +++ b/evidence/lab3-final-callbacks.txt @@ -0,0 +1,5 @@ +PASS: стартовый администратор создан; повторные callbacks не дублируют и не меняют пароль +PASS: production не принимает старый пароль администратора при изменённой конфигурации +PASS: администратор входит через Basic, читает профиль через Bearer и выходит +PASS: callback не повышает права обычного пользователя с совпавшим username +PASS: ограничения username/password сохранены; is_admin нельзя передать при регистрации diff --git a/evidence/lab3-final-checks.txt b/evidence/lab3-final-checks.txt new file mode 100644 index 00000000..cf9348a8 --- /dev/null +++ b/evidence/lab3-final-checks.txt @@ -0,0 +1,12 @@ +smoke PASS +lab3 PASS +regressions PASS +dependencies PASS +database PASS +passwords PASS +settings PASS +callbacks PASS +idempotency_db PASS +readiness PASS +error_headers PASS +PASS: 2 API, internal ports, CPU/RAM limits on all containers diff --git a/evidence/lab3-final-database.txt b/evidence/lab3-final-database.txt new file mode 100644 index 00000000..4da36580 --- /dev/null +++ b/evidence/lab3-final-database.txt @@ -0,0 +1,13 @@ +PASS: два повторных запуска миграций не изменили данные; версия 006 одна +PASS: ошибка участника откатила INSERT чата и участников +Сообщений в исследуемом чате: 10000 +Limit (cost=0.42..202.30 rows=51 width=97) (actual time=0.004..0.009 rows=51 loops=1) + Buffers: shared hit=6 + -> Index Scan using messages_history_idx on messages (cost=0.42..301.27 rows=76 width=97) (actual time=0.003..0.007 rows=51 loops=1) + Index Cond: ((chat_id = '1342df5c-78a8-458a-877c-242bf6acf770'::uuid) AND (ROW(created_at, id) > ROW('2026-11-06 15:36:12.189401+00'::timestamp with time zone, 'b657a982-2303-4a6c-ade9-61a9c5ec3926'::uuid))) + Buffers: shared hit=6 +Planning: + Buffers: shared hit=8 +Planning Time: 0.033 ms +Execution Time: 0.018 ms +PASS: индекс истории используется для глубокой страницы из 10000 сообщений diff --git a/evidence/lab3-final-dependencies.txt b/evidence/lab3-final-dependencies.txt new file mode 100644 index 00000000..ce7f460f --- /dev/null +++ b/evidence/lab3-final-dependencies.txt @@ -0,0 +1,25 @@ +PASS: сессия хранится в Redis с TTL; чтение не продлевает срок +PASS: профиль кэшируется с TTL<=60; повторный GET даёт cache hit; потеря ключа восполняется +PASS: отсутствующая сессия не восстанавливается из профиля/БД; Redis — источник истины +PASS: Redis выключен; live=200, ready=503; проверка сессий недоступна, доступ не выдаётся +PASS: сессия сохранилась после перезапуска Redis +PASS: logout при отказе Redis возвращает 503; успешный отзыв переживает AOF-рестарт и не даёт доступ +PASS: PostgreSQL выключен; live=200, ready=503, чаты=503; действующая сессия и прогретый профиль доступны в Redis +PASS: после восстановления БД прежняя сессия снова работает через HAProxy + Container mtusi2026lab1-redis-1 Stopping + Container mtusi2026lab1-redis-1 Stopped + Container mtusi2026lab1-redis-1 Starting + Container mtusi2026lab1-redis-1 Started + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-redis-1 Restarting + Container mtusi2026lab1-redis-1 Started + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-postgres-1 Stopping + Container mtusi2026lab1-postgres-1 Stopped + Container mtusi2026lab1-postgres-1 Starting + Container mtusi2026lab1-postgres-1 Started + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy diff --git a/evidence/lab3-final-error_headers.txt b/evidence/lab3-final-error_headers.txt new file mode 100644 index 00000000..507557ff --- /dev/null +++ b/evidence/lab3-final-error_headers.txt @@ -0,0 +1,49 @@ +PASS: 500 has X-Request-Id/X-Instance-Id and hides internal exception details +2026-11-06 15:36:22,186 ERROR src.main: Необработанная ошибка запроса +Traceback (most recent call last): + File "/app/src/main.py", line 37, in add_request_id + response = await call_next(request) + ^^^^^^^^^^^^^^^^^^^^^^^^ + File "/usr/local/lib/python3.13/site-packages/starlette/middleware/base.py", line 173, in call_next + raise app_exc from app_exc.__cause__ or app_exc.__context__ + File "/usr/local/lib/python3.13/site-packages/starlette/middleware/base.py", line 149, in coro + await self.app(scope, receive_or_disconnect, send_no_error) + File "/app/src/core/request_limits.py", line 34, in __call__ + await self.app(scope, receive, send) + File "/usr/local/lib/python3.13/site-packages/starlette/middleware/exceptions.py", line 63, in __call__ + await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) + File "/usr/local/lib/python3.13/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app + raise exc + File "/usr/local/lib/python3.13/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app + await app(scope, receive, sender) + File "/usr/local/lib/python3.13/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__ + await self.app(scope, receive, send) + File "/usr/local/lib/python3.13/site-packages/starlette/routing.py", line 676, in __call__ + await self.middleware_stack(scope, receive, send) + File "/usr/local/lib/python3.13/site-packages/fastapi/routing.py", line 2734, in app + await route.handle(scope, receive, send) + File "/usr/local/lib/python3.13/site-packages/fastapi/routing.py", line 1281, in handle + await super().handle(scope, receive, send) + File "/usr/local/lib/python3.13/site-packages/starlette/routing.py", line 282, in handle + await self.app(scope, receive, send) + File "/usr/local/lib/python3.13/site-packages/fastapi/routing.py", line 158, in app + await wrap_app_handling_exceptions(app, request)(scope, receive, send) + File "/usr/local/lib/python3.13/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app + raise exc + File "/usr/local/lib/python3.13/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app + await app(scope, receive, sender) + File "/usr/local/lib/python3.13/site-packages/fastapi/routing.py", line 144, in app + response = await f(request) + ^^^^^^^^^^^^^^^^ + File "/usr/local/lib/python3.13/site-packages/fastapi/routing.py", line 706, in app + raw_response = await run_endpoint_function( + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + ...<3 lines>... + ) + ^ + File "/usr/local/lib/python3.13/site-packages/fastapi/routing.py", line 352, in run_endpoint_function + return await dependant.call(**values) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "", line 13, in unexpected +RuntimeError: audit-internal-detail +2026-11-06 15:36:22,190 INFO src.main: ид_запроса=010678ea-dc01-435e-8d0f-e15d6c4b683b метод=GET путь='/_audit_unexpected_error' статус=500 длительность_мс=84.165 diff --git a/evidence/lab3-final-idempotency_db.txt b/evidence/lab3-final-idempotency_db.txt new file mode 100644 index 00000000..b913f834 --- /dev/null +++ b/evidence/lab3-final-idempotency_db.txt @@ -0,0 +1 @@ +PASS: rollback removes both message/key; committed result replays; expired key can be reused diff --git a/evidence/lab3-final-lab3.txt b/evidence/lab3-final-lab3.txt new file mode 100644 index 00000000..fa88adb3 --- /dev/null +++ b/evidence/lab3-final-lab3.txt @@ -0,0 +1,10 @@ +{ + "parallel_repeats": 16, + "instances": [ + "925b9202-4fd4-4a5b-b7fb-8c8e873b9b27", + "bdd60190-5e10-49b9-81b2-09ea9c656ad4" + ], + "messages_created": 1, + "checks": "normalization, conflict, key scope, invalid keys, access, logout, headers", + "result": "PASS" +} diff --git a/evidence/lab3-final-passwords.txt b/evidence/lab3-final-passwords.txt new file mode 100644 index 00000000..48275b5a --- /dev/null +++ b/evidence/lab3-final-passwords.txt @@ -0,0 +1,2 @@ +PASS: только Argon2id, случайная соль, Unicode 128 символов, неверный пароль +PASS: повторный вход с Argon2id; сессии находятся в Redis; хеш пароля не изменяется diff --git a/evidence/lab3-final-readiness.txt b/evidence/lab3-final-readiness.txt new file mode 100644 index 00000000..6de23eb0 --- /dev/null +++ b/evidence/lab3-final-readiness.txt @@ -0,0 +1 @@ +PASS: bounded readiness response, one pending probe, recovery after cancellation diff --git a/evidence/lab3-final-regressions.txt b/evidence/lab3-final-regressions.txt new file mode 100644 index 00000000..7d08edc9 --- /dev/null +++ b/evidence/lab3-final-regressions.txt @@ -0,0 +1,5 @@ +PASS: обязательные пробельные строки, malformed JSON, типы/лишние поля, MIME 415, Basic без тела +PASS: 101 существующий участник отклонён; курсор сохраняет микросекунды и UUID-порядок, привязан к чату +PASS: исходный текст и его длина сохраняются без strip +PASS: null/повреждённый/чужой профиль заменяется из PostgreSQL с TTL; сессия работает +PASS: Swagger объявляет Basic для входа и Bearer для защищённых методов diff --git a/evidence/lab3-final-settings.txt b/evidence/lab3-final-settings.txt new file mode 100644 index 00000000..77efda40 --- /dev/null +++ b/evidence/lab3-final-settings.txt @@ -0,0 +1,3 @@ +PASS: AppSettings читает .env, окружение имеет приоритет, URL скрыт в repr +PASS: Pydantic отклоняет неверные TTL, pool, timeout и недостаточную длину токена +PASS: production отвергает учебный пароль; ошибки конфигурации скрывают входные значения diff --git a/evidence/lab3-final-smoke.txt b/evidence/lab3-final-smoke.txt new file mode 100644 index 00000000..fc20f9dd --- /dev/null +++ b/evidence/lab3-final-smoke.txt @@ -0,0 +1 @@ +PASS: lab3 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/lab3-idempotency.json b/evidence/lab3-idempotency.json new file mode 100644 index 00000000..c3fac93f --- /dev/null +++ b/evidence/lab3-idempotency.json @@ -0,0 +1,10 @@ +{ + "parallel_repeats": 16, + "instances": [ + "86f94883-e31a-46ab-ac30-e6438c13876c", + "cea74827-4fdc-4fa8-be9f-3ab1f3fc4276" + ], + "messages_created": 1, + "checks": "normalization, conflict, key scope, invalid keys, access, logout, headers", + "result": "PASS" +} diff --git a/evidence/lab3-infrastructure.json b/evidence/lab3-infrastructure.json new file mode 100644 index 00000000..73a5439f --- /dev/null +++ b/evidence/lab3-infrastructure.json @@ -0,0 +1,151 @@ +{ + "api": [ + { + "id": "e377f47f1834", + "name": "/mtusi2026lab1-api-17", + "image_id": "sha256:147950503b4456e0cb3290b9af20fea4e730c6168a69ae3ddb6a77600d2e6676", + "status": "running", + "exit_code": 0, + "cpu_limit": 0.5, + "memory_limit_bytes": 268435456, + "ports": { + "8080/tcp": null + }, + "read_only_root": true + }, + { + "id": "ccd508b0d9c6", + "name": "/mtusi2026lab1-api-18", + "image_id": "sha256:147950503b4456e0cb3290b9af20fea4e730c6168a69ae3ddb6a77600d2e6676", + "status": "running", + "exit_code": 0, + "cpu_limit": 0.5, + "memory_limit_bytes": 268435456, + "ports": { + "8080/tcp": null + }, + "read_only_root": true + } + ], + "lb": [ + { + "id": "279562a6ff00", + "name": "/mtusi2026lab1-lb-1", + "image_id": "sha256:e3229153dd6429fc71f8a9a1f1ccbf8dab6962422d7043c81e84dd6a075e90ee", + "status": "running", + "exit_code": 0, + "cpu_limit": 0.5, + "memory_limit_bytes": 134217728, + "ports": { + "8080/tcp": [ + { + "HostIp": "127.0.0.1", + "HostPort": "8080" + } + ] + }, + "read_only_root": true + } + ], + "postgres": [ + { + "id": "ec54dbe8e8dd", + "name": "/mtusi2026lab1-postgres-1", + "image_id": "sha256:557fea37a744d5f4c8faab304b0a90858b53ab119735a88c131fd19dab802f36", + "status": "running", + "exit_code": 0, + "cpu_limit": 1.0, + "memory_limit_bytes": 536870912, + "ports": { + "5432/tcp": null + }, + "read_only_root": false + } + ], + "redis": [ + { + "id": "f71bc2ef45c8", + "name": "/mtusi2026lab1-redis-1", + "image_id": "sha256:fbdbaea47b9ae4ecc2082ecdb4e1cea81e32176ffb1dcf643d422ad07427e5d9", + "status": "running", + "exit_code": 0, + "cpu_limit": 0.25, + "memory_limit_bytes": 134217728, + "ports": { + "6379/tcp": null + }, + "read_only_root": false + } + ], + "migration": [ + { + "id": "5a2ccb851cbb", + "name": "/mtusi2026lab1-migration-1", + "image_id": "sha256:147950503b4456e0cb3290b9af20fea4e730c6168a69ae3ddb6a77600d2e6676", + "status": "exited", + "exit_code": 0, + "cpu_limit": 0.5, + "memory_limit_bytes": 268435456, + "ports": {}, + "read_only_root": false + } + ], + "post_test_usage": [ + { + "BlockIO": "0B / 0B", + "CPUPerc": "18.35%", + "Container": "e377f47f183418456b39f816013726755dd3e3be1259e2d84b168e3bfbc87745", + "ID": "e377f47f1834", + "MemPerc": "32.11%", + "MemUsage": "82.2MiB / 256MiB", + "Name": "mtusi2026lab1-api-17", + "NetIO": "597kB / 1.95MB", + "PIDs": "10" + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "1.05%", + "Container": "ccd508b0d9c695a3527803368fb69ed25eca9451b33b9cd0fb29da25c4ce088d", + "ID": "ccd508b0d9c6", + "MemPerc": "24.83%", + "MemUsage": "63.55MiB / 256MiB", + "Name": "mtusi2026lab1-api-18", + "NetIO": "313kB / 386kB", + "PIDs": "6" + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.33%", + "Container": "279562a6ff003739c3e1df53cb93a32ad6a6f93b9ace2559ad832d67b49bf301", + "ID": "279562a6ff00", + "MemPerc": "12.89%", + "MemUsage": "16.5MiB / 128MiB", + "Name": "mtusi2026lab1-lb-1", + "NetIO": "136MB / 135MB", + "PIDs": "11" + }, + { + "BlockIO": "0B / 254kB", + "CPUPerc": "0.33%", + "Container": "ec54dbe8e8dd8aa9d107c7cd7d667f5bdfbfa3ea78b1549df0e7792c5dac8ee5", + "ID": "ec54dbe8e8dd", + "MemPerc": "18.13%", + "MemUsage": "92.84MiB / 512MiB", + "Name": "mtusi2026lab1-postgres-1", + "NetIO": "1.62MB / 370kB", + "PIDs": "22" + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.56%", + "Container": "f71bc2ef45c8833c96dadd85d138493ec8e2bf88f79ac09337d696ea6f8a76f7", + "ID": "f71bc2ef45c8", + "MemPerc": "7.57%", + "MemUsage": "9.684MiB / 128MiB", + "Name": "mtusi2026lab1-redis-1", + "NetIO": "20.5kB / 11kB", + "PIDs": "6" + } + ], + "note": "CPU/memory usage snapshot after tests, not a peak load measurement; cgroup CPU deltas under load are in research results." +} diff --git a/evidence/lab3-lab3.txt b/evidence/lab3-lab3.txt new file mode 100644 index 00000000..fc1a1095 --- /dev/null +++ b/evidence/lab3-lab3.txt @@ -0,0 +1,10 @@ +{ + "parallel_repeats": 16, + "instances": [ + "a916210d-1bd0-4086-9e21-7c13158a9ee0", + "f9491c25-70ac-4b45-935d-f742415b6366" + ], + "messages_created": 1, + "checks": "normalization, conflict, key scope, invalid keys, access, logout, headers", + "result": "PASS" +} diff --git a/evidence/lab3-original-research-run.txt b/evidence/lab3-original-research-run.txt new file mode 100644 index 00000000..1159e632 --- /dev/null +++ b/evidence/lab3-original-research-run.txt @@ -0,0 +1,812 @@ +[1/18] {'series': 'growing', 'replicas': 4, 'cpus': 1, 'memory': '256m', 'db': 0.25, 'concurrency': 32, 'repeat': 0} + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Recreate + Container mtusi2026lab1-postgres-1 Recreated + Container mtusi2026lab1-migration-1 Recreate + Container mtusi2026lab1-migration-1 Recreated + Container mtusi2026lab1-api-6 Creating + Container mtusi2026lab1-api-4 Recreate + Container mtusi2026lab1-api-3 Recreate + Container mtusi2026lab1-api-5 Creating + Container mtusi2026lab1-api-5 Created + Container mtusi2026lab1-api-6 Created + Container mtusi2026lab1-api-3 Recreated + Container mtusi2026lab1-api-4 Recreated + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Starting + Container mtusi2026lab1-postgres-1 Started + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-5 Starting + Container mtusi2026lab1-api-5 Started + Container mtusi2026lab1-api-3 Starting + Container mtusi2026lab1-api-3 Started + Container mtusi2026lab1-api-6 Starting + Container mtusi2026lab1-api-6 Started + Container mtusi2026lab1-api-4 Starting + Container mtusi2026lab1-api-4 Started + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-3 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-4 Waiting + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-3 Healthy + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-api-4 Healthy + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-3 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-4 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-3 Healthy + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-api-4 Healthy +RPS 213.789 errors {} p95 286.24 +[2/18] {'series': 'fixed', 'replicas': 1, 'cpus': 2.0, 'memory': '1024m', 'db': 0.25, 'concurrency': 32, 'repeat': 1} + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Running + Container mtusi2026lab1-api-5 Recreate + Container mtusi2026lab1-api-3 Stopping + Container mtusi2026lab1-api-6 Stopping + Container mtusi2026lab1-api-4 Stopping + Container mtusi2026lab1-api-3 Stopped + Container mtusi2026lab1-api-3 Removing + Container mtusi2026lab1-api-3 Removed + Container mtusi2026lab1-api-6 Stopped + Container mtusi2026lab1-api-6 Removing + Container mtusi2026lab1-api-4 Stopped + Container mtusi2026lab1-api-4 Removing + Container mtusi2026lab1-api-6 Removed + Container mtusi2026lab1-api-4 Removed + Container mtusi2026lab1-api-5 Recreated + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-5 Starting + Container mtusi2026lab1-api-5 Started + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-postgres-1 Healthy +RPS 416.231 errors {'503': 16} p95 117.54 +[3/18] {'series': 'growing', 'replicas': 2, 'cpus': 1, 'memory': '256m', 'db': 0.25, 'concurrency': 32, 'repeat': 1} + Container mtusi2026lab1-postgres-1 Running + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-api-6 Creating + Container mtusi2026lab1-api-5 Recreate + Container mtusi2026lab1-api-6 Created + Container mtusi2026lab1-api-5 Recreated + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-6 Starting + Container mtusi2026lab1-api-6 Started + Container mtusi2026lab1-api-5 Starting + Container mtusi2026lab1-api-5 Started + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-api-5 Healthy +RPS 428.551 errors {'503': 10} p95 173.422 +[4/18] {'series': 'growing', 'replicas': 1, 'cpus': 1, 'memory': '256m', 'db': 0.25, 'concurrency': 32, 'repeat': 1} + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Running + Container mtusi2026lab1-api-5 Running + Container mtusi2026lab1-api-6 Stopping + Container mtusi2026lab1-api-6 Stopped + Container mtusi2026lab1-api-6 Removing + Container mtusi2026lab1-api-6 Removed + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-lb-1 Healthy +RPS 306.81 errors {'503': 26} p95 260.156 +[5/18] {'series': 'fixed', 'replicas': 2, 'cpus': 1.0, 'memory': '512m', 'db': 0.25, 'concurrency': 32, 'repeat': 1} + Container mtusi2026lab1-postgres-1 Running + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-api-6 Creating + Container mtusi2026lab1-api-5 Recreate + Container mtusi2026lab1-api-6 Created + Container mtusi2026lab1-api-5 Recreated + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-5 Starting + Container mtusi2026lab1-api-5 Started + Container mtusi2026lab1-api-6 Starting + Container mtusi2026lab1-api-6 Started + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-6 Healthy +RPS 531.374 errors {'503': 56} p95 136.405 +[6/18] {'series': 'fixed', 'replicas': 4, 'cpus': 0.5, 'memory': '256m', 'db': 0.25, 'concurrency': 32, 'repeat': 0} + Container mtusi2026lab1-postgres-1 Running + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-api-8 Creating + Container mtusi2026lab1-api-5 Recreate + Container mtusi2026lab1-api-7 Creating + Container mtusi2026lab1-api-6 Recreate + Container mtusi2026lab1-api-7 Created + Container mtusi2026lab1-api-8 Created + Container mtusi2026lab1-api-5 Recreated + Container mtusi2026lab1-api-6 Recreated + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-6 Starting + Container mtusi2026lab1-api-6 Started + Container mtusi2026lab1-api-8 Starting + Container mtusi2026lab1-api-8 Started + Container mtusi2026lab1-api-7 Starting + Container mtusi2026lab1-api-7 Started + Container mtusi2026lab1-api-5 Starting + Container mtusi2026lab1-api-5 Started + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-5 Healthy +RPS 312.319 errors {'503': 3} p95 345.099 +[7/18] {'series': 'growing', 'replicas': 4, 'cpus': 1, 'memory': '256m', 'db': 0.25, 'concurrency': 32, 'repeat': 1} + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Running + Container mtusi2026lab1-api-6 Recreate + Container mtusi2026lab1-api-7 Recreate + Container mtusi2026lab1-api-5 Recreate + Container mtusi2026lab1-api-8 Recreate + Container mtusi2026lab1-api-8 Recreated + Container mtusi2026lab1-api-7 Recreated + Container mtusi2026lab1-api-5 Recreated + Container mtusi2026lab1-api-6 Recreated + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-8 Starting + Container mtusi2026lab1-api-8 Started + Container mtusi2026lab1-api-6 Starting + Container mtusi2026lab1-api-6 Started + Container mtusi2026lab1-api-7 Starting + Container mtusi2026lab1-api-7 Started + Container mtusi2026lab1-api-5 Starting + Container mtusi2026lab1-api-5 Started + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-5 Healthy +RPS 199.765 errors {} p95 390.46 +[8/18] {'series': 'growing', 'replicas': 2, 'cpus': 1, 'memory': '256m', 'db': 0.25, 'concurrency': 32, 'repeat': 0} + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Running + Container mtusi2026lab1-api-5 Running + Container mtusi2026lab1-api-6 Running + Container mtusi2026lab1-api-8 Stopping + Container mtusi2026lab1-api-7 Stopping + Container mtusi2026lab1-api-8 Stopped + Container mtusi2026lab1-api-8 Removing + Container mtusi2026lab1-api-7 Stopped + Container mtusi2026lab1-api-7 Removing + Container mtusi2026lab1-api-8 Removed + Container mtusi2026lab1-api-7 Removed + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-api-5 Healthy +RPS 411.537 errors {'503': 16} p95 201.141 +[9/18] {'series': 'fixed', 'replicas': 2, 'cpus': 1.0, 'memory': '512m', 'db': 0.25, 'concurrency': 32, 'repeat': 0} + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Running + Container mtusi2026lab1-api-6 Recreate + Container mtusi2026lab1-api-5 Recreate + Container mtusi2026lab1-api-6 Recreated + Container mtusi2026lab1-api-5 Recreated + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-5 Starting + Container mtusi2026lab1-api-5 Started + Container mtusi2026lab1-api-6 Starting + Container mtusi2026lab1-api-6 Started + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-6 Healthy +RPS 294.201 errors {'503': 12} p95 235.511 +[10/18] {'series': 'growing', 'replicas': 1, 'cpus': 1, 'memory': '256m', 'db': 0.25, 'concurrency': 32, 'repeat': 0} + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Running + Container mtusi2026lab1-api-6 Stopping + Container mtusi2026lab1-api-5 Recreate + Container mtusi2026lab1-api-6 Stopped + Container mtusi2026lab1-api-6 Removing + Container mtusi2026lab1-api-6 Removed + Container mtusi2026lab1-api-5 Recreated + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-5 Starting + Container mtusi2026lab1-api-5 Started + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-postgres-1 Healthy +RPS 246.411 errors {'503': 39} p95 213.561 +[11/18] {'series': 'fixed', 'replicas': 1, 'cpus': 2.0, 'memory': '1024m', 'db': 0.25, 'concurrency': 32, 'repeat': 0} + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Running + Container mtusi2026lab1-api-5 Recreate + Container mtusi2026lab1-api-5 Recreated + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-5 Starting + Container mtusi2026lab1-api-5 Started + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-lb-1 Healthy +RPS 246.748 errors {'503': 44} p95 270.373 +[12/18] {'series': 'fixed', 'replicas': 4, 'cpus': 0.5, 'memory': '256m', 'db': 0.25, 'concurrency': 32, 'repeat': 1} + Container mtusi2026lab1-postgres-1 Running + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-api-6 Creating + Container mtusi2026lab1-api-8 Creating + Container mtusi2026lab1-api-7 Creating + Container mtusi2026lab1-api-5 Recreate + Container mtusi2026lab1-api-8 Created + Container mtusi2026lab1-api-6 Created + Container mtusi2026lab1-api-7 Created + Container mtusi2026lab1-api-5 Recreated + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-6 Starting + Container mtusi2026lab1-api-6 Started + Container mtusi2026lab1-api-7 Starting + Container mtusi2026lab1-api-7 Started + Container mtusi2026lab1-api-5 Starting + Container mtusi2026lab1-api-5 Started + Container mtusi2026lab1-api-8 Starting + Container mtusi2026lab1-api-8 Started + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-8 Healthy +RPS 251.44 errors {} p95 300.114 +[13/18] {'series': 'saturation', 'replicas': 4, 'cpus': 1, 'memory': '256m', 'db': 0.25, 'concurrency': 8, 'repeat': 0} + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Running + Container mtusi2026lab1-api-6 Recreate + Container mtusi2026lab1-api-7 Recreate + Container mtusi2026lab1-api-8 Recreate + Container mtusi2026lab1-api-5 Recreate + Container mtusi2026lab1-api-5 Recreated + Container mtusi2026lab1-api-6 Recreated + Container mtusi2026lab1-api-7 Recreated + Container mtusi2026lab1-api-8 Recreated + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-8 Starting + Container mtusi2026lab1-api-8 Started + Container mtusi2026lab1-api-5 Starting + Container mtusi2026lab1-api-5 Started + Container mtusi2026lab1-api-7 Starting + Container mtusi2026lab1-api-7 Started + Container mtusi2026lab1-api-6 Starting + Container mtusi2026lab1-api-6 Started + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-6 Healthy +RPS 137.006 errors {'503': 2} p95 108.973 +[14/18] {'series': 'saturation', 'replicas': 4, 'cpus': 1, 'memory': '256m', 'db': 0.25, 'concurrency': 64, 'repeat': 0} + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Running + Container mtusi2026lab1-api-5 Running + Container mtusi2026lab1-api-6 Running + Container mtusi2026lab1-api-7 Running + Container mtusi2026lab1-api-8 Running + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-6 Healthy +RPS 188.797 errors {'503': 8} p95 707.871 +[15/18] {'series': 'improved-db', 'replicas': 4, 'cpus': 1, 'memory': '256m', 'db': 1, 'concurrency': 32, 'repeat': 0} + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Recreate + Container mtusi2026lab1-postgres-1 Recreated + Container mtusi2026lab1-api-5 Running + Container mtusi2026lab1-api-6 Running + Container mtusi2026lab1-api-7 Running + Container mtusi2026lab1-api-8 Running + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Starting + Container mtusi2026lab1-postgres-1 Started + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-6 Healthy +RPS 341.228 errors {} p95 312.029 +[16/18] {'series': 'saturation', 'replicas': 4, 'cpus': 1, 'memory': '256m', 'db': 0.25, 'concurrency': 8, 'repeat': 1} + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Recreate + Container mtusi2026lab1-postgres-1 Recreated + Container mtusi2026lab1-api-5 Running + Container mtusi2026lab1-api-6 Running + Container mtusi2026lab1-api-7 Running + Container mtusi2026lab1-api-8 Running + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Starting + Container mtusi2026lab1-postgres-1 Started + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-6 Healthy +RPS 182.516 errors {} p95 117.513 +[17/18] {'series': 'saturation', 'replicas': 4, 'cpus': 1, 'memory': '256m', 'db': 0.25, 'concurrency': 64, 'repeat': 1} + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Running + Container mtusi2026lab1-api-5 Running + Container mtusi2026lab1-api-6 Running + Container mtusi2026lab1-api-7 Running + Container mtusi2026lab1-api-8 Running + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-6 Healthy +RPS 181.203 errors {'503': 5} p95 910.173 +[18/18] {'series': 'improved-db', 'replicas': 4, 'cpus': 1, 'memory': '256m', 'db': 1, 'concurrency': 32, 'repeat': 1} + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Recreate + Container mtusi2026lab1-postgres-1 Recreated + Container mtusi2026lab1-api-5 Running + Container mtusi2026lab1-api-6 Running + Container mtusi2026lab1-api-7 Running + Container mtusi2026lab1-api-8 Running + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Starting + Container mtusi2026lab1-postgres-1 Started + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-6 Healthy + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-7 Waiting + Container mtusi2026lab1-api-6 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-7 Healthy + Container mtusi2026lab1-api-6 Healthy +RPS 390.895 errors {} p95 300.253 +PASS: research series complete; inspect results before drawing conclusions + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Running + Container mtusi2026lab1-api-7 Stopping + Container mtusi2026lab1-api-6 Stopping + Container mtusi2026lab1-api-8 Recreate + Container mtusi2026lab1-api-5 Recreate + Container mtusi2026lab1-api-6 Stopped + Container mtusi2026lab1-api-6 Removing + Container mtusi2026lab1-api-7 Stopped + Container mtusi2026lab1-api-7 Removing + Container mtusi2026lab1-api-6 Removed + Container mtusi2026lab1-api-7 Removed + Container mtusi2026lab1-api-8 Recreated + Container mtusi2026lab1-api-5 Recreated + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-8 Starting + Container mtusi2026lab1-api-8 Started + Container mtusi2026lab1-api-5 Starting + Container mtusi2026lab1-api-5 Started + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-5 Healthy + Container mtusi2026lab1-api-8 Waiting + Container mtusi2026lab1-api-5 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-api-8 Healthy + Container mtusi2026lab1-api-5 Healthy +Default two-replica configuration restored diff --git a/evidence/lab3-persistence.txt b/evidence/lab3-persistence.txt new file mode 100644 index 00000000..749fd29b --- /dev/null +++ b/evidence/lab3-persistence.txt @@ -0,0 +1,2 @@ +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/lab3-production.txt b/evidence/lab3-production.txt new file mode 100644 index 00000000..5db4d294 --- /dev/null +++ b/evidence/lab3-production.txt @@ -0,0 +1,6 @@ +PASS: реальный chunked=413, два Authorization=401, no-store; 4000 emoji в escaped JSON допустимы +PASS: реальное незавершённое тело получает 408; health работает во время ожидания +PASS: SQL-пагинация 13 чатов по 3; курсор принадлежит пользователю, чужие чаты скрыты +PASS: пароль с Unicode, пробелами, двоеточием и NUL не усекается +PASS: независимые токены, конкурентные logout/GET, уникальные request_id +PASS: типы/диапазон курсора, Unicode, размер/формат JSON и повреждённый Redis diff --git a/evidence/lab3-public-artifacts.json b/evidence/lab3-public-artifacts.json new file mode 100644 index 00000000..7a64e9ce --- /dev/null +++ b/evidence/lab3-public-artifacts.json @@ -0,0 +1,7 @@ +{ + "contracts/openapi.json": "9c3787e5d08a60c59ae67eeb429e9d75fbf134992a4e1f0954e926db49e111be", + "tests/client.py": "8d50976fc7c7bbb81e88a152f613c61ab25eaf53dba33c6a9dfab19f868d546d", + "tests/smoke.py": "52ac38eb343ff7d43e7543588e1da58ca06c9fce74b2b0d0bf0e4982bc23c1db", + "tests/resilience.py": "1f96663a174b3978d24eff66f2741f5f8c9b788d3242261d37a972720c52788b", + "tests/load.py": "6d3511ea719c7a7e841083e73d435c86dcbfd9f4e5c2a07d62eb9614b01d9933" +} diff --git a/evidence/lab3-public-failover.txt b/evidence/lab3-public-failover.txt new file mode 100644 index 00000000..3ece33b8 --- /dev/null +++ b/evidence/lab3-public-failover.txt @@ -0,0 +1,3 @@ +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 119, "failed_attempts": 2, "first_recovery_seconds": 6.634} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/lab3-quality.txt b/evidence/lab3-quality.txt new file mode 100644 index 00000000..5df37e93 --- /dev/null +++ b/evidence/lab3-quality.txt @@ -0,0 +1,8 @@ +.venv/bin/python scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +.venv/bin/python scripts/validate_openapi.py +PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested. +.venv/bin/python -m ruff check src +All checks passed! +.venv/bin/python -m ruff format --check src +48 files already formatted diff --git a/evidence/lab3-readiness-budget.txt b/evidence/lab3-readiness-budget.txt new file mode 100644 index 00000000..6de23eb0 --- /dev/null +++ b/evidence/lab3-readiness-budget.txt @@ -0,0 +1 @@ +PASS: bounded readiness response, one pending probe, recovery after cancellation diff --git a/evidence/lab3-regressions.txt b/evidence/lab3-regressions.txt new file mode 100644 index 00000000..7d08edc9 --- /dev/null +++ b/evidence/lab3-regressions.txt @@ -0,0 +1,5 @@ +PASS: обязательные пробельные строки, malformed JSON, типы/лишние поля, MIME 415, Basic без тела +PASS: 101 существующий участник отклонён; курсор сохраняет микросекунды и UUID-порядок, привязан к чату +PASS: исходный текст и его длина сохраняются без strip +PASS: null/повреждённый/чужой профиль заменяется из PostgreSQL с TTL; сессия работает +PASS: Swagger объявляет Basic для входа и Bearer для защищённых методов diff --git a/evidence/lab3-research-plots.txt b/evidence/lab3-research-plots.txt new file mode 100644 index 00000000..50444c17 --- /dev/null +++ b/evidence/lab3-research-plots.txt @@ -0,0 +1 @@ +Saved scaling.png/svg and bottleneck.png/svg diff --git a/evidence/lab3-research-run.txt b/evidence/lab3-research-run.txt new file mode 100644 index 00000000..b6cc037c --- /dev/null +++ b/evidence/lab3-research-run.txt @@ -0,0 +1,145 @@ +[1/2] {'series': 'growing', 'replicas': 4, 'cpus': 1, 'memory': '256m', 'db': 0.25, 'concurrency': 32, 'repeat': 0} + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Recreate + Container mtusi2026lab1-postgres-1 Recreated + Container mtusi2026lab1-api-17 Recreate + Container mtusi2026lab1-api-18 Recreate + Container mtusi2026lab1-api-20 Creating + Container mtusi2026lab1-api-19 Creating + Container mtusi2026lab1-api-19 Created + Container mtusi2026lab1-api-20 Created + Container mtusi2026lab1-api-18 Recreated + Container mtusi2026lab1-api-17 Recreated + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Starting + Container mtusi2026lab1-postgres-1 Started + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-20 Starting + Container mtusi2026lab1-api-20 Started + Container mtusi2026lab1-api-18 Starting + Container mtusi2026lab1-api-18 Started + Container mtusi2026lab1-api-19 Starting + Container mtusi2026lab1-api-19 Started + Container mtusi2026lab1-api-17 Starting + Container mtusi2026lab1-api-17 Started + Container mtusi2026lab1-api-20 Waiting + Container mtusi2026lab1-api-18 Waiting + Container mtusi2026lab1-api-19 Waiting + Container mtusi2026lab1-api-17 Waiting + Container mtusi2026lab1-api-20 Healthy + Container mtusi2026lab1-api-18 Healthy + Container mtusi2026lab1-api-19 Healthy + Container mtusi2026lab1-api-17 Healthy + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-20 Waiting + Container mtusi2026lab1-api-18 Waiting + Container mtusi2026lab1-api-19 Waiting + Container mtusi2026lab1-api-17 Waiting + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-api-20 Healthy + Container mtusi2026lab1-api-18 Healthy + Container mtusi2026lab1-api-19 Healthy + Container mtusi2026lab1-api-17 Healthy +RPS 198.697 errors {} p95 302.563 +[2/2] {'series': 'improved-db', 'replicas': 4, 'cpus': 1, 'memory': '256m', 'db': 1, 'concurrency': 32, 'repeat': 0} + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Recreate + Container mtusi2026lab1-postgres-1 Recreated + Container mtusi2026lab1-api-17 Running + Container mtusi2026lab1-api-18 Running + Container mtusi2026lab1-api-19 Running + Container mtusi2026lab1-api-20 Running + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Starting + Container mtusi2026lab1-postgres-1 Started + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-20 Waiting + Container mtusi2026lab1-api-18 Waiting + Container mtusi2026lab1-api-19 Waiting + Container mtusi2026lab1-api-17 Waiting + Container mtusi2026lab1-api-20 Healthy + Container mtusi2026lab1-api-18 Healthy + Container mtusi2026lab1-api-19 Healthy + Container mtusi2026lab1-api-17 Healthy + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-20 Waiting + Container mtusi2026lab1-api-18 Waiting + Container mtusi2026lab1-api-19 Waiting + Container mtusi2026lab1-api-17 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-api-20 Healthy + Container mtusi2026lab1-api-18 Healthy + Container mtusi2026lab1-api-19 Healthy + Container mtusi2026lab1-api-17 Healthy +RPS 590.851 errors {'503': 10} p95 169.569 +PASS: research series complete; inspect results before drawing conclusions + Container mtusi2026lab1-redis-1 Running + Container mtusi2026lab1-postgres-1 Running + Container mtusi2026lab1-api-20 Recreate + Container mtusi2026lab1-api-17 Stopping + Container mtusi2026lab1-api-18 Recreate + Container mtusi2026lab1-api-19 Stopping + Container mtusi2026lab1-api-17 Stopped + Container mtusi2026lab1-api-17 Removing + Container mtusi2026lab1-api-19 Stopped + Container mtusi2026lab1-api-19 Removing + Container mtusi2026lab1-api-17 Removed + Container mtusi2026lab1-api-19 Removed + Container mtusi2026lab1-api-18 Recreated + Container mtusi2026lab1-api-20 Recreated + Container mtusi2026lab1-lb-1 Running + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-migration-1 Starting + Container mtusi2026lab1-migration-1 Started + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-api-20 Starting + Container mtusi2026lab1-api-20 Started + Container mtusi2026lab1-api-18 Starting + Container mtusi2026lab1-api-18 Started + Container mtusi2026lab1-api-20 Waiting + Container mtusi2026lab1-api-18 Waiting + Container mtusi2026lab1-api-20 Healthy + Container mtusi2026lab1-api-18 Healthy + Container mtusi2026lab1-migration-1 Waiting + Container mtusi2026lab1-api-20 Waiting + Container mtusi2026lab1-api-18 Waiting + Container mtusi2026lab1-lb-1 Waiting + Container mtusi2026lab1-postgres-1 Waiting + Container mtusi2026lab1-redis-1 Waiting + Container mtusi2026lab1-migration-1 Exited + Container mtusi2026lab1-redis-1 Healthy + Container mtusi2026lab1-postgres-1 Healthy + Container mtusi2026lab1-lb-1 Healthy + Container mtusi2026lab1-api-20 Healthy + Container mtusi2026lab1-api-18 Healthy +Default two-replica configuration restored diff --git a/evidence/lab3-research/README.md b/evidence/lab3-research/README.md new file mode 100644 index 00000000..baa8e35d --- /dev/null +++ b/evidence/lab3-research/README.md @@ -0,0 +1 @@ +Полная исходная серия до аудита (схема 004): 18 измерений, исходные результаты и графики. После исправлений выполнена отдельная короткая проверка в ../lab3-control/. Повтор полной серии для текущей версии: python3 scripts/research-lab3.py --full. diff --git a/evidence/lab3-research/bottleneck.png b/evidence/lab3-research/bottleneck.png new file mode 100644 index 00000000..c87d0976 Binary files /dev/null and b/evidence/lab3-research/bottleneck.png differ diff --git a/evidence/lab3-research/bottleneck.svg b/evidence/lab3-research/bottleneck.svg new file mode 100644 index 00000000..9714bc14 --- /dev/null +++ b/evidence/lab3-research/bottleneck.svg @@ -0,0 +1,1707 @@ + + + + + + + + 2026-11-06T18:37:44.749568 + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/evidence/lab3-research/method.json b/evidence/lab3-research/method.json new file mode 100644 index 00000000..abe26b9d --- /dev/null +++ b/evidence/lab3-research/method.json @@ -0,0 +1,22 @@ +{ + "host": "macOS-15.6.1-arm64-arm-64bit-Mach-O", + "architecture": "arm64", + "docker": { + "ServerVersion": "28.5.1", + "NCPU": 10, + "MemTotal": 8218034176, + "KernelVersion": "6.10.14-linuxkit", + "OperatingSystem": "Docker Desktop", + "Architecture": "aarch64" + }, + "method": { + "warmup_seconds": 3, + "repeats": 2, + "duration_seconds": 12, + "concurrency": 32, + "profile": "80% POST unique keys, 20% GET first page limit20, 20 seed messages; fresh chat per run", + "generator": "Python http.client, one persistent connection per thread, closed-loop, no retries", + "limits": "PostgreSQL 0.25 CPU/512MiB baseline, LB 0.5CPU/128MiB, Redis 0.25CPU/128MiB", + "randomization_seed": 20261106 + } +} diff --git a/evidence/lab3-research/results.json b/evidence/lab3-research/results.json new file mode 100644 index 00000000..edbb1d62 --- /dev/null +++ b/evidence/lab3-research/results.json @@ -0,0 +1,2402 @@ +[ + { + "job": { + "series": "growing", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.063, + "concurrency": 32, + "successes": { + "write": 2080, + "read": 499 + }, + "errors": {}, + "rps": 213.789, + "all_attempt_latency_ms": { + "p50": 122.037, + "p95": 286.24, + "p99": 395.01 + }, + "successful_latency_ms": { + "p50": 122.037, + "p95": 286.24, + "p99": 395.01 + }, + "instances": [ + "04368908-e658-4c7c-a470-d062cee00352", + "3a6620ad-9802-4a1a-b0aa-d1a90cdd99a4", + "636c6e03-31dc-4934-b669-44db87b904f2", + "bac9cdaa-b147-4016-9670-982faefc8dfb" + ], + "seconds": { + "0": { + "success": 260, + "error": 0 + }, + "1": { + "success": 214, + "error": 0 + }, + "2": { + "success": 182, + "error": 0 + }, + "3": { + "success": 251, + "error": 0 + }, + "4": { + "success": 189, + "error": 0 + }, + "5": { + "success": 230, + "error": 0 + }, + "6": { + "success": 220, + "error": 0 + }, + "7": { + "success": 194, + "error": 0 + }, + "8": { + "success": 232, + "error": 0 + }, + "9": { + "success": 214, + "error": 0 + }, + "10": { + "success": 205, + "error": 0 + }, + "11": { + "success": 188, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "eb381287fdb4": { + "service": "api", + "cpu_cores_used": 0.5, + "throttled_seconds": 1.971, + "throttled_period_ratio": 0.236 + }, + "f3c20a708dcc": { + "service": "api", + "cpu_cores_used": 0.487, + "throttled_seconds": 1.388, + "throttled_period_ratio": 0.224 + }, + "302a50397bc8": { + "service": "api", + "cpu_cores_used": 0.5, + "throttled_seconds": 2.061, + "throttled_period_ratio": 0.244 + }, + "98cb11ec5f92": { + "service": "api", + "cpu_cores_used": 0.497, + "throttled_seconds": 1.572, + "throttled_period_ratio": 0.232 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.257, + "throttled_seconds": 52.306, + "throttled_period_ratio": 0.945 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.025, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.058, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 2724, + "rollbacks": 2708, + "blocks_read": 49, + "blocks_hit": 162542, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "fixed", + "replicas": 1, + "cpus": 2.0, + "memory": "1024m", + "db": 0.25, + "concurrency": 32, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.051, + "concurrency": 32, + "successes": { + "write": 4022, + "read": 994 + }, + "errors": { + "503": 16 + }, + "rps": 416.231, + "all_attempt_latency_ms": { + "p50": 72.754, + "p95": 117.54, + "p99": 150.008 + }, + "successful_latency_ms": { + "p50": 72.792, + "p95": 117.618, + "p99": 150.008 + }, + "instances": [ + "2efaca98-1279-42fe-94e7-73d3fc78b131" + ], + "seconds": { + "0": { + "success": 447, + "error": 16 + }, + "1": { + "success": 435, + "error": 0 + }, + "2": { + "success": 417, + "error": 0 + }, + "3": { + "success": 455, + "error": 0 + }, + "4": { + "success": 391, + "error": 0 + }, + "5": { + "success": 439, + "error": 0 + }, + "6": { + "success": 412, + "error": 0 + }, + "7": { + "success": 380, + "error": 0 + }, + "8": { + "success": 403, + "error": 0 + }, + "9": { + "success": 411, + "error": 0 + }, + "10": { + "success": 410, + "error": 0 + }, + "11": { + "success": 416, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "9239e587f690": { + "service": "api", + "cpu_cores_used": 1.169, + "throttled_seconds": 0.006, + "throttled_period_ratio": 0.047 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.144, + "throttled_seconds": 0.874, + "throttled_period_ratio": 0.063 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.017, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.033, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 4864, + "rollbacks": 4859, + "blocks_read": 50, + "blocks_hit": 294514, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "growing", + "replicas": 2, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.073, + "concurrency": 32, + "successes": { + "write": 4151, + "read": 1023 + }, + "errors": { + "503": 10 + }, + "rps": 428.551, + "all_attempt_latency_ms": { + "p50": 63.357, + "p95": 173.422, + "p99": 277.157 + }, + "successful_latency_ms": { + "p50": 63.445, + "p95": 173.456, + "p99": 277.157 + }, + "instances": [ + "4ef328f6-161e-4e5d-a964-c9eab89fca81", + "cf856ee0-31bc-440b-9d20-a2a0832fa756" + ], + "seconds": { + "0": { + "success": 498, + "error": 9 + }, + "1": { + "success": 576, + "error": 0 + }, + "2": { + "success": 438, + "error": 1 + }, + "3": { + "success": 558, + "error": 0 + }, + "4": { + "success": 452, + "error": 0 + }, + "5": { + "success": 400, + "error": 0 + }, + "6": { + "success": 547, + "error": 0 + }, + "7": { + "success": 372, + "error": 0 + }, + "8": { + "success": 285, + "error": 0 + }, + "9": { + "success": 274, + "error": 0 + }, + "10": { + "success": 285, + "error": 0 + }, + "11": { + "success": 489, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "db2a824f2a00": { + "service": "api", + "cpu_cores_used": 0.819, + "throttled_seconds": 1.849, + "throttled_period_ratio": 0.221 + }, + "bd5b0235f27f": { + "service": "api", + "cpu_cores_used": 0.824, + "throttled_seconds": 1.632, + "throttled_period_ratio": 0.233 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.254, + "throttled_seconds": 26.448, + "throttled_period_ratio": 0.89 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.023, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.057, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 5161, + "rollbacks": 5156, + "blocks_read": 39, + "blocks_hit": 370206, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "growing", + "replicas": 1, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.32, + "concurrency": 32, + "successes": { + "write": 3032, + "read": 748 + }, + "errors": { + "503": 26 + }, + "rps": 306.81, + "all_attempt_latency_ms": { + "p50": 77.9, + "p95": 260.156, + "p99": 396.72 + }, + "successful_latency_ms": { + "p50": 78.1, + "p95": 261.031, + "p99": 398.059 + }, + "instances": [ + "4ef328f6-161e-4e5d-a964-c9eab89fca81" + ], + "seconds": { + "0": { + "success": 441, + "error": 26 + }, + "1": { + "success": 381, + "error": 0 + }, + "2": { + "success": 289, + "error": 0 + }, + "3": { + "success": 392, + "error": 0 + }, + "4": { + "success": 317, + "error": 0 + }, + "5": { + "success": 347, + "error": 0 + }, + "6": { + "success": 423, + "error": 0 + }, + "7": { + "success": 234, + "error": 0 + }, + "8": { + "success": 325, + "error": 0 + }, + "9": { + "success": 382, + "error": 0 + }, + "10": { + "success": 156, + "error": 0 + }, + "11": { + "success": 93, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "db2a824f2a00": { + "service": "api", + "cpu_cores_used": 0.95, + "throttled_seconds": 2.99, + "throttled_period_ratio": 0.752 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.174, + "throttled_seconds": 8.365, + "throttled_period_ratio": 0.219 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.017, + "throttled_seconds": 0.072, + "throttled_period_ratio": 0.008 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.029, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 3812, + "rollbacks": 3804, + "blocks_read": 0, + "blocks_hit": 268979, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "fixed", + "replicas": 2, + "cpus": 1.0, + "memory": "512m", + "db": 0.25, + "concurrency": 32, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.035, + "concurrency": 32, + "successes": { + "write": 5126, + "read": 1269 + }, + "errors": { + "503": 56 + }, + "rps": 531.374, + "all_attempt_latency_ms": { + "p50": 54.171, + "p95": 136.405, + "p99": 176.476 + }, + "successful_latency_ms": { + "p50": 54.417, + "p95": 136.703, + "p99": 176.505 + }, + "instances": [ + "0c2d85cc-ca99-4735-a9ec-a14aa6c272e3", + "692578e2-df33-464f-a411-21153bdeee25" + ], + "seconds": { + "0": { + "success": 620, + "error": 7 + }, + "1": { + "success": 654, + "error": 0 + }, + "2": { + "success": 452, + "error": 14 + }, + "3": { + "success": 624, + "error": 0 + }, + "4": { + "success": 442, + "error": 1 + }, + "5": { + "success": 577, + "error": 7 + }, + "6": { + "success": 554, + "error": 0 + }, + "7": { + "success": 480, + "error": 8 + }, + "8": { + "success": 611, + "error": 0 + }, + "9": { + "success": 659, + "error": 0 + }, + "10": { + "success": 399, + "error": 19 + }, + "11": { + "success": 323, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "1aecf9504651": { + "service": "api", + "cpu_cores_used": 0.916, + "throttled_seconds": 2.349, + "throttled_period_ratio": 0.312 + }, + "b1131b9dcd97": { + "service": "api", + "cpu_cores_used": 0.934, + "throttled_seconds": 2.21, + "throttled_period_ratio": 0.385 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.238, + "throttled_seconds": 13.394, + "throttled_period_ratio": 0.724 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.024, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.06, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 6618, + "rollbacks": 6613, + "blocks_read": 0, + "blocks_hit": 404706, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "fixed", + "replicas": 4, + "cpus": 0.5, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.125, + "concurrency": 32, + "successes": { + "write": 3040, + "read": 747 + }, + "errors": { + "503": 3 + }, + "rps": 312.319, + "all_attempt_latency_ms": { + "p50": 37.504, + "p95": 345.099, + "p99": 468.638 + }, + "successful_latency_ms": { + "p50": 37.371, + "p95": 345.099, + "p99": 468.638 + }, + "instances": [ + "1844a7a1-c409-43c4-a8de-2cd6b1600bef", + "594f8ae5-0189-440d-a505-8d4dd6f7cd39", + "620b03d8-6d6c-40de-9510-a54ba51a2ea0", + "d4c2ec26-eeed-4078-b64f-26fea2a528c8" + ], + "seconds": { + "0": { + "success": 254, + "error": 0 + }, + "1": { + "success": 373, + "error": 0 + }, + "2": { + "success": 320, + "error": 0 + }, + "3": { + "success": 352, + "error": 3 + }, + "4": { + "success": 224, + "error": 0 + }, + "5": { + "success": 355, + "error": 0 + }, + "6": { + "success": 391, + "error": 0 + }, + "7": { + "success": 315, + "error": 0 + }, + "8": { + "success": 291, + "error": 0 + }, + "9": { + "success": 346, + "error": 0 + }, + "10": { + "success": 313, + "error": 0 + }, + "11": { + "success": 253, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "d0e29b2201aa": { + "service": "api", + "cpu_cores_used": 0.436, + "throttled_seconds": 8.563, + "throttled_period_ratio": 0.567 + }, + "0185e1dff3bd": { + "service": "api", + "cpu_cores_used": 0.425, + "throttled_seconds": 6.669, + "throttled_period_ratio": 0.481 + }, + "d16c24c6c953": { + "service": "api", + "cpu_cores_used": 0.443, + "throttled_seconds": 8.619, + "throttled_period_ratio": 0.595 + }, + "c418691b39d2": { + "service": "api", + "cpu_cores_used": 0.437, + "throttled_seconds": 6.958, + "throttled_period_ratio": 0.527 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.211, + "throttled_seconds": 6.524, + "throttled_period_ratio": 0.302 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.026, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.05, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 3888, + "rollbacks": 3876, + "blocks_read": 0, + "blocks_hit": 231109, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "growing", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.139, + "concurrency": 32, + "successes": { + "write": 1952, + "read": 473 + }, + "errors": {}, + "rps": 199.765, + "all_attempt_latency_ms": { + "p50": 137.703, + "p95": 390.46, + "p99": 490.225 + }, + "successful_latency_ms": { + "p50": 137.703, + "p95": 390.46, + "p99": 490.225 + }, + "instances": [ + "133c457b-06af-4737-a009-bd31e2aa6851", + "2eb738ce-8097-4ad7-baed-b611c7159587", + "85481adc-d38a-41c7-852c-4bdc351f346d", + "d1215dd9-3edf-47ab-9d9b-c9d7f1362cab" + ], + "seconds": { + "0": { + "success": 189, + "error": 0 + }, + "1": { + "success": 176, + "error": 0 + }, + "2": { + "success": 276, + "error": 0 + }, + "3": { + "success": 144, + "error": 0 + }, + "4": { + "success": 236, + "error": 0 + }, + "5": { + "success": 217, + "error": 0 + }, + "6": { + "success": 156, + "error": 0 + }, + "7": { + "success": 277, + "error": 0 + }, + "8": { + "success": 151, + "error": 0 + }, + "9": { + "success": 180, + "error": 0 + }, + "10": { + "success": 226, + "error": 0 + }, + "11": { + "success": 197, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "244d74627927": { + "service": "api", + "cpu_cores_used": 0.491, + "throttled_seconds": 0.823, + "throttled_period_ratio": 0.164 + }, + "fc3b6ad2b080": { + "service": "api", + "cpu_cores_used": 0.454, + "throttled_seconds": 0.716, + "throttled_period_ratio": 0.129 + }, + "ebbccedd1574": { + "service": "api", + "cpu_cores_used": 0.484, + "throttled_seconds": 0.849, + "throttled_period_ratio": 0.144 + }, + "a51b63ac71b2": { + "service": "api", + "cpu_cores_used": 0.497, + "throttled_seconds": 1.068, + "throttled_period_ratio": 0.172 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.254, + "throttled_seconds": 57.791, + "throttled_period_ratio": 0.938 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.023, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.053, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 2470, + "rollbacks": 2454, + "blocks_read": 0, + "blocks_hit": 151979, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "growing", + "replicas": 2, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.111, + "concurrency": 32, + "successes": { + "write": 3999, + "read": 985 + }, + "errors": { + "503": 16 + }, + "rps": 411.537, + "all_attempt_latency_ms": { + "p50": 63.153, + "p95": 201.141, + "p99": 303.704 + }, + "successful_latency_ms": { + "p50": 63.322, + "p95": 201.143, + "p99": 303.82 + }, + "instances": [ + "133c457b-06af-4737-a009-bd31e2aa6851", + "2eb738ce-8097-4ad7-baed-b611c7159587" + ], + "seconds": { + "0": { + "success": 468, + "error": 11 + }, + "1": { + "success": 541, + "error": 1 + }, + "2": { + "success": 472, + "error": 0 + }, + "3": { + "success": 350, + "error": 1 + }, + "4": { + "success": 94, + "error": 0 + }, + "5": { + "success": 399, + "error": 0 + }, + "6": { + "success": 382, + "error": 0 + }, + "7": { + "success": 659, + "error": 0 + }, + "8": { + "success": 434, + "error": 3 + }, + "9": { + "success": 478, + "error": 0 + }, + "10": { + "success": 407, + "error": 0 + }, + "11": { + "success": 300, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "244d74627927": { + "service": "api", + "cpu_cores_used": 0.826, + "throttled_seconds": 3.034, + "throttled_period_ratio": 0.344 + }, + "fc3b6ad2b080": { + "service": "api", + "cpu_cores_used": 0.847, + "throttled_seconds": 3.042, + "throttled_period_ratio": 0.414 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.249, + "throttled_seconds": 18.937, + "throttled_period_ratio": 0.723 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.026, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.057, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 5036, + "rollbacks": 5028, + "blocks_read": 0, + "blocks_hit": 307810, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "fixed", + "replicas": 2, + "cpus": 1.0, + "memory": "512m", + "db": 0.25, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.179, + "concurrency": 32, + "successes": { + "write": 2876, + "read": 707 + }, + "errors": { + "503": 12 + }, + "rps": 294.201, + "all_attempt_latency_ms": { + "p50": 96.034, + "p95": 235.511, + "p99": 377.248 + }, + "successful_latency_ms": { + "p50": 96.122, + "p95": 235.511, + "p99": 377.248 + }, + "instances": [ + "34257614-d3ec-4795-8dff-8eabcc3fba1a", + "6fa2733f-7e78-4e68-bc84-7ded28d29f82" + ], + "seconds": { + "0": { + "success": 319, + "error": 12 + }, + "1": { + "success": 387, + "error": 0 + }, + "2": { + "success": 270, + "error": 0 + }, + "3": { + "success": 166, + "error": 0 + }, + "4": { + "success": 187, + "error": 0 + }, + "5": { + "success": 326, + "error": 0 + }, + "6": { + "success": 313, + "error": 0 + }, + "7": { + "success": 384, + "error": 0 + }, + "8": { + "success": 319, + "error": 0 + }, + "9": { + "success": 250, + "error": 0 + }, + "10": { + "success": 408, + "error": 0 + }, + "11": { + "success": 254, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "022fe04f36c0": { + "service": "api", + "cpu_cores_used": 0.817, + "throttled_seconds": 2.225, + "throttled_period_ratio": 0.285 + }, + "f4d51ec5624a": { + "service": "api", + "cpu_cores_used": 0.785, + "throttled_seconds": 2.859, + "throttled_period_ratio": 0.3 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.248, + "throttled_seconds": 28.545, + "throttled_period_ratio": 0.823 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.027, + "throttled_seconds": 0.094, + "throttled_period_ratio": 0.008 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.055, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 3791, + "rollbacks": 3777, + "blocks_read": 0, + "blocks_hit": 279422, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "growing", + "replicas": 1, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.053, + "concurrency": 32, + "successes": { + "write": 2384, + "read": 586 + }, + "errors": { + "503": 39 + }, + "rps": 246.411, + "all_attempt_latency_ms": { + "p50": 80.979, + "p95": 213.561, + "p99": 2588.529 + }, + "successful_latency_ms": { + "p50": 81.249, + "p95": 212.721, + "p99": 790.004 + }, + "instances": [ + "b8b62172-1e9e-455b-985d-cca51f6dad2c" + ], + "seconds": { + "0": { + "success": 314, + "error": 32 + }, + "1": { + "success": 374, + "error": 0 + }, + "2": { + "success": 319, + "error": 0 + }, + "3": { + "success": 318, + "error": 0 + }, + "4": { + "success": 402, + "error": 0 + }, + "5": { + "success": 215, + "error": 0 + }, + "6": { + "success": 351, + "error": 4 + }, + "7": { + "success": 10, + "error": 3 + }, + "9": { + "success": 12, + "error": 0 + }, + "10": { + "success": 254, + "error": 0 + }, + "11": { + "success": 401, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "95945cd8f7f8": { + "service": "api", + "cpu_cores_used": 0.921, + "throttled_seconds": 2.549, + "throttled_period_ratio": 0.696 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.168, + "throttled_seconds": 12.93, + "throttled_period_ratio": 0.281 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.042, + "throttled_seconds": 1.031, + "throttled_period_ratio": 0.09 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.113, + "throttled_seconds": 10.038, + "throttled_period_ratio": 0.185 + } + }, + "postgres_delta": { + "commits": 3001, + "rollbacks": 2995, + "blocks_read": 0, + "blocks_hit": 203302, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "fixed", + "replicas": 1, + "cpus": 2.0, + "memory": "1024m", + "db": 0.25, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.045, + "concurrency": 32, + "successes": { + "write": 2389, + "read": 583 + }, + "errors": { + "503": 44 + }, + "rps": 246.748, + "all_attempt_latency_ms": { + "p50": 82.077, + "p95": 270.373, + "p99": 2134.155 + }, + "successful_latency_ms": { + "p50": 82.186, + "p95": 241.754, + "p99": 762.948 + }, + "instances": [ + "ff9aa62e-7a72-4a81-a453-fc38c9ae9fac" + ], + "seconds": { + "0": { + "success": 442, + "error": 26 + }, + "1": { + "success": 397, + "error": 0 + }, + "2": { + "success": 72, + "error": 15 + }, + "3": { + "success": 6, + "error": 0 + }, + "5": { + "success": 67, + "error": 0 + }, + "6": { + "success": 154, + "error": 0 + }, + "7": { + "success": 412, + "error": 0 + }, + "8": { + "success": 327, + "error": 0 + }, + "9": { + "success": 376, + "error": 0 + }, + "10": { + "success": 357, + "error": 0 + }, + "11": { + "success": 350, + "error": 0 + }, + "4": { + "success": 12, + "error": 3 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "4c12f33367ec": { + "service": "api", + "cpu_cores_used": 1.019, + "throttled_seconds": 0.004, + "throttled_period_ratio": 0.023 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.196, + "throttled_seconds": 26.123, + "throttled_period_ratio": 0.442 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.044, + "throttled_seconds": 1.043, + "throttled_period_ratio": 0.091 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.05, + "throttled_seconds": 0.721, + "throttled_period_ratio": 0.034 + } + }, + "postgres_delta": { + "commits": 3084, + "rollbacks": 3067, + "blocks_read": 231, + "blocks_hit": 211003, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "fixed", + "replicas": 4, + "cpus": 0.5, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.138, + "concurrency": 32, + "successes": { + "write": 2454, + "read": 598 + }, + "errors": {}, + "rps": 251.44, + "all_attempt_latency_ms": { + "p50": 105.713, + "p95": 300.114, + "p99": 405.122 + }, + "successful_latency_ms": { + "p50": 105.713, + "p95": 300.114, + "p99": 405.122 + }, + "instances": [ + "45529784-2ef8-4034-bf5c-473e6ee82be6", + "8453a14a-1c41-4d57-a801-11cca5851b38", + "9bfcfcc5-6247-4ab3-acbb-5de71ca7f0a6", + "bb684af6-7e67-44e8-ba27-84abd8f442c3" + ], + "seconds": { + "0": { + "success": 293, + "error": 0 + }, + "1": { + "success": 291, + "error": 0 + }, + "2": { + "success": 203, + "error": 0 + }, + "3": { + "success": 210, + "error": 0 + }, + "4": { + "success": 291, + "error": 0 + }, + "5": { + "success": 304, + "error": 0 + }, + "6": { + "success": 144, + "error": 0 + }, + "7": { + "success": 299, + "error": 0 + }, + "8": { + "success": 282, + "error": 0 + }, + "9": { + "success": 144, + "error": 0 + }, + "10": { + "success": 276, + "error": 0 + }, + "11": { + "success": 315, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "eb7e10d4bd0b": { + "service": "api", + "cpu_cores_used": 0.401, + "throttled_seconds": 7.574, + "throttled_period_ratio": 0.477 + }, + "4606371b71ef": { + "service": "api", + "cpu_cores_used": 0.399, + "throttled_seconds": 7.289, + "throttled_period_ratio": 0.496 + }, + "97ed7c644a96": { + "service": "api", + "cpu_cores_used": 0.382, + "throttled_seconds": 5.903, + "throttled_period_ratio": 0.38 + }, + "45cf842a585a": { + "service": "api", + "cpu_cores_used": 0.386, + "throttled_seconds": 5.785, + "throttled_period_ratio": 0.413 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.243, + "throttled_seconds": 34.154, + "throttled_period_ratio": 0.715 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.025, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.052, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 3134, + "rollbacks": 3121, + "blocks_read": 0, + "blocks_hit": 227380, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "saturation", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 8, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 13.27, + "concurrency": 8, + "successes": { + "write": 1457, + "read": 361 + }, + "errors": { + "503": 2 + }, + "rps": 137.006, + "all_attempt_latency_ms": { + "p50": 21.391, + "p95": 108.973, + "p99": 308.348 + }, + "successful_latency_ms": { + "p50": 21.274, + "p95": 108.961, + "p99": 306.817 + }, + "instances": [ + "0cadf110-e663-4a11-9415-650ca2d79d91", + "30dbc2a7-d6b5-402b-b712-82f8e6aae3cd", + "a42e7db4-75b7-4037-abf8-a57a7eae0bb6", + "fcd9c04d-59b2-4092-8819-456d47f66d92" + ], + "seconds": { + "0": { + "success": 228, + "error": 0 + }, + "1": { + "success": 219, + "error": 0 + }, + "2": { + "success": 79, + "error": 0 + }, + "3": { + "success": 40, + "error": 0 + }, + "4": { + "success": 76, + "error": 0 + }, + "5": { + "success": 67, + "error": 0 + }, + "6": { + "success": 220, + "error": 0 + }, + "7": { + "success": 292, + "error": 0 + }, + "8": { + "success": 189, + "error": 0 + }, + "9": { + "success": 257, + "error": 0 + }, + "10": { + "success": 150, + "error": 2 + }, + "11": { + "success": 1, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "848a127ef749": { + "service": "api", + "cpu_cores_used": 0.629, + "throttled_seconds": 1.194, + "throttled_period_ratio": 0.252 + }, + "95b8eb5e744d": { + "service": "api", + "cpu_cores_used": 0.555, + "throttled_seconds": 1.269, + "throttled_period_ratio": 0.208 + }, + "0dde79ac04f4": { + "service": "api", + "cpu_cores_used": 0.581, + "throttled_seconds": 1.577, + "throttled_period_ratio": 0.253 + }, + "566dcb0004c3": { + "service": "api", + "cpu_cores_used": 0.562, + "throttled_seconds": 1.449, + "throttled_period_ratio": 0.211 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.262, + "throttled_seconds": 41.634, + "throttled_period_ratio": 0.914 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.049, + "throttled_seconds": 0.842, + "throttled_period_ratio": 0.068 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.068, + "throttled_seconds": 0.232, + "throttled_period_ratio": 0.028 + } + }, + "postgres_delta": { + "commits": 1903, + "rollbacks": 1859, + "blocks_read": 1, + "blocks_hit": 135753, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "saturation", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 64, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.267, + "concurrency": 64, + "successes": { + "write": 1874, + "read": 442 + }, + "errors": { + "503": 8 + }, + "rps": 188.797, + "all_attempt_latency_ms": { + "p50": 300.149, + "p95": 707.871, + "p99": 902.089 + }, + "successful_latency_ms": { + "p50": 300.251, + "p95": 707.928, + "p99": 902.089 + }, + "instances": [ + "0cadf110-e663-4a11-9415-650ca2d79d91", + "30dbc2a7-d6b5-402b-b712-82f8e6aae3cd", + "a42e7db4-75b7-4037-abf8-a57a7eae0bb6", + "fcd9c04d-59b2-4092-8819-456d47f66d92" + ], + "seconds": { + "0": { + "success": 266, + "error": 8 + }, + "1": { + "success": 160, + "error": 0 + }, + "2": { + "success": 219, + "error": 0 + }, + "3": { + "success": 180, + "error": 0 + }, + "4": { + "success": 193, + "error": 0 + }, + "5": { + "success": 205, + "error": 0 + }, + "6": { + "success": 135, + "error": 0 + }, + "7": { + "success": 253, + "error": 0 + }, + "8": { + "success": 180, + "error": 0 + }, + "9": { + "success": 183, + "error": 0 + }, + "10": { + "success": 210, + "error": 0 + }, + "11": { + "success": 132, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "848a127ef749": { + "service": "api", + "cpu_cores_used": 0.446, + "throttled_seconds": 0.375, + "throttled_period_ratio": 0.099 + }, + "95b8eb5e744d": { + "service": "api", + "cpu_cores_used": 0.461, + "throttled_seconds": 0.731, + "throttled_period_ratio": 0.138 + }, + "0dde79ac04f4": { + "service": "api", + "cpu_cores_used": 0.427, + "throttled_seconds": 0.431, + "throttled_period_ratio": 0.115 + }, + "566dcb0004c3": { + "service": "api", + "cpu_cores_used": 0.455, + "throttled_seconds": 0.532, + "throttled_period_ratio": 0.144 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.254, + "throttled_seconds": 61.348, + "throttled_period_ratio": 0.932 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.022, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.053, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 2381, + "rollbacks": 2376, + "blocks_read": 0, + "blocks_hit": 168153, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "improved-db", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 1, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.162, + "concurrency": 32, + "successes": { + "write": 3334, + "read": 816 + }, + "errors": {}, + "rps": 341.228, + "all_attempt_latency_ms": { + "p50": 33.386, + "p95": 312.029, + "p99": 445.522 + }, + "successful_latency_ms": { + "p50": 33.386, + "p95": 312.029, + "p99": 445.522 + }, + "instances": [ + "0cadf110-e663-4a11-9415-650ca2d79d91", + "30dbc2a7-d6b5-402b-b712-82f8e6aae3cd", + "a42e7db4-75b7-4037-abf8-a57a7eae0bb6", + "fcd9c04d-59b2-4092-8819-456d47f66d92" + ], + "seconds": { + "0": { + "success": 452, + "error": 0 + }, + "1": { + "success": 363, + "error": 0 + }, + "2": { + "success": 394, + "error": 0 + }, + "3": { + "success": 390, + "error": 0 + }, + "4": { + "success": 258, + "error": 0 + }, + "5": { + "success": 264, + "error": 0 + }, + "6": { + "success": 459, + "error": 0 + }, + "7": { + "success": 318, + "error": 0 + }, + "8": { + "success": 301, + "error": 0 + }, + "9": { + "success": 252, + "error": 0 + }, + "10": { + "success": 373, + "error": 0 + }, + "11": { + "success": 326, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "848a127ef749": { + "service": "api", + "cpu_cores_used": 0.901, + "throttled_seconds": 4.456, + "throttled_period_ratio": 0.435 + }, + "95b8eb5e744d": { + "service": "api", + "cpu_cores_used": 0.85, + "throttled_seconds": 5.255, + "throttled_period_ratio": 0.516 + }, + "0dde79ac04f4": { + "service": "api", + "cpu_cores_used": 0.905, + "throttled_seconds": 3.738, + "throttled_period_ratio": 0.415 + }, + "566dcb0004c3": { + "service": "api", + "cpu_cores_used": 0.852, + "throttled_seconds": 5.264, + "throttled_period_ratio": 0.484 + }, + "73da35c364cb": { + "service": "postgres", + "cpu_cores_used": 0.436, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.05, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.093, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 4283, + "rollbacks": 4270, + "blocks_read": 43, + "blocks_hit": 338210, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "saturation", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 8, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.07, + "concurrency": 8, + "successes": { + "write": 1767, + "read": 436 + }, + "errors": {}, + "rps": 182.516, + "all_attempt_latency_ms": { + "p50": 21.138, + "p95": 117.513, + "p99": 249.372 + }, + "successful_latency_ms": { + "p50": 21.138, + "p95": 117.513, + "p99": 249.372 + }, + "instances": [ + "0cadf110-e663-4a11-9415-650ca2d79d91", + "30dbc2a7-d6b5-402b-b712-82f8e6aae3cd", + "a42e7db4-75b7-4037-abf8-a57a7eae0bb6", + "fcd9c04d-59b2-4092-8819-456d47f66d92" + ], + "seconds": { + "0": { + "success": 273, + "error": 0 + }, + "1": { + "success": 96, + "error": 0 + }, + "2": { + "success": 96, + "error": 0 + }, + "3": { + "success": 67, + "error": 0 + }, + "4": { + "success": 105, + "error": 0 + }, + "5": { + "success": 222, + "error": 0 + }, + "6": { + "success": 187, + "error": 0 + }, + "7": { + "success": 308, + "error": 0 + }, + "8": { + "success": 184, + "error": 0 + }, + "9": { + "success": 178, + "error": 0 + }, + "10": { + "success": 224, + "error": 0 + }, + "11": { + "success": 263, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "848a127ef749": { + "service": "api", + "cpu_cores_used": 0.659, + "throttled_seconds": 2.4, + "throttled_period_ratio": 0.293 + }, + "95b8eb5e744d": { + "service": "api", + "cpu_cores_used": 0.626, + "throttled_seconds": 2.126, + "throttled_period_ratio": 0.271 + }, + "0dde79ac04f4": { + "service": "api", + "cpu_cores_used": 0.6, + "throttled_seconds": 2.305, + "throttled_period_ratio": 0.315 + }, + "566dcb0004c3": { + "service": "api", + "cpu_cores_used": 0.655, + "throttled_seconds": 2.201, + "throttled_period_ratio": 0.346 + }, + "0e05653fd4e4": { + "service": "postgres", + "cpu_cores_used": 0.257, + "throttled_seconds": 22.819, + "throttled_period_ratio": 0.931 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.034, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.053, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 2233, + "rollbacks": 2225, + "blocks_read": 90, + "blocks_hit": 159401, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "saturation", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 64, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.4, + "concurrency": 64, + "successes": { + "write": 1820, + "read": 427 + }, + "errors": { + "503": 5 + }, + "rps": 181.203, + "all_attempt_latency_ms": { + "p50": 289.129, + "p95": 910.173, + "p99": 1195.996 + }, + "successful_latency_ms": { + "p50": 289.364, + "p95": 910.173, + "p99": 1195.996 + }, + "instances": [ + "0cadf110-e663-4a11-9415-650ca2d79d91", + "30dbc2a7-d6b5-402b-b712-82f8e6aae3cd", + "a42e7db4-75b7-4037-abf8-a57a7eae0bb6", + "fcd9c04d-59b2-4092-8819-456d47f66d92" + ], + "seconds": { + "0": { + "success": 290, + "error": 5 + }, + "1": { + "success": 204, + "error": 0 + }, + "2": { + "success": 231, + "error": 0 + }, + "3": { + "success": 125, + "error": 0 + }, + "4": { + "success": 223, + "error": 0 + }, + "5": { + "success": 222, + "error": 0 + }, + "6": { + "success": 146, + "error": 0 + }, + "7": { + "success": 243, + "error": 0 + }, + "8": { + "success": 180, + "error": 0 + }, + "9": { + "success": 155, + "error": 0 + }, + "10": { + "success": 122, + "error": 0 + }, + "11": { + "success": 106, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "848a127ef749": { + "service": "api", + "cpu_cores_used": 0.412, + "throttled_seconds": 0.625, + "throttled_period_ratio": 0.121 + }, + "95b8eb5e744d": { + "service": "api", + "cpu_cores_used": 0.398, + "throttled_seconds": 0.549, + "throttled_period_ratio": 0.156 + }, + "0dde79ac04f4": { + "service": "api", + "cpu_cores_used": 0.39, + "throttled_seconds": 0.581, + "throttled_period_ratio": 0.134 + }, + "566dcb0004c3": { + "service": "api", + "cpu_cores_used": 0.356, + "throttled_seconds": 0.296, + "throttled_period_ratio": 0.094 + }, + "0e05653fd4e4": { + "service": "postgres", + "cpu_cores_used": 0.266, + "throttled_seconds": 63.173, + "throttled_period_ratio": 0.978 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.02, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.046, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 2333, + "rollbacks": 2323, + "blocks_read": 388, + "blocks_hit": 178454, + "temp_bytes": 0 + } + }, + { + "job": { + "series": "improved-db", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 1, + "concurrency": 32, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.077, + "concurrency": 32, + "successes": { + "write": 3787, + "read": 934 + }, + "errors": {}, + "rps": 390.895, + "all_attempt_latency_ms": { + "p50": 22.637, + "p95": 300.253, + "p99": 413.923 + }, + "successful_latency_ms": { + "p50": 22.637, + "p95": 300.253, + "p99": 413.923 + }, + "instances": [ + "0cadf110-e663-4a11-9415-650ca2d79d91", + "30dbc2a7-d6b5-402b-b712-82f8e6aae3cd", + "a42e7db4-75b7-4037-abf8-a57a7eae0bb6", + "fcd9c04d-59b2-4092-8819-456d47f66d92" + ], + "seconds": { + "0": { + "success": 445, + "error": 0 + }, + "1": { + "success": 293, + "error": 0 + }, + "2": { + "success": 377, + "error": 0 + }, + "3": { + "success": 543, + "error": 0 + }, + "4": { + "success": 352, + "error": 0 + }, + "5": { + "success": 386, + "error": 0 + }, + "6": { + "success": 527, + "error": 0 + }, + "7": { + "success": 322, + "error": 0 + }, + "8": { + "success": 260, + "error": 0 + }, + "9": { + "success": 341, + "error": 0 + }, + "10": { + "success": 516, + "error": 0 + }, + "11": { + "success": 359, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "848a127ef749": { + "service": "api", + "cpu_cores_used": 0.887, + "throttled_seconds": 4.223, + "throttled_period_ratio": 0.44 + }, + "95b8eb5e744d": { + "service": "api", + "cpu_cores_used": 0.869, + "throttled_seconds": 4.731, + "throttled_period_ratio": 0.484 + }, + "0dde79ac04f4": { + "service": "api", + "cpu_cores_used": 0.935, + "throttled_seconds": 3.508, + "throttled_period_ratio": 0.466 + }, + "566dcb0004c3": { + "service": "api", + "cpu_cores_used": 0.88, + "throttled_seconds": 3.859, + "throttled_period_ratio": 0.429 + }, + "3d939b621b85": { + "service": "postgres", + "cpu_cores_used": 0.482, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.008 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.053, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.008 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.095, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 4814, + "rollbacks": 4802, + "blocks_read": 320, + "blocks_hit": 483542, + "temp_bytes": 0 + } + } +] diff --git a/evidence/lab3-research/run-01.json b/evidence/lab3-research/run-01.json new file mode 100644 index 00000000..d00d3db5 --- /dev/null +++ b/evidence/lab3-research/run-01.json @@ -0,0 +1,140 @@ +{ + "job": { + "series": "growing", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.063, + "concurrency": 32, + "successes": { + "write": 2080, + "read": 499 + }, + "errors": {}, + "rps": 213.789, + "all_attempt_latency_ms": { + "p50": 122.037, + "p95": 286.24, + "p99": 395.01 + }, + "successful_latency_ms": { + "p50": 122.037, + "p95": 286.24, + "p99": 395.01 + }, + "instances": [ + "04368908-e658-4c7c-a470-d062cee00352", + "3a6620ad-9802-4a1a-b0aa-d1a90cdd99a4", + "636c6e03-31dc-4934-b669-44db87b904f2", + "bac9cdaa-b147-4016-9670-982faefc8dfb" + ], + "seconds": { + "0": { + "success": 260, + "error": 0 + }, + "1": { + "success": 214, + "error": 0 + }, + "2": { + "success": 182, + "error": 0 + }, + "3": { + "success": 251, + "error": 0 + }, + "4": { + "success": 189, + "error": 0 + }, + "5": { + "success": 230, + "error": 0 + }, + "6": { + "success": 220, + "error": 0 + }, + "7": { + "success": 194, + "error": 0 + }, + "8": { + "success": 232, + "error": 0 + }, + "9": { + "success": 214, + "error": 0 + }, + "10": { + "success": 205, + "error": 0 + }, + "11": { + "success": 188, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "eb381287fdb4": { + "service": "api", + "cpu_cores_used": 0.5, + "throttled_seconds": 1.971, + "throttled_period_ratio": 0.236 + }, + "f3c20a708dcc": { + "service": "api", + "cpu_cores_used": 0.487, + "throttled_seconds": 1.388, + "throttled_period_ratio": 0.224 + }, + "302a50397bc8": { + "service": "api", + "cpu_cores_used": 0.5, + "throttled_seconds": 2.061, + "throttled_period_ratio": 0.244 + }, + "98cb11ec5f92": { + "service": "api", + "cpu_cores_used": 0.497, + "throttled_seconds": 1.572, + "throttled_period_ratio": 0.232 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.257, + "throttled_seconds": 52.306, + "throttled_period_ratio": 0.945 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.025, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.058, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 2724, + "rollbacks": 2708, + "blocks_read": 49, + "blocks_hit": 162542, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-02.json b/evidence/lab3-research/run-02.json new file mode 100644 index 00000000..d7318276 --- /dev/null +++ b/evidence/lab3-research/run-02.json @@ -0,0 +1,121 @@ +{ + "job": { + "series": "fixed", + "replicas": 1, + "cpus": 2.0, + "memory": "1024m", + "db": 0.25, + "concurrency": 32, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.051, + "concurrency": 32, + "successes": { + "write": 4022, + "read": 994 + }, + "errors": { + "503": 16 + }, + "rps": 416.231, + "all_attempt_latency_ms": { + "p50": 72.754, + "p95": 117.54, + "p99": 150.008 + }, + "successful_latency_ms": { + "p50": 72.792, + "p95": 117.618, + "p99": 150.008 + }, + "instances": [ + "2efaca98-1279-42fe-94e7-73d3fc78b131" + ], + "seconds": { + "0": { + "success": 447, + "error": 16 + }, + "1": { + "success": 435, + "error": 0 + }, + "2": { + "success": 417, + "error": 0 + }, + "3": { + "success": 455, + "error": 0 + }, + "4": { + "success": 391, + "error": 0 + }, + "5": { + "success": 439, + "error": 0 + }, + "6": { + "success": 412, + "error": 0 + }, + "7": { + "success": 380, + "error": 0 + }, + "8": { + "success": 403, + "error": 0 + }, + "9": { + "success": 411, + "error": 0 + }, + "10": { + "success": 410, + "error": 0 + }, + "11": { + "success": 416, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "9239e587f690": { + "service": "api", + "cpu_cores_used": 1.169, + "throttled_seconds": 0.006, + "throttled_period_ratio": 0.047 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.144, + "throttled_seconds": 0.874, + "throttled_period_ratio": 0.063 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.017, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.033, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 4864, + "rollbacks": 4859, + "blocks_read": 50, + "blocks_hit": 294514, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-03.json b/evidence/lab3-research/run-03.json new file mode 100644 index 00000000..cec384ca --- /dev/null +++ b/evidence/lab3-research/run-03.json @@ -0,0 +1,128 @@ +{ + "job": { + "series": "growing", + "replicas": 2, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.073, + "concurrency": 32, + "successes": { + "write": 4151, + "read": 1023 + }, + "errors": { + "503": 10 + }, + "rps": 428.551, + "all_attempt_latency_ms": { + "p50": 63.357, + "p95": 173.422, + "p99": 277.157 + }, + "successful_latency_ms": { + "p50": 63.445, + "p95": 173.456, + "p99": 277.157 + }, + "instances": [ + "4ef328f6-161e-4e5d-a964-c9eab89fca81", + "cf856ee0-31bc-440b-9d20-a2a0832fa756" + ], + "seconds": { + "0": { + "success": 498, + "error": 9 + }, + "1": { + "success": 576, + "error": 0 + }, + "2": { + "success": 438, + "error": 1 + }, + "3": { + "success": 558, + "error": 0 + }, + "4": { + "success": 452, + "error": 0 + }, + "5": { + "success": 400, + "error": 0 + }, + "6": { + "success": 547, + "error": 0 + }, + "7": { + "success": 372, + "error": 0 + }, + "8": { + "success": 285, + "error": 0 + }, + "9": { + "success": 274, + "error": 0 + }, + "10": { + "success": 285, + "error": 0 + }, + "11": { + "success": 489, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "db2a824f2a00": { + "service": "api", + "cpu_cores_used": 0.819, + "throttled_seconds": 1.849, + "throttled_period_ratio": 0.221 + }, + "bd5b0235f27f": { + "service": "api", + "cpu_cores_used": 0.824, + "throttled_seconds": 1.632, + "throttled_period_ratio": 0.233 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.254, + "throttled_seconds": 26.448, + "throttled_period_ratio": 0.89 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.023, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.057, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 5161, + "rollbacks": 5156, + "blocks_read": 39, + "blocks_hit": 370206, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-04.json b/evidence/lab3-research/run-04.json new file mode 100644 index 00000000..f2c59f44 --- /dev/null +++ b/evidence/lab3-research/run-04.json @@ -0,0 +1,121 @@ +{ + "job": { + "series": "growing", + "replicas": 1, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.32, + "concurrency": 32, + "successes": { + "write": 3032, + "read": 748 + }, + "errors": { + "503": 26 + }, + "rps": 306.81, + "all_attempt_latency_ms": { + "p50": 77.9, + "p95": 260.156, + "p99": 396.72 + }, + "successful_latency_ms": { + "p50": 78.1, + "p95": 261.031, + "p99": 398.059 + }, + "instances": [ + "4ef328f6-161e-4e5d-a964-c9eab89fca81" + ], + "seconds": { + "0": { + "success": 441, + "error": 26 + }, + "1": { + "success": 381, + "error": 0 + }, + "2": { + "success": 289, + "error": 0 + }, + "3": { + "success": 392, + "error": 0 + }, + "4": { + "success": 317, + "error": 0 + }, + "5": { + "success": 347, + "error": 0 + }, + "6": { + "success": 423, + "error": 0 + }, + "7": { + "success": 234, + "error": 0 + }, + "8": { + "success": 325, + "error": 0 + }, + "9": { + "success": 382, + "error": 0 + }, + "10": { + "success": 156, + "error": 0 + }, + "11": { + "success": 93, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "db2a824f2a00": { + "service": "api", + "cpu_cores_used": 0.95, + "throttled_seconds": 2.99, + "throttled_period_ratio": 0.752 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.174, + "throttled_seconds": 8.365, + "throttled_period_ratio": 0.219 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.017, + "throttled_seconds": 0.072, + "throttled_period_ratio": 0.008 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.029, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 3812, + "rollbacks": 3804, + "blocks_read": 0, + "blocks_hit": 268979, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-05.json b/evidence/lab3-research/run-05.json new file mode 100644 index 00000000..3d5f34c1 --- /dev/null +++ b/evidence/lab3-research/run-05.json @@ -0,0 +1,128 @@ +{ + "job": { + "series": "fixed", + "replicas": 2, + "cpus": 1.0, + "memory": "512m", + "db": 0.25, + "concurrency": 32, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.035, + "concurrency": 32, + "successes": { + "write": 5126, + "read": 1269 + }, + "errors": { + "503": 56 + }, + "rps": 531.374, + "all_attempt_latency_ms": { + "p50": 54.171, + "p95": 136.405, + "p99": 176.476 + }, + "successful_latency_ms": { + "p50": 54.417, + "p95": 136.703, + "p99": 176.505 + }, + "instances": [ + "0c2d85cc-ca99-4735-a9ec-a14aa6c272e3", + "692578e2-df33-464f-a411-21153bdeee25" + ], + "seconds": { + "0": { + "success": 620, + "error": 7 + }, + "1": { + "success": 654, + "error": 0 + }, + "2": { + "success": 452, + "error": 14 + }, + "3": { + "success": 624, + "error": 0 + }, + "4": { + "success": 442, + "error": 1 + }, + "5": { + "success": 577, + "error": 7 + }, + "6": { + "success": 554, + "error": 0 + }, + "7": { + "success": 480, + "error": 8 + }, + "8": { + "success": 611, + "error": 0 + }, + "9": { + "success": 659, + "error": 0 + }, + "10": { + "success": 399, + "error": 19 + }, + "11": { + "success": 323, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "1aecf9504651": { + "service": "api", + "cpu_cores_used": 0.916, + "throttled_seconds": 2.349, + "throttled_period_ratio": 0.312 + }, + "b1131b9dcd97": { + "service": "api", + "cpu_cores_used": 0.934, + "throttled_seconds": 2.21, + "throttled_period_ratio": 0.385 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.238, + "throttled_seconds": 13.394, + "throttled_period_ratio": 0.724 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.024, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.06, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 6618, + "rollbacks": 6613, + "blocks_read": 0, + "blocks_hit": 404706, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-06.json b/evidence/lab3-research/run-06.json new file mode 100644 index 00000000..a44773e3 --- /dev/null +++ b/evidence/lab3-research/run-06.json @@ -0,0 +1,142 @@ +{ + "job": { + "series": "fixed", + "replicas": 4, + "cpus": 0.5, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.125, + "concurrency": 32, + "successes": { + "write": 3040, + "read": 747 + }, + "errors": { + "503": 3 + }, + "rps": 312.319, + "all_attempt_latency_ms": { + "p50": 37.504, + "p95": 345.099, + "p99": 468.638 + }, + "successful_latency_ms": { + "p50": 37.371, + "p95": 345.099, + "p99": 468.638 + }, + "instances": [ + "1844a7a1-c409-43c4-a8de-2cd6b1600bef", + "594f8ae5-0189-440d-a505-8d4dd6f7cd39", + "620b03d8-6d6c-40de-9510-a54ba51a2ea0", + "d4c2ec26-eeed-4078-b64f-26fea2a528c8" + ], + "seconds": { + "0": { + "success": 254, + "error": 0 + }, + "1": { + "success": 373, + "error": 0 + }, + "2": { + "success": 320, + "error": 0 + }, + "3": { + "success": 352, + "error": 3 + }, + "4": { + "success": 224, + "error": 0 + }, + "5": { + "success": 355, + "error": 0 + }, + "6": { + "success": 391, + "error": 0 + }, + "7": { + "success": 315, + "error": 0 + }, + "8": { + "success": 291, + "error": 0 + }, + "9": { + "success": 346, + "error": 0 + }, + "10": { + "success": 313, + "error": 0 + }, + "11": { + "success": 253, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "d0e29b2201aa": { + "service": "api", + "cpu_cores_used": 0.436, + "throttled_seconds": 8.563, + "throttled_period_ratio": 0.567 + }, + "0185e1dff3bd": { + "service": "api", + "cpu_cores_used": 0.425, + "throttled_seconds": 6.669, + "throttled_period_ratio": 0.481 + }, + "d16c24c6c953": { + "service": "api", + "cpu_cores_used": 0.443, + "throttled_seconds": 8.619, + "throttled_period_ratio": 0.595 + }, + "c418691b39d2": { + "service": "api", + "cpu_cores_used": 0.437, + "throttled_seconds": 6.958, + "throttled_period_ratio": 0.527 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.211, + "throttled_seconds": 6.524, + "throttled_period_ratio": 0.302 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.026, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.05, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 3888, + "rollbacks": 3876, + "blocks_read": 0, + "blocks_hit": 231109, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-07.json b/evidence/lab3-research/run-07.json new file mode 100644 index 00000000..b46930fb --- /dev/null +++ b/evidence/lab3-research/run-07.json @@ -0,0 +1,140 @@ +{ + "job": { + "series": "growing", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.139, + "concurrency": 32, + "successes": { + "write": 1952, + "read": 473 + }, + "errors": {}, + "rps": 199.765, + "all_attempt_latency_ms": { + "p50": 137.703, + "p95": 390.46, + "p99": 490.225 + }, + "successful_latency_ms": { + "p50": 137.703, + "p95": 390.46, + "p99": 490.225 + }, + "instances": [ + "133c457b-06af-4737-a009-bd31e2aa6851", + "2eb738ce-8097-4ad7-baed-b611c7159587", + "85481adc-d38a-41c7-852c-4bdc351f346d", + "d1215dd9-3edf-47ab-9d9b-c9d7f1362cab" + ], + "seconds": { + "0": { + "success": 189, + "error": 0 + }, + "1": { + "success": 176, + "error": 0 + }, + "2": { + "success": 276, + "error": 0 + }, + "3": { + "success": 144, + "error": 0 + }, + "4": { + "success": 236, + "error": 0 + }, + "5": { + "success": 217, + "error": 0 + }, + "6": { + "success": 156, + "error": 0 + }, + "7": { + "success": 277, + "error": 0 + }, + "8": { + "success": 151, + "error": 0 + }, + "9": { + "success": 180, + "error": 0 + }, + "10": { + "success": 226, + "error": 0 + }, + "11": { + "success": 197, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "244d74627927": { + "service": "api", + "cpu_cores_used": 0.491, + "throttled_seconds": 0.823, + "throttled_period_ratio": 0.164 + }, + "fc3b6ad2b080": { + "service": "api", + "cpu_cores_used": 0.454, + "throttled_seconds": 0.716, + "throttled_period_ratio": 0.129 + }, + "ebbccedd1574": { + "service": "api", + "cpu_cores_used": 0.484, + "throttled_seconds": 0.849, + "throttled_period_ratio": 0.144 + }, + "a51b63ac71b2": { + "service": "api", + "cpu_cores_used": 0.497, + "throttled_seconds": 1.068, + "throttled_period_ratio": 0.172 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.254, + "throttled_seconds": 57.791, + "throttled_period_ratio": 0.938 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.023, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.053, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 2470, + "rollbacks": 2454, + "blocks_read": 0, + "blocks_hit": 151979, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-08.json b/evidence/lab3-research/run-08.json new file mode 100644 index 00000000..3ed4e273 --- /dev/null +++ b/evidence/lab3-research/run-08.json @@ -0,0 +1,128 @@ +{ + "job": { + "series": "growing", + "replicas": 2, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.111, + "concurrency": 32, + "successes": { + "write": 3999, + "read": 985 + }, + "errors": { + "503": 16 + }, + "rps": 411.537, + "all_attempt_latency_ms": { + "p50": 63.153, + "p95": 201.141, + "p99": 303.704 + }, + "successful_latency_ms": { + "p50": 63.322, + "p95": 201.143, + "p99": 303.82 + }, + "instances": [ + "133c457b-06af-4737-a009-bd31e2aa6851", + "2eb738ce-8097-4ad7-baed-b611c7159587" + ], + "seconds": { + "0": { + "success": 468, + "error": 11 + }, + "1": { + "success": 541, + "error": 1 + }, + "2": { + "success": 472, + "error": 0 + }, + "3": { + "success": 350, + "error": 1 + }, + "4": { + "success": 94, + "error": 0 + }, + "5": { + "success": 399, + "error": 0 + }, + "6": { + "success": 382, + "error": 0 + }, + "7": { + "success": 659, + "error": 0 + }, + "8": { + "success": 434, + "error": 3 + }, + "9": { + "success": 478, + "error": 0 + }, + "10": { + "success": 407, + "error": 0 + }, + "11": { + "success": 300, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "244d74627927": { + "service": "api", + "cpu_cores_used": 0.826, + "throttled_seconds": 3.034, + "throttled_period_ratio": 0.344 + }, + "fc3b6ad2b080": { + "service": "api", + "cpu_cores_used": 0.847, + "throttled_seconds": 3.042, + "throttled_period_ratio": 0.414 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.249, + "throttled_seconds": 18.937, + "throttled_period_ratio": 0.723 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.026, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.057, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 5036, + "rollbacks": 5028, + "blocks_read": 0, + "blocks_hit": 307810, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-09.json b/evidence/lab3-research/run-09.json new file mode 100644 index 00000000..57c2b31d --- /dev/null +++ b/evidence/lab3-research/run-09.json @@ -0,0 +1,128 @@ +{ + "job": { + "series": "fixed", + "replicas": 2, + "cpus": 1.0, + "memory": "512m", + "db": 0.25, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.179, + "concurrency": 32, + "successes": { + "write": 2876, + "read": 707 + }, + "errors": { + "503": 12 + }, + "rps": 294.201, + "all_attempt_latency_ms": { + "p50": 96.034, + "p95": 235.511, + "p99": 377.248 + }, + "successful_latency_ms": { + "p50": 96.122, + "p95": 235.511, + "p99": 377.248 + }, + "instances": [ + "34257614-d3ec-4795-8dff-8eabcc3fba1a", + "6fa2733f-7e78-4e68-bc84-7ded28d29f82" + ], + "seconds": { + "0": { + "success": 319, + "error": 12 + }, + "1": { + "success": 387, + "error": 0 + }, + "2": { + "success": 270, + "error": 0 + }, + "3": { + "success": 166, + "error": 0 + }, + "4": { + "success": 187, + "error": 0 + }, + "5": { + "success": 326, + "error": 0 + }, + "6": { + "success": 313, + "error": 0 + }, + "7": { + "success": 384, + "error": 0 + }, + "8": { + "success": 319, + "error": 0 + }, + "9": { + "success": 250, + "error": 0 + }, + "10": { + "success": 408, + "error": 0 + }, + "11": { + "success": 254, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "022fe04f36c0": { + "service": "api", + "cpu_cores_used": 0.817, + "throttled_seconds": 2.225, + "throttled_period_ratio": 0.285 + }, + "f4d51ec5624a": { + "service": "api", + "cpu_cores_used": 0.785, + "throttled_seconds": 2.859, + "throttled_period_ratio": 0.3 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.248, + "throttled_seconds": 28.545, + "throttled_period_ratio": 0.823 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.027, + "throttled_seconds": 0.094, + "throttled_period_ratio": 0.008 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.055, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 3791, + "rollbacks": 3777, + "blocks_read": 0, + "blocks_hit": 279422, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-10.json b/evidence/lab3-research/run-10.json new file mode 100644 index 00000000..c7885124 --- /dev/null +++ b/evidence/lab3-research/run-10.json @@ -0,0 +1,117 @@ +{ + "job": { + "series": "growing", + "replicas": 1, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.053, + "concurrency": 32, + "successes": { + "write": 2384, + "read": 586 + }, + "errors": { + "503": 39 + }, + "rps": 246.411, + "all_attempt_latency_ms": { + "p50": 80.979, + "p95": 213.561, + "p99": 2588.529 + }, + "successful_latency_ms": { + "p50": 81.249, + "p95": 212.721, + "p99": 790.004 + }, + "instances": [ + "b8b62172-1e9e-455b-985d-cca51f6dad2c" + ], + "seconds": { + "0": { + "success": 314, + "error": 32 + }, + "1": { + "success": 374, + "error": 0 + }, + "2": { + "success": 319, + "error": 0 + }, + "3": { + "success": 318, + "error": 0 + }, + "4": { + "success": 402, + "error": 0 + }, + "5": { + "success": 215, + "error": 0 + }, + "6": { + "success": 351, + "error": 4 + }, + "7": { + "success": 10, + "error": 3 + }, + "9": { + "success": 12, + "error": 0 + }, + "10": { + "success": 254, + "error": 0 + }, + "11": { + "success": 401, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "95945cd8f7f8": { + "service": "api", + "cpu_cores_used": 0.921, + "throttled_seconds": 2.549, + "throttled_period_ratio": 0.696 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.168, + "throttled_seconds": 12.93, + "throttled_period_ratio": 0.281 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.042, + "throttled_seconds": 1.031, + "throttled_period_ratio": 0.09 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.113, + "throttled_seconds": 10.038, + "throttled_period_ratio": 0.185 + } + }, + "postgres_delta": { + "commits": 3001, + "rollbacks": 2995, + "blocks_read": 0, + "blocks_hit": 203302, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-11.json b/evidence/lab3-research/run-11.json new file mode 100644 index 00000000..2a641af8 --- /dev/null +++ b/evidence/lab3-research/run-11.json @@ -0,0 +1,121 @@ +{ + "job": { + "series": "fixed", + "replicas": 1, + "cpus": 2.0, + "memory": "1024m", + "db": 0.25, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.045, + "concurrency": 32, + "successes": { + "write": 2389, + "read": 583 + }, + "errors": { + "503": 44 + }, + "rps": 246.748, + "all_attempt_latency_ms": { + "p50": 82.077, + "p95": 270.373, + "p99": 2134.155 + }, + "successful_latency_ms": { + "p50": 82.186, + "p95": 241.754, + "p99": 762.948 + }, + "instances": [ + "ff9aa62e-7a72-4a81-a453-fc38c9ae9fac" + ], + "seconds": { + "0": { + "success": 442, + "error": 26 + }, + "1": { + "success": 397, + "error": 0 + }, + "2": { + "success": 72, + "error": 15 + }, + "3": { + "success": 6, + "error": 0 + }, + "5": { + "success": 67, + "error": 0 + }, + "6": { + "success": 154, + "error": 0 + }, + "7": { + "success": 412, + "error": 0 + }, + "8": { + "success": 327, + "error": 0 + }, + "9": { + "success": 376, + "error": 0 + }, + "10": { + "success": 357, + "error": 0 + }, + "11": { + "success": 350, + "error": 0 + }, + "4": { + "success": 12, + "error": 3 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "4c12f33367ec": { + "service": "api", + "cpu_cores_used": 1.019, + "throttled_seconds": 0.004, + "throttled_period_ratio": 0.023 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.196, + "throttled_seconds": 26.123, + "throttled_period_ratio": 0.442 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.044, + "throttled_seconds": 1.043, + "throttled_period_ratio": 0.091 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.05, + "throttled_seconds": 0.721, + "throttled_period_ratio": 0.034 + } + }, + "postgres_delta": { + "commits": 3084, + "rollbacks": 3067, + "blocks_read": 231, + "blocks_hit": 211003, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-12.json b/evidence/lab3-research/run-12.json new file mode 100644 index 00000000..a4bb2b17 --- /dev/null +++ b/evidence/lab3-research/run-12.json @@ -0,0 +1,140 @@ +{ + "job": { + "series": "fixed", + "replicas": 4, + "cpus": 0.5, + "memory": "256m", + "db": 0.25, + "concurrency": 32, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.138, + "concurrency": 32, + "successes": { + "write": 2454, + "read": 598 + }, + "errors": {}, + "rps": 251.44, + "all_attempt_latency_ms": { + "p50": 105.713, + "p95": 300.114, + "p99": 405.122 + }, + "successful_latency_ms": { + "p50": 105.713, + "p95": 300.114, + "p99": 405.122 + }, + "instances": [ + "45529784-2ef8-4034-bf5c-473e6ee82be6", + "8453a14a-1c41-4d57-a801-11cca5851b38", + "9bfcfcc5-6247-4ab3-acbb-5de71ca7f0a6", + "bb684af6-7e67-44e8-ba27-84abd8f442c3" + ], + "seconds": { + "0": { + "success": 293, + "error": 0 + }, + "1": { + "success": 291, + "error": 0 + }, + "2": { + "success": 203, + "error": 0 + }, + "3": { + "success": 210, + "error": 0 + }, + "4": { + "success": 291, + "error": 0 + }, + "5": { + "success": 304, + "error": 0 + }, + "6": { + "success": 144, + "error": 0 + }, + "7": { + "success": 299, + "error": 0 + }, + "8": { + "success": 282, + "error": 0 + }, + "9": { + "success": 144, + "error": 0 + }, + "10": { + "success": 276, + "error": 0 + }, + "11": { + "success": 315, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "eb7e10d4bd0b": { + "service": "api", + "cpu_cores_used": 0.401, + "throttled_seconds": 7.574, + "throttled_period_ratio": 0.477 + }, + "4606371b71ef": { + "service": "api", + "cpu_cores_used": 0.399, + "throttled_seconds": 7.289, + "throttled_period_ratio": 0.496 + }, + "97ed7c644a96": { + "service": "api", + "cpu_cores_used": 0.382, + "throttled_seconds": 5.903, + "throttled_period_ratio": 0.38 + }, + "45cf842a585a": { + "service": "api", + "cpu_cores_used": 0.386, + "throttled_seconds": 5.785, + "throttled_period_ratio": 0.413 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.243, + "throttled_seconds": 34.154, + "throttled_period_ratio": 0.715 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.025, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.052, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 3134, + "rollbacks": 3121, + "blocks_read": 0, + "blocks_hit": 227380, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-13.json b/evidence/lab3-research/run-13.json new file mode 100644 index 00000000..f8fefeab --- /dev/null +++ b/evidence/lab3-research/run-13.json @@ -0,0 +1,142 @@ +{ + "job": { + "series": "saturation", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 8, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 13.27, + "concurrency": 8, + "successes": { + "write": 1457, + "read": 361 + }, + "errors": { + "503": 2 + }, + "rps": 137.006, + "all_attempt_latency_ms": { + "p50": 21.391, + "p95": 108.973, + "p99": 308.348 + }, + "successful_latency_ms": { + "p50": 21.274, + "p95": 108.961, + "p99": 306.817 + }, + "instances": [ + "0cadf110-e663-4a11-9415-650ca2d79d91", + "30dbc2a7-d6b5-402b-b712-82f8e6aae3cd", + "a42e7db4-75b7-4037-abf8-a57a7eae0bb6", + "fcd9c04d-59b2-4092-8819-456d47f66d92" + ], + "seconds": { + "0": { + "success": 228, + "error": 0 + }, + "1": { + "success": 219, + "error": 0 + }, + "2": { + "success": 79, + "error": 0 + }, + "3": { + "success": 40, + "error": 0 + }, + "4": { + "success": 76, + "error": 0 + }, + "5": { + "success": 67, + "error": 0 + }, + "6": { + "success": 220, + "error": 0 + }, + "7": { + "success": 292, + "error": 0 + }, + "8": { + "success": 189, + "error": 0 + }, + "9": { + "success": 257, + "error": 0 + }, + "10": { + "success": 150, + "error": 2 + }, + "11": { + "success": 1, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "848a127ef749": { + "service": "api", + "cpu_cores_used": 0.629, + "throttled_seconds": 1.194, + "throttled_period_ratio": 0.252 + }, + "95b8eb5e744d": { + "service": "api", + "cpu_cores_used": 0.555, + "throttled_seconds": 1.269, + "throttled_period_ratio": 0.208 + }, + "0dde79ac04f4": { + "service": "api", + "cpu_cores_used": 0.581, + "throttled_seconds": 1.577, + "throttled_period_ratio": 0.253 + }, + "566dcb0004c3": { + "service": "api", + "cpu_cores_used": 0.562, + "throttled_seconds": 1.449, + "throttled_period_ratio": 0.211 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.262, + "throttled_seconds": 41.634, + "throttled_period_ratio": 0.914 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.049, + "throttled_seconds": 0.842, + "throttled_period_ratio": 0.068 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.068, + "throttled_seconds": 0.232, + "throttled_period_ratio": 0.028 + } + }, + "postgres_delta": { + "commits": 1903, + "rollbacks": 1859, + "blocks_read": 1, + "blocks_hit": 135753, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-14.json b/evidence/lab3-research/run-14.json new file mode 100644 index 00000000..d3fc45af --- /dev/null +++ b/evidence/lab3-research/run-14.json @@ -0,0 +1,142 @@ +{ + "job": { + "series": "saturation", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 64, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.267, + "concurrency": 64, + "successes": { + "write": 1874, + "read": 442 + }, + "errors": { + "503": 8 + }, + "rps": 188.797, + "all_attempt_latency_ms": { + "p50": 300.149, + "p95": 707.871, + "p99": 902.089 + }, + "successful_latency_ms": { + "p50": 300.251, + "p95": 707.928, + "p99": 902.089 + }, + "instances": [ + "0cadf110-e663-4a11-9415-650ca2d79d91", + "30dbc2a7-d6b5-402b-b712-82f8e6aae3cd", + "a42e7db4-75b7-4037-abf8-a57a7eae0bb6", + "fcd9c04d-59b2-4092-8819-456d47f66d92" + ], + "seconds": { + "0": { + "success": 266, + "error": 8 + }, + "1": { + "success": 160, + "error": 0 + }, + "2": { + "success": 219, + "error": 0 + }, + "3": { + "success": 180, + "error": 0 + }, + "4": { + "success": 193, + "error": 0 + }, + "5": { + "success": 205, + "error": 0 + }, + "6": { + "success": 135, + "error": 0 + }, + "7": { + "success": 253, + "error": 0 + }, + "8": { + "success": 180, + "error": 0 + }, + "9": { + "success": 183, + "error": 0 + }, + "10": { + "success": 210, + "error": 0 + }, + "11": { + "success": 132, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "848a127ef749": { + "service": "api", + "cpu_cores_used": 0.446, + "throttled_seconds": 0.375, + "throttled_period_ratio": 0.099 + }, + "95b8eb5e744d": { + "service": "api", + "cpu_cores_used": 0.461, + "throttled_seconds": 0.731, + "throttled_period_ratio": 0.138 + }, + "0dde79ac04f4": { + "service": "api", + "cpu_cores_used": 0.427, + "throttled_seconds": 0.431, + "throttled_period_ratio": 0.115 + }, + "566dcb0004c3": { + "service": "api", + "cpu_cores_used": 0.455, + "throttled_seconds": 0.532, + "throttled_period_ratio": 0.144 + }, + "91cd41379d5e": { + "service": "postgres", + "cpu_cores_used": 0.254, + "throttled_seconds": 61.348, + "throttled_period_ratio": 0.932 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.022, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.053, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 2381, + "rollbacks": 2376, + "blocks_read": 0, + "blocks_hit": 168153, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-15.json b/evidence/lab3-research/run-15.json new file mode 100644 index 00000000..17d562ac --- /dev/null +++ b/evidence/lab3-research/run-15.json @@ -0,0 +1,140 @@ +{ + "job": { + "series": "improved-db", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 1, + "concurrency": 32, + "repeat": 0 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.162, + "concurrency": 32, + "successes": { + "write": 3334, + "read": 816 + }, + "errors": {}, + "rps": 341.228, + "all_attempt_latency_ms": { + "p50": 33.386, + "p95": 312.029, + "p99": 445.522 + }, + "successful_latency_ms": { + "p50": 33.386, + "p95": 312.029, + "p99": 445.522 + }, + "instances": [ + "0cadf110-e663-4a11-9415-650ca2d79d91", + "30dbc2a7-d6b5-402b-b712-82f8e6aae3cd", + "a42e7db4-75b7-4037-abf8-a57a7eae0bb6", + "fcd9c04d-59b2-4092-8819-456d47f66d92" + ], + "seconds": { + "0": { + "success": 452, + "error": 0 + }, + "1": { + "success": 363, + "error": 0 + }, + "2": { + "success": 394, + "error": 0 + }, + "3": { + "success": 390, + "error": 0 + }, + "4": { + "success": 258, + "error": 0 + }, + "5": { + "success": 264, + "error": 0 + }, + "6": { + "success": 459, + "error": 0 + }, + "7": { + "success": 318, + "error": 0 + }, + "8": { + "success": 301, + "error": 0 + }, + "9": { + "success": 252, + "error": 0 + }, + "10": { + "success": 373, + "error": 0 + }, + "11": { + "success": 326, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "848a127ef749": { + "service": "api", + "cpu_cores_used": 0.901, + "throttled_seconds": 4.456, + "throttled_period_ratio": 0.435 + }, + "95b8eb5e744d": { + "service": "api", + "cpu_cores_used": 0.85, + "throttled_seconds": 5.255, + "throttled_period_ratio": 0.516 + }, + "0dde79ac04f4": { + "service": "api", + "cpu_cores_used": 0.905, + "throttled_seconds": 3.738, + "throttled_period_ratio": 0.415 + }, + "566dcb0004c3": { + "service": "api", + "cpu_cores_used": 0.852, + "throttled_seconds": 5.264, + "throttled_period_ratio": 0.484 + }, + "73da35c364cb": { + "service": "postgres", + "cpu_cores_used": 0.436, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.05, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.093, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 4283, + "rollbacks": 4270, + "blocks_read": 43, + "blocks_hit": 338210, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-16.json b/evidence/lab3-research/run-16.json new file mode 100644 index 00000000..cd2e1557 --- /dev/null +++ b/evidence/lab3-research/run-16.json @@ -0,0 +1,140 @@ +{ + "job": { + "series": "saturation", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 8, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.07, + "concurrency": 8, + "successes": { + "write": 1767, + "read": 436 + }, + "errors": {}, + "rps": 182.516, + "all_attempt_latency_ms": { + "p50": 21.138, + "p95": 117.513, + "p99": 249.372 + }, + "successful_latency_ms": { + "p50": 21.138, + "p95": 117.513, + "p99": 249.372 + }, + "instances": [ + "0cadf110-e663-4a11-9415-650ca2d79d91", + "30dbc2a7-d6b5-402b-b712-82f8e6aae3cd", + "a42e7db4-75b7-4037-abf8-a57a7eae0bb6", + "fcd9c04d-59b2-4092-8819-456d47f66d92" + ], + "seconds": { + "0": { + "success": 273, + "error": 0 + }, + "1": { + "success": 96, + "error": 0 + }, + "2": { + "success": 96, + "error": 0 + }, + "3": { + "success": 67, + "error": 0 + }, + "4": { + "success": 105, + "error": 0 + }, + "5": { + "success": 222, + "error": 0 + }, + "6": { + "success": 187, + "error": 0 + }, + "7": { + "success": 308, + "error": 0 + }, + "8": { + "success": 184, + "error": 0 + }, + "9": { + "success": 178, + "error": 0 + }, + "10": { + "success": 224, + "error": 0 + }, + "11": { + "success": 263, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "848a127ef749": { + "service": "api", + "cpu_cores_used": 0.659, + "throttled_seconds": 2.4, + "throttled_period_ratio": 0.293 + }, + "95b8eb5e744d": { + "service": "api", + "cpu_cores_used": 0.626, + "throttled_seconds": 2.126, + "throttled_period_ratio": 0.271 + }, + "0dde79ac04f4": { + "service": "api", + "cpu_cores_used": 0.6, + "throttled_seconds": 2.305, + "throttled_period_ratio": 0.315 + }, + "566dcb0004c3": { + "service": "api", + "cpu_cores_used": 0.655, + "throttled_seconds": 2.201, + "throttled_period_ratio": 0.346 + }, + "0e05653fd4e4": { + "service": "postgres", + "cpu_cores_used": 0.257, + "throttled_seconds": 22.819, + "throttled_period_ratio": 0.931 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.034, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.053, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 2233, + "rollbacks": 2225, + "blocks_read": 90, + "blocks_hit": 159401, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-17.json b/evidence/lab3-research/run-17.json new file mode 100644 index 00000000..4e52e96a --- /dev/null +++ b/evidence/lab3-research/run-17.json @@ -0,0 +1,142 @@ +{ + "job": { + "series": "saturation", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 0.25, + "concurrency": 64, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.4, + "concurrency": 64, + "successes": { + "write": 1820, + "read": 427 + }, + "errors": { + "503": 5 + }, + "rps": 181.203, + "all_attempt_latency_ms": { + "p50": 289.129, + "p95": 910.173, + "p99": 1195.996 + }, + "successful_latency_ms": { + "p50": 289.364, + "p95": 910.173, + "p99": 1195.996 + }, + "instances": [ + "0cadf110-e663-4a11-9415-650ca2d79d91", + "30dbc2a7-d6b5-402b-b712-82f8e6aae3cd", + "a42e7db4-75b7-4037-abf8-a57a7eae0bb6", + "fcd9c04d-59b2-4092-8819-456d47f66d92" + ], + "seconds": { + "0": { + "success": 290, + "error": 5 + }, + "1": { + "success": 204, + "error": 0 + }, + "2": { + "success": 231, + "error": 0 + }, + "3": { + "success": 125, + "error": 0 + }, + "4": { + "success": 223, + "error": 0 + }, + "5": { + "success": 222, + "error": 0 + }, + "6": { + "success": 146, + "error": 0 + }, + "7": { + "success": 243, + "error": 0 + }, + "8": { + "success": 180, + "error": 0 + }, + "9": { + "success": 155, + "error": 0 + }, + "10": { + "success": 122, + "error": 0 + }, + "11": { + "success": 106, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "848a127ef749": { + "service": "api", + "cpu_cores_used": 0.412, + "throttled_seconds": 0.625, + "throttled_period_ratio": 0.121 + }, + "95b8eb5e744d": { + "service": "api", + "cpu_cores_used": 0.398, + "throttled_seconds": 0.549, + "throttled_period_ratio": 0.156 + }, + "0dde79ac04f4": { + "service": "api", + "cpu_cores_used": 0.39, + "throttled_seconds": 0.581, + "throttled_period_ratio": 0.134 + }, + "566dcb0004c3": { + "service": "api", + "cpu_cores_used": 0.356, + "throttled_seconds": 0.296, + "throttled_period_ratio": 0.094 + }, + "0e05653fd4e4": { + "service": "postgres", + "cpu_cores_used": 0.266, + "throttled_seconds": 63.173, + "throttled_period_ratio": 0.978 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.02, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.046, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 2333, + "rollbacks": 2323, + "blocks_read": 388, + "blocks_hit": 178454, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/run-18.json b/evidence/lab3-research/run-18.json new file mode 100644 index 00000000..7345bde4 --- /dev/null +++ b/evidence/lab3-research/run-18.json @@ -0,0 +1,140 @@ +{ + "job": { + "series": "improved-db", + "replicas": 4, + "cpus": 1, + "memory": "256m", + "db": 1, + "concurrency": 32, + "repeat": 1 + }, + "report": { + "duration_seconds": 12, + "elapsed_seconds": 12.077, + "concurrency": 32, + "successes": { + "write": 3787, + "read": 934 + }, + "errors": {}, + "rps": 390.895, + "all_attempt_latency_ms": { + "p50": 22.637, + "p95": 300.253, + "p99": 413.923 + }, + "successful_latency_ms": { + "p50": 22.637, + "p95": 300.253, + "p99": 413.923 + }, + "instances": [ + "0cadf110-e663-4a11-9415-650ca2d79d91", + "30dbc2a7-d6b5-402b-b712-82f8e6aae3cd", + "a42e7db4-75b7-4037-abf8-a57a7eae0bb6", + "fcd9c04d-59b2-4092-8819-456d47f66d92" + ], + "seconds": { + "0": { + "success": 445, + "error": 0 + }, + "1": { + "success": 293, + "error": 0 + }, + "2": { + "success": 377, + "error": 0 + }, + "3": { + "success": 543, + "error": 0 + }, + "4": { + "success": 352, + "error": 0 + }, + "5": { + "success": 386, + "error": 0 + }, + "6": { + "success": 527, + "error": 0 + }, + "7": { + "success": 322, + "error": 0 + }, + "8": { + "success": 260, + "error": 0 + }, + "9": { + "success": 341, + "error": 0 + }, + "10": { + "success": 516, + "error": 0 + }, + "11": { + "success": 359, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" + }, + "cpu": { + "848a127ef749": { + "service": "api", + "cpu_cores_used": 0.887, + "throttled_seconds": 4.223, + "throttled_period_ratio": 0.44 + }, + "95b8eb5e744d": { + "service": "api", + "cpu_cores_used": 0.869, + "throttled_seconds": 4.731, + "throttled_period_ratio": 0.484 + }, + "0dde79ac04f4": { + "service": "api", + "cpu_cores_used": 0.935, + "throttled_seconds": 3.508, + "throttled_period_ratio": 0.466 + }, + "566dcb0004c3": { + "service": "api", + "cpu_cores_used": 0.88, + "throttled_seconds": 3.859, + "throttled_period_ratio": 0.429 + }, + "3d939b621b85": { + "service": "postgres", + "cpu_cores_used": 0.482, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.008 + }, + "f71bc2ef45c8": { + "service": "redis", + "cpu_cores_used": 0.053, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.008 + }, + "279562a6ff00": { + "service": "lb", + "cpu_cores_used": 0.095, + "throttled_seconds": 0.0, + "throttled_period_ratio": 0.0 + } + }, + "postgres_delta": { + "commits": 4814, + "rollbacks": 4802, + "blocks_read": 320, + "blocks_hit": 483542, + "temp_bytes": 0 + } +} diff --git a/evidence/lab3-research/scaling.png b/evidence/lab3-research/scaling.png new file mode 100644 index 00000000..df57707b Binary files /dev/null and b/evidence/lab3-research/scaling.png differ diff --git a/evidence/lab3-research/scaling.svg b/evidence/lab3-research/scaling.svg new file mode 100644 index 00000000..1f409c1e --- /dev/null +++ b/evidence/lab3-research/scaling.svg @@ -0,0 +1,2578 @@ + + + + + + + + 2026-11-06T18:37:44.633331 + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/evidence/lab3-research/summary.csv b/evidence/lab3-research/summary.csv new file mode 100644 index 00000000..185372b2 --- /dev/null +++ b/evidence/lab3-research/summary.csv @@ -0,0 +1,19 @@ +series,replicas,cpus,memory,db,concurrency,repeat,rps,p95_ms,errors,postgres_cores +growing,4,1,256m,0.25,32,0,213.789,286.24,0,0.257 +fixed,1,2.0,1024m,0.25,32,1,416.231,117.54,16,0.144 +growing,2,1,256m,0.25,32,1,428.551,173.422,10,0.254 +growing,1,1,256m,0.25,32,1,306.81,260.156,26,0.174 +fixed,2,1.0,512m,0.25,32,1,531.374,136.405,56,0.238 +fixed,4,0.5,256m,0.25,32,0,312.319,345.099,3,0.211 +growing,4,1,256m,0.25,32,1,199.765,390.46,0,0.254 +growing,2,1,256m,0.25,32,0,411.537,201.141,16,0.249 +fixed,2,1.0,512m,0.25,32,0,294.201,235.511,12,0.248 +growing,1,1,256m,0.25,32,0,246.411,213.561,39,0.168 +fixed,1,2.0,1024m,0.25,32,0,246.748,270.373,44,0.196 +fixed,4,0.5,256m,0.25,32,1,251.44,300.114,0,0.243 +saturation,4,1,256m,0.25,8,0,137.006,108.973,2,0.262 +saturation,4,1,256m,0.25,64,0,188.797,707.871,8,0.254 +improved-db,4,1,256m,1,32,0,341.228,312.029,0,0.436 +saturation,4,1,256m,0.25,8,1,182.516,117.513,0,0.257 +saturation,4,1,256m,0.25,64,1,181.203,910.173,5,0.266 +improved-db,4,1,256m,1,32,1,390.895,300.253,0,0.482 diff --git a/evidence/lab3-restart-proof.json b/evidence/lab3-restart-proof.json new file mode 100644 index 00000000..bd6fa270 --- /dev/null +++ b/evidence/lab3-restart-proof.json @@ -0,0 +1,18 @@ +{ + "api_before": { + "f730d1a3b9656761f8b2ac78e1d7d7e46f10e84d6e02f709e8538d325cd3adad": "2026-11-06T15:33:45.580305918Z", + "68b27434c3a1f944dd5ca1cc99905c5d041cc80217cd1184b9af932c69e71b51": "2026-11-06T15:33:54.765635756Z" + }, + "api_after": { + "e377f47f183418456b39f816013726755dd3e3be1259e2d84b168e3bfbc87745": "2026-11-06T15:35:15.093688376Z", + "ccd508b0d9c695a3527803368fb69ed25eca9451b33b9cd0fb29da25c4ce088d": "2026-11-06T15:35:15.170884043Z" + }, + "storage_before": { + "postgres": "2026-11-06T15:20:17.858429503Z", + "redis": "2026-11-06T14:45:02.138503885Z" + }, + "storage_after": { + "postgres": "2026-11-06T15:35:11.3787345Z", + "redis": "2026-11-06T15:35:11.424523166Z" + } +} diff --git a/evidence/lab3-review-summary.json b/evidence/lab3-review-summary.json new file mode 100644 index 00000000..940151d5 --- /dev/null +++ b/evidence/lab3-review-summary.json @@ -0,0 +1,9 @@ +{ + "audit-run": "PASS", + "edge-audit": "PASS", + "verify": "PASS", + "final-checks": "PASS", + "clean-start": "PASS", + "research-run": "PASS", + "research-plots": "PASS" +} diff --git a/evidence/lab3-review.txt b/evidence/lab3-review.txt new file mode 100644 index 00000000..e6956f26 --- /dev/null +++ b/evidence/lab3-review.txt @@ -0,0 +1,15 @@ +Starting audit-run +audit-run PASS +Starting edge-audit +edge-audit PASS +Starting verify +verify PASS +Starting final-checks +final-checks PASS +Starting clean-start +clean-start PASS +Starting research-run +research-run PASS +Starting research-plots +research-plots PASS +PASS: complete pre-submission review diff --git a/evidence/lab3-rolling-load.json b/evidence/lab3-rolling-load.json new file mode 100644 index 00000000..4b81de1b --- /dev/null +++ b/evidence/lab3-rolling-load.json @@ -0,0 +1,192 @@ +{ + "duration_seconds": 40, + "elapsed_seconds": 40.071, + "concurrency": 8, + "successes": { + "write": 6411, + "read": 1598 + }, + "errors": { + "503": 39 + }, + "rps": 199.869, + "all_attempt_latency_ms": { + "p50": 16.527, + "p95": 120.783, + "p99": 192.247 + }, + "successful_latency_ms": { + "p50": 16.62, + "p95": 122.345, + "p99": 192.247 + }, + "instances": [ + "26994c32-3a09-4940-b937-2ccdb77cb027", + "27af37f5-0a10-42e6-885c-481bd4951cb0", + "677c0215-4023-4648-ba02-4cbe3cffc627", + "a64d7a03-52c8-42e9-b205-18ea7884844b" + ], + "seconds": { + "0": { + "success": 123, + "error": 0 + }, + "1": { + "success": 122, + "error": 0 + }, + "2": { + "success": 155, + "error": 0 + }, + "3": { + "success": 231, + "error": 0 + }, + "4": { + "success": 164, + "error": 39 + }, + "5": { + "success": 137, + "error": 0 + }, + "6": { + "success": 241, + "error": 0 + }, + "7": { + "success": 363, + "error": 0 + }, + "8": { + "success": 178, + "error": 0 + }, + "9": { + "success": 191, + "error": 0 + }, + "10": { + "success": 217, + "error": 0 + }, + "11": { + "success": 163, + "error": 0 + }, + "12": { + "success": 111, + "error": 0 + }, + "13": { + "success": 173, + "error": 0 + }, + "14": { + "success": 278, + "error": 0 + }, + "15": { + "success": 161, + "error": 0 + }, + "16": { + "success": 168, + "error": 0 + }, + "17": { + "success": 264, + "error": 0 + }, + "18": { + "success": 90, + "error": 0 + }, + "19": { + "success": 112, + "error": 0 + }, + "20": { + "success": 195, + "error": 0 + }, + "21": { + "success": 285, + "error": 0 + }, + "22": { + "success": 104, + "error": 0 + }, + "23": { + "success": 149, + "error": 0 + }, + "24": { + "success": 286, + "error": 0 + }, + "25": { + "success": 278, + "error": 0 + }, + "26": { + "success": 135, + "error": 0 + }, + "27": { + "success": 219, + "error": 0 + }, + "28": { + "success": 347, + "error": 0 + }, + "29": { + "success": 157, + "error": 0 + }, + "30": { + "success": 162, + "error": 0 + }, + "31": { + "success": 294, + "error": 0 + }, + "32": { + "success": 158, + "error": 0 + }, + "33": { + "success": 170, + "error": 0 + }, + "34": { + "success": 213, + "error": 0 + }, + "35": { + "success": 331, + "error": 0 + }, + "36": { + "success": 152, + "error": 0 + }, + "37": { + "success": 149, + "error": 0 + }, + "38": { + "success": 300, + "error": 0 + }, + "39": { + "success": 283, + "error": 0 + } + }, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries" +} diff --git a/evidence/lab3-rolling.txt b/evidence/lab3-rolling.txt new file mode 100644 index 00000000..80b233ee --- /dev/null +++ b/evidence/lab3-rolling.txt @@ -0,0 +1,37 @@ +{ + "mode": "rolling replacement, one surge, same schema", + "events": [ + { + "old_container": "4856ec40e6db", + "new_container": "f730d1a3b965", + "new_instance": "677c0215-4023-4648-ba02-4cbe3cffc627", + "old_exit_code": 0, + "drained_slots": [ + "api1" + ], + "stop_seconds": 1.168 + }, + { + "old_container": "6205d0db9981", + "new_container": "68b27434c3a1", + "new_instance": "a64d7a03-52c8-42e9-b205-18ea7884844b", + "old_exit_code": 0, + "drained_slots": [ + "api2" + ], + "stop_seconds": 0.895 + } + ] +} + Container mtusi2026lab1-api-15 Running + Container mtusi2026lab1-api-16 Running + Container mtusi2026lab1-api-17 Creating + Container mtusi2026lab1-api-17 Created + Container mtusi2026lab1-api-17 Starting + Container mtusi2026lab1-api-17 Started + Container mtusi2026lab1-api-16 Running + Container mtusi2026lab1-api-17 Running + Container mtusi2026lab1-api-18 Creating + Container mtusi2026lab1-api-18 Created + Container mtusi2026lab1-api-18 Starting + Container mtusi2026lab1-api-18 Started diff --git a/evidence/lab3-smoke.txt b/evidence/lab3-smoke.txt new file mode 100644 index 00000000..fc20f9dd --- /dev/null +++ b/evidence/lab3-smoke.txt @@ -0,0 +1 @@ +PASS: lab3 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/lab3-transfer-edge-checks.txt b/evidence/lab3-transfer-edge-checks.txt new file mode 100644 index 00000000..31b7c89e --- /dev/null +++ b/evidence/lab3-transfer-edge-checks.txt @@ -0,0 +1,18 @@ +{ + "result": "PASS", + "checks": [ + "duplicate Idempotency-Key and Content-Type rejected", + "strict integer and repeated query parameters", + "NUL/emoji user and chat Unicode roundtrip", + "100 code point names and 4000 code point messages", + "no text trim or silent Unicode normalization", + "16 conflicting simultaneous requests: 8 replayed winners, 8 conflicts, one DB row", + "DB failure after message INSERT rolls back message and key; same key succeeds after recovery", + "invalid cached Unicode/timezone falls back to DB with TTL", + "one TCP connection reaches both API" + ], + "keepalive_instances": [ + "1117b014-edbb-473e-ba6f-107991a36b0c", + "cf4df4a1-2ff5-4d04-a979-42a2425d3b80" + ] +} diff --git a/evidence/lab3-transfer-failover.txt b/evidence/lab3-transfer-failover.txt new file mode 100644 index 00000000..9bd4d7d8 --- /dev/null +++ b/evidence/lab3-transfer-failover.txt @@ -0,0 +1,3 @@ +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 110, "failed_attempts": 3, "first_recovery_seconds": 2.597} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/lab3-transfer-final-smoke.txt b/evidence/lab3-transfer-final-smoke.txt new file mode 100644 index 00000000..fc20f9dd --- /dev/null +++ b/evidence/lab3-transfer-final-smoke.txt @@ -0,0 +1 @@ +PASS: lab3 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/lab3-transfer-migrations.txt b/evidence/lab3-transfer-migrations.txt new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/evidence/lab3-transfer-migrations.txt @@ -0,0 +1 @@ + diff --git a/evidence/lab3-transfer-persistence.txt b/evidence/lab3-transfer-persistence.txt new file mode 100644 index 00000000..749fd29b --- /dev/null +++ b/evidence/lab3-transfer-persistence.txt @@ -0,0 +1,2 @@ +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/lab3-transfer-quality.txt b/evidence/lab3-transfer-quality.txt new file mode 100644 index 00000000..5df37e93 --- /dev/null +++ b/evidence/lab3-transfer-quality.txt @@ -0,0 +1,8 @@ +.venv/bin/python scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +.venv/bin/python scripts/validate_openapi.py +PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested. +.venv/bin/python -m ruff check src +All checks passed! +.venv/bin/python -m ruff format --check src +48 files already formatted diff --git a/evidence/lab3-transfer-runtime.json b/evidence/lab3-transfer-runtime.json new file mode 100644 index 00000000..97d65595 --- /dev/null +++ b/evidence/lab3-transfer-runtime.json @@ -0,0 +1,22 @@ +{ + "project": "mtusi2026lab3", + "schema_revision": "006", + "api_instances": 2, + "api_states": { + "71547b2044ee": { + "status": "running", + "health": "healthy" + }, + "faa17650a7a8": { + "status": "running", + "health": "healthy" + } + }, + "public_sha256": { + "contracts/openapi.json": "9c3787e5d08a60c59ae67eeb429e9d75fbf134992a4e1f0954e926db49e111be", + "tests/client.py": "8d50976fc7c7bbb81e88a152f613c61ab25eaf53dba33c6a9dfab19f868d546d", + "tests/smoke.py": "52ac38eb343ff7d43e7543588e1da58ca06c9fce74b2b0d0bf0e4982bc23c1db", + "tests/resilience.py": "1f96663a174b3978d24eff66f2741f5f8c9b788d3242261d37a972720c52788b", + "tests/load.py": "6d3511ea719c7a7e841083e73d435c86dcbfd9f4e5c2a07d62eb9614b01d9933" + } +} diff --git a/evidence/lab3-transfer-runtime.txt b/evidence/lab3-transfer-runtime.txt new file mode 100644 index 00000000..83af50bf --- /dev/null +++ b/evidence/lab3-transfer-runtime.txt @@ -0,0 +1,11 @@ +persistence PASS +failover PASS + Container mtusi2026lab3-api-1 Running + Container mtusi2026lab3-api-2 Starting + Container mtusi2026lab3-api-2 Started + Container mtusi2026lab3-api-2 Waiting + Container mtusi2026lab3-api-1 Waiting + Container mtusi2026lab3-api-2 Healthy + Container mtusi2026lab3-api-1 Healthy +final-smoke PASS +PASS: transferred application retains data/session and survives API failover diff --git a/evidence/lab3-transfer-tests.txt b/evidence/lab3-transfer-tests.txt new file mode 100644 index 00000000..9e3e992c --- /dev/null +++ b/evidence/lab3-transfer-tests.txt @@ -0,0 +1,13 @@ +.venv/bin/python tests/smoke.py --base-url "http://localhost:8081" +PASS: lab3 public API scenario; infrastructure and grading evidence remain separate. +.venv/bin/python tests/lab3.py --base-url "http://localhost:8081" +{ + "parallel_repeats": 16, + "instances": [ + "1117b014-edbb-473e-ba6f-107991a36b0c", + "cf4df4a1-2ff5-4d04-a979-42a2425d3b80" + ], + "messages_created": 1, + "checks": "normalization, conflict, key scope, invalid keys, access, logout, headers", + "result": "PASS" +} diff --git a/evidence/lab3-unicode-migration.json b/evidence/lab3-unicode-migration.json new file mode 100644 index 00000000..8b663546 --- /dev/null +++ b/evidence/lab3-unicode-migration.json @@ -0,0 +1,23 @@ +{ + "before": { + "messages": { + "count": 83505, + "digest": "96926747176835df6a17d627a5a8ff32" + }, + "message_requests": { + "count": 82522, + "digest": "703190403d91cc26b340d25edf5ee6be" + } + }, + "after": { + "messages": { + "count": 83505, + "digest": "96926747176835df6a17d627a5a8ff32" + }, + "message_requests": { + "count": 82522, + "digest": "703190403d91cc26b340d25edf5ee6be" + } + }, + "same_existing_bytes": true +} diff --git a/evidence/lab3-unicode-names-migration.json b/evidence/lab3-unicode-names-migration.json new file mode 100644 index 00000000..07b0d432 --- /dev/null +++ b/evidence/lab3-unicode-names-migration.json @@ -0,0 +1,23 @@ +{ + "before": { + "users": { + "count": 297, + "digest": "aa93cf19bae06e20372f71b3c8ddd908" + }, + "chats": { + "count": 201, + "digest": "b626c1d4c74a5baa792cd6da396f06c2" + } + }, + "after": { + "users": { + "count": 297, + "digest": "aa93cf19bae06e20372f71b3c8ddd908" + }, + "chats": { + "count": 201, + "digest": "b626c1d4c74a5baa792cd6da396f06c2" + } + }, + "same_existing_bytes": true +} diff --git a/evidence/lab3-verify.txt b/evidence/lab3-verify.txt new file mode 100644 index 00000000..ef57a4f6 --- /dev/null +++ b/evidence/lab3-verify.txt @@ -0,0 +1,21 @@ +lab3-smoke.txt PASS +lab3-lab3.txt PASS +lab3-extra.txt PASS +lab3-regressions.txt PASS +lab3-production.txt PASS +lab3-db-idempotency.txt PASS +lab3-public-failover.txt PASS + Container mtusi2026lab1-api-16 Running + Container mtusi2026lab1-api-15 Starting + Container mtusi2026lab1-api-15 Started + Container mtusi2026lab1-api-16 Waiting + Container mtusi2026lab1-api-15 Waiting + Container mtusi2026lab1-api-16 Healthy + Container mtusi2026lab1-api-15 Healthy +Failure test 4856ec40e6db 87e7909e-5d06-4d0e-9314-5bdc86b2f617 +Failure test 6205d0db9981 f9491c25-70ac-4b45-935d-f742415b6366 +Both replicas survived sequential hard-failure tests +lab3-rolling.txt PASS +lab3-persistence.txt PASS +lab3-final-smoke.txt PASS +PASS: all lab3 acceptance checks diff --git a/examples/requests.http b/examples/requests.http index 492f9383..431683cd 100644 --- a/examples/requests.http +++ b/examples/requests.http @@ -20,11 +20,9 @@ Content-Type: application/json "password": "Example-only-not-a-real-secret-123" } -### Create session: generate the Base64 value locally, do not commit credentials -# Base64 of UTF-8 username:password; only this endpoint accepts Basic. +### Create session with Basic; the HTTP client encodes UTF-8 credentials POST {{baseUrl}}/api/v1/auth/sessions -Authorization: Basic REPLACE_WITH_LOCAL_BASE64_CREDENTIAL - +Authorization: Basic admin admin-password-123 ### Create chat; both UUIDs must be real registered users POST {{baseUrl}}/api/v1/chats @@ -35,8 +33,8 @@ Content-Type: application/json ### Send message POST {{baseUrl}}/api/v1/chats/{{chatId}}/messages +Idempotency-Key: {{$uuid}} Authorization: Bearer {{sessionToken}} -Idempotency-Key: example-message-001 Content-Type: application/json {"text":"Привет, Боб!"} diff --git a/infra/haproxy.cfg b/infra/haproxy.cfg new file mode 100644 index 00000000..5a96573b --- /dev/null +++ b/infra/haproxy.cfg @@ -0,0 +1,43 @@ +global + log stdout format raw local0 + maxconn 2048 + stats socket /tmp/haproxy.sock mode 600 level admin + stats socket ipv4@127.0.0.1:9999 level admin + +defaults + log global + mode http + option httplog + timeout connect 1s + timeout client 15s + timeout server 12s + timeout http-request 7s + timeout http-keep-alive 2s + timeout check 1s + # Не повторяем POST автоматически: неизвестный результат повторяет клиент + # с исходным Idempotency-Key. Соединения к недоступному API не ждут долго. + retries 0 + +resolvers docker + nameserver dns 127.0.0.11:53 + resolve_retries 3 + timeout resolve 1s + timeout retry 1s + hold valid 1s + hold obsolete 1s + hold nx 1s + hold timeout 1s + hold refused 1s + accepted_payload_size 8192 + +frontend public + bind :8080 + default_backend api_pool + +backend api_pool + 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 2 + server-template api 1-16 api:8080 check resolvers docker resolve-prefer ipv4 init-addr none diff --git a/lab.json b/lab.json index 57bb9fbb..5194777c 100644 --- a/lab.json +++ b/lab.json @@ -4,5 +4,5 @@ "contract_version": "1.3.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/env.py b/migrations/env.py new file mode 100644 index 00000000..16eaf7ee --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,34 @@ +import asyncio + +from alembic import context +from sqlalchemy import pool +from sqlalchemy.ext.asyncio import create_async_engine + +from src.core.db.models import Base +from src.core.settings import settings + +url = settings.sqlalchemy_database_url +target_metadata = Base.metadata + + +def run_migrations(connection): + context.configure(connection=connection, target_metadata=target_metadata, compare_type=True) + with context.begin_transaction(): + context.run_migrations() + + +async def run_online(): + engine = create_async_engine(url, poolclass=pool.NullPool) + try: + async with engine.connect() as connection: + await connection.run_sync(run_migrations) + finally: + await engine.dispose() + + +if context.is_offline_mode(): + context.configure(url=url, target_metadata=target_metadata, literal_binds=True) + with context.begin_transaction(): + context.run_migrations() +else: + asyncio.run(run_online()) diff --git a/migrations/script.py.mako b/migrations/script.py.mako new file mode 100644 index 00000000..5449f624 --- /dev/null +++ b/migrations/script.py.mako @@ -0,0 +1,17 @@ +"""${message}""" +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/migrations/versions/001_initial.py b/migrations/versions/001_initial.py new file mode 100644 index 00000000..b8cef401 --- /dev/null +++ b/migrations/versions/001_initial.py @@ -0,0 +1,60 @@ +"""Пользователи, чаты, участники, сообщения и сессии.""" +from alembic import op +import sqlalchemy as sa + +revision = '001' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table( + 'users', + sa.Column('id', sa.Uuid(), primary_key=True), + sa.Column('username', sa.String(32), nullable=False, unique=True), + sa.Column('display_name', sa.String(100), nullable=False), + sa.Column('password_hash', sa.Text(), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), nullable=False), + sa.CheckConstraint("username ~ '^[a-z0-9_]{3,32}$'"), + sa.CheckConstraint('length(trim(display_name)) > 0'), + ) + op.create_table( + 'chats', + sa.Column('id', sa.Uuid(), primary_key=True), + sa.Column('title', sa.String(100), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), nullable=False), + sa.CheckConstraint('length(trim(title)) > 0'), + ) + op.create_table( + 'chat_members', + sa.Column('chat_id', sa.Uuid(), sa.ForeignKey('chats.id', ondelete='CASCADE'), primary_key=True), + sa.Column('user_id', sa.Uuid(), sa.ForeignKey('users.id'), primary_key=True), + ) + op.create_index('chat_members_user_idx', 'chat_members', ['user_id', 'chat_id']) + op.create_table( + 'messages', + sa.Column('id', sa.Uuid(), primary_key=True), + sa.Column('chat_id', sa.Uuid(), sa.ForeignKey('chats.id'), nullable=False), + sa.Column('sender_id', sa.Uuid(), sa.ForeignKey('users.id'), nullable=False), + sa.Column('text', sa.String(4000), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint(['chat_id', 'sender_id'], ['chat_members.chat_id', 'chat_members.user_id']), + sa.CheckConstraint('length(trim(text)) > 0'), + ) + op.create_index('messages_history_idx', 'messages', ['chat_id', 'created_at', 'id']) + op.create_table( + 'sessions', + sa.Column('token_hash', sa.Text(), primary_key=True), + sa.Column('user_id', sa.Uuid(), sa.ForeignKey('users.id', ondelete='CASCADE'), nullable=False), + sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False), + ) + op.create_index('sessions_expiry_idx', 'sessions', ['expires_at']) + + +def downgrade(): + op.drop_table('sessions') + op.drop_table('messages') + op.drop_table('chat_members') + op.drop_table('chats') + op.drop_table('users') diff --git a/migrations/versions/002_redis_sessions.py b/migrations/versions/002_redis_sessions.py new file mode 100644 index 00000000..109638b5 --- /dev/null +++ b/migrations/versions/002_redis_sessions.py @@ -0,0 +1,22 @@ +"""Сессии перенесены в Redis.""" +from alembic import op +import sqlalchemy as sa + +revision = '002' +down_revision = '001' +branch_labels = None +depends_on = None + + +def upgrade(): + op.drop_table('sessions') + + +def downgrade(): + op.create_table( + 'sessions', + sa.Column('token_hash', sa.Text(), primary_key=True), + sa.Column('user_id', sa.Uuid(), sa.ForeignKey('users.id', ondelete='CASCADE'), nullable=False), + sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False), + ) + op.create_index('sessions_expiry_idx', 'sessions', ['expires_at']) diff --git a/migrations/versions/003_admin.py b/migrations/versions/003_admin.py new file mode 100644 index 00000000..7b57ee8c --- /dev/null +++ b/migrations/versions/003_admin.py @@ -0,0 +1,16 @@ +"""Признак системного администратора.""" +from alembic import op +import sqlalchemy as sa + +revision = '003' +down_revision = '002' +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column('users', sa.Column('is_admin', sa.Boolean(), nullable=False, server_default=sa.false())) + + +def downgrade(): + op.drop_column('users', 'is_admin') diff --git a/migrations/versions/004_idempotency.py b/migrations/versions/004_idempotency.py new file mode 100644 index 00000000..017366af --- /dev/null +++ b/migrations/versions/004_idempotency.py @@ -0,0 +1,25 @@ +"""Общая идемпотентность сообщений, совместимая с предыдущей схемой.""" + +from alembic import op +import sqlalchemy as sa + +revision = "004" +down_revision = "003" +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table( + "message_requests", + sa.Column("sender_id", sa.Uuid(), sa.ForeignKey("users.id"), primary_key=True), + sa.Column("chat_id", sa.Uuid(), sa.ForeignKey("chats.id"), primary_key=True), + sa.Column("key", sa.String(128), primary_key=True), + sa.Column("request_text", sa.String(4000), nullable=False), + sa.Column("message_id", sa.Uuid(), sa.ForeignKey("messages.id"), nullable=True), + sa.Column("expires_at", sa.DateTime(timezone=True), nullable=False), + ) + + +def downgrade(): + op.drop_table("message_requests") diff --git a/migrations/versions/005_message_unicode.py b/migrations/versions/005_message_unicode.py new file mode 100644 index 00000000..762803d8 --- /dev/null +++ b/migrations/versions/005_message_unicode.py @@ -0,0 +1,55 @@ +"""Сохранять весь допустимый Unicode сообщений, включая U+0000, без потерь.""" + +import sqlalchemy as sa +from alembic import op + +revision = "005" +down_revision = "004" +branch_labels = None +depends_on = None + + +def upgrade(): + op.drop_constraint("messages_text_check", "messages", type_="check") + op.alter_column( + "messages", + "text", + existing_type=sa.String(4000), + type_=sa.LargeBinary(), + existing_nullable=False, + postgresql_using="convert_to(text, 'UTF8')", + ) + op.alter_column( + "message_requests", + "request_text", + existing_type=sa.String(4000), + type_=sa.LargeBinary(), + existing_nullable=False, + postgresql_using="convert_to(request_text, 'UTF8')", + ) + op.create_check_constraint( + "messages_text_bytes_check", "messages", "octet_length(text) BETWEEN 1 AND 16000" + ) + + +def downgrade(): + # convert_from откажет при наличии U+0000: не удаляем символы или строки + # молча. Откат возможен только если все данные представимы в PostgreSQL text. + op.drop_constraint("messages_text_bytes_check", "messages", type_="check") + op.alter_column( + "messages", + "text", + existing_type=sa.LargeBinary(), + type_=sa.String(4000), + existing_nullable=False, + postgresql_using="convert_from(text, 'UTF8')", + ) + op.alter_column( + "message_requests", + "request_text", + existing_type=sa.LargeBinary(), + type_=sa.String(4000), + existing_nullable=False, + postgresql_using="convert_from(request_text, 'UTF8')", + ) + op.create_check_constraint("messages_text_check", "messages", "length(trim(text)) > 0") diff --git a/migrations/versions/006_unicode_names.py b/migrations/versions/006_unicode_names.py new file mode 100644 index 00000000..e60df496 --- /dev/null +++ b/migrations/versions/006_unicode_names.py @@ -0,0 +1,40 @@ +"""Сохранение допустимого Unicode имён и названий, включая U+0000.""" + +import sqlalchemy as sa +from alembic import op + +revision = "006" +down_revision = "005" +branch_labels = None +depends_on = None + + +def upgrade(): + for table, column in [("users", "display_name"), ("chats", "title")]: + op.drop_constraint(f"{table}_{column}_check", table, type_="check") + op.alter_column( + table, + column, + existing_type=sa.String(100), + type_=sa.LargeBinary(), + existing_nullable=False, + postgresql_using=f"convert_to({column}, 'UTF8')", + ) + op.create_check_constraint( + f"{table}_{column}_bytes_check", table, f"octet_length({column}) BETWEEN 1 AND 400" + ) + + +def downgrade(): + # PostgreSQL откажет при U+0000 и откатит транзакцию вместо потери символов. + for table, column in [("users", "display_name"), ("chats", "title")]: + op.drop_constraint(f"{table}_{column}_bytes_check", table, type_="check") + op.alter_column( + table, + column, + existing_type=sa.LargeBinary(), + type_=sa.String(100), + existing_nullable=False, + postgresql_using=f"convert_from({column}, 'UTF8')", + ) + op.create_check_constraint(f"{table}_{column}_check", table, f"length(trim({column})) > 0") diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..6c704779 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,7 @@ +[tool.ruff] +target-version = "py313" +line-length = 100 +include = ["src/**/*.py"] + +[tool.ruff.lint] +select = ["E4", "E7", "E9", "F", "I"] diff --git a/requirements-dev.txt b/requirements-dev.txt index 6b103ddb..fc598663 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,2 +1,6 @@ -# Only for full OpenAPI/JSON Schema validation; HTTP tests use stdlib. +# Development tools: OpenAPI validation, linting and formatting. HTTP tests use stdlib. openapi-spec-validator==0.7.2 +ruff==0.14.2 + +# Research charts (not installed in the application image). +matplotlib==3.11.2 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..284bbcf5 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,19 @@ +annotated-doc==0.0.5 +annotated-types==0.8.0 +anyio==4.15.1 +click==8.5.0 +fastapi==0.141.1 +h11==0.16.0 +idna==3.20 +pydantic==2.13.5 +pydantic-core==2.46.5 +starlette==1.7.0 +typing-extensions==4.16.0 +typing-inspection==0.4.4 +uvicorn==0.53.0 +asyncpg==0.30.0 +redis==5.2.1 +argon2-cffi==25.1.0 +SQLAlchemy[asyncio]==2.0.44 +alembic==1.17.0 +pydantic-settings==2.15.0 diff --git a/scripts/audit-dependencies.py b/scripts/audit-dependencies.py new file mode 100644 index 00000000..24b164a4 --- /dev/null +++ b/scripts/audit-dependencies.py @@ -0,0 +1,61 @@ +"""Проверка реально установленных Python-пакетов API через публичную базу OSV.""" +import argparse +from concurrent.futures import ThreadPoolExecutor +from datetime import datetime, timezone +import json +from pathlib import Path +import subprocess +import ssl +from urllib.request import Request, urlopen + + +def fetch(url, data=None): + request = Request(url, data=None if data is None else json.dumps(data).encode(), + headers={'Content-Type': 'application/json'}) + with urlopen(request, timeout=30, context=TLS_CONTEXT) as response: + return json.load(response) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--output', default='evidence/lab2-production-dependencies.json') + parser.add_argument('--ca-file', help='Дополнительный доверенный CA bundle; TLS-проверка остаётся включённой') + args = parser.parse_args() + global TLS_CONTEXT + TLS_CONTEXT = ssl.create_default_context(cafile=args.ca_file) + raw = subprocess.check_output(['docker', 'compose', 'exec', '-T', 'api', 'python', '-c', + 'import importlib.metadata as m,json; print(json.dumps(sorted({d.metadata["Name"]:d.version for d in m.distributions()}.items())))'], + text=True, timeout=20) + packages = json.loads(raw) + queries = [{'package': {'ecosystem': 'PyPI', 'name': name}, 'version': version} + for name, version in packages] + pending = list(enumerate(queries)) + vulnerabilities = {i: set() for i in range(len(packages))} + while pending: + results = fetch('https://api.osv.dev/v1/querybatch', {'queries': [q for _, q in pending]})['results'] + if len(results) != len(pending): + raise RuntimeError('Неполный ответ OSV') + next_pending = [] + for (index, query), result in zip(pending, results): + vulnerabilities[index].update(v['id'] for v in result.get('vulns', [])) + if result.get('next_page_token'): + next_pending.append((index, {**query, 'page_token': result['next_page_token']})) + pending = next_pending + ids = sorted(set().union(*vulnerabilities.values())) + with ThreadPoolExecutor(max_workers=4) as pool: + records = dict(zip(ids, pool.map(lambda i: fetch('https://api.osv.dev/v1/vulns/' + i), ids))) + findings = [{'name': packages[i][0], 'version': packages[i][1], + 'vulnerabilities': [records[v] for v in sorted(found)]} + for i, found in vulnerabilities.items() if found] + report = {'checked_at_utc': datetime.now(timezone.utc).isoformat(), + 'source': 'https://api.osv.dev/v1/querybatch', 'packages': packages, 'findings': findings, + 'scope': 'Python packages in running API; excludes OS/container image vulnerabilities'} + Path(args.output).write_text(json.dumps(report, ensure_ascii=False, indent=2) + '\n') + print('OSV: проверено пакетов:', len(packages), '; пакетов с известными advisory:', len(findings)) + for finding in findings: + print(finding['name'], finding['version'], ', '.join(v['id'] for v in finding['vulnerabilities'])) + raise SystemExit(bool(findings)) + + +if __name__ == '__main__': + main() diff --git a/scripts/audit-lab3.py b/scripts/audit-lab3.py new file mode 100644 index 00000000..0335edf3 --- /dev/null +++ b/scripts/audit-lab3.py @@ -0,0 +1,550 @@ +"""Воспроизведение замечаний к lab3. Только текущий локальный Compose-стенд. +Временные переименования, pause и замены контейнеров восстанавливаются в finally. +Секреты не печатаются. Все созданные пользователи и сообщения синтетические. +""" + +import argparse +import base64 +import http.client +import json +import os +import subprocess +import sys +import threading +import time +import uuid +from concurrent.futures import ThreadPoolExecutor +from urllib.parse import urlsplit + +from replicas import ( + ROOT, + compose, + containers, + describe, + docker, + instance_id, + wait_ready, + wait_visible, +) + +sys.path.insert(0, str(ROOT / "tests")) +from client import Client + +EVIDENCE = ROOT / "evidence" + + +def sql(query): + return subprocess.check_output( + [ + "docker", + "compose", + "exec", + "-T", + "postgres", + "sh", + "-c", + 'exec psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB" -At', + ], + cwd=ROOT, + input=query, + text=True, + timeout=15, + ).strip() + + +def wire(method, path, actor=None, body=None, timeout=7): + url = urlsplit(os.environ.get("BASE_URL", "http://localhost:8080")) + headers = {"Content-Type": "application/json"} + if actor: + headers["Authorization"] = "Bearer " + actor["token"] + conn = http.client.HTTPConnection(url.hostname, url.port, timeout=timeout) + before = time.monotonic() + try: + conn.request( + method, + path, + body=None if body is None else json.dumps(body).encode(), + headers=headers, + ) + response = conn.getresponse() + response.read() + return { + "status": response.status, + "seconds": round(time.monotonic() - before, 3), + "instance": response.getheader("X-Instance-Id"), + } + finally: + conn.close() + + +def direct_ready(cid): + code = """import urllib.request,urllib.error +try: r=urllib.request.urlopen('http://127.0.0.1:8080/health/ready',timeout=6) +except urllib.error.HTTPError as e: r=e +with r: print(r.status) +""" + return int(docker("exec", cid, "python", "-c", code)) + + +def restored(): + for cid in containers(): + wait_visible(wait_ready(cid)) + + +def contract(): + client = Client() + alice, bob, eve, chat = client.bootstrap() + path = "/api/v1/chats/" + chat["id"] + "/messages" + for endpoint in ["/api/v1/chats", path]: + for query in [ + "cursor=W10", + "cursor=bnVsbA", + "limit=abc", + "limit=0", + "limit=101", + ]: + client.call("GET", endpoint + "?" + query, 400, actor=alice) + # Unicode Basic password including colon and no body on login. + registration = { + "username": "audit_utf_" + uuid.uuid4().hex[:12], + "display_name": "Unicode", + "password": "пароль:😀с-пробелом 123", + } + client.call("POST", "/api/v1/users", 201, body=registration, schema="User") + basic = ( + "Basic " + + base64.b64encode( + (registration["username"] + ":" + registration["password"]).encode() + ).decode() + ) + client.call( + "POST", + "/api/v1/auth/sessions", + 201, + headers={"Authorization": basic}, + schema="Session", + ) + client.call( + "POST", + "/api/v1/auth/sessions", + 400, + raw=b"{}", + headers={"Authorization": basic}, + ) + client.call( + "POST", + "/api/v1/chats", + 400, + actor=alice, + body={ + "title": "Unknown member", + "member_ids": [alice["id"], str(uuid.uuid4())], + }, + ) + # Empty page limit=7 is warmed before the successful write. + assert not client.call("GET", path + "?limit=7", actor=alice, schema="MessagePage")[ + "json" + ]["items"] + key = uuid.uuid4().hex + barrier = threading.Barrier(16) + clients = [Client() for _ in range(16)] + message = "Начало\u0000конец 👋" + + def send(local): + barrier.wait() + return local.message(alice, chat, message, key=key) + + with ThreadPoolExecutor(max_workers=16) as pool: + responses = list(pool.map(send, clients)) + original = responses[0]["json"] + assert all(r["json"] == original for r in responses) + observed = {r["headers"]["X-Instance-Id"] for r in responses} + assert len(observed) >= 2 + assert int(sql(f"SELECT count(*) FROM messages WHERE chat_id='{chat['id']}'")) == 1 + assert ( + int(sql(f"SELECT count(*) FROM message_requests WHERE chat_id='{chat['id']}'")) + == 1 + ) + assert ( + int( + sql( + f"SELECT position(decode('00','hex') IN text) FROM messages WHERE id='{original['id']}'" + ) + ) + > 0 + ) + for limit in [1, 7, 10, 20, 50, 100]: + result = client.call( + "GET", path + "?limit=" + str(limit), actor=bob, schema="MessagePage" + )["json"] + assert result["items"] == [original], "Stale history or lost U+0000" + client.call( + "POST", + path, + 409, + actor=alice, + body={"text": "changed"}, + headers={"Idempotency-Key": key}, + ) + chat2 = client.call( + "POST", + "/api/v1/chats", + 201, + actor=alice, + body={"title": "Other chat", "member_ids": [alice["id"]]}, + schema="Chat", + )["json"] + client.message(alice, chat2, message, key=key) + client.message(bob, chat, message, key=key) + for bad in ["", "x" * 129, "bad key", "bad/key"]: + client.call( + "POST", + path, + 400, + actor=alice, + body={"text": "bad key"}, + headers={"Idempotency-Key": bad}, + ) + # Bind cursor to actual chat; limit=1 produces a nonempty cursor. + page = client.call("GET", path + "?limit=1", actor=alice, schema="MessagePage")[ + "json" + ] + client.call( + "GET", + "/api/v1/chats/" + chat2["id"] + "/messages?cursor=" + page["next_cursor"], + 400, + actor=alice, + ) + large = client.call( + "POST", + path, + 413, + actor=alice, + raw=b'{"text":"valid"}' + b" " * 70000, + headers={ + "Content-Type": "application/json", + "Idempotency-Key": uuid.uuid4().hex, + }, + ) + assert large["headers"]["X-Instance-Id"] and large["headers"]["X-Request-Id"] + # A raw U+0000 is illegal JSON; escaped \u0000 is valid and must round-trip. + client.call( + "POST", + path, + 400, + actor=alice, + raw=b'{"text":"bad\x00json"}', + headers={ + "Content-Type": "application/json", + "Idempotency-Key": uuid.uuid4().hex, + }, + ) + client.call( + "POST", + path, + 404, + actor=eve, + body={"text": message}, + headers={"Idempotency-Key": key}, + ) + # Exercise boundary on Unicode code points, not UTF-8 byte count. + long_message = client.message(alice, chat, "\u0000" * 4000)["json"] + assert long_message["text"] == "\u0000" * 4000 + replay = client.message(alice, chat, message, key=key) + assert replay["json"] == original + return { + "result": "PASS", + "simultaneous_requests": 16, + "observed_instances": sorted(observed), + "database_rows_for_first_key": 1, + "headers_413": "present", + "nul_roundtrip": "201, replay and history; 4000 code points supported", + "checks": [ + "invalid cursor types/limit", + "UTF-8 Basic", + "login body rejected", + "unknown member 400", + "key scope/format", + "foreign cursor", + "fresh history limit=7 and other limits", + "outsider hidden", + "raw invalid JSON rejected", + ], + } + + +def readiness(): + client = Client() + actor = client.user("audit_ready") + evidence = [] + for table in ["users", "chats", "chat_members", "messages", "message_requests"]: + hidden = "audit_hidden_" + table + try: + sql(f"ALTER TABLE {table} RENAME TO {hidden}") + statuses = {cid[:12]: direct_ready(cid) for cid in containers()} + assert set(statuses.values()) == {503}, statuses + deadline = time.monotonic() + 8 + while wire("GET", "/api/v1/users/me", actor)["status"] != 503: + assert time.monotonic() < deadline, "LB still routes to unready API" + time.sleep(0.2) + results = [] + for _ in range(8): + result = wire("GET", "/api/v1/users/me", actor) + assert result["status"] == 503, result + results.append(result) + registration = wire( + "POST", + "/api/v1/users", + body={ + "username": "blocked_" + uuid.uuid4().hex[:12], + "display_name": "Must not route", + "password": "valid-audit-password-123", + }, + ) + assert registration["status"] == 503, registration + evidence.append( + { + "missing_table": table, + "direct_readiness": statuses, + "business_requests": results, + "registration": registration, + } + ) + finally: + sql(f"ALTER TABLE IF EXISTS {hidden} RENAME TO {table}") + restored() + return {"result": "PASS", "experiments": evidence} + + +def frozen_stores(): + client = Client() + actor = client.user("audit_pause") + client.call("GET", "/api/v1/users/me", actor=actor, schema="User") + evidence = [] + for service in ["postgres", "redis"]: + try: + began = time.monotonic() + compose("pause", service) + transition = wire("GET", "/health/ready") + assert transition["status"] == 503, transition + assert transition["seconds"] < 7 + # Allow active checks to exclude all API, then test the LB route, + # including registration that itself does not depend on Redis. + time.sleep(4) + result = wire("GET", "/api/v1/users/me", actor) + registration = wire( + "POST", + "/api/v1/users", + body={ + "username": "paused_" + uuid.uuid4().hex[:12], + "display_name": "Paused store", + "password": "valid-audit-password-123", + }, + ) + assert result["status"] == registration["status"] == 503, ( + result, + registration, + ) + evidence.append( + { + "service": service, + "initial_ready_request": transition, + "business": result, + "registration": registration, + "elapsed_seconds": round(time.monotonic() - began, 3), + } + ) + finally: + compose("unpause", service) + restored() + client.call("GET", "/api/v1/users/me", actor=actor, schema="User") + return {"result": "PASS", "experiments": evidence} + + +def changing_ips(): + client = Client() + actor = client.user("audit_dns") + chat = client.call( + "POST", + "/api/v1/chats", + 201, + actor=actor, + body={"title": "Forced new IPs", "member_ids": [actor["id"]]}, + schema="Chat", + )["json"] + originals = containers() + lb = compose("ps", "-q", "lb") + lb_started = describe(lb)["StartedAt"] + holders = [] + events = [] + occupied = [] + try: + for old in originals: + networks = json.loads( + docker("inspect", "--format", "{{json .NetworkSettings.Networks}}", old) + ) + assert len(networks) == 1 + network, state = next(iter(networks.items())) + ip = state["IPAddress"] + alias = instance_id(old) + docker("stop", "--time", "10", old) + docker("rm", old) + holder = "lab3-audit-ip-" + uuid.uuid4().hex[:10] + docker( + "run", + "-d", + "--name", + holder, + "--network", + network, + "--ip", + ip, + "--cpus", + ".05", + "--memory", + "32m", + "--read-only", + "--cap-drop", + "ALL", + "--entrypoint", + "python", + "mtusi-messenger:lab3", + "-c", + "import time; time.sleep(900)", + ) + holders.append(holder) + occupied.append(ip) + before = set(containers()) + compose("up", "-d", "--no-deps", "--no-recreate", "--scale", "api=2", "api") + added = set(containers()) - before + assert len(added) == 1 + new = added.pop() + new_alias = wait_ready(new) + new_ip = docker( + "inspect", + "--format", + "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}", + new, + ) + assert new_ip not in occupied + began = time.monotonic() + wait_visible(new_alias, timeout=10) + message = client.message(actor, chat, "DNS " + new_alias)["json"] + page = client.call( + "GET", + "/api/v1/chats/" + chat["id"] + "/messages", + actor=actor, + schema="MessagePage", + )["json"] + assert message in page["items"] + events.append( + { + "old_container": old[:12], + "old_ip": ip, + "old_instance": alias, + "new_container": new[:12], + "new_ip": new_ip, + "new_instance": new_alias, + "discovery_after_ready_seconds": round(time.monotonic() - began, 3), + } + ) + observed = { + client.call("GET", "/api/v1/users/me", actor=actor, schema="User")[ + "headers" + ]["X-Instance-Id"] + for _ in range(40) + } + assert observed == {e["new_instance"] for e in events}, observed + assert describe(lb)["StartedAt"] == lb_started, "HAProxy was restarted" + return { + "result": "PASS", + "events": events, + "observed_instances_after": sorted(observed), + "lb_restarted": False, + "old_ips_kept_occupied_during_test": occupied, + } + finally: + for holder in holders: + docker("rm", "-f", holder) + compose("up", "-d", "--no-deps", "--wait", "api") + restored() + + +def limits_and_evidence(): + limits = {} + for service in ["api", "postgres", "redis", "lb", "migration"]: + limits[service] = [] + for cid in compose("ps", "--all", "-q", service).split(): + state = json.loads( + docker("inspect", "--format", "{{json .HostConfig}}", cid) + ) + record = { + "container": cid[:12], + "cpu": state["NanoCpus"] / 1e9, + "memory_bytes": state["Memory"], + } + assert record["cpu"] > 0 and record["memory_bytes"] > 0 + if service == "api": + assert not state["PortBindings"] + limits[service].append(record) + rows = json.loads((EVIDENCE / "lab3-research/results.json").read_text()) + for series in ["fixed", "growing"]: + for count in [1, 2, 4]: + assert any( + r["job"]["series"] == series and r["job"]["replicas"] == count + for r in rows + ) + fixed = [r for r in rows if r["job"]["series"] == "fixed"] + assert {r["job"]["replicas"] * r["job"]["cpus"] for r in fixed} == {2.0} + assert {r["job"]["replicas"] * int(r["job"]["memory"][:-1]) for r in fixed} == { + 1024 + } + assert any(r["job"]["series"] == "improved-db" for r in rows) + for name in ["results.json", "summary.csv", "scaling.png", "bottleneck.png"]: + assert (EVIDENCE / "lab3-research" / name).stat().st_size > 0 + # The rolling command explicitly invokes Python and needs no executable bit. + assert "$(PYTHON) scripts/rolling-update.py" in (ROOT / "Makefile").read_text() + for file in ["scripts/restart.sh", "scripts/stop-one.sh"]: + assert (ROOT / file).stat().st_mode & 0o111 + return { + "result": "PASS", + "limits": limits, + "fixed_api_budget": {"cpu": 2, "memory_mib": 1024}, + "research_runs": len(rows), + "rolling_command": "python3 scripts/rolling-update.py", + "shell_hooks_executable": True, + } + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "--phase", choices=["all", "contract", "infrastructure"], default="all" + ) + args = parser.parse_args() + checks = [("contract", contract)] if args.phase == "contract" else [] + if args.phase == "all": + checks.append(("contract", contract)) + if args.phase in ["all", "infrastructure"]: + checks += [ + ("readiness", readiness), + ("frozen-stores", frozen_stores), + ("changing-ips", changing_ips), + ("limits-evidence", limits_and_evidence), + ] + summary = {} + for name, check in checks: + print("Checking " + name, flush=True) + result = check() + (EVIDENCE / ("lab3-audit-" + name + ".json")).write_text( + json.dumps(result, ensure_ascii=False, indent=2) + "\n" + ) + summary[name] = "PASS" + print(name + " PASS", flush=True) + (EVIDENCE / ("lab3-audit-summary-" + args.phase + ".json")).write_text( + json.dumps(summary, indent=2) + "\n" + ) + + +if __name__ == "__main__": + main() diff --git a/scripts/benchmark.py b/scripts/benchmark.py new file mode 100644 index 00000000..866b2cb4 --- /dev/null +++ b/scripts/benchmark.py @@ -0,0 +1,167 @@ +"""Повторяемый closed-loop: постоянные HTTP-соединения, 80% POST / 20% GET. +Не измеряет open-loop SLA. Ошибки и задержки всех попыток учитываются отдельно. +""" + +import http.client +import json +import math +import os +import sys +import threading +import time +import uuid +from collections import Counter +from concurrent.futures import ThreadPoolExecutor +from pathlib import Path +from urllib.parse import urlsplit + +from runtime_config import base_url + +os.environ.setdefault("BASE_URL", base_url()) + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests")) +from client import Client + + +def fixture(): + c = Client(os.environ.get("BASE_URL")) + actor = c.user("bench") + chat = c.call( + "POST", + "/api/v1/chats", + 201, + actor=actor, + body={"title": "Synthetic benchmark", "member_ids": [actor["id"]]}, + schema="Chat", + )["json"] + for n in range(20): + c.message(actor, chat, "seed-%d" % n) + return actor, chat + + +def measure(actor, chat, duration=15, concurrency=16, event=None): + url = urlsplit(os.environ.get("BASE_URL", "http://localhost:8080")) + assert url.scheme == "http", "Local isolated HTTP benchmark only" + path = "/api/v1/chats/" + chat["id"] + "/messages" + barrier = threading.Barrier(concurrency + 1) + start = [None] + prefix = uuid.uuid4().hex + + def worker(index): + connection = http.client.HTTPConnection(url.hostname, url.port or 80, timeout=5) + headers = { + "Authorization": "Bearer " + actor["token"], + "Content-Type": "application/json", + } + latencies, successes, failures, instances, seconds, confirmations = ( + [], + Counter(), + Counter(), + set(), + {}, + [], + ) + completed = [] + counter = 0 + barrier.wait() + while time.monotonic() < start[0] + duration: + write = counter % 5 != 4 + key = "%s-%d-%d" % (prefix, index, counter) + body = json.dumps({"text": "load-" + key}) if write else None + before = time.monotonic() + status = 0 + try: + connection.request( + "POST" if write else "GET", + path if write else path + "?limit=20", + body=body, + headers={**headers, "Idempotency-Key": key}, + ) + response = connection.getresponse() + data = response.read() + status = response.status + if status == (201 if write else 200): + parsed = json.loads(data) + successes["write" if write else "read"] += 1 + instances.add(response.getheader("X-Instance-Id")) + completed.append((time.monotonic(), "write" if write else "read")) + if write: + confirmations.append((key, parsed["id"])) + else: + failures[str(status)] += 1 + except (OSError, http.client.HTTPException, ValueError): + failures["connection"] += 1 + connection.close() + elapsed = time.monotonic() - before + latencies.append((elapsed, status == (201 if write else 200))) + bucket = int(before - start[0]) + record = seconds.setdefault(bucket, {"success": 0, "error": 0}) + record["success" if status == (201 if write else 200) else "error"] += 1 + counter += 1 + connection.close() + return ( + latencies, + successes, + failures, + instances, + seconds, + confirmations, + completed, + ) + + with ThreadPoolExecutor(max_workers=concurrency) as pool: + futures = [pool.submit(worker, i) for i in range(concurrency)] + start[0] = time.monotonic() + barrier.wait() + results = [f.result() for f in futures] + elapsed = time.monotonic() - start[0] + latencies = sorted(v for r in results for v, _ in r[0]) + successful = sorted(v for r in results for v, ok in r[0] if ok) + successes, failures, instances, seconds = Counter(), Counter(), set(), {} + for _, ok, bad, seen, buckets, _, _ in results: + successes.update(ok) + failures.update(bad) + instances.update(seen) + for b, counts in buckets.items(): + dest = seconds.setdefault(b, {"success": 0, "error": 0}) + for k, v in counts.items(): + dest[k] += v + + def percentiles(values): + return { + name: round(values[max(0, math.ceil(p * len(values)) - 1)] * 1000, 3) + if values + else None + for name, p in [("p50", 0.5), ("p95", 0.95), ("p99", 0.99)] + } + + report = { + "duration_seconds": duration, + "elapsed_seconds": round(elapsed, 3), + "concurrency": concurrency, + "successes": dict(successes), + "errors": dict(failures), + "rps": round(sum(successes.values()) / elapsed, 3), + "all_attempt_latency_ms": percentiles(latencies), + "successful_latency_ms": percentiles(successful), + "instances": sorted(instances), + "seconds": seconds, + "profile": "closed-loop, keepalive, 80% unique POST/20% GET first 20, no retries", + } + if event: + report["first_success_after_action_seconds"] = { + kind: round( + min( + ( + at - event[0] + for r in results + for at, method in r[6] + if method == kind and at >= event[0] + ), + default=duration, + ), + 3, + ) + for kind in ["read", "write"] + } + return report, [x for r in results for x in r[5]] diff --git a/scripts/check-transfer.py b/scripts/check-transfer.py new file mode 100644 index 00000000..9f8adfa0 --- /dev/null +++ b/scripts/check-transfer.py @@ -0,0 +1,73 @@ +"""Проверка реализации в новом клоне: контракт, restart и отказ одной API.""" + +import hashlib +import json +import subprocess +import sys + +from replicas import ROOT, compose, containers, describe, wait_ready, wait_visible + + +def check(name, *command): + result = subprocess.run(command, cwd=ROOT, text=True, capture_output=True) + (ROOT / "evidence" / ("lab3-transfer-" + name + ".txt")).write_text( + result.stdout + result.stderr + ) + print(name, "PASS" if result.returncode == 0 else "FAIL", flush=True) + if result.returncode: + raise RuntimeError((result.stdout + result.stderr)[-2000:]) + + +check( + "persistence", + sys.executable, + "tests/resilience.py", + "--mode", + "persistence", + "--action-script", + "scripts/restart.sh", +) +try: + check( + "failover", + sys.executable, + "tests/resilience.py", + "--mode", + "failover", + "--action-script", + "scripts/stop-one.sh", + ) +finally: + compose("up", "-d", "--no-deps", "--wait", "api") + for cid in containers(): + wait_visible(wait_ready(cid)) +check("final-smoke", sys.executable, "tests/smoke.py") +logs = compose("logs", "--no-color", "migration") +(ROOT / "evidence/lab3-transfer-migrations.txt").write_text(logs + "\n") +proof = { + "project": "mtusi2026lab3", + "schema_revision": "006", + "api_instances": len(containers()), + "api_states": { + cid[:12]: { + "status": describe(cid)["Status"], + "health": describe(cid).get("Health", {}).get("Status"), + } + for cid in containers() + }, + "public_sha256": { + name: hashlib.sha256((ROOT / name).read_bytes()).hexdigest() + for name in [ + "contracts/openapi.json", + "tests/client.py", + "tests/smoke.py", + "tests/resilience.py", + "tests/load.py", + ] + }, +} +assert proof["api_instances"] == 2 +(ROOT / "evidence/lab3-transfer-runtime.json").write_text( + json.dumps(proof, ensure_ascii=False, indent=2) + "\n" +) +print("PASS: transferred application retains data/session and survives API failover", flush=True) diff --git a/scripts/edge-audit-lab3.py b/scripts/edge-audit-lab3.py new file mode 100644 index 00000000..1ea0d651 --- /dev/null +++ b/scripts/edge-audit-lab3.py @@ -0,0 +1,344 @@ +"""Дополнительный adversarial-аудит: неоднозначность, Unicode, гонки, rollback. +Только синтетические данные. Временный DB-trigger действует на один случайный +ключ и удаляется в finally. Повреждённый кэш восстанавливается из PostgreSQL. +""" + +import base64 +import http.client +import json +import os +import subprocess +import sys +import threading +import uuid +from concurrent.futures import ThreadPoolExecutor +from urllib.parse import urlencode, urlsplit + +from replicas import ROOT + +sys.path.insert(0, str(ROOT / "tests")) +from client import Client + + +def sql(query): + return subprocess.check_output( + [ + "docker", + "compose", + "exec", + "-T", + "postgres", + "sh", + "-c", + 'exec psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB" -At', + ], + cwd=ROOT, + input=query, + text=True, + timeout=15, + ).strip() + + +def redis(*args, raw=None): + command = ["docker", "compose", "exec", "-T", "redis", "redis-cli", "--raw"] + if raw is not None: + command += ["-x"] + return subprocess.check_output( + command + list(args), cwd=ROOT, input=raw, timeout=10 + ).rstrip(b"\n") + + +def duplicate_headers(path, actor, pairs): + body = b'{"text":"ambiguous headers"}' + conn = http.client.HTTPConnection( + urlsplit(os.environ.get("BASE_URL", "http://localhost:8080")).hostname, + urlsplit(os.environ.get("BASE_URL", "http://localhost:8080")).port or 80, + timeout=5, + ) + try: + conn.putrequest("POST", path) + for name, value in [ + ("Authorization", "Bearer " + actor["token"]), + ("Content-Length", str(len(body))), + *pairs, + ]: + conn.putheader(name, value) + conn.endheaders(body) + response = conn.getresponse() + response.read() + assert response.status == 400, response.status + assert response.getheader("X-Instance-Id") and response.getheader( + "X-Request-Id" + ) + finally: + conn.close() + + +def main(): + client = Client() + alice, bob, eve, chat = client.bootstrap() + path = "/api/v1/chats/" + chat["id"] + "/messages" + duplicate_headers( + path, + alice, + [ + ("Content-Type", "application/json"), + ("Idempotency-Key", uuid.uuid4().hex), + ("Idempotency-Key", uuid.uuid4().hex), + ], + ) + duplicate_headers( + path, + alice, + [ + ("Content-Type", "application/json"), + ("Content-Type", "text/plain"), + ("Idempotency-Key", uuid.uuid4().hex), + ], + ) + for endpoint in ["/api/v1/chats", path]: + for val in ["1.0", "1e0", "+1", " 1 ", "١", "true", "-1", "101"]: + client.call( + "GET", endpoint + "?" + urlencode({"limit": val}), 400, actor=alice + ) + for query in ["limit=1&limit=100", "cursor=W10&cursor=bnVsbA", "extra=1"]: + client.call("GET", endpoint + "?" + query, 400, actor=alice) + # Every Unicode scalar permitted by the contract is stored unchanged. + special = "Имя\u0000😀" + registration = { + "username": "edge_" + uuid.uuid4().hex[:16], + "display_name": special, + "password": " пароль:\u0000😀с пробелами ", + } + registered = client.call( + "POST", "/api/v1/users", 201, body=registration, schema="User" + )["json"] + assert registered["display_name"] == special + basic = ( + "Basic " + + base64.b64encode( + (registration["username"] + ":" + registration["password"]).encode() + ).decode() + ) + session = client.call( + "POST", + "/api/v1/auth/sessions", + 201, + headers={"Authorization": basic}, + schema="Session", + )["json"] + assert session["user"] == registered + special_actor = {"id": registered["id"], "token": session["token"]} + assert ( + client.call("GET", "/api/v1/users/me", actor=special_actor, schema="User")[ + "json" + ] + == registered + ) + title = "Чат\u0000😀" + special_chat = client.call( + "POST", + "/api/v1/chats", + 201, + actor=special_actor, + body={"title": title, "member_ids": [registered["id"]]}, + schema="Chat", + )["json"] + assert special_chat["title"] == title + assert ( + client.call( + "GET", + "/api/v1/chats/" + special_chat["id"], + actor=special_actor, + schema="Chat", + )["json"] + == special_chat + ) + maximum_name = { + **registration, + "username": "max_" + uuid.uuid4().hex[:16], + "display_name": "😀" * 100, + } + assert ( + client.call("POST", "/api/v1/users", 201, body=maximum_name, schema="User")[ + "json" + ]["display_name"] + == "😀" * 100 + ) + max_chat = client.call( + "POST", + "/api/v1/chats", + 201, + actor=alice, + body={"title": "😀" * 100, "member_ids": [alice["id"]]}, + schema="Chat", + )["json"] + assert max_chat["title"] == "😀" * 100 + for value in ["\u0000", "😀" * 4000, "e\u0301", "\u200b", " Пробелы\nсохраняются "]: + assert client.message(alice, chat, value)["json"]["text"] == value + for value in ["😀" * 4001, "\u2003" * 4, None, 123, True, [], {}]: + client.call( + "POST", + path, + 400, + actor=alice, + body={"text": value}, + headers={"Idempotency-Key": uuid.uuid4().hex}, + ) + same_key = uuid.uuid4().hex + client.message(alice, chat, "é", key=same_key) + client.call( + "POST", + path, + 409, + actor=alice, + body={"text": "e\u0301"}, + headers={"Idempotency-Key": same_key}, + ) + # Conflicting simultaneous content must commit exactly one of the bodies. + race_chat = client.call( + "POST", + "/api/v1/chats", + 201, + actor=alice, + body={"title": "Conflicting race", "member_ids": [alice["id"]]}, + schema="Chat", + )["json"] + race_path = "/api/v1/chats/" + race_chat["id"] + "/messages" + race_key = uuid.uuid4().hex + barrier = threading.Barrier(16) + locals_ = [Client() for _ in range(16)] + + def contender(i): + barrier.wait() + return locals_[i].call( + "POST", + race_path, + [201, 409], + actor=alice, + body={"text": "A" if i % 2 else "B"}, + headers={"Idempotency-Key": race_key}, + schema="Message", + ) + + with ThreadPoolExecutor(max_workers=16) as pool: + results = list(pool.map(contender, range(16))) + winners = [r["json"] for r in results if r["status"] == 201] + assert len(winners) == 8 and all(m == winners[0] for m in winners) + assert ( + int(sql(f"SELECT count(*) FROM messages WHERE chat_id='{race_chat['id']}'")) + == 1 + ) + # Inject a failure exactly between the INSERT message and saving its result. + fail_key = uuid.uuid4().hex + trigger = "audit_fail_" + uuid.uuid4().hex[:10] + fault_chat = client.call( + "POST", + "/api/v1/chats", + 201, + actor=alice, + body={"title": "Atomicity fault", "member_ids": [alice["id"]]}, + schema="Chat", + )["json"] + fault_path = "/api/v1/chats/" + fault_chat["id"] + "/messages" + try: + sql( + f"CREATE FUNCTION {trigger}() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'injected audit failure'; END $$; CREATE TRIGGER {trigger} BEFORE UPDATE ON message_requests FOR EACH ROW WHEN (NEW.key='{fail_key}' AND NEW.message_id IS NOT NULL) EXECUTE FUNCTION {trigger}();" + ) + client.call( + "POST", + fault_path, + 503, + actor=alice, + body={"text": "Retry after rollback"}, + headers={"Idempotency-Key": fail_key}, + ) + assert ( + int( + sql(f"SELECT count(*) FROM messages WHERE chat_id='{fault_chat['id']}'") + ) + == 0 + ) + assert ( + int( + sql( + f"SELECT count(*) FROM message_requests WHERE chat_id='{fault_chat['id']}'" + ) + ) + == 0 + ) + finally: + sql( + f"DROP TRIGGER IF EXISTS {trigger} ON message_requests; DROP FUNCTION IF EXISTS {trigger}();" + ) + client.message(alice, fault_chat, "Retry after rollback", key=fail_key) + assert ( + int(sql(f"SELECT count(*) FROM messages WHERE chat_id='{fault_chat['id']}'")) + == 1 + ) + # Corrupt-but-parseable cached values must not break the public API. + profile_key = "user:v1:" + alice["id"] + for field, value in [ + ("display_name", "bad\ud800"), + ("created_at", "0001-01-01T00:00:00+23:59"), + ("created_at", "2026-11-06T12:00:00"), + ]: + try: + redis( + "SET", + profile_key, + raw=json.dumps({**alice["user"], field: value}).encode(), + ) + assert ( + client.call("GET", "/api/v1/users/me", actor=alice, schema="User")[ + "json" + ] + == alice["user"] + ) + assert 0 < int(redis("TTL", profile_key)) <= 60 + finally: + redis("DEL", profile_key) + # Reuse one TCP connection: routing must not pin this user to one API. + conn = http.client.HTTPConnection( + urlsplit(os.environ.get("BASE_URL", "http://localhost:8080")).hostname, + urlsplit(os.environ.get("BASE_URL", "http://localhost:8080")).port or 80, + timeout=5, + ) + instances = set() + try: + for _ in range(8): + conn.request( + "GET", + "/api/v1/users/me", + headers={"Authorization": "Bearer " + alice["token"]}, + ) + r = conn.getresponse() + assert r.status == 200 + r.read() + instances.add(r.getheader("X-Instance-Id")) + finally: + conn.close() + assert len(instances) >= 2 + proof = { + "result": "PASS", + "checks": [ + "duplicate Idempotency-Key and Content-Type rejected", + "strict integer and repeated query parameters", + "NUL/emoji user and chat Unicode roundtrip", + "100 code point names and 4000 code point messages", + "no text trim or silent Unicode normalization", + "16 conflicting simultaneous requests: 8 replayed winners, 8 conflicts, one DB row", + "DB failure after message INSERT rolls back message and key; same key succeeds after recovery", + "invalid cached Unicode/timezone falls back to DB with TTL", + "one TCP connection reaches both API", + ], + "keepalive_instances": sorted(instances), + } + (ROOT / "evidence/lab3-edge-audit.json").write_text( + json.dumps(proof, ensure_ascii=False, indent=2) + "\n" + ) + print(json.dumps(proof, ensure_ascii=False, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/scripts/final-checks-lab3.py b/scripts/final-checks-lab3.py new file mode 100644 index 00000000..e2c36f14 --- /dev/null +++ b/scripts/final-checks-lab3.py @@ -0,0 +1,76 @@ +"""Финальные регрессии и безопасный снимок инфраструктуры без Config.Env.""" + +import json +import subprocess +import sys +from pathlib import Path + +from replicas import compose, containers, docker + +ROOT = Path(__file__).resolve().parents[1] +DEST = ROOT / "evidence" + + +def check(name, command, stdin=None): + result = subprocess.run(command, cwd=ROOT, input=stdin, text=True, capture_output=True) + (DEST / ("lab3-final-" + name + ".txt")).write_text(result.stdout + result.stderr) + print(name, "PASS" if result.returncode == 0 else "FAIL", flush=True) + if result.returncode: + raise RuntimeError((result.stdout + result.stderr)[-3000:]) + + +for name in ["smoke", "lab3", "regressions", "dependencies"]: + check(name, [sys.executable, "tests/" + name + ".py"]) +for name in [ + "database", + "passwords", + "settings", + "callbacks", + "idempotency_db", + "readiness", + "error_headers", +]: + check( + name, + ["docker", "exec", "-i", containers()[0], "python"], + (ROOT / "tests" / (name + ".py")).read_text(), + ) + +snapshot = {} +all_ids = [] +for service in ["api", "lb", "postgres", "redis", "migration"]: + ids = compose("ps", "--all", "-q", service).split() + snapshot[service] = [] + for cid in ids: + # Only explicitly selected infrastructure fields; credentials are excluded. + obj = json.loads(docker("inspect", "--format", "{{json .}}", cid)) + state = obj["State"] + record = { + "id": cid[:12], + "name": obj["Name"], + "image_id": obj["Image"], + "status": state["Status"], + "exit_code": state["ExitCode"], + "cpu_limit": obj["HostConfig"]["NanoCpus"] / 1e9, + "memory_limit_bytes": obj["HostConfig"]["Memory"], + "ports": obj["NetworkSettings"]["Ports"], + "read_only_root": obj["HostConfig"]["ReadonlyRootfs"], + } + snapshot[service].append(record) + assert record["cpu_limit"] > 0 and record["memory_limit_bytes"] > 0 + if service == "api": + assert not any(record["ports"].values()), "API port is published" + if state["Running"]: + all_ids.append(cid) +assert len(snapshot["api"]) == 2 +snapshot["post_test_usage"] = [ + json.loads(line) + for line in docker("stats", "--no-stream", "--format", "{{json .}}", *all_ids).splitlines() +] +snapshot["note"] = ( + "CPU/memory usage snapshot after tests, not a peak load measurement; cgroup CPU deltas under load are in research results." +) +(DEST / "lab3-infrastructure.json").write_text( + json.dumps(snapshot, ensure_ascii=False, indent=2) + "\n" +) +print("PASS: 2 API, internal ports, CPU/RAM limits on all containers", flush=True) diff --git a/scripts/haproxy.py b/scripts/haproxy.py new file mode 100644 index 00000000..8221befe --- /dev/null +++ b/scripts/haproxy.py @@ -0,0 +1,61 @@ +"""Runtime API доступен только на loopback внутри контейнера HAProxy.""" + +import csv +import io +import re +import subprocess +import time + +from replicas import ROOT, compose, docker + + +def runtime(command): + container = compose("ps", "-q", "lb") + return subprocess.check_output( + ["docker", "exec", "-i", container, "nc", "-w", "1", "127.0.0.1", "9999"], + input=command + "\n", + text=True, + cwd=ROOT, + timeout=5, + ).strip() + + +def drain(container): + address = docker( + "inspect", "--format", "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}", container + ) + lines = runtime("show servers state api_pool").splitlines() + header = next(line.lstrip("# ").split() for line in lines if line.startswith("#")) + rows = [ + dict(zip(header, line.split())) + for line in lines + if line and not line.startswith("#") and line != "1" + ] + names = [r["srv_name"] for r in rows if r.get("srv_addr") == address] + if not names or any(re.fullmatch(r"api[0-9]+", n) is None for n in names): + raise RuntimeError("Cannot map container to HAProxy backend") + for name in names: + result = runtime("set server api_pool/" + name + " state drain") + if result: + raise RuntimeError(result) + deadline = time.monotonic() + 10 + while True: + rows = list(csv.DictReader(io.StringIO(runtime("show stat").lstrip("# ")))) + active = sum( + int(r["scur"]) for r in rows if r["pxname"] == "api_pool" and r["svname"] in names + ) + if active == 0: + return names + if time.monotonic() > deadline: + raise RuntimeError("Backend did not drain within 10s") + time.sleep(0.2) + + +def release(names): + # Старый процесс уже остановлен. Слот возвращаем под контроль health checks, + # но не объявляем здоровым принудительно: новая реплика должна пройти ready. + for name in names: + for setting in ["health down", "state ready"]: + result = runtime("set server api_pool/" + name + " " + setting) + if result: + raise RuntimeError(result) diff --git a/scripts/plot-lab3.py b/scripts/plot-lab3.py new file mode 100644 index 00000000..6b75e5b3 --- /dev/null +++ b/scripts/plot-lab3.py @@ -0,0 +1,100 @@ +"""Графики только из сохранённых исходных результатов; значения не сглаживаются.""" + +import json +import os +import statistics +from collections import defaultdict +from pathlib import Path + +os.environ.setdefault("MPLCONFIGDIR", "/tmp/mtusi-lab3-matplotlib") +os.environ.setdefault("XDG_CACHE_HOME", "/tmp/mtusi-lab3-cache") +import matplotlib + +matplotlib.use("Agg") +import matplotlib.pyplot as plt + +ROOT = Path(__file__).resolve().parents[1] +DEST = ROOT / "evidence/lab3-research" +rows = json.loads((DEST / "results.json").read_text()) +groups = defaultdict(list) +for row in rows: + job = row["job"] + groups[(job["series"], job["replicas"], job["concurrency"])].append(row) +plt.rcParams.update( + { + "font.family": "DejaVu Sans", + "font.size": 11, + "axes.spines.top": False, + "axes.spines.right": False, + "figure.dpi": 140, + } +) +colors = {"fixed": "#315de0", "growing": "#139879"} +fig, axes = plt.subplots(1, 2, figsize=(11, 4.3), layout="constrained") +for series, label in [ + ("fixed", "Постоянные 2 CPU / 1 GiB на все API"), + ("growing", "1 CPU / 256 MiB на каждую API"), +]: + values = [[r["report"]["rps"] for r in groups[(series, n, 32)]] for n in [1, 2, 4]] + axes[0].errorbar( + [1, 2, 4], + [statistics.mean(v) for v in values], + yerr=[statistics.stdev(v) for v in values], + marker="o", + capsize=5, + label=label, + color=colors[series], + ) + latency = [ + [r["report"]["all_attempt_latency_ms"]["p95"] for r in groups[(series, n, 32)]] + for n in [1, 2, 4] + ] + axes[1].plot( + [1, 2, 4], + [statistics.mean(v) for v in latency], + marker="o", + color=colors[series], + label=label, + ) +for ax in axes: + ax.set_xlabel("Количество API-реплик") + ax.set_xticks([1, 2, 4]) + ax.grid(alpha=0.2) +axes[0].set_ylabel("Успешных запросов / с") +axes[0].set_title("Масштабирование: среднее ± SD, 2 повтора") +axes[1].set_ylabel("p95 всех попыток, мс") +axes[1].set_title("Задержка при 32 клиентах") +axes[0].legend(fontsize=8, loc="best") +fig.suptitle("PostgreSQL: 0.25 CPU; профиль 80% POST / 20% GET", fontsize=12) +fig.savefig(DEST / "scaling.png") +fig.savefig(DEST / "scaling.svg") +plt.close(fig) + +fig, axes = plt.subplots(1, 2, figsize=(11, 4.2), layout="constrained") +concurrencies = [8, 32, 64] +measurements = [ + groups[("growing", 4, 32)] if c == 32 else groups[("saturation", 4, c)] for c in concurrencies +] +axes[0].plot( + concurrencies, + [statistics.mean(r["report"]["rps"] for r in rs) for rs in measurements], + marker="o", + color="#315de0", +) +axes[0].set_xlabel("Параллельные клиенты") +axes[0].set_ylabel("Успешных запросов / с") +axes[0].set_title("4 API: рост очереди не даёт роста RPS") +axes[0].grid(alpha=0.2) +base = groups[("growing", 4, 32)] +improved = groups[("improved-db", 4, 32)] +means = [statistics.mean(r["report"]["rps"] for r in rs) for rs in [base, improved]] +axes[1].bar(["БД: 0.25 CPU", "БД: 1 CPU"], means, color=["#778392", "#139879"], width=0.55) +for i, m in enumerate(means): + axes[1].text(i, m + max(means) * 0.02, f"{m:.1f}", ha="center") +axes[1].set_ylim(0, max(means) * 1.18) +axes[1].set_ylabel("Успешных запросов / с") +axes[1].set_title("Проверка улучшения: 4 API, 32 клиента") +fig.savefig(DEST / "bottleneck.png") +fig.savefig(DEST / "bottleneck.svg") +plt.close(fig) +print("Saved scaling.png/svg and bottleneck.png/svg") diff --git a/scripts/replicas.py b/scripts/replicas.py new file mode 100644 index 00000000..0bc932e8 --- /dev/null +++ b/scripts/replicas.py @@ -0,0 +1,60 @@ +"""Управление только API-контейнерами текущего Compose-проекта.""" + +import json +import os +import subprocess +import time +import urllib.request +from pathlib import Path + +from runtime_config import base_url + +os.environ.setdefault("BASE_URL", base_url()) + +ROOT = Path(__file__).resolve().parents[1] + + +def docker(*args): + return subprocess.check_output(["docker", *args], cwd=ROOT, text=True).strip() + + +def compose(*args): + return docker("compose", "--env-file", os.environ.get("ENV_FILE", ".env"), *args) + + +def containers(): + return compose("ps", "--all", "-q", "api").split() + + +def describe(container): + # Никогда не выводим Config.Env: там credentials. + return json.loads(docker("inspect", "--format", "{{json .State}}", container)) + + +def instance_id(container): + code = "import urllib.request; r=urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2); print(r.headers['X-Instance-Id'])" + return docker("exec", container, "python", "-c", code) + + +def wait_ready(container, timeout=60): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + state = describe(container) + if state.get("Health", {}).get("Status") == "healthy": + return instance_id(container) + time.sleep(0.5) + raise RuntimeError("API container did not become healthy: " + container[:12]) + + +def wait_visible(instance, timeout=30): + base = os.environ.get("BASE_URL", "http://localhost:8080").rstrip("/") + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + try: + with urllib.request.urlopen(base + "/health/ready", timeout=2) as response: + if response.headers.get("X-Instance-Id") == instance: + return + except OSError: + pass + time.sleep(0.1) + raise RuntimeError("New replica was not discovered by HAProxy") diff --git a/scripts/research-lab3.py b/scripts/research-lab3.py new file mode 100644 index 00000000..a2add2ab --- /dev/null +++ b/scripts/research-lab3.py @@ -0,0 +1,294 @@ +"""Трек 1: фиксированные/растущие ресурсы, насыщение PostgreSQL, улучшение. +Учебные данные добавляются; volumes не удаляются. По завершении восстанавливает +обычную конфигурацию. Сводки не содержат сессионных токенов. +""" + +import argparse +import csv +import hashlib +import json +import os +import platform +import random +import subprocess +import time +from pathlib import Path + +from benchmark import fixture, measure +from replicas import compose, containers, docker, wait_ready, wait_visible + +ROOT = Path(__file__).resolve().parents[1] +DEST = ROOT / "evidence/lab3-research" + + +def cpu_stats(): + result = {} + for service in ["api", "postgres", "redis", "lb"]: + for cid in compose("ps", "-q", service).split(): + raw = docker("exec", cid, "cat", "/sys/fs/cgroup/cpu.stat") + memory = docker( + "exec", cid, "cat", "/sys/fs/cgroup/memory.current", "/sys/fs/cgroup/memory.peak" + ).split() + result[cid] = { + "service": service, + "memory_current_bytes": int(memory[0]), + "memory_peak_bytes": int(memory[1]), + **{line.split()[0]: int(line.split()[1]) for line in raw.splitlines()}, + } + return result + + +def pg_stats(): + # Не выводим env/password и не меняем данные. + query = "SELECT json_build_object('commits',xact_commit,'rollbacks',xact_rollback,'blocks_read',blks_read,'blocks_hit',blks_hit,'temp_bytes',temp_bytes) FROM pg_stat_database WHERE datname=current_database()" + result = subprocess.check_output( + [ + "docker", + "compose", + "--env-file", + os.environ.get("ENV_FILE", ".env"), + "exec", + "-T", + "postgres", + "sh", + "-c", + 'exec psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -At', + ], + input=query, + text=True, + cwd=ROOT, + ) + return json.loads(result) + + +def configure(replicas, cpus, memory, db_cpus, pool=10): + os.environ.update( + API_REPLICAS=str(replicas), + API_CPUS=str(cpus), + API_MEMORY=memory, + POSTGRES_CPUS=str(db_cpus), + DATABASE_POOL_SIZE=str(pool), + LOG_LEVEL="WARNING", + ) + compose("up", "-d", "--no-build", "--wait") + ids = containers() + assert len(ids) == replicas + for cid in ids: + wait_visible(wait_ready(cid)) + + +def main(): + global DEST + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "--full", + action="store_true", + help="Полная матрица 1/2/4; без флага короткая проверка ресурса БД", + ) + parser.add_argument("--duration", type=int, default=12) + parser.add_argument("--repeats", type=int, default=2) + parser.add_argument("--concurrency", type=int, default=32) + args = parser.parse_args() + if not args.full: + DEST = ROOT / "evidence/lab3-control" + args.duration = min(args.duration, 8) + args.repeats = 1 + DEST.mkdir(parents=True, exist_ok=True) + machine = { + "host": platform.platform(), + "architecture": platform.machine(), + "docker": json.loads(docker("info", "--format", "{{json .}}")), + } + # Keep only hardware/runtime fields. Docker Info also has registry/user info. + machine["docker"] = { + k: machine["docker"].get(k) + for k in [ + "ServerVersion", + "NCPU", + "MemTotal", + "KernelVersion", + "OperatingSystem", + "Architecture", + ] + } + machine["application"] = { + "schema_revision": "006", + "message_storage": "UTF-8 bytea", + "source_sha256": { + str(path.relative_to(ROOT)): hashlib.sha256(path.read_bytes()).hexdigest() + for path in sorted((ROOT / "src").rglob("*.py")) + }, + "configuration_sha256": { + name: hashlib.sha256((ROOT / name).read_bytes()).hexdigest() + for name in ["compose.yaml", "infra/haproxy.cfg", "scripts/benchmark.py"] + }, + } + machine["method"] = { + "warmup_seconds": 3, + "repeats": args.repeats, + "duration_seconds": args.duration, + "concurrency": args.concurrency, + "profile": "80% POST unique keys, 20% GET first page limit20, 20 seed messages; fresh chat per run", + "generator": "Python http.client, one persistent connection per thread, closed-loop, no retries", + "limits": "PostgreSQL 0.25 CPU/512MiB baseline, LB 0.5CPU/128MiB, Redis 0.25CPU/128MiB", + "randomization_seed": 20261106, + } + (DEST / "method.json").write_text(json.dumps(machine, ensure_ascii=False, indent=2) + "\n") + jobs = [] + for repeat in range(args.repeats): + for count in [1, 2, 4]: + jobs += [ + dict( + series="fixed", + replicas=count, + cpus=2 / count, + memory=f"{1024 // count}m", + db=0.25, + concurrency=args.concurrency, + repeat=repeat, + ), + dict( + series="growing", + replicas=count, + cpus=1, + memory="256m", + db=0.25, + concurrency=args.concurrency, + repeat=repeat, + ), + ] + random.Random(20261106).shuffle(jobs) + # Additional load sweep establishes a plateau; the final series tests a + # hypothesis that allocating more CPU to PostgreSQL raises throughput. + for repeat in range(args.repeats): + for concurrency in [8, 64]: + jobs.append( + dict( + series="saturation", + replicas=4, + cpus=1, + memory="256m", + db=0.25, + concurrency=concurrency, + repeat=repeat, + ) + ) + jobs.append( + dict( + series="improved-db", + replicas=4, + cpus=1, + memory="256m", + db=1, + concurrency=args.concurrency, + repeat=repeat, + ) + ) + if not args.full: + jobs = [ + job + for job in jobs + if job["replicas"] == 4 + and job["series"] in {"growing", "improved-db"} + and job["concurrency"] == args.concurrency + ] + results = [] + try: + for index, job in enumerate(jobs): + print(f"[{index + 1}/{len(jobs)}] {job}", flush=True) + configure(job["replicas"], job["cpus"], job["memory"], job["db"]) + actor, chat = fixture() + measure(actor, chat, 3, job["concurrency"]) + before = cpu_stats() + pg_before = pg_stats() + began = time.monotonic() + report, _ = measure(actor, chat, args.duration, job["concurrency"]) + elapsed = time.monotonic() - began + pg_after = pg_stats() + after = cpu_stats() + cpu = {} + for cid, end in after.items(): + if cid not in before: + continue + delta = {k: end[k] - before[cid][k] for k in end if k != "service"} + cpu[cid[:12]] = { + "service": end["service"], + "memory_current_mib": round(end["memory_current_bytes"] / 1048576, 2), + "memory_peak_lifetime_mib": round(end["memory_peak_bytes"] / 1048576, 2), + "cpu_cores_used": round(delta["usage_usec"] / 1e6 / elapsed, 3), + "throttled_seconds": round(delta.get("throttled_usec", 0) / 1e6, 3), + "throttled_period_ratio": round( + delta.get("nr_throttled", 0) / max(1, delta.get("nr_periods", 0)), 3 + ), + } + row = { + "job": job, + "report": report, + "cpu": cpu, + "postgres_delta": {k: pg_after[k] - pg_before[k] for k in pg_before}, + } + results.append(row) + (DEST / f"run-{index + 1:02}.json").write_text( + json.dumps(row, ensure_ascii=False, indent=2) + "\n" + ) + print( + "RPS", + report["rps"], + "errors", + report["errors"], + "p95", + report["all_attempt_latency_ms"]["p95"], + flush=True, + ) + (DEST / "results.json").write_text(json.dumps(results, ensure_ascii=False, indent=2) + "\n") + with (DEST / "summary.csv").open("w") as f: + fields = [ + "series", + "replicas", + "cpus", + "memory", + "db", + "concurrency", + "repeat", + "rps", + "p95_ms", + "errors", + "postgres_cores", + ] + writer = csv.DictWriter(f, fieldnames=fields) + writer.writeheader() + for row in results: + writer.writerow( + { + **row["job"], + "rps": row["report"]["rps"], + "p95_ms": row["report"]["all_attempt_latency_ms"]["p95"], + "errors": sum(row["report"]["errors"].values()), + "postgres_cores": sum( + c["cpu_cores_used"] + for c in row["cpu"].values() + if c["service"] == "postgres" + ), + } + ) + print( + "PASS: research series complete; inspect results before drawing conclusions", flush=True + ) + finally: + for key in [ + "API_REPLICAS", + "API_CPUS", + "API_MEMORY", + "POSTGRES_CPUS", + "DATABASE_POOL_SIZE", + "LOG_LEVEL", + ]: + os.environ.pop(key, None) + compose("up", "-d", "--no-build", "--wait") + for cid in containers(): + wait_visible(wait_ready(cid)) + print("Default two-replica configuration restored", flush=True) + + +if __name__ == "__main__": + main() diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..18fb6abf --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,6 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +# --volumes не используется: PostgreSQL и сессии должны сохраниться. +docker compose --env-file "${ENV_FILE:-.env}" restart postgres redis +docker compose --env-file "${ENV_FILE:-.env}" up -d --wait --force-recreate api diff --git a/scripts/review-lab3.py b/scripts/review-lab3.py new file mode 100644 index 00000000..ad6ebee9 --- /dev/null +++ b/scripts/review-lab3.py @@ -0,0 +1,42 @@ +"""Полная проверка перед сдачей; инфраструктурные этапы строго последовательно.""" + +import argparse +import json +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +parser = argparse.ArgumentParser(description=__doc__) +parser.add_argument( + "--research", action="store_true", help="Повторить 18 измерений и построить графики" +) +args = parser.parse_args() +steps = [ + ("audit-run", "scripts/audit-lab3.py"), + ("edge-audit", "scripts/edge-audit-lab3.py"), + ("verify", "scripts/verify-lab3.py"), + ("final-checks", "scripts/final-checks-lab3.py"), + ("clean-start", "scripts/test-clean-lab3.py"), +] +if args.research: + steps += [ + ("research-run", "scripts/research-lab3.py"), + ("research-plots", "scripts/plot-lab3.py"), + ] +summary = {} +for name, script in steps: + print("Starting " + name, flush=True) + with (ROOT / "evidence" / ("lab3-" + name + ".txt")).open("w") as output: + result = subprocess.run( + [sys.executable, script] + (["--full"] if name == "research-run" else []), + cwd=ROOT, + stdout=output, + stderr=subprocess.STDOUT, + ) + summary[name] = "PASS" if result.returncode == 0 else "FAIL" + (ROOT / "evidence/lab3-review-summary.json").write_text(json.dumps(summary, indent=2) + "\n") + print(name + " " + summary[name], flush=True) + if result.returncode: + raise SystemExit(result.returncode) +print("PASS: complete pre-submission review", flush=True) diff --git a/scripts/rolling-update.py b/scripts/rolling-update.py new file mode 100644 index 00000000..affb9dd3 --- /dev/null +++ b/scripts/rolling-update.py @@ -0,0 +1,43 @@ +"""Compose rolling replacement с одной временной репликой и ожиданием LB.""" + +import json +import time + +from haproxy import drain, release +from replicas import compose, containers, describe, docker, wait_ready, wait_visible + +old = [c for c in containers() if describe(c)["Running"]] +if len(old) < 2 or len(old) != len(containers()): + raise SystemExit("Restore all API replicas first; need >=2") +count = len(old) +events = [] +# Build separately before starting this script. --no-recreate keeps old instances +# alive even when the image has changed. Each new container uses the current image. +for index, previous in enumerate(old): + before = set(containers()) + compose("up", "-d", "--no-deps", "--no-recreate", "--scale", f"api={count + 1}", "api") + added = set(containers()) - before + assert len(added) == 1, "Expected one surge replica" + replacement = added.pop() + new_instance = wait_ready(replacement) + wait_visible(new_instance) + drained_slots = drain(previous) + started = time.monotonic() + docker("stop", "--time", "10", previous) + state = describe(previous) + assert state["ExitCode"] == 0, "API did not exit gracefully" + docker("rm", previous) + release(drained_slots) + event = { + "old_container": previous[:12], + "new_container": replacement[:12], + "new_instance": new_instance, + "old_exit_code": state["ExitCode"], + "drained_slots": drained_slots, + "stop_seconds": round(time.monotonic() - started, 3), + } + events.append(event) +assert len(containers()) == count +print( + json.dumps({"mode": "rolling replacement, one surge, same schema", "events": events}, indent=2) +) diff --git a/scripts/runtime_config.py b/scripts/runtime_config.py new file mode 100644 index 00000000..386db3dd --- /dev/null +++ b/scripts/runtime_config.py @@ -0,0 +1,30 @@ +"""Внешний адрес локального стенда: BASE_URL → APP_PORT → выбранный .env.""" + +import os +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] + + +def base_url() -> str: + if os.environ.get("BASE_URL"): + return os.environ["BASE_URL"].rstrip("/") + port = os.environ.get("APP_PORT") + if not port: + path = Path(os.environ.get("ENV_FILE", str(ROOT / ".env"))) + if not path.is_absolute(): + path = ROOT / path + if path.is_file(): + for line in path.read_text().splitlines(): + key, separator, value = line.partition("=") + if separator and key.strip() == "APP_PORT": + port = value.split("#", 1)[0].strip().strip("\"'") + break + port = port or "8080" + if not port.isascii() or not port.isdigit() or not 1 <= int(port) <= 65535: + raise ValueError("APP_PORT должен быть номером порта от 1 до 65535") + return "http://localhost:" + str(int(port)) + + +if __name__ == "__main__": + print(base_url()) diff --git a/scripts/stop-one.py b/scripts/stop-one.py new file mode 100644 index 00000000..0430d67c --- /dev/null +++ b/scripts/stop-one.py @@ -0,0 +1,34 @@ +"""Сопоставить X-Instance-Id реальному контейнеру и оставить его остановленным.""" + +import json +import os +import time + +from replicas import containers, describe, docker, instance_id + +target = os.environ["TARGET_INSTANCE_ID"] +running = [c for c in containers() if describe(c)["Running"]] +if len(running) < 2: + raise SystemExit("Need at least two running API containers") +for container in running: + if instance_id(container) == target: + before = describe(container) + started = time.monotonic() + docker("stop", "--time", "10", container) + after = describe(container) + assert not after["Running"] + print( + json.dumps( + { + "container": container[:12], + "instance_id": target, + "started_at": before["StartedAt"], + "finished_at": after["FinishedAt"], + "stop_seconds": round(time.monotonic() - started, 3), + "exit_code": after["ExitCode"], + } + ) + ) + break +else: + raise SystemExit("Requested instance not found in this Compose project") diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..383f91ed --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +exec "${PYTHON:-python3}" scripts/stop-one.py diff --git a/scripts/test-clean-lab3.py b/scripts/test-clean-lab3.py new file mode 100644 index 00000000..d2321639 --- /dev/null +++ b/scripts/test-clean-lab3.py @@ -0,0 +1,65 @@ +"""Запуск на пустых volumes в отдельном временном Compose-проекте. +Текущий стенд и его volumes не удаляются. Секрет создаётся в TemporaryDirectory. +""" + +import json +import os +import secrets +import socket +import subprocess +import sys +import uuid +from pathlib import Path +from tempfile import TemporaryDirectory + +ROOT = Path(__file__).resolve().parents[1] +project = "lab3-audit-clean-" + uuid.uuid4().hex[:8] +with socket.socket() as sock: + sock.bind(("127.0.0.1", 0)) + port = sock.getsockname()[1] +with TemporaryDirectory(prefix="lab3-clean-") as folder: + env_file = Path(folder) / ".env" + env_file.write_text( + "POSTGRES_PASSWORD=" + secrets.token_hex(24) + "\nAPP_PORT=" + str(port) + "\n" + ) + env = { + **os.environ, + "COMPOSE_PROJECT_NAME": project, + "ENV_FILE": str(env_file), + "APP_PORT": str(port), + } + command = ["docker", "compose", "--env-file", str(env_file)] + + def run(*args): + return subprocess.run(args, cwd=ROOT, env=env, text=True, check=True, capture_output=True) + + try: + run(*command, "up", "-d", "--no-build", "--wait") + version = run( + *command, + "exec", + "-T", + "postgres", + "sh", + "-c", + 'exec psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -Atc "SELECT version_num FROM alembic_version"', + ).stdout.strip() + assert version == "006", version + result = run(sys.executable, "tests/smoke.py", "--base-url", f"http://127.0.0.1:{port}") + proof = { + "result": "PASS", + "project": project, + "schema_revision": version, + "public_smoke": result.stdout.strip(), + "empty_volumes": True, + "temporary_project_removed": True, + } + except subprocess.CalledProcessError as error: + print((error.stdout + error.stderr)[-3000:]) + raise + finally: + run(*command, "down", "--volumes") + (ROOT / "evidence/lab3-clean-start.json").write_text( + json.dumps(proof, ensure_ascii=False, indent=2) + "\n" + ) + print(json.dumps(proof, ensure_ascii=False, indent=2)) diff --git a/scripts/test-expiry.sh b/scripts/test-expiry.sh new file mode 100755 index 00000000..1c479ec8 --- /dev/null +++ b/scripts/test-expiry.sh @@ -0,0 +1,8 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +# Сохраняет обычный TTL и восстанавливает API даже при падении проверки. +restore() { docker compose up -d --wait --force-recreate api; } +trap restore EXIT +SESSION_TTL_SECONDS=2 docker compose up -d --wait --force-recreate api +"${PYTHON:-python3}" tests/expiry.py diff --git a/scripts/test-persistence.py b/scripts/test-persistence.py new file mode 100644 index 00000000..cafd8ec8 --- /dev/null +++ b/scripts/test-persistence.py @@ -0,0 +1,69 @@ +"""Исходный persistence-сценарий плюс доказательство рестарта всех трёх сервисов.""" + +import argparse +import json +from pathlib import Path +from runtime_config import base_url +import subprocess +import sys + +ROOT = Path(__file__).resolve().parents[1] + + +def states(): + result = {} + for service in ("api", "postgres", "redis"): + ids = subprocess.check_output( + ["docker", "compose", "ps", "-q", service], cwd=ROOT, text=True + ).split() + if not ids: + raise RuntimeError("Не запущен сервис " + service) + result[service] = {} + for container in ids: + state = json.loads( + subprocess.check_output( + ["docker", "inspect", "--format", "{{json .State}}", container], + cwd=ROOT, + text=True, + ) + ) + result[service][container] = { + "StartedAt": state["StartedAt"], + "Pid": state["Pid"], + } + return result + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--base-url", default=base_url()) + parser.add_argument("--action-script", required=True) + parser.add_argument("--ca-file") + args = parser.parse_args() + before = states() + print("ДО: " + json.dumps(before), flush=True) + command = [ + sys.executable, + str(ROOT / "tests/resilience.py"), + "--mode", + "persistence", + "--action-script", + args.action_script, + "--base-url", + args.base_url, + ] + if args.ca_file: + command += ["--ca-file", args.ca_file] + subprocess.run(command, cwd=ROOT, check=True) + after = states() + print("ПОСЛЕ: " + json.dumps(after), flush=True) + for service in before: + if before[service] == after[service]: + raise AssertionError("Сервис не перезапущен: " + service) + print( + "PASS: StartedAt изменился у API, PostgreSQL и Redis; прежний токен и данные сохранены" + ) + + +if __name__ == "__main__": + main() diff --git a/scripts/verify-lab3.py b/scripts/verify-lab3.py new file mode 100644 index 00000000..66cd4395 --- /dev/null +++ b/scripts/verify-lab3.py @@ -0,0 +1,135 @@ +"""Полная проверка lab3. Использует текущий учебный стенд, volumes сохраняет.""" + +import json +import subprocess +import sys +import time +from concurrent.futures import ThreadPoolExecutor +from pathlib import Path + +from benchmark import Client, fixture, measure +from replicas import compose, containers, describe, docker, instance_id, wait_ready, wait_visible + +ROOT = Path(__file__).resolve().parents[1] +EVIDENCE = ROOT / "evidence" + + +def run_test(name, *args, stdin=None): + result = subprocess.run(args, cwd=ROOT, input=stdin, text=True, capture_output=True) + (EVIDENCE / name).write_text(result.stdout + result.stderr) + print(name, "PASS" if result.returncode == 0 else "FAIL", flush=True) + if result.returncode: + raise RuntimeError((result.stdout + result.stderr)[-3000:]) + + +def main(): + py = sys.executable + for filename in ["smoke", "lab3", "extra", "regressions", "production"]: + run_test("lab3-" + filename + ".txt", py, "tests/" + filename + ".py") + run_test( + "lab3-db-idempotency.txt", + "docker", + "exec", + "-i", + containers()[0], + "python", + stdin=(ROOT / "tests/idempotency_db.py").read_text(), + ) + run_test( + "lab3-public-failover.txt", + py, + "tests/resilience.py", + "--mode", + "failover", + "--action-script", + "scripts/stop-one.sh", + ) + compose("up", "-d", "--no-deps", "--wait", "api") + for c in containers(): + wait_visible(wait_ready(c)) + events = [] + actor, chat = fixture() + initial = containers() + for target in initial: + alias = instance_id(target) + print("Failure test", target[:12], alias, flush=True) + with ThreadPoolExecutor(max_workers=1) as pool: + event = [] + load = pool.submit(measure, actor, chat, 12, 8, event) + time.sleep(3) + event.append(time.monotonic()) + docker("kill", "--signal", "KILL", target) + report, confirmations = load.result() + assert not describe(target)["Running"] + assert report["successes"].get("read") and report["successes"].get("write") + assert all(report["seconds"][b]["success"] > 0 for b in range(5, 11)), ( + "No service after detection" + ) + c = Client() + # Each acknowledged message is read back with the same idempotency key. + for key, mid in confirmations: + response = c.message(actor, chat, "load-" + key, key=key) + assert response["json"]["id"] == mid + assert response["headers"]["X-Instance-Id"] != alias + report.update( + { + "stopped_container": target[:12], + "stopped_instance": alias, + "exit_code": describe(target)["ExitCode"], + "all_acknowledged_messages_replayed": len(confirmations), + "failure_started_at_load_second": 3, + "service_after_second_5": "reads/writes continue", + "note": "hard SIGKILL, no automatic retries in load, replay confirmations after failure", + } + ) + events.append(report) + docker("start", target) + wait_visible(wait_ready(target)) + (EVIDENCE / "lab3-failures.json").write_text( + json.dumps(events, ensure_ascii=False, indent=2) + "\n" + ) + print("Both replicas survived sequential hard-failure tests", flush=True) + actor, chat = fixture() + with ThreadPoolExecutor(max_workers=1) as pool: + load = pool.submit(measure, actor, chat, 40, 8) + time.sleep(3) + run_test("lab3-rolling.txt", py, "scripts/rolling-update.py") + report, confirmations = load.result() + (EVIDENCE / "lab3-rolling-load.json").write_text(json.dumps(report, indent=2) + "\n") + assert report["successes"].get("read") and report["successes"].get("write") + # Zero errors are not required by rubric; any transitional errors are reported. + client = Client() + for key, mid in confirmations: + assert client.message(actor, chat, "load-" + key, key=key)["json"]["id"] == mid + before = {c: describe(c)["StartedAt"] for c in containers()} + storage_before = { + s: describe(compose("ps", "-q", s))["StartedAt"] for s in ["postgres", "redis"] + } + run_test( + "lab3-persistence.txt", + py, + "tests/resilience.py", + "--mode", + "persistence", + "--action-script", + "scripts/restart.sh", + ) + after = {c: describe(c)["StartedAt"] for c in containers()} + storage_after = { + s: describe(compose("ps", "-q", s))["StartedAt"] for s in ["postgres", "redis"] + } + assert set(before) != set(after) or before != after + assert all(storage_before[s] != storage_after[s] for s in storage_before) + proof = { + "api_before": before, + "api_after": after, + "storage_before": storage_before, + "storage_after": storage_after, + } + (EVIDENCE / "lab3-restart-proof.json").write_text(json.dumps(proof, indent=2) + "\n") + run_test("lab3-final-smoke.txt", py, "tests/smoke.py") + print("PASS: all lab3 acceptance checks", flush=True) + + +if __name__ == "__main__": + main() diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/__main__.py b/src/__main__.py new file mode 100644 index 00000000..da5d2b9b --- /dev/null +++ b/src/__main__.py @@ -0,0 +1,13 @@ +import uvicorn + +from src.core.settings import settings + +if __name__ == "__main__": + uvicorn.run( + "src.main:app", + host=settings.app_host, + port=settings.app_port, + workers=settings.app_workers, + log_level=settings.log_level.lower(), + timeout_graceful_shutdown=settings.shutdown_timeout_seconds, + ) diff --git a/src/api/__init__.py b/src/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/api/dependencies.py b/src/api/dependencies.py new file mode 100644 index 00000000..d04ea93d --- /dev/null +++ b/src/api/dependencies.py @@ -0,0 +1,106 @@ +import base64 +import binascii +import re +from typing import Annotated + +from fastapi import Depends, Request +from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer +from fastapi.security.http import HTTPBase, HTTPBasicCredentials +from sqlalchemy.ext.asyncio import AsyncSession + +from src.core.db.session import get_db_session +from src.core.errors import ApiError +from src.core.settings import settings +from src.service.auth import AuthService +from src.service.chat import ChatService +from src.service.dto import UserDTO +from src.service.exceptions import InvalidSessionException +from src.service.message import MessageService +from src.service.user import UserService + +bearer_scheme = HTTPBearer( + scheme_name="bearerAuth", + description="Вставьте токен из ответа на вход, без префикса Bearer.", + auto_error=False, +) +basic_scheme = HTTPBase(scheme="basic", scheme_name="basicAuth", auto_error=False) + + +async def get_basic_credentials( + request: Request, + credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(basic_scheme)], +) -> HTTPBasicCredentials: + if await request.body(): + raise ApiError(400, "invalid_request", "Вход не принимает тело запроса") + try: + authorization = request.headers.getlist("authorization") + if len(authorization) != 1 or len(authorization[0]) > settings.authorization_max_length: + raise ValueError + if credentials is None or credentials.scheme.lower() != "basic": + raise ValueError + decoded = base64.b64decode(credentials.credentials, validate=True).decode("utf-8") + username, separator, password = decoded.partition(":") + if ( + not separator + or re.fullmatch(r"[a-z0-9_]{3,32}", username) is None + or not 12 <= len(password) <= 128 + or not password.strip() + ): + raise ValueError + except (ValueError, UnicodeError, binascii.Error): + raise ApiError(401, "unauthorized", "Неверный логин или пароль") from None + return HTTPBasicCredentials(username=username, password=password) + + +def get_user_service( + db_session: Annotated[AsyncSession, Depends(get_db_session, scope="function")], +) -> UserService: + return UserService(db_session=db_session) + + +def get_chat_service( + db_session: Annotated[AsyncSession, Depends(get_db_session, scope="function")], +) -> ChatService: + return ChatService(db_session=db_session) + + +def get_message_service( + db_session: Annotated[AsyncSession, Depends(get_db_session, scope="function")], +) -> MessageService: + return MessageService(db_session=db_session) + + +def get_auth_service( + db_session: Annotated[AsyncSession, Depends(get_db_session, scope="function")], +) -> AuthService: + return AuthService( + db_session=db_session, + session_ttl_seconds=settings.session_ttl_seconds, + ) + + +def get_bearer_token( + request: Request, + credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(bearer_scheme)], +) -> str: + if credentials is None or len(request.headers.getlist("authorization")) != 1: + raise ApiError(401, "unauthorized", "Требуется Bearer-токен") + token = credentials.credentials + if ( + credentials.scheme.lower() != "bearer" + or not token + or len(token) > 512 + or any(c.isspace() for c in token) + ): + raise ApiError(401, "unauthorized", "Требуется Bearer-токен") + return token + + +async def get_current_user( + auth: Annotated[AuthService, Depends(get_auth_service)], + token: Annotated[str, Depends(get_bearer_token)], +) -> UserDTO: + try: + return await auth.get_user(token=token) + except InvalidSessionException as error: + raise ApiError(401, "unauthorized", str(error)) from error diff --git a/src/api/router.py b/src/api/router.py new file mode 100644 index 00000000..553eedc5 --- /dev/null +++ b/src/api/router.py @@ -0,0 +1,14 @@ +from fastapi import APIRouter + +from src.api.v1.routers.auth.router import router as auth_router +from src.api.v1.routers.chats.router import router as chats_router +from src.api.v1.routers.messages.router import router as messages_router +from src.api.v1.routers.users.router import router as users_router + +router = APIRouter(prefix="/api/v1", tags=["Версия 1"]) + +router.include_router(users_router) +router.include_router(chats_router) +router.include_router(messages_router) + +router.include_router(auth_router) diff --git a/src/api/v1/__init__.py b/src/api/v1/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/api/v1/routers/__init__.py b/src/api/v1/routers/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/api/v1/routers/auth/__init__.py b/src/api/v1/routers/auth/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/api/v1/routers/auth/callback.py b/src/api/v1/routers/auth/callback.py new file mode 100644 index 00000000..45334b8b --- /dev/null +++ b/src/api/v1/routers/auth/callback.py @@ -0,0 +1,31 @@ +import logging + +from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker + +from src.core.db.repo.auth import fetch_user_credentials +from src.core.security import verify_password_async +from src.core.settings import settings +from src.service.user import UserService + +logger = logging.getLogger(__name__) + + +async def create_admin(session_factory: async_sessionmaker[AsyncSession]) -> None: + async with session_factory.begin() as session: + admin = await UserService(db_session=session).create_admin( + username=settings.admin_username, + display_name=settings.admin_display_name, + password=settings.admin_password.get_secret_value(), + ) + if admin is None and settings.app_env == "production": + credentials = await fetch_user_credentials(session, settings.admin_username) + if credentials is None or not await verify_password_async( + settings.admin_password.get_secret_value(), credentials.password_hash + ): + raise RuntimeError( + "Пароль существующего администратора не совпадает с настройкой production" + ) + if admin is None: + logger.info("Администратор уже существует") + else: + logger.info("Администратор создан: пользователь=%s", admin.id) diff --git a/src/api/v1/routers/auth/router.py b/src/api/v1/routers/auth/router.py new file mode 100644 index 00000000..49076ecd --- /dev/null +++ b/src/api/v1/routers/auth/router.py @@ -0,0 +1,50 @@ +from dataclasses import asdict +from typing import Annotated + +from fastapi import APIRouter, Depends, Response +from fastapi.security import HTTPBasicCredentials + +from src.api.dependencies import ( + get_auth_service, + get_basic_credentials, + get_bearer_token, + get_current_user, +) +from src.api.v1.routers.auth.schemas import SessionOut +from src.api.v1.routers.users.schemas import UserOut +from src.core.errors import ApiError +from src.service.auth import AuthService +from src.service.dto import UserDTO +from src.service.exceptions import InvalidCredentialsException + +router = APIRouter(prefix="/auth/sessions", tags=["Сессии"]) + + +@router.post("", status_code=201, response_model=SessionOut) +async def login( + credentials: Annotated[HTTPBasicCredentials, Depends(get_basic_credentials)], + auth: Annotated[AuthService, Depends(get_auth_service)], +) -> SessionOut: + try: + result = await auth.login( + username=credentials.username, + password=credentials.password, + ) + except InvalidCredentialsException as error: + raise ApiError(401, "unauthorized", str(error)) from error + return SessionOut( + token=result.token, + token_type=result.token_type, + expires_at=result.expires_at, + user=UserOut(**asdict(result.user)), + ) + + +@router.delete("/current", status_code=204) +async def logout( + current_user: Annotated[UserDTO, Depends(get_current_user)], + token: Annotated[str, Depends(get_bearer_token)], + auth: Annotated[AuthService, Depends(get_auth_service)], +) -> Response: + await auth.logout(token=token) + return Response(status_code=204) diff --git a/src/api/v1/routers/auth/schemas.py b/src/api/v1/routers/auth/schemas.py new file mode 100644 index 00000000..cdeff562 --- /dev/null +++ b/src/api/v1/routers/auth/schemas.py @@ -0,0 +1,13 @@ +from datetime import datetime +from typing import Literal + +from pydantic import BaseModel + +from src.api.v1.routers.users.schemas import UserOut + + +class SessionOut(BaseModel): + token: str + token_type: Literal["Bearer"] + expires_at: datetime + user: UserOut diff --git a/src/api/v1/routers/chats/__init__.py b/src/api/v1/routers/chats/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/api/v1/routers/chats/router.py b/src/api/v1/routers/chats/router.py new file mode 100644 index 00000000..d0718393 --- /dev/null +++ b/src/api/v1/routers/chats/router.py @@ -0,0 +1,86 @@ +from typing import Annotated +from uuid import UUID + +from fastapi import APIRouter, Depends, Query, status + +from src.api.dependencies import get_chat_service, get_current_user +from src.api.v1.routers.chats.schemas import ChatOut, CreateChatIn, GetChatOut +from src.api.v1.schemas import PaginationQuery +from src.core.errors import ApiError +from src.core.pagination import decode_cursor, encode_cursor +from src.service.chat import ChatService +from src.service.dto import ChatDTO, UserDTO +from src.service.exceptions import ChatNotFoundException, InvalidChatMembersException + +router = APIRouter(prefix="/chats", tags=["Чаты"]) + + +def _to_chat_out(chat: ChatDTO) -> ChatOut: + return ChatOut( + id=chat.id, + title=chat.title, + member_ids=list(chat.member_ids), + created_at=chat.created_at, + ) + + +@router.post("", status_code=status.HTTP_201_CREATED) +async def create_chat( + chat_in: CreateChatIn, + current_user: Annotated[UserDTO, Depends(get_current_user)], + chat_service: Annotated[ChatService, Depends(get_chat_service)], +) -> ChatOut: + try: + chat = await chat_service.create_chat( + title=chat_in.title, + member_ids=chat_in.member_ids, + current_user_id=current_user.id, + ) + except InvalidChatMembersException as error: + raise ApiError(400, "invalid_request", str(error)) from error + return _to_chat_out(chat) + + +@router.get("", status_code=status.HTTP_200_OK) +async def get_chats( + pagination: Annotated[PaginationQuery, Query()], + current_user: Annotated[UserDTO, Depends(get_current_user)], + chat_service: Annotated[ChatService, Depends(get_chat_service)], +) -> GetChatOut: + after = None + if pagination.cursor is not None: + after = decode_cursor( + pagination.cursor, + expected_kind="chats", + expected_owner_id=current_user.id, + ) + chats = await chat_service.get_chats( + current_user_id=current_user.id, limit=pagination.limit + 1, after=after + ) + + page = chats[: pagination.limit] + next_cursor = None + if len(chats) > pagination.limit: + last = page[-1] + next_cursor = encode_cursor("chats", current_user.id, last.created_at, last.id) + + return GetChatOut( + items=[_to_chat_out(chat) for chat in page], + next_cursor=next_cursor, + ) + + +@router.get("/{chat_id}", summary="Получить доступный чат") +async def get_chat( + chat_id: UUID, + current_user: Annotated[UserDTO, Depends(get_current_user)], + chat_service: Annotated[ChatService, Depends(get_chat_service)], +) -> ChatOut: + try: + chat = await chat_service.get_chat( + chat_id=chat_id, + current_user_id=current_user.id, + ) + except ChatNotFoundException as error: + raise ApiError(404, "not_found", str(error)) from error + return _to_chat_out(chat) diff --git a/src/api/v1/routers/chats/schemas.py b/src/api/v1/routers/chats/schemas.py new file mode 100644 index 00000000..b51b7f88 --- /dev/null +++ b/src/api/v1/routers/chats/schemas.py @@ -0,0 +1,42 @@ +from datetime import datetime +from uuid import UUID + +from pydantic import BaseModel, ConfigDict, Field, field_validator + + +class CreateChatIn(BaseModel): + model_config = ConfigDict(extra="forbid") + + title: str = Field(min_length=1, max_length=100) + member_ids: list[UUID] = Field(min_length=1, max_length=100) + + @field_validator("title") + @classmethod + def title_must_not_be_blank(cls, value: str) -> str: + value.encode("utf-8") + if not value.strip(): + raise ValueError("title не может состоять только из пробелов") + return value + + @field_validator("member_ids") + @classmethod + def member_ids_must_be_unique(cls, value: list[UUID]) -> list[UUID]: + if len(value) != len(set(value)): + raise ValueError("member_ids не должен содержать дубликаты") + return value + + +class ChatOut(BaseModel): + model_config = ConfigDict(extra="forbid") + + id: UUID + title: str + member_ids: list[UUID] + created_at: datetime + + +class GetChatOut(BaseModel): + model_config = ConfigDict(extra="forbid") + + items: list[ChatOut] + next_cursor: str | None diff --git a/src/api/v1/routers/messages/__init__.py b/src/api/v1/routers/messages/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/api/v1/routers/messages/router.py b/src/api/v1/routers/messages/router.py new file mode 100644 index 00000000..1b8eab1b --- /dev/null +++ b/src/api/v1/routers/messages/router.py @@ -0,0 +1,94 @@ +import re +from typing import Annotated +from uuid import UUID + +from fastapi import APIRouter, Depends, Header, Query, Request, status + +from src.api.dependencies import get_current_user, get_message_service +from src.api.v1.routers.messages.schemas import ( + CreateMessageIn, + MessageOut, + MessagePageOut, +) +from src.api.v1.schemas import PaginationQuery +from src.core.errors import ApiError +from src.core.pagination import decode_cursor, encode_cursor +from src.service.dto import MessageDTO, UserDTO +from src.service.exceptions import ChatNotFoundException, IdempotencyConflictException +from src.service.message import MessageService + +router = APIRouter(prefix="/chats", tags=["Сообщения"]) + + +def _to_message_out(message: MessageDTO) -> MessageOut: + return MessageOut( + id=message.id, + chat_id=message.chat_id, + sender_id=message.sender_id, + text=message.text, + created_at=message.created_at, + ) + + +@router.post("/{chat_id}/messages", status_code=status.HTTP_201_CREATED) +async def create_message( + chat_id: UUID, + message_in: CreateMessageIn, + request: Request, + current_user: Annotated[UserDTO, Depends(get_current_user)], + message_service: Annotated[MessageService, Depends(get_message_service)], + idempotency_key: Annotated[str, Header()], +) -> MessageOut: + if ( + len(request.headers.getlist("idempotency-key")) != 1 + or re.fullmatch(r"[A-Za-z0-9._:-]{1,128}", idempotency_key) is None + ): + raise ApiError(400, "invalid_request", "Требуется корректный Idempotency-Key") + try: + message = await message_service.create_message( + chat_id=chat_id, + sender_id=current_user.id, + text=message_in.text, + idempotency_key=idempotency_key, + ) + except ChatNotFoundException as error: + raise ApiError(404, "not_found", str(error)) from error + except IdempotencyConflictException as error: + raise ApiError(409, "conflict", "Ключ уже использован для другого сообщения") from error + return _to_message_out(message) + + +@router.get("/{chat_id}/messages", status_code=status.HTTP_200_OK) +async def get_messages( + chat_id: UUID, + pagination: Annotated[PaginationQuery, Query()], + current_user: Annotated[UserDTO, Depends(get_current_user)], + message_service: Annotated[MessageService, Depends(get_message_service)], +) -> MessagePageOut: + after = None + if pagination.cursor is not None: + after = decode_cursor( + pagination.cursor, + expected_kind="messages", + expected_owner_id=chat_id, + ) + try: + messages = await message_service.get_messages( + chat_id=chat_id, + current_user_id=current_user.id, + limit=pagination.limit + 1, + after=after, + ) + except ChatNotFoundException as error: + raise ApiError(404, "not_found", str(error)) from error + + page = messages[: pagination.limit] + next_cursor = None + if len(messages) > pagination.limit: + last = page[-1] + next_cursor = encode_cursor("messages", chat_id, last.created_at, last.id) + + return MessagePageOut( + items=[_to_message_out(message) for message in page], + next_cursor=next_cursor, + ) diff --git a/src/api/v1/routers/messages/schemas.py b/src/api/v1/routers/messages/schemas.py new file mode 100644 index 00000000..4fd538df --- /dev/null +++ b/src/api/v1/routers/messages/schemas.py @@ -0,0 +1,36 @@ +from datetime import datetime +from uuid import UUID + +from pydantic import BaseModel, ConfigDict, Field, field_validator + + +class CreateMessageIn(BaseModel): + model_config = ConfigDict(extra="forbid") + + text: str = Field(min_length=1, max_length=4000) + attachment_ids: list[UUID] = Field(default_factory=list, max_length=0) + + @field_validator("text") + @classmethod + def text_must_not_be_blank(cls, value: str) -> str: + value.encode("utf-8") + if not value.strip(): + raise ValueError("text не может состоять только из пробелов") + return value + + +class MessageOut(BaseModel): + model_config = ConfigDict(extra="forbid") + + id: UUID + chat_id: UUID + sender_id: UUID + text: str + created_at: datetime + + +class MessagePageOut(BaseModel): + model_config = ConfigDict(extra="forbid") + + items: list[MessageOut] + next_cursor: str | None diff --git a/src/api/v1/routers/users/__init__.py b/src/api/v1/routers/users/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/api/v1/routers/users/router.py b/src/api/v1/routers/users/router.py new file mode 100644 index 00000000..cbe37264 --- /dev/null +++ b/src/api/v1/routers/users/router.py @@ -0,0 +1,44 @@ +from typing import Annotated + +from fastapi import APIRouter, Depends, status + +from src.api.dependencies import get_current_user, get_user_service +from src.api.v1.routers.users.schemas import CreateUserIn, UserOut +from src.core.errors import ApiError +from src.service.dto import UserDTO +from src.service.exceptions import UserAlreadyExistsException +from src.service.user import UserService + +router = APIRouter(prefix="/users", tags=["Пользователи"]) + + +def _to_user_out(user: UserDTO) -> UserOut: + return UserOut( + id=user.id, + username=user.username, + display_name=user.display_name, + created_at=user.created_at, + ) + + +@router.post("", status_code=status.HTTP_201_CREATED) +async def create_user( + user_in: CreateUserIn, + user_service: Annotated[UserService, Depends(get_user_service)], +) -> UserOut: + try: + user = await user_service.create_user( + password=user_in.password, + username=user_in.username, + display_name=user_in.display_name, + ) + except UserAlreadyExistsException as error: + raise ApiError(409, "conflict", str(error)) from error + return _to_user_out(user) + + +@router.get("/me", status_code=status.HTTP_200_OK) +async def get_me( + current_user: Annotated[UserDTO, Depends(get_current_user)], +) -> UserOut: + return _to_user_out(current_user) diff --git a/src/api/v1/routers/users/schemas.py b/src/api/v1/routers/users/schemas.py new file mode 100644 index 00000000..7cac9dae --- /dev/null +++ b/src/api/v1/routers/users/schemas.py @@ -0,0 +1,29 @@ +from datetime import datetime +from uuid import UUID + +from pydantic import BaseModel, ConfigDict, Field, field_validator + + +class CreateUserIn(BaseModel): + model_config = ConfigDict(extra="forbid") + + username: str = Field(pattern=r"^[a-z0-9_]{3,32}$") + password: str = Field(min_length=12, max_length=128) + display_name: str = Field(min_length=1, max_length=100) + + @field_validator("display_name", "password") + @classmethod + def required_string_must_not_be_blank(cls, value: str) -> str: + value.encode("utf-8") + if not value.strip(): + raise ValueError("Обязательная строка не может состоять только из пробелов") + return value + + +class UserOut(BaseModel): + model_config = ConfigDict(extra="forbid") + + id: UUID + username: str + display_name: str + created_at: datetime diff --git a/src/api/v1/schemas.py b/src/api/v1/schemas.py new file mode 100644 index 00000000..63106894 --- /dev/null +++ b/src/api/v1/schemas.py @@ -0,0 +1,21 @@ +import re + +from pydantic import BaseModel, ConfigDict, Field, field_validator + +from src.core.settings import settings + + +class PaginationQuery(BaseModel): + model_config = ConfigDict(extra="forbid") + + limit: int = Field(default=50, ge=1, le=100) + cursor: str | None = Field(default=None, min_length=1, max_length=settings.cursor_max_length) + + @field_validator("limit", mode="before") + @classmethod + def limit_must_be_integer(cls, value): + if isinstance(value, bool) or isinstance(value, float): + raise ValueError("limit должен быть целым числом") + if isinstance(value, str) and re.fullmatch(r"[0-9]+", value) is None: + raise ValueError("limit должен содержать десятичные цифры") + return value diff --git a/src/core/__init__.py b/src/core/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/core/cache.py b/src/core/cache.py new file mode 100644 index 00000000..19444bfe --- /dev/null +++ b/src/core/cache.py @@ -0,0 +1,68 @@ +"""Клиент Redis и необязательный кэш профилей пользователей.""" + +import logging + +from redis.asyncio import Redis +from redis.exceptions import RedisError + +from src.core.settings import settings + +logger = logging.getLogger(__name__) +cache: Redis | None = None + + +def open_cache(): + global cache + cache = Redis.from_url( + settings.redis_url.get_secret_value(), + decode_responses=True, + max_connections=settings.redis_max_connections, + socket_connect_timeout=settings.redis_connect_timeout_seconds, + socket_timeout=settings.redis_read_timeout_seconds, + retry_on_timeout=settings.redis_retry_on_timeout, + ) + + +async def close_cache(): + global cache + if cache is not None: + await cache.aclose() + cache = None + + +async def get(key: str) -> str | None: + if cache is None: + return None + try: + return await cache.get(key) + except UnicodeError: + logger.warning("Повреждённый UTF-8 в кэше профиля; читаем PostgreSQL") + await delete(key) + return None + except (RedisError, OSError): + logger.warning("Redis недоступен; читаем профиль из PostgreSQL") + return None + + +async def set(key: str, value: str): + if cache is None: + return + try: + await cache.set(key, value, ex=settings.profile_cache_ttl_seconds) + except (RedisError, OSError): + logger.warning("Redis недоступен; пропускаем запись кэша") + + +async def delete(key: str) -> None: + if cache is None: + return + try: + await cache.delete(key) + except (RedisError, OSError): + logger.warning("Redis недоступен; пропускаем удаление кэша профиля") + + +def get_client() -> Redis: + if cache is None: + raise RedisError("Клиент Redis не настроен") + return cache diff --git a/src/core/callback.py b/src/core/callback.py new file mode 100644 index 00000000..3cf0cff6 --- /dev/null +++ b/src/core/callback.py @@ -0,0 +1,24 @@ +from collections.abc import AsyncIterator +from contextlib import asynccontextmanager + +from fastapi import FastAPI + +from src.api.v1.routers.auth.callback import create_admin +from src.core import readiness +from src.core.db import session as database + +STARTUP_CALLBACKS = (create_admin,) + + +@asynccontextmanager +async def lifespan(app: FastAPI) -> AsyncIterator[None]: + async with database.database_lifespan(app): + session_factory = database.session_factory + if session_factory is None: + raise RuntimeError("Фабрика сессий не запущена") + for callback in STARTUP_CALLBACKS: + await callback(session_factory) + try: + yield + finally: + await readiness.close() diff --git a/src/core/db/__init__.py b/src/core/db/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/core/db/models.py b/src/core/db/models.py new file mode 100644 index 00000000..da242f8e --- /dev/null +++ b/src/core/db/models.py @@ -0,0 +1,90 @@ +from datetime import datetime +from uuid import UUID + +from sqlalchemy import ( + Boolean, + CheckConstraint, + DateTime, + ForeignKey, + ForeignKeyConstraint, + Index, + String, + Text, + false, +) +from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship + +from src.core.db.types import Utf8Text + + +class Base(DeclarativeBase): + pass + + +class User(Base): + __tablename__ = "users" + __table_args__ = ( + CheckConstraint("username ~ '^[a-z0-9_]{3,32}$'"), + CheckConstraint( + "octet_length(display_name) BETWEEN 1 AND 400", name="users_display_name_bytes_check" + ), + ) + + id: Mapped[UUID] = mapped_column(primary_key=True) + username: Mapped[str] = mapped_column(String(32), unique=True) + display_name: Mapped[str] = mapped_column(Utf8Text()) + password_hash: Mapped[str] = mapped_column(Text) + is_admin: Mapped[bool] = mapped_column(Boolean, default=False, server_default=false()) + created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True)) + + +class Chat(Base): + __tablename__ = "chats" + __table_args__ = ( + CheckConstraint("octet_length(title) BETWEEN 1 AND 400", name="chats_title_bytes_check"), + ) + + id: Mapped[UUID] = mapped_column(primary_key=True) + title: Mapped[str] = mapped_column(Utf8Text()) + created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True)) + members: Mapped[list["ChatMember"]] = relationship(order_by="ChatMember.user_id") + + +class ChatMember(Base): + __tablename__ = "chat_members" + __table_args__ = (Index("chat_members_user_idx", "user_id", "chat_id"),) + + chat_id: Mapped[UUID] = mapped_column( + ForeignKey("chats.id", ondelete="CASCADE"), primary_key=True + ) + user_id: Mapped[UUID] = mapped_column(ForeignKey("users.id"), primary_key=True) + + +class Message(Base): + __tablename__ = "messages" + __table_args__ = ( + ForeignKeyConstraint( + ["chat_id", "sender_id"], ["chat_members.chat_id", "chat_members.user_id"] + ), + CheckConstraint("octet_length(text) BETWEEN 1 AND 16000", name="messages_text_bytes_check"), + Index("messages_history_idx", "chat_id", "created_at", "id"), + ) + + id: Mapped[UUID] = mapped_column(primary_key=True) + chat_id: Mapped[UUID] = mapped_column(ForeignKey("chats.id")) + sender_id: Mapped[UUID] = mapped_column(ForeignKey("users.id")) + text: Mapped[str] = mapped_column(Utf8Text()) + created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True)) + + +class MessageRequest(Base): + # Таблица обслуживает только POST /chats/{chat_id}/messages: метод и путь + # однозначно заданы таблицей и chat_id. Общая область ключа: sender/chat/key. + __tablename__ = "message_requests" + + sender_id: Mapped[UUID] = mapped_column(ForeignKey("users.id"), primary_key=True) + chat_id: Mapped[UUID] = mapped_column(ForeignKey("chats.id"), primary_key=True) + key: Mapped[str] = mapped_column(String(128), primary_key=True) + request_text: Mapped[str] = mapped_column(Utf8Text()) + message_id: Mapped[UUID | None] = mapped_column(ForeignKey("messages.id")) + expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True)) diff --git a/src/core/db/repo/__init__.py b/src/core/db/repo/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/core/db/repo/auth.py b/src/core/db/repo/auth.py new file mode 100644 index 00000000..2497013b --- /dev/null +++ b/src/core/db/repo/auth.py @@ -0,0 +1,16 @@ +from sqlalchemy import select +from sqlalchemy.ext.asyncio import AsyncSession + +from src.core.db.models import User +from src.core.db.repo.user import to_user +from src.service.dto import UserCredentialsDTO + + +async def fetch_user_credentials( + session: AsyncSession, + username: str, +) -> UserCredentialsDTO | None: + user = await session.scalar(select(User).where(User.username == username)) + if user is None: + return None + return UserCredentialsDTO(user=to_user(user), password_hash=user.password_hash) diff --git a/src/core/db/repo/chat.py b/src/core/db/repo/chat.py new file mode 100644 index 00000000..aa887703 --- /dev/null +++ b/src/core/db/repo/chat.py @@ -0,0 +1,59 @@ +from datetime import datetime +from uuid import UUID + +from sqlalchemy import select, tuple_ +from sqlalchemy.ext.asyncio import AsyncSession +from sqlalchemy.orm import selectinload + +from src.core.db.models import Chat, ChatMember +from src.service.dto import ChatDTO + + +def to_chat(chat: Chat) -> ChatDTO: + return ChatDTO( + chat.id, chat.title, tuple(member.user_id for member in chat.members), chat.created_at + ) + + +async def fetch_chat_by_id(session: AsyncSession, chat_id: UUID) -> ChatDTO | None: + chat = await session.scalar( + select(Chat).options(selectinload(Chat.members)).where(Chat.id == chat_id), + ) + return to_chat(chat) if chat else None + + +async def fetch_chats( + session: AsyncSession, + user_id: UUID, + limit: int, + after: tuple[datetime, UUID] | None = None, +) -> list[ChatDTO]: + query = ( + select(Chat) + .options(selectinload(Chat.members)) + .join(ChatMember) + .where(ChatMember.user_id == user_id) + ) + if after is not None: + query = query.where(tuple_(Chat.created_at, Chat.id) > after) + chats = await session.scalars(query.order_by(Chat.created_at, Chat.id).limit(limit)) + return [to_chat(chat) for chat in chats] + + +async def create_chat( + session: AsyncSession, + chat_id: UUID, + title: str, + member_ids: list[UUID], + created_at: datetime, +) -> ChatDTO: + chat = Chat( + id=chat_id, + title=title, + created_at=created_at, + members=[ChatMember(user_id=user_id) for user_id in member_ids], + ) + session.add(chat) + # ORM сохраняет чат и участников в транзакции текущего запроса. + await session.flush() + return to_chat(chat) diff --git a/src/core/db/repo/message.py b/src/core/db/repo/message.py new file mode 100644 index 00000000..3ece891c --- /dev/null +++ b/src/core/db/repo/message.py @@ -0,0 +1,106 @@ +from datetime import datetime +from uuid import UUID + +from sqlalchemy import select, tuple_ +from sqlalchemy.ext.asyncio import AsyncSession + +from src.core.db.models import Message +from src.service.dto import MessageDTO + + +def to_message(message: Message) -> MessageDTO: + return MessageDTO( + message.id, message.chat_id, message.sender_id, message.text, message.created_at + ) + + +def history_query(chat_id: UUID, limit: int, after: tuple[datetime, UUID] | None = None): + query = select(Message).where(Message.chat_id == chat_id) + if after is not None: + query = query.where(tuple_(Message.created_at, Message.id) > after) + return query.order_by(Message.created_at, Message.id).limit(limit) + + +async def fetch_messages_by_chat_id( + session: AsyncSession, + chat_id: UUID, + limit: int, + after: tuple[datetime, UUID] | None = None, +) -> list[MessageDTO]: + messages = await session.scalars(history_query(chat_id, limit, after)) + return [to_message(message) for message in messages] + + +async def create_message( + session: AsyncSession, + message_id: UUID, + chat_id: UUID, + sender_id: UUID, + text: str, + created_at: datetime, +) -> MessageDTO: + message = Message( + id=message_id, chat_id=chat_id, sender_id=sender_id, text=text, created_at=created_at + ) + session.add(message) + await session.flush() + return to_message(message) + + +async def create_idempotent_message( + session: AsyncSession, + message_id: UUID, + chat_id: UUID, + sender_id: UUID, + text: str, + created_at: datetime, + idempotency_key: str, +) -> MessageDTO: + from sqlalchemy import func, update + from sqlalchemy import text as sql_text + from sqlalchemy.dialects.postgresql import insert + + from src.core.db.models import MessageRequest + from src.service.exceptions import IdempotencyConflictException + + identity = ( + MessageRequest.sender_id == sender_id, + MessageRequest.chat_id == chat_id, + MessageRequest.key == idempotency_key, + ) + # UNIQUE/UPSERT сериализует конкурентные повторы даже на разных процессах. + # Проигравший ждёт COMMIT; при rollback он сам получает право на создание. + values = dict( + sender_id=sender_id, + chat_id=chat_id, + key=idempotency_key, + request_text=text, + message_id=None, + expires_at=func.clock_timestamp() + sql_text("interval '24 hours'"), + ) + claim = ( + insert(MessageRequest) + .values(**values) + .on_conflict_do_update( + index_elements=["sender_id", "chat_id", "key"], + set_={ + k: v for k, v in values.items() if k in {"request_text", "message_id", "expires_at"} + }, + where=MessageRequest.expires_at <= func.clock_timestamp(), + ) + .returning(MessageRequest.key) + ) + owned = await session.scalar(claim) + if owned is None: + existing = (await session.execute(select(MessageRequest).where(*identity))).scalar_one() + if existing.request_text != text: + raise IdempotencyConflictException + message = await session.get(Message, existing.message_id) + if message is None: + raise RuntimeError("Нарушена атомарность результата идемпотентности") + return to_message(message) + + message = await create_message(session, message_id, chat_id, sender_id, text, created_at) + await session.execute(update(MessageRequest).where(*identity).values(message_id=message.id)) + # COMMIT сообщения и ключа выполняется вместе в get_db_session до HTTP 201. + return message diff --git a/src/core/db/repo/user.py b/src/core/db/repo/user.py new file mode 100644 index 00000000..6f10eed6 --- /dev/null +++ b/src/core/db/repo/user.py @@ -0,0 +1,120 @@ +import json +import re +from dataclasses import asdict +from datetime import datetime, timezone +from uuid import UUID + +from sqlalchemy import func, select +from sqlalchemy.dialects.postgresql import insert +from sqlalchemy.ext.asyncio import AsyncSession + +from src.core import cache +from src.core.db.models import User +from src.service.dto import UserDTO + + +def to_user(user: User) -> UserDTO: + return UserDTO(user.id, user.username, user.display_name, user.created_at) + + +async def fetch_user_by_id(session: AsyncSession, user_id: UUID) -> UserDTO | None: + key = f"user:v1:{user_id}" + cached = await cache.get(key) + if cached is not None: + try: + data = json.loads(cached) + if ( + not isinstance(data, dict) + or set(data) != {"id", "username", "display_name", "created_at"} + or not all(isinstance(value, str) for value in data.values()) + ): + raise ValueError + created_at = datetime.fromisoformat(data["created_at"]) + if created_at.tzinfo is None: + raise ValueError + data["display_name"].encode("utf-8") + user = UserDTO( + UUID(data["id"]), + data["username"], + data["display_name"], + created_at.astimezone(timezone.utc), + ) + if ( + user.id != user_id + or re.fullmatch(r"[a-z0-9_]{3,32}", user.username) is None + or not 1 <= len(user.display_name) <= 100 + or not user.display_name.strip() + or user.created_at.tzinfo is None + ): + raise ValueError + return user + except (ValueError, KeyError, TypeError, OverflowError): + await cache.delete(key) + model = await session.get(User, user_id) + user = to_user(model) if model else None + if user: + await cache.set(key, json.dumps(asdict(user), default=str)) + return user + + +async def fetch_user_by_username(session: AsyncSession, username: str) -> UserDTO | None: + model = await session.scalar(select(User).where(User.username == username)) + return to_user(model) if model else None + + +async def create_user( + session: AsyncSession, + user_id: UUID, + username: str, + display_name: str, + created_at: datetime, + password_hash: str, +) -> UserDTO: + user = User( + id=user_id, + username=username, + display_name=display_name, + created_at=created_at, + password_hash=password_hash, + ) + session.add(user) + await session.flush() + return to_user(user) + + +async def users_exist(session: AsyncSession, user_ids: list[UUID]) -> bool: + count = await session.scalar( + select(func.count()).select_from(User).where(User.id.in_(user_ids)) + ) + return count == len(user_ids) + + +async def create_admin( + session: AsyncSession, + user_id: UUID, + username: str, + display_name: str, + password_hash: str, + created_at: datetime, +) -> UserDTO | None: + user = await session.scalar( + insert(User) + .values( + id=user_id, + username=username, + display_name=display_name, + password_hash=password_hash, + created_at=created_at, + is_admin=True, + ) + .on_conflict_do_nothing(index_elements=[User.username]) + .returning(User) + ) + return to_user(user) if user else None + + +async def admin_exists(session: AsyncSession, username: str) -> bool: + user_id = await session.scalar( + select(User.id).where(User.username == username, User.is_admin.is_(True)) + ) + return user_id is not None diff --git a/src/core/db/session.py b/src/core/db/session.py new file mode 100644 index 00000000..e4aaeb05 --- /dev/null +++ b/src/core/db/session.py @@ -0,0 +1,48 @@ +from contextlib import asynccontextmanager + +from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine + +from src.core.cache import close_cache, open_cache +from src.core.settings import settings + +session_factory: async_sessionmaker[AsyncSession] | None = None + + +def create_engine(): + return create_async_engine( + settings.sqlalchemy_database_url, + pool_size=settings.database_pool_size, + max_overflow=settings.database_max_overflow, + pool_timeout=settings.database_pool_timeout_seconds, + pool_pre_ping=settings.database_pool_pre_ping, + hide_parameters=True, + connect_args={ + "timeout": settings.database_connect_timeout_seconds, + "command_timeout": settings.database_command_timeout_seconds, + "server_settings": {"statement_timeout": str(settings.database_statement_timeout_ms)}, + }, + ) + + +@asynccontextmanager +async def database_lifespan(app): + global session_factory + engine = create_engine() + session_factory = async_sessionmaker(engine, expire_on_commit=False) + open_cache() + try: + yield + finally: + await close_cache() + await engine.dispose() + session_factory = None + + +async def get_db_session(): + if session_factory is None: + raise RuntimeError("Фабрика сессий не запущена") + async with session_factory() as session: + # Одна транзакция на запрос: аутентификация и бизнес-операция вместе. + # Depends(scope='function') завершает COMMIT до отправки ответа. + async with session.begin(): + yield session diff --git a/src/core/db/types.py b/src/core/db/types.py new file mode 100644 index 00000000..36501fdc --- /dev/null +++ b/src/core/db/types.py @@ -0,0 +1,15 @@ +"""Unicode-строки в UTF-8 bytea: PostgreSQL text не допускает U+0000.""" + +from sqlalchemy import LargeBinary +from sqlalchemy.types import TypeDecorator + + +class Utf8Text(TypeDecorator[str]): + impl = LargeBinary + cache_ok = True + + def process_bind_param(self, value: str | None, dialect) -> bytes | None: + return None if value is None else value.encode("utf-8") + + def process_result_value(self, value: bytes | None, dialect) -> str | None: + return None if value is None else value.decode("utf-8") diff --git a/src/core/errors.py b/src/core/errors.py new file mode 100644 index 00000000..70a62c9b --- /dev/null +++ b/src/core/errors.py @@ -0,0 +1,29 @@ +from fastapi import Request +from fastapi.responses import JSONResponse + + +class ApiError(Exception): + def __init__(self, status_code: int, code: str, message: str) -> None: + self.status_code = status_code + self.code = code + self.message = message + super().__init__(message) + + +def error_response( + request: Request, + status_code: int, + code: str, + message: str, +) -> JSONResponse: + request_id = getattr(request.state, "request_id", "неизвестен") + return JSONResponse( + status_code=status_code, + content={ + "error": { + "code": code, + "message": message, + "request_id": request_id, + } + }, + ) diff --git a/src/core/pagination.py b/src/core/pagination.py new file mode 100644 index 00000000..d49e7b7c --- /dev/null +++ b/src/core/pagination.py @@ -0,0 +1,43 @@ +import base64 +import json +from datetime import datetime, timezone +from uuid import UUID + +from src.core.errors import ApiError + + +def encode_cursor(kind: str, owner_id: UUID, created_at: datetime, item_id: UUID) -> str: + payload = { + "kind": kind, + "owner_id": str(owner_id), # id чата + "created_at": created_at.isoformat(), # его время + "item_id": str(item_id), # id сообщения + } + raw = json.dumps(payload, separators=(",", ":")).encode("utf-8") + return base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=") + + +def decode_cursor( + cursor: str, expected_kind: str, expected_owner_id: UUID +) -> tuple[datetime, UUID]: + try: + padding = "=" * (-len(cursor) % 4) + raw = base64.b64decode(cursor + padding, altchars=b"-_", validate=True) + payload = json.loads(raw.decode("utf-8")) + if ( + not isinstance(payload, dict) + or set(payload) != {"kind", "owner_id", "created_at", "item_id"} + or not all(isinstance(value, str) for value in payload.values()) + ): + raise ValueError + if payload["kind"] != expected_kind: + raise ValueError + if UUID(payload["owner_id"]) != expected_owner_id: + raise ValueError + created_at = datetime.fromisoformat(payload["created_at"]) + item_id = UUID(payload["item_id"]) + if created_at.tzinfo is None: + raise ValueError + return created_at.astimezone(timezone.utc), item_id + except (KeyError, TypeError, ValueError, UnicodeError, OverflowError): + raise ApiError(400, "invalid_request", "Некорректный курсор") from None diff --git a/src/core/readiness.py b/src/core/readiness.py new file mode 100644 index 00000000..f627475a --- /dev/null +++ b/src/core/readiness.py @@ -0,0 +1,59 @@ +"""Ограниченная проверка обязательного пути, включая повреждение схемы. + +Отмена asyncpg может ждать ответа замороженного PostgreSQL. HTTP readiness +не должна ждать завершения такого rollback; незавершённая проба остаётся одной +на процесс и до её завершения сервер считается неготовым. +""" + +import asyncio + +from sqlalchemy import select + +from src.core import cache +from src.core.db import session as database +from src.core.db.models import Chat, ChatMember, Message, MessageRequest, User +from src.core.errors import ApiError +from src.core.settings import settings + +_pending: asyncio.Task[None] | None = None + + +async def _probe() -> None: + factory = database.session_factory + if factory is None: + raise ApiError(503, "unavailable", "Хранилище временно недоступно") + async with factory() as session: + for model in (User, Chat, ChatMember, Message, MessageRequest): + await session.execute(select(model).limit(0)) + await cache.get_client().ping() + + +def _consume_result(task: asyncio.Task[None]) -> None: + if not task.cancelled(): + task.exception() + + +async def check() -> None: + global _pending + if _pending is None or _pending.done(): + _pending = asyncio.create_task(_probe()) + _pending.add_done_callback(_consume_result) + task = _pending + # wait(), в отличие от wait_for(), не ждёт завершения отмены драйвером. + done, _ = await asyncio.wait({task}, timeout=settings.healthcheck_timeout_seconds) + if not done: + if not task.cancelling(): + task.cancel() + raise ApiError(503, "unavailable", "Проверка готовности превысила время ожидания") + if task.cancelled(): + raise ApiError(503, "unavailable", "Хранилище временно недоступно") + task.result() + + +async def close() -> None: + global _pending + if _pending is not None and not _pending.done(): + if not _pending.cancelling(): + _pending.cancel() + await asyncio.wait({_pending}, timeout=settings.healthcheck_timeout_seconds) + _pending = None diff --git a/src/core/request_limits.py b/src/core/request_limits.py new file mode 100644 index 00000000..8e72345e --- /dev/null +++ b/src/core/request_limits.py @@ -0,0 +1,122 @@ +"""Ограниченный приём тела до валидации FastAPI, включая chunked-запросы.""" + +import asyncio +import json + +from anyio import CapacityLimiter, WouldBlock +from starlette.requests import Request +from starlette.types import ASGIApp, Message, Receive, Scope, Send + +from src.core.errors import error_response +from src.core.settings import settings + + +def unique_object(pairs: list[tuple[str, object]]) -> dict[str, object]: + result = {} + for key, value in pairs: + if key in result: + raise ValueError("Повторяющийся ключ JSON") + result[key] = value + return result + + +def reject_constant(value: str) -> None: + raise ValueError("Недопустимое значение JSON") + + +class RequestLimitsMiddleware: + def __init__(self, app: ASGIApp) -> None: + self.app = app + self.limiter = CapacityLimiter(settings.http_concurrency_limit) + + async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: + if scope["type"] != "http" or not scope["path"].startswith("/api/v1/"): + await self.app(scope, receive, send) + return + try: + self.limiter.acquire_nowait() + except WouldBlock: + await error_response(Request(scope), 503, "unavailable", "Сервис перегружен")( + scope, receive, send + ) + return + try: + await self.handle_request(scope, receive, send) + finally: + self.limiter.release() + + async def handle_request(self, scope: Scope, receive: Receive, send: Send) -> None: + request = Request(scope) + if len(request.headers.getlist("content-type")) > 1 or any( + len(request.query_params.getlist(name)) > 1 for name in ("limit", "cursor") + ): + await error_response( + request, 400, "invalid_request", "Неоднозначные параметры запроса" + )(scope, receive, send) + return + content_type = request.headers.get("content-type", "").split(";", 1)[0].strip().lower() + json_body = ( + request.method == "POST" and scope["path"].rstrip("/") != "/api/v1/auth/sessions" + ) + if json_body and content_type != "application/json": + await error_response( + request, 415, "unsupported_media_type", "Требуется application/json" + )(scope, receive, send) + return + try: + length = int(request.headers.get("content-length", "0")) + if length < 0: + raise ValueError + except ValueError: + await error_response(request, 400, "invalid_request", "Неверная длина тела")( + scope, receive, send + ) + return + body = bytearray() + too_large = length > settings.http_max_body_bytes + if not too_large: + try: + async with asyncio.timeout(settings.http_body_timeout_seconds): + while True: + message = await receive() + if message["type"] == "http.disconnect": + return + chunk = message.get("body", b"") + if len(body) + len(chunk) > settings.http_max_body_bytes: + too_large = True + break + body.extend(chunk) + if not message.get("more_body", False): + break + except TimeoutError: + await error_response(request, 408, "invalid_request", "Истекло время приёма тела")( + scope, receive, send + ) + return + if too_large: + await error_response(request, 413, "payload_too_large", "Превышен размер тела запроса")( + scope, receive, send + ) + return + if json_body: + try: + json.loads( + body.decode("utf-8"), + object_pairs_hook=unique_object, + parse_constant=reject_constant, + ) + except (ValueError, UnicodeError, RecursionError): + await error_response(request, 400, "invalid_request", "Некорректный JSON UTF-8")( + scope, receive, send + ) + return + delivered = False + + async def buffered_receive() -> Message: + nonlocal delivered + if delivered: + return await receive() + delivered = True + return {"type": "http.request", "body": bytes(body), "more_body": False} + + await self.app(scope, buffered_receive, send) diff --git a/src/core/security.py b/src/core/security.py new file mode 100644 index 00000000..210710c5 --- /dev/null +++ b/src/core/security.py @@ -0,0 +1,42 @@ +"""Пароли: Argon2id. Токены: SHA-256 от случайного секрета.""" + +import hashlib + +from anyio import CapacityLimiter, to_thread +from argon2 import PasswordHasher, Type +from argon2.exceptions import InvalidHashError, VerificationError + +from src.core.settings import settings + +password_hasher = PasswordHasher( + time_cost=settings.argon2_time_cost, + memory_cost=settings.argon2_memory_cost, + parallelism=settings.argon2_parallelism, + salt_len=settings.argon2_salt_len, + hash_len=settings.argon2_hash_len, + type=Type.ID, +) +password_limiter = CapacityLimiter(settings.password_hash_concurrency) + + +async def hash_password_async(password: str) -> str: + return await to_thread.run_sync(hash_password, password, limiter=password_limiter) + + +async def verify_password_async(password: str, stored: str) -> bool: + return await to_thread.run_sync(verify_password, password, stored, limiter=password_limiter) + + +def hash_password(password: str) -> str: + return password_hasher.hash(password) + + +def verify_password(password: str, stored: str) -> bool: + try: + return password_hasher.verify(stored, password) + except (VerificationError, InvalidHashError): + return False + + +def hash_token(token: str) -> str: + return hashlib.sha256(token.encode("utf-8")).hexdigest() diff --git a/src/core/session_store.py b/src/core/session_store.py new file mode 100644 index 00000000..1d7083fa --- /dev/null +++ b/src/core/session_store.py @@ -0,0 +1,55 @@ +"""Сессии в Redis: абсолютный срок жизни, без продления при чтении.""" + +import json +from dataclasses import asdict +from datetime import datetime, timezone +from uuid import UUID + +from redis.exceptions import ResponseError + +from src.core import cache +from src.service.dto import StoredSessionDTO + + +def session_key(token_hash: str) -> str: + return f"session:v1:{token_hash}" + + +async def create_session(token_hash: str, session: StoredSessionDTO) -> None: + await cache.get_client().set( + session_key(token_hash), + json.dumps(asdict(session), default=str), + pxat=int(session.expires_at.timestamp() * 1000), + ) + + +async def fetch_session(token_hash: str) -> StoredSessionDTO | None: + key = session_key(token_hash) + try: + raw = await cache.get_client().get(key) + if raw is None: + return None + data = json.loads(raw) + if ( + not isinstance(data, dict) + or set(data) != {"user_id", "expires_at"} + or not all(isinstance(value, str) for value in data.values()) + ): + raise ValueError + session = StoredSessionDTO( + UUID(data["user_id"]), datetime.fromisoformat(data["expires_at"]) + ) + if session.expires_at.tzinfo is None or session.expires_at <= datetime.now(timezone.utc): + raise ValueError + return session + except ResponseError as error: + if not str(error).startswith("WRONGTYPE"): + raise + except (ValueError, KeyError, TypeError, UnicodeError): + pass + await cache.get_client().delete(key) + return None + + +async def delete_session(token_hash: str) -> None: + await cache.get_client().delete(session_key(token_hash)) diff --git a/src/core/settings.py b/src/core/settings.py new file mode 100644 index 00000000..ad47ea60 --- /dev/null +++ b/src/core/settings.py @@ -0,0 +1,95 @@ +from typing import Literal, Self + +from pydantic import Field, SecretStr, field_validator, model_validator +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class AppSettings(BaseSettings): + model_config = SettingsConfigDict( + env_file=".env", + env_file_encoding="utf-8", + extra="ignore", + hide_input_in_errors=True, + ) + + app_host: str = "0.0.0.0" + app_port: int = Field(default=8080, ge=1, le=65535) + app_env: Literal["lab", "production"] = "lab" + app_workers: int = Field(default=1, ge=1) + shutdown_timeout_seconds: int = Field(default=10, gt=0) + healthcheck_timeout_seconds: float = Field(default=2, gt=0) + http_max_body_bytes: int = Field(default=65536, ge=65536) + http_body_timeout_seconds: float = Field(default=5, gt=0) + http_concurrency_limit: int = Field(default=64, gt=0) + authorization_max_length: int = Field(default=2048, ge=1024) + cursor_max_length: int = Field(default=1024, ge=512) + password_hash_concurrency: int = Field(default=2, gt=0) + + database_url: SecretStr + database_pool_size: int = Field(default=10, gt=0) + database_max_overflow: int = Field(default=0, ge=0) + database_pool_timeout_seconds: float = Field(default=5, gt=0) + database_connect_timeout_seconds: float = Field(default=5, gt=0) + database_command_timeout_seconds: float = Field(default=5, gt=0) + database_statement_timeout_ms: int = Field(default=5000, gt=0) + database_pool_pre_ping: bool = True + + redis_url: SecretStr + redis_max_connections: int = Field(default=10, gt=0) + redis_connect_timeout_seconds: float = Field(default=0.3, gt=0) + redis_read_timeout_seconds: float = Field(default=0.3, gt=0) + redis_retry_on_timeout: bool = False + profile_cache_ttl_seconds: int = Field(default=60, gt=0) + + admin_username: str = Field(default="admin", pattern=r"^[a-z0-9_]{3,32}$") + admin_password: SecretStr = Field( + default=SecretStr("admin-password-123"), + min_length=12, + max_length=128, + ) + admin_display_name: str = Field(default="Администратор", min_length=1, max_length=100) + + session_ttl_seconds: int = Field(default=3600, gt=0) + session_token_bytes: int = Field(default=32, ge=32) + argon2_time_cost: int = Field(default=3, gt=0) + argon2_memory_cost: int = Field(default=65536, ge=8192) + argon2_parallelism: int = Field(default=4, gt=0) + argon2_salt_len: int = Field(default=16, ge=16) + argon2_hash_len: int = Field(default=32, ge=32) + + log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "INFO" + log_format: str = "%(asctime)s %(levelname)s %(name)s: %(message)s" + + @field_validator("admin_password") + @classmethod + def admin_password_must_not_be_blank(cls, value: SecretStr) -> SecretStr: + if not value.get_secret_value().strip(): + raise ValueError("Пароль администратора не может быть пробельным") + value.get_secret_value().encode("utf-8") + return value + + @field_validator("admin_display_name") + @classmethod + def admin_display_name_must_be_valid(cls, value: str) -> str: + if not value.strip() or "\x00" in value: + raise ValueError("Некорректное имя администратора") + return value + + @model_validator(mode="after") + def production_must_use_custom_password(self) -> Self: + if self.app_env == "production" and self.admin_password.get_secret_value() == ( + "admin-password-123" + ): + raise ValueError("Для production требуется собственный ADMIN_PASSWORD") + return self + + @property + def sqlalchemy_database_url(self) -> str: + return self.database_url.get_secret_value().replace( + "postgresql://", + "postgresql+asyncpg://", + 1, + ) + + +settings = AppSettings() diff --git a/src/main.py b/src/main.py new file mode 100644 index 00000000..cb10fd7b --- /dev/null +++ b/src/main.py @@ -0,0 +1,114 @@ +import logging +import time +import uuid + +from fastapi import FastAPI, Request +from fastapi.exceptions import RequestValidationError +from fastapi.responses import JSONResponse +from redis.exceptions import RedisError +from sqlalchemy.exc import DBAPIError +from sqlalchemy.exc import TimeoutError as DatabaseTimeoutError +from starlette.exceptions import HTTPException as StarletteHTTPException + +from src.api.router import router as api_router +from src.core import readiness +from src.core.callback import lifespan +from src.core.errors import ApiError, error_response +from src.core.request_limits import RequestLimitsMiddleware +from src.core.settings import settings + +logging.basicConfig(level=settings.log_level, format=settings.log_format) + +INSTANCE_ID = str(uuid.uuid4()) + +app = FastAPI(lifespan=lifespan) +app.add_middleware(RequestLimitsMiddleware) +app.include_router(api_router) +logger = logging.getLogger(__name__) + + +@app.middleware("http") +async def add_request_id(request: Request, call_next): + request_id = str(uuid.uuid4()) + request.state.request_id = request_id + started_at = time.perf_counter() + + try: + response = await call_next(request) + except Exception: + logger.exception( + "Необработанная ошибка запроса", + extra={"request_id": request_id}, + ) + + response = JSONResponse( + status_code=500, + content={ + "error": { + "code": "internal_error", + "message": "Внутренняя ошибка сервера", + "request_id": request_id, + } + }, + ) + + response.headers["X-Request-Id"] = request_id + response.headers["X-Instance-Id"] = INSTANCE_ID + if request.url.path.startswith("/api/v1/"): + response.headers["Cache-Control"] = "no-store" + response.headers["X-Content-Type-Options"] = "nosniff" + logger.info( + "ид_запроса=%s метод=%s путь=%r статус=%s длительность_мс=%.3f", + request_id, + request.method, + request.url.path, + response.status_code, + (time.perf_counter() - started_at) * 1000, + ) + return response + + +@app.exception_handler(ApiError) +async def handle_api_error(request: Request, exc: ApiError) -> JSONResponse: + response = error_response(request, exc.status_code, exc.code, exc.message) + if exc.status_code == 401: + response.headers["WWW-Authenticate"] = ( + 'Basic realm="messenger", charset="UTF-8"' + if request.url.path == "/api/v1/auth/sessions" + else 'Bearer realm="messenger"' + ) + return response + + +@app.exception_handler(RequestValidationError) +async def handle_validation_error(request: Request, exc: RequestValidationError) -> JSONResponse: + return error_response(request, 400, "invalid_request", "Некорректные параметры запроса") + + +@app.exception_handler(StarletteHTTPException) +async def handle_http_error(request: Request, exc: StarletteHTTPException) -> JSONResponse: + code = "not_found" if exc.status_code == 404 else "invalid_request" + message = "Объект не найден" if exc.status_code == 404 else "Некорректный запрос" + return error_response(request, exc.status_code, code, message) + + +@app.get("/health/live") +async def get_health(): + return { + "status": "ok", + } + + +@app.get("/health/ready") +async def get_ready(): + await readiness.check() + return {"status": "ok"} + + +async def handle_database_error(request: Request, exc: Exception) -> JSONResponse: + logger.warning("Хранилище временно недоступно: тип_ошибки=%s", type(exc).__name__) + return error_response(request, 503, "unavailable", "Хранилище временно недоступно") + + +for error_type in (RedisError, DBAPIError, DatabaseTimeoutError, OSError, TimeoutError): + app.add_exception_handler(error_type, handle_database_error) diff --git a/src/service/__init__.py b/src/service/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/service/auth.py b/src/service/auth.py new file mode 100644 index 00000000..425f2856 --- /dev/null +++ b/src/service/auth.py @@ -0,0 +1,74 @@ +import logging +import secrets +from datetime import datetime, timedelta, timezone + +from sqlalchemy.ext.asyncio import AsyncSession + +from src.core import session_store +from src.core.db.repo import auth as auth_repo +from src.core.db.repo import user as user_repo +from src.core.security import hash_password, hash_token, verify_password_async +from src.core.settings import settings +from src.service.dto import AuthSessionDTO, StoredSessionDTO, UserDTO +from src.service.exceptions import InvalidCredentialsException, InvalidSessionException + +logger = logging.getLogger(__name__) +# Для неизвестного логина тоже выполняем Argon2, чтобы не отвечать заметно быстрее. +_DUMMY_PASSWORD_HASH = hash_password("dummy-password-for-timing") + + +class AuthService: + def __init__( + self, + db_session: AsyncSession, + *, + session_ttl_seconds: int, + ) -> None: + self._db_session = db_session + self._session_ttl = timedelta(seconds=session_ttl_seconds) + + async def login(self, *, username: str, password: str) -> AuthSessionDTO: + credentials = await auth_repo.fetch_user_credentials( + session=self._db_session, + username=username, + ) + password_hash = credentials.password_hash if credentials else _DUMMY_PASSWORD_HASH + password_valid = await verify_password_async(password, password_hash) + if credentials is None or not password_valid: + logger.warning("Вход отклонён: неверный логин или пароль") + raise InvalidCredentialsException("Неверный логин или пароль") + + user = credentials.user + token = secrets.token_urlsafe(settings.session_token_bytes) + expires_at = datetime.now(timezone.utc) + self._session_ttl + await session_store.create_session( + token_hash=hash_token(token), + session=StoredSessionDTO(user_id=user.id, expires_at=expires_at), + ) + logger.info("Сессия создана: пользователь=%s", user.id) + return AuthSessionDTO( + token=token, + expires_at=expires_at, + user=user, + ) + + async def get_user(self, *, token: str) -> UserDTO: + session = await session_store.fetch_session(token_hash=hash_token(token)) + if session is None: + logger.info("Доступ отклонён: сессия отсутствует или истекла") + raise InvalidSessionException("Нет действующей сессии") + + user = await user_repo.fetch_user_by_id( + session=self._db_session, + user_id=session.user_id, + ) + if user is None: + logger.warning("Доступ отклонён: пользователь сессии не найден") + raise InvalidSessionException("Нет действующей сессии") + + logger.debug("Сессия подтверждена: пользователь=%s", user.id) + return user + + async def logout(self, *, token: str) -> None: + await session_store.delete_session(token_hash=hash_token(token)) + logger.info("Сессия завершена") diff --git a/src/service/chat.py b/src/service/chat.py new file mode 100644 index 00000000..97ce519c --- /dev/null +++ b/src/service/chat.py @@ -0,0 +1,61 @@ +from datetime import datetime, timezone +from uuid import UUID, uuid4 + +from sqlalchemy.ext.asyncio import AsyncSession + +from src.core.db.repo import chat as chat_repo +from src.core.db.repo import user as user_repo +from src.service.dto import ChatDTO +from src.service.exceptions import ChatNotFoundException, InvalidChatMembersException + + +class ChatService: + def __init__(self, db_session: AsyncSession) -> None: + self._db_session = db_session + + async def create_chat( + self, + *, + title: str, + member_ids: list[UUID], + current_user_id: UUID, + ) -> ChatDTO: + if current_user_id not in member_ids: + raise InvalidChatMembersException("Создатель должен быть в списке member_ids") + if not await user_repo.users_exist( + session=self._db_session, + user_ids=member_ids, + ): + raise InvalidChatMembersException("Указан неизвестный пользователь") + + return await chat_repo.create_chat( + session=self._db_session, + chat_id=uuid4(), + title=title, + member_ids=member_ids, + created_at=datetime.now(timezone.utc), + ) + + async def get_chat(self, *, chat_id: UUID, current_user_id: UUID) -> ChatDTO: + chat = await chat_repo.fetch_chat_by_id( + session=self._db_session, + chat_id=chat_id, + ) + self._check_access(chat=chat, user_id=current_user_id) + return chat + + async def get_chats( + self, + *, + current_user_id: UUID, + limit: int, + after: tuple[datetime, UUID] | None = None, + ) -> list[ChatDTO]: + return await chat_repo.fetch_chats( + session=self._db_session, user_id=current_user_id, limit=limit, after=after + ) + + @staticmethod + def _check_access(*, chat: ChatDTO | None, user_id: UUID) -> None: + if chat is None or user_id not in chat.member_ids: + raise ChatNotFoundException("Чат не найден") diff --git a/src/service/dto.py b/src/service/dto.py new file mode 100644 index 00000000..1d6b0280 --- /dev/null +++ b/src/service/dto.py @@ -0,0 +1,49 @@ +from dataclasses import dataclass, field +from datetime import datetime +from typing import Literal +from uuid import UUID + + +@dataclass(frozen=True, slots=True) +class UserDTO: + id: UUID + username: str + display_name: str + created_at: datetime + + +@dataclass(frozen=True, slots=True) +class ChatDTO: + id: UUID + title: str + member_ids: tuple[UUID, ...] + created_at: datetime + + +@dataclass(frozen=True, slots=True) +class MessageDTO: + id: UUID + chat_id: UUID + sender_id: UUID + text: str + created_at: datetime + + +@dataclass(frozen=True, slots=True) +class AuthSessionDTO: + token: str = field(repr=False) + expires_at: datetime + user: UserDTO + token_type: Literal["Bearer"] = "Bearer" + + +@dataclass(frozen=True, slots=True) +class StoredSessionDTO: + user_id: UUID + expires_at: datetime + + +@dataclass(frozen=True, slots=True) +class UserCredentialsDTO: + user: UserDTO + password_hash: str = field(repr=False) diff --git a/src/service/exceptions.py b/src/service/exceptions.py new file mode 100644 index 00000000..4c682db7 --- /dev/null +++ b/src/service/exceptions.py @@ -0,0 +1,30 @@ +class IdempotencyConflictException(Exception): + pass + + +class ServiceException(Exception): + pass + + +class UserAlreadyExistsException(ServiceException): + pass + + +class UserNotFoundException(ServiceException): + pass + + +class InvalidChatMembersException(ServiceException): + pass + + +class ChatNotFoundException(ServiceException): + pass + + +class InvalidCredentialsException(ServiceException): + pass + + +class InvalidSessionException(ServiceException): + pass diff --git a/src/service/message.py b/src/service/message.py new file mode 100644 index 00000000..afe7ae9d --- /dev/null +++ b/src/service/message.py @@ -0,0 +1,62 @@ +from datetime import datetime, timezone +from uuid import UUID, uuid4 + +from sqlalchemy.ext.asyncio import AsyncSession + +from src.core.db.repo import chat as chat_repo +from src.core.db.repo import message as message_repo +from src.service.dto import ChatDTO, MessageDTO +from src.service.exceptions import ChatNotFoundException + + +class MessageService: + def __init__(self, db_session: AsyncSession) -> None: + self._db_session = db_session + + async def create_message( + self, + *, + chat_id: UUID, + sender_id: UUID, + text: str, + idempotency_key: str, + ) -> MessageDTO: + chat = await chat_repo.fetch_chat_by_id( + session=self._db_session, + chat_id=chat_id, + ) + self._check_access(chat=chat, user_id=sender_id) + return await message_repo.create_idempotent_message( + session=self._db_session, + message_id=uuid4(), + idempotency_key=idempotency_key, + chat_id=chat_id, + sender_id=sender_id, + text=text, + created_at=datetime.now(timezone.utc), + ) + + async def get_messages( + self, + *, + chat_id: UUID, + current_user_id: UUID, + limit: int, + after: tuple[datetime, UUID] | None = None, + ) -> list[MessageDTO]: + chat = await chat_repo.fetch_chat_by_id( + session=self._db_session, + chat_id=chat_id, + ) + self._check_access(chat=chat, user_id=current_user_id) + return await message_repo.fetch_messages_by_chat_id( + session=self._db_session, + chat_id=chat_id, + limit=limit, + after=after, + ) + + @staticmethod + def _check_access(*, chat: ChatDTO | None, user_id: UUID) -> None: + if chat is None or user_id not in chat.member_ids: + raise ChatNotFoundException("Чат не найден") diff --git a/src/service/user.py b/src/service/user.py new file mode 100644 index 00000000..05b6882e --- /dev/null +++ b/src/service/user.py @@ -0,0 +1,63 @@ +from datetime import datetime, timezone +from uuid import UUID, uuid4 + +from sqlalchemy.exc import IntegrityError +from sqlalchemy.ext.asyncio import AsyncSession + +from src.core.db.repo import user as user_repo +from src.core.security import hash_password_async +from src.service.dto import UserDTO +from src.service.exceptions import UserAlreadyExistsException, UserNotFoundException + + +class UserService: + def __init__(self, db_session: AsyncSession) -> None: + self._db_session = db_session + + async def create_user(self, *, username: str, display_name: str, password: str) -> UserDTO: + password_hash = await hash_password_async(password) + try: + return await user_repo.create_user( + session=self._db_session, + user_id=uuid4(), + username=username, + display_name=display_name, + created_at=datetime.now(timezone.utc), + password_hash=password_hash, + ) + except IntegrityError as error: + if getattr(error.orig, "sqlstate", None) != "23505": + raise + raise UserAlreadyExistsException("Пользователь с таким именем уже существует") from None + + async def create_admin( + self, + *, + username: str, + display_name: str, + password: str, + ) -> UserDTO | None: + password_hash = await hash_password_async(password) + user = await user_repo.create_admin( + session=self._db_session, + user_id=uuid4(), + username=username, + display_name=display_name, + password_hash=password_hash, + created_at=datetime.now(timezone.utc), + ) + if user is None and not await user_repo.admin_exists( + session=self._db_session, + username=username, + ): + raise UserAlreadyExistsException("Имя администратора занято обычным пользователем") + return user + + async def get_user_by_id(self, *, user_id: UUID) -> UserDTO: + user = await user_repo.fetch_user_by_id( + session=self._db_session, + user_id=user_id, + ) + if user is None: + raise UserNotFoundException("Пользователь не найден") + return user diff --git a/tests/auth_logging.py b/tests/auth_logging.py new file mode 100644 index 00000000..66160eaf --- /dev/null +++ b/tests/auth_logging.py @@ -0,0 +1,36 @@ +"""Проверка русских событий авторизации и отсутствия секретов в логах Compose.""" +import subprocess +import base64 +from datetime import datetime, timezone +from pathlib import Path + +from client import Client, arguments, check, run + + +def main(): + args = arguments('Проверка логов авторизации') + started = datetime.now(timezone.utc).isoformat().replace('+00:00', 'Z') + client = Client(args.base_url, args.ca_file) + actor = client.user('auth_logs') + username = actor['registration']['username'] + wrong_password = 'wrong-password-for-logging' + client.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': 'Basic ' + base64.b64encode((username + ':' + wrong_password).encode()).decode()}) + missing_username = 'missing_' + actor['id'].replace('-', '')[:12] + client.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': 'Basic ' + base64.b64encode((missing_username + ':' + wrong_password).encode()).decode()}) + client.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=actor) + client.call('GET', '/api/v1/users/me', 401, actor=actor) + result = subprocess.run( + ['docker', 'compose', 'logs', '--no-color', '--since', started, 'api'], + cwd=Path(__file__).resolve().parents[1], check=True, capture_output=True, text=True, + ) + logs = result.stdout + result.stderr + check('src.service.auth' in logs, 'Не используется обычный логгер модуля') + for message in ('Сессия создана', 'Вход отклонён', 'Сессия завершена', 'Доступ отклонён'): + check(message in logs, 'В логах отсутствует событие авторизации') + for secret in (actor['token'], actor['registration']['password'], wrong_password): + check(secret not in logs, 'В логах обнаружены credentials') + print('PASS: русские события входа/отказа/logout; пароли и токены не попали в логи') + + +if __name__ == '__main__': + run(main) diff --git a/tests/callbacks.py b/tests/callbacks.py new file mode 100644 index 00000000..264ca119 --- /dev/null +++ b/tests/callbacks.py @@ -0,0 +1,107 @@ +"""docker compose exec -T api python < tests/callbacks.py.""" +import asyncio +import base64 +import json +from uuid import uuid4 +from urllib.error import HTTPError +from urllib.request import Request, urlopen +from unittest.mock import patch + +from pydantic import SecretStr + +from sqlalchemy import delete, func, select +from sqlalchemy.ext.asyncio import async_sessionmaker + +from src.api.v1.routers.auth.callback import create_admin +from src.core.db.models import User +from src.core.db.session import create_engine +from src.core.settings import settings +from src.service.exceptions import UserAlreadyExistsException +from src.service.user import UserService + + +def call(method, path, expected, headers=None, body=None): + data = None if body is None else json.dumps(body).encode() + request_headers = dict(headers or {}) + if body is not None: + request_headers['Content-Type'] = 'application/json' + request = Request(f'http://127.0.0.1:{settings.app_port}' + path, + method=method, headers=request_headers, data=data) + try: + response = urlopen(request, timeout=10) + except HTTPError as error: + response = error + with response: + assert response.status == expected, response.status + return json.load(response) if response.status != 204 else None + + +async def main(): + engine = create_engine() + factory = async_sessionmaker(engine, expire_on_commit=False) + try: + async with factory() as session: + admin = await session.scalar(select(User).where(User.username == settings.admin_username)) + assert admin is not None and admin.is_admin + identity = (admin.id, admin.password_hash, admin.created_at) + await asyncio.gather(create_admin(factory), create_admin(factory)) + async with factory() as session: + admin = await session.scalar(select(User).where(User.username == settings.admin_username)) + assert (admin.id, admin.password_hash, admin.created_at) == identity + assert await session.scalar(select(func.count()).select_from(User).where( + User.username == settings.admin_username, + )) == 1 + print('PASS: стартовый администратор создан; повторные callbacks не дублируют и не меняют пароль') + with patch.object(settings, 'app_env', 'production'), patch.object( + settings, 'admin_password', SecretStr('different-production-password-123') + ): + try: + await create_admin(factory) + except RuntimeError: + pass + else: + raise AssertionError('Production запущен с несовпадающим паролем в БД') + print('PASS: production не принимает старый пароль администратора при изменённой конфигурации') + + credential = base64.b64encode((settings.admin_username + ':' + settings.admin_password.get_secret_value()).encode()).decode() + result = call('POST', '/api/v1/auth/sessions', 201, headers={'Authorization': 'Basic ' + credential}) + bearer = {'Authorization': 'Bearer ' + result['token']} + me = call('GET', '/api/v1/users/me', 200, bearer) + assert me['id'] == str(identity[0]) + call('DELETE', '/api/v1/auth/sessions/current', 204, bearer) + print('PASS: администратор входит через Basic, читает профиль через Bearer и выходит') + + username = 'seed_test_' + uuid4().hex[:12] + password = 'valid-password-for-seed-test' + normal = call('POST', '/api/v1/users', 201, body={ + 'username': username, 'display_name': 'Обычный пользователь', 'password': password, + }) + try: + try: + async with factory.begin() as session: + await UserService(session).create_admin( + username=username, display_name='Администратор', password=password, + ) + except UserAlreadyExistsException: + pass + else: + raise AssertionError('Обычный пользователь стал администратором') + async with factory() as session: + assert not await session.scalar(select(User.is_admin).where(User.username == username)) + finally: + from uuid import UUID + async with factory.begin() as session: + await session.execute(delete(User).where(User.id == UUID(normal['id']))) + print('PASS: callback не повышает права обычного пользователя с совпавшим username') + + base = {'username': 'constraints_' + uuid4().hex[:12], 'display_name': 'Проверка', + 'password': password} + call('POST', '/api/v1/users', 400, body={**base, 'username': 'user@example.com'}) + call('POST', '/api/v1/users', 400, body={**base, 'password': 'admin'}) + call('POST', '/api/v1/users', 400, body={**base, 'is_admin': True}) + print('PASS: ограничения username/password сохранены; is_admin нельзя передать при регистрации') + finally: + await engine.dispose() + + +asyncio.run(main()) diff --git a/tests/database.py b/tests/database.py new file mode 100644 index 00000000..253fedf1 --- /dev/null +++ b/tests/database.py @@ -0,0 +1,87 @@ +"""docker compose exec -T api python < tests/database.py.""" +import asyncio +import subprocess +import sys +from datetime import datetime, timedelta, timezone +from uuid import uuid4 + +from sqlalchemy import Column, MetaData, String, Table, func, insert, select, text +from sqlalchemy.exc import IntegrityError +from sqlalchemy.ext.asyncio import async_sessionmaker + +from src.core.db.models import Chat, ChatMember, Message, User +from src.core.db.repo.chat import create_chat +from src.core.db.repo.message import history_query +from src.core.db.session import create_engine + + +async def main(): + engine = create_engine() + factory = async_sessionmaker(engine, expire_on_commit=False) + try: + async def counts(): + async with factory() as session: + return [await session.scalar(select(func.count()).select_from(model)) + for model in (User, Chat, ChatMember, Message)] + + before = await counts() + for _ in range(2): + subprocess.run([sys.executable, '-m', 'alembic', 'upgrade', 'head'], check=True) + assert before == await counts(), 'Повтор миграций изменил данные' + versions = Table('alembic_version', MetaData(), Column('version_num', String(32), primary_key=True)) + async with factory() as session: + assert (await session.scalars(select(versions.c.version_num))).all() == ['006'] + print('PASS: два повторных запуска миграций не изменили данные; версия 006 одна') + + user_id, chat_id = uuid4(), uuid4() + now = datetime.now(timezone.utc) + try: + async with factory.begin() as session: + await create_chat(session, chat_id, 'rollback', [user_id], now) + except IntegrityError as error: + assert getattr(error.orig, 'sqlstate', None) == '23503' + else: + raise AssertionError('Ожидался отказ внешнего ключа') + async with factory() as session: + assert await session.get(Chat, chat_id) is None + assert await session.scalar( + select(func.count()).select_from(ChatMember).where(ChatMember.chat_id == chat_id) + ) == 0 + print('PASS: ошибка участника откатила INSERT чата и участников') + + async with factory() as session: + try: + session.add(User(id=user_id, username='plan_' + uuid4().hex[:16], + display_name='EXPLAIN', password_hash='test-not-for-login', created_at=now)) + await session.flush() + await create_chat(session, chat_id, 'EXPLAIN', [user_id], now) + await session.execute(insert(Message), [ + dict(id=uuid4(), chat_id=chat_id, sender_id=user_id, text=f'message {n}', + created_at=now + timedelta(milliseconds=n)) for n in range(1, 10001) + ]) + # ANALYZE и EXPLAIN — диагностические команды PostgreSQL, не запросы репозитория. + await session.execute(text('ANALYZE messages')) + count = await session.scalar( + select(func.count()).select_from(Message).where(Message.chat_id == chat_id) + ) + print('Сообщений в исследуемом чате:', count) + after = (await session.execute( + select(Message.created_at, Message.id).where(Message.chat_id == chat_id) + .order_by(Message.created_at, Message.id).offset(8999).limit(1) + )).one() + # Исследуется именно выражение SQLAlchemy, используемое в репозитории. + compiled = history_query(chat_id, 51, tuple(after)).compile( + dialect=engine.dialect, compile_kwargs={'literal_binds': True}, + ) + rows = await session.execute(text('EXPLAIN (ANALYZE, BUFFERS) ' + str(compiled))) + plan = '\n'.join(row[0] for row in rows) + assert 'messages_history_idx' in plan, plan + print(plan) + print('PASS: индекс истории используется для глубокой страницы из 10000 сообщений') + finally: + await session.rollback() + finally: + await engine.dispose() + + +asyncio.run(main()) diff --git a/tests/dependencies.py b/tests/dependencies.py new file mode 100644 index 00000000..73df39ba --- /dev/null +++ b/tests/dependencies.py @@ -0,0 +1,160 @@ +"""Проверка отказов только локального Compose-стенда; сервисы восстанавливаются.""" + +import hashlib +import json +import subprocess +import time +from pathlib import Path + +from client import Client, Failure, arguments, run + +ROOT = Path(__file__).resolve().parents[1] + + +def compose(*args): + subprocess.run( + ["docker", "compose", *args], cwd=ROOT, check=True, stdout=subprocess.DEVNULL, timeout=60 + ) + + +def main(): + args = arguments("Отказы Redis и PostgreSQL") + c = Client(args.base_url, args.ca_file) + actor = c.user("outage") + c.call("GET", "/api/v1/users/me", actor=actor, schema="User") + key = "user:v1:" + actor["id"] + + def redis(*args): + return subprocess.check_output( + ["docker", "compose", "exec", "-T", "redis", "redis-cli", "--raw", *args], + cwd=ROOT, + text=True, + timeout=10, + ).strip() + + assert redis("EXISTS", key) == "1", "Профиль не попал в Redis" + assert 0 < int(redis("TTL", key)) <= 60 + + def hits(): + return int( + next( + line.split(":")[1] + for line in redis("INFO", "stats").splitlines() + if line.startswith("keyspace_hits:") + ) + ) + + before = hits() + c.call("GET", "/api/v1/users/me", actor=actor, schema="User") + assert hits() > before, "Повторное чтение не использовало Redis" + redis("DEL", key) + c.call("GET", "/api/v1/users/me", actor=actor, schema="User") + assert redis("EXISTS", key) == "1", "Кэш не восстановился из PostgreSQL" + token_key = "session:v1:" + hashlib.sha256(actor["token"].encode()).hexdigest() + assert redis("EXISTS", token_key) == "1", "Сессия отсутствует в Redis" + ttl_before = int(redis("PTTL", token_key)) + c.call("GET", "/api/v1/users/me", actor=actor, schema="User") + assert 0 < int(redis("PTTL", token_key)) <= ttl_before, "Чтение продлило сессию" + print("PASS: сессия хранится в Redis с TTL; чтение не продлевает срок") + print( + "PASS: профиль кэшируется с TTL<=60; повторный GET даёт cache hit; потеря ключа восполняется" + ) + lost = c.user("lost") + c.call("GET", "/api/v1/users/me", actor=lost, schema="User") + lost_key = "session:v1:" + hashlib.sha256(lost["token"].encode()).hexdigest() + redis("DEL", lost_key) + c.call("GET", "/api/v1/users/me", 401, actor=lost) + assert redis("EXISTS", lost_key) == "0", "Удалённая сессия воскресла" + print("PASS: отсутствующая сессия не восстанавливается из профиля/БД; Redis — источник истины") + # При общей неисправности LB исключает все API. Проверку отдельного + # процесса выполняем из контейнера; внешние API-порты не открываем. + container = subprocess.check_output( + ["docker", "compose", "ps", "-q", "api"], cwd=ROOT, text=True + ).split()[0] + + def probe(method, path, expected=200, actor=None, body=None, schema=None): + code = r""" +import json, sys, urllib.request, urllib.error +p=json.load(sys.stdin) +h={"Content-Type":"application/json"} +if p["actor"]: h["Authorization"]="Bearer "+p["actor"]["token"] +r=urllib.request.Request("http://127.0.0.1:8080"+p["path"], method=p["method"], headers=h, + data=json.dumps(p["body"]).encode() if p["body"] is not None else None) +try: response=urllib.request.urlopen(r, timeout=6) +except urllib.error.HTTPError as e: response=e +with response: + assert response.headers.get("X-Request-Id") and response.headers.get("X-Instance-Id") + print(response.status) +""" + result = subprocess.check_output( + ["docker", "exec", "-i", container, "python", "-c", code], + cwd=ROOT, + text=True, + input=json.dumps(dict(method=method, path=path, actor=actor, body=body)), + ) + assert int(result.strip()) == expected, (method, path, expected, result) + + def wait_lb(): + deadline = time.monotonic() + 30 + while True: + try: + c.call("GET", "/health/ready", schema="Health") + return + except Failure: + if time.monotonic() > deadline: + raise + time.sleep(0.25) + + try: + compose("stop", "redis") + probe("GET", "/health/live", schema="Health") + probe("GET", "/api/v1/users/me", 503, actor=actor) + probe("DELETE", "/api/v1/auth/sessions/current", 503, actor=actor) + probe("GET", "/health/ready", 503) + probe("GET", "/api/v1/users/me", 503, actor={"token": "invalid-token"}) + probe("GET", "/api/v1/users/me", 401) + print( + "PASS: Redis выключен; live=200, ready=503; проверка сессий недоступна, доступ не выдаётся" + ) + finally: + compose("up", "-d", "--wait", "redis") + wait_lb() + probe("GET", "/api/v1/users/me", actor=actor, schema="User") + print("PASS: сессия сохранилась после перезапуска Redis") + revoked = c.user("revoked") + probe("GET", "/api/v1/users/me", actor=revoked, schema="User") + probe("DELETE", "/api/v1/auth/sessions/current", 204, actor=revoked) + compose("restart", "redis") + compose("up", "-d", "--wait", "redis") + probe("GET", "/api/v1/users/me", 401, actor=revoked) + probe( + "POST", + "/api/v1/chats", + 401, + actor=revoked, + body={"title": "Нет доступа", "member_ids": [revoked["id"]]}, + ) + print( + "PASS: logout при отказе Redis возвращает 503; успешный отзыв переживает AOF-рестарт и не даёт доступ" + ) + try: + compose("stop", "postgres") + probe("GET", "/health/live", schema="Health") + probe("GET", "/health/ready", 503) + probe("GET", "/api/v1/chats", 503, actor=actor) + probe("GET", "/api/v1/users/me", 401) + probe("GET", "/api/v1/users/me", 401, actor={"token": "invalid-token"}) + probe("GET", "/api/v1/users/me", actor=actor, schema="User") + print( + "PASS: PostgreSQL выключен; live=200, ready=503, чаты=503; действующая сессия и прогретый профиль доступны в Redis" + ) + finally: + compose("up", "-d", "--wait", "postgres") + probe("GET", "/api/v1/users/me", actor=actor, schema="User") + wait_lb() + c.call("GET", "/api/v1/users/me", actor=actor, schema="User") + print("PASS: после восстановления БД прежняя сессия снова работает через HAProxy") + + +if __name__ == "__main__": + run(main) diff --git a/tests/error_headers.py b/tests/error_headers.py new file mode 100644 index 00000000..025d2366 --- /dev/null +++ b/tests/error_headers.py @@ -0,0 +1,53 @@ +"""Неожиданное исключение не раскрывает детали и сохраняет обязательные headers. +Маршрут добавлен только в этот изолированный процесс, не в работающий API. +""" + +import asyncio +import json + +from src.main import INSTANCE_ID, app + + +@app.get("/_audit_unexpected_error") +async def unexpected(): + raise RuntimeError("audit-internal-detail") + + +async def main(): + messages = [] + + async def receive(): + await asyncio.Event().wait() + + async def send(message): + messages.append(message) + + scope = { + "type": "http", + "asgi": {"version": "3.0", "spec_version": "2.4"}, + "http_version": "1.1", + "method": "GET", + "scheme": "http", + "path": "/_audit_unexpected_error", + "raw_path": b"/_audit_unexpected_error", + "query_string": b"", + "root_path": "", + "headers": [], + "server": ("localhost", 80), + "client": ("127.0.0.1", 1), + } + await app(scope, receive, send) + start = next(m for m in messages if m["type"] == "http.response.start") + payload = json.loads( + b"".join(m.get("body", b"") for m in messages if m["type"] == "http.response.body") + ) + headers = dict(start["headers"]) + assert start["status"] == 500 + assert headers[b"x-instance-id"].decode() == INSTANCE_ID + assert payload["error"]["request_id"] == headers[b"x-request-id"].decode() + assert payload["error"]["code"] == "internal_error" + assert "audit-internal-detail" not in json.dumps(payload) + print("PASS: 500 has X-Request-Id/X-Instance-Id and hides internal exception details") + + +asyncio.run(main()) diff --git a/tests/expiry.py b/tests/expiry.py new file mode 100644 index 00000000..c5bc8b09 --- /dev/null +++ b/tests/expiry.py @@ -0,0 +1,17 @@ +"""Отдельный стенд/запуск с SESSION_TTL_SECONDS=2, не обычный smoke.""" +import time +from client import Client, arguments, run + + +def main(): + args = arguments('Истечение короткой сессии') + c = Client(args.base_url, args.ca_file) + actor = c.user('expiry') + c.call('GET', '/api/v1/users/me', actor=actor, schema='User') + time.sleep(3) + c.call('GET', '/api/v1/users/me', 401, actor=actor) + print('PASS: действующая сессия стала 401 через 3 секунды при TTL=2') + + +if __name__ == '__main__': + run(main) diff --git a/tests/extra.py b/tests/extra.py new file mode 100644 index 00000000..bce67089 --- /dev/null +++ b/tests/extra.py @@ -0,0 +1,111 @@ +from concurrent.futures import ThreadPoolExecutor +import json +import base64 +import uuid +from urllib.parse import quote + +from client import Client, arguments, check, run + + +def main(): + args = arguments("Собственные тесты мессенджера") + client = Client(args.base_url, args.ca_file) + alice, bob, eve, chat = client.bootstrap() + chat_path = "/api/v1/chats/" + chat["id"] + + client.call( + "POST", + "/api/v1/users", + 400, + raw=b'{"username":', + headers={"Content-Type": "application/json"}, + ) + client.call("POST", "/api/v1/users", 409, body=alice["registration"]) + client.call( + "POST", + chat_path + "/messages", + 400, + actor=alice, + body={"text": " "}, + ) + client.call("GET", chat_path, 404, actor=eve) + + # Уникальность защищена ограничением БД, в том числе при гонке. + registration = { + 'username': 'race_' + uuid.uuid4().hex[:16], + 'display_name': 'Гонка регистрации', + 'password': 'Пароль:с-двоеточием-123', + } + def register(_): + local = Client(args.base_url, args.ca_file) + return local.call('POST', '/api/v1/users', [201, 409], body=registration)['status'] + with ThreadPoolExecutor(max_workers=6) as pool: + statuses = list(pool.map(register, range(6))) + check(statuses.count(201) == 1 and statuses.count(409) == 5, 'гонка регистрации не защищена') + session = client.call('POST', '/api/v1/auth/sessions', 201, + headers={'Authorization': 'Basic ' + base64.b64encode((registration['username'] + ':' + registration['password']).encode()).decode()}, schema='Session')['json'] + actor = {'token': session['token']} + client.call('GET', '/api/v1/users/me', actor=actor, schema='User') + client.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=actor) + # Даже прогретый Redis не должен разрешать отозванную сессию. + client.call('GET', '/api/v1/users/me', 401, actor=actor) + + def send_batch(worker_id): + local = Client(args.base_url, args.ca_file) + result = [] + for index in range(10): + response = local.message( + alice, + chat, + "параллельное-%d-%d" % (worker_id, index), + )["json"] + result.append(response["id"]) + return result + + with ThreadPoolExecutor(max_workers=10) as pool: + created_ids = [ + message_id + for batch in pool.map(send_batch, range(10)) + for message_id in batch + ] + + check(len(created_ids) == 100, "не все параллельные запросы вернули ответ") + check(len(set(created_ids)) == 100, "параллельные запросы вернули повторяющиеся ID") + + received = [] + cursor = None + while True: + path = chat_path + "/messages?limit=17" + if cursor is not None: + path += "&cursor=" + quote(cursor, safe="") + page = client.call("GET", path, actor=bob, schema="MessagePage")["json"] + received.extend(page["items"]) + cursor = page["next_cursor"] + if cursor is None: + break + + concurrent_messages = [ + message for message in received if message["text"].startswith("параллельное-") + ] + received_ids = [message["id"] for message in concurrent_messages] + check(len(received_ids) == 100, "в истории нет ровно 100 параллельно созданных сообщений") + check(len(set(received_ids)) == 100, "в истории есть повторяющиеся сообщения") + check(set(received_ids) == set(created_ids), "ID созданных и прочитанных сообщений не совпадают") + + print( + json.dumps( + { + "результат": "ПРОЙДЕНО", + "гонка_регистрации": "1 создан, 5 конфликтов", + "unicode_пароль_и_logout_с_кэшем": "ПРОЙДЕНО", + "параллельных_клиентов": 10, + "создано_сообщений": len(created_ids), + "уникальных_сообщений": len(set(received_ids)), + }, + ensure_ascii=False, + ) + ) + + +if __name__ == "__main__": + run(main) diff --git a/tests/idempotency_db.py b/tests/idempotency_db.py new file mode 100644 index 00000000..2d0d6d00 --- /dev/null +++ b/tests/idempotency_db.py @@ -0,0 +1,79 @@ +"""Запуск внутри API: rollback и 24h expiry без ожидания суток.""" + +import asyncio +from datetime import datetime, timezone +from uuid import uuid4 + +from sqlalchemy import delete, select, text, update +from sqlalchemy.ext.asyncio import async_sessionmaker + +from src.core.db.models import Chat, ChatMember, Message, MessageRequest, User +from src.core.db.repo.message import create_idempotent_message +from src.core.db.session import create_engine + + +async def main(): + engine = create_engine() + factory = async_sessionmaker(engine, expire_on_commit=False) + user_id, chat_id = uuid4(), uuid4() + now = datetime.now(timezone.utc) + async with factory.begin() as s: + s.add( + User( + id=user_id, + username="idem_" + user_id.hex[:20], + display_name="Fixture", + password_hash="not-for-login", + created_at=now, + ) + ) + await s.flush() + s.add(Chat(id=chat_id, title="Idempotency fixture", created_at=now)) + await s.flush() + s.add(ChatMember(chat_id=chat_id, user_id=user_id)) + try: + try: + async with factory.begin() as s: + await create_idempotent_message( + s, uuid4(), chat_id, user_id, "rollback", now, "same" + ) + raise RuntimeError("simulated failure before COMMIT") + except RuntimeError: + pass + async with factory() as s: + assert not list(await s.scalars(select(Message).where(Message.chat_id == chat_id))) + assert not list( + await s.scalars(select(MessageRequest).where(MessageRequest.chat_id == chat_id)) + ) + async with factory.begin() as s: + first = await create_idempotent_message( + s, uuid4(), chat_id, user_id, "committed", now, "same" + ) + async with factory.begin() as s: + replay = await create_idempotent_message( + s, uuid4(), chat_id, user_id, "committed", now, "same" + ) + assert replay == first + await s.execute( + update(MessageRequest) + .where(MessageRequest.chat_id == chat_id) + .values(expires_at=text("clock_timestamp() - interval '1 second'")) + ) + async with factory.begin() as s: + fresh = await create_idempotent_message( + s, uuid4(), chat_id, user_id, "new after expiry", now, "same" + ) + assert fresh.id != first.id + print( + "PASS: rollback removes both message/key; committed result replays; expired key can be reused" + ) + finally: + async with factory.begin() as s: + await s.execute(delete(MessageRequest).where(MessageRequest.chat_id == chat_id)) + await s.execute(delete(Message).where(Message.chat_id == chat_id)) + await s.execute(delete(Chat).where(Chat.id == chat_id)) + await s.execute(delete(User).where(User.id == user_id)) + await engine.dispose() + + +asyncio.run(main()) diff --git a/tests/lab3.py b/tests/lab3.py new file mode 100644 index 00000000..7e456994 --- /dev/null +++ b/tests/lab3.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Конкурентная идемпотентность, область ключей и повторная авторизация.""" + +import json +import threading +import uuid +from concurrent.futures import ThreadPoolExecutor + +from client import Client, arguments, check, run + + +def main(): + args = arguments("Lab3 concurrency and idempotency") + c = Client(args.base_url, args.ca_file) + alice, bob, eve, chat = c.bootstrap() + path = "/api/v1/chats/" + chat["id"] + "/messages" + key = uuid.uuid4().hex + clients = [Client(args.base_url, args.ca_file) for _ in range(16)] + barrier = threading.Barrier(len(clients)) + + def send(local): + barrier.wait() + return local.message(alice, chat, "Одна отправка 👋", key=key) + + with ThreadPoolExecutor(max_workers=len(clients)) as pool: + responses = list(pool.map(send, clients)) + instances = {r["headers"]["X-Instance-Id"] for r in responses} + check(len(instances) >= 2, "Parallel repeats did not reach >=2 replicas") + original = responses[0]["json"] + check(all(r["json"] == original for r in responses), "Concurrent results differ") + messages = c.call("GET", path, actor=bob, schema="MessagePage")["json"]["items"] + check(messages == [original], "Concurrent retries duplicated message") + # Missing attachment_ids and [] are semantically equal; raw JSON ordering too. + replay = c.call( + "POST", + path, + 201, + actor=alice, + raw=b'{"attachment_ids":[],"text":"' + "Одна отправка 👋".encode() + b'"}', + headers={"Idempotency-Key": key, "Content-Type": "application/json"}, + schema="Message", + ) + check(replay["json"] == original, "Equivalent JSON did not replay") + c.call( + "POST", + path, + 409, + actor=alice, + body={"text": "Другой текст"}, + headers={"Idempotency-Key": key}, + ) + other = c.message(bob, chat, "Другой отправитель", key=key)["json"] + check(other["id"] != original["id"], "Key incorrectly shared across users") + chat2 = c.call( + "POST", + "/api/v1/chats", + 201, + actor=alice, + body={"title": "Другой чат", "member_ids": [alice["id"]]}, + schema="Chat", + )["json"] + check( + c.message(alice, chat2, "Другой чат", key=key)["json"]["id"] != original["id"], + "Key scope includes wrong chat", + ) + for invalid in [None, "", "bad key", "x" * 129, "bad/key"]: + c.call( + "POST", + path, + 400, + actor=alice, + body={"text": "invalid"}, + headers={} if invalid is None else {"Idempotency-Key": invalid}, + ) + # Validation/access failures must not consume a key. + retry_key = uuid.uuid4().hex + c.call( + "POST", path, 400, actor=alice, body={"text": ""}, headers={"Idempotency-Key": retry_key} + ) + c.message(alice, chat, "После 400", key=retry_key) + c.call( + "POST", + path, + 404, + actor=eve, + body={"text": original["text"]}, + headers={"Idempotency-Key": key}, + ) + c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=alice) + c.call( + "POST", + path, + 401, + actor=alice, + body={"text": original["text"]}, + headers={"Idempotency-Key": key}, + ) + for route in ["/health/live", "/health/ready", "/missing"]: + c.call("GET", route, 404 if route == "/missing" else 200) + print( + json.dumps( + { + "parallel_repeats": len(responses), + "instances": sorted(instances), + "messages_created": 1, + "checks": "normalization, conflict, key scope, invalid keys, access, logout, headers", + "result": "PASS", + }, + ensure_ascii=False, + indent=2, + ) + ) + + +if __name__ == "__main__": + run(main) diff --git a/tests/passwords.py b/tests/passwords.py new file mode 100644 index 00000000..3e7fe8d3 --- /dev/null +++ b/tests/passwords.py @@ -0,0 +1,68 @@ +"""docker compose exec -T api python < tests/passwords.py.""" +import asyncio +import base64 +import json +from datetime import datetime, timezone +from uuid import uuid4 +from urllib.error import HTTPError +from urllib.request import Request, urlopen + +from sqlalchemy import delete, select +from sqlalchemy.ext.asyncio import async_sessionmaker + +from src.core import cache, session_store +from src.core.db.models import User +from src.core.db.session import create_engine +from src.core.security import hash_password, hash_token, verify_password + + +def login(username, password, expected): + credential = base64.b64encode((username + ':' + password).encode()).decode() + request = Request('http://127.0.0.1:8080/api/v1/auth/sessions', method='POST', + headers={'Authorization': 'Basic ' + credential}) + try: + response = urlopen(request, timeout=10) + except HTTPError as error: + response = error + with response: + assert response.status == expected, response.status + return json.load(response) + + +async def main(): + password = 'Я:' + 'ю' * 126 + hashed = hash_password(password) + assert hashed.startswith('$argon2id$') + assert hash_password(password) != hashed + assert verify_password(password, hashed) + assert not verify_password('неверный-пароль', hashed) + assert not verify_password(password, 'unsupported$hash') + print('PASS: только Argon2id, случайная соль, Unicode 128 символов, неверный пароль') + engine = create_engine() + factory = async_sessionmaker(engine, expire_on_commit=False) + user_id, username = uuid4(), 'password_' + uuid4().hex[:16] + tokens = [] + cache.open_cache() + try: + async with factory.begin() as session: + session.add(User(id=user_id, username=username, display_name='Password test', + password_hash=hashed, created_at=datetime.now(timezone.utc))) + login(username, 'неверный-пароль', 401) + for _ in range(2): + result = login(username, password, 201) + tokens.append(result['token']) + stored = await session_store.fetch_session(hash_token(result['token'])) + assert stored is not None and stored.user_id == user_id + async with factory() as session: + assert await session.scalar(select(User.password_hash).where(User.id == user_id)) == hashed + print('PASS: повторный вход с Argon2id; сессии находятся в Redis; хеш пароля не изменяется') + finally: + for token in tokens: + await session_store.delete_session(hash_token(token)) + await cache.close_cache() + async with factory.begin() as session: + await session.execute(delete(User).where(User.id == user_id)) + await engine.dispose() + + +asyncio.run(main()) diff --git a/tests/production.py b/tests/production.py new file mode 100644 index 00000000..7176b1d3 --- /dev/null +++ b/tests/production.py @@ -0,0 +1,187 @@ +"""Негативные HTTP/Redis проверки для production-аудита; только локальный Compose.""" +import base64 +from concurrent.futures import ThreadPoolExecutor +import hashlib +import http.client +import json +from pathlib import Path +import subprocess +import threading +import uuid +from urllib.parse import urlsplit, quote + +from client import Client, arguments, check, run + +ROOT = Path(__file__).resolve().parents[1] + + +def redis(*args, raw=None): + command = ['docker', 'compose', 'exec', '-T', 'redis', 'redis-cli', '--raw'] + if raw is not None: + command += ['-x'] + return subprocess.check_output(command + list(args), cwd=ROOT, input=raw, timeout=10).rstrip(b'\n') + + +def main(): + args = arguments('Production-аудит входных данных, Redis и изоляции сессий') + c = Client(args.base_url, args.ca_file) + alice, bob, _, chat = c.bootstrap() + path = '/api/v1/chats/' + chat['id'] + '/messages' + failures = [] + def expect(label, method, path, status, **kwargs): + response = c.call(method, path, [200, 201, 400, 401, 413, 415, 500, 503], **kwargs) + if response['status'] != status: + failures.append(f'{label}: ожидался {status}, получен {response["status"]}') + return response + + payload = {'kind': 'messages', 'owner_id': chat['id'], + 'created_at': '2026-01-01T00:00:00+00:00', 'item_id': str(uuid.uuid4())} + for field, value in [('owner_id', 123), ('item_id', 123), ('owner_id', []), + ('created_at', '9999-12-31T23:59:59-23:59')]: + cursor = base64.urlsafe_b64encode(json.dumps({**payload, field: value}).encode()).decode() + expect('Типы/диапазон курсора ' + field, 'GET', path + '?cursor=' + cursor, 400, actor=alice) + expect('Длина курсора', 'GET', path + '?cursor=' + 'a' * 5000, 400, actor=alice) + for text in ('\ud800', '\udfff'): + expect('Непредставимый текст PostgreSQL', 'POST', path, 400, actor=alice, + raw=json.dumps({'text': 'valid' + text}).encode(), headers={'Content-Type': 'application/json'}) + registration = {'username': 'unicode_' + uuid.uuid4().hex[:16], + 'password': 'valid-password-123', 'display_name': 'Valid'} + for field, value in [('display_name', '\ud800'), + ('password', 'secret-audit-\ud800')]: + expect('Непредставимая строка ' + field, 'POST', '/api/v1/users', 400, + raw=json.dumps({**registration, field: value}).encode(), headers={'Content-Type': 'application/json'}) + expect('Большое тело с допустимым коротким текстом', 'POST', path, 413, actor=alice, + raw=b'{"text":"valid"}' + b' ' * 70000, headers={'Content-Type': 'application/json'}) + expect('Повторяющиеся ключи JSON', 'POST', path, 400, actor=alice, + raw=b'{"text":"first","text":"second"}', headers={'Content-Type': 'application/json'}) + expect('JSON не в UTF-8', 'POST', path, 400, actor=alice, + raw='{"text":"valid"}'.encode('utf-16'), headers={'Content-Type': 'application/json'}) + expect('Нестандартный NaN', 'POST', path, 400, actor=alice, + raw=b'{"text":"valid","extra":NaN}', headers={'Content-Type': 'application/json'}) + expect('Глубокий JSON', 'POST', path, 400, actor=alice, + raw=b'[' * 1200 + b'0' + b']' * 1200, headers={'Content-Type': 'application/json'}) + + # Максимально допустимый текст остаётся допустимым и в escaped JSON. + emoji = '😀' * 4000 + maximum = c.call('POST', path, 201, actor=alice, schema='Message', + raw=json.dumps({'text': emoji}, ensure_ascii=True).encode(), + headers={'Content-Type': 'application/json', 'Idempotency-Key':uuid.uuid4().hex}) + check(maximum['json']['text'] == emoji, 'Лимит тела отверг/исказил 4000 Unicode-символов') + check(maximum['headers'].get('Cache-Control') == 'no-store', 'Ответ API может попасть в кэш') + check(maximum['headers'].get('X-Content-Type-Options') == 'nosniff', 'Нет nosniff') + + parsed = urlsplit(c.base) + check(parsed.scheme == 'http', 'Сетевые проверки этого Compose-сценария рассчитаны на localhost HTTP') + def wire(duplicate=False, chunked=False, slow=False, started=None): + connection = http.client.HTTPConnection(parsed.hostname, parsed.port, timeout=10) + try: + if chunked: + connection.request('POST', path, body=iter([b'{"text":"valid"}', b' ' * 40000, b' ' * 40000]), + headers={'Authorization': 'Bearer ' + alice['token'], + 'Content-Type': 'application/json'}, encode_chunked=True) + elif slow: + connection.putrequest('POST', path) + connection.putheader('Authorization', 'Bearer ' + alice['token']) + connection.putheader('Content-Type', 'application/json') + connection.putheader('Content-Length', '100') + connection.endheaders(b'{\"text\":') + started.set() + else: + connection.putrequest('GET', '/api/v1/users/me') + connection.putheader('Authorization', 'Bearer ' + alice['token']) + if duplicate: + connection.putheader('Authorization', 'Bearer ' + bob['token']) + connection.endheaders() + response = connection.getresponse() + data = json.loads(response.read()) + check(data['error']['request_id'] == response.getheader('X-Request-Id'), 'Сетевой запрос потерял request_id') + return response.status + finally: + connection.close() + check(wire(chunked=True) == 413, 'Chunked обходит лимит размера тела') + check(wire(duplicate=True) == 401, 'Два Authorization принимаются неоднозначно') + print('PASS: реальный chunked=413, два Authorization=401, no-store; 4000 emoji в escaped JSON допустимы') + started = threading.Event() + with ThreadPoolExecutor(max_workers=1) as pool: + pending = pool.submit(wire, slow=True, started=started) + check(started.wait(2), 'Не началась медленная передача') + health = c.call('GET', '/health/live', schema='Health') + check(health['elapsed'] < 2, 'Медленная передача заблокировала event loop') + check(pending.result(timeout=10) == 408, 'У медленной передачи нет deadline') + print('PASS: реальное незавершённое тело получает 408; health работает во время ожидания') + + + created = [chat] + for index in range(12): + created.append(c.call('POST', '/api/v1/chats', 201, actor=alice, schema='Chat', + body={'title': 'SQL-page-' + str(index), 'member_ids': [alice['id']]})['json']) + chats, cursor = [], None + while True: + url = '/api/v1/chats?limit=3' + ('&cursor=' + quote(cursor, safe='') if cursor else '') + page = c.call('GET', url, actor=alice, schema='ChatPage')['json'] + chats.extend(item['id'] for item in page['items']) + cursor = page['next_cursor'] + if cursor is None: + break + c.call('GET', '/api/v1/chats?cursor=' + quote(cursor, safe=''), 400, actor=bob) + check(len(chats) <= len(created), 'Зациклилась пагинация чатов') + check(chats == [item['id'] for item in sorted(created, key=lambda x: (x['created_at'], uuid.UUID(x['id']).int))], + 'SQL-пагинация чатов потеряла/повторила объекты') + bob_chats = c.call('GET', '/api/v1/chats', actor=bob, schema='ChatPage')['json'] + check([item['id'] for item in bob_chats['items']] == [chat['id']], 'В список попали чужие чаты') + print('PASS: SQL-пагинация 13 чатов по 3; курсор принадлежит пользователю, чужие чаты скрыты') + + profile_key = 'user:v1:' + alice['id'] + for raw in (b'\xff\xfe', b'null', b'{"id":123}'): + redis('SET', profile_key, raw=raw) + expect('Повреждённый профиль Redis', 'GET', '/api/v1/users/me', 200, actor=alice) + session_key = 'session:v1:' + hashlib.sha256(alice['token'].encode()).hexdigest() + saved = redis('GET', session_key) + ttl = int(redis('PTTL', session_key)) + for raw in (b'\xff', json.dumps({'user_id': 123, 'expires_at': '2099-01-01T00:00:00+00:00'}).encode(), + b'[]', b'null', json.dumps({'user_id': alice['id'], 'expires_at': '2020-01-01T00:00:00+00:00'}).encode()): + try: + redis('SET', session_key, raw=raw) + redis('PEXPIRE', session_key, str(ttl)) + expect('Повреждённая/истёкшая сессия Redis', 'GET', '/api/v1/users/me', 401, actor=alice) + finally: + redis('SET', session_key, raw=saved) + redis('PEXPIRE', session_key, str(ttl)) + c.call('GET', '/api/v1/users/me', actor=alice, schema='User') + + # NUL недопустим в PostgreSQL text, но пароль хранится как Argon2-хеш. + special = {**registration, 'username': 'nulpass_' + uuid.uuid4().hex[:16], + 'password': ' пароль:\x00с пробелами '} + c.call('POST', '/api/v1/users', 201, body=special, schema='User') + special_basic = 'Basic ' + base64.b64encode((special['username'] + ':' + special['password']).encode()).decode() + special_session = c.call('POST', '/api/v1/auth/sessions', 201, + headers={'Authorization': special_basic}, schema='Session')['json'] + c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor={'token': special_session['token']}) + print('PASS: пароль с Unicode, пробелами, двоеточием и NUL не усекается') + + basic = 'Basic ' + base64.b64encode((alice['registration']['username'] + ':' + alice['registration']['password']).encode()).decode() + another = c.call('POST', '/api/v1/auth/sessions', 201, headers={'Authorization': basic}, schema='Session')['json'] + check(another['token'] != alice['token'], 'Повторный вход вернул старый токен') + def get_profile(_): + return Client(args.base_url, args.ca_file).call('GET', '/api/v1/users/me', actor=alice, schema='User') + with ThreadPoolExecutor(max_workers=8) as pool: + profiles = list(pool.map(get_profile, range(24))) + check(all(p['json']['id'] == alice['id'] for p in profiles), 'Смешаны пользователи конкурентных запросов') + check(len({p['headers']['X-Request-Id'] for p in profiles}) == 24, 'Повторяется request_id') + with ThreadPoolExecutor(max_workers=4) as pool: + list(pool.map(lambda _: Client(args.base_url, args.ca_file).call( + 'DELETE', '/api/v1/auth/sessions/current', [204, 401], actor=alice), range(4))) + c.call('GET', '/api/v1/users/me', 401, actor=alice) + c.call('GET', '/api/v1/users/me', actor={'token': another['token']}, schema='User') + c.call('GET', '/api/v1/users/me', actor=bob, schema='User') + c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor={'token': another['token']}) + print('PASS: независимые токены, конкурентные logout/GET, уникальные request_id') + if failures: + for failure in failures: + print('FAIL:', failure) + check(False, 'Production-аудит нашёл ошибки: ' + str(len(failures))) + print('PASS: типы/диапазон курсора, Unicode, размер/формат JSON и повреждённый Redis') + + +if __name__ == '__main__': + run(main) diff --git a/tests/production_limits.py b/tests/production_limits.py new file mode 100644 index 00000000..94e9cdec --- /dev/null +++ b/tests/production_limits.py @@ -0,0 +1,137 @@ +"""Изолированные проверки ASGI-лимитов и реального пула Argon2 в образе API.""" +import asyncio +import errno +import json +import os +from pathlib import Path +from tempfile import TemporaryDirectory +import threading +import time +from unittest.mock import patch + +from src.core import security +from src.core.request_limits import RequestLimitsMiddleware +from src.core.settings import settings + + +def scope(path='/api/v1/users', headers=()): + return {'type': 'http', 'asgi': {'version': '3.0'}, 'http_version': '1.1', + 'method': 'POST', 'scheme': 'http', 'path': path, 'raw_path': path.encode(), + 'query_string': b'', 'headers': [(b'content-type', b'application/json'), *headers], + 'state': {'request_id': 'limit-test'}, 'server': ('localhost', 80), 'client': ('localhost', 1)} + + +async def invoke(app, messages=(), path='/api/v1/users', headers=(), delay=0): + sent = [] + pending = iter(messages or [{'type': 'http.request', 'body': b'{}', 'more_body': False}]) + async def receive(): + if delay: + await asyncio.sleep(delay) + return next(pending) + async def send(message): + sent.append(message) + await app(scope(path, headers), receive, send) + if not sent: + return None + status = next(m['status'] for m in sent if m['type'] == 'http.response.start') + data = json.loads(b''.join(m.get('body', b'') for m in sent if m['type'] == 'http.response.body')) + if status >= 400: + assert data['error']['request_id'] == 'limit-test' + return status, data + + +async def main(): + assert os.geteuid() != 0, 'API работает от root' + status = Path('/proc/self/status').read_text() + assert 'NoNewPrivs:\t1' in status + assert 'CapEff:\t0000000000000000' in status + probe = Path('/app/.production-write-probe') + try: + probe.write_text('probe') + except OSError as error: + assert error.errno == errno.EROFS, error.errno + else: + probe.unlink() + raise AssertionError('Корневая файловая система API доступна для записи') + with TemporaryDirectory() as directory: + Path(directory, 'probe').write_text('temporary') + print('PASS: non-root, read-only rootfs, пустые capabilities, no-new-privileges; /tmp доступен') + called = 0 + async def endpoint(scope, receive, send): + nonlocal called + called += 1 + message = await receive() + await send({'type': 'http.response.start', 'status': 200, 'headers': []}) + await send({'type': 'http.response.body', 'body': json.dumps({'bytes': len(message['body'])}).encode()}) + app = RequestLimitsMiddleware(endpoint) + maximum = settings.http_max_body_bytes + assert (await invoke(app, headers=[(b'content-length', str(maximum+1).encode())]))[0] == 413 + assert called == 0 + chunks = [{'type': 'http.request', 'body': b' ' * 40000, 'more_body': True}, + {'type': 'http.request', 'body': b' ' * 40000, 'more_body': False}] + assert (await invoke(app, messages=chunks))[0] == 413 + assert (await invoke(app, messages=chunks, headers=[(b'content-length', b'1')]))[0] == 413 + assert (await invoke(app, headers=[(b'content-length', b'-1')]))[0] == 400 + with patch.object(settings, 'http_body_timeout_seconds', 0.02): + assert (await invoke(app, delay=0.1))[0] == 408 + assert await invoke(app, messages=[{'type': 'http.disconnect'}]) is None + assert called == 0 + assert (await invoke(app))[0] == 200 + assert app.limiter.borrowed_tokens == 0 + print('PASS: Content-Length/chunked/ложный размер, timeout, disconnect; слот всегда освобождается') + + gate = asyncio.Event() + entered = asyncio.Event() + active = 0 + async def blocked(scope, receive, send): + nonlocal active + if scope['path'].startswith('/api/'): + active += 1 + if active == 2: + entered.set() + await gate.wait() + await endpoint(scope, receive, send) + with patch.object(settings, 'http_concurrency_limit', 2): + limited = RequestLimitsMiddleware(blocked) + tasks = [asyncio.create_task(invoke(limited)) for _ in range(2)] + try: + await asyncio.wait_for(entered.wait(), 1) + assert (await invoke(limited))[0] == 503 + assert (await invoke(limited, path='/health/live'))[0] == 200 + tasks[0].cancel() + await asyncio.gather(tasks[0], return_exceptions=True) + assert limited.limiter.borrowed_tokens == 1 + gate.set() + assert (await tasks[1])[0] == 200 + assert (await invoke(limited))[0] == 200 + finally: + gate.set() + for task in tasks: + task.cancel() + await asyncio.gather(*tasks, return_exceptions=True) + assert limited.limiter.borrowed_tokens == 0 + print('PASS: перегрузка=503 в общем JSON, health доступен, отмена запроса не теряет слот') + + lock = threading.Lock() + active = peak = 0 + def slow_hash(password): + nonlocal active, peak + with lock: + active += 1 + peak = max(peak, active) + try: + time.sleep(0.03) + return 'hashed-' + password + finally: + with lock: + active -= 1 + count = settings.password_hash_concurrency * 4 + with patch.object(security, 'hash_password', slow_hash): + result = await asyncio.gather(*(security.hash_password_async(str(i)) for i in range(count))) + assert result == ['hashed-' + str(i) for i in range(count)] + assert peak == settings.password_hash_concurrency, peak + assert security.password_limiter.borrowed_tokens == 0 + print('PASS: очередь Argon2 соблюдает лимит параллелизма; результаты не смешиваются') + + +asyncio.run(main()) diff --git a/tests/readiness.py b/tests/readiness.py new file mode 100644 index 00000000..177efc06 --- /dev/null +++ b/tests/readiness.py @@ -0,0 +1,53 @@ +"""Изолированная проверка HTTP-бюджета readiness при медленной отмене драйвера.""" + +import asyncio +import time +from unittest.mock import patch + +from src.core import readiness +from src.core.errors import ApiError +from src.core.settings import settings + + +async def main(): + cancelled = asyncio.Event() + release = asyncio.Event() + calls = 0 + + async def stalled_driver(): + nonlocal calls + calls += 1 + try: + await asyncio.Event().wait() + except asyncio.CancelledError: + cancelled.set() + await release.wait() + + with ( + patch.object(settings, "healthcheck_timeout_seconds", 0.03), + patch.object(readiness, "_probe", stalled_driver), + ): + for _ in range(3): + began = time.monotonic() + try: + await readiness.check() + except ApiError as error: + assert error.status_code == 503 + else: + raise AssertionError("Stalled dependency was declared ready") + assert time.monotonic() - began < 0.15, "HTTP waited for slow driver cancellation" + assert cancelled.is_set() and calls == 1, "Readiness spawned accumulating probes" + release.set() + await asyncio.sleep(0) + await readiness.close() + + async def healthy(): + pass + + with patch.object(readiness, "_probe", healthy): + await readiness.check() + await readiness.close() + print("PASS: bounded readiness response, one pending probe, recovery after cancellation") + + +asyncio.run(main()) diff --git a/tests/regressions.py b/tests/regressions.py new file mode 100644 index 00000000..d336a637 --- /dev/null +++ b/tests/regressions.py @@ -0,0 +1,163 @@ +"""Проверки замечаний преподавателя: HTTP, повреждённый Redis и точный курсор.""" +import base64 +import json +import subprocess +import uuid +from pathlib import Path +from urllib.parse import quote + +from client import Client, arguments, check, run + +ROOT = Path(__file__).resolve().parents[1] + + +def database(code): + container = subprocess.check_output(['docker', 'compose', 'ps', '-q', 'api'], cwd=ROOT, text=True).split()[0] + result = subprocess.run(['docker', 'exec', '-i', container, 'python'], + input=code, cwd=ROOT, text=True, capture_output=True, timeout=30) + check(result.returncode == 0, 'Ошибка подготовки/удаления ORM-фикстуры') + return json.loads(result.stdout) if result.stdout.strip() else None + + +def main(): + args = arguments('Регрессии по замечаниям преподавателя') + c = Client(args.base_url, args.ca_file) + alice, bob, eve, chat = c.bootstrap() + path = '/api/v1/chats/' + chat['id'] + '/messages' + registration = {**alice['registration'], 'username': 'blank_' + uuid.uuid4().hex[:16]} + for field, value in [('password', ' ' * 12), ('password', '\t\n' * 6), + ('display_name', ' \t\n')]: + c.call('POST', '/api/v1/users', 400, body={**registration, field: value}) + for body in ({'title': ' \t\n', 'member_ids': [alice['id']]}, + {'title': 'Duplicate', 'member_ids': [alice['id'], alice['id']]}, + {'title': 'Unknown', 'member_ids': [alice['id'], str(uuid.uuid4())]}, + {'title': 'Missing actor', 'member_ids': [bob['id']]}): + c.call('POST', '/api/v1/chats', 400, actor=alice, body=body) + basic = {'Authorization': 'Basic ' + base64.b64encode(( + alice['registration']['username'] + ':' + alice['registration']['password'] + ).encode()).decode()} + for raw in (b'{"extra":"forbidden"}', b'{}', b'null', b' ', b'{'): + c.call('POST', '/api/v1/auth/sessions', 400, headers=basic, raw=raw) + c.call('POST', '/api/v1/auth/sessions', 401) + for credential in ('Basic !!!', 'Basic /w==', 'Bearer something', + 'Basic ' + base64.b64encode(b'no-colon').decode()): + c.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': credential}) + for payload in ('null', '[]', '{}', '123', '{broken'): + cursor = base64.urlsafe_b64encode(payload.encode()).decode().rstrip('=') + c.call('GET', path + '?cursor=' + quote(cursor, safe=''), 400, actor=alice) + for endpoint, body, actor in ( + ('/api/v1/users', registration, None), + ('/api/v1/chats', {'title': 'MIME', 'member_ids': [alice['id']]}, alice), + (path, {'text': 'valid'}, alice), + ): + response = c.call('POST', endpoint, 415, actor=actor, body=body, + headers={'Content-Type': 'text/plain'}) + check(response['json']['error']['code'] == 'unsupported_media_type', 'Неверный код MIME-ошибки') + for raw in (b'{"text":"valid"}}', b'{"text":"valid"}]', b'{"text":"valid"}{}'): + c.call('POST', path, 400, actor=alice, raw=raw, headers={'Content-Type': 'application/json'}) + for body in ({'text': ' ' * 12}, {'text': ' ' + 'a' * 4000}, {'text': 123}, + {'text': 'valid', 'sender_id': eve['id']}): + c.call('POST', path, 400, actor=alice, body=body) + check(c.call('GET', path, actor=alice, schema='MessagePage')['json']['items'] == [], + 'Отклонённый запрос сохранил сообщение') + print('PASS: обязательные пробельные строки, malformed JSON, типы/лишние поля, MIME 415, Basic без тела') + + # Создаём только синтетические строки через SQLAlchemy; пароли/токены в команды не передаются. + fixture = database(f''' +import asyncio, json +from datetime import datetime, timezone +from uuid import UUID, uuid4 +from sqlalchemy.ext.asyncio import async_sessionmaker +from src.core.db.session import create_engine +from src.core.db.models import User, Message +async def main(): + engine = create_engine() + ids = [uuid4() for _ in range(100)] + base = uuid4().int & ~65535 + message_ids = [UUID(int=base+4), UUID(int=base+3), UUID(int=base+2), UUID(int=base+1)] + times = [100000, 200000, 200000, 200000] + async with async_sessionmaker(engine).begin() as session: + session.add_all([User(id=i, username='fixture_' + i.hex[:20], display_name='Fixture', + password_hash='not-for-login', created_at=datetime.now(timezone.utc)) for i in ids]) + session.add_all([Message(id=i, chat_id=UUID('{chat['id']}'), sender_id=UUID('{alice['id']}'), + text='precision', created_at=datetime(2026, 1, 1, 0, 0, 0, us, timezone.utc)) + for i, us in zip(message_ids, times)]) + await engine.dispose() + print(json.dumps({{'users': [str(i) for i in ids], 'messages': [str(i) for i in message_ids]}})) +asyncio.run(main()) +''') + try: + c.call('POST', '/api/v1/chats', 400, actor=alice, + body={'title': '101 участников', 'member_ids': [alice['id'], *fixture['users']]}) + hundred = c.call('POST', '/api/v1/chats', 201, actor=alice, schema='Chat', + body={'title': '100 участников', + 'member_ids': [alice['id'], *fixture['users'][:99]]})['json'] + check(len(hundred['member_ids']) == 100, 'Допустимые 100 участников не сохранены') + received = [] + cursor = None + while True: + url = path + '?limit=1' + ('&cursor=' + quote(cursor, safe='') if cursor else '') + page = c.call('GET', url, actor=bob, schema='MessagePage')['json'] + received.extend(item['id'] for item in page['items']) + if cursor is None: + first_cursor = page['next_cursor'] + cursor = page['next_cursor'] + if cursor is None: + break + check(len(received) <= 4, 'Курсор зациклился') + expected = [fixture['messages'][0], *reversed(fixture['messages'][1:])] + check(received == expected, 'Потеря/повтор сообщений при микросекундах или равном времени') + other = c.call('POST', '/api/v1/chats', 201, actor=alice, + body={'title': 'Other', 'member_ids': [alice['id']]}, schema='Chat')['json'] + c.call('GET', '/api/v1/chats/' + other['id'] + '/messages?cursor=' + quote(first_cursor, safe=''), + 400, actor=alice) + print('PASS: 101 существующий участник отклонён; курсор сохраняет микросекунды и UUID-порядок, привязан к чату') + finally: + database(f''' +import asyncio +from uuid import UUID +from sqlalchemy import delete, select +from sqlalchemy.ext.asyncio import async_sessionmaker +from src.core.db.session import create_engine +from src.core.db.models import User, Chat, ChatMember, Message +async def main(): + engine = create_engine() + ids = [UUID(i) for i in {fixture['users']!r}] + async with async_sessionmaker(engine).begin() as session: + chats = list(await session.scalars(select(ChatMember.chat_id).where(ChatMember.user_id.in_(ids)))) + await session.execute(delete(Message).where(Message.chat_id.in_(chats))) + await session.execute(delete(Chat).where(Chat.id.in_(chats))) + await session.execute(delete(User).where(User.id.in_(ids))) + await engine.dispose() +asyncio.run(main()) +''') + text = ' строка\nс пробелами ' + saved = c.message(alice, chat, text)['json'] + check(saved['text'] == text, 'Текст обрезан до сохранения') + history = c.call('GET', path, actor=alice, schema='MessagePage')['json'] + check(next(m for m in history['items'] if m['id'] == saved['id'])['text'] == text, + 'Сохранённый текст изменён') + print('PASS: исходный текст и его длина сохраняются без strip') + + key = 'user:v1:' + alice['id'] + def redis(*args): + return subprocess.check_output(['docker', 'compose', 'exec', '-T', 'redis', + 'redis-cli', '--raw', *args], cwd=ROOT, text=True, timeout=10).strip() + for corrupted in ('null', '[]', '{}', '{broken', '"text"', '{"id":123}', + json.dumps({**alice['user'], 'id': bob['id']})): + redis('SET', key, corrupted) + me = c.call('GET', '/api/v1/users/me', actor=alice, schema='User')['json'] + check(me == alice['user'], 'Повреждённый профиль отвергает сессию или меняет пользователя') + check(json.loads(redis('GET', key))['id'] == alice['id'], 'Повреждённый кэш не заменён') + check(0 < int(redis('TTL', key)) <= 60, 'Восстановленному профилю не назначен TTL') + print('PASS: null/повреждённый/чужой профиль заменяется из PostgreSQL с TTL; сессия работает') + + spec = json.loads(c.call('GET', '/openapi.json')['bytes']) + login = spec['paths']['/api/v1/auth/sessions']['post'] + check('requestBody' not in login and {'basicAuth': []} in login['security'], 'Swagger вход не Basic') + check({'bearerAuth': []} in spec['paths']['/api/v1/users/me']['get']['security'], 'Swagger не передаёт Bearer') + print('PASS: Swagger объявляет Basic для входа и Bearer для защищённых методов') + + +if __name__ == '__main__': + run(main) diff --git a/tests/settings.py b/tests/settings.py new file mode 100644 index 00000000..fbe60761 --- /dev/null +++ b/tests/settings.py @@ -0,0 +1,53 @@ +"""docker compose exec -T api python < tests/settings.py.""" +import os +from pathlib import Path +from tempfile import TemporaryDirectory +from unittest.mock import patch + +from pydantic import ValidationError + +from src.core.settings import AppSettings + +# Только искусственные значения, соединения с этими адресами не открываются. +with patch.dict(os.environ, { + 'DATABASE_URL': 'postgresql://settings_user:settings_test_password@postgres/settings_db', + 'REDIS_URL': 'redis://redis:6379/0', +}, clear=True): + with TemporaryDirectory() as directory: + path = Path(directory) / '.env' + path.write_text('SESSION_TTL_SECONDS=2\nDATABASE_POOL_SIZE=3\n', encoding='utf-8') + config = AppSettings(_env_file=path) + assert config.session_ttl_seconds == 2 + assert config.database_pool_size == 3 + assert config.sqlalchemy_database_url.startswith('postgresql+asyncpg://') + assert 'settings_test_password' not in repr(config) + with patch.dict(os.environ, {'SESSION_TTL_SECONDS': '4'}): + assert AppSettings(_env_file=path).session_ttl_seconds == 4 + print('PASS: AppSettings читает .env, окружение имеет приоритет, URL скрыт в repr') + + for values, expected_field in ( + ({'session_ttl_seconds': 0}, 'session_ttl_seconds'), + ({'session_ttl_seconds': 'invalid'}, 'session_ttl_seconds'), + ({'database_pool_size': 0}, 'database_pool_size'), + ({'redis_read_timeout_seconds': -1}, 'redis_read_timeout_seconds'), + ({'session_token_bytes': 16}, 'session_token_bytes'), + ): + try: + AppSettings(_env_file=None, **values) + except ValidationError as error: + assert any(item['loc'] == (expected_field,) for item in error.errors()) + else: + raise AssertionError('Некорректная настройка принята: ' + expected_field) + print('PASS: Pydantic отклоняет неверные TTL, pool, timeout и недостаточную длину токена') + for values in ({'app_env': 'production'}, {'admin_password': ' ' * 12}, + {'admin_display_name': 'bad\x00'}): + try: + AppSettings(_env_file=None, **values) + except ValidationError as error: + assert 'settings_test_password' not in str(error) + assert 'input_value' not in str(error) + else: + raise AssertionError('Небезопасная конфигурация принята') + assert AppSettings(_env_file=None, app_env='production', + admin_password='custom-production-password-123').app_env == 'production' + print('PASS: production отвергает учебный пароль; ошибки конфигурации скрывают входные значения')