Checkpointer 层详解
Checkpointer 在超步边界(或配置点)把 channel 状态 + 元数据 写成 checkpoint,用 thread_id 区分会话,用 checkpoint_id 链式版本。
1. 解决什么问题
| 需求 | Checkpointer |
|---|---|
| 多轮对话记忆 | 同一 thread 恢复最后 checkpoint |
| 崩溃恢复 | 从最近快照继续 |
| 时间旅行 / 分支 | 保留 checkpoint 链 |
| HITL | 暂停在 checkpoint,人工改 state 再 resume |
2. 核心概念
| 概念 | 含义 |
|---|---|
thread_id | 会话/线程标识 |
checkpoint | 一次快照(channel 值、下一步待执行信息等) |
checkpoint_id | 快照 ID,可 parent 形成链 |
serde | 序列化(JSON、msgpack 等) |
3. BaseCheckpointSaver
实现需提供(语义):
put/get/listcheckpoint- 与 graph 配置绑定(
checkpointer=on compile) - 可选 Store(长期 KV,与 checkpoint 互补)
常见实现:MemorySaver(开发)、Postgres / SQLite(生产)。
4. 与 Pregel 的协作
每个 superstep 结束后,若配置持久化,引擎调用 saver 写入。invoke / stream 结束时也可落盘。
恢复:getState / invoke 带 checkpoint_id 或 thread 最新点。
5. 序列化注意
- State 需 可 serde;含函数、不可序列化对象会失败。
- 大 payload 影响 DB 与延迟;可存引用 id 而非全文。
6. 与「事件溯源」对比
采用 checkpoint 快照 而非无限事件日志:实现与恢复路径更简单;代价是存储步级全量(可压缩/裁剪策略另论)。见 08。
7. 总结
Checkpointer = 可恢复的图状态数据库;生产选型 Postgres 类实现,开发用 Memory。