第 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 /
developCheckpoint;源码mongodb-8.0的src/checkpoint/、src/block/block_ckpt*.c、src/meta/meta_ckpt.c。
一、定义与触发
- API:
WT_SESSION::checkpoint;或内部调用(recovery、backup、compaction、schema alter、内部 checkpoint 线程、启停等)。 - 在 snapshot isolation 事务 中执行,自始至终看到一致库视图。
- 典型配置
use_timestamp=true:把checkpoint_timestamp设为当前stable_timestamp,并写入该 checkpoint 的元数据;启动时据此设置内部 stable,并对更新做 rollback to stable(Checkpoint Overview)。
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(边界)
checkpoint_threads> 1:主线程走树,叶子 reconcile 可交给 worker;internal 页仍由主线程在排空 done 队列后处理;worker 各自 session,并导入主 checkpoint 快照副本(Guide Multi-threaded checkpoints)。- 跳过:自上次以来无修改、上次 checkpoint timestamp 已等于当前 stable、或文件尾无可用空间等(可强制覆盖)。
- 垃圾回收:处理 btree 时可把聚合 stop 已全局可见的页标删除,在 reconcile 时去掉;块是否可复用还取决于旧 checkpoint 是否仍引用(第 12–13 篇)。
五、与 Journal 的分工(一句)
Checkpoint 钉住「已知好点」;checkpoint 之间的修改由 logging / journal 覆盖。崩溃恢复 = 最近 checkpoint + 日志回放(第 10 篇)。MongoDB 的 journaling 与 checkpoint 间隔配置见第 14 篇。
六、工程间隙与开放问题
- Guide 与 MongoDB 默认 checkpoint 周期可能不同;排障看
mongod参数与 WT statistics。 - 本篇无本地强制 checkpoint / 崩溃注入实测。
- 开放问题: 大 HS + 高 dirty 时,checkpoint 前 eviction 等待是否成为尾延迟主因——第 15 篇给观测口径,需实测才下结论。
七、收束
- Checkpoint = 一致快照事务 + 用户表 reconcile + 随后 HS + flush + 元数据/turtle 切换。
- 开始前借 eviction 减压;generation 防止 eviction 超前。
- 下一篇:Journal / Logging——checkpoint 之间丢不丢、怎么回放。
参考资料
- WiredTiger Architecture Guide, Checkpoint:https://source.wiredtiger.com/develop/arch-checkpoint.html
- Eviction、History Store、Block Manager
mongodb-8.0:src/checkpoint/、src/meta/meta_ckpt.c- 第 4、6、8 篇;系列索引
上一篇:History
Store
下一篇:Journal
/ Logging
同主题继续阅读
把当前热点继续串成多页阅读,而不是停在单篇消费。
【WiredTiger 内核】文档库存储引擎全景:MongoDB 默认引擎的生态位
定位文档库默认引擎 WiredTiger 相对 PG/InnoDB/SQLite/RocksDB 的生态位;钉住 Session→Cache→Reconcile→HS→Checkpoint 主线、站内分工与 17 篇阅读路线,并以 Berenson 隔离词汇与 Durable History 为学术/工程锚点。
【WiredTiger 内核】Reconciliation:内存页到 on-disk image
拆解 WiredTiger reconciliation:把 in-memory 页转为 on-disk image、按 leaf_page_max 与 split_pct 分裂,并在用户表 reconcile 时选出最新已提交值、将更旧更新写入 History Store;锚定 wiki 与 src/reconcile/。
【WiredTiger 内核】Cache · Eviction · Reconciliation · History Store · Checkpoint
补齐文档库默认引擎内核层:从 Connection/Session、Cache/Eviction、B-Tree update chain、Reconciliation 到 Timestamps、History Store、Checkpoint/Journal 与 Rollback-to-Stable,并以 MongoDB 嵌入边界与 PG/InnoDB/RocksDB 对照收束。
【WiredTiger 内核】Eviction:脏页必须先 reconcile
拆解 WiredTiger Eviction 的 server/worker/队列、target/trigger 阈值,以及 dirty eviction 经 reconciliation 把最新值写入用户表、旧版本写入 History Store;说明应用线程被迫协助驱逐的条件。