исправление - символ не дает 500

This commit is contained in:
2026-10-09 21:09:19 +03:00
parent be89a23dfa
commit 1d02ee3f33
11 changed files with 143 additions and 69 deletions
+8 -8
View File
@@ -3,24 +3,24 @@ docker compose --profile tools run --rm db-test
Container mtusi-lab2-valkey-1 Running
Container mtusi-lab2-valkey-1 Waiting
Container mtusi-lab2-postgres-1 Waiting
Container mtusi-lab2-postgres-1 Healthy
Container mtusi-lab2-valkey-1 Healthy
Container mtusi-lab2-db-test-run-a1c49274ce09 Creating
Container mtusi-lab2-db-test-run-a1c49274ce09 Created
Container mtusi-lab2-postgres-1 Healthy
Container mtusi-lab2-db-test-run-343a686d3ad8 Creating
Container mtusi-lab2-db-test-run-343a686d3ad8 Created
test_concurrent_registration_has_one_winner (test_store.StoreIntegrationTest.test_concurrent_registration_has_one_winner) ...
20 параллельных create_user: 1 создан, 19 получили Conflict (409)
ok
test_create_chat_rolls_back_when_failing_midway (test_store.StoreIntegrationTest.test_create_chat_rolls_back_when_failing_midway) ...
чат с несуществующим участником: транзакция откатилась, строк chats/chat_members не осталось
ok
test_database_rejects_message_from_non_member (test_store.StoreIntegrationTest.test_database_rejects_message_from_non_member) ...
test_database_rejects_message_from_non_member (test_store.StoreIntegrationTest.test_database_rejects_message_from_non_member) ... ok
test_migrations_rerun_is_noop (test_store.StoreIntegrationTest.test_migrations_rerun_is_noop) ...
INSERT сообщения от не-участника напрямую в БД: ForeignKeyViolation
ok
test_migrations_rerun_is_noop (test_store.StoreIntegrationTest.test_migrations_rerun_is_noop) ...
schema_migrations: ['001_core', '002_sessions']; повторный migrate() ничего не применил
ok
schema_migrations: ['001_core', '002_sessions', '003_text_as_bytes']; повторный migrate() ничего не применил
----------------------------------------------------------------------
Ran 4 tests in 0.120s
Ran 4 tests in 0.190s
OK
+32 -32
View File
@@ -2,7 +2,7 @@ docker compose exec -T postgres psql -X -q -U messenger -d messenger -v ON_ERROR
=== Объём данных
messages_total | messages_in_big_chat
----------------+----------------------
21618 | 12000
21650 | 12000
=== Индекс истории (migrations/001_core.sql)
@@ -14,58 +14,58 @@ docker compose exec -T postgres psql -X -q -U messenger -d messenger -v ON_ERROR
=== 1. Первая страница (limit=50, приложение просит 51 строку, чтобы понять, есть ли следующая)
QUERY PLAN
---------------------------------------------------------------------------------------------------------
Limit (actual time=0.018..0.041 rows=51 loops=1)
Buffers: shared hit=5
-> Index Scan using messages_chat_history_idx on messages (actual time=0.017..0.033 rows=51 loops=1)
Index Cond: (chat_id = '4981747a-a263-417e-80a3-fb7063eb8be5'::uuid)
Buffers: shared hit=5
Limit (actual time=0.023..0.043 rows=51 loops=1)
Buffers: shared hit=6
-> Index Scan using messages_chat_history_idx on messages (actual time=0.022..0.036 rows=51 loops=1)
Index Cond: (chat_id = 'b0b6cd43-57d9-4deb-99f3-e04443a88e4c'::uuid)
Buffers: shared hit=6
Planning:
Buffers: shared hit=6
Planning Time: 0.139 ms
Execution Time: 0.066 ms
Planning Time: 0.160 ms
Execution Time: 0.077 ms
=== 2. Страница из середины истории по keyset-курсору (после 6000-го сообщения)
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=0.009..0.020 rows=51 loops=1)
Buffers: shared hit=4
-> Index Scan using messages_chat_history_idx on messages (actual time=0.009..0.016 rows=51 loops=1)
Index Cond: ((chat_id = '4981747a-a263-417e-80a3-fb7063eb8be5'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, '41b24ea4-c9d1-467e-a5cc-6b4a5413a8be'::uuid)))
Buffers: shared hit=4
Planning Time: 0.068 ms
Execution Time: 0.076 ms
Limit (actual time=0.019..0.055 rows=51 loops=1)
Buffers: shared hit=6
-> Index Scan using messages_chat_history_idx on messages (actual time=0.018..0.044 rows=51 loops=1)
Index Cond: ((chat_id = 'b0b6cd43-57d9-4deb-99f3-e04443a88e4c'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, 'bc979085-5d84-44a6-9df2-3b1fff1bdaf1'::uuid)))
Buffers: shared hit=6
Planning Time: 0.157 ms
Execution Time: 0.122 ms
=== 3. Для сравнения: та же страница через OFFSET (так приложение НЕ делает)
QUERY PLAN
-----------------------------------------------------------------------------------------------------------
Limit (actual time=1.377..1.386 rows=51 loops=1)
Buffers: shared hit=161
-> Index Scan using messages_chat_history_idx on messages (actual time=0.023..1.131 rows=6051 loops=1)
Index Cond: (chat_id = '4981747a-a263-417e-80a3-fb7063eb8be5'::uuid)
Buffers: shared hit=161
Planning Time: 0.099 ms
Execution Time: 1.406 ms
Limit (actual time=1.586..1.603 rows=51 loops=1)
Buffers: shared hit=163
-> Index Scan using messages_chat_history_idx on messages (actual time=0.020..1.305 rows=6051 loops=1)
Index Cond: (chat_id = 'b0b6cd43-57d9-4deb-99f3-e04443a88e4c'::uuid)
Buffers: shared hit=163
Planning Time: 0.095 ms
Execution Time: 1.625 ms
=== 4. Для сравнения: запрос 2 без индекса (индекс удалён только внутри этой транзакции)
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=2.522..2.528 rows=51 loops=1)
Buffers: shared hit=296
-> Sort (actual time=2.522..2.524 rows=51 loops=1)
Limit (actual time=4.238..4.247 rows=51 loops=1)
Buffers: shared hit=289
-> Sort (actual time=4.237..4.241 rows=51 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 31kB
Buffers: shared hit=296
-> Seq Scan on messages (actual time=0.631..2.057 rows=6000 loops=1)
Filter: ((chat_id = '4981747a-a263-417e-80a3-fb7063eb8be5'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, '41b24ea4-c9d1-467e-a5cc-6b4a5413a8be'::uuid)))
Rows Removed by Filter: 15618
Buffers: shared hit=296
Buffers: shared hit=289
-> Seq Scan on messages (actual time=0.967..3.376 rows=6000 loops=1)
Filter: ((chat_id = 'b0b6cd43-57d9-4deb-99f3-e04443a88e4c'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, 'bc979085-5d84-44a6-9df2-3b1fff1bdaf1'::uuid)))
Rows Removed by Filter: 15650
Buffers: shared hit=289
Planning:
Buffers: shared hit=4
Planning Time: 0.128 ms
Execution Time: 2.554 ms
Planning Time: 0.119 ms
Execution Time: 4.268 ms
=== ROLLBACK: синтетические данные удалены, индекс на месте
+12 -12
View File
@@ -1,21 +1,21 @@
docker compose ps -a --format "table {{.Service}}\t{{.ID}}\t{{.Status}}"
SERVICE CONTAINER ID STATUS
api 08b92853bbb4 Up 3 minutes (healthy)
api-short-ttl 047512285fe6 Up About a minute (healthy)
postgres 094d2f567e2e Up 49 seconds (healthy)
valkey 195f4a7b30f1 Up About a minute (healthy)
api 719af7c38c2d Up 41 seconds (healthy)
api-short-ttl 047512285fe6 Up 8 hours (healthy)
postgres 094d2f567e2e Up 8 hours (healthy)
valkey 195f4a7b30f1 Up 7 hours (healthy)
python tests/resilience.py --mode persistence --action-script "scripts/restart.cmd" --base-url "http://127.0.0.1:8080"
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
real 0m11.081s
real 0m12.535s
user 0m0.015s
sys 0m0.000s
sys 0m0.015s
docker compose ps -a --format "table {{.Service}}\t{{.ID}}\t{{.Status}}"
SERVICE CONTAINER ID STATUS
api 08b92853bbb4 Up 2 seconds (health: starting)
api-short-ttl 047512285fe6 Up About a minute (healthy)
postgres 094d2f567e2e Up 8 seconds (healthy)
valkey 195f4a7b30f1 Up 8 seconds (healthy)
api-1 | 2026-10-09 10:18:10,200 INFO messenger.db: Миграции применены: 001_core, 002_sessions
api-1 | 2026-10-09 10:21:37,443 INFO messenger.db: Миграции: новых нет, схема актуальна
api 719af7c38c2d Up 3 seconds (health: starting)
api-short-ttl 047512285fe6 Up 8 hours (healthy)
postgres 094d2f567e2e Up 9 seconds (healthy)
valkey 195f4a7b30f1 Up 9 seconds (healthy)
api-1 | 2026-10-09 17:57:26,685 INFO messenger.db: Миграции применены: 003_text_as_bytes
api-1 | 2026-10-09 17:58:17,783 INFO messenger.db: Миграции: новых нет, схема актуальна
+2 -1
View File
@@ -6,6 +6,7 @@ test_chat_membership_rules (test_api.MessengerApiTest.test_chat_membership_rules
test_duplicate_username_returns_409 (test_api.MessengerApiTest.test_duplicate_username_returns_409) ... ok
test_empty_blank_and_too_long_messages_return_400 (test_api.MessengerApiTest.test_empty_blank_and_too_long_messages_return_400) ... ok
test_malformed_json_returns_400 (test_api.MessengerApiTest.test_malformed_json_returns_400) ... ok
test_nul_character_is_stored (test_api.MessengerApiTest.test_nul_character_is_stored) ... ok
test_outsider_gets_404_for_foreign_chat (test_api.MessengerApiTest.test_outsider_gets_404_for_foreign_chat) ... ok
test_pagination_walks_several_pages (test_api.MessengerApiTest.test_pagination_walks_several_pages) ... ok
test_unicode_roundtrip (test_api.MessengerApiTest.test_unicode_roundtrip) ... ok
@@ -22,6 +23,6 @@ test_wrong_password_and_unknown_user_look_the_same (test_auth.AuthTest.test_wron
test_x_user_id_does_not_change_identity (test_auth.AuthTest.test_x_user_id_does_not_change_identity) ... ok
----------------------------------------------------------------------
Ran 21 tests in 2.181s
Ran 22 tests in 3.300s
OK