PolarDB新增非阻塞DDL(Nonblock DDL)功能。Nonblock DDL规避了执行DDL过程中MDL锁长时间获取不成功导致的连接堆积和阻塞,有效地解决了DDL导致的锁表问题。
背景信息
您在执行DDL操作的时候,若目标表存在未提交的长事务或大查询,DDL将持续等待获取MDL-X锁。在PolarDB中由于MDL-X锁具有最高优先级,DDL在等待MDL-X锁的过程中,将阻塞目标表上所有的新事务,这将导致业务连接的堆积和阻塞,可能会造成整个业务系统崩溃的严重后果。PolarDB提供的Nonblock DDL功能,可以保证即使在无法获得MDL-X锁的情况下,依然允许新事务进入目标表,从而保证整个业务系统的稳定。
适用范围
PolarDB MySQL版集群需满足如下条件之一:
PolarDB MySQL版8.0.1版本且修订版本为8.0.1.1.29及以上。
PolarDB MySQL版8.0.2版本且修订版本为8.0.2.2.12及以上。
您可以通过查询版本号来确认集群版本。
注意事项
开启Nonblock DDL功能会导致DDL的优先级降低,同时因MDL锁获取失败从而导致执行DDL的失败概率也会相应增大。
使用限制
PolarDB MySQL版8.0.1版本且修订版本为8.0.1.1.29及以上,以及8.0.2.2.12版本:
仅ALTER TABLE语句支持Nonblock DDL功能,应用于
ALTER TABLE table_name ADD INDEX index_name ( `column1`, `column2`, `column3` )语句效果更佳。对于在InnoDB引擎上创建的表,需要使用
ALTER TABLE table_name engine=innodb命令来代替OPTIMIZE TABLE table_name命令进行碎片整理。
PolarDB MySQL版8.0.2版本且修订版本为8.0.2.2.13及以上:
ALTER TABLE、OPTIMIZE TABLE和TRUNCATE TABLE语句支持Nonblock DDL功能。
使用方法
您可以先通过loose_polar_nonblock_ddl_mode参数开启Nonblock DDL功能,并通过loose_polar_nonblock_ddl_retry_times参数设置获取MDL-X锁超时重试的次数、loose_polar_nonblock_ddl_retry_interval参数设置获取MDL-X锁超时重试的时间间隔、loose_polar_nonblock_ddl_lock_wait_timeout参数设置获取MDL-X锁超时的时间。具体操作请参见设置集群参数和节点参数。参数说明如下:
参数 | 级别 | 说明 |
loose_polar_nonblock_ddl_mode | Session | Nonblock DDL功能开关。取值范围如下:
|
loose_polar_nonblock_ddl_retry_times | Session | 设置获取MDL-X锁超时重试的次数。取值范围:0~31536000。默认值为0(由参数 说明 该参数值建议设置为4194304。 |
loose_polar_nonblock_ddl_retry_interval | Session | 设置获取MDL-X锁超时重试的时间间隔。取值范围:1~31536000。单位为秒。默认值为6。 |
loose_polar_nonblock_ddl_lock_wait_timeout | Session | 设置获取MDL-X锁超时的时间。取值范围:1~31536000。单位为秒。默认值为1。 |
性能对比
本文档对比了开启和关闭Nonblock DDL功能对业务的影响情况,同时与使用外部工具gh-ost进行表结构变更也进行了性能对比。
测试工具
SysBench是一个跨平台且支持多线程的模块化基准测试工具,用于评估系统在运行高负载的数据库时相关核心参数的性能表现。使用SysBench是为了绕过复杂的数据库基准设置,甚至在没有安装数据库的前提下,快速了解数据库系统的性能。关于如何使用SysBench,请参见性能测试方法(OLTP)。
测试环境
一个规格为8核64 GB的标准版PolarDB MySQL版8.0版本的集群。
测试方法
使用SysBench创建1个测试表
sbtest1,并插入1000000行数据。./oltp_read_write.lua --mysql-host="集群地址" --mysql-port=“端口号” --mysql-user=“用户名” --mysql-password=“用户密码” --mysql-db="sbtest" --tables=1 --table-size=1000000 --report-interval=1 --percentile=99 --threads=8 --time=6000 prepare通过SysBench中的
oltp_read_write.lua模拟业务。./oltp_read_write.lua --mysql-host="集群地址" --mysql-port=“端口号” --mysql-user=“用户名” --mysql-password=“用户密码” --mysql-db="sbtest" --tables=1 --table-size=1000000 --report-interval=1 --percentile=99 --threads=8 --time=6000 run在目标表
sbtest1上开启一个事务但不提交,该事务将持有目标表sbtest1的MDL锁。/* session 1 */ begin; select * from sbtest1;在新会话中,开启和关闭Nonblock DDL功能的条件下,分别对表
sbtest1进行加列操作,观察TPS的变化情况。/* session 2 */ alter table sbtest1 add column d int;使用gh-ost工具进行加列操作,观察TPS的变化情况(开启Binlog)。
./gh-ost --assume-rbr --user="用户名" --password="用户密码" --host="集群地址" --port="端口号" --database="sbtest" --table="sbtest1" --alter="ADD COLUMN d INT" --allow-on-master --aliyun-rds --initially-drop-old-table --initially-drop-ghost-table --execute;
测试结果
关闭Nonblock DDL,TPS持续跌零。默认超时时间为31536000,严重影响业务。
[ 6s ] thds: 8 tps: 288.00 qps: 5751.00 (r/w/o: 4023.00/1152.00/576.00) [ 7s ] thds: 8 tps: 288.00 qps: 5742.90 (r/w/o: 4016.93/1151.98/573.99) [ 8s ] thds: 8 tps: 272.00 qps: 5462.06 (r/w/o: 3827.04/1089.01/546.01) [ 9s ] thds: 8 tps: 290.00 qps: 5782.06 (r/w/o: 4043.04/1159.01/580.01) [ 10s ] thds: 8 tps: 270.00 qps: 5436.99 (r/w/o: 3809.00/1088.00/540.00) [ 11s ] thds: 8 tps: 289.00 qps: 5746.91 (r/w/o: 4020.94/1147.98/577.99) [ 12s ] thds: 8 tps: 277.00 qps: 5558.08 (r/w/o: 3893.05/1111.02/554.01) [ 13s ] thds: 8 tps: 284.00 qps: 5672.96 (r/w/o: 3967.97/1136.99/568.00) [ 14s ] thds: 8 tps: 238.00 qps: 4767.03 (r/w/o: 3341.02/950.01/476.00) [ 15s ] thds: 8 tps: 321.00 qps: 6429.97 (r/w/o: 4502.98/1284.99/642.00) [ 16s ] thds: 8 tps: 278.00 qps: 5529.98 (r/w/o: 3865.98/1109.00/555.00) [ 17s ] thds: 8 tps: 284.00 qps: 5698.06 (r/w/o: 3992.04/1137.01/569.01) [ 18s ] thds: 8 tps: 123.00 qps: 2474.00 (r/w/o: 1730.00/491.00/253.00) [ 19s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 20s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 21s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 22s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 23s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 24s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 25s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 26s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 27s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 28s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 29s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 30s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 31s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 32s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 33s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 34s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 35s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 36s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 37s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 38s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 39s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00 [ 40s ] thds: 8 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms, 99%): 0.00开启Nonblock DDL,TPS周期性下降,但未跌零。对业务影响较小,能保证业务系统的稳定。sysbench 压测输出中,每隔约 7 秒出现一次 tps 从正常的约 280 骤降到约 70 的周期性抖动(第 12s、19s、26s、33s),紧接着下一秒 tps 骤升至约 483–489,随后恢复正常。
[ 9s ] thds: 8 tps: 280.00 qps: 5600.00 (r/w/o: 3924.00/1116.00/560.00) la [ 10s ] thds: 8 tps: 278.99 qps: 5574.83 (r/w/o: 3903.88/1112.97/557.98) la [ 11s ] thds: 8 tps: 276.00 qps: 5517.98 (r/w/o: 3859.99/1106.00/552.00) [ 12s ] thds: 8 tps: 71.00 qps: 1404.04 (r/w/o: 975.03/282.01/147.00) lat [ 13s ] thds: 8 tps: 485.99 qps: 9817.74 (r/w/o: 6900.82/1944.95/971.97) [ 14s ] thds: 8 tps: 290.01 qps: 5773.20 (r/w/o: 4034.14/1159.04/580.02) [ 15s ] thds: 8 tps: 278.00 qps: 5572.01 (r/w/o: 3904.01/1112.00/556.00) [ 16s ] thds: 8 tps: 270.00 qps: 5404.96 (r/w/o: 3785.98/1079.99/539.00) [ 17s ] thds: 8 tps: 283.00 qps: 5633.02 (r/w/o: 3933.02/1134.00/566.00) [ 18s ] thds: 8 tps: 286.00 qps: 5734.99 (r/w/o: 4020.99/1142.00/572.00) [ 19s ] thds: 8 tps: 66.00 qps: 1334.00 (r/w/o: 931.00/264.00/139.00) lat [ 20s ] thds: 8 tps: 489.99 qps: 9900.72 (r/w/o: 6956.80/1963.94/979.97) [ 21s ] thds: 8 tps: 283.01 qps: 5633.14 (r/w/o: 3938.10/1129.03/566.01) [ 22s ] thds: 8 tps: 271.00 qps: 5438.02 (r/w/o: 3811.01/1085.00/542.00) [ 23s ] thds: 8 tps: 286.99 qps: 5744.85 (r/w/o: 4023.89/1146.97/573.98) [ 24s ] thds: 8 tps: 273.01 qps: 5456.14 (r/w/o: 3819.10/1091.03/546.01) [ 25s ] thds: 8 tps: 281.99 qps: 5626.83 (r/w/o: 3933.88/1128.96/563.98) [ 26s ] thds: 8 tps: 77.00 qps: 1580.05 (r/w/o: 1111.03/307.01/162.00) lat [ 27s ] thds: 8 tps: 483.00 qps: 9731.03 (r/w/o: 6832.02/1933.01/966.00) [ 28s ] thds: 8 tps: 282.00 qps: 5631.01 (r/w/o: 3941.01/1127.00/563.00) [ 29s ] thds: 8 tps: 278.00 qps: 5590.00 (r/w/o: 3919.00/1114.00/557.00) [ 30s ] thds: 8 tps: 272.00 qps: 5436.99 (r/w/o: 3806.99/1086.00/544.00) [ 31s ] thds: 8 tps: 292.00 qps: 5836.99 (r/w/o: 4084.99/1168.00/584.00) [ 32s ] thds: 8 tps: 278.99 qps: 5579.81 (r/w/o: 3903.87/1117.96/557.98) [ 33s ] thds: 8 tps: 70.00 qps: 1434.04 (r/w/o: 1008.03/278.01/148.00) lat [ 34s ] thds: 8 tps: 483.00 qps: 9758.07 (r/w/o: 6850.05/1942.01/966.01) [ 35s ] thds: 8 tps: 281.00 qps: 5606.01 (r/w/o: 3927.01/1117.00/562.00) [ 36s ] thds: 8 tps: 282.00 qps: 5642.98 (r/w/o: 3953.99/1125.00/564.00)使用gh-ost进行表结构无锁变更,TPS周期性跌零。对业务影响很大,这是cut-over阶段短暂锁表造成的。
[ 18s ] thds: 4 tps: 257.00 qps: 5141.99 (r/w/o: 3594.99/1034.00/513. [ 19s ] thds: 4 tps: 235.00 qps: 4696.99 (r/w/o: 3289.99/936.00/471.0 [ 20s ] thds: 4 tps: 259.00 qps: 5199.06 (r/w/o: 3643.04/1038.01/518. [ 21s ] thds: 4 tps: 262.00 qps: 5224.98 (r/w/o: 3656.99/1044.00/524. [ 22s ] thds: 4 tps: 277.00 qps: 5533.95 (r/w/o: 3868.96/1110.99/553. [ 23s ] thds: 4 tps: 233.00 qps: 4682.03 (r/w/o: 3281.02/935.01/466.0 [ 24s ] thds: 4 tps: 272.00 qps: 5419.97 (r/w/o: 3789.98/1085.99/544. [ 25s ] thds: 4 tps: 254.00 qps: 5092.04 (r/w/o: 3572.03/1012.01/508. [ 26s ] thds: 4 tps: 252.00 qps: 5038.94 (r/w/o: 3525.96/1008.99/503. [ 27s ] thds: 4 tps: 264.00 qps: 5265.06 (r/w/o: 3682.05/1055.01/528. [ 28s ] thds: 4 tps: 262.00 qps: 5239.95 (r/w/o: 3667.96/1047.99/523. [ 29s ] thds: 4 tps: 254.00 qps: 5095.06 (r/w/o: 3567.04/1020.01/508. [ 30s ] thds: 4 tps: 26.00 qps: 529.00 (r/w/o: 373.00/100.00/56.00) l [ 31s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 32s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 33s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 34s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 35s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 36s ] thds: 4 tps: 254.00 qps: 5124.96 (r/w/o: 3596.97/1019.99/508. [ 37s ] thds: 4 tps: 38.00 qps: 775.00 (r/w/o: 547.00/148.00/80.00) l [ 38s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 39s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 40s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 41s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 42s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 43s ] thds: 4 tps: 239.00 qps: 4837.04 (r/w/o: 3396.03/963.01/478.0 [ 44s ] thds: 4 tps: 43.00 qps: 863.00 (r/w/o: 608.00/165.00/90.00) l [ 45s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 46s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 47s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 48s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 49s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 50s ] thds: 4 tps: 484.00 qps: 9731.07 (r/w/o: 6823.05/1940.01/968. [ 51s ] thds: 4 tps: 57.00 qps: 1134.00 (r/w/o: 793.00/224.00/117.00) [ 52s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 53s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 54s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 55s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 56s ] thds: 4 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,9 [ 57s ] thds: 4 tps: 476.00 qps: 9547.93 (r/w/o: 6691.95/1903.99/951. [ 58s ] thds: 4 tps: 62.00 qps: 1271.99 (r/w/o: 896.00/248.00/128.00)
Nonblock DDL与gh-ost工具性能对比
开启Nonblock DDL并使用传统的加列方式(INSTANT、INPLACE、COPY)与使用gh-ost工具分别对100万行数据的表进行加列操作的性能对比如下:
在无负载的情况下,通过下图可以看出开启Nonblock DDL并使用传统的加列方式(INSTANT、INPLACE、COPY)比使用gh-ost工具耗时更少。

使用SysBench的oltp_read_write模拟业务负载,通过下图可以看出开启Nonblock DDL并使用传统的加列方式(INSTANT、INPLACE、COPY)比使用gh-ost工具耗时更少。

测试结论
Nonblock DDL功能在DDL执行期间能更好的规避新事务阻塞、TPS跌零等问题,从而尽可能的保证业务系统的稳定性。同时,能够提供更高效的DDL变更能力。
联系我们
若您对DDL操作有任何疑问,可通过钉钉搜索群号入群咨询。您可以直接@群内专家,并附上您要咨询的问题;同时群内也有PolarDB MySQL版小助手24*7小时在线回答您的问题。钉钉群号:15375044501。