如何监控 TiFlash 的“Apply Region”延迟?

解读

“Apply Region”延迟是 TiFlash 把 Raft 日志真正回放到本地列存(DeltaTree)的耗时,它直接决定 HTAP 场景下“实时”这一卖点能否兑现。国内金融、电商订单、物流分拨等场景对“写入即可见”极度敏感,面试时如果只答“看 Grafana”会被认为没下过机房;必须给出可落地的白屏+黑屏方案,并能把指标拆解到“Region 级”“TiFlash 实例级”“表级”三个维度,才能体现 DBA 或 SRE 的实战厚度。

知识点

  1. TiFlash 内部线程模型:
    • Apply Thread(单线程/Region)负责把 committed raft log 转成列存变更;
    • 耗时大头在 write stall、DeltaTree flush、PageStorage GC、raft index 推进慢。
  2. 关键指标:
    • tikv_raftstore_apply_wait_duration_seconds(TiKV 侧,但 TiFlash 复用同一套 raftstore)
    • tiflash_raft_apply_write_command_duration_seconds{type="apply"}
    • tiflash_raft_entry_cache_miss 高 → 触发 read_index → 放大延迟
    • tiflash_storage_write_stall_duration_seconds
  3. 白屏:Grafana → TiFlash-Details → Raft → Apply duration p99;
    黑屏:curl http://{tiflash_ip}:20292/metrics | grep apply;
    日志:grep “Apply duration” tiflash.log 可打印单 Region 耗时。
  4. 国内合规要求:
    • 金融生产网禁止拉取公网镜像,需提前在内网 Harbor 同步 grafana.json;
    • 信创环境(鲲鹏+麒麟)编译的 TiFlash 指标名一样,但采样周期可能 5 s,需调整 Prometheus rule。
  5. 阈值经验:
    • 表级 p99 > 500 ms 即告警;
    • 单 Region 连续 3 次 > 1 s 需手动 split;
    • 若 write stall 占比 > 30 %,优先调 storage.delta_cache_limit 到 2 GB。

答案

线上监控分四层落地:

  1. 实例级:Prometheus 采集 tiflash_raft_apply_write_command_duration_seconds,Grafana 配置 p99 告警规则,阈值 500 ms,连续 5 min 触发钉钉+语音;
  2. Region 级:通过 tiup ctl:v<cluster_version> tiflash proxy --host:20170 发送 debug/region/{region_id}/apply_perf,拿到最近一次 apply 耗时,封装成 Python 脚本每 30 s 扫 top10 慢 Region,自动输出到夜莺;
  3. 表级:information_schema.tiflash_replica + system.tiflash_tables 关联,计算 max(apply_lag),若 > 2 s 直接联动业务灰度杀掉长事务;
  4. 黑屏兜底:在 TiFlash 日志开启 “raft.apply” 模块 debug 级,grep “Apply duration” | awk 计算均值,应急时 1 min 内可判断是否做滚动重启。
    同时把 write stall、entry cache miss、apply thread CPU 三项作为辅助指标,任一突增即视为同根因,避免误报。

拓展思考

  1. 如果业务侧对“实时”要求 < 200 ms,可把 TiFlash 部署在 NVMe + io_uring 机型,并将 storage.delta_cache_limit 调到 4 GB,同时把 raftstore.apply-pool-size 提到 4,监控指标需同步下调阈值到 200 ms。
  2. 在跨双活机房场景,TiFlash 副本数=2,任一机房网络抖动都会放大 apply lag,此时可引入“apply lag 热图”:用 Prometheus recording rule 按 region_id 做 histogram,Grafana 用 Heatmap 面板一眼定位慢 Region,方便快速切流。
  3. 未来升级到 TiFlash 7.x 的 Fast Add Column 特性后,DDL 期间会产生瞬时大事务,可提前把 apply duration 告警静默窗口与 DDL 窗口绑定,避免误告警。