Добавить вложения: S3, метаданные в PostgreSQL и очистка незавершённых загрузок

This commit is contained in:
2026-10-30 23:10:44 +03:00
parent ec540a1a72
commit d812d8f8d8
75 changed files with 5968 additions and 16 deletions
+81
View File
@@ -0,0 +1,81 @@
# Запуск из чистого clone на пустых volumes, публичный smoke
# дата: 2026-10-30T19:58Z, чистый clone
# стенд: Apple M3 Pro, 11 CPU, 18 GB RAM, macOS 26.1, Docker 29.3.0, Compose 5.1.0
$ make env
./scripts/init-env.sh
пароли записаны в .env
$ docker compose --env-file .env config --quiet
$ docker compose --env-file .env up -d --build
Image mtusi-messenger:lab4 Built
Image mtusi-messenger-s3:lab4 Built
Image mtusi-messenger:lab4 Built
Image mtusi-messenger:lab4 Built
Image mtusi-messenger:lab4 Built
Image mtusi-messenger-lb:lab4 Built
Network mtusi-messenger-lab4_storage Created
Network mtusi-messenger-lab4_data Created
Network mtusi-messenger-lab4_edge Created
Volume mtusi-messenger-lab4_redisdata Created
Volume mtusi-messenger-lab4_s3data Created
Volume mtusi-messenger-lab4_pgdata Created
Container mtusi-messenger-lab4-redis-1 Created
Container mtusi-messenger-lab4-s3-1 Created
Container mtusi-messenger-lab4-postgres-1 Created
Container mtusi-messenger-lab4-s3-init-1 Created
Container mtusi-messenger-lab4-migrate-1 Created
Container mtusi-messenger-lab4-api-1 Created
Container mtusi-messenger-lab4-janitor-1 Created
Container mtusi-messenger-lab4-api-2 Created
Container mtusi-messenger-lab4-lb-1 Created
Container mtusi-messenger-lab4-postgres-1 Started
Container mtusi-messenger-lab4-redis-1 Started
Container mtusi-messenger-lab4-s3-1 Started
Container mtusi-messenger-lab4-postgres-1 Healthy
Container mtusi-messenger-lab4-s3-1 Healthy
Container mtusi-messenger-lab4-migrate-1 Started
Container mtusi-messenger-lab4-s3-init-1 Started
Container mtusi-messenger-lab4-redis-1 Healthy
Container mtusi-messenger-lab4-migrate-1 Exited
Container mtusi-messenger-lab4-migrate-1 Exited
Container mtusi-messenger-lab4-s3-init-1 Exited
Container mtusi-messenger-lab4-s3-init-1 Exited
Container mtusi-messenger-lab4-janitor-1 Started
Container mtusi-messenger-lab4-api-1 Started
Container mtusi-messenger-lab4-api-2 Started
Container mtusi-messenger-lab4-lb-1 Started
$ ./scripts/wait-ready.sh
готов: http://localhost:8080
$ docker compose --env-file .env ps -a
SERVICE IMAGE STATE STATUS PORTS
api mtusi-messenger:lab4 running Up 4 seconds (healthy) 8080/tcp
api mtusi-messenger:lab4 running Up 4 seconds (healthy) 8080/tcp
janitor mtusi-messenger:lab4 running Up 4 seconds 8080/tcp
lb mtusi-messenger-lb:lab4 running Up 4 seconds (health: starting) 127.0.0.1:8080->8080/tcp
migrate mtusi-messenger:lab4 exited Exited (0) 16 seconds ago
postgres postgres:17.11-alpine running Up 22 seconds (healthy) 5432/tcp
redis redis:7.4.11-alpine running Up 22 seconds (healthy) 6379/tcp
s3 mtusi-messenger-s3:lab4 running Up 22 seconds (healthy) 7333/tcp, 8080/tcp, 8333/tcp, 8888/tcp, 9333/tcp, 18080/tcp, 18888/tcp, 19333/tcp
s3-init mtusi-messenger:lab4 exited Exited (0) 5 seconds ago
$ docker compose --env-file .env logs migrate s3-init
migrate-1 | применена: 0001_init.sql
migrate-1 | применена: 0002_idempotency.sql
migrate-1 | применена: 0003_attachments.sql
s3-init-1 | bucket создан: messenger-attachments
s3-init-1 | S3 ещё не готов: ReadTimeoutError
s3-init-1 | bucket уже есть: messenger-attachments
s3-init-1 | пробная запись прошла
$ make check
python3 scripts/check_contract.py
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
$ make test
python3 tests/smoke.py --base-url "http://localhost:8080"
PASS: lab4 public API scenario; infrastructure and grading evidence remain separate.
+13
View File
@@ -0,0 +1,13 @@
# Сценарий защиты: файл, сообщение, второй участник, посторонний, прямой доступ к S3
# дата: 2026-10-30T19:58Z, чистый clone
# стенд: Apple M3 Pro, 11 CPU, 18 GB RAM, macOS 26.1, Docker 29.3.0, Compose 5.1.0
$ python3 scripts/attachment_demo.py
загрузка: ready 111817 байт, sha256 99ea7533c441f79c
сообщение: 201 attachment_ids = 1
второй участник скачал: 200 image/png sha256 совпал: True
посторонний, метаданные: 404 байты: 404
прямой GET в S3 без подписи изнутри сети: 403
# на реплике нет файла оригинала: временный файл загрузки удалён, поиск по файловой системе проверяет test_no_file_on_api_replica
файлов в /tmp: 0
+158
View File
@@ -0,0 +1,158 @@
# Собственные тесты API и разбора изображений
# дата: 2026-10-30T19:58Z, чистый clone
# стенд: Apple M3 Pro, 11 CPU, 18 GB RAM, macOS 26.1, Docker 29.3.0, Compose 5.1.0
$ make student-test
BASE_URL="http://localhost:8080" python3 -m unittest discover -s student-tests -v
test_blank_title (test_access.CreateChatTest.test_blank_title) ... ok
test_creator_must_be_member (test_access.CreateChatTest.test_creator_must_be_member) ... ok
test_duplicate_members (test_access.CreateChatTest.test_duplicate_members) ... ok
test_empty_members (test_access.CreateChatTest.test_empty_members) ... ok
test_failed_create_leaves_no_chat (test_access.CreateChatTest.test_failed_create_leaves_no_chat) ... ok
test_unknown_member (test_access.CreateChatTest.test_unknown_member) ... ok
test_bad_chat_id (test_access.ForeignChatTest.test_bad_chat_id) ... ok
test_chat_is_not_listed_for_outsider (test_access.ForeignChatTest.test_chat_is_not_listed_for_outsider) ... ok
test_foreign_chat_looks_like_missing_chat (test_access.ForeignChatTest.test_foreign_chat_looks_like_missing_chat) ... ok
test_member_id_must_be_canonical_uuid (test_access.ForeignChatTest.test_member_id_must_be_canonical_uuid) ... ok
test_members_can_read_and_write (test_access.ForeignChatTest.test_members_can_read_and_write) ... ok
test_outsider_gets_404_everywhere (test_access.ForeignChatTest.test_outsider_gets_404_everywhere) ... ok
test_outsider_message_is_not_saved (test_access.ForeignChatTest.test_outsider_message_is_not_saved) ... ok
test_bad_ids (test_attachments.AccessTest.test_bad_ids) ... ok
test_download_never_redirects (test_attachments.AccessTest.test_download_never_redirects) ... ok
test_no_token (test_attachments.AccessTest.test_no_token) ... ok
test_outsider_cannot_upload (test_attachments.AccessTest.test_outsider_cannot_upload) ... ok
test_outsider_gets_404 (test_attachments.AccessTest.test_outsider_gets_404) ... ok
test_outsider_looks_the_same_as_missing (test_attachments.AccessTest.test_outsider_looks_the_same_as_missing) ... ok
test_variants (test_attachments.AccessTest.test_variants) ... ok
test_broken_images (test_attachments.ContentCheckTest.test_broken_images) ... ok
test_extension_is_not_trusted (test_attachments.ContentCheckTest.test_extension_is_not_trusted) ... ok
test_mime_spoofing (test_attachments.ContentCheckTest.test_mime_spoofing) ... ok
test_unsupported_content_type (test_attachments.ContentCheckTest.test_unsupported_content_type) ... ok
test_bad_filenames (test_attachments.FilenameTest.test_bad_filenames) ... ok
test_good_filenames (test_attachments.FilenameTest.test_good_filenames) ... ok
test_missing_filename (test_attachments.FilenameTest.test_missing_filename) ... ok
test_decompression_bomb (test_attachments.LimitsTest.test_decompression_bomb) ... ok
test_empty_file (test_attachments.LimitsTest.test_empty_file) ... ok
test_pixels_over_limit_with_small_file (test_attachments.LimitsTest.test_pixels_over_limit_with_small_file) ... ok
test_too_large_chunked (test_attachments.LimitsTest.test_too_large_chunked) ... ok
test_too_large_for_balancer (test_attachments.LimitsTest.test_too_large_for_balancer) ... ok
test_too_large_with_length (test_attachments.LimitsTest.test_too_large_with_length) ... ok
test_attachment_of_my_other_chat (test_attachments.MessageLinkTest.test_attachment_of_my_other_chat) ... ok
test_attachment_only_message (test_attachments.MessageLinkTest.test_attachment_only_message) ... ok
test_empty_message_without_attachments (test_attachments.MessageLinkTest.test_empty_message_without_attachments) ... ok
test_failed_link_saves_nothing_and_frees_key (test_attachments.MessageLinkTest.test_failed_link_saves_nothing_and_frees_key) ... ok
test_foreign_attachment (test_attachments.MessageLinkTest.test_foreign_attachment) ... ok
test_idempotent_repeat (test_attachments.MessageLinkTest.test_idempotent_repeat) ... ok
test_limits (test_attachments.MessageLinkTest.test_limits) ... ok
test_message_with_attachments (test_attachments.MessageLinkTest.test_message_with_attachments) ... ok
test_missing_list_equals_empty_list (test_attachments.MessageLinkTest.test_missing_list_equals_empty_list) ... ok
test_one_file_in_two_messages (test_attachments.MessageLinkTest.test_one_file_in_two_messages) ... ok
test_ten_attachments (test_attachments.MessageLinkTest.test_ten_attachments) ... ok
test_text_message_has_empty_list (test_attachments.MessageLinkTest.test_text_message_has_empty_list) ... ok
test_unknown_attachment (test_attachments.MessageLinkTest.test_unknown_attachment) ... ok
test_too_many_parallel_uploads_get_429 (test_attachments.ParallelLimitTest.test_too_many_parallel_uploads_get_429) ... ok
test_attachment_fields (test_attachments.UploadTest.test_attachment_fields) ... ok
test_download_headers (test_attachments.UploadTest.test_download_headers) ... ok
test_file_of_exactly_10_mib (test_attachments.UploadTest.test_file_of_exactly_10_mib) ... ok
test_one_byte_over_10_mib (test_attachments.UploadTest.test_one_byte_over_10_mib) ... ok
test_upload_and_download_real_images (test_attachments.UploadTest.test_upload_and_download_real_images) ... ok
test_basic_is_not_accepted_on_business_endpoints (test_auth.BearerTest.test_basic_is_not_accepted_on_business_endpoints) ... ok
test_garbage_token_is_401_not_500 (test_auth.BearerTest.test_garbage_token_is_401_not_500) ... ok
test_no_token (test_auth.BearerTest.test_no_token) ... ok
test_token_is_checked_before_anything_else (test_auth.BearerTest.test_token_is_checked_before_anything_else) ... ok
test_unknown_tokens (test_auth.BearerTest.test_unknown_tokens) ... ok
test_x_user_id_does_not_change_identity (test_auth.BearerTest.test_x_user_id_does_not_change_identity) ... ok
test_x_user_id_gives_no_access (test_auth.BearerTest.test_x_user_id_gives_no_access) ... ok
test_colon_and_unicode_in_password (test_auth.LoginTest.test_colon_and_unicode_in_password) ... ok
test_garbage_after_basic_is_401_not_500 (test_auth.LoginTest.test_garbage_after_basic_is_401_not_500) ... ok
test_login (test_auth.LoginTest.test_login) ... ok
test_malformed_credentials (test_auth.LoginTest.test_malformed_credentials) ... ok
test_token_is_opaque_and_unique (test_auth.LoginTest.test_token_is_opaque_and_unique) ... ok
test_username_that_cannot_exist (test_auth.LoginTest.test_username_that_cannot_exist) ... ok
test_wrong_password_and_unknown_user_look_the_same (test_auth.LoginTest.test_wrong_password_and_unknown_user_look_the_same) ... ok
test_login_again_after_logout (test_auth.LogoutTest.test_login_again_after_logout) ... ok
test_logout_requires_token (test_auth.LogoutTest.test_logout_requires_token) ... ok
test_logout_revokes_only_current_session (test_auth.LogoutTest.test_logout_revokes_only_current_session) ... ok
test_invalid_http (test_balancing.ErrorsFromBalancerTest.test_invalid_http) ... ok
test_too_large_headers (test_balancing.ErrorsFromBalancerTest.test_too_large_headers) ... ok
test_too_long_uri (test_balancing.ErrorsFromBalancerTest.test_too_long_uri) ... ok
test_every_answer_has_instance_id (test_balancing.ReplicasTest.test_every_answer_has_instance_id) ... ok
test_logout_works_on_all_replicas (test_balancing.ReplicasTest.test_logout_works_on_all_replicas) ... ok
test_no_sticky_cookie (test_balancing.ReplicasTest.test_no_sticky_cookie) ... ok
test_one_session_is_served_by_different_replicas (test_balancing.ReplicasTest.test_one_session_is_served_by_different_replicas) ... ok
test_read_after_write_on_any_replica (test_balancing.ReplicasTest.test_read_after_write_on_any_replica) ... ok
test_session_created_on_one_replica_works_on_all (test_balancing.ReplicasTest.test_session_created_on_one_replica_works_on_all) ... ok
test_100_messages_from_10_clients (test_concurrency.ConcurrencyTest.test_100_messages_from_10_clients) ... ok
test_same_username_registered_once (test_concurrency.ConcurrencyTest.test_same_username_registered_once) ... ok
test_bad_keys (test_idempotency.IdempotencyTest.test_bad_keys) ... ok
test_good_key_alphabet (test_idempotency.IdempotencyTest.test_good_key_alphabet) ... ok
test_invalid_body_does_not_take_key (test_idempotency.IdempotencyTest.test_invalid_body_does_not_take_key) ... ok
test_key_is_required (test_idempotency.IdempotencyTest.test_key_is_required) ... ok
test_key_order_in_json_does_not_matter (test_idempotency.IdempotencyTest.test_key_order_in_json_does_not_matter) ... ok
test_other_text_with_same_key_is_409 (test_idempotency.IdempotencyTest.test_other_text_with_same_key_is_409) ... ok
test_parallel_repeat_through_different_replicas (test_idempotency.IdempotencyTest.test_parallel_repeat_through_different_replicas) ... ok
test_repeat_checks_access_again (test_idempotency.IdempotencyTest.test_repeat_checks_access_again) ... ok
test_repeat_returns_same_message (test_idempotency.IdempotencyTest.test_repeat_returns_same_message) ... ok
test_same_key_for_other_user_and_other_chat (test_idempotency.IdempotencyTest.test_same_key_for_other_user_and_other_chat) ... ok
test_jpeg_without_frame_or_scan (test_images.BrokenImagesTest.test_jpeg_without_frame_or_scan) ... ok
test_not_images (test_images.BrokenImagesTest.test_not_images) ... ok
test_png_bad_crc (test_images.BrokenImagesTest.test_png_bad_crc) ... ok
test_png_data_after_end (test_images.BrokenImagesTest.test_png_data_after_end) ... ok
test_png_data_is_not_zlib (test_images.BrokenImagesTest.test_png_data_is_not_zlib) ... ok
test_png_with_wrong_amount_of_pixels (test_images.BrokenImagesTest.test_png_with_wrong_amount_of_pixels) ... ok
test_png_zero_size (test_images.BrokenImagesTest.test_png_zero_size) ... ok
test_random_corruption_never_crashes (test_images.BrokenImagesTest.test_random_corruption_never_crashes) ... ok
test_truncated (test_images.BrokenImagesTest.test_truncated) ... ok
test_jpeg_over_limit (test_images.PixelLimitTest.test_jpeg_over_limit) ... ok
test_limit_is_exact (test_images.PixelLimitTest.test_limit_is_exact) ... ok
test_png_bomb_is_rejected_before_unpacking (test_images.PixelLimitTest.test_png_bomb_is_rejected_before_unpacking) ... ok
test_course_fixture (test_images.RealImagesTest.test_course_fixture) ... ok
test_jpeg_with_data_after_end (test_images.RealImagesTest.test_jpeg_with_data_after_end) ... ok
test_marker_on_block_border (test_images.RealImagesTest.test_marker_on_block_border) ... ok
test_real_images_are_accepted (test_images.RealImagesTest.test_real_images_are_accepted) ... ok
test_chat_cursor_belongs_to_user (test_pagination.ChatPaginationTest.test_chat_cursor_belongs_to_user) ... ok
test_walk_chat_list (test_pagination.ChatPaginationTest.test_walk_chat_list) ... ok
test_bad_cursor (test_pagination.MessagePaginationTest.test_bad_cursor) ... ok
test_bad_limit (test_pagination.MessagePaginationTest.test_bad_limit) ... ok
test_cursor_format_is_checked (test_pagination.MessagePaginationTest.test_cursor_format_is_checked) ... ok
test_cursor_from_another_chat (test_pagination.MessagePaginationTest.test_cursor_from_another_chat) ... ok
test_default_limit (test_pagination.MessagePaginationTest.test_default_limit) ... ok
test_exact_page_has_no_cursor (test_pagination.MessagePaginationTest.test_exact_page_has_no_cursor) ... ok
test_limit_can_change_between_pages (test_pagination.MessagePaginationTest.test_limit_can_change_between_pages) ... ok
test_new_message_appears_after_last_page (test_pagination.MessagePaginationTest.test_new_message_appears_after_last_page) ... ok
test_no_duplicates_and_order (test_pagination.MessagePaginationTest.test_no_duplicates_and_order) ... ok
test_walk_several_pages (test_pagination.MessagePaginationTest.test_walk_several_pages) ... ok
test_broken_json_without_token_is_401 (test_validation.InvalidJsonTest.test_broken_json_without_token_is_401) ... ok
test_content_type_must_be_json (test_validation.InvalidJsonTest.test_content_type_must_be_json) ... ok
test_deeply_nested_json (test_validation.InvalidJsonTest.test_deeply_nested_json) ... ok
test_extra_field (test_validation.InvalidJsonTest.test_extra_field) ... ok
test_malformed_json (test_validation.InvalidJsonTest.test_malformed_json) ... ok
test_malformed_json_in_message (test_validation.InvalidJsonTest.test_malformed_json_in_message) ... ok
test_missing_field (test_validation.InvalidJsonTest.test_missing_field) ... ok
test_not_an_object (test_validation.InvalidJsonTest.test_not_an_object) ... ok
test_only_utf8_is_accepted (test_validation.InvalidJsonTest.test_only_utf8_is_accepted) ... ok
test_too_large_json (test_validation.InvalidJsonTest.test_too_large_json) ... ok
test_wrong_type (test_validation.InvalidJsonTest.test_wrong_type) ... ok
test_empty_message (test_validation.MessageValidationTest.test_empty_message) ... ok
test_nul_character (test_validation.MessageValidationTest.test_nul_character) ... ok
test_sender_id_is_not_accepted (test_validation.MessageValidationTest.test_sender_id_is_not_accepted) ... ok
test_text_limit (test_validation.MessageValidationTest.test_text_limit) ... ok
test_unicode_is_kept (test_validation.MessageValidationTest.test_unicode_is_kept) ... ok
test_whitespace_message (test_validation.MessageValidationTest.test_whitespace_message) ... ok
test_bad_display_names (test_validation.UserValidationTest.test_bad_display_names) ... ok
test_bad_passwords (test_validation.UserValidationTest.test_bad_passwords) ... ok
test_bad_usernames (test_validation.UserValidationTest.test_bad_usernames) ... ok
test_display_name_length_is_counted_in_code_points (test_validation.UserValidationTest.test_display_name_length_is_counted_in_code_points) ... ok
test_duplicate_username (test_validation.UserValidationTest.test_duplicate_username) ... ok
test_password_is_never_returned (test_validation.UserValidationTest.test_password_is_never_returned) ... ok
test_password_length_limits (test_validation.UserValidationTest.test_password_length_limits) ... ok
----------------------------------------------------------------------
Ran 142 tests in 7.240s
OK
отправлено 100, подтверждено 100, в истории 100, уникальных id 100
10 ключей по 8 параллельных запросов: реплик 2, сообщений в истории 10
+63
View File
@@ -0,0 +1,63 @@
# Сохранность: перезапуск всего стенда с сохранением volumes, включая оригинал файла
# дата: 2026-10-30T19:58Z, чистый clone
# стенд: Apple M3 Pro, 11 CPU, 18 GB RAM, macOS 26.1, Docker 29.3.0, Compose 5.1.0
# до перезапуска
/mtusi-messenger-lab4-api-1 StartedAt=2026-10-30T19:58:29.088370587Z
/mtusi-messenger-lab4-api-2 StartedAt=2026-10-30T19:58:29.209140045Z
/mtusi-messenger-lab4-janitor-1 StartedAt=2026-10-30T19:58:29.085001378Z
/mtusi-messenger-lab4-lb-1 StartedAt=2026-10-30T19:58:29.300703295Z
/mtusi-messenger-lab4-postgres-1 StartedAt=2026-10-30T19:58:11.349641968Z
/mtusi-messenger-lab4-redis-1 StartedAt=2026-10-30T19:58:11.345301093Z
/mtusi-messenger-lab4-s3-1 StartedAt=2026-10-30T19:58:11.372245384Z
$ cat scripts/restart.sh
#!/bin/sh
# Перезапускает балансировщик, все реплики API, janitor и все хранилища. Volumes не трогает.
set -eu
cd "$(dirname "$0")/.."
docker compose --env-file .env restart postgres redis s3 api janitor lb
# /health/ready от S3 не зависит, поэтому готовность S3 ждём здесь: том данных появляется за 20-30 секунд
container=$(docker compose --env-file .env ps -q s3)
for attempt in $(seq 1 80); do
[ "$(docker inspect -f '{{.State.Health.Status}}' "$container")" = healthy ] && exit 0
sleep 1
done
echo "S3 не стал готов за 80 секунд" >&2
exit 1
$ make test-persistence ACTION_SCRIPT=scripts/restart.sh
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost: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.
# после перезапуска: новое время старта у всех контейнеров
/mtusi-messenger-lab4-api-1 StartedAt=2026-10-30T19:58:43.982991844Z
/mtusi-messenger-lab4-api-2 StartedAt=2026-10-30T19:58:44.040335636Z
/mtusi-messenger-lab4-janitor-1 StartedAt=2026-10-30T19:58:43.669196927Z
/mtusi-messenger-lab4-lb-1 StartedAt=2026-10-30T19:58:43.367738094Z
/mtusi-messenger-lab4-postgres-1 StartedAt=2026-10-30T19:58:43.452856427Z
/mtusi-messenger-lab4-redis-1 StartedAt=2026-10-30T19:58:43.413552302Z
/mtusi-messenger-lab4-s3-1 StartedAt=2026-10-30T19:58:44.201471136Z
$ docker volume ls --filter name=mtusi-messenger-lab4
DRIVER VOLUME NAME
local mtusi-messenger-lab4_pgdata
local mtusi-messenger-lab4_redisdata
local mtusi-messenger-lab4_s3data
# повторный запуск миграций и создания bucket
$ docker compose --env-file .env run --rm --no-deps migrate
Container mtusi-messenger-lab4-migrate-run-5b40a0f89a6a Creating
Container mtusi-messenger-lab4-migrate-run-5b40a0f89a6a Created
уже применена: 0001_init.sql
уже применена: 0002_idempotency.sql
уже применена: 0003_attachments.sql
$ docker compose --env-file .env run --rm --no-deps s3-init
Container mtusi-messenger-lab4-s3-init-run-0ac64ba9510a Creating
Container mtusi-messenger-lab4-s3-init-run-0ac64ba9510a Created
bucket уже есть: messenger-attachments
пробная запись прошла
+88
View File
@@ -0,0 +1,88 @@
# Публичный failover: файл загружен, обслужившая реплика остановлена, скачивание через другую; два прогона
# дата: 2026-10-30T19:59Z, чистый clone
# стенд: Apple M3 Pro, 11 CPU, 18 GB RAM, macOS 26.1, Docker 29.3.0, Compose 5.1.0
## прогон 1
# реплики до отказа
NAME STATE STATUS
mtusi-messenger-lab4-api-1 running Up 36 seconds (healthy)
mtusi-messenger-lab4-api-2 running Up 36 seconds (healthy)
$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080"
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 116, "failed_attempts": 0, "first_recovery_seconds": 1.97}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
# какой контейнер реально остановлен
NAME STATE STATUS
mtusi-messenger-lab4-api-1 exited Exited (0) 14 seconds ago
mtusi-messenger-lab4-api-2 running Up 52 seconds (healthy)
$ make restore
docker compose --env-file .env up -d --no-recreate --no-build api
Container mtusi-messenger-lab4-postgres-1 Running
Container mtusi-messenger-lab4-redis-1 Running
Container mtusi-messenger-lab4-s3-1 Running
Container mtusi-messenger-lab4-api-2 Running
Container mtusi-messenger-lab4-postgres-1 Waiting
Container mtusi-messenger-lab4-s3-1 Waiting
Container mtusi-messenger-lab4-s3-1 Healthy
Container mtusi-messenger-lab4-postgres-1 Healthy
Container mtusi-messenger-lab4-migrate-1 Starting
Container mtusi-messenger-lab4-s3-init-1 Starting
Container mtusi-messenger-lab4-migrate-1 Started
Container mtusi-messenger-lab4-s3-init-1 Started
Container mtusi-messenger-lab4-migrate-1 Waiting
Container mtusi-messenger-lab4-redis-1 Waiting
Container mtusi-messenger-lab4-s3-init-1 Waiting
Container mtusi-messenger-lab4-redis-1 Healthy
Container mtusi-messenger-lab4-s3-init-1 Exited
Container mtusi-messenger-lab4-migrate-1 Exited
Container mtusi-messenger-lab4-api-1 Starting
Container mtusi-messenger-lab4-api-1 Started
BASE_URL="http://localhost:8080" ./scripts/wait-ready.sh
готов: http://localhost:8080
## прогон 2
# реплики до отказа
NAME STATE STATUS
mtusi-messenger-lab4-api-1 running Up 4 seconds (healthy)
mtusi-messenger-lab4-api-2 running Up 58 seconds (healthy)
$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080"
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 116, "failed_attempts": 0, "first_recovery_seconds": 1.731}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
# какой контейнер реально остановлен
NAME STATE STATUS
mtusi-messenger-lab4-api-1 exited Exited (0) 14 seconds ago
mtusi-messenger-lab4-api-2 running Up About a minute (healthy)
$ make restore
docker compose --env-file .env up -d --no-recreate --no-build api
Container mtusi-messenger-lab4-s3-1 Running
Container mtusi-messenger-lab4-postgres-1 Running
Container mtusi-messenger-lab4-redis-1 Running
Container mtusi-messenger-lab4-api-2 Running
Container mtusi-messenger-lab4-postgres-1 Waiting
Container mtusi-messenger-lab4-s3-1 Waiting
Container mtusi-messenger-lab4-s3-1 Healthy
Container mtusi-messenger-lab4-postgres-1 Healthy
Container mtusi-messenger-lab4-migrate-1 Starting
Container mtusi-messenger-lab4-s3-init-1 Starting
Container mtusi-messenger-lab4-s3-init-1 Started
Container mtusi-messenger-lab4-migrate-1 Started
Container mtusi-messenger-lab4-redis-1 Waiting
Container mtusi-messenger-lab4-s3-init-1 Waiting
Container mtusi-messenger-lab4-migrate-1 Waiting
Container mtusi-messenger-lab4-redis-1 Healthy
Container mtusi-messenger-lab4-migrate-1 Exited
Container mtusi-messenger-lab4-s3-init-1 Exited
Container mtusi-messenger-lab4-api-1 Starting
Container mtusi-messenger-lab4-api-1 Started
BASE_URL="http://localhost:8080" ./scripts/wait-ready.sh
готов: http://localhost:8080
+10
View File
@@ -0,0 +1,10 @@
# Остановка каждой реплики под нагрузкой (SIGTERM), полные данные в failover-each.json
# дата: 2026-10-30T20:00Z, чистый clone
# стенд: Apple M3 Pro, 11 CPU, 18 GB RAM, macOS 26.1, Docker 29.3.0, Compose 5.1.0
$ make failover-each
BASE_URL="http://localhost:8080" python3 scripts/chaos.py stop --output evidence/failover-each.json
{"seconds": 17.89, "successful_requests": 10761, "failed_attempts": 0, "failed_statuses": [], "successful_rps": 601.6, "longest_gap_between_successes_s": 1.027, "first_success_after_action_s": 0.003, "last_success_on_target_after_action_s": 12.886, "instances_seen": 2, "target": "b4e0f5de7608", "signal": "SIGTERM", "stop_command_s": 0.59, "excluded_by_lb_after_s": 0.9, "verify": {"confirmed": 10761, "in_history": 10761, "lost_confirmed": 0, "duplicate_ids": 0, "duplicate_texts": 0, "unconfirmed_but_saved": 0}}
{"seconds": 19.96, "successful_requests": 11872, "failed_attempts": 0, "failed_statuses": [], "successful_rps": 594.9, "longest_gap_between_successes_s": 1.015, "first_success_after_action_s": 0.0, "last_success_on_target_after_action_s": 14.955, "instances_seen": 2, "target": "6f14dc16ebf3", "signal": "SIGTERM", "stop_command_s": 0.61, "excluded_by_lb_after_s": 1.51, "verify": {"confirmed": 11872, "in_history": 11872, "lost_confirmed": 0, "duplicate_ids": 0, "duplicate_texts": 0, "unconfirmed_but_saved": 0}}
PASS: подтверждённые сообщения на месте, дублей нет
+16
View File
@@ -0,0 +1,16 @@
# Поочерёдная замена реплик под нагрузкой, полные данные в rolling-update.json
# дата: 2026-10-30T20:00Z, чистый clone
# стенд: Apple M3 Pro, 11 CPU, 18 GB RAM, macOS 26.1, Docker 29.3.0, Compose 5.1.0
# образы до замены
/mtusi-messenger-lab4-api-1 sha256:693fe62509aa4bc5ff409c715e21da0f894
/mtusi-messenger-lab4-api-2 sha256:693fe62509aa4bc5ff409c715e21da0f894
$ make rolling-update APP_VERSION=2
BASE_URL="http://localhost:8080" python3 scripts/chaos.py rolling --output evidence/rolling-update.json
{"seconds": 25.51, "successful_requests": 17974, "failed_attempts": 0, "failed_statuses": [], "successful_rps": 704.5, "longest_gap_between_successes_s": 1.001, "first_success_after_action_s": 0.003, "last_success_on_target_after_action_s": null, "instances_seen": 4, "update_seconds": 15.51, "containers_before": {"b4e0f5de7608": "sha256:693fe62509aa", "6f14dc16ebf3": "sha256:693fe62509aa"}, "containers_after": {"abc646dd09cf": "sha256:bdbd0270135d", "d7f52b7d6c23": "sha256:bdbd0270135d"}, "app_version": "2", "verify": {"confirmed": 17974, "in_history": 17974, "lost_confirmed": 0, "duplicate_ids": 0, "duplicate_texts": 0, "unconfirmed_but_saved": 0}}
PASS: подтверждённые сообщения на месте, дублей нет
# образы и версия после замены
/mtusi-messenger-lab4-api-3 sha256:bdbd0270135d576aecc5d4df3c91c41eb31 APP_VERSION=2
/mtusi-messenger-lab4-api-4 sha256:bdbd0270135d576aecc5d4df3c91c41eb31 APP_VERSION=2
+43
View File
@@ -0,0 +1,43 @@
# Тесты отказов: сбой между S3 и БД, отказ S3, приватность bucket, истечение, отказ Redis и PostgreSQL, транзакции
# дата: 2026-10-30T20:01Z, чистый clone
# стенд: Apple M3 Pro, 11 CPU, 18 GB RAM, macOS 26.1, Docker 29.3.0, Compose 5.1.0
$ make test-failures
BASE_URL="http://localhost:8080" python3 -m unittest discover -s student-tests/failures -v
test_postgres_down (test_dependencies.DependencyDownTest.test_postgres_down) ... ok
test_redis_down (test_dependencies.DependencyDownTest.test_redis_down) ... ok
test_uncertain_write_can_be_repeated_with_same_key (test_dependencies.DependencyDownTest.test_uncertain_write_can_be_repeated_with_same_key) ... ok
test_expires_at_matches_ttl (test_expiry.SessionExpiryTest.test_expires_at_matches_ttl) ... ok
test_idempotency_key_is_free_after_ttl (test_expiry.SessionExpiryTest.test_idempotency_key_is_free_after_ttl) ... ok
test_login_after_expiry (test_expiry.SessionExpiryTest.test_login_after_expiry) ... ok
test_requests_do_not_extend_session (test_expiry.SessionExpiryTest.test_requests_do_not_extend_session) ... ok
test_session_expires (test_expiry.SessionExpiryTest.test_session_expires) ... ok
test_commit_lost_after_s3_put (test_storage.ConsistencyTest.test_commit_lost_after_s3_put)
Состояние как после падения между PUT и подтверждением: объект есть, строка pending, клиент не получил 201. ... ok
test_grace_period_protects_fresh_upload (test_storage.ConsistencyTest.test_grace_period_protects_fresh_upload) ... ok
test_interrupted_upload_leaves_nothing (test_storage.ConsistencyTest.test_interrupted_upload_leaves_nothing) ... ok
test_orphan_object_is_removed_only_after_grace (test_storage.ConsistencyTest.test_orphan_object_is_removed_only_after_grace) ... ok
test_postgres_lost_during_parallel_uploads (test_storage.ConsistencyTest.test_postgres_lost_during_parallel_uploads)
Настоящий сбой: база пропадает, пока идут загрузки. Часть объектов попадает в S3 без подтверждения. ... ok
test_ready_attachments_are_never_removed (test_storage.ConsistencyTest.test_ready_attachments_are_never_removed) ... ok
test_second_pass_changes_nothing (test_storage.ConsistencyTest.test_second_pass_changes_nothing) ... ok
test_bucket_bootstrap_can_be_repeated (test_storage.PrivateBucketTest.test_bucket_bootstrap_can_be_repeated) ... ok
test_bucket_is_private_and_api_key_is_limited (test_storage.PrivateBucketTest.test_bucket_is_private_and_api_key_is_limited) ... ok
test_no_file_on_api_replica (test_storage.PrivateBucketTest.test_no_file_on_api_replica) ... ok
test_failed_upload_is_cleaned (test_storage.S3DownTest.test_failed_upload_is_cleaned) ... ok
test_s3_down (test_storage.S3DownTest.test_s3_down) ... ok
test_chat_with_unknown_member_is_rolled_back (test_transactions.TransactionTest.test_chat_with_unknown_member_is_rolled_back) ... ok
test_failed_message_leaves_no_key (test_transactions.TransactionTest.test_failed_message_leaves_no_key) ... ok
test_migrations_can_be_repeated (test_transactions.TransactionTest.test_migrations_can_be_repeated) ... ok
test_password_is_stored_as_argon2id_hash (test_transactions.TransactionTest.test_password_is_stored_as_argon2id_hash) ... ok
test_same_key_in_parallel_creates_one_row (test_transactions.TransactionTest.test_same_key_in_parallel_creates_one_row) ... ok
test_username_race_creates_one_row (test_transactions.TransactionTest.test_username_race_creates_one_row) ... ok
test_valid_chat_is_committed_whole (test_transactions.TransactionTest.test_valid_chat_is_committed_whole) ... ok
----------------------------------------------------------------------
Ran 27 tests in 301.600s
OK
подтверждено 6, в S3 без подтверждения 8, после очистки расхождений нет
+45
View File
@@ -0,0 +1,45 @@
# Память при одновременных загрузках, полные данные в upload-peak.json
# дата: 2026-10-30T20:06Z, чистый clone
# стенд: Apple M3 Pro, 11 CPU, 18 GB RAM, macOS 26.1, Docker 29.3.0, Compose 5.1.0
# пять прогонов подряд: один удачный ничего не доказывает
$ make upload-peak
BASE_URL="http://localhost:8080" python3 scripts/upload_peak.py --output evidence/upload-peak.json
{"parallel_uploads": 1, "file_bytes": 10481664, "seconds": 0.14, "slowest_upload_s": 0.14, "statuses": {"201": 1}, "memory": {"abc646dd09cf": {"process_peak_rss_mib": 116.9, "cgroup_peak_mib": 98.7}, "d7f52b7d6c23": {"process_peak_rss_mib": 117.4, "cgroup_peak_mib": 110.3}}}
{"parallel_uploads": 10, "file_bytes": 10481664, "seconds": 1.29, "slowest_upload_s": 1.29, "statuses": {"201": 10}, "memory": {"abc646dd09cf": {"process_peak_rss_mib": 123.8, "cgroup_peak_mib": 160.0}, "d7f52b7d6c23": {"process_peak_rss_mib": 123.9, "cgroup_peak_mib": 160.2}}}
{"parallel_uploads": 30, "file_bytes": 10481664, "seconds": 3.79, "slowest_upload_s": 3.78, "statuses": {"201": 30}, "memory": {"abc646dd09cf": {"process_peak_rss_mib": 125.9, "cgroup_peak_mib": 256.1}, "d7f52b7d6c23": {"process_peak_rss_mib": 125.9, "cgroup_peak_mib": 258.8}}}
PASS: все загрузки успешны
$ make upload-peak
BASE_URL="http://localhost:8080" python3 scripts/upload_peak.py --output evidence/upload-peak.json
{"parallel_uploads": 1, "file_bytes": 10481664, "seconds": 1.76, "slowest_upload_s": 1.76, "statuses": {"201": 1}, "memory": {"abc646dd09cf": {"process_peak_rss_mib": 117.4, "cgroup_peak_mib": 110.7}, "d7f52b7d6c23": {"process_peak_rss_mib": 117.3, "cgroup_peak_mib": 110.6}}}
{"parallel_uploads": 10, "file_bytes": 10481664, "seconds": 1.32, "slowest_upload_s": 1.31, "statuses": {"201": 10}, "memory": {"abc646dd09cf": {"process_peak_rss_mib": 124.0, "cgroup_peak_mib": 161.9}, "d7f52b7d6c23": {"process_peak_rss_mib": 124.3, "cgroup_peak_mib": 158.7}}}
{"parallel_uploads": 30, "file_bytes": 10481664, "seconds": 3.88, "slowest_upload_s": 3.87, "statuses": {"201": 30}, "memory": {"abc646dd09cf": {"process_peak_rss_mib": 126.2, "cgroup_peak_mib": 256.4}, "d7f52b7d6c23": {"process_peak_rss_mib": 125.8, "cgroup_peak_mib": 256.5}}}
PASS: все загрузки успешны
$ make upload-peak
BASE_URL="http://localhost:8080" python3 scripts/upload_peak.py --output evidence/upload-peak.json
{"parallel_uploads": 1, "file_bytes": 10481664, "seconds": 1.83, "slowest_upload_s": 1.83, "statuses": {"201": 1}, "memory": {"abc646dd09cf": {"process_peak_rss_mib": 117.4, "cgroup_peak_mib": 99.0}, "d7f52b7d6c23": {"process_peak_rss_mib": 117.8, "cgroup_peak_mib": 110.6}}}
{"parallel_uploads": 10, "file_bytes": 10481664, "seconds": 1.34, "slowest_upload_s": 1.34, "statuses": {"201": 10}, "memory": {"abc646dd09cf": {"process_peak_rss_mib": 124.2, "cgroup_peak_mib": 176.0}, "d7f52b7d6c23": {"process_peak_rss_mib": 124.3, "cgroup_peak_mib": 168.9}}}
{"parallel_uploads": 30, "file_bytes": 10481664, "seconds": 4.48, "slowest_upload_s": 4.48, "statuses": {"201": 30}, "memory": {"abc646dd09cf": {"process_peak_rss_mib": 126.0, "cgroup_peak_mib": 256.1}, "d7f52b7d6c23": {"process_peak_rss_mib": 126.5, "cgroup_peak_mib": 256.5}}}
PASS: все загрузки успешны
$ make upload-peak
BASE_URL="http://localhost:8080" python3 scripts/upload_peak.py --output evidence/upload-peak.json
{"parallel_uploads": 1, "file_bytes": 10481664, "seconds": 0.14, "slowest_upload_s": 0.14, "statuses": {"201": 1}, "memory": {"abc646dd09cf": {"process_peak_rss_mib": 117.8, "cgroup_peak_mib": 110.8}, "d7f52b7d6c23": {"process_peak_rss_mib": 117.1, "cgroup_peak_mib": 98.4}}}
{"parallel_uploads": 10, "file_bytes": 10481664, "seconds": 2.92, "slowest_upload_s": 2.92, "statuses": {"201": 10}, "memory": {"abc646dd09cf": {"process_peak_rss_mib": 125.6, "cgroup_peak_mib": 175.5}, "d7f52b7d6c23": {"process_peak_rss_mib": 126.4, "cgroup_peak_mib": 163.7}}}
{"parallel_uploads": 30, "file_bytes": 10481664, "seconds": 6.3, "slowest_upload_s": 6.29, "statuses": {"201": 30}, "memory": {"abc646dd09cf": {"process_peak_rss_mib": 129.2, "cgroup_peak_mib": 256.1}, "d7f52b7d6c23": {"process_peak_rss_mib": 128.8, "cgroup_peak_mib": 256.1}}}
PASS: все загрузки успешны
$ make upload-peak
BASE_URL="http://localhost:8080" python3 scripts/upload_peak.py --output evidence/upload-peak.json
{"parallel_uploads": 1, "file_bytes": 10481664, "seconds": 0.48, "slowest_upload_s": 0.48, "statuses": {"201": 1}, "memory": {"abc646dd09cf": {"process_peak_rss_mib": 117.2, "cgroup_peak_mib": 110.5}, "d7f52b7d6c23": {"process_peak_rss_mib": 117.5, "cgroup_peak_mib": 110.2}}}
{"parallel_uploads": 10, "file_bytes": 10481664, "seconds": 3.87, "slowest_upload_s": 3.87, "statuses": {"201": 10}, "memory": {"abc646dd09cf": {"process_peak_rss_mib": 124.3, "cgroup_peak_mib": 163.7}, "d7f52b7d6c23": {"process_peak_rss_mib": 124.1, "cgroup_peak_mib": 164.1}}}
{"parallel_uploads": 30, "file_bytes": 10481664, "seconds": 6.43, "slowest_upload_s": 6.42, "statuses": {"201": 30}, "memory": {"abc646dd09cf": {"process_peak_rss_mib": 127.3, "cgroup_peak_mib": 256.0}, "d7f52b7d6c23": {"process_peak_rss_mib": 126.9, "cgroup_peak_mib": 256.0}}}
PASS: все загрузки успешны
# S3 за все прогоны: перезапуски и OOM
restarts=0 oom=false
oom 0
oom_kill 0
oom_group_kill 0
+92
View File
@@ -0,0 +1,92 @@
# Контейнеры: лимиты, порты, сети, секреты, права ключа S3
# дата: 2026-10-30T20:07Z, чистый clone
# стенд: Apple M3 Pro, 11 CPU, 18 GB RAM, macOS 26.1, Docker 29.3.0, Compose 5.1.0
$ docker compose ps # опубликован только lb, консоль S3 наружу не открыта
SERVICE PORTS
api 8080/tcp
api 8080/tcp
janitor 8080/tcp
lb 127.0.0.1:8080->8080/tcp
postgres 5432/tcp
redis 6379/tcp
s3 7333/tcp, 8080/tcp, 8333/tcp, 8888/tcp, 9333/tcp, 18080/tcp, 18888/tcp, 19333/tcp
$ grep -c container_name compose.yaml
0
# лимиты CPU (в наносекундах на секунду) и памяти (байт)
/mtusi-messenger-lab4-api-3 cpus=1000000000 memory=268435456
/mtusi-messenger-lab4-api-4 cpus=1000000000 memory=268435456
/mtusi-messenger-lab4-janitor-1 cpus=250000000 memory=134217728
/mtusi-messenger-lab4-lb-1 cpus=500000000 memory=134217728
/mtusi-messenger-lab4-postgres-1 cpus=1000000000 memory=536870912
/mtusi-messenger-lab4-redis-1 cpus=500000000 memory=134217728
/mtusi-messenger-lab4-s3-1 cpus=1000000000 memory=1073741824
# сети
lb mtusi-messenger-lab4_edge
api mtusi-messenger-lab4_data mtusi-messenger-lab4_edge mtusi-messenger-lab4_storage
janitor mtusi-messenger-lab4_data mtusi-messenger-lab4_storage
s3 mtusi-messenger-lab4_storage
postgres mtusi-messenger-lab4_data
redis mtusi-messenger-lab4_data
$ docker exec abc646dd09cf id
uid=10001(app) gid=10001(app) groups=10001(app)
$ docker exec <api> ls /run/secrets # ключа администратора S3 у API нет
cache_url
database_url
s3_access_key_id
s3_secret_access_key
$ docker exec <api> env | grep -ci "password\|postgresql://\|redis://\|secret_access_key="
0
$ docker compose --env-file .env images
CONTAINER REPOSITORY TAG PLATFORM IMAGE ID SIZE CREATED
mtusi-messenger-lab4-api-3 mtusi-messenger lab4 linux/arm64 bdbd0270135d 230MB 9 minutes ago
mtusi-messenger-lab4-api-4 mtusi-messenger lab4 linux/arm64 bdbd0270135d 230MB 9 minutes ago
mtusi-messenger-lab4-janitor-1 sha256 693fe62509aa4bc5ff409c715e21da0f894a2223d5b0ba57cdaab4303cc12145 linux/arm64 693fe62509aa 230MB 9 minutes ago
mtusi-messenger-lab4-lb-1 mtusi-messenger-lb lab4 linux/arm64 874b8c49635f 38.8MB 30 minutes ago
mtusi-messenger-lab4-migrate-1 sha256 693fe62509aa4bc5ff409c715e21da0f894a2223d5b0ba57cdaab4303cc12145 linux/arm64 693fe62509aa 230MB 9 minutes ago
mtusi-messenger-lab4-postgres-1 postgres 17.11-alpine linux/arm64/v8 25a89970a832 291MB 6 weeks ago
mtusi-messenger-lab4-redis-1 redis 7.4.11-alpine linux/arm64/v8 ec4f171e9ff0 39.5MB 6 weeks ago
mtusi-messenger-lab4-s3-1 mtusi-messenger-s3 lab4 linux/arm64 bb740ce2c254 509MB 31 minutes ago
mtusi-messenger-lab4-s3-init-1 sha256 693fe62509aa4bc5ff409c715e21da0f894a2223d5b0ba57cdaab4303cc12145 linux/arm64 693fe62509aa 230MB 9 minutes ago
$ docker exec <api> pip freeze
annotated-doc==0.0.5
annotated-types==0.8.0
anyio==4.15.1
argon2-cffi==25.1.0
argon2-cffi-bindings==26.1.0
boto3==1.43.122
botocore==1.43.122
cffi==2.1.1
click==8.5.0
fastapi==0.143.0
h11==0.16.0
idna==3.20
jmespath==1.1.0
opentelemetry-api==1.45.1
psycopg==3.3.6
psycopg-binary==3.3.6
psycopg-pool==3.3.3
pycparser==3.1
pydantic==2.13.5
pydantic_core==2.46.5
python-dateutil==2.9.0.post0
redis==8.1.0
s3transfer==0.19.3
six==1.17.0
starlette==1.7.0
typing-inspection==0.4.4
typing_extensions==4.16.0
urllib3==2.8.0
uvicorn==0.54.0
$ git ls-files | grep -E "^\.env$|secrets" # в Git секретов нет
(пусто)
+12
View File
@@ -0,0 +1,12 @@
# SIGTERM через балансировщик
# дата: 2026-10-30T20:07Z, чистый clone
# стенд: Apple M3 Pro, 11 CPU, 18 GB RAM, macOS 26.1, Docker 29.3.0, Compose 5.1.0
$ python3 scripts/sigterm_check.py
$ docker kill --signal=SIGTERM abc646dd09cf
новых запросов после SIGTERM: 20, попало на останавливаемую реплику: 0
начатые до SIGTERM запросы: ['HTTP/1.1 201 Created']
из них завершила останавливаемая реплика: 3
процесс завершился за 1.73 с, код выхода 0
PASS: остановка корректная и укладывается в 10 секунд
+128
View File
@@ -0,0 +1,128 @@
# Индекс истории: EXPLAIN (ANALYZE, BUFFERS), объём данных в первой таблице вывода
# дата: 2026-10-30T20:07Z, чистый clone
# стенд: Apple M3 Pro, 11 CPU, 18 GB RAM, macOS 26.1, Docker 29.3.0, Compose 5.1.0
$ make explain
./scripts/explain_history.sh
== Объём данных
messages_total | messages_in_chat | table_size | index_size
----------------+------------------+------------+------------
101045 | 10000 | 11 MB | 9544 kB
(1 row)
== 1. Первая страница (limit 50, запрос берёт 51 строку)
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.42..729.05 rows=51 width=112) (actual time=0.028..0.228 rows=51 loops=1)
Buffers: shared hit=63
-> Index Scan using messages_chat_history_idx on messages (cost=0.42..142912.03 rows=10003 width=112) (actual time=0.023..0.219 rows=51 loops=1)
Index Cond: (chat_id = '00000000-0000-4000-8000-000000000100'::uuid)
Buffers: shared hit=63
SubPlan 1
-> Aggregate (cost=13.78..13.79 rows=1 width=32) (actual time=0.004..0.004 rows=1 loops=51)
Buffers: shared hit=54
-> Sort (cost=13.74..13.76 rows=6 width=20) (actual time=0.002..0.002 rows=0 loops=51)
Sort Key: a."position"
Sort Method: quicksort Memory: 25kB
Buffers: shared hit=54
-> Bitmap Heap Scan on message_attachments a (cost=4.20..13.67 rows=6 width=20) (actual time=0.000..0.000 rows=0 loops=51)
Recheck Cond: (message_id = messages.id)
Buffers: shared hit=51
-> Bitmap Index Scan on message_attachments_pkey (cost=0.00..4.20 rows=6 width=0) (actual time=0.000..0.000 rows=0 loops=51)
Index Cond: (message_id = messages.id)
Buffers: shared hit=51
Planning:
Buffers: shared hit=76
Planning Time: 0.174 ms
Execution Time: 0.255 ms
(22 rows)
== 2. Страница из середины истории: keyset после 5000-го сообщения
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.42..738.40 rows=51 width=112) (actual time=0.013..0.198 rows=51 loops=1)
Buffers: shared hit=59
-> Index Scan using messages_chat_history_idx on messages (cost=0.42..101986.67 rows=7048 width=112) (actual time=0.013..0.196 rows=51 loops=1)
Index Cond: ((chat_id = '00000000-0000-4000-8000-000000000100'::uuid) AND (ROW(created_at, id) > ROW('2026-10-30 11:47:36.896462+00'::timestamp with time zone, '0c56411c-acb3-420b-bd20-5ac5ed7ab8a2'::uuid)))
Buffers: shared hit=59
SubPlan 1
-> Aggregate (cost=13.78..13.79 rows=1 width=32) (actual time=0.003..0.003 rows=1 loops=51)
Buffers: shared hit=51
-> Sort (cost=13.74..13.76 rows=6 width=20) (actual time=0.002..0.002 rows=0 loops=51)
Sort Key: a."position"
Sort Method: quicksort Memory: 25kB
Buffers: shared hit=51
-> Bitmap Heap Scan on message_attachments a (cost=4.20..13.67 rows=6 width=20) (actual time=0.000..0.000 rows=0 loops=51)
Recheck Cond: (message_id = messages.id)
Buffers: shared hit=51
-> Bitmap Index Scan on message_attachments_pkey (cost=0.00..4.20 rows=6 width=0) (actual time=0.000..0.000 rows=0 loops=51)
Index Cond: (message_id = messages.id)
Buffers: shared hit=51
Planning Time: 0.045 ms
Execution Time: 0.232 ms
(20 rows)
== 3. Та же страница через OFFSET (так делать не стали)
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=71434.79..72163.43 rows=51 width=112) (actual time=5.082..5.132 rows=51 loops=1)
Buffers: shared hit=5552
-> Index Scan using messages_chat_history_idx on messages (cost=0.42..142912.03 rows=10003 width=112) (actual time=0.012..5.035 rows=5051 loops=1)
Index Cond: (chat_id = '00000000-0000-4000-8000-000000000100'::uuid)
Buffers: shared hit=5552
SubPlan 1
-> Aggregate (cost=13.78..13.79 rows=1 width=32) (actual time=0.001..0.001 rows=1 loops=5051)
Buffers: shared hit=5051
-> Sort (cost=13.74..13.76 rows=6 width=20) (actual time=0.001..0.001 rows=0 loops=5051)
Sort Key: a."position"
Sort Method: quicksort Memory: 25kB
Buffers: shared hit=5051
-> Bitmap Heap Scan on message_attachments a (cost=4.20..13.67 rows=6 width=20) (actual time=0.000..0.000 rows=0 loops=5051)
Recheck Cond: (message_id = messages.id)
Buffers: shared hit=5051
-> Bitmap Index Scan on message_attachments_pkey (cost=0.00..4.20 rows=6 width=0) (actual time=0.000..0.000 rows=0 loops=5051)
Index Cond: (message_id = messages.id)
Buffers: shared hit=5051
Planning Time: 0.032 ms
Execution Time: 5.152 ms
(20 rows)
== 4. Запрос из пункта 2 без индекса messages_chat_history_idx (DROP INDEX внутри транзакции с ROLLBACK)
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=3400.42..4104.22 rows=51 width=112) (actual time=3.126..3.183 rows=51 loops=1)
Buffers: shared hit=1454
-> Result (cost=3400.42..100662.45 rows=7048 width=112) (actual time=3.125..3.180 rows=51 loops=1)
Buffers: shared hit=1454
-> Sort (cost=3400.42..3418.04 rows=7048 width=80) (actual time=3.098..3.100 rows=51 loops=1)
Sort Key: messages.created_at, messages.id
Sort Method: top-N heapsort Memory: 31kB
Buffers: shared hit=1403
-> Seq Scan on messages (cost=0.00..3165.29 rows=7048 width=80) (actual time=1.800..2.874 rows=5000 loops=1)
Filter: ((chat_id = '00000000-0000-4000-8000-000000000100'::uuid) AND (ROW(created_at, id) > ROW('2026-10-30 11:47:36.896462+00'::timestamp with time zone, '0c56411c-acb3-420b-bd20-5ac5ed7ab8a2'::uuid)))
Rows Removed by Filter: 96045
Buffers: shared hit=1397
SubPlan 1
-> Aggregate (cost=13.78..13.79 rows=1 width=32) (actual time=0.001..0.001 rows=1 loops=51)
Buffers: shared hit=51
-> Sort (cost=13.74..13.76 rows=6 width=20) (actual time=0.001..0.001 rows=0 loops=51)
Sort Key: a."position"
Sort Method: quicksort Memory: 25kB
Buffers: shared hit=51
-> Bitmap Heap Scan on message_attachments a (cost=4.20..13.67 rows=6 width=20) (actual time=0.000..0.000 rows=0 loops=51)
Recheck Cond: (message_id = messages.id)
Buffers: shared hit=51
-> Bitmap Index Scan on message_attachments_pkey (cost=0.00..4.20 rows=6 width=0) (actual time=0.000..0.000 rows=0 loops=51)
Index Cond: (message_id = messages.id)
Buffers: shared hit=51
Planning:
Buffers: shared hit=4
Planning Time: 0.050 ms
Execution Time: 3.202 ms
(29 rows)
+52
View File
@@ -0,0 +1,52 @@
{
"workload": "4 клиента, closed loop, POST сообщений через балансировщик, повтор с тем же ключом",
"mode": "stop",
"rounds": [
{
"seconds": 17.89,
"successful_requests": 10761,
"failed_attempts": 0,
"failed_statuses": [],
"successful_rps": 601.6,
"longest_gap_between_successes_s": 1.027,
"first_success_after_action_s": 0.003,
"last_success_on_target_after_action_s": 12.886,
"instances_seen": 2,
"target": "b4e0f5de7608",
"signal": "SIGTERM",
"stop_command_s": 0.59,
"excluded_by_lb_after_s": 0.9,
"verify": {
"confirmed": 10761,
"in_history": 10761,
"lost_confirmed": 0,
"duplicate_ids": 0,
"duplicate_texts": 0,
"unconfirmed_but_saved": 0
}
},
{
"seconds": 19.96,
"successful_requests": 11872,
"failed_attempts": 0,
"failed_statuses": [],
"successful_rps": 594.9,
"longest_gap_between_successes_s": 1.015,
"first_success_after_action_s": 0.0,
"last_success_on_target_after_action_s": 14.955,
"instances_seen": 2,
"target": "6f14dc16ebf3",
"signal": "SIGTERM",
"stop_command_s": 0.61,
"excluded_by_lb_after_s": 1.51,
"verify": {
"confirmed": 11872,
"in_history": 11872,
"lost_confirmed": 0,
"duplicate_ids": 0,
"duplicate_texts": 0,
"unconfirmed_but_saved": 0
}
}
]
}
+35
View File
@@ -0,0 +1,35 @@
{
"workload": "4 клиента, closed loop, POST сообщений через балансировщик, повтор с тем же ключом",
"mode": "rolling",
"rounds": [
{
"seconds": 25.51,
"successful_requests": 17974,
"failed_attempts": 0,
"failed_statuses": [],
"successful_rps": 704.5,
"longest_gap_between_successes_s": 1.001,
"first_success_after_action_s": 0.003,
"last_success_on_target_after_action_s": null,
"instances_seen": 4,
"update_seconds": 15.51,
"containers_before": {
"b4e0f5de7608": "sha256:693fe62509aa",
"6f14dc16ebf3": "sha256:693fe62509aa"
},
"containers_after": {
"abc646dd09cf": "sha256:bdbd0270135d",
"d7f52b7d6c23": "sha256:bdbd0270135d"
},
"app_version": "2",
"verify": {
"confirmed": 17974,
"in_history": 17974,
"lost_confirmed": 0,
"duplicate_ids": 0,
"duplicate_texts": 0,
"unconfirmed_but_saved": 0
}
}
]
}
+76
View File
@@ -0,0 +1,76 @@
{
"limit_per_replica_mib": 256,
"replicas": 2,
"levels": [
{
"parallel_uploads": 0,
"memory": {
"abc646dd09cf": {
"process_peak_rss_mib": 117.2,
"cgroup_peak_mib": 110.5
},
"d7f52b7d6c23": {
"process_peak_rss_mib": 117.1,
"cgroup_peak_mib": 110.2
}
}
},
{
"parallel_uploads": 1,
"file_bytes": 10481664,
"seconds": 0.48,
"slowest_upload_s": 0.48,
"statuses": {
"201": 1
},
"memory": {
"abc646dd09cf": {
"process_peak_rss_mib": 117.2,
"cgroup_peak_mib": 110.5
},
"d7f52b7d6c23": {
"process_peak_rss_mib": 117.5,
"cgroup_peak_mib": 110.2
}
}
},
{
"parallel_uploads": 10,
"file_bytes": 10481664,
"seconds": 3.87,
"slowest_upload_s": 3.87,
"statuses": {
"201": 10
},
"memory": {
"abc646dd09cf": {
"process_peak_rss_mib": 124.3,
"cgroup_peak_mib": 163.7
},
"d7f52b7d6c23": {
"process_peak_rss_mib": 124.1,
"cgroup_peak_mib": 164.1
}
}
},
{
"parallel_uploads": 30,
"file_bytes": 10481664,
"seconds": 6.43,
"slowest_upload_s": 6.42,
"statuses": {
"201": 30
},
"memory": {
"abc646dd09cf": {
"process_peak_rss_mib": 127.3,
"cgroup_peak_mib": 256.0
},
"d7f52b7d6c23": {
"process_peak_rss_mib": 126.9,
"cgroup_peak_mib": 256.0
}
}
}
]
}