MySQL的多层SP中Cursor的m_max_cursor_index相关BUG分析
MySQL 的多层 SP 中 Cursor 的 m_max_cursor_index 相关 BUG 分析
一、问题发现
在一次开发中在sp中使用多层cursor的时候想知道每层的m_max_cursor_index
值分别是多少,以用来做后续开发。于是做了以下的试验,但是发现第一个level=2那层的m_max_cursor_index
的值有点问题。
注:本次使用的MySQL数据库版本为最新的debug版本。
SQL语句示例:
greatsql> CREATE TABLE t1 (a INT, b VARCHAR(10)); 以下注释里面是该层sp_pcontext的参数值。 DELIMITER $$ CREATE PROCEDURE processnames() -- level=0,m_max_cursor_index=1+8+1 BEGIN DECLARE nameCursor0 CURSOR FOR SELECT * FROM t1; -- level=1,m_cursor_offset=0,m_max_cursor_index=1+8+1 begin DECLARE nameCursor1 CURSOR FOR SELECT * FROM t1; -- level=2,m_cursor_offset=1,m_max_cursor_index=1+8 ☆问题点 begin DECLARE nameCursor2 CURSOR FOR SELECT * FROM t1; -- level=3,m_cursor_offset=2,m_max_cursor_index=1 DECLARE nameCursor3 CURSOR FOR SELECT * FROM t1; -- level=3,m_cursor_offset=2,m_max_cursor_index=2 DECLARE nameCursor4 CURSOR FOR SELECT * FROM t1; -- level=3,m_cursor_offset=2,m_max_cursor_index=3 DECLARE nameCursor5 CURSOR FOR SELECT * FROM t1; -- level=3,m_cursor_offset=2,m_max_cursor_index=4 end; end; begin DECLARE nameCursor6 CURSOR FOR SELECT * FROM t1; -- level=2,m_cursor_offset=1,m_max_cursor_index=1 end; END $$ DELIMITER ;
首先查看上面的sp的code,可以发现nameCursor6
和nameCursor1
属于同一层,因此他们的offset值一样。
greatsql> show procedure code processnames; +-----+---------------------------------------+ | Pos | Instruction | +-----+---------------------------------------+ | 0 | cpush nameCursor0@0: SELECT * FROM t1 | | 1 | cpush nameCursor1@1: SELECT * FROM t1 | | 2 | cpush nameCursor2@2: SELECT * FROM t1 | | 3 | cpush nameCursor3@3: SELECT * FROM t1 | | 4 | cpush nameCursor4@4: SELECT * FROM t1 | | 5 | cpush nameCursor5@5: SELECT * FROM t1 | | 6 | cpop 4 | | 7 | cpop 1 | | 8 | cpush nameCursor6@1: SELECT * FROM t1 | | 9 | cpop 1 | | 10 | cpop 1 | +-----+---------------------------------------+ 11 rows in set (6.02 sec)
然后通过debug查看每层sp_pcontext的参数值(相关参数值已经在上面标识出),发现第一个level=2的sp_pcontext的m_max_cursor_index
值多了很多,预期值应该是4+1,但是实际是8+1,而上面的层都没错,这说明代码最里面那层m_max_cursor_index
赋值错了。
二、问题调查过程
1、发现了问题点就看看代码里面对于每层的m_max_cursor_index
是怎么赋值的。
1、初始化sp_pcontext的时候所有的参数都为0 sp_pcontext::sp_pcontext(THD *thd) : m_level(0), m_max_var_index(0), m_max_cursor_index(0)...{init(0, 0, 0, 0);} 2、每加一层sp_pcontext,当前的m_cursor_offset=上一层cursor个数 sp_pcontext::sp_pcontext(THD *thd, sp_pcontext *prev, sp_pcontext::enum_scope scope) : m_level(prev->m_level + 1), m_max_var_index(0), m_max_cursor_index(0)... {init(prev->current_cursor_count());} void sp_pcontext::init(uint cursor_offset) {m_cursor_offset = cursor_offset;} uint current_cursor_count() const { return m_cursor_offset + static_cast<uint>(m_cursors.size()); } 3、退出当前sp_pcontext层,需要把当前的max_cursor_index()信息值赋值给上一层的m_max_cursor_index,即当前的cursor数量累加给上一层 sp_pcontext *sp_pcontext::pop_context() { uint submax = max_cursor_index(); if (submax > m_parent->m_max_cursor_index) m_parent->m_max_cursor_index = submax; } uint max_cursor_index() const { return m_max_cursor_index + static_cast<uint>(m_cursors.size()); } 4、每次增加一个cursor,m_max_cursor_index值递增,m_max_cursor_index是计数器。 bool sp_pcontext::add_cursor(LEX_STRING name) { if (m_cursors.size() == m_max_cursor_index) ++m_max_cursor_index; return m_cursors.push_back(name); }
2、根据第一步的分析,只在最里面那层的m_max_cursor_index
累加出来计算错误,看看上面的累加过程,是用max_cursor_index()
值来累加的,于是查看max_cursor_index()
函数的实现:
uint max_cursor_index() const { return m_max_cursor_index + static_cast<uint>(m_cursors.size()); }
这里是把当前层的m_max_cursor_index
值加上m_cursors.size()
,但是在函数add_cursor
里面,m_cursors
数组每增加一个cursor
,m_max_cursor_index
都要加1,也就是说在最里面那层sp_pcontext
的计算重复了,计算了2遍m_cursors.size()
,导致上面的level=2那层的m_max_cursor_index
值变成2*4=8了。到这里问题点发现。
三、问题解决方案
通过以上代码解析后,可以考虑只对最里面那层sp_pcontext
的max_cursor_index()
取值进行修改,最里面那层的sp_pcontext
没有m_children
,因此可以用这个数组值进行判断。代码作如下修改:
uint max_cursor_index() const { if(m_children.size() == 0) -- 最里面那层sp_pcontext直接返回m_max_cursor_index的值。 return m_max_cursor_index; -- 可以改为static_cast<uint>(m_cursors.size()),二者值一样。 else -- 上层sp_pcontext返回下层所有sp_pcontext的m_max_cursor_index的值,再加上当前层的m_cursors.size()值。 return m_max_cursor_index + static_cast<uint>(m_cursors.size()); }
四、问题总结
在MySQL的sp里面使用cursor的话,因为m_max_cursor_index
只用于统计,不用于实际赋值和计算过程,因此不影响使用。但是如果要用这个值用于二次开发,就要注意到这个问题。上面的修改方案只是其中一个解决方案,也可以根据自己的需要去改add_cursor的m_max_cursor_index
的赋值过程。
这次发现的问题属于不参与计算的bug,但却影响开源代码的后续开发,在实际开发应用中类似的问题也要注意,一不小心就会踩坑。
Enjoy GreatSQL :)
关于 GreatSQL
GreatSQL是适用于金融级应用的国内自主开源数据库,具备高性能、高可靠、高易用性、高安全等多个核心特性,可以作为MySQL或Percona Server的可选替换,用于线上生产环境,且完全免费并兼容MySQL或Percona Server。
相关链接: GreatSQL社区 Gitee GitHub Bilibili
GreatSQL社区:
社区有奖建议反馈: https://greatsql.cn/thread-54-1-1.html
社区博客有奖征稿详情: https://greatsql.cn/thread-100-1-1.html
(对文章有疑问或者有独到见解都可以去社区官网提出或分享哦~)
技术交流群:
微信&QQ群:
QQ群:533341697
微信群:添加GreatSQL社区助手(微信号:wanlidbc
)好友,待社区助手拉您进群。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
对 NGINX、Kong 和 Amazon 的 API 管理解决方案进行基准测试:它们能否交付实时 API?
原文作者:Alessandro Fael Garcia of F5 原文链接:对 NGINX、Kong 和 Amazon 的 API 管理解决方案进行基准测试:它们能否交付实时 API? 转载来源:NGINX 开源社区 NGINX 唯一中文官方社区 ,尽在nginx.org.cn 速度 — 这在当今数字环境中尤为重要,如果您的应用性能太慢,消费者会毫不犹豫地选择您的竞争对手。应用速度最终取决于 API 的响应能力、运行状况和适应性,而影响 API 响应能力的关键因素之一就是 API 网关引入的延迟。但是,并非所有 API 网关的性能都是相同水平。 这点让我想起去年秋天,一位 NGINX 客户(消费信贷行业的一家知名公司)告诉我们,随着越来越多的应用和其他组件需要彼此通信以提供用户期望的数字体验,“实时” API 性能的重要性正日益提高。我们很高兴得知 NGINX Plus 是唯一能实现客户所需的超短 API 延迟(低至 10 毫秒)的 API 网关。其他许多客户,例如Capital One,也与我们分享了如何通过使用 NGINX 开源版或 NGINX Plus 作为 API 网关来缩短...
- 下一篇
开源流量回放平台 AREX 在携程的大规模落地实践
AREX 是一款由携程开源的流量回放平台,孵化于机票 BU 内部,聚焦录制回放核心链路的建设,从基础方案建设到核心业务线的深入落地验证,在集团复杂业务场景下不断迭代和优化,积累了大量经验,取得了可见的成果,在携程落地至今已有 4000+ 应用接入,交付率和缺陷数均有所改善。 本篇文章主要介绍 AREX 在携程内部落地实践过程中遇到的一系列挑战和解决方案,以及如何通过 AREX 快速部署一站式流量录制回放解决方案来降低接入成本,快速落地。 背景 流量录制回放技术在性能测试、回归测试、自动化测试以及线上问题快速修复方面有广泛的应用前景,可以帮助技术团队应对复杂的业务场景和系统架构,同时确保系统的稳定性,并在研发流程中提高效率。 然而在技术方案落地时,会面临很多的挑战,比如基础设施建设难度大、前期投入成本和收益不成正比、落地场景模糊不清等。 方案 目前市场上已知的开源解决方案大部分都是在 Jvm-Sandbox-Repeater 基础上进行二次开发和改造,核心原理也都是通过录制线上真实流量然后在测试环境进行回放,验证代码逻辑正确性。那么可能有人会问:既然已有成熟的解决方案,为什么还要“重复造...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- 2048小游戏-低调大师作品
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- CentOS6,CentOS7官方镜像安装Oracle11G
- CentOS关闭SELinux安全模块
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- CentOS7安装Docker,走上虚拟化容器引擎之路
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库