mthoutai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一、使用方式概览

  • 选择与你最接近的画像(A/B/C)。
  • 复制对应的 flink-conf.yaml 片段 与调优项到你的环境。
  • 参考容量估算确认存储与网络是否足够。
  • 上线后,用巡检脚本观察健康度,并接入告警阈值
  • 把你的拓扑/并发/状态量/SLA发我,我基于真实画像生成精调参数表容量算计

二、画像 A:低延迟实时(端到端 P99 ≤ 200ms)

适用:风控、告警、在线特征、强实时链路
目标:尽量少重放、频繁 CK、反压下仍快速完成 CK

# 语义与周期(低延迟优先)
execution.checkpointing.mode: AT_LEAST_ONCE
execution.checkpointing.interval: 200ms
execution.checkpointing.max-concurrent-checkpoints: 2
execution.checkpointing.min-pause: 0ms
execution.checkpointing.timeout: 30s
execution.checkpointing.tolerable-failed-checkpoints: 3
# 存储
execution.checkpointing.storage: filesystem
execution.checkpointing.dir: hdfs:///flink/ckpt/<job>
  execution.checkpointing.num-retained: 2
  # 非对齐(反压友好)
  execution.checkpointing.unaligned.enabled: true
  execution.checkpointing.aligned-checkpoint-timeout: 1s
  # 任务完成后继续 CK
  execution.checkpointing.checkpoints-after-tasks-finish: true
  # 1.20 文件合并(缓解小文件洪泛)
  execution.checkpointing.file-merging.enabled: true
  execution.checkpointing.file-merging.max-file-size: 128mb

Exactly-once 必须时:把 mode 调为 EXACTLY_ONCE,同时将 execution.checkpointing.max-concurrent-checkpoints: 1

三、 画像 B:均衡吞吐(推荐大多数线上作业)

适用:日志清洗、指标汇总、CDC 同步
目标:稳定与成本平衡,默认推荐

execution.checkpointing.mode: EXACTLY_ONCE
execution.checkpointing.interval: 10s
execution.checkpointing.max-concurrent-checkpoints: 1
execution.checkpointing.min-pause: 2s
execution.checkpointing.timeout: 2m
execution.checkpointing.tolerable-failed-checkpoints: 2
execution.checkpointing.num-retained: 3
execution.checkpointing.storage: filesystem
execution.checkpointing.dir: hdfs:///flink/ckpt/<namespace>/<job>
  execution.checkpointing.unaligned.enabled: true
  execution.checkpointing.aligned-checkpoint-timeout: 5s
  # 恢复更快
  execution.checkpointing.local-backup.enabled: true
  execution.checkpointing.local-backup.dirs: /data/flink/localState
  # 1.20 文件合并
  execution.checkpointing.file-merging.enabled: true
  execution.checkpointing.file-merging.max-file-size: 256mb
  execution.checkpointing.file-merging.max-space-amplification: 2.0
  execution.checkpointing.file-merging.across-checkpoint-boundary: true

四、画像 C:大状态吞吐(状态 100GB+)

适用:大窗口聚合、复杂 CEP、亿级 keyed state
目标:可恢复性优先、控制 NN/S3 元数据与存储压力

execution.checkpointing.mode: EXACTLY_ONCE
execution.checkpointing.interval: 30s
execution.checkpointing.max-concurrent-checkpoints: 1
execution.checkpointing.min-pause: 5s
execution.checkpointing.timeout: 5m
execution.checkpointing.tolerable-failed-checkpoints: 1
execution.checkpointing.num-retained: 2
# 强烈建议
execution.checkpointing.incremental: true
execution.checkpointing.local-backup.enabled: true
execution.checkpointing.local-backup.dirs: /nvme/localState
execution.checkpointing.storage: filesystem
execution.checkpointing.dir: hdfs:///flink/ckpt/<bigstate>/<job>
  # 非对齐:确认存在反压再启用;否则可关闭换更小状态
  execution.checkpointing.unaligned.enabled: true
  execution.checkpointing.aligned-checkpoint-timeout: 10s
  # 文件合并
  execution.checkpointing.file-merging.enabled: true
  execution.checkpointing.file-merging.max-file-size: 512mb
  execution.checkpointing.file-merging.across-checkpoint-boundary: true
  execution.checkpointing.file-merging.pool-blocking: true

五、容量/带宽快速估算(拿来就算)

ΔS = 单次增量 CK 大小(GB),T = CK 间隔(秒)

  • 所需写入带宽(不含副本/EC)
    BW ≈ ΔS / T(GB/s)

  • HDFS 三副本:实际带宽 ≈ 3 × BW

  • NameNode/S3 元数据压力 ≈ 单次 CK 生成的文件数 × 并发作业数

    • 开启文件合并(Flink 1.20+)可把万级小文件降至百级

经验值

  • 小状态(< 5GB):interval≈10stimeout≈1–2min
  • 中状态(5–50GB):interval≈10–30s,启用增量 + 本地备份
  • 大状态(> 50GB):interval≥30s,强烈建议增量 + 本地备份 + 文件合并

六、运维巡检脚本(bash,可直接用)

#!/usr/bin/env bash
set -euo pipefail
CKPT_DIR="hdfs:///flink/ckpt/<job>"
  APP_ID="<your_flink_app_id>"
    JM_HOST="<jm-host>"
      echo "[1/4] HDFS 目录可达/可写"
      hdfs dfs -test -d "${CKPT_DIR}" || hdfs dfs -mkdir -p "${CKPT_DIR}"
      TEST="${CKPT_DIR}/._ckpt_write_test_$(date +%s)"
      hdfs dfs -touchz "${TEST}" && hdfs dfs -rm -f "${TEST}" && echo "OK"
      echo "[2/4] 最近1小时小文件数量"
      hdfs dfs -ls -R "${CKPT_DIR}" | \
      awk -v ts=$(date -d "-1 hour" +%s) '{cmd="date -d \""$6" "$7"\" +%s"; cmd | getline t; close(cmd); if(t>=ts) print}' | wc -l
      echo "[3/4] Flink 最新 checkpoint 概览"
      curl -s "http://${JM_HOST}:8081/jobs/${APP_ID}/checkpoints" | jq '.'
      echo "[4/4] 失败/超时热点(Top5)"
      curl -s "http://${JM_HOST}:8081/jobs/${APP_ID}/checkpoints/details" | \
      jq '[.history[] | select(.status=="FAILED")][0:5]'

替换 <jm-host><job><your_flink_app_id>

七、Grafana 告警阈值(Prometheus 指标)

  • Checkpoint 逼近超时
    flink_checkpoint_in_progress > 0 AND duration_p95 > timeout * 0.7 持续 5 分钟
  • 反压
    flink_taskmanager_job_task_backPressuredTimeMsPerSecond > 100 持续 5 分钟
  • 存储侧异常
    flink_jobmanager_num_failed_checkpoints 环比上升
    last_failed_message 包含 I/O 或 timeout
  • NameNode 小文件洪泛
    FsNameSystemState.FilesTotal 日增量显著异常
  • S3 异常
    S3 4xx/5xx 错误率 > 1% 且持续 10 分钟

八、常见问题与落地建议

  1. CK 超时:存储吞吐不足或反压 → 提高 timeout、启用非对齐、检查网络/限流。
  2. 目录权限/可见性:JM/TM 路径不一致或权限不足 → 统一 dir 并校验 Kerberos/HDFS Token。
  3. UnionListState 不一致:部分子任务完成导致 CK 不成功 → 使用该状态的子任务要么都完成要么都未完成
  4. 小文件洪泛:启用文件合并(1.20+)、调大 CK 间隔、调整 sink 滚动策略。
  5. 端到端 Exactly-once 失效:Source/Sink 未开启事务或有副作用 → 使用事务性 sink / 重构副作用。
posted on 2025-11-06 15:24  mthoutai  阅读(21)  评论(0)    收藏  举报