From 4830be414eeebe131072b2bea93b37b1571ff0b3 Mon Sep 17 00:00:00 2001 From: Alexey Bodanyuk <144894236+wfa22@users.noreply.github.com> Date: Mon, 19 Oct 2026 16:03:22 +0300 Subject: [PATCH] =?UTF-8?q?perf:=20=D0=B8=D0=B4=D0=B5=D0=BC=D0=BF=D0=BE?= =?UTF-8?q?=D1=82=D0=B5=D0=BD=D1=82=D0=BD=D0=B0=D1=8F=20=D0=BE=D1=82=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=BA=D0=B0=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BE=D0=B4=D0=BD=D0=B8=D0=BC=20?= =?UTF-8?q?=D0=B2=D1=8B=D0=B7=D0=BE=D0=B2=D0=BE=D0=BC=20SQL=20(=D1=82?= =?UTF-8?q?=D1=80=D0=B5=D0=BA=201)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Гипотеза из профиля (py-spy, 2 x 1.0 CPU, 240 req/s): ~57 % CPU реплики уходит в psycopg — накладные расходы на каждый оператор; на одно сообщение ~11 обращений к PostgreSQL в двух транзакциях (проверка членства, BEGIN, ключ, часы чата, сообщение, COMMIT, проверки пула). - migrations/0003: функция post_message — та же логика и тот же SQL, что в многооператорном пути; вызывается в autocommit, поэтому вызов и есть транзакция (ключ, часы чата и сообщение — всё или ничего). - пул: autocommit-соединения; многооператорная работа по-прежнему в явной conn.transaction(); ping — один оператор без транзакции. - MESSAGE_WRITE_PATH=function (по умолчанию) | multi (прежний путь) — A/B на одном образе. Порядок ответов одинаков: постороннему всегда 404, даже при неверном ключе/теле. - Оба режима проходят одни и те же тесты идемпотентности и контракта. Co-Authored-By: Claude Opus 5.5 --- .env.example | 2 + compose.yaml | 2 + migrations/0003_post_message_function.sql | 50 +++++++++++++++++++++++ src/messenger/app.py | 41 ++++++++++++++----- src/messenger/config.py | 11 +++++ src/messenger/db.py | 11 ++++- src/messenger/repo.py | 23 +++++++++++ student-tests/test_balancing.py | 2 +- student-tests/test_db.py | 3 +- student-tests/unit/test_unit.py | 11 +++++ 10 files changed, 141 insertions(+), 15 deletions(-) create mode 100644 migrations/0003_post_message_function.sql diff --git a/.env.example b/.env.example index c18efac8..06ec8428 100644 --- a/.env.example +++ b/.env.example @@ -15,6 +15,8 @@ CACHE_URL_FILE=/run/secrets/cache_url API_REPLICAS=2 # Idempotency-Key results are kept at least this long (contract: >= 24 h). IDEMPOTENCY_TTL_SECONDS=86400 +# Write path of POST message: function = one SQL call (migration 0003), multi = lab3 baseline path. +MESSAGE_WRITE_PATH=function # Additional settings of this implementation (defaults shown; all optional): LOG_LEVEL=info DB_POOL_MIN=1 diff --git a/compose.yaml b/compose.yaml index 53789a79..9e41512f 100644 --- a/compose.yaml +++ b/compose.yaml @@ -27,6 +27,8 @@ x-api-env: &api-env DRAIN_SECONDS: ${DRAIN_SECONDS:-3} SHUTDOWN_TIMEOUT_SECONDS: ${SHUTDOWN_TIMEOUT_SECONDS:-8} DOCS_ENABLED: ${DOCS_ENABLED:-true} + # Track 1 improvement: `function` = one SQL call per send (default), `multi` = old path (A/B). + MESSAGE_WRITE_PATH: ${MESSAGE_WRITE_PATH:-function} services: postgres: diff --git a/migrations/0003_post_message_function.sql b/migrations/0003_post_message_function.sql new file mode 100644 index 00000000..268a0c0a --- /dev/null +++ b/migrations/0003_post_message_function.sql @@ -0,0 +1,50 @@ +-- 0003 (lab 3, track 1 improvement): the whole idempotent "send message" as ONE statement. +-- Additive (a new function), so replicas of the previous version keep working while it is +-- applied, and the previous write path (MESSAGE_WRITE_PATH=multi) stays usable for A/B runs. +-- +-- Same semantics and the same SQL as repo.post_message (multi-statement path), but executed +-- inside the server: the API sends one call instead of ~11 round trips in two transactions +-- (membership check, BEGIN, key claim, chat clock, message, COMMIT, pool checks). Called in +-- autocommit mode, so the function call IS the transaction: key row, chat clock and message +-- commit together or not at all. VOLATILE + READ COMMITTED: every statement inside takes a +-- fresh snapshot, so after waiting on a concurrent claim of the same key the stored result +-- of the winner is visible, exactly as in the multi-statement path. +-- +-- o_status: created | replayed | conflict | not_found | retry (key vanished in between: cleanup) +CREATE FUNCTION post_message(p_user uuid, p_chat uuid, p_key text, p_hash bytea, p_message uuid, + p_text bytea, p_ttl_seconds integer) +RETURNS TABLE (o_status text, o_id uuid, o_text bytea, o_created_at timestamptz) +LANGUAGE plpgsql VOLATILE AS $$ +DECLARE + v_created timestamptz; +BEGIN + PERFORM 1 FROM chat_members m WHERE m.chat_id = p_chat AND m.user_id = p_user; + IF NOT FOUND THEN + RETURN QUERY SELECT 'not_found'::text, NULL::uuid, NULL::bytea, NULL::timestamptz; + RETURN; + END IF; + + INSERT INTO idempotency_keys AS k (user_id, chat_id, key, request_hash, message_id) + VALUES (p_user, p_chat, p_key, p_hash, p_message) + ON CONFLICT (user_id, chat_id, key) DO UPDATE + SET request_hash = EXCLUDED.request_hash, message_id = EXCLUDED.message_id, created_at = clock_timestamp() + WHERE k.created_at < clock_timestamp() - make_interval(secs => p_ttl_seconds); + IF FOUND THEN + -- Per-chat clock under the chat row lock (see migrations/0001 and docs/DATA_MODEL.md). + UPDATE chats c SET last_message_at = greatest(clock_timestamp(), c.last_message_at + interval '1 microsecond') + WHERE c.id = p_chat RETURNING c.last_message_at INTO v_created; + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + VALUES (p_message, p_chat, p_user, p_text, v_created); + RETURN QUERY SELECT 'created'::text, p_message, p_text, v_created; + RETURN; + END IF; + + RETURN QUERY + SELECT CASE WHEN k.request_hash = p_hash THEN 'replayed' ELSE 'conflict' END, m.id, m.text, m.created_at + FROM idempotency_keys k JOIN messages m ON m.id = k.message_id + WHERE k.user_id = p_user AND k.chat_id = p_chat AND k.key = p_key; + IF NOT FOUND THEN + RETURN QUERY SELECT 'retry'::text, NULL::uuid, NULL::bytea, NULL::timestamptz; + END IF; +END +$$; diff --git a/src/messenger/app.py b/src/messenger/app.py index 1f0c4e18..aa4852a1 100644 --- a/src/messenger/app.py +++ b/src/messenger/app.py @@ -287,23 +287,41 @@ async def get_chat(request: Request) -> JSONResponse: return ok((await repo.get_chat(db_of(request), user_id, chat_id)).to_json()) -async def create_message(request: Request) -> JSONResponse: - user_id = await authenticate(request) - chat_id = path_uuid(request, "chat_id") - db = db_of(request) - await repo.ensure_member(db, user_id, chat_id) # outsiders get 404 before key/body are looked at - key = idempotency_key(request) - data = await read_json(request) +def parse_message(data: dict[str, Any]) -> tuple[str, list[str]]: validation.check_fields(data, {"text"}, frozenset({"attachment_ids"})) text = validation.text_field(data, "text", 4000) attachment_ids = validation.attachment_ids_field(data) if attachment_ids: # Lab 3 has no attachments: every referenced ID is unknown (lab 4 semantics: 404). raise NotFound("Вложение не найдено") - # sender_id is derived from the session, never taken from the body. + return text, attachment_ids + + +async def create_message(request: Request) -> JSONResponse: + """Order of answers for both write paths: 401 -> 400 bad chat id -> 404 outsider -> 400 key/body + -> 409 other body with the same key -> 201 (new or replayed). sender_id comes from the session.""" + user_id = await authenticate(request) + chat_id = path_uuid(request, "chat_id") + db = db_of(request) settings = request.app.state.settings - result = await repo.post_message(db, user_id, chat_id, key, request_fingerprint(text, attachment_ids), text, - settings.idempotency_ttl_seconds) + if settings.message_write_path == "multi": + await repo.ensure_member(db, user_id, chat_id) # outsiders get 404 before key/body are looked at + key = idempotency_key(request) + text, attachment_ids = parse_message(await read_json(request)) + result = await repo.post_message(db, user_id, chat_id, key, request_fingerprint(text, attachment_ids), text, + settings.idempotency_ttl_seconds) + else: + try: + key = idempotency_key(request) + text, attachment_ids = parse_message(await read_json(request)) + except ApiError: + # Rare error path only: an outsider must still get 404, not a hint about the body. + await repo.ensure_member(db, user_id, chat_id) + raise + # Membership check, key claim, chat clock and message: one SQL call, one transaction. + result = await repo.post_message_single_call(db, user_id, chat_id, key, + request_fingerprint(text, attachment_ids), text, + settings.idempotency_ttl_seconds) if result.replayed: log.info("idempotent_replay", extra={"fields": {"request_id": request.state.request_id, "message_id": result.message.id}}) @@ -361,7 +379,8 @@ def create_app(settings: Settings | None = None) -> Starlette: log.info("dependencies_configured", extra={"fields": { "session_store": sessions.name, "session_ttl_s": settings.session_ttl_seconds, "db_pool_max": settings.db_pool_max, "db_timeout_s": settings.db_timeout_seconds, - "instance_id": settings.instance_id, "drain_s": settings.drain_seconds}}) + "instance_id": settings.instance_id, "drain_s": settings.drain_seconds, + "message_write_path": settings.message_write_path}}) tasks = [asyncio.create_task(purge_loop())] if settings.stats_log_seconds > 0: tasks.append(asyncio.create_task(stats_loop(settings.stats_log_seconds))) diff --git a/src/messenger/config.py b/src/messenger/config.py index f363b0c3..1c109902 100644 --- a/src/messenger/config.py +++ b/src/messenger/config.py @@ -69,6 +69,13 @@ def _bool(name: str, default: bool) -> bool: return raw in {"true", "1"} +def _choice(name: str, default: str, allowed: set[str]) -> str: + value = os.environ.get(name, "").strip().lower() or default + if value not in allowed: + raise ConfigError(f"{name} must be one of {sorted(allowed)}, got {value!r}") + return value + + def _secret(name: str, required: bool) -> str: """Value of _FILE (preferred, Docker secret) or (local runs/tests).""" path = os.environ.get(f"{name}_FILE", "").strip() @@ -107,6 +114,7 @@ class Settings: drain_seconds: float = 0.0 idempotency_ttl_seconds: int = 86400 stats_log_seconds: float = 0.0 + message_write_path: str = "function" @property def db_statement_timeout_ms(self) -> int: @@ -154,4 +162,7 @@ class Settings: idempotency_ttl_seconds=_int("IDEMPOTENCY_TTL_SECONDS", 86400, 86400, 30 * 24 * 3600), # Experiments (bench/track1): log DB pool counters and process CPU every N s; 0 = off. stats_log_seconds=_float("STATS_LOG_SECONDS", 0.0, 0.0, 60.0), + # Track 1: `function` = one SQL call per send (migration 0003), `multi` = the previous + # multi-statement transaction; both have identical semantics (A/B on one image). + message_write_path=_choice("MESSAGE_WRITE_PATH", "function", {"function", "multi"}), ) diff --git a/src/messenger/db.py b/src/messenger/db.py index 33f41267..1c81bfdd 100644 --- a/src/messenger/db.py +++ b/src/messenger/db.py @@ -74,6 +74,10 @@ class Database: "options": options, "connect_timeout": max(1, int(settings.db_timeout_seconds)), "application_name": "messenger-api", + # Autocommit connections: a single statement (post_message function, ping) is its + # own transaction with ONE round trip; multi-statement work uses an explicit + # conn.transaction() (BEGIN ... COMMIT), see run(). + "autocommit": True, # Detect a dead peer / lost network without any cancel request (see module doc). "keepalives": 1, "keepalives_idle": 5, @@ -91,14 +95,17 @@ class Database: async def close(self) -> None: await self.pool.close(timeout=2) - async def run(self, work: Work[T], deadline: float | None = None) -> T: + async def run(self, work: Work[T], deadline: float | None = None, *, transaction: bool = True) -> T: """Run `work(conn)` in one transaction (COMMIT on success, ROLLBACK on any exception). + transaction=False: `work` issues exactly ONE statement, which autocommit makes atomic. Domain errors raised by `work` (NotFound, Conflict, ...) propagate unchanged; database unavailability and the deadline become Unavailable (503). """ async def unit() -> T: async with self.pool.connection() as conn: + if not transaction: + return await work(conn) async with conn.transaction(): return await work(conn) @@ -118,7 +125,7 @@ class Database: async def select_one(conn: psycopg.AsyncConnection) -> None: await conn.execute("SELECT 1") try: - await self.run(select_one, deadline=timeout) + await self.run(select_one, deadline=timeout, transaction=False) return True except Unavailable: return False diff --git a/src/messenger/repo.py b/src/messenger/repo.py index 71bb0c6d..02e1a8c3 100644 --- a/src/messenger/repo.py +++ b/src/messenger/repo.py @@ -268,6 +268,29 @@ async def post_message(db: Database, user_id: str, chat_id: str, key: str, reque return await db.run(work) +POST_MESSAGE_CALL = "SELECT o_status, o_id, o_text, o_created_at FROM post_message(%s, %s, %s, %s, %s, %s, %s)" + + +async def post_message_single_call(db: Database, user_id: str, chat_id: str, key: str, request_hash: bytes, + text: str, ttl_seconds: int) -> PostResult: + """Same contract as post_message, ONE round trip: SQL function post_message (migration 0003) + in autocommit mode, i.e. the call is the whole transaction. Track 1 improvement.""" + message_id = uuid.uuid4() + + async def work(conn: psycopg.AsyncConnection) -> tuple: + cur = await conn.execute(POST_MESSAGE_CALL, + (user_id, chat_id, key, request_hash, message_id, _b(text), ttl_seconds)) + return await cur.fetchone() + status, stored_id, stored_text, created_at = await db.run(work, transaction=False) + if status == "not_found": + raise NotFound("Чат не найден") + if status == "conflict": + raise Conflict("Idempotency-Key уже использован с другим сообщением") + if status == "retry": + raise Unavailable("Повторите запрос с тем же Idempotency-Key") + return PostResult(Message(str(stored_id), chat_id, user_id, _s(stored_text), created_at), status == "replayed") + + async def purge_idempotency_keys(db: Database, ttl_seconds: int, batch: int = 5000) -> int: """Delete expired keys in bounded batches; one replica at a time (advisory xact lock).""" async def work(conn: psycopg.AsyncConnection) -> int: diff --git a/student-tests/test_balancing.py b/student-tests/test_balancing.py index d1b2eed2..f9ad850d 100644 --- a/student-tests/test_balancing.py +++ b/student-tests/test_balancing.py @@ -317,7 +317,7 @@ def test_running_containers_have_limits_and_no_bypass_ports(): def test_migrations_ran_once_by_the_migrate_service(): applied = psql("SELECT version FROM schema_migrations ORDER BY version").split() - assert applied == ["0001_init", "0002_idempotency_keys"] + assert applied == ["0001_init", "0002_idempotency_keys", "0003_post_message_function"] migrate = compose("ps", "-a", "--format", "json", "migrate").stdout rows = [json.loads(line) for line in migrate.splitlines() if line.strip()] assert rows and all(r["State"] == "exited" and r["ExitCode"] == 0 for r in rows) diff --git a/student-tests/test_db.py b/student-tests/test_db.py index 3e250c68..5fb4a26b 100644 --- a/student-tests/test_db.py +++ b/student-tests/test_db.py @@ -77,7 +77,8 @@ def test_migrations_rerun_is_a_noop(): result = compose("run", "--rm", "--no-deps", "migrate") assert "0001_init: already applied, skipped" in result.stdout assert "0002_idempotency_keys: already applied, skipped" in result.stdout - assert psql("SELECT count(*) FROM schema_migrations") == "2" + assert "0003_post_message_function: already applied, skipped" in result.stdout + assert psql("SELECT count(*) FROM schema_migrations") == "3" assert psql("SELECT count(*) FROM users") == users_before diff --git a/student-tests/unit/test_unit.py b/student-tests/unit/test_unit.py index ebbc5b7d..3840880b 100644 --- a/student-tests/unit/test_unit.py +++ b/student-tests/unit/test_unit.py @@ -189,3 +189,14 @@ def test_idempotency_ttl_cannot_be_below_24_hours(monkeypatch): monkeypatch.setenv("IDEMPOTENCY_TTL_SECONDS", "3600") with pytest.raises(ConfigError): Settings.from_env() + + +def test_message_write_path_setting(monkeypatch): + monkeypatch.setenv("DATABASE_URL", "postgresql://x@y/z") + monkeypatch.delenv("MESSAGE_WRITE_PATH", raising=False) + assert Settings.from_env().message_write_path == "function" + monkeypatch.setenv("MESSAGE_WRITE_PATH", "multi") + assert Settings.from_env().message_write_path == "multi" + monkeypatch.setenv("MESSAGE_WRITE_PATH", "fast") + with pytest.raises(ConfigError): + Settings.from_env()