当大家看到这个标题时,就已经知道了下面几点:
【一场由TiCDC异常引发的GC不干活导致的Tikv硬盘使用问题】(1)出了撒子问题?ok,集群所有KV节点存储硬盘使用80%以上,凌晨触发频繁报警,搞DB的兄弟们还能不能愉快的睡觉?
(2)谁搞的?ok,GC不干活。
(3)真正导致问题的原因是啥?OK,TiCDC背锅。
完了,看到这里本篇文章就基本介绍完了,会不会被劝退?但是老铁千万别走,且听我将细节娓娓道来,后面有“干货”,我会说明我到底做了啥子操作降低硬盘使用;GC为啥不干活?GC的原理是啥?还有为啥是TiCDC背锅。
凌晨的报警和处理
晚上11点收到多条tikv集群硬盘空间报警,爬起来打开电脑看看到底出了啥问题,一般TiKV使用磁盘空间一般都有下面几个原因:
- 本来硬盘空间就比较紧张,是不是有哪个业务又晚上“偷偷”灌数据了?这种情况一般看下grafana的overview监控就能定位。
- tikv日志暴涨,之前遇到过集群升级后tikv日志由于静默reigon开启导致的日志量过大。这个看下tikv日志产生情况,如果是日志过量,直接rm日志即可,另外tikv支持配置日志保留策略来自动删除日志。
- 其他情况
第二天.......
好好收拾下测试TiDB集群,解决Tikv存储问题,靠2种方法:
(1)找资源扩容KV
(2)看看是不是有表过大或者其他异常。
因为是测试集群,不着急直接扩容,万一是有一些大表可以清理下数据啥的,资源就有了。所以我先通过下面的命令来统计库表资源使用情况(不太准,可以参考):
如果不涉及分区表用下面的方式查看表的使用情况:
select TABLE_SCHEMA,TABLE_NAME,TABLE_ROWS,(DATA_LENGTH+INDEX_LENGTH)/1024/1024/1024 as table_size from tables order by table_size desc limit 20;
另外partition表提供了分区表和非分区表的资源使用情况(我们使用分区表较多):
select TABLE_SCHEMA,TABLE_NAME,PARTITION_NAME,TABLE_ROWS,(DATA_LENGTH+INDEX_LENGTH)/1024/1024/1024 as table_size from information_schema.PARTITIONS order by table_size desc limit 20;
另外官方也提供了接口来统计table使用情况(比较准确):
tidb-ctl table disk-usage --database(-d) [database name] --table(-t) [table name]
先看下SQL的统计结果,我发现有些表都几亿,并且有些表rows为0,竟然也有size,因为我之前经常遇到统计信息的不准导致的tables系统表信息不准的情况,为此,我竟然回收了下集群所有table的统计信息。
文章图片
然后做的操作就是跟业务沟通,删除一个没用的分区,或者turncate一些table数据,然后我就等待着资源回收了。但是我都等了半个多小时,在没有多少新写入的情况下,region数量和磁盘空间丝毫没有动静,难道GC出了问题?
OK,那我就先从mysql.tidb表看下GC的情况,不看不知道,一看吓一跳啊,我的gc-save-point竟然还在2021年的4月份,那集群中到底累积了多少MVCC版本??我的天。
文章图片
分析和解决GC的问题 通过上图已经可以发现是GC的问题,我们需要找出GC出了什么问题。一般通过日志和监控。先查看tidb server的日志:
{"level":"INFO","time":"2022/03/16 22:55:06.268 +08:00","caller":"gc_worker.go:230","message":"[gc worker] quit","uuid":"5fdb15468e00003"}
{"level":"INFO","time":"2022/03/16 22:55:22.603 +08:00","caller":"gc_worker.go:202","message":"[gc worker] start","uuid":"5fe4ce7c7200002"}
{"level":"INFO","time":"2022/03/16 22:58:22.629 +08:00","caller":"gc_worker.go:608","message":"[gc worker] there's another service in the cluster requires an earlier safe point. gc will continue with the earlier one","uuid":"5fe4ce7c7200002","ourSafePoint":431867062548692992,"minSafePoint":424280964185194498}
{"level":"INFO","time":"2022/03/16 22:58:22.637 +08:00","caller":"gc_worker.go:329","message":"[gc worker] starts the whole job","uuid":"5fe4ce7c7200002","safePoint":424280964185194498,"concurrency":5}
{"level":"INFO","time":"2022/03/16 22:58:22.639 +08:00","caller":"gc_worker.go:1036","message":"[gc worker] start resolve locks","uuid":"5fe4ce7c7200002","safePoint":424280964185194498,"concurrency":5}
{"level":"INFO","time":"2022/03/16 22:58:41.268 +08:00","caller":"gc_worker.go:1057","message":"[gc worker] finish resolve locks","uuid":"5fe4ce7c7200002","safePoint":424280964185194498,"regions":81662}
{"level":"INFO","time":"2022/03/16 22:59:22.617 +08:00","caller":"gc_worker.go:300","message":"[gc worker] there's already a gc job running, skipped","leaderTick on":"5fe4ce7c7200002"}
{"level":"INFO","time":"2022/03/16 23:00:21.331 +08:00","caller":"gc_worker.go:701","message":"[gc worker] start delete ranges","uuid":"5fe4ce7c7200002","ranges":0}
{"level":"INFO","time":"2022/03/16 23:00:21.331 +08:00","caller":"gc_worker.go:750","message":"[gc worker] finish delete ranges","uuid":"5fe4ce7c7200002","num of ranges":0,"cost time":"677ns"}
{"level":"INFO","time":"2022/03/16 23:00:21.336 +08:00","caller":"gc_worker.go:773","message":"[gc worker] start redo-delete ranges","uuid":"5fe4ce7c7200002","num of ranges":0}
{"level":"INFO","time":"2022/03/16 23:00:21.336 +08:00","caller":"gc_worker.go:802","message":"[gc worker] finish redo-delete ranges","uuid":"5fe4ce7c7200002","num of ranges":0,"cost time":"4.016μs"}
{"level":"INFO","time":"2022/03/16 23:00:21.339 +08:00","caller":"gc_worker.go:1562","message":"[gc worker] sent safe point to PD","uuid":"5fe4ce7c7200002","safe point":424280964185194498}
{"level":"INFO","time":"2022/03/16 23:08:22.638 +08:00","caller":"gc_worker.go:608","message":"[gc worker] there's another service in the cluster requires an earlier safe point. gc will continue with the earlier one","uuid":"5fe4ce7c7200002","ourSafePoint":431867219835092992,"minSafePoint":424280964185194498}
{"level":"INFO","time":"2022/03/16 23:08:22.647 +08:00","caller":"gc_worker.go:329","message":"[gc worker] starts the whole job","uuid":"5fe4ce7c7200002","safePoint":424280964185194498,"concurrency":5}
{"level":"INFO","time":"2022/03/16 23:08:22.648 +08:00","caller":"gc_worker.go:1036","message":"[gc worker] start resolve locks","uuid":"5fe4ce7c7200002","safePoint":424280964185194498,"concurrency":5}
{"level":"INFO","time":"2022/03/16 23:08:43.659 +08:00","caller":"gc_worker.go:1057","message":"[gc worker] finish resolve locks","uuid":"5fe4ce7c7200002","safePoint":424280964185194498,"regions":81647}
{"level":"INFO","time":"2022/03/16 23:09:22.616 +08:00","caller":"gc_worker.go:300","message":"[gc worker] there's already a gc job running, skipped","leaderTick on":"5fe4ce7c7200002"}
{"level":"INFO","time":"2022/03/16 23:10:22.616 +08:00","caller":"gc_worker.go:300","message":"[gc worker] there's already a gc job running, skipped","leaderTick on":"5fe4ce7c7200002"}
{"level":"INFO","time":"2022/03/16 23:10:23.727 +08:00","caller":"gc_worker.go:701","message":"[gc worker] start delete ranges","uuid":"5fe4ce7c7200002","ranges":0}
{"level":"INFO","time":"2022/03/16 23:10:23.727 +08:00","caller":"gc_worker.go:750","message":"[gc worker] finish delete ranges","uuid":"5fe4ce7c7200002","num of ranges":0,"cost time":"701ns"}
{"level":"INFO","time":"2022/03/16 23:10:23.729 +08:00","caller":"gc_worker.go:773","message":"[gc worker] start redo-delete ranges","uuid":"5fe4ce7c7200002","num of ranges":0}
{"level":"INFO","time":"2022/03/16 23:10:23.730 +08:00","caller":"gc_worker.go:802","message":"[gc worker] finish redo-delete ranges","uuid":"5fe4ce7c7200002","num of ranges":0,"cost time":"710ns"}
{"level":"INFO","time":"2022/03/16 23:10:23.733 +08:00","caller":"gc_worker.go:1562","message":"[gc worker] sent safe point to PD","uuid":"5fe4ce7c7200002","safe point":424280964185194498}
{"level":"INFO","time":"2022/03/16 23:18:22.647 +08:00","caller":"gc_worker.go:608","message":"[gc worker] there's another service in the cluster requires an earlier safe point. gc will continue with the earlier one","uuid":"5fe4ce7c7200002","ourSafePoint":431867377121492992,"minSafePoint":424280964185194498}
通过日志发现GC一直等早期的safe point:424280964185194498
there's another service in the cluster requires an earlier safe point. gc will continue with the earlier one","uuid":"5fe4ce7c7200002","ourSafePoint":431870050977185792,"minSafePoint":424280964185194498
查看下该TSO对应的系统时间,发现跟上面mysql.tidb截图的tikv-gc-safe-point对应的时间:2021-04-16 00:17:13一致。
$ tiup ctl:v5.4.0 pd -u http://10.203.178.96:2379 tso 424280964185194498
system:2021-04-16 00:17:13.934 +0800 CST
logic:2
需要执行 pd-ctl service-gc-safepoint --pd 命令查询当前的 GC safepoint 与 service GC safepoint,发现卡住的 safe point 跟 ticdc 的 safe\_point: 424280964185194498 一致。
$ tiup ctl:v5.4.0 pd -u http://10.xxxx.96:2379 service-gc-safepoint
{
"service_gc_safe_points": [
{
"service_id": "gc_worker",
"expired_at": 9223372036854775807,
"safe_point": 431880793651412992
},
{
"service_id": "ticdc",
"expired_at": 1618503433,
"safe_point": 424280964185194498
}
],
"gc_safe_point": 424280964185194498
}
查看该集群的ticdc任务,发现卡住的safe point跟changefeed id为dxl-xxxx-task这个同步任务的TSO一致:
文章图片
到此终于找到GC问题的原因。ticdc同步任务没有及时删除导致的。解决方案:删除ticdc中该同步任务
tiup cdc:v5.4.0 cli changefeed remove --pd='http://10.xxxx.96:2379' --changefeed-id=dxl-replication-task
等一段时间观察safe-point的变化,发现已经开始GC:
文章图片
不过又卡在了另一个同步任务(ticdc-demo-xxxx-test-shbt-core)上,继续remove
tiup cdc:v5.4.0 cli changefeed remove --pd='http://10.xxxx.96:2379' --changefeed-id=ticdc-demo-xxxx-test-shbt-core
删除后,等待较长时间(几个小时),最终GC safe point恢复正常:
文章图片
GC回收完毕后单KV存储又之前的800G降低到200G左右,大家也可以从下图中右边的 cf size的监控看出来,write CF从之前的3.2T降低到790G左右,下降的最多,大家知道对于tikv的列簇来说,write cf是存数据的版本信息(MVCC)的,这个的下降也印证了TiCDC导致的MVCC版本过多的问题,现在问题完全解决:
文章图片
问题虽然解决完毕了,但是Ticdc默认有个24小时的GC hold的时间,超过这个时间,就不会再影响GC,为啥有上面的问题,我也咨询了官方的产研同学:OK,首先定性:”是个BUG“,这个问题在 4.0.13, 5.0.2, 5.1.0 这几个版本中修复了,因为测试集群,版本从3.0一路升级而来,因该是用了老版本踩到了坑,修复 PR见后面的链接: https://github.com/pingcap/ti...
GC原理 虽然讲明白了ticdc->gc->硬盘报警的问题处理,但是我们还需要对GC的原理进行下简单的说明。
TiDB server架构中的GC模块
文章图片
GC作为TiDB Server的模块,主要的作用就是清理不再需要的旧数据。
大家都知道Tikv底层使用是基于LSM的数据架构,这种数据存储的方式在做数据 update 时,不会更新原来的数据,而是重新写一份新的数据,并且给每份数据都给定一个事务时间戳,这种机制也是我们经常提到的 MVCC(多版本并发控制)机制,为了避免同一条记录的版本过多,TiDB设定了一个 tikv\_gc\_life\_time 来控制版本的保留时间(默认10min),并且每经过 tikv\_gc\_run\_interval 这个时间间隔,就会触发GC的操作,清除历史版本的记录,下面详细聊下GC的流程。
GC的流程
GC都是由GC Leader触发的,一个tidb server的cluster会选举出一个GC leader,具体是哪个节点可以查看mysql.tidb表,gc leader只是一个身份状态,真正run RC的是该Leader节点的gc worker,另外GC的流程其实从文章上面的日志可以看出来。
- (1)Resolve Locks 阶段
- (2)Delete Ranges阶段
- (3)Do GC阶段
- (4)发送safe point给PD,结束本轮的GC。日志为:sent safe point to PD
涉及到GC的主要看下面的几个监控即可:
tikv-detail中的GC模块->GC task:跟上文所描述,如果执行了drop/truncate操作就看看下面监控中total-unafe-destroy-range的metric是否有值,另外日常的DML的GC,可以看看total-gc-keys的task情况。
文章图片
tikv-detail中的GC模块->GC task duration:
不同gc task的删除耗时。
文章图片
tikv-detail中的GC模块->GC speed:
按照keys/s的旧MVCC版本的删除速度。
文章图片
PS:想了解更多的GC内容可以查看官方文档或者详细阅读asktug中坤爷和娇姐写的GC三部曲:第一篇为 『GC 原理浅析』,第二篇为『GC 监控及日志解读』 ,而最后一篇则为『GC 处理案例 & FAQ』。
原文作者:@代晓磊_360 发表于: 2022-03-28
原文链接:https://tidb.io/blog/36c58d32
推荐阅读
- SQL|MySQL数据库编程01
- SQL|关系数据库标准语言SQL02
- SQL|关系数据库标准语言SQL01
- 正则表达式教程(30分钟让你精通正则表达式语法)
- 云栖号技术分享|专访李飞飞 (从清华附中高材生到阿里飞刀,一口井钻出「云原生」)
- java|主题(JAVA 桥接模式)
- SQL|如何实现高性能的数据处理\数据库性能优化
- 架构|Java大牛带你4小时开发一个SpringBoot+vue前后端分离博客项目
- 服务器|分布式ID问题及解决方案