Cache the immutable message statement and validate scaling evidence

This commit is contained in:
Artyom
2026-11-05 23:59:54 +03:00
parent 05d69ba6ac
commit 57744c532c
7 changed files with 87 additions and 39 deletions
+2 -2
View File
@@ -27,9 +27,9 @@ make limits # уровень 4: лимиты, порты, с
make detection # уровень 4: время обнаружения SIGTERM/kill/pause/PostgreSQL против заявленного максимума
make failover-each # уровень 4: каждая реплика по очереди под k6 (RATE=100) → evidence/failover-each
make rolling-update [FROM_REV=<коммит>] # уровень 4: замена реплик по одной под k6, migrate один раз
make track1 # трек 1: снимок хоста, серии A (1x4/2x2/4x1) и B (1x1/2x1/8x1), до/после на 8x1, контроль, графики (≈3,5 ч)
make track1 # трек 1: снимок хоста, серии A (1x4/2x2/4x1) и B (1x1/2x1/8x1), до/после на 8x1, контроль, графики; volumes сохраняются
make track1-control # трек 1: пул 4/8/16 и maxconn 16/32/64 на 8x1; 8x0.5 против 4x1 на одних CPU
make plots # трек 1: графики и сводка потолков из evidence/track1/*/summary.csv
make plots # трек 1: графики и сводка потолков из evidence/track1-sqlalchemy/*/summary.csv
make down # остановка без удаления данных (с профилем bench); make reset-data — удаление volumes
```
@@ -60,3 +60,17 @@ Context7: /sqlalchemy/sqlalchemy/rel_2_0_45, core/connections.rst, SQL compilati
caching и lambda_stmt. Прямой lambda_stmt(post_message) отвергнут библиотекой из-за
timedelta внутри функции; enable_tracking=False допустим для полностью статического
дерева с явными bindparam. Версия проверена локально: 2.0.45.
Для графиков используется Matplotlib 3.11.2 (версия закреплена в make plots), backend Agg.
Context7 подтвердил PNG-рендеринг; поле Description для даты/SHA дополнительно сверено с
[официальным API PNG](https://matplotlib.org/stable/api/backend_agg_api.html#matplotlib.backends.backend_agg.FigureCanvasAgg.print_png).
## Наблюдения базовых серий
A: 54 прогона; пики 2687/2532/2422 rps для 1×4/2×2/4×1.
B: 36 прогонов; 8×1 даёт около 3420 rps при 3600 предложенных, дальше
пик не растёт. На 5400 медианы: около 32 соединений ждут Lock, 16 idle in transaction
из пула 64; PG CPU около 158/200%, API около 733/800%, LB далёк от квоты.
Это сочетание CPU приложения и ожидания клиента под блокировкой чата; рост очереди
LB — следствие, а не доказательство насыщения CPU балансировщика. Контроль пулов и
кэширование CTE проверяют эту интерпретацию. Все 90 точек приняты без помех.
@@ -5,12 +5,12 @@
## 2. Базовые серии SQLAlchemy
- [ ] 2.1 Снять хост и серию A: 1×4/2×2/4×1, 3 повтора каждой точки.
- [ ] 2.2 Снять серию B: 1×1/2×1/8×1, найти и объяснить насыщенное звено.
- [x] 2.1 Снять хост и серию A: 1×4/2×2/4×1, 3 повтора каждой точки.
- [x] 2.2 Снять серию B: 1×1/2×1/8×1, найти и объяснить насыщенное звено.
## 3. Улучшение messaging
- [ ] 3.1 Кэшировать неизменный CTE; проверить API, гонки, TTL, rollback и изоляцию параметров.
- [x] 3.1 Кэшировать неизменный CTE; проверить API, гонки, TTL, rollback и изоляцию параметров.
- [ ] 3.2 Провести ≥3 чередующихся повтора before/after на одной конфигурации.
- [ ] 3.3 Провести контроль пулов/maxconn и числа процессов на одинаковых CPU.
+3
View File
@@ -259,6 +259,8 @@ def provenance(folder, args, env):
"project": args.project,
"command": sys.argv,
"api_image": env.get("API_IMAGE", "compose default"),
"warmup_seconds": args.warmup,
"duration_seconds": args.duration,
"containers": args.deployment,
}
(folder / "metadata.json").write_text(json.dumps(metadata, ensure_ascii=False) + "\n")
@@ -344,6 +346,7 @@ def step(base, rate, repeat, args, env):
or row["gap_s"] > args.max_gap
or wall > args.warmup + args.duration + args.max_overhead
or row["cpu_k6"] >= 95 * float(env.get("K6_CPUS", "2"))
or any(row[f"cpu_{name}"] <= 0 for name in ("vm", "api", "postgres", "k6"))
or not rows(folder / "pg_activity.csv", start, end)
or not rows(folder / "lb_stats.csv", start, end)
)
@@ -18,6 +18,7 @@ from sqlalchemy import (
bindparam,
func,
insert,
lambda_stmt,
literal,
select,
tuple_,
@@ -173,7 +174,7 @@ def post_message() -> Select:
).add_cte(touched)
POST_MESSAGE = post_message()
POST_MESSAGE = lambda_stmt(post_message, enable_tracking=False)
def member_ids() -> Label[Sequence[UUID]]:
+15 -5
View File
@@ -95,12 +95,22 @@ async def test_key_is_scoped_by_user_and_chat(client: AsyncClient) -> None:
bob = await register(client, "bob")
chat = await make_chat(client, alice, bob)
other = await make_chat(client, alice)
ids = {
(await send(client, chat, alice, "алиса", "shared-1")).json()["id"],
(await send(client, chat, bob, "боб", "shared-1")).json()["id"],
(await send(client, other, alice, "другой чат", "shared-1")).json()["id"],
}
cases = [(chat, alice, "алиса"), (chat, bob, "боб"), (other, alice, "другой чат")]
messages = []
for target, user, text in cases:
response = await send(client, target, user, text, "shared-1")
assert response.status_code == 201
message = response.json()
assert (message["chat_id"], message["sender_id"], message["text"]) == (
target["id"],
user["id"],
text,
)
messages.append(message)
ids = {message["id"] for message in messages}
assert len(ids) == 3
for (target, user, text), message in zip(cases, messages, strict=True):
assert (await send(client, target, user, text, "shared-1")).json() == message
async def test_replay_checks_session_and_access_again(client: AsyncClient) -> None:
+48 -28
View File
@@ -2,6 +2,8 @@ import argparse
import collections
import csv
import statistics
import subprocess
import time
from pathlib import Path
import matplotlib.pyplot as plt
@@ -13,8 +15,13 @@ USL_POINTS = 3
MIN_REPEATS = 3
COMPONENTS = (("api", "API"), ("postgres", "PostgreSQL"), ("lb", "HAProxy"), ("k6", "k6"))
INK, MUTED, GRID, SURFACE = "#0b0b0b", "#52514e", "#e4e3df", "#fcfcfb"
STAMP = (
f"date: {time.strftime('%FT%TZ', time.gmtime())} commit: "
+ subprocess.check_output(["/usr/bin/env", "git", "rev-parse", "HEAD"], text=True).strip()
)
SUMMARY = (
"series variant config rates capacity_rate capacity_ok_rps capacity_p95_ms capacity_p99_ms "
"series variant config rates capacity_rate capacity_label "
"capacity_ok_rps capacity_p95_ms capacity_p99_ms "
"peak_rate peak_ok_rps peak_ok_min peak_ok_max peak_p95_ms peak_failed_ratio peak_dropped "
"cpu_api cpu_api_limit cpu_postgres cpu_lb cpu_k6 pg_active_avg pg_lock_avg pg_idle_tx_avg "
"pg_idle_avg pool_total pg_backends_max lb_qcur_max"
@@ -114,6 +121,9 @@ def summarize(key, rates):
for field, name in (("ok_rps", "ok_rps"), ("p95_ms", "p95_ms"), ("p99_ms", "p99_ms")):
row[f"capacity_{name}"] = med(rates[capacity], field)[0]
row["capacity_rate"] = capacity
row["capacity_label"] = (
f"≥{capacity}" if capacity in (min(rates), max(rates)) else str(capacity)
)
return row
@@ -123,13 +133,21 @@ def direct_label(axis, x, y, text):
)
def lines(axis, keys, groups, field):
for key in keys:
def save(figure, path):
figure.tight_layout()
figure.savefig(path, dpi=150, metadata={"Description": STAMP})
plt.close(figure)
def lines(axis, keys, groups, field, categorical=False):
for index, key in enumerate(keys):
rates = sorted(groups[key])
values = [med(groups[key][rate], field) for rate in rates]
look = VARIANTS.get(key[1]) or style(key[2])
if categorical:
look = {"color": PALETTE[index], "marker": MARKERS[index]}
axis.errorbar(
rates,
[index] if categorical else rates,
[v[0] for v in values],
yerr=[[v[1] for v in values], [v[2] for v in values]],
capsize=3,
@@ -138,28 +156,34 @@ def lines(axis, keys, groups, field):
markeredgewidth=1.5,
**look,
)
direct_label(axis, rates[-1], values[-1][0], label(key))
if not categorical:
direct_label(axis, rates[-1], values[-1][0], label(key))
if categorical:
axis.set_xticks(range(len(keys)), [label(k) for k in keys], rotation=25, ha="right")
def throughput(series, keys, groups, out):
figure, axes = plt.subplots(1, 3, figsize=(17, 4.8))
top = max(rate for key in keys for rate in groups[key])
axes[0].plot(
[0, top], [0, top], color=MUTED, linestyle="--", linewidth=1, label="RPS = нагрузка"
)
lines(axes[0], keys, groups, "ok_rps")
categorical = len({rate for key in keys for rate in groups[key]}) == 1
if not categorical:
axes[0].plot(
[0, top], [0, top], color=MUTED, linestyle="--", linewidth=1, label="RPS = нагрузка"
)
lines(axes[0], keys, groups, "ok_rps", categorical)
axes[0].set_title("Успешные RPS")
for axis, field, title in ((axes[1], "p95_ms", "p95, мс"), (axes[2], "p99_ms", "p99, мс")):
lines(axis, keys, groups, field)
lines(axis, keys, groups, field, categorical)
axis.set_yscale("log")
axis.set_title(title)
for axis in axes:
axis.set_xlabel("Предложенная нагрузка, запросов/с")
axis.legend(loc="upper left", fontsize=9)
figure.suptitle(f"{series}: медиана 3 повторов, усы — min–max", color=INK)
figure.tight_layout()
figure.savefig(out / f"{series}-throughput.png", dpi=150)
plt.close(figure)
axis.set_xlabel(
f"Вариант при {top} запросах/с" if categorical else "Предложенная нагрузка, запросов/с"
)
if not categorical:
axis.legend(loc="upper left", fontsize=9)
figure.suptitle(f"{series}: медиана повторов, усы — min–max", color=INK)
save(figure, out / f"{series}-throughput.png")
def cpu(series, keys, groups, out):
@@ -176,9 +200,7 @@ def cpu(series, keys, groups, out):
axis.set_title(f"CPU, % ядра — {label(key)}")
axis.set_xlabel("Предложенная нагрузка, запросов/с")
axis.legend(fontsize=8, loc="upper left")
figure.tight_layout()
figure.savefig(out / f"{series}-cpu.png", dpi=150)
plt.close(figure)
save(figure, out / f"{series}-cpu.png")
def usl(points):
@@ -248,9 +270,7 @@ def scaling(rows, out):
axis.set_title("Пропускная способность от ресурсов API")
axis.set_xticks(cpus)
axis.legend(loc="upper left", fontsize=9)
figure.tight_layout()
figure.savefig(out / "scaling.png", dpi=150)
plt.close(figure)
save(figure, out / "scaling.png")
return fit
@@ -288,13 +308,12 @@ def utilisation(rows, out):
axis.set_ylim(0, 115)
axis.set_title("Загрузка звеньев в точке пика успешных RPS", pad=34)
axis.legend(fontsize=9, ncols=4, loc="lower left", bbox_to_anchor=(0, 1.0))
figure.tight_layout()
figure.savefig(out / "utilisation.png", dpi=150)
plt.close(figure)
save(figure, out / "utilisation.png")
def write(rows, out, fit):
with (out / "capacity.csv").open("w", newline="") as handle:
handle.write(f"# {STAMP}\n")
writer = csv.DictWriter(handle, fieldnames=SUMMARY, extrasaction="ignore")
writer.writeheader()
writer.writerows(rows)
@@ -305,7 +324,7 @@ def write(rows, out, fit):
lines_ = [head, "|" + " --- |" * 11]
for r in rows:
lines_.append(
f"| {r['series']} | {r['variant']} | {r['config']} | {r.get('capacity_rate', '—')} | "
f"| {r['series']} | {r['variant']} | {r['config']} | {r.get('capacity_label', '—')} | "
f"{r.get('capacity_p95_ms', '—')} | {r['peak_ok_rps']:.0f} "
f"[{r['peak_ok_min']:.0f}–{r['peak_ok_max']:.0f}] | {r['peak_rate']} | "
f"{r['cpu_api']:.0f} / {r['cpu_api_limit']} | {r['cpu_postgres']:.0f} | "
@@ -315,9 +334,10 @@ def write(rows, out, fit):
_, sigma, kappa, peak = fit
best = f"{peak:.1f}" if peak else "—"
lines_.append(
f"\nUSL по серии B: σ = {sigma:.4f}, κ = {kappa:.5f}, пик при N* = {best} CPU"
f"\nОписательная USL-аппроксимация серии B: σ = {sigma:.4f}, κ = {kappa:.5f}, "
f"модельный N* = {best} CPU. Это не измеренный оптимум и не доказательство причины."
)
(out / "capacity.md").write_text("\n".join(lines_) + "\n")
(out / "capacity.md").write_text(STAMP + "\n\n" + "\n".join(lines_) + "\n")
print("\n".join(lines_))