create skeleton for lab 3
This commit is contained in:
Executable
+28
@@ -0,0 +1,28 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
BASE_URL="${BASE_URL:-http://localhost:8080}"
|
||||
TTL="${TTL:?укажите TTL, с которым запущен API (SESSION_TTL_SECONDS)}"
|
||||
API="$BASE_URL/api/v1"
|
||||
USERNAME="expiry_$(date +%s)"
|
||||
PASSWORD="expiry-password-123"
|
||||
|
||||
code() {
|
||||
curl -s -o /dev/null -w '%{http_code}' "$@"
|
||||
}
|
||||
|
||||
curl -sf -o /dev/null -X POST "$API/users" -H 'Content-Type: application/json' \
|
||||
-d "{\"username\":\"$USERNAME\",\"display_name\":\"Expiry\",\"password\":\"$PASSWORD\"}"
|
||||
SESSION=$(curl -sf -X POST "$API/auth/sessions" -u "$USERNAME:$PASSWORD")
|
||||
TOKEN=$(printf '%s' "$SESSION" | python3 -c 'import json, sys; print(json.load(sys.stdin)["token"])')
|
||||
EXPIRES_AT=$(printf '%s' "$SESSION" | python3 -c 'import json, sys; print(json.load(sys.stdin)["expires_at"])')
|
||||
AUTH="Authorization: Bearer $TOKEN"
|
||||
|
||||
echo "SESSION_TTL_SECONDS=$TTL, login at $(date -u +%H:%M:%S)Z, expires_at=$EXPIRES_AT"
|
||||
echo "GET /users/me right after login -> $(code "$API/users/me" -H "$AUTH")"
|
||||
sleep $((TTL + 1))
|
||||
printf "%-35s -> %s\n" "GET /users/me after $((TTL + 1)) s" "$(code "$API/users/me" -H "$AUTH")"
|
||||
echo "DELETE /auth/sessions/current after -> $(code -X DELETE "$API/auth/sessions/current" -H "$AUTH")"
|
||||
NEW_TOKEN=$(curl -sf -X POST "$API/auth/sessions" -u "$USERNAME:$PASSWORD" \
|
||||
| python3 -c 'import json, sys; print(json.load(sys.stdin)["token"])')
|
||||
echo "GET /users/me with new login token -> $(code "$API/users/me" -H "Authorization: Bearer $NEW_TOKEN")"
|
||||
@@ -0,0 +1,132 @@
|
||||
"""EXPLAIN (ANALYZE, BUFFERS) запроса истории на синтетических данных.
|
||||
|
||||
Данные создаются в одной транзакции и откатываются в конце: база не меняется.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
from uuid import uuid4
|
||||
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.asyncio import AsyncConnection
|
||||
|
||||
from app.core.config import Settings
|
||||
from app.core.database import create_db_engine
|
||||
|
||||
TARGET_MESSAGES = 10_000
|
||||
NOISE_MESSAGES = 40_000
|
||||
PAGE = 51
|
||||
|
||||
HISTORY_FIRST_PAGE = """
|
||||
SELECT * FROM messages
|
||||
WHERE chat_id = :chat_id
|
||||
ORDER BY created_at, id
|
||||
LIMIT :page
|
||||
"""
|
||||
|
||||
HISTORY_AFTER_CURSOR = """
|
||||
SELECT * FROM messages
|
||||
WHERE chat_id = :chat_id AND (created_at, id) > (:after_created_at, :after_id)
|
||||
ORDER BY created_at, id
|
||||
LIMIT :page
|
||||
"""
|
||||
|
||||
|
||||
async def seed(connection: AsyncConnection) -> dict[str, object]:
|
||||
user_id, target_id, noise_id = uuid4(), uuid4(), uuid4()
|
||||
await connection.execute(
|
||||
text(
|
||||
"INSERT INTO users (id, username, display_name, password_hash) "
|
||||
"VALUES (:id, :username, 'Explain', 'x')"
|
||||
),
|
||||
{"id": user_id, "username": "explain_" + uuid4().hex[:8]},
|
||||
)
|
||||
await connection.execute(
|
||||
text("INSERT INTO chats (id, title) VALUES (:a, 'target'), (:b, 'noise')"),
|
||||
{"a": target_id, "b": noise_id},
|
||||
)
|
||||
await connection.execute(
|
||||
text("INSERT INTO chat_members (chat_id, user_id) VALUES (:a, :u), (:b, :u)"),
|
||||
{"a": target_id, "b": noise_id, "u": user_id},
|
||||
)
|
||||
for chat_id, count in ((target_id, TARGET_MESSAGES), (noise_id, NOISE_MESSAGES)):
|
||||
await connection.execute(
|
||||
text(
|
||||
"INSERT INTO messages (id, chat_id, sender_id, text, created_at) "
|
||||
"SELECT gen_random_uuid(), :chat_id, :user_id, 'message ' || g, "
|
||||
"now() - make_interval(secs => :count - g) "
|
||||
"FROM generate_series(1, :count) AS g"
|
||||
),
|
||||
{"chat_id": chat_id, "user_id": user_id, "count": count},
|
||||
)
|
||||
await connection.execute(text("ANALYZE messages"))
|
||||
|
||||
middle = (
|
||||
await connection.execute(
|
||||
text(
|
||||
"SELECT created_at, id FROM messages WHERE chat_id = :c "
|
||||
"ORDER BY created_at, id OFFSET :o LIMIT 1"
|
||||
),
|
||||
{"c": target_id, "o": TARGET_MESSAGES // 2},
|
||||
)
|
||||
).one()
|
||||
return {
|
||||
"chat_id": target_id,
|
||||
"after_created_at": middle.created_at,
|
||||
"after_id": middle.id,
|
||||
"page": PAGE,
|
||||
}
|
||||
|
||||
|
||||
async def explain(
|
||||
connection: AsyncConnection, title: str, query: str, params: dict[str, object]
|
||||
) -> None:
|
||||
rows = await connection.execute(
|
||||
text(f"EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING ON) {query}"), params
|
||||
)
|
||||
print(f"== {title}")
|
||||
print(query.strip())
|
||||
print()
|
||||
for row in rows:
|
||||
print(row[0])
|
||||
print()
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
engine = create_db_engine(Settings()) # type: ignore[call-arg]
|
||||
async with engine.connect() as connection:
|
||||
transaction = await connection.begin()
|
||||
params = await seed(connection)
|
||||
total = (
|
||||
await connection.execute(text("SELECT count(*) FROM messages"))
|
||||
).scalar_one()
|
||||
print(
|
||||
f"messages in table: {total}; target chat: {TARGET_MESSAGES}; noise chat: {NOISE_MESSAGES}"
|
||||
)
|
||||
print()
|
||||
|
||||
first_page = {"chat_id": params["chat_id"], "page": PAGE}
|
||||
await explain(
|
||||
connection, "first page (limit+1 = 51)", HISTORY_FIRST_PAGE, first_page
|
||||
)
|
||||
await explain(
|
||||
connection,
|
||||
"page after cursor in the middle of history",
|
||||
HISTORY_AFTER_CURSOR,
|
||||
params,
|
||||
)
|
||||
|
||||
await connection.execute(text("SET LOCAL enable_indexscan = off"))
|
||||
await connection.execute(text("SET LOCAL enable_bitmapscan = off"))
|
||||
await explain(
|
||||
connection,
|
||||
"same page with index scans disabled (for comparison)",
|
||||
HISTORY_AFTER_CURSOR,
|
||||
params,
|
||||
)
|
||||
|
||||
await transaction.rollback()
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Executable
+64
@@ -0,0 +1,64 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
BASE_URL="${BASE_URL:-http://localhost:8080}"
|
||||
API="$BASE_URL/api/v1"
|
||||
USERNAME="demo_$(date +%s)"
|
||||
PASSWORD="demo-password-123"
|
||||
|
||||
compose() {
|
||||
docker compose --env-file .env "$@"
|
||||
}
|
||||
|
||||
code() {
|
||||
curl -s -o /dev/null -w '%{http_code} (%{time_total}s)' "$@"
|
||||
}
|
||||
|
||||
wait_healthy() {
|
||||
attempts=0
|
||||
until [ "$(compose ps --format '{{.Health}}' "$1")" = "healthy" ]; do
|
||||
attempts=$((attempts + 1))
|
||||
[ "$attempts" -lt 60 ] || { echo "$1 не стал healthy" >&2; exit 1; }
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
curl -sf -o /dev/null -X POST "$API/users" -H 'Content-Type: application/json' \
|
||||
-d "{\"username\":\"$USERNAME\",\"display_name\":\"Demo\",\"password\":\"$PASSWORD\"}"
|
||||
TOKEN=$(curl -sf -X POST "$API/auth/sessions" -u "$USERNAME:$PASSWORD" \
|
||||
| python3 -c 'import json, sys; print(json.load(sys.stdin)["token"])')
|
||||
AUTH="Authorization: Bearer $TOKEN"
|
||||
|
||||
echo "== baseline"
|
||||
echo "GET /users/me (Bearer) -> $(code "$API/users/me" -H "$AUTH")"
|
||||
|
||||
echo
|
||||
echo "== redis stopped"
|
||||
compose stop redis >/dev/null 2>&1
|
||||
echo "GET /users/me (Bearer) -> $(code "$API/users/me" -H "$AUTH")"
|
||||
echo "POST /auth/sessions (Basic) -> $(code -X POST "$API/auth/sessions" -u "$USERNAME:$PASSWORD")"
|
||||
echo "GET /users/me (no token) -> $(code "$API/users/me")"
|
||||
echo "GET /health/ready -> $(code "$BASE_URL/health/ready")"
|
||||
echo "GET /health/live -> $(code "$BASE_URL/health/live")"
|
||||
compose start redis >/dev/null 2>&1
|
||||
wait_healthy redis
|
||||
echo "-- redis started again (AOF restored)"
|
||||
echo "GET /users/me (same token) -> $(code "$API/users/me" -H "$AUTH")"
|
||||
|
||||
echo
|
||||
echo "== postgres stopped"
|
||||
compose stop postgres >/dev/null 2>&1
|
||||
echo "GET /users/me (Bearer) -> $(code "$API/users/me" -H "$AUTH")"
|
||||
echo "GET /api/v1/chats (Bearer) -> $(code "$API/chats" -H "$AUTH")"
|
||||
echo "GET /health/ready -> $(code "$BASE_URL/health/ready")"
|
||||
echo "GET /health/live -> $(code "$BASE_URL/health/live")"
|
||||
compose start postgres >/dev/null 2>&1
|
||||
wait_healthy postgres
|
||||
echo "-- postgres started again"
|
||||
echo "GET /users/me (same token) -> $(code "$API/users/me" -H "$AUTH")"
|
||||
|
||||
echo
|
||||
echo "== logout"
|
||||
echo "DELETE /auth/sessions/current -> $(code -X DELETE "$API/auth/sessions/current" -H "$AUTH")"
|
||||
echo "GET /users/me (same token) -> $(code "$API/users/me" -H "$AUTH")"
|
||||
Executable
+35
@@ -0,0 +1,35 @@
|
||||
#!/bin/sh
|
||||
# Перезапуск API и всех хранилищ с сохранением volumes (make test-persistence).
|
||||
# У api несколько реплик, перед ними nginx.
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
compose() {
|
||||
docker compose --env-file .env "$@"
|
||||
}
|
||||
|
||||
# Ждёт, пока ВСЕ контейнеры service станут healthy (у api их несколько).
|
||||
wait_healthy() {
|
||||
for service in "$@"; do
|
||||
attempts=0
|
||||
until states=$(compose ps --format '{{.Health}}' "$service") \
|
||||
&& [ -n "$states" ] \
|
||||
&& [ -z "$(printf '%s\n' "$states" | grep -v '^healthy$')" ]; do
|
||||
attempts=$((attempts + 1))
|
||||
if [ "$attempts" -ge 60 ]; then
|
||||
echo "$service не стал healthy за 60 секунд" >&2
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
compose restart postgres redis
|
||||
wait_healthy postgres redis
|
||||
compose restart api
|
||||
wait_healthy api
|
||||
# TODO(3.3): если nginx перерезолвит DNS сам, рестарт LB не обязателен —
|
||||
# но перезапуск «всего стенда» в защите всё равно его включает.
|
||||
compose restart nginx
|
||||
wait_healthy nginx
|
||||
Executable
+21
@@ -0,0 +1,21 @@
|
||||
#!/bin/sh
|
||||
# Останавливает одну API-реплику по TARGET_INSTANCE_ID (сценарий отказа, make test-failover).
|
||||
# TODO(3.5): реализовать — см. docs/LAB3_TASKS.md, задача 3.5.
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
|
||||
# Что нужно сделать:
|
||||
# 1. Сопоставить TARGET_INSTANCE_ID (значение X-Instance-Id) с контейнером.
|
||||
# Проще всего, если X-Instance-Id = короткий id контейнера (HOSTNAME):
|
||||
# docker compose --env-file .env ps -q api -> полные id реплик api
|
||||
# и найти тот, что начинается с TARGET_INSTANCE_ID.
|
||||
# 2. Убедиться, что это контейнер service `api` ЭТОГО compose-проекта,
|
||||
# а не postgres/nginx/чужой контейнер. Не нашли -> exit 1.
|
||||
# 3. Остановить его (docker stop [-t N]) и оставить остановленным.
|
||||
# Не трогать БД/LB/volumes. Уложиться в 10 секунд.
|
||||
# 4. Напечатать (в stderr), что именно остановлено — пригодится для отчёта.
|
||||
# Восстановление — отдельно: make restore.
|
||||
|
||||
echo 'TODO: implement stop-one.sh' >&2
|
||||
exit 2
|
||||
Reference in New Issue
Block a user