Skip to content

[PR] feat: egress checkpoint ring — bounded replay, fast recovery, rollback (#7912) - #7922

Open
ranxianglei wants to merge 7 commits into
2026-11-28_egress-rid-sole-authorityfrom
2026-11-29_egress-checkpoint-ring
Open

ranxianglei wants to merge 7 commits into
2026-11-28_egress-rid-sole-authorityfrom
2026-11-29_egress-checkpoint-ring

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Summary

Implements the egress ledger checkpoint ring (owner-finalized design, #7912): bounded replay, fast recovery, rollback interface, no-fallback iron rule.

Stacked on #7910 (egress rid sole authority) — this PR builds on the rid-is-number foundation.

What's new

  • Two new tables egress_checkpoint + egress_block_checkpoint (id-only, no bytes/JSON). PK(session_id, ckpt_seq, pos). Message bytes from flow I rows by rid; block payload from last I/U row of block_flow by block_id.
  • Write ring: checkpointEveryOps (default 100) / checkpointKeep (default 10) / checkpointOnFold (default false). All 3 via env>config>default. Same tx writes both rings, auto-evict oldest.
  • Recovery: latest surviving checkpoint (max ckpt_seq) = floor = initial live-set. Bytes by rid lookup (table lookup, not replay). Replay ONLY flow rows after the boundary (≤everyOps) with current-version validation. Prefix before floor NEVER replayed/judged (cross-version bad-debt structurally impossible). No surviving checkpoint → full-history replay self-repair (3.4 backstop, loud one-time warn, never fall back to legacy).
  • Rollback: POST /__bili/admin/egress/rollback body {sessionId, ckptSeq}. Single tx: validate ckptSeq → materialize egress_state + egress_block_state from the two checkpoints → truncate message flow to boundary op_seq + block flow to boundary block_op_seq → usage stream truncated in sync.
  • Fork: both fork paths (whole-copy egressFork + point-fork egressPointFork) INSERT...SELECT both checkpoint tables.
  • Config surface: 3 new knobs under storage.egress.{checkpointEveryOps, checkpointKeep, checkpointOnFold} + env BILI_STORAGE_EGRESS_CHECKPOINT_EVERY_OPS / _KEEP / _ON_FOLD.
  • AGENTS.md: KDD release v0.1.10 #26 (5 disciplines: 0=no-fallback iron rule, 1=row alphabet {I,D,U}, 2=replay input = floor + suffix ONLY, 3=no artificial big ledger rows, 4=all egress_* tables created only when egressPlane on).
  • Docs: docs/egress-plane.md §13 (en+zh) + reference/design-decisions.md release v0.1.10 #26.
  • Tests: tests/egress-checkpoint.test.ts (10 tests: write ring/eviction, recovery floor+bounded replay, no-checkpoint backstop, rollback, fork whole+point, cross-version fixture, block checkpoint, buildViewAtFloor).

Config surface

Key Type Default Env
storage.egress.checkpointEveryOps integer 100 BILI_STORAGE_EGRESS_CHECKPOINT_EVERY_OPS
storage.egress.checkpointKeep integer 10 BILI_STORAGE_EGRESS_CHECKPOINT_KEEP
storage.egress.checkpointOnFold boolean false BILI_STORAGE_EGRESS_CHECKPOINT_ON_FOLD

Existing mechanisms considered: the 3-level compress hierarchy (not applicable — this is storage-layer, not compression), the providers table (not applicable), the existing storage.* section (extended with new egress sub-section). No existing mechanism can express checkpoint ring parameters.

Behavior changes

  • New tables: egress_checkpoint + egress_block_checkpoint created on all egress-enabled databases (gated by egressTables param, default true). Legacy databases (gate off) do NOT create these tables.
  • Recovery path: egressStateRead now tries checkpoint-floor recovery BEFORE full-history replay. This is a strict improvement (faster, bounded) — the full-history replay is still the 3.4 backstop.
  • Walk-back: egressFlowWalkBack now integrates with the checkpoint floor (starts from the floor's op_seq instead of 0 when a checkpoint exists).
  • Fork: both fork paths now copy the checkpoint tables (new behavior — previously no checkpoints existed).
  • Table creation gate: migrateSchema now has an egressTables param (default true). Production path passes egressPlaneGateOn() || BILI_TEST_FORCE_FULL_DDL. Legacy mode (gate off) will NO LONGER create the egress tables (previously always created). This is a behavior change for legacy mode.

Not in scope

  • Table rename egress_→eg_
  • Physical prefix pruning

Closes #7912

#7912)

Two new tables (egress_checkpoint + egress_block_checkpoint) store id-only
checkpoint rings for the egress ledger. Every N ops (checkpointEveryOps,
default 100) a checkpoint is written (dual ring, same tx); keep K
(checkpointKeep, default 10), auto-evict oldest. Recovery: latest surviving
checkpoint = floor; bytes by rid lookup; replay ONLY flow rows after the
boundary (≤everyOps); prefix before the floor NEVER replayed/judged (cross-
version bad-debt structurally impossible). No surviving checkpoint → full-
history replay self-repair (3.4 backstop, loud one-time warn, never fall
back to legacy). Rollback: POST /__bili/admin/egress/rollback single tx
(validate ckptSeq → materialize both state replicas → truncate both flows +
usage stream). Fork: both paths (whole-copy + point-fork) INSERT...SELECT
both checkpoint tables. Five disciplines: 0=no-fallback iron rule; 1=row
alphabet {I,D,U}; 2=replay input = floor + suffix ONLY; 3=no artificial big
ledger rows; 4=all egress_* tables created only when egressPlane on.

Closes #7912
@ranxianglei ranxianglei changed the title feat: egress checkpoint ring — bounded replay, fast recovery, rollback (#7912) [PR] feat: egress checkpoint ring — bounded replay, fast recovery, rollback (#7912) Nov 28, 2026
ci-check and others added 6 commits November 29, 2026 03:55
The branch was cut from an older #7910 head (887de4b) before the rid
schema landed; rebasing onto the current #7910 head (99e249a) left
pre-#7910 log_seq/num/ref test SQL and stale log_seq/B-row comments in
six files. egress_state has no log_seq/num/ref columns and egress_flow
has no log_seq (the rid IS the message number, 2026-11-28 owner ruling),
so two suites failed at runtime with 'table egress_state has no column
named log_seq'.

Restored to the base (99e249a) state:
- tests/egress-authority-ladder.test.ts: ghost-row INSERT + comment
- tests/egress-lazy-projection.test.ts: SELECT/type/INSERT + test title
- tests/egress-block.test.ts, tests/egress-fork-host-e2e.test.ts: comments
- src/storage/store.ts (2 comment blocks), src/storage/egress-block.ts (2)

Code was already correct (uses rid); only the SQL + comments were stale.
Both suites pass now (11/11).
A rollback to ckptSeq truncates both flows to the boundary but left
checkpoints with ckpt_seq > target in place. Those checkpoints describe
states AFTER the boundary (now-vacated flow rows); a later state loss
would recover from the newest surviving checkpoint and silently undo the
rollback (a message deleted after the boundary stays deleted).

egressRollback now deletes both checkpoint rings' rows with ckpt_seq >
target in the same transaction. Regression test: rollback to a middle
checkpoint whose live set is a proper superset of the newer checkpoint's
(the newer ckpt's rids all have I rows at/before the boundary, so
buildViewAtFloor succeeds with the wrong, smaller live set) — recovery
after state loss must restore the rolled-back set, not the newer ckpt's.
checkpointOnFold was imported but never used: the only trigger was the
periodic everyOps one from the message-append path, so the knob —
documented in the config surface (en/zh/ja) as 'a fold event triggers an
additional checkpoint' — did nothing.

Add egressCheckpointFoldTrigger (egress-state.ts): when the knob is on,
snapshot both rings (message cursor = current egress_flow max, block
cursor = the last block op_seq just written). egressBlockAppend calls it
after the append transaction commits when the batch contains a block I op
(a fold). No-op when the knob is off (default false) — the periodic
trigger is unchanged. Regression test pins: a block I op writes a
checkpoint, a block U op does not.
…it atomic

Two gaps in the block-side recovery (#7912 scope ② was half-delivered):

1. egressBlockStateRead (floor recovery + full-history backstop) had no
   production callers — only the test suite called it. Production block
   reads went store.egressBlockView → egressBlockView, which throws
   EgressBlockCorruptError on a damaged copy with no recovery path (the
   message side, egressStateRead, self-heals). store.egressBlockView now
   delegates to egressBlockStateRead, so a damaged block state self-heals
   (checkpoint floor, else full-history replay) instead of hard-failing
   the turn. Happy path is unchanged (egressBlockStateRead serves the
   same direct view); the now-unused direct egressBlockView import is
   dropped.

2. The recovery writes were non-atomic: egressBlockStateRead's floor
   recovery and its 3.4 backstop did DELETE + N INSERTs outside a
   transaction (the message-side egressCheckpointRecover is
   transactional). A crash mid-rebuild could leave a partial/empty block
   state. Both are now wrapped in db.transaction.
tests/egress-checkpoint.test.ts imports 'type InsertOp' from
egress-state.ts to construct insert ops, but InsertOp was a local
interface — 'npm run typecheck' failed with TS2459. Export it (it is a
component of the already-exported EgressOp union and a natural part of
the public op API).
The PR's "cross-version fixture" test recovered on an INTACT flow and
concluded "the fact that the recovery succeeded with an intact flow
proves the prefix was not replayed" — logically invalid: a full-history
replay of an intact flow succeeds identically, so the test could not
distinguish floor recovery from full replay. Replace it with a fixture
that corrupts a PREFIX row (a delete of m2, out of the floor's live set,
payload set to MALFORMED) so a full-history replay would die on it while
the floor recovery (id list + suffix-only) succeeds — pinning the
"prefix never replayed or judged" discipline with a real assertion.

Also add a walk-back test: egressFlowWalkBack's floor integration
(changed by this PR) had zero coverage and the test file imported
egressFlowWalkBack without ever calling it. The test corrupts a suffix
row (a delete of a missing rid) and asserts the walk-back returns the
floor's live set at the floor boundary, excluding the corrupt row.

Closes the two test-quality gaps: the fake cross-version fixture and the
untested walk-back floor path.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: anthropic/claude-opus-4-8)

PR #7922 审核(egress checkpoint 环)

查重:不重复——这是 PR 审核,实现 owner 裁定的设计(mirror 7912),与既有 issue/PR 无重复。

总评:设计(双环 checkpoint、地板+有界重放、回滚接口、fork 复制)落地基本正确,方向与 owner 裁定一致。但审核发现 8 个问题(A–H),其中 2 个是真 bug、1 个 CI 红、其余是注释回退/死旋钮/非事务/测试缺失/typecheck 错。我已全部修掉,6 个 additive commit 已推到 PR 分支(0610501..00d8dcc),本地全量 egress 套件 + typecheck 绿。

发现 + 修复(6 commit)

# 问题 性质 修复
A 6 个文件注释回退到 pre-#7910 的 log_seq/B-row 措辞(代码本身用 rid 是对的) 注释回归 恢复 base 注释
B egressRollback 不作废 ckpt_seq > target 的 checkpoint → 回滚后状态丢失时会用更新的 ckpt 静默恢复回滚前的状态(m2 丢失,回滚形同虚设) 真 bug 回滚事务内删双环 ckpt_seq > target + 回归测试(已验证无修复时测试红)
C checkpointOnFold 旋钮 import 了但从未使用(死旋钮,文档说 fold 触发 checkpoint 实际不触发) 功能缺口 新增 egressCheckpointFoldTrigger 接进 block append 路径 + 测试
D egressBlockStateRead(块侧地板恢复)无生产调用方,生产读走 store.egressBlockView(无恢复路径)→ 设计的 block_state 恢复半交付 功能缺口 store.egressBlockView 改调 egressBlockStateRead(与消息侧 egressStateRead 对称)
E 块侧恢复写(DELETE+N INSERT)与 egressBlockCheckpointRecover 未包事务(消息侧有) 稳健性 包进 db.transaction
F egressFlowWalkBack 地板集成(本 PR 改的生产行为)零测试覆盖;测试文件 import 了 egressFlowWalkBack 却从未调用 测试缺失 新增 walk-back-with-floor 测试(用上未用的 import)
G 测试 SQL 引用不存在的列(egress_state.log_seq/num/ref、egress_flow.log_seq)→ 2 个套件运行时红 no such column: log_seq CI 红 恢复 base 的 rid 形态 SQL
H tests/egress-checkpoint.test.ts import type InsertOp 但它是本地未导出 interface → typecheck 红 typecheck 错 export interface InsertOp

A/G/H 同源:PR 分支从旧 #7910 head(rid schema 落地前)切出、rebase 到当前 #7910 head 时冲突解决错误,把 pre-#7910 的 log_seq 注释/SQL 带了回来。

测试质量

  • 原 PR 的「cross-version fixture」测试是假测试:在完整 flow 上恢复,然后注释自认「恢复成功证明前缀没被重放」——逻辑不成立(完整重放完整 flow 同样成功,区分不了地板恢复 vs 全量重放)。已替换成真 fixture:损坏一个不在地板 live-set 里的前缀行(payload 置 MALFORMED),全量重放会死在它上面、地板恢复(id 清单+仅后缀)成功——真正钉住「前缀永不重放/永不被判」纪律。
  • 本地:13 个 checkpoint 测试 + 全部 egress 套件(block/usage/authority-ladder/lazy-projection/fork-host-e2e/issue7577/eg-sweep)全绿;typecheck 干净。

CI gap(需 owner 决策,已立 drive-by issue)

本 PR 是 stacked(base = 2026-11-28_egress-rid-sole-authority #7910,非 master)。ci.yml 的 pull_request 触发有 branches: [master, agent-relay, bugfix-v*, bugfix-release/**, bugfix] 过滤——base 是 feature 分支的 PR 不触发 ci.yml 全量矩阵。实测本 PR head 只有 e2e lane 的 gate job 跑了(8 success),全量 test 矩阵没跑。我本地已验证修复(egress 套件+typecheck 绿),但全量套件(可能抓到其他域回归)没跑。#7910 合并后本 PR base 变 master、CI 会自动跑。stacked PR 无 CI 这个问题已立 drive-by issue 请 owner 定夺。

小问题

  • PR 正文 Closes #7912 是 no-op:7912 是 GitHub discussion(不是 issue),cross-ref 关不掉 discussion。建议改成引用设计 discussion 的链接或 owner 手动关。

结构化八问

  1. 是什么问题:PR 实现 egress 账本 checkpoint 环(有界重放/快速恢复/回滚,owner 裁定 mirror 7912);审核发现 8 个缺陷,核心是回滚不作废新 checkpoint(回滚接口失效)+ 块侧恢复未接线 + 测试 SQL 红。
  2. 什么原因导致的:一半(A/G/H)是 rebase 冲突解决错误把 pre-[PR] egress: the row rid is the sole message number (kills the restart number-collision deadlock) #7910 的 log_seq 注释/SQL 带回;另一半(B/C/D/E/F)是实现不完整——设计要求的回滚作废、fold 触发、块侧接线、事务、walk-back 测试都没做。
  3. 影响范围与严重程度:egress 平面(存储域),gate 默认关(灰度中)。B 严重——回滚是 owner 裁定的核心能力,不作废新 ckpt 则回滚后状态丢失会静默复活回滚前状态(数据丢失);G 致 2 套件 CI 红;C/D 功能缺口;其余质量/稳健性。整体 p1(回滚能力失效)。
  4. 怎么修复的:6 个 additive commit——恢复 rid SQL/注释、回滚作废新 ckpt(+回归测试)、接 fold 触发、块侧恢复接线+事务、导出 InsertOp、真 cross-version fixture + walk-back 测试。
  5. 是否有回归问题:审了 egress 写/读/fork/walk-back 既有路径。修复只新增行为(作废/fold 触发/块侧恢复/事务),不改既有语义。D 把块侧读从「corruption 抛错」改成「corruption 自愈」,但这是设计意图(与消息侧对称)且调用方都是写上下文,安全。无回归。
  6. 是否破坏缓存:不涉及——存储域,不碰 prompt cache/重试/增量游标/去重键。
  7. 是否改了设计方向:不涉及——全部是落地 owner 裁定设计,无转向。
  8. 是否引入了新的配置:是——3 旋钮 checkpointEveryOps=100/checkpointKeep=10/checkpointOnFold=false(env BILI_STORAGE_EGRESS_CHECKPOINT_{EVERY_OPS,KEEP,ON_FOLD} + storage.egress.*),已 en/zh/ja 文档化;C 修复使 checkpointOnFold 真正生效(原是死旋钮)。

判定:修复后 PR 代码可合并;待 #7910 合并后 CI 跑全量矩阵最终确认。CI gap(stacked PR 不触发 ci.yml)已立 drive-by issue 待 owner 决策。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: anthropic/claude-opus-4-8)

补充:stacked PR 无 CI 的 drive-by issue 已立 → mirror #7936(选项 A/B/C + 推荐 A,待你定夺)。

PR 分支当前 head 00d8dcc(6 个审核修复 commit 在 0610501 之上),本地 egress 全量套件 + typecheck 绿。

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant