Repository navigation
[PR] feat: egress checkpoint ring — bounded replay, fast recovery, rollback (#7912) - #7922
ranxianglei wants to merge 7 commits into
Conversation
#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
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>
[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 分支( 发现 + 修复(6 commit)
A/G/H 同源:PR 分支从旧 #7910 head(rid schema 落地前)切出、rebase 到当前 #7910 head 时冲突解决错误,把 pre-#7910 的 log_seq 注释/SQL 带了回来。 测试质量
CI gap(需 owner 决策,已立 drive-by issue)本 PR 是 stacked(base = 小问题
结构化八问
判定:修复后 PR 代码可合并;待 #7910 合并后 CI 跑全量矩阵最终确认。CI gap(stacked PR 不触发 ci.yml)已立 drive-by issue 待 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
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.checkpointEveryOps(default 100) /checkpointKeep(default 10) /checkpointOnFold(default false). All 3 via env>config>default. Same tx writes both rings, auto-evict oldest.POST /__bili/admin/egress/rollbackbody {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.egressFork+ point-forkegressPointFork) INSERT...SELECT both checkpoint tables.storage.egress.{checkpointEveryOps, checkpointKeep, checkpointOnFold}+ envBILI_STORAGE_EGRESS_CHECKPOINT_EVERY_OPS/_KEEP/_ON_FOLD.Config surface
storage.egress.checkpointEveryOpsBILI_STORAGE_EGRESS_CHECKPOINT_EVERY_OPSstorage.egress.checkpointKeepBILI_STORAGE_EGRESS_CHECKPOINT_KEEPstorage.egress.checkpointOnFoldBILI_STORAGE_EGRESS_CHECKPOINT_ON_FOLDExisting mechanisms considered: the 3-level
compresshierarchy (not applicable — this is storage-layer, not compression), the providers table (not applicable), the existingstorage.*section (extended with newegresssub-section). No existing mechanism can express checkpoint ring parameters.Behavior changes
egress_checkpoint+egress_block_checkpointcreated on all egress-enabled databases (gated byegressTablesparam, default true). Legacy databases (gate off) do NOT create these tables.egressStateReadnow 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.egressFlowWalkBacknow integrates with the checkpoint floor (starts from the floor's op_seq instead of 0 when a checkpoint exists).migrateSchemanow has anegressTablesparam (default true). Production path passesegressPlaneGateOn() || 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
Closes #7912