lab2 - оценка 5
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
2026-10-07 19:06:42 +03:00
parent 65003b6119
commit f07ad1e097
32 changed files with 3718 additions and 18 deletions
+60
View File
@@ -0,0 +1,60 @@
@echo off
setlocal enabledelayedexpansion
chcp 65001 >nul
cd /d "%~dp0.."
set OUT=evidence\cache_track.md
if not exist evidence mkdir evidence
> %OUT% echo # Трек 5: согласованность кэша истории — замеры и демонстрации
>> %OUT% echo Сформировано: %DATE% %TIME%
>> %OUT% echo.
echo [1/5] Baseline: CACHE_MODE=off
set CACHE_MODE=off
docker compose up -d api >nul
timeout /t 12 /nobreak >nul
python student-tests\cache_consistency.py --phase off > evidence\phase_off.txt 2>&1
if errorlevel 1 ( type evidence\phase_off.txt & goto :fail )
type evidence\phase_off.txt
type evidence\phase_off.txt >> %OUT%
echo. >> %OUT%
echo [2/5] Naive cache: CACHE_MODE=naive
set CACHE_MODE=naive
docker compose up -d api >nul
timeout /t 12 /nobreak >nul
python student-tests\cache_consistency.py --phase naive > evidence\phase_naive.txt 2>&1
if errorlevel 1 ( type evidence\phase_naive.txt & goto :fail )
type evidence\phase_naive.txt
type evidence\phase_naive.txt >> %OUT%
echo. >> %OUT%
echo [3/5] Protected cache: CACHE_MODE=versioned
set CACHE_MODE=versioned
docker compose up -d api >nul
timeout /t 12 /nobreak >nul
python student-tests\cache_consistency.py --phase versioned > evidence\phase_versioned.txt 2>&1
if errorlevel 1 ( type evidence\phase_versioned.txt & goto :fail )
type evidence\phase_versioned.txt
type evidence\phase_versioned.txt >> %OUT%
echo. >> %OUT%
echo [4/5] Recovery after redis restart
python student-tests\cache_consistency.py --phase recovery > evidence\phase_recovery.txt 2>&1
if errorlevel 1 ( type evidence\phase_recovery.txt & goto :fail )
type evidence\phase_recovery.txt
type evidence\phase_recovery.txt >> %OUT%
echo. >> %OUT%
echo [5/5] Restoring default CACHE_MODE
set CACHE_MODE=
docker compose up -d api >nul
timeout /t 12 /nobreak >nul
echo Done: %OUT%
exit /b 0
:fail
echo PHASE FAILED — see evidence\phase_*.txt
set CACHE_MODE=
docker compose up -d api >nul
exit /b 1
+11
View File
@@ -0,0 +1,11 @@
@echo off
cd /d "%~dp0.."
echo [expiry] starting api with SESSION_TTL_SECONDS=2
set SESSION_TTL_SECONDS=2
docker compose up -d api
timeout /t 10 /nobreak >nul
python student-tests\expiry_checks.py --base-url http://localhost:8080
set SESSION_TTL_SECONDS=
echo [expiry] restoring default TTL
docker compose up -d api
timeout /t 10 /nobreak >nul
+100
View File
@@ -0,0 +1,100 @@
@echo off
setlocal enabledelayedexpansion
chcp 65001 >nul
cd /d "%~dp0.."
rem Артефакты складываем рядом с bat-файлом
set MD=%~dp0explain_plans.md
set PW=%~dp0plan_with_index.txt
set PN=%~dp0plan_no_index.txt
echo === EXPLAIN demo for messages_history_idx ===
echo.
echo [1/8] Fetch chat and member ids...
for /f "usebackq tokens=*" %%i in (`docker compose exec -T postgres psql -U messenger -d messenger -tAc "SELECT id FROM chats ORDER BY created_at LIMIT 1;"`) do set CHAT=%%i
for /f "usebackq tokens=*" %%i in (`docker compose exec -T postgres psql -U messenger -d messenger -tAc "SELECT user_id FROM chat_members WHERE chat_id='%CHAT%' LIMIT 1;"`) do set SENDER=%%i
echo Chat: %CHAT%
echo Sender: %SENDER%
if not exist student-tests\seed.sql (
echo ERROR: student-tests\seed.sql not found
exit /b 1
)
if not exist student-tests\explain.sql (
echo ERROR: student-tests\explain.sql not found
exit /b 1
)
echo.
echo [2/8] Seeding 10000 messages...
type student-tests\seed.sql | docker compose exec -T postgres psql -U messenger -d messenger -v chat=%CHAT% -v sender=%SENDER% -f -
if errorlevel 1 (
echo ERROR: seed failed
exit /b 1
)
for /f "usebackq tokens=*" %%c in (`docker compose exec -T postgres psql -U messenger -d messenger -tAc "SELECT count(*) FROM messages WHERE chat_id='%CHAT%';"`) do set ROWS=%%c
echo Seed done. Rows in chat: %ROWS%
echo.
echo [3/8] ANALYZE messages (свежая статистика для планировщика)...
docker compose exec postgres psql -U messenger -d messenger -c "ANALYZE messages;"
echo.
echo [4/8] Index presence check (ожидаем messages_history_idx в списке):
docker compose exec -T postgres psql -U messenger -d messenger -tAc "SELECT indexname FROM pg_indexes WHERE tablename='messages';"
echo.
echo [5/8] EXPLAIN WITH index -^> %PW%
type student-tests\explain.sql | docker compose exec -T postgres psql -U messenger -d messenger -v chat=%CHAT% -f - > %PW%
type %PW%
echo.
echo [6/8] Dropping index...
docker compose exec postgres psql -U messenger -d messenger -c "DROP INDEX IF EXISTS messages_history_idx;"
echo.
echo [7/8] EXPLAIN WITHOUT index -^> %PN%
type student-tests\explain.sql | docker compose exec -T postgres psql -U messenger -d messenger -v chat=%CHAT% -f - > %PN%
type %PN%
echo.
echo [8/8] Restoring index + assembling %MD% ...
docker compose exec postgres psql -U messenger -d messenger -c "CREATE INDEX IF NOT EXISTS messages_history_idx ON messages (chat_id, created_at, id);"
docker compose exec postgres psql -U messenger -d messenger -c "ANALYZE messages;"
> %MD% echo # EXPLAIN-планы истории сообщений: messages_history_idx
>> %MD% echo.
>> %MD% echo Сформировано: %DATE% %TIME%
>> %MD% echo Чат: %CHAT%
>> %MD% echo Сообщений в чате: %ROWS%
>> %MD% echo Статистика: ANALYZE messages выполнен после сева и после восстановления индекса.
>> %MD% echo Запрос: keyset-пагинация первой страницы, LIMIT 51, курсор = начало времён.
>> %MD% echo.
>> %MD% echo ## План С индексом messages_history_idx
>> %MD% echo.
>> %MD% echo ```text
type %PW% >> %MD%
>> %MD% echo ```
>> %MD% echo.
>> %MD% echo Ожидаемая подпись: Index Scan using messages_history_idx, Index Cond содержит
>> %MD% echo chat_id и ROW^(created_at, id^) ^> ROW^(...^); Buffers — единицы/десятки страниц.
>> %MD% echo.
>> %MD% echo ## План БЕЗ индекса: контрольный прогон
>> %MD% echo.
>> %MD% echo ```text
type %PN% >> %MD%
>> %MD% echo ```
>> %MD% echo.
>> %MD% echo Ожидаемая подпись: Sort + Seq Scan on messages или Bitmap Heap Scan по pkey
>> %MD% echo с фильтром по всей истории; Buffers заметно больше, Execution Time выше.
>> %MD% echo.
>> %MD% echo ## Вывод
>> %MD% echo Индекс messages_history_idx покрывает запрос keyset-пагинации: с ним план
>> %MD% echo читает только нужные строки через Index Scan по ^(chat_id, created_at, id^),
>> %MD% echo без него планировщик вынужден выбирать всю историю чата и сортировать её.
>> %MD% echo Пагинация не выполняет полный scan истории на каждую страницу.
echo Done: %MD%
echo.
echo === EXPLAIN demo finished ===
endlocal
+60
View File
@@ -0,0 +1,60 @@
# EXPLAIN-планы истории сообщений: messages_history_idx
Сформировано: 07.10.2026 17:59:48,83
Чат: 1781106d-bfbe-4064-9ab4-a99f955636b0
Сообщений в чате: 30002
Статистика: ANALYZE messages выполнен после сева и после восстановления индекса.
Запрос: keyset-пагинация первой страницы, LIMIT 51, курсор = начало времён.
## План С индексом messages_history_idx
```text
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.41..5.06 rows=51 width=65) (actual time=0.049..0.059 rows=51 loops=1)
Buffers: shared hit=4 read=1
-> Index Scan using messages_history_idx on messages (cost=0.41..2732.85 rows=30002 width=65) (actual time=0.048..0.055 rows=51 loops=1)
Index Cond: ((chat_id = '1781106d-bfbe-4064-9ab4-a99f955636b0'::uuid) AND (ROW(created_at, id) > ROW('2020-01-01 00:00:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
Buffers: shared hit=4 read=1
Planning:
Buffers: shared hit=109
Planning Time: 0.864 ms
Execution Time: 0.118 ms
(9 rows)
```
Ожидаемая подпись: Index Scan using messages_history_idx, Index Cond содержит
chat_id и ROW(created_at, id) > ROW(...); Buffers — единицы/десятки страниц.
## План БЕЗ индекса: контрольный прогон
```text
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=1912.07..1912.19 rows=51 width=65) (actual time=4.535..4.541 rows=51 loops=1)
Buffers: shared hit=383
-> Sort (cost=1912.07..1987.07 rows=30002 width=65) (actual time=4.533..4.536 rows=51 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 38kB
Buffers: shared hit=383
-> Seq Scan on messages (cost=0.00..911.14 rows=30002 width=65) (actual time=0.010..2.480 rows=30002 loops=1)
Filter: ((chat_id = '1781106d-bfbe-4064-9ab4-a99f955636b0'::uuid) AND (ROW(created_at, id) > ROW('2020-01-01 00:00:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
Rows Removed by Filter: 520
Buffers: shared hit=377
Planning:
Buffers: shared hit=87 dirtied=1
Planning Time: 0.536 ms
Execution Time: 4.613 ms
(14 rows)
```
Ожидаемая подпись: Sort + Seq Scan on messages или Bitmap Heap Scan по pkey
с фильтром по всей истории; Buffers заметно больше, Execution Time выше.
## Вывод
Индекс messages_history_idx покрывает запрос keyset-пагинации: с ним план
читает только нужные строки через Index Scan по (chat_id, created_at, id),
без него планировщик вынужден выбирать всю историю чата и сортировать её.
Пагинация не выполняет полный scan истории на каждую страницу.
+10
View File
@@ -0,0 +1,10 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
echo "Restarting all services (preserving volumes)..."
docker compose restart postgres redis api
echo "Restart completed. Waiting for readiness is done by test harness."
+2 -2
View File
@@ -1,10 +1,10 @@
#!/bin/sh
set -eu
# TODO: copy to restart.sh and implement using YOUR compose/stack services.
# TODO: copy to restart.bat and implement using YOUR compose/stack services.
# Restart API and ALL used state stores (DB, sessions, S3, broker as applicable).
# Preserve persistent volumes and schema/data; never down -v / flush / re-seed.
# Allow operator unseal if required in lab7; do not print credentials.
# Exit 0 only after the intended action; test waits for API readiness separately.
printf '%s
' 'TODO: implement restart.sh for your infrastructure' >&2
' 'TODO: implement restart.bat for your infrastructure' >&2
exit 2