ЛР2: PostgreSQL, сессии и Valkey на оценку 4

This commit is contained in:
gera
2026-10-16 00:50:33 +03:00
parent 65003b6119
commit f612fbef77
32 changed files with 2339 additions and 21 deletions
+59
View File
@@ -0,0 +1,59 @@
#!/bin/sh
set -eu
ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
OUT="${1:-evidence/explain.txt}"
mkdir -p "$(dirname "$OUT")"
echo "=== seed >=10000 messages ==="
docker compose --env-file .env exec -T postgres psql -U messenger -d messenger <<'SQL'
do $$
declare
uid uuid := gen_random_uuid();
cid uuid := gen_random_uuid();
begin
insert into users(id, username, display_name, password_hash, created_at)
values (uid, 'explain_seed_' || substr(replace(uid::text,'-',''),1,8), 'Explain', 'x', now())
on conflict do nothing;
insert into chats(id, title, created_at) values (cid, 'explain chat', now());
insert into chat_members(chat_id, user_id) values (cid, uid);
insert into messages(id, chat_id, sender_id, text, created_at)
select gen_random_uuid(), cid, uid, 'msg-' || g, now() - ((10001 - g) * interval '1 millisecond')
from generate_series(1, 10001) g;
raise notice 'chat_id=%', cid;
end $$;
SQL
CHAT=$(docker compose --env-file .env exec -T postgres psql -U messenger -d messenger -Atc \
"select id from chats where title='explain chat' order by created_at desc limit 1;")
echo "chat_id=$CHAT"
{
echo "date=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo "chat_id=$CHAT"
echo "count=$(docker compose --env-file .env exec -T postgres psql -U messenger -d messenger -Atc "select count(*) from messages where chat_id='$CHAT';")"
echo
echo "=== keyset with index messages_history_idx ==="
docker compose --env-file .env exec -T postgres psql -U messenger -d messenger <<SQL
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = '$CHAT'
AND (created_at, id) > ('1970-01-01'::timestamptz, '00000000-0000-4000-8000-000000000000'::uuid)
ORDER BY created_at, id
LIMIT 50;
SQL
echo
echo "=== OFFSET page ~200 (full scan risk) ==="
docker compose --env-file .env exec -T postgres psql -U messenger -d messenger <<SQL
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = '$CHAT'
ORDER BY created_at, id
OFFSET 5000 LIMIT 50;
SQL
} > "$OUT"
echo "wrote $OUT"
cat "$OUT"
echo "PASS: explain captured"
+13
View File
@@ -0,0 +1,13 @@
#!/bin/sh
set -eu
ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)"
mkdir -p "$ROOT/secrets"
if [ ! -f "$ROOT/secrets/postgres_password" ]; then
openssl rand -base64 24 | tr -d '\n' > "$ROOT/secrets/postgres_password"
fi
PASS=$(cat "$ROOT/secrets/postgres_password")
ENC=$(printf '%s' "$PASS" | python3 -c 'import sys,urllib.parse; print(urllib.parse.quote(sys.stdin.read(), safe=""))')
printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable\n' "$ENC" > "$ROOT/secrets/database_url"
printf 'redis://valkey:6379/0\n' > "$ROOT/secrets/cache_url"
chmod 644 "$ROOT/secrets/"*
echo "secrets ready in $ROOT/secrets"
+7
View File
@@ -0,0 +1,7 @@
#!/bin/sh
set -eu
ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
docker compose --env-file .env stop
docker compose --env-file .env start
exit 0
+54
View File
@@ -0,0 +1,54 @@
#!/bin/sh
set -eu
ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
BASE="${BASE_URL:-http://127.0.0.1:8080}"
USER="dep_$(date +%s)"
PASS="T3st-$(openssl rand -hex 8)"
curl -sf -X POST "$BASE/api/v1/users" \
-H 'Content-Type: application/json' \
-d "{\"username\":\"$USER\",\"display_name\":\"Deps\",\"password\":\"$PASS\"}" >/tmp/dep_user.json
CRED=$(printf '%s:%s' "$USER" "$PASS" | base64 -w0 2>/dev/null || printf '%s:%s' "$USER" "$PASS" | base64)
SESS=$(curl -sf -X POST "$BASE/api/v1/auth/sessions" -H "Authorization: Basic $CRED")
TOKEN=$(printf '%s' "$SESS" | python3 -c 'import sys,json; print(json.load(sys.stdin)["token"])')
echo "=== stop valkey (cache bypass) ==="
docker compose --env-file .env stop valkey >/dev/null
sleep 1
code=$(curl -sS -o /tmp/dep_vk.json -w '%{http_code}' -H "Authorization: Bearer $TOKEN" "$BASE/api/v1/users/me")
echo "with valkey down /users/me -> $code (expect 200)"
test "$code" = "200"
echo "=== start valkey, stop postgres ==="
docker compose --env-file .env start valkey >/dev/null
i=0
while [ "$i" -lt 20 ]; do
if docker compose --env-file .env exec -T valkey valkey-cli ping 2>/dev/null | grep -q PONG; then
break
fi
i=$((i + 1))
sleep 0.5
done
docker compose --env-file .env stop postgres >/dev/null
sleep 1
ready=$(curl -sS -o /tmp/dep_ready.json -w '%{http_code}' "$BASE/health/ready" || true)
me=$(curl -sS -o /tmp/dep_me.json -w '%{http_code}' -H "Authorization: Bearer $TOKEN" "$BASE/api/v1/users/me" || true)
echo "postgres down ready=$ready me=$me (expect 503/503)"
test "$ready" = "503"
test "$me" = "503"
echo "=== restore postgres ==="
docker compose --env-file .env start postgres >/dev/null
i=0
while [ "$i" -lt 40 ]; do
if curl -sf "$BASE/health/ready" >/dev/null 2>&1; then
break
fi
i=$((i + 1))
sleep 0.5
done
code=$(curl -sS -o /tmp/dep_ok.json -w '%{http_code}' -H "Authorization: Bearer $TOKEN" "$BASE/api/v1/users/me")
echo "restored /users/me -> $code (expect 200)"
test "$code" = "200"
echo "PASS: dependency failures"
+46
View File
@@ -0,0 +1,46 @@
#!/bin/sh
set -eu
ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
BASE="${BASE_URL:-http://127.0.0.1:8080}"
echo "=== restart api with SESSION_TTL_SECONDS=2 ==="
SESSION_TTL_SECONDS=2 docker compose --env-file .env up -d --no-deps --force-recreate api >/dev/null
i=0
while [ "$i" -lt 40 ]; do
if curl -sf "$BASE/health/ready" >/dev/null 2>&1; then
break
fi
i=$((i + 1))
sleep 0.5
done
USER="exp_$(date +%s)"
PASS="T3st-$(openssl rand -hex 8)"
curl -sf -X POST "$BASE/api/v1/users" \
-H 'Content-Type: application/json' \
-d "{\"username\":\"$USER\",\"display_name\":\"Expiry\",\"password\":\"$PASS\"}" >/tmp/exp_user.json
CRED=$(printf '%s:%s' "$USER" "$PASS" | base64 -w0 2>/dev/null || printf '%s:%s' "$USER" "$PASS" | base64)
SESS=$(curl -sf -X POST "$BASE/api/v1/auth/sessions" -H "Authorization: Basic $CRED")
TOKEN=$(printf '%s' "$SESS" | python3 -c 'import sys,json; print(json.load(sys.stdin)["token"])')
code=$(curl -sS -o /tmp/exp_me1.json -w '%{http_code}' -H "Authorization: Bearer $TOKEN" "$BASE/api/v1/users/me")
echo "immediate /users/me -> $code (expect 200)"
test "$code" = "200"
echo "sleep 3 for expiry"
sleep 3
code=$(curl -sS -o /tmp/exp_me2.json -w '%{http_code}' -H "Authorization: Bearer $TOKEN" "$BASE/api/v1/users/me" || true)
echo "after ttl /users/me -> $code (expect 401)"
test "$code" = "401"
echo "=== restore SESSION_TTL_SECONDS=3600 ==="
SESSION_TTL_SECONDS=3600 docker compose --env-file .env up -d --no-deps --force-recreate api >/dev/null
i=0
while [ "$i" -lt 40 ]; do
if curl -sf "$BASE/health/ready" >/dev/null 2>&1; then
break
fi
i=$((i + 1))
sleep 0.5
done
echo "PASS: session expiry"