[MySQL 5.6] Innodb 新特性之 multi purge thread

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
在做5.6.12 vs 5.6.11的性能对比时,大量update产生了很长的purge history list。手贱把innodb_fast_shutdowns设置为0了,结果Purge线程一直干活了,差不多两个小时才结束….

我们知道,在MySQL5.5版本中,就已经开始将purge 任务从master线程中独立出来,而到了5.6,已经支持多个purge线程同时进行,简单的理了下代码逻辑.

////////////////////////////////////////////////////////


在5.6中,提供了参数Innodb_purge_threads来控制做purge操作的后台线程数,最大允许设置为32.

purge线程被分为两类,一类是coordinator thread,只有一个这样的线程,另外的Innodb_purge_threads-1个是worker线程.

线程在Innodb启动时创建
quoted code in innobase_start_or_create_for_mysql:
2584         if (!srv_read_only_mode
2585             && srv_force_recovery < SRV_FORCE_NO_BACKGROUND) {
2586
2587                 os_thread_create(
2588                         srv_purge_coordinator_thread,
2589                         NULL, thread_ids + 5 + SRV_MAX_N_IO_THREADS);
2590
2591                 ut_a(UT_ARR_SIZE(thread_ids)
2592                      > 5 + srv_n_purge_threads + SRV_MAX_N_IO_THREADS);
2593
2594                 /* We've already created the purge coordinator thread above. */
2595                 for (i = 1; i < srv_n_purge_threads; ++i) {
2596                         os_thread_create(
2597                                 srv_worker_thread, NULL,
2598                                 thread_ids + 5 + i + SRV_MAX_N_IO_THREADS);
2599                 }
2600
2601                 srv_start_wait_for_purge_to_start();
2602
2603         } else {
2604                 purge_sys->state = PURGE_STATE_DISABLED;
2605         }
a. coordinator thread
协调线程的入口函数是srv_purge_coordinator_thread

分为三个阶段:
正常工作阶段,在一个while循环中调用:
2754                 rseg_history_len = srv_do_purge(
2755                         srv_n_purge_threads, &n_total_purged);

 

从while break的条件由函数srv_purge_should_exit确定,这里有一个特殊情况,当innodb_fast_shutdown设置为0时,如果上一次purge的Page数不为0,则返回false,表示不退出循环,这是因为当fast shutdown为0时,需要做完所有的purge操作才会结束线程任务


第二个阶段是确认innodb_fast_shutdown被设置为0时,所有的记录都被purge掉了;这可以避免在退出上述循环后,有新的记录加入。这里已经不再使用worker线程了(trx_purge的第一个参数为1)

2769         while (srv_fast_shutdown == 0 && n_pages_purged > 0) {
2770                 n_pages_purged = trx_purge(1, srv_purge_batch_size, false);
2771         }
最后对history list做一次truncate,并确保所有worker线程退出

2773         /* Force a truncate of the history list. */
2774         n_pages_purged = trx_purge(1, srv_purge_batch_size, true);
这里有两个需要关注的函数:

a1)srv_do_purge:

在协调线程的主要工作都是在这个函数中,注意,不是有多少工作线程,就会用多少的,这里 实际上是把多余的线程当做一个池子,只有purge跟不上更新的时候,才会去调度这些线程:

调整n_use_threads
>>当trx_sys->rseg_history_len相比上次purge有增长时,或者超过了innodb_max_purge_lag_delay(不为0),++n_use_threads
>>否则,如果存在activity(srv_check_activity),–n_use_threads

执行purge调度
2577                 n_pages_purged = trx_purge(
2578                         n_use_threads, srv_purge_batch_size, false);

每128次purge, truncate一次history list,这也是为什么我们每隔一会,才看到history list长度变小的原因

2580                 if (!(count++ % TRX_SYS_N_RSEGS)) {
2581                         /* Force a truncate of the history list. */
2582                         n_pages_purged += trx_purge(
2583                                 1, srv_purge_batch_size, true);
2584                 }
a2)  trx_purge
trx_purge是purge任务调度的核心函数,包含三个参数:
* n_purge_threads —>使用到的worker线程数
* batch_size  —-> 由innodb_purge_batch_size控制,表示一次Purge的记录数
* truncate —>是否truncate history list 

持有purge_sys->latch的x锁,并建立read view,然后释放锁
purge_sys->view = read_view_purge_open(purge_sys->heap);

读取需要purge的undo记录,记录数不超过batch size,这些purge任务被指派给n_purge_threads个thr
1213         /* Fetch the UNDO recs that need to be purged. */
1214         n_pages_handled = trx_purge_attach_undo_recs(
1215                 n_purge_threads, purge_sys, &purge_sys->limit, batch_size);
当n_purge_threads>1时,会将n_purge_threads-1个thr加入到工作队列,由worker线程来消费

1221                 /* Submit the tasks to the work queue. */
1222                 for (i = 0; i < n_purge_threads - 1; ++i) {
1223                         thr = que_fork_scheduler_round_robin(
1224                                 purge_sys->query, thr);
1225
1226                         ut_a(thr != NULL);
1227
1228                         srv_que_task_enqueue_low(thr);
1229                 }

 

调度线程同样也需要运行一个thr任务,而不是仅仅只做分配;完成后,还需要等待其他worker线程完成任务
1243 run_synchronously:
1244                 ++purge_sys->n_submitted;
1245
1246                 que_run_threads(thr);
1247
1248                 os_atomic_inc_ulint(
1249                         &purge_sys->bh_mutex, &purge_sys->n_completed, 1);
1250
1251                 if (n_purge_threads > 1) {
1252                         trx_purge_wait_for_workers_to_complete(purge_sys);
1253                 }

当truncate为true时,还需要调用trx_purge_truncate–>trx_purge_truncate_history从回滚段中移除历史记录。


b.worker thread


入口函数是srv_worker_thread
2473         do {
2474                 srv_suspend_thread(slot);
2475
2476                 os_event_wait(slot->event);
2477
2478                 if (srv_task_execute()) {
2479
2480                         /* If there are tasks in the queue, wakeup
2481                         the purge coordinator thread. */
2482
2483                         srv_wake_purge_thread_if_not_active();
2484                 }
2485
2486                 /* Note: we are checking the state without holding the
2487                 purge_sys->latch here. */
2488         } while (purge_sys->state != PURGE_STATE_EXIT);
purge_sys->state是协调线程在将要退出前设置成PURGE_STATE_EXIT。因此worker线程总是在coordinator线程退出之后再退出


srv_task_execute()的工作流程也很简单: 持有srv_sys->tasks_mutex锁,从srv_sys->tasks中取出一个thr,然后释放tasks_mutex。再调用que_run_threads(thr)完成purge

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
存储 关系型数据库 MySQL
MySQL InnoDB数据存储结构
MySQL InnoDB数据存储结构
|
1月前
|
存储 缓存 关系型数据库
MySQL的varchar水真的太深了——InnoDB记录存储结构
varchar(M) 能存多少个字符,为什么提示最大16383?innodb怎么知道varchar真正有多长?记录为NULL,innodb如何处理?某个列数据占用的字节数非常多怎么办?影响每行实际可用空间的因素有哪些?本篇围绕innodb默认行格式dynamic来说说原理。
834 6
MySQL的varchar水真的太深了——InnoDB记录存储结构
|
2月前
|
存储 缓存 关系型数据库
MySQL - 存储引擎MyISAM和Innodb
MySQL - 存储引擎MyISAM和Innodb
|
15天前
|
存储 关系型数据库 MySQL
MySQL引擎对决:深入解析MyISAM和InnoDB的区别
MySQL引擎对决:深入解析MyISAM和InnoDB的区别
31 0
|
2月前
|
SQL 关系型数据库 MySQL
Mysql事务隔离级别和锁特性
Mysql事务隔离级别和锁特性
|
1天前
|
SQL 关系型数据库 MySQL
MySQL8.0索引新特性
MySQL8.0索引新特性
|
29天前
|
存储 缓存 关系型数据库
MySQL事务的四大特性是如何保证的
在MySQL数据库中还有一种二进制日志,其用来基于时间点的还原及主从复制。从表面上来看其和重做日志非常相似,都是记录了对于数据库操作的日志。但是,从本质上来看有着非常大的不同。
14 1
|
1月前
|
SQL 关系型数据库 MySQL
深入理解MySQL事务特性:保证数据完整性与一致性
深入理解MySQL事务特性:保证数据完整性与一致性
97 1
|
1月前
|
存储 安全 关系型数据库
MySQL 临时表的用法和特性
MySQL 临时表的用法和特性
|
1月前
|
关系型数据库 MySQL 测试技术
数据库专家带你体验PolarDB MySQL版 Serverless的极致弹性特性!
本次基于阿里云瑶池数据库解决方案体验馆,带你体验PolarDB MySQL Serverless形态下的性能压测环境,基于可选择的标准压测工具进行压测,构造弹性场景进行压测,实时动态展示弹性能力、价格和性价比结果,压测环境可开放定制修改、可重复验证。参与活动即有机会获得鼠标、小米打印机、卫衣等精美礼品。
数据库专家带你体验PolarDB MySQL版 Serverless的极致弹性特性!