ЛР5: брокер Redis Streams, worker, outbox, retry/DLQ/replay, варианты

This commit is contained in:
LOSYASH
2026-11-12 15:14:48 +01:00
parent 276dbdad8a
commit b4804ac5c1
98 changed files with 9131 additions and 38 deletions
+105
View File
@@ -0,0 +1,105 @@
== позитивный сценарий ==
ok - upload PNG -> 202 (got 202)
ok - 202 queued, variants=[], error_code=null
ok - sha256/size describe the original
ok - no S3 key/bucket in public JSON
ok - upload JPEG with Unicode filename -> 202 (got 202)
ok - filename returned as is (display only)
ok - both become ready (worker)
ok - member downloads PNG -> 200 (got 200)
ok - downloaded bytes are identical (sha256)
ok - true Content-Type image/png
ok - nosniff header
ok - member downloads JPEG (variant=original) -> 200 (got 200)
ok - JPEG bytes and type
ok - message with empty text + 2 attachments -> 201 (got 201)
ok - attachment_ids kept in request order
ok - history shows attachment_ids
ok - text-only POST (lab3) still valid, attachment_ids=[]
ok - member reads metadata -> 200 (got 200)
ok - thumbnail variant exists in lab5 -> 200 (got 200)
ok - unknown variant name -> 400 (got 400)
== доступ ==
ok - outsider GET attachment metadata -> 404 (got 404)
ok - unknown attachment metadata -> 404 (got 404)
ok - outsider GET attachment/content -> 404 (got 404)
ok - unknown attachment/content -> 404 (got 404)
ok - anonymous download -> 401 (got 401)
ok - attachment id is not a UUID -> 400 (got 400)
ok - outsider uploads into foreign chat -> 404 (got 404)
ok - anonymous upload -> 401 (got 401)
== привязка к сообщению ==
ok - eve uploads into her own chat -> 202 (got 202)
ok - alice uploads into her second chat -> 202 (got 202)
ok - foreign attachment_id (eve's chat) in message -> 404 (got 404)
ok - attachment of ANOTHER chat of the same user -> 404 (got 404)
ok - unknown attachment_id -> 404 (got 404)
ok - one valid + one foreign -> whole message rejected -> 404 (got 404)
ok - duplicate attachment_ids -> 400 (got 400)
ok - same UUID in different case is a duplicate -> 400 (got 400)
ok - more than 10 attachment_ids -> 400 (got 400)
ok - attachment_id is not a UUID -> 400 (got 400)
ok - attachment_ids: null -> 400 (got 400)
ok - blank text without attachments -> 400 (got 400)
ok - text field is required -> 400 (got 400)
ok - any member may reference a chat attachment again -> 201 (got 201)
ok - eve attaches alice's file to her own chat -> 404 (got 404)
== идемпотентность с вложениями ==
ok - missing attachment_ids == [] (same Message)
ok - same key, different attachment_ids -> 409 (got 409)
ok - replay with attachments returns the same Message
== размер ==
ok - Content-Length = 10 MiB + 1, body never sent (limit before reading) -> 413 (got 413)
ok - Content-Length = 500 MiB -> 413 (got 413)
ok - chunked body > 10 MiB without Content-Length (stream cut at limit) -> 413 (got 413)
ok - real PNG of 10835273 bytes -> 413 (got 413)
ok - real PNG of 10439723 bytes (≤ 10 MiB) accepted -> 202 (got 202)
ok - big file sha256/size
ok - empty body -> 400 (got 400)
== пиксели ==
ok - decompression bomb 8000x8000 in 62290 bytes -> 413 (got 413)
ok - 40 040 001 pixels (just over the limit) -> 413 (got 413)
== повреждённые файлы ==
ok - truncated PNG -> 415 (got 415)
ok - truncated PNG: error code
ok - PNG with flipped byte (CRC) -> 415 (got 415)
ok - PNG with flipped byte (CRC): error code
ok - PNG signature only -> 415 (got 415)
ok - PNG signature only: error code
ok - PNG header + garbage -> 415 (got 415)
ok - PNG header + garbage: error code
ok - truncated JPEG -> 415 (got 415)
ok - truncated JPEG: error code
ok - JPEG SOI + garbage -> 415 (got 415)
ok - JPEG SOI + garbage: error code
== fuzz: случайные порчи байтов (только 202 или 415, никогда 500) ==
ok - 30 corrupted files -> only 202/415 (got [202, 415])
== подмена MIME ==
ok - PNG bytes sent as image/jpeg -> 415 (got 415)
ok - JPEG bytes sent as image/png -> 415 (got 415)
ok - text renamed to .png -> 415 (got 415)
ok - GIF sent as image/png -> 415 (got 415)
ok - HTML sent as image/jpeg -> 415 (got 415)
ok - PNG with Content-Type image/gif -> 415 (got 415)
ok - PNG with Content-Type application/octet-stream -> 415 (got 415)
ok - PNG with Content-Type text/html -> 415 (got 415)
== опасные filename ==
ok - filename '../../etc/passwd' -> 400 (got 400)
ok - filename 'dir/a.png' -> 400 (got 400)
ok - filename 'C:\\Windows\\a.png' -> 400 (got 400)
ok - filename 'a\x00.png' -> 400 (got 400)
ok - filename 'a\nb.png' -> 400 (got 400)
ok - filename 'tab\t.png' -> 400 (got 400)
ok - filename '\x1b[31mred.png' -> 400 (got 400)
ok - filename 'photo\u202egnp.exe' -> 400 (got 400)
ok - filename '..' -> 400 (got 400)
ok - filename '.' -> 400 (got 400)
ok - filename ' ' -> 400 (got 400)
ok - filename 'xxxxxxxxxxxxxxxxxxxx' -> 400 (got 400)
ok - filename missing -> 400 (got 400)
ok - filename of exactly 255 chars -> 202 (got 202)
ok - filename with spaces, Unicode and punctuation -> 202 (got 202)
ok - media type parameters are ignored -> 202 (got 202)
ok - no 500 and no dropped connection among 100 checked responses
93 passed, 0 failed
+3
View File
@@ -0,0 +1,3 @@
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 143, "failed_attempts": 0, "first_recovery_seconds": 0.114}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
+93
View File
@@ -0,0 +1,93 @@
== worker остановлен: приём не зависит от обработки ==
ok - upload → 202 queued without worker (0.02s)
ok - still queued after 3 s: API did not resize synchronously
ok - content of queued attachment → 409 (got 409)
ok - thumbnail of queued attachment → 409 (got 409)
ok - message with queued attachment → 201 (got 201)
ok - other messages keep working
ok - job is in the broker stream (len=1), outbox published
== рестарт брокера и API с сохранением volumes ==
ok - after broker restart the message is still there (len=1)
ok - same session, same attachment, still queued
== worker запущен ==
ok - after restart: status ready, error_code null
ok - after restart: variants thumbnail+crop+optimized (['thumbnail', 'crop', 'optimized'])
ok - after restart: original byte-for-byte
ok - after restart: thumbnail 256x160, sha256 matches
ok - staging removed after ready (original lives in originals/)
attachment b442df77-ff58-4ab7-89e9-979b507b38f8, job e2871dfb-6dd2-46c7-8dd1-ce29c7f22558, events: ['01:30:28.325 accepted 1 api', '01:30:37.814 claimed 1 worker-1-0', '01:30:38.282 ready 1 worker-1']
[worker-down: 14s]
== брокер недоступен: приём не зависит от брокера (transactional outbox) ==
ok - /health/ready = 200: broker is not critical for API
ok - upload → 202 queued while broker is down (0.0s, publish timeout ≤ 2s)
ok - intent kept in outbox, not published yet
ok - after broker is back: status ready, error_code null
ok - after broker is back: variants thumbnail+crop+optimized (['thumbnail', 'crop', 'optimized'])
ok - after broker is back: original byte-for-byte
ok - after broker is back: thumbnail 256x160, sha256 matches
ok - relay published the outbox row when broker came back
[broker-down: 4s]
== повторная доставка уже ready-задачи ==
published duplicate: 1794436247461-0 {"schema_version": 1, "job_id": "5c7dd513-a5f2-4663-b2ad-c498ea0163e5", "type": "at
published duplicate: 1794436247942-0 {"schema_version": 1, "job_id": "5c7dd513-a5f2-4663-b2ad-c498ea0163e5", "type": "at
ok - duplicates: status stays ready, variants unchanged (same sha256)
ok - no second set of variants: rows=3, S3 objects=3
ok - job finished exactly once, attempt unchanged
ok - duplicates acked (no pending messages)
[redelivery: 7s]
== crash после записи в S3, до COMMIT и ack ==
recovered in 22.7s (worker restart + lease expiry)
ok - after crash before ack: status ready, error_code null
ok - after crash before ack: variants thumbnail+crop+optimized (['thumbnail', 'crop', 'optimized'])
ok - after crash before ack: original byte-for-byte
ok - after crash before ack: thumbnail 256x160, sha256 matches
ok - same attempt taken over after lease expiry (redelivery, not a new job)
ok - S3 objects from the crashed attempt reused: still exactly 3 variants
events: ['01:30:56.132 accepted 1 api', '01:30:56.135 claimed 1 worker-fault-1', '01:31:18.241 lease_takeover 1 worker-fault-1', '01:31:18.745 ready 1 worker-fault']
[crash-s3: 27s]
== crash после COMMIT ready, до ack ==
ok - commit before crash: status ready, error_code null
ok - commit before crash: variants thumbnail+crop+optimized (['thumbnail', 'crop', 'optimized'])
ok - commit before crash: original byte-for-byte
ok - commit before crash: thumbnail 256x160, sha256 matches
ok - unacked message redelivered after restart and acked as no-op
ok - ready recorded once, 3 variants
[crash-ack: 8s]
== crash API между COMMIT и publish (transactional outbox) ==
ok - client did NOT get 202: API died after COMMIT (got 502)
ok - attachment and job were committed before the crash (1 new)
ok - relay-published job: status ready, error_code null
ok - relay-published job: variants thumbnail+crop+optimized (['thumbnail', 'crop', 'optimized'])
ok - relay-published job: original byte-for-byte
ok - relay-published job: thumbnail 256x160, sha256 matches
ok - outbox row published by relay (no new job created)
[outbox: 4s]
== временные ошибки → backoff+jitter → failed → replay ==
ok - after 23s: failed, error_code=retry_exhausted
ok - bounded: 5 attempts, 4 scheduled retries, delays [0.6, 1.8, 3.8, 6.4]
ok - backoff grows 2^n with jitter in [d/2, d] (JOB_BACKOFF_BASE_SECONDS=1)
ok - content of failed → 409 (got 409)
ok - failed attachment cannot be attached → 409 (got 409)
ok - failed job is in the registry (python -m app.replay --list)
ok - explicit replay accepted
ok - after replay: status ready, error_code null
ok - after replay: variants thumbnail+crop+optimized (['thumbnail', 'crop', 'optimized'])
ok - after replay: original byte-for-byte
ok - after replay: thumbnail 256x160, sha256 matches
ok - same job_id, attempt 6, replays=1 (audit)
ok - replay of a ready job is refused (no duplicate execution)
[retry-replay: 31s]
== долгая, но живая задача: heartbeat продлевает аренду ==
ok - job ran 31s > lease 20s, no takeover, attempt 1
== замёрзший worker: аренда истекает, задачу забирают ==
ok - claimed by worker-fault, then it froze (processing)
recovered in 40.8s
ok - after frozen worker: status ready, error_code null
ok - after frozen worker: variants thumbnail+crop+optimized (['thumbnail', 'crop', 'optimized'])
ok - after frozen worker: original byte-for-byte
ok - after frozen worker: thumbnail 256x160, sha256 matches
ok - reaper: lease expired → attempt 2 → ready
ok - woken stale worker could not commit (lease_token fencing), result unchanged
[lease: 104s]
66 passed, 0 failed
+2
View File
@@ -0,0 +1,2 @@
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
+5
View File
@@ -0,0 +1,5 @@
failed jobs: 0
1794436317733-0 {"job": "{\"attachment_id\":\"e8a81276-b5df-45cc-8362-f38abe00a557\",\"job_id\":\"2db55464-1734-40c4-9ce1-05b1db9fb834\"}", "reason": "injected_transient", "source_id": "1794436317728-0", "at": "2026-11-11T22:31:57.733543Z"}
1794436083241-0 {"job": "{\"attachment_id\":\"abc0488f-8870-4a6b-96de-299a6571d281\",\"job_id\":\"900a5962-c5a8-4680-8cfd-d3033df2e16d\"}", "reason": "injected_transient", "source_id": "1794436083233-0", "at": "2026-11-11T22:28:03.241832Z"}
1794435590706-0 {"job": "{\"attachment_id\":\"92896e61-6806-4f6e-b4ac-31c8c7aa1c86\",\"job_id\":\"fae19d4e-898f-4d18-bc22-5dd0dbe3c53a\"}", "reason": "injected_transient", "source_id": "1794435590697-0", "at": "2026-11-11T22:19:50.706703Z"}
{"stream_length": 0, "groups": [{"name": "workers", "consumers": 4, "pending": 0, "lag": 0}], "dlq_length": 3, "outbox_unpublished": 0, "jobs_by_status": {"ready": 70}}
+1
View File
@@ -0,0 +1 @@
PASS: lab5 public API scenario; infrastructure and grading evidence remain separate.
+59
View File
@@ -0,0 +1,59 @@
== аутентификация ==
ok - wrong password -> 401 + Basic challenge
ok - unknown user -> 401 (same as wrong password)
ok - no token -> 401 + Bearer challenge
ok - X-User-Id alone gives no access
ok - unknown/forged token -> 401
ok - Basic on business endpoint -> 401
ok - X-User-Id cannot override bearer identity
ok - password shorter than 12 -> 400
== logout ==
ok - logout -> 204
ok - token after logout -> 401
ok - second logout -> 401
== гонка регистрации одного username ==
ok - 20 parallel registrations -> one 201, nineteen 409 (got [201, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409])
== ошибки и доступ (из lab1) ==
ok - malformed JSON -> 400
ok - duplicate username -> 409
ok - invalid username 'review123\n' -> 400, not 500 (got 400)
ok - invalid username 'review123\r\n' -> 400, not 500 (got 400)
ok - invalid username ' review123' -> 400, not 500 (got 400)
ok - invalid username 'Review123' -> 400, not 500 (got 400)
ok - invalid username 'ab' -> 400, not 500 (got 400)
ok - blank message -> 400
ok - sender_id in body is rejected
ok - outsider POST to foreign chat -> 404
ok - outsider GET foreign history -> 404
ok - invalid chat uuid -> 400
== пагинация ==
ok - 5 messages / limit 2 -> 3 pages, no duplicates
ok - cursor from another chat -> 400
== Content-Type ==
ok - valid JSON with text/plain -> 415 (got 415)
ok - message with text/plain -> 415 (got 415)
ok - text/plain without token -> 401 first (got 401)
ok - application/json; charset=utf-8 accepted (got 201)
30 passed, 0 failed
== 20 параллельных запросов с одним ключом ==
ok - all parallel repeats -> 201 (got [201])
ok - all responses carry the same message id (1 distinct)
ok - requests were served by >=2 replicas (2)
ok - exactly one message in history
ok - every replay returns the identical Message
== семантика ключа ==
ok - same key, different body -> 409 (got 409)
ok - other JSON formatting = same request (got 201)
ok - same key in another chat is independent
ok - same key from another user is independent
ok - missing Idempotency-Key -> 400 (got 400)
ok - invalid key 'has space'... -> 400 (got 400)
ok - invalid key 'xxxxxxxxxxxx'... -> 400 (got 400)
ok - invalid key 'slash/key'... -> 400 (got 400)
ok - replay without session -> 401, access re-checked (got 401)
== неопределённый результат: клиент повторяет с тем же ключом ==
ok - retry after uncertain result -> 201
ok - still exactly one message
16 passed, 0 failed
+7
View File
@@ -0,0 +1,7 @@
ok normal 320x200 with alpha: ok (0.4s)
ok pixel limit (36 Mp > limit 10 Mp): invalid_image: pixel limit (0.4s)
ok memory limit (36 Mp, RLIMIT_AS 120 MiB): invalid_image: memory limit (0.3s)
ok corrupt/truncated PNG: invalid_image: cannot decode: OSError (0.3s)
ok time limit (hangs 10 s, timeout 2 s): timeout(killed) (2.0s)
5 passed, 0 failed