ЛР 2: PostgreSQL, Bearer-сессии, Valkey и исследование восстановления

This commit is contained in:
Алихан Абдурашидов
2026-10-30 14:09:36 +03:00
parent 65003b6119
commit cddd65fe4e
40 changed files with 7890 additions and 35 deletions
+4
View File
@@ -0,0 +1,4 @@
.venv/bin/python scripts/check_contract.py
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
.venv/bin/python scripts/validate_openapi.py
PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
File diff suppressed because it is too large Load Diff
+49
View File
@@ -0,0 +1,49 @@
{
"date_utc": "2026-10-30T11:00:43.989800+00:00",
"checks": {
"valkey_really_caches_profiles": {
"cached_profiles": 48,
"all_profiles_public_fields_only": true,
"positive_ttl": true
},
"password_kdf_and_hashed_tokens": {
"all_passwords_scrypt": true,
"all_token_digests_32_bytes": true
},
"cache_outage": {
"fallback_profile_and_history": true,
"anonymous_rejected": true,
"logout_effective": true,
"scenario_seconds": 0.85
},
"cache_restart_no_revoked_session_resurrection": true,
"cache_flush_repopulates_from_postgres": true,
"postgres_outage": {
"authenticated_request_503_seconds": 2.01,
"live_200": true,
"ready_503": true,
"no_anonymous_trust": true
},
"postgres_recovery_same_session": true,
"expiry_ttl_2_seconds": true,
"repeat_migration_does_not_duplicate_data": {
"migrations": 1,
"users": 50
},
"full_stack_persistence": {
"api_and_all_stores_restarted": true,
"before": {
"api": "2026-10-30T10:58:17.679191091Z",
"postgres": "2026-10-30T11:00:54.063549678Z",
"valkey": "2026-10-30T11:00:46.458543341Z"
},
"after": {
"api": "2026-10-30T11:01:04.399852418Z",
"postgres": "2026-10-30T11:01:04.247473126Z",
"valkey": "2026-10-30T11:01:04.435663252Z"
},
"elapsed_seconds": 7.924
}
},
"status": "PASS"
}
+49
View File
@@ -0,0 +1,49 @@
{
"date_utc": "2026-10-30T11:00:43.989800+00:00",
"checks": {
"valkey_really_caches_profiles": {
"cached_profiles": 48,
"all_profiles_public_fields_only": true,
"positive_ttl": true
},
"password_kdf_and_hashed_tokens": {
"all_passwords_scrypt": true,
"all_token_digests_32_bytes": true
},
"cache_outage": {
"fallback_profile_and_history": true,
"anonymous_rejected": true,
"logout_effective": true,
"scenario_seconds": 0.85
},
"cache_restart_no_revoked_session_resurrection": true,
"cache_flush_repopulates_from_postgres": true,
"postgres_outage": {
"authenticated_request_503_seconds": 2.01,
"live_200": true,
"ready_503": true,
"no_anonymous_trust": true
},
"postgres_recovery_same_session": true,
"expiry_ttl_2_seconds": true,
"repeat_migration_does_not_duplicate_data": {
"migrations": 1,
"users": 50
},
"full_stack_persistence": {
"api_and_all_stores_restarted": true,
"before": {
"api": "2026-10-30T10:58:17.679191091Z",
"postgres": "2026-10-30T11:00:54.063549678Z",
"valkey": "2026-10-30T11:00:46.458543341Z"
},
"after": {
"api": "2026-10-30T11:01:04.399852418Z",
"postgres": "2026-10-30T11:01:04.247473126Z",
"valkey": "2026-10-30T11:01:04.435663252Z"
},
"elapsed_seconds": 7.924
}
},
"status": "PASS"
}
+3
View File
@@ -0,0 +1,3 @@
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8082"
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
+25
View File
@@ -0,0 +1,25 @@
index,chat_size,query,p50,p95,p99,mean,qps
chat_only,80000,keyset,7.0761,7.1933,7.1945,7.0884,141.064
chat_only,80000,offset,24.9378,28.3299,29.9997,25.4597,39.275
chat_only,15000,keyset,1.4773,1.5489,1.5723,1.4867,672.438
chat_only,15000,offset,4.1769,8.9294,9.3571,4.5287,220.78
chat_only,4000,keyset,0.6269,0.8833,0.9718,0.6489,1539.583
chat_only,4000,offset,0.6617,1.123,1.5037,0.729,1370.731
chat_only,1000,keyset,0.3048,0.443,0.6305,0.325,3072.865
chat_only,1000,offset,0.3366,0.4242,0.4286,0.3492,2860.548
keyset,80000,keyset,0.2249,0.2545,0.3508,0.2318,4307.199
keyset,80000,offset,6.9243,12.6501,15.0282,7.8992,126.569
keyset,15000,keyset,0.2069,0.2373,0.2455,0.2108,4736.312
keyset,15000,offset,1.3274,6.6483,6.7603,2.32,430.747
keyset,4000,keyset,0.2088,0.2822,0.3122,0.2174,4590.519
keyset,4000,offset,0.5051,0.657,0.7066,0.5265,1897.228
keyset,1000,keyset,0.1998,0.235,0.2685,0.205,4868.845
keyset,1000,offset,0.2825,0.517,0.5368,0.3155,3164.696
covering,80000,keyset,0.1993,0.3393,0.404,0.2336,4272.262
covering,80000,offset,6.7655,7.1888,7.3155,6.8453,146.073
covering,15000,keyset,0.2027,0.2262,0.2397,0.206,4840.044
covering,15000,offset,1.374,1.5291,1.5962,1.3926,717.922
covering,4000,keyset,0.1897,0.2183,0.2226,0.1935,5160.995
covering,4000,offset,0.508,0.7336,0.8249,0.5293,1888.336
covering,1000,keyset,0.1999,0.2138,0.2153,0.2028,4925.03
covering,1000,offset,0.2774,0.3663,0.4765,0.2948,3388.315
1 index chat_size query p50 p95 p99 mean qps
2 chat_only 80000 keyset 7.0761 7.1933 7.1945 7.0884 141.064
3 chat_only 80000 offset 24.9378 28.3299 29.9997 25.4597 39.275
4 chat_only 15000 keyset 1.4773 1.5489 1.5723 1.4867 672.438
5 chat_only 15000 offset 4.1769 8.9294 9.3571 4.5287 220.78
6 chat_only 4000 keyset 0.6269 0.8833 0.9718 0.6489 1539.583
7 chat_only 4000 offset 0.6617 1.123 1.5037 0.729 1370.731
8 chat_only 1000 keyset 0.3048 0.443 0.6305 0.325 3072.865
9 chat_only 1000 offset 0.3366 0.4242 0.4286 0.3492 2860.548
10 keyset 80000 keyset 0.2249 0.2545 0.3508 0.2318 4307.199
11 keyset 80000 offset 6.9243 12.6501 15.0282 7.8992 126.569
12 keyset 15000 keyset 0.2069 0.2373 0.2455 0.2108 4736.312
13 keyset 15000 offset 1.3274 6.6483 6.7603 2.32 430.747
14 keyset 4000 keyset 0.2088 0.2822 0.3122 0.2174 4590.519
15 keyset 4000 offset 0.5051 0.657 0.7066 0.5265 1897.228
16 keyset 1000 keyset 0.1998 0.235 0.2685 0.205 4868.845
17 keyset 1000 offset 0.2825 0.517 0.5368 0.3155 3164.696
18 covering 80000 keyset 0.1993 0.3393 0.404 0.2336 4272.262
19 covering 80000 offset 6.7655 7.1888 7.3155 6.8453 146.073
20 covering 15000 keyset 0.2027 0.2262 0.2397 0.206 4840.044
21 covering 15000 offset 1.374 1.5291 1.5962 1.3926 717.922
22 covering 4000 keyset 0.1897 0.2183 0.2226 0.1935 5160.995
23 covering 4000 offset 0.508 0.7336 0.8249 0.5293 1888.336
24 covering 1000 keyset 0.1999 0.2138 0.2153 0.2028 4925.03
25 covering 1000 offset 0.2774 0.3663 0.4765 0.2948 3388.315
+2
View File
@@ -0,0 +1,2 @@
Benchmarking 100000 messages, 4 skewed chats, 3 indexes and 2 queries...
PASS: restored 100000 messages to a new PostgreSQL container/volume; counts, 7 samples, complete SHA256 and foreign keys verified.
+38
View File
@@ -0,0 +1,38 @@
# План запроса и восстановление
| Индекс | Сообщений в чате | Запрос | p50, мс | p95, мс | p99, мс | Sequential QPS |
| --- | ---: | --- | ---: | ---: | ---: | ---: |
| chat_only | 80000 | keyset | 7.0761 | 7.1933 | 7.1945 | 141.064 |
| chat_only | 80000 | offset | 24.9378 | 28.3299 | 29.9997 | 39.275 |
| chat_only | 15000 | keyset | 1.4773 | 1.5489 | 1.5723 | 672.438 |
| chat_only | 15000 | offset | 4.1769 | 8.9294 | 9.3571 | 220.78 |
| chat_only | 4000 | keyset | 0.6269 | 0.8833 | 0.9718 | 1539.583 |
| chat_only | 4000 | offset | 0.6617 | 1.123 | 1.5037 | 1370.731 |
| chat_only | 1000 | keyset | 0.3048 | 0.443 | 0.6305 | 3072.865 |
| chat_only | 1000 | offset | 0.3366 | 0.4242 | 0.4286 | 2860.548 |
| keyset | 80000 | keyset | 0.2249 | 0.2545 | 0.3508 | 4307.199 |
| keyset | 80000 | offset | 6.9243 | 12.6501 | 15.0282 | 126.569 |
| keyset | 15000 | keyset | 0.2069 | 0.2373 | 0.2455 | 4736.312 |
| keyset | 15000 | offset | 1.3274 | 6.6483 | 6.7603 | 430.747 |
| keyset | 4000 | keyset | 0.2088 | 0.2822 | 0.3122 | 4590.519 |
| keyset | 4000 | offset | 0.5051 | 0.657 | 0.7066 | 1897.228 |
| keyset | 1000 | keyset | 0.1998 | 0.235 | 0.2685 | 4868.845 |
| keyset | 1000 | offset | 0.2825 | 0.517 | 0.5368 | 3164.696 |
| covering | 80000 | keyset | 0.1993 | 0.3393 | 0.404 | 4272.262 |
| covering | 80000 | offset | 6.7655 | 7.1888 | 7.3155 | 146.073 |
| covering | 15000 | keyset | 0.2027 | 0.2262 | 0.2397 | 4840.044 |
| covering | 15000 | offset | 1.374 | 1.5291 | 1.5962 | 717.922 |
| covering | 4000 | keyset | 0.1897 | 0.2183 | 0.2226 | 5160.995 |
| covering | 4000 | offset | 0.508 | 0.7336 | 0.8249 | 1888.336 |
| covering | 1000 | keyset | 0.1999 | 0.2138 | 0.2153 | 4925.03 |
| covering | 1000 | offset | 0.2774 | 0.3663 | 0.4765 | 3388.315 |
| Индекс | Индексы messages, MiB | INSERT 1000 + COMMIT p50, мс | p95, мс | Средний WAL на 1000 строк, bytes |
| --- | ---: | ---: | ---: | ---: |
| chat_only | 5.203 | 9.6394 | 11.5286 | 379956 |
| keyset | 11.156 | 10.289 | 12.1678 | 453492 |
| covering | 27.609 | 13.9626 | 64.6203 | 708616 |
Backup: 3004503 bytes, 0.5288 с. Новый PostgreSQL/volume: 1.3216 с. Restore: 0.6135 с. Provision + restore: 1.9351 с.
Сверены количества всех таблиц, 7 выбранных сообщений, SHA-256 всей истории из 100 000 сообщений, отсутствие нарушенных связей. Запись после снимка ожидаемо отсутствует в восстановленной БД.
File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 121 KiB

