forked from mtusi2026/mtusi2026lab3
feat: implement estim 4
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
COMPOSE=${COMPOSE:-docker compose}
|
||||
|
||||
{
|
||||
echo "# scripts/explain.sh; date: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||
$COMPOSE exec -T postgres psql -U messenger -d messenger -X -q < scripts/explain.sql
|
||||
} > evidence/explain.txt
|
||||
cat evidence/explain.txt
|
||||
@@ -1,66 +0,0 @@
|
||||
\set ON_ERROR_STOP on
|
||||
\set big_chat '''00000000-0000-4000-8000-00000000e001'''
|
||||
\set seed_user '''00000000-0000-4000-8000-00000000e000'''
|
||||
|
||||
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
||||
VALUES (:seed_user, 'explain_seed', 'Explain seed', 'not-a-login', now())
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT INTO chats (id, title, created_at)
|
||||
SELECT CASE WHEN g = 0 THEN :big_chat::uuid
|
||||
ELSE ('00000000-0000-4000-8000-' || lpad(to_hex(57345 + g), 12, '0'))::uuid END,
|
||||
'explain chat ' || g, now()
|
||||
FROM generate_series(0, 20) AS g
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT INTO chat_members (chat_id, user_id, position)
|
||||
SELECT id, :seed_user, 0 FROM chats WHERE title LIKE 'explain chat %'
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT gen_random_uuid(), c.id, :seed_user, 'synthetic message ' || n,
|
||||
timestamptz '2026-01-01' + n * interval '1 second'
|
||||
FROM chats c
|
||||
CROSS JOIN LATERAL generate_series(1, CASE WHEN c.id = :big_chat THEN 20000 ELSE 2000 END) AS n
|
||||
WHERE c.title LIKE 'explain chat %'
|
||||
AND NOT EXISTS (SELECT 1 FROM messages m WHERE m.chat_id = c.id);
|
||||
|
||||
ANALYZE users;
|
||||
ANALYZE chats;
|
||||
ANALYZE chat_members;
|
||||
ANALYZE messages;
|
||||
|
||||
SELECT count(*) AS messages_total,
|
||||
count(*) FILTER (WHERE chat_id = :big_chat) AS messages_in_big_chat
|
||||
FROM messages;
|
||||
|
||||
SELECT created_at AS cur_ts, id AS cur_id
|
||||
FROM messages WHERE chat_id = :big_chat
|
||||
ORDER BY created_at, id OFFSET 9999 LIMIT 1 \gset
|
||||
|
||||
\echo '=== 1. First page (limit=50 -> LIMIT 51), index messages_chat_created_id_idx'
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :big_chat
|
||||
ORDER BY created_at, id
|
||||
LIMIT 51;
|
||||
|
||||
\echo '=== 2. Page in the middle by keyset cursor (after 10 000 messages)'
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :big_chat AND (created_at, id) > (:'cur_ts', :'cur_id')
|
||||
ORDER BY created_at, id
|
||||
LIMIT 51;
|
||||
|
||||
\echo '=== 3. Same middle page WITHOUT the index (dropped inside a rolled-back transaction)'
|
||||
BEGIN;
|
||||
DROP INDEX messages_chat_created_id_idx;
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :big_chat AND (created_at, id) > (:'cur_ts', :'cur_id')
|
||||
ORDER BY created_at, id
|
||||
LIMIT 51;
|
||||
ROLLBACK;
|
||||
@@ -1,27 +0,0 @@
|
||||
#!/bin/sh
|
||||
set -u
|
||||
cd "$(dirname "$0")/.."
|
||||
B=http://localhost:8080
|
||||
C=${COMPOSE:-docker compose}
|
||||
u="outage_$(head -c4 /dev/urandom | od -An -tx1 | tr -d ' \n')"; p="Outage-demo-password"
|
||||
code() { curl -s -o /dev/null -w '%{http_code}' "$@"; }
|
||||
wait_ready() { for i in $(seq 1 60); do [ "$(code $B/health/ready)" = 200 ] && return; sleep 1; done; }
|
||||
echo "# Dependency outage demo on the live stand; date: $(date -u +%Y-%m-%dT%H:%M:%SZ). Token is kept in a shell variable, never printed."
|
||||
curl -s -o /dev/null -X POST $B/api/v1/users -H 'Content-Type: application/json' -d "{\"username\":\"$u\",\"display_name\":\"Demo\",\"password\":\"$p\"}"
|
||||
T=$(curl -s -X POST $B/api/v1/auth/sessions -u "$u:$p" | python3 -c 'import sys,json;print(json.load(sys.stdin)["token"])')
|
||||
echo "baseline: GET /users/me (Bearer) -> $(code $B/api/v1/users/me -H "Authorization: Bearer $T")"
|
||||
echo; echo "## $C stop redis"; $C stop redis >/dev/null 2>&1
|
||||
echo "GET /health/ready -> $(code $B/health/ready)"
|
||||
echo "GET /users/me (valid Bearer) -> $(code $B/api/v1/users/me -H "Authorization: Bearer $T")"
|
||||
echo "GET /users/me (no auth) -> $(code $B/api/v1/users/me)"
|
||||
echo "POST /auth/sessions (Basic) -> $(code -X POST $B/api/v1/auth/sessions -u "$u:$p")"
|
||||
echo "body: $(curl -s $B/api/v1/users/me -H "Authorization: Bearer $T")"
|
||||
$C start redis >/dev/null 2>&1; wait_ready
|
||||
echo "## $C start redis -> ready; same token: GET /users/me -> $(code $B/api/v1/users/me -H "Authorization: Bearer $T")"
|
||||
echo; echo "## $C stop postgres"; $C stop postgres >/dev/null 2>&1
|
||||
echo "GET /health/ready -> $(code $B/health/ready)"
|
||||
echo "GET /users/me (valid Bearer) -> $(code $B/api/v1/users/me -H "Authorization: Bearer $T")"
|
||||
echo "GET /chats (valid Bearer) -> $(code $B/api/v1/chats -H "Authorization: Bearer $T")"
|
||||
echo "body: $(curl -s $B/api/v1/chats -H "Authorization: Bearer $T")"
|
||||
$C start postgres >/dev/null 2>&1; wait_ready
|
||||
echo "## $C start postgres -> ready; same token: GET /users/me -> $(code $B/api/v1/users/me -H "Authorization: Bearer $T")"
|
||||
+2
-1
@@ -1,7 +1,8 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
COMPOSE=${COMPOSE:-podman compose}
|
||||
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
|
||||
COMPOSE=${COMPOSE:-$([ "$ENGINE" = podman ] && echo podman-compose || echo "docker compose")}
|
||||
|
||||
$COMPOSE stop lb api redis postgres
|
||||
$COMPOSE start postgres redis api lb
|
||||
|
||||
Executable
+37
@@ -0,0 +1,37 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
|
||||
COMPOSE=${COMPOSE:-$([ "$ENGINE" = podman ] && echo podman-compose || echo "docker compose")}
|
||||
PROJECT=${PROJECT:-messenger-lab3}
|
||||
|
||||
replicas() {
|
||||
$ENGINE ps -aq --no-trunc --filter "label=com.docker.compose.project=$PROJECT" --filter "label=com.docker.compose.service=api" | sort
|
||||
}
|
||||
|
||||
wait_healthy() {
|
||||
for _ in $(seq 1 60); do
|
||||
if [ "$($ENGINE inspect -f '{{.State.Health.Status}}' "$1" 2>/dev/null)" = "healthy" ]; then
|
||||
return 0
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
echo "replica $1 did not become healthy" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
$ENGINE build --build-arg "BUILD_ID=${BUILD_ID:-$(date +%s)}" -t localhost/messenger-api:lab3 .
|
||||
$COMPOSE run --rm migrate
|
||||
|
||||
for old in $(replicas); do
|
||||
before=$(replicas)
|
||||
$ENGINE stop -t 10 "$old"
|
||||
$ENGINE rm "$old"
|
||||
$COMPOSE up -d --no-deps --no-recreate api
|
||||
for id in $(replicas); do
|
||||
if ! echo "$before" | grep -q "$id"; then
|
||||
wait_healthy "$id"
|
||||
fi
|
||||
done
|
||||
sleep 2
|
||||
done
|
||||
+2
-2
@@ -1,12 +1,12 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
ENGINE=${ENGINE:-podman}
|
||||
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
|
||||
PROJECT=${PROJECT:-messenger-lab3}
|
||||
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}"
|
||||
|
||||
for id in $($ENGINE ps -q --filter "label=com.docker.compose.project=$PROJECT" --filter "label=com.docker.compose.service=api"); do
|
||||
if [ "$($ENGINE inspect -f '{{.Config.Hostname}}' "$id")" = "$TARGET_INSTANCE_ID" ]; then
|
||||
$ENGINE stop -t 5 "$id" >/dev/null
|
||||
$ENGINE stop -t 10 "$id" >/dev/null
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user