MySQL的XA_prepare_event类型binlog的解析
为了支持新版的xa事务,MySQL新加了一种binlog event类型:XA_prepare
项目中使用的开源组件mysql-binlog-connector-java无法解析此种binlog event
分析源码后发现第一个问题:
MySQL中一共有39种binlog event
但是mysql-binlog-connector-java组件只写了38种binlog event,少了一种名为TRANSACTION_CONTEXT的binlog event,导致无法解析TRANSACTION_CONTEXT_EVENT/VIEW_CHANGE_EVENT/XA_PREPARE_LOG_EVENT类型的binlog event
修改完本地分支后,顺手向mysql-binlog-connector-java的作者提了一个issue,作者很友好也很迅速的修改了相关的代码
现在mysql-binlog-connector-java可以识别出XA_prepare的binlog event了,但是只是识别出来而已,我还要需要对这种binlog event进行反序列化
网上没有现成的资料,在MySQL的代码库里翻了一通后终于找到了关键代码

XA_prepare_event:: XA_prepare_event(const char* buf, const Format_description_event *description_event) : Binary_log_event(&buf, description_event->binlog_version, description_event->server_version) { uint32_t temp= 0; uint8_t temp_byte; buf+= description_event->post_header_len[XA_PREPARE_LOG_EVENT - 1]; memcpy(&temp_byte, buf, 1); one_phase= (bool) temp_byte; buf += sizeof(temp_byte); memcpy(&temp, buf, sizeof(temp)); my_xid.formatID= le32toh(temp); buf += sizeof(temp); memcpy(&temp, buf, sizeof(temp)); my_xid.gtrid_length= le32toh(temp); buf += sizeof(temp); memcpy(&temp, buf, sizeof(temp)); my_xid.bqual_length= le32toh(temp); buf += sizeof(temp); memcpy(my_xid.data, buf, my_xid.gtrid_length + my_xid.bqual_length); }
从源码中我们可以知道,XA_prepare类型的binlog event的数据部分结构如下:
one_phase : boolean, 1byte
formatID : int, 4byte
gtrid_length : int, 4byte
bqual_length : int, 4byte
data : String, gtrid + bqual, (gtrid_length + bqual_length)byte
例如[0, 123, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 103, 116, 114, 105, 100, 98, 113, 117, 97, 108]
就对应为:
one_phase:false
formatID : 123
gtrid_length : 5
bqual_length : 5
data : "gtridbqual" ---> gtrid = "gtrid", bqual = "bqual"
于是可以很容易的写出XA_prepare类型的binlog event的解析器了
顺手提了个pr,已经被作者合进去了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用