+2
View File
@@ -0,0 +1,2 @@
python3 tests/smoke.py --base-url "http://localhost:8082"
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
+17
View File
@@ -0,0 +1,17 @@
python3 -m unittest discover -s student-tests -p 'test_*.py' -v
test_100_messages_10_clients (test_api.APItests.test_100_messages_10_clients) ... ok
test_authentication_before_access_and_foreign_chat (test_api.APItests.test_authentication_before_access_and_foreign_chat) ... ok
test_bad_password_unknown_username_and_basic_only_for_login (test_api.APItests.test_bad_password_unknown_username_and_basic_only_for_login) ... ok
test_chat_rollback_and_membership_validation (test_api.APItests.test_chat_rollback_and_membership_validation) ... ok
test_empty_message_unicode_and_sender (test_api.APItests.test_empty_message_unicode_and_sender) ... ok
test_logout_revokes_only_current_session (test_api.APItests.test_logout_revokes_only_current_session) ... ok
test_malformed_json_and_types (test_api.APItests.test_malformed_json_and_types) ... ok
test_multiple_pages_bad_limits_and_cross_resource_cursor (test_api.APItests.test_multiple_pages_bad_limits_and_cross_resource_cursor) ... ok
test_racing_registration (test_api.APItests.test_racing_registration) ... ok
test_read_after_write_during_pagination (test_api.APItests.test_read_after_write_during_pagination) ... ok
test_unicode_password_128_codepoints_and_colon (test_api.APItests.test_unicode_password_128_codepoints_and_colon) ... ok
----------------------------------------------------------------------
Ran 11 tests in 6.839s
OK