土法炼钢兴趣小组的算法知识备份

【WiredTiger 内核】Checkpoint:跨文件一致快照

文章导航

分类入口
databasestorage
标签入口
#wiredtiger#checkpoint#durability#history-store#eviction#metadata#mongodb

目录

第 7–8 篇 说明时间戳与 History Store;真正把「某一应用时间上的一致视图」钉到磁盘上的,是 Checkpoint。Architecture Guide Checkpoint:checkpoint 是崩溃或异常关机后可以恢复的已知时间点;checkpoint 与 logging 共同提供耐久性——恢复时从最近 checkpoint 起步,再回放日志。

本文钉住算法阶段、用户表与 HS 的先后顺序、checkpoint generation,以及与 eviction 的关系。Journal 细节见第 10 篇;RTS 见第 11 篇。

本文是「WiredTiger 内核」系列第 9 篇(共 17 篇)。→ 系列目录

先修:第 4、6–8 篇。续读:第 10–11 篇。

版本锚定:Architecture Guide Version 12.0.0 / develop Checkpoint;源码 mongodb-8.0src/checkpoint/src/block/block_ckpt*.csrc/meta/meta_ckpt.c


一、定义与触发

Guide 注明:截至该文档版本,checkpoint_timestamp 不用作 checkpoint 事务的 read_timestamp


二、算法阶段

flowchart TD
  lock["Locking"] --> evict["Eviction reduce dirty"]
  evict --> prep["Prepare: txn + handle list"]
  prep --> data["Data files reconcile"]
  data --> hs["History Store checkpoint"]
  hs --> flush["Flush to disk"]
  flush --> meta["Metadata + turtle"]

2.1 Locking

取得 checkpoint_lock(最高优先级,保证同时只有一个 checkpoint)以及后续过程中的 dhandle / schema / flush 等锁。

2.2 Eviction(减压)

在 checkpoint 事务开始(pin 内容)之前,先借 eviction 把 dirty 压到目标比例,利用多线程 eviction,缩短临界区。Checkpoint 线程会等待,除非看不到进展(第 4 篇)。

2.3 Prepare

开始 checkpoint 事务、更新全局 checkpoint 状态、收集待 checkpoint 的 handle 列表。全局 schema 锁避免建删表;全局事务锁保护全局事务状态,并保证 stable_timestamp 不越过 checkpoint 快照。

遍历 connection dhandle:每个 handle 对应一棵 btree;干净、无修改的 btree 可排除(特定强制配置例外)。纳入列表的 btree 在可能时删除更早、块已不再使用的旧 checkpoint。

2.4 Data files(用户表)

对列表中每棵树:遍历,reconcile 所有脏页;干净页跳过。Eviction 已提前写干净的页仍然跳过,不论该写出更新对 checkpoint 事务是否可见。Checkpoint 保证:树中每个页都存在可写盘的 clean 版本。块管理器维护 extent list 并处理旧 checkpoint 删除(第 12 篇)。

2.5 History Store(故意在后)

The history store is checkpointed after the data files intentionally as during the reconciliation of the data files additional writes may be created in the history store and its important to include them in the checkpoint.

用户表 reconcile 会向 HS 追加写(第 6、8 篇);若先 checkpoint HS,会漏掉这批写入。

2.6 Flush → Metadata

所有已 checkpoint 的 btree 与 HS flush 落盘并等待完成。每个数据文件(含 HS)在元数据中新建条目;元数据文件最后 checkpoint。WT 通常维护两个 checkpoint;最新位置写入 turtle 文件。


三、Checkpoint generation 与 eviction

Checkpoint 开始时递增 generation;处理完某 btree 后将其 checkpoint_gen 设为最新。若某 btree 的 generation 落后于当前 checkpoint generation,可见性检查会纳入 checkpoint 事务 ID / timestamp,阻止 eviction 把该树的更新赶到 checkpoint 前面

这是「并行 eviction 与正在进行的 checkpoint」之间的正确性阀门。


四、并行 checkpoint、跳过与 GC(边界)


五、与 Journal 的分工(一句)

Checkpoint 钉住「已知好点」;checkpoint 之间的修改由 logging / journal 覆盖。崩溃恢复 = 最近 checkpoint + 日志回放(第 10 篇)。MongoDB 的 journaling 与 checkpoint 间隔配置见第 14 篇。


六、工程间隙与开放问题


七、收束

  1. Checkpoint = 一致快照事务 + 用户表 reconcile + 随后 HS + flush + 元数据/turtle 切换。
  2. 开始前借 eviction 减压;generation 防止 eviction 超前。
  3. 下一篇:Journal / Logging——checkpoint 之间丢不丢、怎么回放。

参考资料


上一篇History Store
下一篇Journal / Logging

同主题继续阅读

把当前热点继续串成多页阅读,而不是停在单篇消费。

2026-07-22 · database / storage

【WiredTiger 内核】Reconciliation:内存页到 on-disk image

拆解 WiredTiger reconciliation:把 in-memory 页转为 on-disk image、按 leaf_page_max 与 split_pct 分裂,并在用户表 reconcile 时选出最新已提交值、将更旧更新写入 History Store;锚定 wiki 与 src/reconcile/。

2026-07-22 · database / storage

【WiredTiger 内核】Eviction:脏页必须先 reconcile

拆解 WiredTiger Eviction 的 server/worker/队列、target/trigger 阈值,以及 dirty eviction 经 reconciliation 把最新值写入用户表、旧版本写入 History Store;说明应用线程被迫协助驱逐的条件。


By .