INSPECTION_RESULT

TiDB 内置了一些诊断规则,用于检测系统中的故障以及隐患。

该诊断功能可以帮助用户快速发现问题,减少用户的重复性手动工作。可使用 select * from information_schema.inspection_result 语句来触发内部诊断。

诊断结果表 information_schema.inspection_result 的表结构如下:

  1. USE information_schema;
  2. DESC inspection_result;
  1. +----------------+--------------+------+------+---------+-------+
  2. | Field | Type | Null | Key | Default | Extra |
  3. +----------------+--------------+------+------+---------+-------+
  4. | RULE | varchar(64) | YES | | NULL | |
  5. | ITEM | varchar(64) | YES | | NULL | |
  6. | TYPE | varchar(64) | YES | | NULL | |
  7. | INSTANCE | varchar(64) | YES | | NULL | |
  8. | STATUS_ADDRESS | varchar(64) | YES | | NULL | |
  9. | VALUE | varchar(64) | YES | | NULL | |
  10. | REFERENCE | varchar(64) | YES | | NULL | |
  11. | SEVERITY | varchar(64) | YES | | NULL | |
  12. | DETAILS | varchar(256) | YES | | NULL | |
  13. +----------------+--------------+------+------+---------+-------+
  14. 9 rows in set (0.00 sec)

字段解释:

  • RULE:诊断规则名称,目前实现了以下规则:
    • config:配置一致性以及合理性检测。如果同一个配置在不同实例不一致,会生成 warning 诊断结果。
    • version:版本一致性检测。如果同一类型的实例版本不同,会生成 critical 诊断结果。
    • node-load:服务器负载检测。如果当前系统负载太高,会生成对应的 warning 诊断结果。
    • critical-error:系统各个模块定义了严重的错误,如果某一个严重错误在对应时间段内超过阈值,会生成 warning 诊断结果。
    • threshold-check:诊断系统会对一些关键指标进行阈值判断,如果超过阈值会生成对应的诊断信息。
  • ITEM:每一个规则会对不同的项进行诊断,该字段表示对应规则下面的具体诊断项。
  • TYPE:诊断的实例类型,可取值为 tidbpdtikv
  • INSTANCE:诊断的具体实例地址。
  • STATUS_ADDRESS:实例的 HTTP API 服务地址。
  • VALUE:针对这个诊断项得到的值。
  • REFERENCE:针对这个诊断项的参考值(阈值)。如果 VALUE 超过阈值,就会产生对应的诊断信息。
  • SEVERITY:严重程度,取值为 warningcritical
  • DETAILS:诊断的详细信息,可能包含进一步调查的 SQL 或文档链接。

诊断示例

对当前时间的集群进行诊断。

  1. SELECT * FROM information_schema.inspection_result\G
  1. ***************************[ 1. row ]***************************
  2. RULE | config
  3. ITEM | log.slow-threshold
  4. TYPE | tidb
  5. INSTANCE | 172.16.5.40:4000
  6. VALUE | 0
  7. REFERENCE | not 0
  8. SEVERITY | warning
  9. DETAILS | slow-threshold = 0 will record every query to slow log, it may affect performance
  10. ***************************[ 2. row ]***************************
  11. RULE | version
  12. ITEM | git_hash
  13. TYPE | tidb
  14. INSTANCE |
  15. VALUE | inconsistent
  16. REFERENCE | consistent
  17. SEVERITY | critical
  18. DETAILS | the cluster has 2 different tidb version, execute the sql to see more detail: select * from information_schema.cluster_info where type='tidb'
  19. ***************************[ 3. row ]***************************
  20. RULE | threshold-check
  21. ITEM | storage-write-duration
  22. TYPE | tikv
  23. INSTANCE | 172.16.5.40:23151
  24. VALUE | 130.417
  25. REFERENCE | < 0.100
  26. SEVERITY | warning
  27. DETAILS | max duration of 172.16.5.40:23151 tikv storage-write-duration was too slow
  28. ***************************[ 4. row ]***************************
  29. RULE | threshold-check
  30. ITEM | rocksdb-write-duration
  31. TYPE | tikv
  32. INSTANCE | 172.16.5.40:20151
  33. VALUE | 108.105
  34. REFERENCE | < 0.100
  35. SEVERITY | warning
  36. DETAILS | max duration of 172.16.5.40:20151 tikv rocksdb-write-duration was too slow

上述诊断结果发现了以下几个问题:

  • 第一行表示 TiDB 的 log.slow-threshold 配置值为 0,可能会影响性能。
  • 第二行表示集群中有 2 个不同的 TiDB 版本
  • 第三、四行表示 TiKV 的写入延迟太大,期望时间是不超过 0.1s, 但实际值远超预期。

