Carry over the lab 3 fixes made after 61c0c28

Bring the system changes of lab 3 from 61c0c28..72a1e57 onto the lab 4
code, resolving each conflict by meaning and keeping everything lab 4
added:

- a message is posted with one SQL statement under the chat row lock,
  and the attachment links go into the same statement; a foreign key
  violation on the attachment link still answers 404 and leaves nothing
  behind. Text and the attachment list are checked before the write, an
  outsider gets 404 for both, as before the carry over;
- the request pipeline counts the body bytes actually received against
  the limit of the route, so a chunked body over 1 MiB gets 413 and the
  upload route keeps its MAX_UPLOAD_BYTES limit;
- a request cancelled at shutdown gets 503 unavailable in the contract
  format, the worker waits a second less than the rest of the shutdown
  budget, SHUTDOWN_TIMEOUT_SECONDS becomes 6 s and the stop hooks 7 s;
- readiness sits behind a Readiness port with the draining flag in
  shared infrastructure, and the drain window is logged through Logger;
- logout revokes through SessionRevocations and withdraws the mark when
  PostgreSQL rejects the delete; an empty cursor is 400;
- EventBus.project is gone, the sweep test subscribes and waits on a
  queue; domain tests check attachment_list instead of Chat.post;
- make up waits for every replica in the balancer, BASE_URL follows the
  lb port, the image is tagged by the compose project, retry-check and
  faults-evidence are added, detection runs three times, failover-each
  and rolling-update record the stand usage, CI checks numeric uids.
  down also takes the tools and reconcile profiles of lab 4.

