Skip to content

fix(wal): checkpoint 重写改为临时文件+原子 rename,堵住崩溃丢数据窗口#159

Merged
NeverENG merged 1 commit into
mainfrom
fix/wal-atomic-checkpoint
Jul 19, 2026
Merged

fix(wal): checkpoint 重写改为临时文件+原子 rename,堵住崩溃丢数据窗口#159
NeverENG merged 1 commit into
mainfrom
fix/wal-atomic-checkpoint

Conversation

@NeverENG

Copy link
Copy Markdown
Owner

问题

doRewrite#157 引入)就地 Truncate(0) → 重写 → Sync,三步非原子。若在 Truncate 之后、重写 fsync 之前进程崩溃,WAL 被清空/残缺——而 checkpoint 快照的 active+dirty 此刻尚无 SSTable 副本(正因如此才在快照里),会静默丢失已 ack 的写,违背"Append 返回即已 fsync 落盘"的契约。

修复

标准原子替换:records 写满并 fsyncwal.log.tmpos.Rename 覆盖(POSIX 原子)→ fsync 父目录让 rename 持久。恢复时只会看到完整旧 WAL 或完整新 WAL,绝不残缺。仍只由 flushLoop 执行,维持文件唯一写者不变式。

验证

TestWALRewriteAtomicReplace:陈旧 .tmp 残留不干扰恢复;重写后恰只剩新记录(含墓碑/空值边界)、可继续 Append、重开仍完整。全量 -race 通过。

🤖 Generated with Claude Code

doRewrite 此前就地 Truncate(0)→重写→Sync,三步非原子。若在 Truncate 之后、
重写 fsync 之前崩溃,WAL 被清空/残缺——而 checkpoint 快照的 active+dirty 此刻
尚无 SSTable 副本,会静默丢失已 ack 的写,违背"Append 返回即已落盘"契约。

改为标准原子替换:records 写满并 fsync 到 wal.log.tmp,os.Rename 覆盖(POSIX 原子),
再 fsync 父目录让 rename 持久。恢复时只会看到完整旧 WAL 或完整新 WAL,绝不残缺。
仍只由 flushLoop 执行,维持文件唯一写者不变式。抽出 encodeRecord 供 append/rewrite 复用。

新增 TestWALRewriteAtomicReplace:预置陈旧 .tmp 残留不干扰恢复;重写后恰只剩新记录
(含墓碑/空值边界)、可继续 Append、重开仍完整。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 19, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@NeverENG, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 20 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 479649b5-ba2c-4557-8043-71f4f7dbd40d

📥 Commits

Reviewing files that changed from the base of the PR and between 3d935df and 1e04a6c.

📒 Files selected for processing (2)
  • storage/wal.go
  • storage/wal_test.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/wal-atomic-checkpoint

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@NeverENG
NeverENG merged commit 9adfbd6 into main Jul 19, 2026
3 of 4 checks passed
@github-actions

Copy link
Copy Markdown

🐯 BanGD 数据库内核评审

⚠️ 本次未能生成有效的结构化评审(模型返回的内容无法解析)。不阻塞合入,可重试,或改用更强的模型。

模型原始输出片段(调试用)
{}

NeverENG added a commit that referenced this pull request Jul 19, 2026
记录本轮从「PUT 225 qps 反常」出发的完整排障链:实测归因 fsync 瓶颈→
group commit→写恢复测试挖出 SSTable 重载致命 bug(重启丢数据)→WAL 自清洁→
自审堵上原子重写窗口。关联 PR #150/#153/#157/#159/#158。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant