Перенести решение лабораторной 2 как основу для лабораторной 3

This commit is contained in:
2026-11-02 10:00:11 +03:00
parent 0ab3a341aa
commit 5b9f377783
76 changed files with 7008 additions and 0 deletions
+75
View File
@@ -0,0 +1,75 @@
-- EXPLAIN evidence for the message history query (make explain).
--
-- Everything runs in one transaction that is rolled back: it seeds 12 000
-- messages in one chat and 90 000 in 300 other chats, so the plan is measured
-- on a realistic volume without leaving data on the stand. The PREPARE text
-- is the statement of Messages.ListByChat in internal/chat/store/postgres.go;
-- keep them identical.
\set ON_ERROR_STOP on
\pset pager off
BEGIN;
SELECT uuidv7() AS author \gset
SELECT uuidv7() AS big_chat \gset
INSERT INTO users (id, username, display_name, password_hash, created_at)
VALUES (:'author', 'explain_' || substr(md5(random()::text), 1, 12), 'EXPLAIN', 'not-a-password-hash',
uuid_extract_timestamp(:'author'));
CREATE TEMP TABLE noise_chats ON COMMIT DROP AS
SELECT uuidv7() AS id FROM generate_series(1, 300);
INSERT INTO chats (id, title, created_at)
SELECT :'big_chat'::uuid, 'explain: big chat', uuid_extract_timestamp(:'big_chat')
UNION ALL
SELECT id, 'explain: other chat', uuid_extract_timestamp(id) FROM noise_chats;
INSERT INTO chat_members (chat_id, user_id)
SELECT id, :'author' FROM chats WHERE title LIKE 'explain:%';
-- Rows are inserted ordered by n, so the messages of all chats interleave in
-- the heap as they would under real traffic; ids stay increasing per chat.
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
SELECT id, chat_id, :'author', 'message ' || n, uuid_extract_timestamp(id)
FROM (
SELECT uuidv7() AS id, chat_id, n
FROM (
SELECT :'big_chat'::uuid AS chat_id, n FROM generate_series(1, 12000) AS n
UNION ALL
SELECT c.id, n FROM noise_chats AS c, generate_series(1, 300) AS n
) AS planned
ORDER BY n
) AS generated;
ANALYZE users, chats, chat_members, messages;
SELECT (SELECT count(*) FROM messages WHERE chat_id = :'big_chat') AS big_chat_messages,
(SELECT count(*) FROM messages) AS all_messages;
PREPARE history(uuid, uuid, int) AS
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = $1
AND id > COALESCE($2, '00000000-0000-0000-0000-000000000000'::uuid)
ORDER BY id
LIMIT $3;
\echo
\echo == First page: no cursor, limit 50 (+1 row that tells whether a next page exists)
EXPLAIN (ANALYZE, BUFFERS) EXECUTE history(:'big_chat', NULL, 51);
SELECT id AS deep_cursor FROM messages WHERE chat_id = :'big_chat' ORDER BY id OFFSET 10000 LIMIT 1 \gset
\echo
\echo == Deep page: cursor after the 10 001st message of the chat
EXPLAIN (ANALYZE, BUFFERS) EXECUTE history(:'big_chat', :'deep_cursor', 51);
\echo
\echo == For contrast, OFFSET pagination of the same page reads every skipped row
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = :'big_chat'
ORDER BY id
OFFSET 10001 LIMIT 51;
ROLLBACK;
+42
View File
@@ -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"
+5
View File
@@ -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%
+9
View File
@@ -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