From 7f599cb25eecefb7c0cfb34b45f3af1bb2eb39e8 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 15:21:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B1=D1=80=D0=B0=D1=82=D1=8C=20?= =?UTF-8?q?=D1=81=D1=82=D0=B5=D0=BD=D0=B4=20=D0=B8=D0=B7=20API,=20PostgreS?= =?UTF-8?q?QL=20=D0=B8=20Valkey=20=D1=81=20=D1=85=D1=83=D0=BA=D0=BE=D0=BC?= =?UTF-8?q?=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B7=D0=B0=D0=BF=D1=83=D1=81=D0=BA?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/app.yml | 15 ++++---- Dockerfile | 2 +- Makefile | 32 +++++++--------- compose.yaml | 76 +++++++++++++++++++++++++++++++++++-- infra/valkey/valkey.conf | 17 +++++++++ scripts/init-secrets.sh | 42 ++++++++++++++++++++ scripts/restart.cmd | 5 +++ scripts/restart.sh | 9 +++++ student-tests/lifecycle.sh | 62 ------------------------------ student-tests/wait-ready.sh | 17 --------- 10 files changed, 167 insertions(+), 110 deletions(-) create mode 100644 infra/valkey/valkey.conf create mode 100755 scripts/init-secrets.sh create mode 100644 scripts/restart.cmd create mode 100755 scripts/restart.sh delete mode 100644 student-tests/lifecycle.sh delete mode 100644 student-tests/wait-ready.sh diff --git a/.github/workflows/app.yml b/.github/workflows/app.yml index dc5c3033..a27b1206 100644 --- a/.github/workflows/app.yml +++ b/.github/workflows/app.yml @@ -8,7 +8,6 @@ defaults: shell: bash env: BASE_URL: http://localhost:8080 - CONTAINER: mtusi-messenger-lab2 jobs: unit: runs-on: ubuntu-latest @@ -30,22 +29,22 @@ jobs: - uses: actions/setup-python@v5 with: python-version: '3.12' - - run: docker build -t mtusi-messenger:lab2 . - - run: docker run -d --name "$CONTAINER" -p 127.0.0.1:8080:8080 --env-file .env.example mtusi-messenger:lab2 - - run: sh student-tests/wait-ready.sh + - run: make secrets + - run: docker compose up -d --build --wait - run: mkdir -p evidence-ci - name: Public smoke run: make test PYTHON=python BASE_URL="$BASE_URL" | tee evidence-ci/smoke.txt + - name: Persistence across a restart of the API and both stores + run: make test-persistence PYTHON=python BASE_URL="$BASE_URL" ACTION_SCRIPT=scripts/restart.sh | tee evidence-ci/persistence.txt - name: Concurrency check run: go run ./tools/concurrency --base-url "$BASE_URL" --output evidence-ci/concurrency.json - - name: SIGTERM and restart - run: sh student-tests/lifecycle.sh | tee evidence-ci/lifecycle.txt - name: Container logs if: always() - run: docker logs "$CONTAINER" > evidence-ci/container.log 2>&1 || true + run: docker compose logs > evidence-ci/compose.log 2>&1 || true - name: Cleanup + # The CI stand is disposable, so its volumes go too. if: always() - run: docker rm -f "$CONTAINER" || true + run: docker compose down -v || true - uses: actions/upload-artifact@v4 if: always() with: diff --git a/Dockerfile b/Dockerfile index 42d4357a..b8bc70b1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,6 +15,6 @@ ENV APP_HOST=0.0.0.0 \ APP_PORT=8080 \ APP_ENV=lab EXPOSE 8080 -HEALTHCHECK --interval=10s --timeout=2s --start-period=3s --retries=3 \ +HEALTHCHECK --interval=10s --timeout=2s --start-period=10s --retries=3 \ CMD wget -qO- "http://127.0.0.1:${APP_PORT:-8080}/health/ready" >/dev/null || exit 1 ENTRYPOINT ["/usr/local/bin/server"] diff --git a/Makefile b/Makefile index 3cb2500e..d1d37188 100644 --- a/Makefile +++ b/Makefile @@ -28,12 +28,9 @@ test-persistence: # --------------------------------------------------------------------------- # Application targets (Go implementation). The targets above are the skeleton. # --------------------------------------------------------------------------- -IMAGE ?= mtusi-messenger:lab2 -CONTAINER ?= mtusi-messenger-lab2 -PUBLISH ?= 127.0.0.1:8080 EVIDENCE_DIR ?= evidence -.PHONY: build vet unit up down docker-build docker-run docker-stop docker-logs wait-ready concurrency lifecycle +.PHONY: build vet unit secrets up down destroy logs concurrency explain build: go build -o bin/server ./cmd/server @@ -41,32 +38,29 @@ build: vet: go vet ./... +# The tests start disposable PostgreSQL and Valkey containers: Docker must run. unit: go test -race -count=1 ./... +secrets: + sh scripts/init-secrets.sh + up: docker compose up -d --build --wait +# Stops and removes the containers; volumes and therefore data are kept. down: docker compose down -docker-build: - docker build -t "$(IMAGE)" . +# Deletes the volumes too: every user, chat, message and session of the stand. +destroy: + docker compose down -v -docker-run: - docker run -d --name "$(CONTAINER)" -p "$(PUBLISH):8080" --env-file .env "$(IMAGE)" - -docker-stop: - docker stop -t 10 "$(CONTAINER)" && docker rm "$(CONTAINER)" - -docker-logs: - docker logs "$(CONTAINER)" - -wait-ready: - BASE_URL="$(BASE_URL)" sh student-tests/wait-ready.sh +logs: + docker compose logs concurrency: go run ./tools/concurrency --base-url "$(BASE_URL)" --output "$(EVIDENCE_DIR)/concurrency.json" -lifecycle: - BASE_URL="$(BASE_URL)" CONTAINER="$(CONTAINER)" sh student-tests/lifecycle.sh +explain: + docker compose exec -T postgres psql -U messenger -d messenger -X -q -v ON_ERROR_STOP=1 -f - < scripts/explain-history.sql diff --git a/compose.yaml b/compose.yaml index 13cdcb58..b21072f2 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,12 +1,82 @@ +# Local stand: the API over PostgreSQL (users, chats, messages) and Valkey +# (sessions). Only the API is published, on loopback. Credentials are files in +# ./secrets created by `make secrets`; named volumes keep the data across +# restarts, and only `make destroy` removes them. services: api: build: . image: mtusi-messenger:lab2 - # Fixed name so the Makefile targets and student-tests/lifecycle.sh address - # the same container whether it was started by compose or by docker run. + # Fixed name for docker logs/inspect in the report. container_name: mtusi-messenger-lab2 - env_file: .env + env_file: + - path: .env + required: false + environment: + DATABASE_URL_FILE: /run/secrets/database_url + SESSION_STORE_URL_FILE: /run/secrets/session_store_url + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + secrets: + - database_url + - session_store_url + depends_on: + postgres: + condition: service_healthy + valkey: + condition: service_healthy # Loopback only; override with PUBLISH=127.0.0.1:18080 when 8080 is busy. ports: - "${PUBLISH:-127.0.0.1:8080}:8080" stop_grace_period: 10s + + postgres: + image: postgres:18.6-alpine3.24@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873 + environment: + POSTGRES_DB: messenger + POSTGRES_USER: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + secrets: + - postgres_password + # Since PostgreSQL 18 the image keeps PGDATA in a versioned directory + # below /var/lib/postgresql, so the volume is mounted on the parent. + volumes: + - pgdata:/var/lib/postgresql + healthcheck: + test: ["CMD", "pg_isready", "-U", "messenger", "-d", "messenger"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 10s + # A clean shutdown writes a checkpoint instead of leaving crash recovery + # to the next start. + stop_grace_period: 30s + + valkey: + image: valkey/valkey:9.1.2-alpine3.24@sha256:48332870af354a799964c0012ae1194a0bf2bf894eb508f945810596dc2d8d11 + command: ["valkey-server", "/usr/local/etc/valkey/valkey.conf"] + secrets: + - valkey_auth + volumes: + - ./infra/valkey/valkey.conf:/usr/local/etc/valkey/valkey.conf:ro + - valkeydata:/data + healthcheck: + # valkey-cli exits 0 even on NOAUTH, so the reply itself is checked. + test: ["CMD-SHELL", "VALKEYCLI_AUTH=\"$$(sed -n 's/^requirepass //p' /run/secrets/valkey_auth)\" valkey-cli ping | grep -q PONG"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s + stop_grace_period: 10s + +volumes: + pgdata: + valkeydata: + +secrets: + database_url: + file: ./secrets/database_url + session_store_url: + file: ./secrets/session_store_url + postgres_password: + file: ./secrets/postgres_password + valkey_auth: + file: ./secrets/valkey_auth.conf diff --git a/infra/valkey/valkey.conf b/infra/valkey/valkey.conf new file mode 100644 index 00000000..f417d667 --- /dev/null +++ b/infra/valkey/valkey.conf @@ -0,0 +1,17 @@ +# Valkey is the only store of user sessions, so it is their source of truth. +# Every write is fsynced before the reply: an acknowledged login survives a +# crash or a restart. Only logins and logouts write; session lookups are reads +# and pay nothing for this. +appendonly yes +appendfsync always +dir /data + +# The append-only file already persists everything; RDB snapshots are off. +save "" + +# Sessions are never evicted to free memory: a full store refuses new logins +# (503) instead of silently logging users out. +maxmemory-policy noeviction + +# requirepass comes from the compose secret, never from the repository. +include /run/secrets/valkey_auth diff --git a/scripts/init-secrets.sh b/scripts/init-secrets.sh new file mode 100755 index 00000000..ae7df48b --- /dev/null +++ b/scripts/init-secrets.sh @@ -0,0 +1,42 @@ +#!/bin/sh +# Creates the local credentials of the compose stand in ./secrets, which git +# ignores. Existing secrets are kept: PostgreSQL applies its password only +# when the volume is first initialised, so a new password would lock the API +# out of an existing database. To start over: make destroy, then delete +# ./secrets and run this again. +set -eu +cd "$(dirname "$0")/.." + +files="postgres_password database_url valkey_auth.conf session_store_url" +present=0 +for f in $files; do + if [ -e "secrets/$f" ]; then + present=$((present + 1)) + fi +done +if [ "$present" -eq 4 ]; then + echo "secrets already exist in ./secrets, kept" + exit 0 +fi +if [ "$present" -ne 0 ]; then + echo "./secrets is incomplete: delete it (and run make destroy if the stand holds data), then run again" >&2 + exit 1 +fi + +# 24 random bytes as hex: URL-safe without escaping. +random() { + od -An -tx1 -N24 /dev/urandom | tr -d ' \n' +} +pg=$(random) +kv=$(random) + +# The directory is private; the files stay readable for the unprivileged +# users inside the containers, which see them through bind mounts. +umask 022 +mkdir -p secrets +chmod 700 secrets +printf '%s\n' "$pg" > secrets/postgres_password +printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable\n' "$pg" > secrets/database_url +printf 'requirepass %s\n' "$kv" > secrets/valkey_auth.conf +printf 'redis://:%s@valkey:6379/0\n' "$kv" > secrets/session_store_url +echo "created ./secrets" diff --git a/scripts/restart.cmd b/scripts/restart.cmd new file mode 100644 index 00000000..5f86f138 --- /dev/null +++ b/scripts/restart.cmd @@ -0,0 +1,5 @@ +@echo off +rem Windows entry point of scripts/restart.sh: tests/resilience.py starts the +rem hook without a shell, and Windows cannot execute a .sh file directly. +sh "%~dp0restart.sh" +exit /b %errorlevel% diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..11591f9c --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,9 @@ +#!/bin/sh +# Restart hook of `make test-persistence` (tests/resilience.py): stops the +# API, PostgreSQL and Valkey gracefully and starts the same containers again. +# Volumes and data are kept and nothing is re-seeded. The test hides the +# output, so failures surface through the exit code only. +set -eu +cd "$(dirname "$0")/.." +docker compose stop +docker compose up -d --wait diff --git a/student-tests/lifecycle.sh b/student-tests/lifecycle.sh deleted file mode 100644 index 0eda2169..00000000 --- a/student-tests/lifecycle.sh +++ /dev/null @@ -1,62 +0,0 @@ -#!/bin/sh -# Lifecycle proof for the in-memory design: -# 1. create a user, a chat and a message, read the message back; -# 2. stop the container with SIGTERM and record how the process shut down; -# 3. start the same container again and show the old X-User-Id is gone (401). -# Usage: BASE_URL=http://localhost:8080 CONTAINER=mtusi-messenger-lab2 sh student-tests/lifecycle.sh -set -eu -BASE_URL=${BASE_URL:-http://localhost:8080} -CONTAINER=${CONTAINER:-mtusi-messenger-lab2} -HERE=$(dirname "$0") - -json_field() { # json_field '' key -> first string value of key - printf '%s' "$1" | sed -n "s/.*\"$2\":\"\([^\"]*\)\".*/\1/p" | head -n 1 -} - -echo "== 1. write and read before restart ==" -user=$(curl -sS -X POST "$BASE_URL/api/v1/users" -H 'Content-Type: application/json' \ - -d "{\"username\":\"lifecycle_$(date +%s)\",\"display_name\":\"Lifecycle\"}") -user_id=$(json_field "$user" id) -[ -n "$user_id" ] || { echo "user was not created: $user" >&2; exit 1; } -chat=$(curl -sS -X POST "$BASE_URL/api/v1/chats" -H 'Content-Type: application/json' -H "X-User-Id: $user_id" \ - -d "{\"title\":\"lifecycle\",\"member_ids\":[\"$user_id\"]}") -chat_id=$(json_field "$chat" id) -[ -n "$chat_id" ] || { echo "chat was not created: $chat" >&2; exit 1; } -message=$(curl -sS -X POST "$BASE_URL/api/v1/chats/$chat_id/messages" -H 'Content-Type: application/json' -H "X-User-Id: $user_id" \ - -d '{"text":"before restart"}') -message_id=$(json_field "$message" id) -[ -n "$message_id" ] || { echo "message was not created: $message" >&2; exit 1; } -history=$(curl -sS "$BASE_URL/api/v1/chats/$chat_id/messages" -H "X-User-Id: $user_id") -case "$history" in - *"$message_id"*) echo "message $message_id is readable before restart" ;; - *) echo "message missing from history: $history" >&2; exit 1 ;; -esac - -echo "== 2. SIGTERM via docker stop (grace 10s) ==" -started=$(date +%s) -docker stop -t 10 "$CONTAINER" >/dev/null -finished=$(date +%s) -exit_code=$(docker inspect -f '{{.State.ExitCode}}' "$CONTAINER") -echo "docker stop took $((finished - started))s wall clock, container exit code $exit_code" -echo "last log lines:" -docker logs --tail 3 "$CONTAINER" 2>&1 | sed 's/^/ /' -if [ "$exit_code" != "0" ]; then - echo "process did not exit cleanly on SIGTERM" >&2 - exit 1 -fi - -echo "== 3. restart the same container: state must be gone ==" -if ! docker start "$CONTAINER" >/dev/null; then - echo "cannot start $CONTAINER again: run it without --rm so the stopped container survives docker stop" >&2 - exit 1 -fi -BASE_URL="$BASE_URL" sh "$HERE/wait-ready.sh" >/dev/null -me_status=$(curl -s -o /dev/null -w '%{http_code}' "$BASE_URL/api/v1/users/me" -H "X-User-Id: $user_id") -chat_status=$(curl -s -o /dev/null -w '%{http_code}' "$BASE_URL/api/v1/chats/$chat_id" -H "X-User-Id: $user_id") -echo "GET /users/me with the old X-User-Id -> $me_status" -echo "GET /chats/$chat_id with the old X-User-Id -> $chat_status" -if [ "$me_status" != "401" ] || [ "$chat_status" != "401" ]; then - echo "expected 401 for both after restart" >&2 - exit 1 -fi -echo "PASS: state lived only in the process memory and was lost on restart" diff --git a/student-tests/wait-ready.sh b/student-tests/wait-ready.sh deleted file mode 100644 index 001f32a6..00000000 --- a/student-tests/wait-ready.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/sh -# Waits until GET /health/ready answers 200 or the attempt budget runs out. -# Usage: BASE_URL=http://localhost:8080 ATTEMPTS=60 sh student-tests/wait-ready.sh -set -eu -BASE_URL=${BASE_URL:-http://localhost:8080} -ATTEMPTS=${ATTEMPTS:-60} -i=0 -while [ "$i" -lt "$ATTEMPTS" ]; do - if curl -sf "$BASE_URL/health/ready" >/dev/null 2>&1; then - echo "ready: $BASE_URL" - exit 0 - fi - i=$((i + 1)) - sleep 0.5 -done -echo "not ready after $ATTEMPTS attempts: $BASE_URL" >&2 -exit 1