Track 1 tooling of lab 3 (scaling.py, its samplers, tools/ and the CPU
pinning of api, lb, valkey and k6) stays out; stand-usage.sh queries the
database size itself instead of the track 1 sampler.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V1d3RLHNBBMA6UqBLLJv27
This commit is contained in:
Artyom
2026-10-30 06:25:23 +03:00
co-authored by Claude Opus 5.5
parent 7ac1a4a11b
commit e927afdb09
56 changed files with 932 additions and 397 deletions
+16 -77
View File
@@ -9,13 +9,12 @@ from messenger.identity.domain.errors import InvalidUserData
from messenger.identity.domain.models import Session, User, token_digest
from messenger.identity.domain.values import DisplayName, Password, Username
from messenger.messaging.domain.errors import (
ChatNotFound,
IdempotencyConflict,
InvalidAttachmentList,
InvalidChatData,
InvalidMessageText,
)
from messenger.messaging.domain.models import Chat, Receipt
from messenger.messaging.domain.models import Chat, Message, Receipt, attachment_list
from messenger.messaging.domain.values import MessageText
from messenger.shared.infrastructure.clock import MonotonicUtcClock
@@ -90,17 +89,6 @@ def test_message_text_rejects_whitespace() -> None:
assert MessageText("").value == ""
def make_chat(*members: object) -> Chat:
ids = [uuid4() for _ in members] if members else [uuid4()]
return Chat.create(
chat_id=uuid4(),
title="Чат",
member_ids=ids,
creator_id=ids[0],
created_at=MOMENT,
)
def test_chat_requires_creator_in_members() -> None:
members = [uuid4(), uuid4()]
with pytest.raises(InvalidChatData):
@@ -137,87 +125,38 @@ def test_chat_rejects_more_than_hundred_members() -> None:
)
def test_outsider_cannot_post() -> None:
chat = make_chat(1)
with pytest.raises(ChatNotFound):
chat.post(message_id=uuid4(), sender_id=uuid4(), text="привет", now=MOMENT)
def test_post_never_moves_history_back() -> None:
chat = make_chat(1)
sender = chat.member_ids[0]
first = chat.post(
message_id=uuid4(), sender_id=sender, text="первое", now=MOMENT + timedelta(seconds=1)
)
second = chat.post(message_id=uuid4(), sender_id=sender, text="второе", now=MOMENT)
assert second.created_at == first.created_at + timedelta(microseconds=1)
assert chat.last_message_at == second.created_at
def test_post_keeps_clock_when_it_is_ahead() -> None:
chat = make_chat(1)
sender = chat.member_ids[0]
first = chat.post(message_id=uuid4(), sender_id=sender, text="первое", now=MOMENT)
later = MOMENT + timedelta(seconds=5)
second = chat.post(message_id=uuid4(), sender_id=sender, text="второе", now=later)
assert first.created_at == MOMENT
assert second.created_at == later
def test_post_validates_membership_before_text() -> None:
chat = make_chat(1)
with pytest.raises(ChatNotFound):
chat.post(message_id=uuid4(), sender_id=uuid4(), text="", now=MOMENT)
def test_empty_text_needs_an_attachment() -> None:
chat = make_chat(1)
sender = chat.member_ids[0]
with pytest.raises(InvalidMessageText):
chat.post(message_id=uuid4(), sender_id=sender, text="", now=MOMENT)
ids = (uuid4(), uuid4())
message = chat.post(
message_id=uuid4(), sender_id=sender, text="", now=MOMENT, attachment_ids=ids
)
assert message.attachment_ids == ids
with pytest.raises(InvalidMessageText):
chat.post(message_id=uuid4(), sender_id=sender, text=" ", now=MOMENT, attachment_ids=ids)
attachment_list(MessageText(""), ())
assert attachment_list(MessageText(""), ids) == ids
@pytest.mark.parametrize("ids", [[uuid4() for _ in range(11)], [UUID_TWICE, UUID_TWICE]])
def test_attachment_list_is_bounded_and_unique(ids: list[UUID]) -> None:
chat = make_chat(1)
with pytest.raises(InvalidAttachmentList):
chat.post(
message_id=uuid4(),
sender_id=chat.member_ids[0],
text="x",
now=MOMENT,
attachment_ids=ids,
)
def test_membership_is_checked_before_attachments() -> None:
chat = make_chat(1)
with pytest.raises(ChatNotFound):
chat.post(
message_id=uuid4(),
sender_id=uuid4(),
text="",
now=MOMENT,
attachment_ids=[uuid4() for _ in range(11)],
)
attachment_list(MessageText("x"), ids)
def test_receipt_replays_only_the_same_request() -> None:
chat = make_chat(1)
message = chat.post(message_id=uuid4(), sender_id=chat.member_ids[0], text="раз", now=MOMENT)
message = Message(uuid4(), uuid4(), uuid4(), MessageText("раз"), MOMENT)
receipt = Receipt("key-1", b"a" * 32, message)
assert receipt.replay(b"a" * 32) is message
with pytest.raises(IdempotencyConflict):
receipt.replay(b"b" * 32)
def test_posted_message_describes_itself() -> None:
message = Message(uuid4(), uuid4(), uuid4(), MessageText("пять"), MOMENT)
event = message.posted()
assert (event.message_id, event.chat_id, event.sender_id) == (
message.id,
message.chat_id,
message.sender_id,
)
assert (event.text_length, event.created_at, event.name) == (4, MOMENT, "message.posted")
def test_user_registration_emits_event() -> None:
user, event = User.register(
user_id=uuid4(),
+4 -3
View File
@@ -46,15 +46,16 @@ def test_timeouts_shrink_inwards_and_shutdown_fits_the_stop_budget() -> None:
database = api["DB_ACQUIRE_TIMEOUT_SECONDS"] + api["DB_COMMAND_TIMEOUT_SECONDS"]
drain, shutdown = api["DRAIN_SECONDS"], api["SHUTDOWN_TIMEOUT_SECONDS"]
print(
f"обнаружение {detection} с (зависание {hung} с), окно {drain} с, остановка {shutdown} с,"
f" хук {hook} с, stop_grace_period {grace} с; LB server {lb['timeout server']} с >"
f"обнаружение {detection} с (зависание {hung} с), окно {drain} с,"
f" дообработка {shutdown - drain - 1} с, остановка {shutdown} с, хук {hook} с,"
f" stop_grace_period {grace} с; LB server {lb['timeout server']} с >"
f" запрос {api['REQUEST_TIMEOUT_SECONDS']} с > БД {database} с;"
f" readiness {2 * PING_TIMEOUT_SECONDS} с < check {lb['timeout check']} с"
)
assert lb["timeout server"] > api["REQUEST_TIMEOUT_SECONDS"] > database
assert lb["timeout check"] > 2 * PING_TIMEOUT_SECONDS
assert drain > detection
assert shutdown - drain >= database
assert shutdown - drain - 1 >= database
assert shutdown < hook < grace < 10
assert lb["hard-stop-after"] < seconds(stand["lb"]["stop_grace_period"])
assert (detection, hung) == (1.5, 3.5)