诊断集群在时间段 “2020-03-26 00:03:00”, “2020-03-26 00:08:00” 的问题。指定时间范围需要使用 /*+ time_range() */ 的 SQL Hint,参考下面的查询示例:

  1. select /*+ time_range("2020-03-26 00:03:00", "2020-03-26 00:08:00") */ * from information_schema.inspection_result\G
  1. ***************************[ 1. row ]***************************
  2. RULE | critical-error
  3. ITEM | server-down
  4. TYPE | tidb
  5. INSTANCE | 172.16.5.40:4009
  6. VALUE |
  7. REFERENCE |
  8. SEVERITY | critical
  9. DETAILS | tidb 172.16.5.40:4009 restarted at time '2020/03/26 00:05:45.670'
  10. ***************************[ 2. row ]***************************
  11. RULE | threshold-check
  12. ITEM | get-token-duration
  13. TYPE | tidb
  14. INSTANCE | 172.16.5.40:10089
  15. VALUE | 0.234
  16. REFERENCE | < 0.001
  17. SEVERITY | warning
  18. DETAILS | max duration of 172.16.5.40:10089 tidb get-token-duration is too slow

上面的诊断结果发现了以下问题:

  • 第一行表示 172.16.5.40:4009 TiDB 实例在 2020/03/26 00:05:45.670 发生了重启。
  • 第二行表示 172.16.5.40:10089 TiDB 实例的最大的 get-token-duration 时间为 0.234s, 期望时间是小于 0.001s。

也可以指定条件,比如只查询 critical 严重级别的诊断结果:

  1. select * from information_schema.inspection_result where severity='critical';

只查询 critical-error 规则的诊断结果:

  1. select * from information_schema.inspection_result where rule='critical-error';

诊断规则介绍

诊断模块内部包含一系列的规则,这些规则会通过查询已有的监控表和集群信息表,对结果和阈值进行对比。如果结果超过阈值将生成 warningcritical 的结果,并在 details 列中提供相应信息。

可以通过查询 inspection_rules 系统表查询已有的诊断规则:

  1. select * from information_schema.inspection_rules where type='inspection';
  1. +-----------------+------------+---------+
  2. | NAME | TYPE | COMMENT |
  3. +-----------------+------------+---------+
  4. | config | inspection | |
  5. | version | inspection | |
  6. | node-load | inspection | |
  7. | critical-error | inspection | |
  8. | threshold-check | inspection | |
  9. +-----------------+------------+---------+

config 诊断规则

config 诊断规则通过查询 CLUSTER_CONFIG 系统表,执行以下两个诊断规则:

  • 检测相同组件的配置值是否一致,并非所有配置项都会有一致性检查,下面是一致性检查的白名单:

    1. // TiDB 配置一致性检查白名单
    2. port
    3. status.status-port
    4. host
    5. path
    6. advertise-address
    7. status.status-port
    8. log.file.filename
    9. log.slow-query-file
    10. tmp-storage-path
    11. // PD 配置一致性检查白名单
    12. advertise-client-urls
    13. advertise-peer-urls
    14. client-urls
    15. data-dir
    16. log-file
    17. log.file.filename
    18. metric.job
    19. name
    20. peer-urls
    21. // TiKV 配置一致性检查白名单
    22. server.addr
    23. server.advertise-addr
    24. server.status-addr
    25. log-file
    26. raftstore.raftdb-path
    27. storage.data-dir
    28. storage.block-cache.capacity
  • 检测以下配置项的值是否符合预期。

    | 组件 | 配置项 | 预期值 | | :—— | :—— | :—— | | TiDB | log.slow-threshold | 大于 0 | | TiKV | raftstore.sync-log | true |

version 诊断规则

version 诊断规则通过查询 CLUSTER_INFO 系统表,检测相同组件的版本 hash 是否一致。示例如下:

  1. SELECT * FROM information_schema.inspection_result WHERE rule='version'\G
  1. ***************************[ 1. row ]***************************
  2. RULE | version
  3. ITEM | git_hash
  4. TYPE | tidb
  5. INSTANCE |
  6. VALUE | inconsistent
  7. REFERENCE | consistent
  8. SEVERITY | critical
  9. DETAILS | the cluster has 2 different tidb versions, execute the sql to see more detail: SELECT * FROM information_schema.cluster_info WHERE type='tidb'

critical-error 诊断规则

