Реализация лабораторной 2: PostgreSQL, сессии, Valkey-кэш
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
Tmribob
2026-10-30 19:37:21 +03:00
parent 65003b6119
commit 3df9b09896
44 changed files with 3519 additions and 17 deletions
+48
View File
@@ -0,0 +1,48 @@
#!/bin/sh
# Generate the local, synthetic stand's secrets and .env.
# Values are written to ./secrets (git-ignored, mode 0600) and never printed.
# Re-running is safe: existing files are kept unless FORCE=1.
set -eu
cd "$(dirname "$0")/.."
PYTHON="${PYTHON:-python}"
SECRETS_DIR="${SECRETS_DIR:-secrets}"
if [ ! -f .env ]; then
cp .env.example .env
printf '%s\n' "created .env from .env.example"
fi
mkdir -p "$SECRETS_DIR"
chmod 700 "$SECRETS_DIR" 2>/dev/null || true
umask 077
random_password() {
"$PYTHON" -c 'import secrets; print(secrets.token_hex(24))'
}
write_if_missing() {
path="$SECRETS_DIR/$1"
if [ -f "$path" ] && [ "${FORCE:-0}" != "1" ]; then
return 0
fi
printf '%s' "$2" > "$path"
chmod 600 "$path" 2>/dev/null || true
}
if [ -f "$SECRETS_DIR/database_url" ] && [ -f "$SECRETS_DIR/cache_url" ] && [ "${FORCE:-0}" != "1" ]; then
printf '%s\n' "secrets already present in $SECRETS_DIR (FORCE=1 to regenerate)"
exit 0
fi
postgres_password="$(random_password)"
valkey_password="$(random_password)"
write_if_missing postgres_password "$postgres_password"
write_if_missing valkey_password "$valkey_password"
write_if_missing database_url "postgresql://messenger:${postgres_password}@postgres:5432/messenger"
write_if_missing cache_url "redis://:${valkey_password}@valkey:6379/0"
printf '%s\n' "wrote secrets to $SECRETS_DIR: postgres_password, valkey_password, database_url, cache_url"
printf '%s\n' "next: docker compose --env-file .env up -d --build"
+15
View File
@@ -0,0 +1,15 @@
#!/bin/sh
# Seed 12 000 synthetic messages in a dedicated chat and capture the plans for
# the first page, a deep keyset page and the naive OFFSET alternative.
# Output goes to evidence/explain-history.txt (text, no secrets).
set -eu
cd "$(dirname "$0")/.."
mkdir -p evidence
docker compose --env-file .env exec -T postgres \
psql -X -U messenger -d messenger -v ON_ERROR_STOP=1 -f - \
< scripts/explain-history.sql > evidence/explain-history.txt 2>&1
cat evidence/explain-history.txt
printf '%s\n' "saved evidence/explain-history.txt"
+75
View File
@@ -0,0 +1,75 @@
-- Index justification for messages (chat_id, created_at, id).
-- Fixtures are deterministic and synthetic; re-running changes nothing.
\set ON_ERROR_STOP on
\timing on
INSERT INTO users (id, username, display_name, password_hash, created_at)
VALUES ('11111111-1111-4111-8111-111111111111', 'seed_user', 'Seed User',
'not-a-usable-hash', now())
ON CONFLICT (id) DO NOTHING;
INSERT INTO chats (id, title, created_at)
VALUES ('22222222-2222-4222-8222-222222222222', 'Seed history', now())
ON CONFLICT (id) DO NOTHING;
INSERT INTO chat_members (chat_id, user_id, joined_at)
VALUES ('22222222-2222-4222-8222-222222222222',
'11111111-1111-4111-8111-111111111111', now())
ON CONFLICT DO NOTHING;
DO $$
BEGIN
IF (SELECT count(*) FROM messages
WHERE chat_id = '22222222-2222-4222-8222-222222222222') = 0 THEN
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
SELECT gen_random_uuid(),
'22222222-2222-4222-8222-222222222222',
'11111111-1111-4111-8111-111111111111',
'seed message ' || g,
timestamptz '2026-01-01 00:00:00+00' + make_interval(secs => g * 0.001)
FROM generate_series(1, 12000) AS g;
END IF;
END $$;
ANALYZE messages;
\echo '=== dataset ==='
SELECT count(*) AS messages_in_chat
FROM messages
WHERE chat_id = '22222222-2222-4222-8222-222222222222';
\echo '=== index used by the history query ==='
SELECT indexname, indexdef FROM pg_indexes WHERE tablename = 'messages';
\echo '=== plan: first page (no cursor) ==='
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = '22222222-2222-4222-8222-222222222222'
ORDER BY created_at, id
LIMIT 50;
-- Pick the 10 001st record as the cursor of a deep page.
SELECT created_at AS cursor_ts, id AS cursor_id
FROM messages
WHERE chat_id = '22222222-2222-4222-8222-222222222222'
ORDER BY created_at, id
OFFSET 10000 LIMIT 1
\gset
\echo '=== plan: deep keyset page after 10 000 records (what the API sends) ==='
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = '22222222-2222-4222-8222-222222222222'
AND (created_at, id) > (:'cursor_ts'::timestamptz, :'cursor_id'::uuid)
ORDER BY created_at, id
LIMIT 50;
\echo '=== comparison: the OFFSET form this API does not use ==='
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = '22222222-2222-4222-8222-222222222222'
ORDER BY created_at, id
OFFSET 10000 LIMIT 50;
+17
View File
@@ -0,0 +1,17 @@
@echo off
rem Windows copy of scripts/restart.sh for `make test-persistence`.
rem Recreates the API and BOTH state stores from the same images, keeping the
rem named volumes (pgdata, valkeydata): no `down -v`, no seed, no data reset.
setlocal
cd /d "%~dp0.."
echo restart: stopping api, postgres and valkey (volumes preserved)
docker compose --env-file .env down --timeout 15
if errorlevel 1 exit /b 1
echo restart: starting api, postgres and valkey
docker compose --env-file .env up -d --wait --wait-timeout 120
if errorlevel 1 exit /b 1
docker compose --env-file .env ps
echo stack_restarted volumes_preserved
+17
View File
@@ -0,0 +1,17 @@
#!/bin/sh
# Restart hook for `make test-persistence ACTION_SCRIPT=scripts/restart.sh`.
# Recreates the API and BOTH state stores from the same images, keeping the
# named volumes (pgdata, valkeydata): no `down -v`, no seed, no data reset.
set -eu
cd "$(dirname "$0")/.."
ENV_FILE="${COMPOSE_ENV_FILE:-.env}"
printf '%s\n' "restart: stopping api, postgres and valkey (volumes preserved)"
docker compose --env-file "$ENV_FILE" down --timeout 15
printf '%s\n' "restart: starting api, postgres and valkey"
docker compose --env-file "$ENV_FILE" up -d --wait --wait-timeout 120
docker compose --env-file "$ENV_FILE" ps
printf '%s\n' "stack_restarted volumes_preserved"
+16
View File
@@ -0,0 +1,16 @@
#!/bin/sh
# Start the dedicated short-TTL stand, run the expiry tests, always tear it down.
set -eu
cd "$(dirname "$0")/.."
PORT="${TTL_PORT:-8081}"
status=0
sh scripts/ttl-stack.sh start
cd student-tests
BASE_URL="http://127.0.0.1:$PORT" "${PYTHON:-python}" -B -m unittest -v test_expiry || status=$?
cd ..
sh scripts/ttl-stack.sh stop
exit "$status"
+27
View File
@@ -0,0 +1,27 @@
#!/bin/sh
# Separate short-TTL stand used by `make test-ttl`.
# It is a distinct Compose project on its own port with its own disposable
# volumes, so the main stand and its synthetic data are untouched.
set -eu
cd "$(dirname "$0")/.."
PROJECT="${TTL_PROJECT:-mtusi-lab2-ttl}"
PORT="${TTL_PORT:-8081}"
ACTION="${1:-}"
case "$ACTION" in
start)
SESSION_TTL_SECONDS=2 API_PUBLISH_PORT="$PORT" \
docker compose -p "$PROJECT" --env-file .env up -d --wait --wait-timeout 180
printf 'ttl_stack_ready base_url=http://127.0.0.1:%s session_ttl_seconds=2\n' "$PORT"
;;
stop)
# -v removes only this dedicated synthetic stand's volumes.
docker compose -p "$PROJECT" --env-file .env down -v --timeout 10
printf 'ttl_stack_removed\n'
;;
*)
printf 'usage: %s start|stop\n' "$0" >&2
exit 2
;;
esac