critical-error 诊断规则执行以下两个诊断规则:

  • 通过查询 metrics schema 数据库中相关的监控系统表,检测集群是否有出现以下比较严重的错误:

    | 组件 | 错误名字 | 相关监控表 | 错误说明 | | —— | —— | —— | —— | | TiDB | panic-count | tidb_panic_count_total_count | TiDB 出现 panic 错误 | | TiDB | binlog-error | tidb_binlog_error_total_count | TiDB 写 binlog 时出现的错误 | | TiKV | critical-error | tikv_critical_error_total_coun | TiKV 的 critical error | | TiKV | scheduler-is-busy | tikv_scheduler_is_busy_total_count | TiKV 的 scheduler 太忙,会导致 TiKV 临时不可用 | | TiKV | coprocessor-is-busy | tikv_coprocessor_is_busy_total_count | TiKV 的 coprocessor 太忙 | | TiKV | channel-is-full | tikv_channel_full_total_count | TiKV 出现 channel full 的错误 | | TiKV | tikv_engine_write_stall | tikv_engine_write_stall | TiKV 出现写入 stall 的错误 |

  • 通过查询 metrics_schema.up 监控表和 CLUSTER_LOG 系统表,检查是否有组件发生重启。

threshold-check 诊断规则

threshold-check 诊断规则通过查询 metrics schema 数据库中相关的监控系统表,检测集群中以下指标是否超出阈值:

组件 监控指标 相关监控表 预期值 说明
TiDB tso-duration pd_tso_wait_duration 小于 50 ms 获取事务 TSO 时间戳的等待耗时
TiDB get-token-duration tidb_get_token_duration 小于 1 ms 查询获取 token 的耗时,相关的 TiDB 配置参数是 token-limit
TiDB load-schema-duration tidb_load_schema_duration 小于 1 s TiDB 更新表元信息的耗时
TiKV scheduler-cmd-duration tikv_scheduler_command_duration 小于 0.1 s TiKV 执行 KV cmd 请求的耗时
TiKV handle-snapshot-duration tikv_handle_snapshot_duration 小于 30 s TiKV 处理 snapshot 的耗时
TiKV storage-write-duration tikv_storage_async_request_duration 小于 0.1 s TiKV 写入的延迟
TiKV storage-snapshot-duration tikv_storage_async_request_duration 小于 50 ms TiKV 获取 snapshot 的耗时
TiKV rocksdb-write-duration tikv_engine_write_duration 小于 100 ms TiKV RocksDB 的写入延迟
TiKV rocksdb-get-duration tikv_engine_max_get_duration 小于 50 ms TiKV RocksDB 的读取延迟
TiKV rocksdb-seek-duration tikv_engine_max_seek_duration 小于 50 ms TiKV RocksDB 执行 seek 的延迟
TiKV scheduler-pending-cmd-coun tikv_scheduler_pending_commands 小于 1000 TiKV 中被阻塞的命令数量
TiKV index-block-cache-hit tikv_block_index_cache_hit 大于 0.95 TiKV 中 index block 缓存的命中率
TiKV filter-block-cache-hit tikv_block_filter_cache_hit 大于 0.95 TiKV 中 filter block 缓存的命中率
TiKV data-block-cache-hit tikv_block_data_cache_hit 大于 0.80 TiKV 中 data block 缓存的命中率
TiKV leader-score-balance pd_scheduler_store_status 小于 0.05 检测各个 TiKV 实例的 leader score 是否均衡,期望实例间的差异小于 5%
TiKV region-score-balance pd_scheduler_store_status 小于 0.05 检测各个 TiKV 实例的 Region score 是否均衡,期望实例间的差异小于 5%
TiKV store-available-balance pd_scheduler_store_status 小于 0.2 检测各个 TiKV 实例的存储可用空间大小是否均衡,期望实例间的差异小于 20%
TiKV region-count pd_scheduler_store_status 小于 20000 检测各个 TiKV 实例的 Region 数量,期望单个实例的 Region 数量小于 20000
PD region-health pd_region_health 小于 100 检测集群中处于调度中间状态的 Region 数量,期望总数小于 100

另外还会检测 TiKV 实例的以下 thread cpu usage 是否过高:

  • scheduler-worker-cpu
  • coprocessor-normal-cpu
  • coprocessor-high-cpu
  • coprocessor-low-cpu
  • grpc-cpu
  • raftstore-cpu
  • apply-cpu
  • storage-readpool-normal-cpu
  • storage-readpool-high-cpu
  • storage-readpool-low-cpu
  • split-check-cpu

TiDB 内置的诊断规则还在不断的完善改进中,如果你也想到了一些诊断规则,非常欢迎在 tidb repository 下提 PR 或 Issue。