OceanBase 源码学习: 4.3 错误处理和 RPC 系统
OceanBase 源码学习: 4.3 错误处理和 RPC 系统
本文涵盖了 OceanBase 完善的错误处理框架及其与 RPC 通信基础设施(Infra)的集成,该系统在整个分布式数据库系统中提供统一的错误码、跨库(MySQL,Oracle)兼容性映射以及强大的错误传播机制。
系统架构
OceanBase 实现了一套集中式错误处理系统,作为 SQL 处理、存储操作和 RPC 通信中所有错误报告的基础。该系统提供了错误代码标准化、多数据库兼容(MySQL,Oracle)和结构化错误传播的功能。
错误代码架构
源文件:
https://github.com/oceanbase/oceanbase/blob/8e2580cf/src/share/ob_errno.def#L1-L30 https://github.com/oceanbase/oceanbase/blob/8e2580cf/src/share/ob_errno.h#L12-L24 https://github.com/oceanbase/oceanbase/blob/8e2580cf/src/share/ob_errno.cpp#L1-L56
错误代码系统
错误代码组织
错误处理系统将错误代码组织成逻辑范围,每个范围服务于特定的系统组件:
OB_ERROROB_ALLOCATE_MEMORY_FAILED,OB_TIMEOUT | ||
RootServer | ||
错误定义宏
系统使用几种宏类型来定义具有不同兼容性要求的错误:
// Basic error definition
DEFINE_ERROR(symbol, code, mysql_code, sqlstate, message)
// Oracle-compatible error
DEFINE_ORACLE_ERROR(symbol, code, mysql_code, sqlstate, mysql_msg, oracle_code, oracle_msg)
// PL/SQL specific error
DEFINE_PLS_ERROR(symbol, code, mysql_code, sqlstate, mysql_msg, oracle_code, oracle_msg)
源文件:
https://github.com/oceanbase/oceanbase/blob/8e2580cf/src/share/ob_errno.def#L43-L96 https://github.com/oceanbase/oceanbase/blob/8e2580cf/src/share/ob_errno.h#L23-L412
跨数据库兼容性(MySQL,Oracle)
源文件:
https://github.com/oceanbase/oceanbase/blob/8e2580cf/src/share/ob_errno.def#L195-L196 https://github.com/oceanbase/oceanbase/blob/8e2580cf/src/share/ob_errno.cpp#L58-L71
RPC 错误处理
RPC 特定的错误代码
系统针对 RPC 通信失败定义了具体的错误码:
-4011 | OB_PACKET_NOT_SENT | |
-4119 | OB_RPC_PACKET_TOO_LONG | |
-4121 | OB_RPC_SEND_ERROR | |
-4122 | OB_RPC_POST_ERROR | |
-4128 | OB_RPC_PACKET_INVALID | |
-4265 | OB_RPC_CONNECT_ERROR |
错误传输机制
源文件:
https://github.com/oceanbase/oceanbase/blob/8e2580cf/src/share/ob_errno.def#L213-L223 https://github.com/oceanbase/oceanbase/blob/8e2580cf/deps/oblib/src/lib/ob_errno.h#L86-L93
与表达系统集成
表达式错误类型
表达式求值系统通过 ObItemType 枚举系统与错误处理框架紧密集成:
源文件:
https://github.com/oceanbase/oceanbase/blob/8e2580cf/src/objit/include/objit/common/ob_item_type.h#L21-L90 https://github.com/oceanbase/oceanbase/blob/8e2580cf/src/sql/engine/expr/ob_expr_eval_functions.cpp#L1-L50
函数的错误处理
表达式系统为不同的函数类别提供了专门的错误处理:
源文件:
https://github.com/oceanbase/oceanbase/blob/8e2580cf/src/objit/include/objit/common/ob_item_type.h#L98-L429 https://github.com/oceanbase/oceanbase/blob/8e2580cf/src/sql/engine/expr/ob_expr_eval_functions.cpp#L100-L500
错误上下文和传播
错误上下文结构
语句类型集成
错误系统通过语句类型框架与 SQL 语句处理集成:
T_SELECT | ACTION_TYPE_SELECT | |
T_INSERT | ACTION_TYPE_INSERT | |
T_UPDATE | ACTION_TYPE_UPDATE | |
T_DELETE | ACTION_TYPE_DELETE | |
T_CREATE_TABLE | ACTION_TYPE_CREATE_TABLE |
源文件:
https://github.com/oceanbase/oceanbase/blob/8e2580cf/src/sql/resolver/ob_stmt_type.h#L14-L172 https://github.com/oceanbase/oceanbase/blob/8e2580cf/deps/oblib/src/lib/ob_errno.h#L155-L228
致命错误(fatal)处理
致命错误类别
-8000 到 -9000 范围内的致命错误需要立即终止连接并执行系统恢复程序:
源文件:
https://github.com/oceanbase/oceanbase/blob/8e2580cf/src/share/ob_errno.def#L13-L14 https://github.com/oceanbase/oceanbase/blob/8e2580cf/deps/oblib/src/lib/ob_errno.h#L143-L146
与分布式操作集成
错误处理系统为分布式数据库操作提供专门的支持,确保跨集群节点的一致错误报告,并在故障情况下保持事务完整性。
分布式事务错误处理
事务层错误(范围 -6000 到 -7000)包括针对分布式场景的专门处理:
分布式死锁检测: OB_DEAD_LOCK具有集群范围的检测能力共识失败:与 Paxos/Raft 共识协议集成 网络分区处理:裂脑场景的超时和重试机制 资源冲突:跨节点资源争用解决
RPC错误恢复
RPC 系统实现了复杂的重试和回退机制:
自动重试:可配置针对瞬时网络故障的重试策略 断路器:防止分布式调用中的级联故障 负载均衡:RPC 错误时自动故障转移到健康节点 连接池:高效的连接重用和错误清理
源文件:
https://github.com/oceanbase/oceanbase/blob/8e2580cf/src/share/ob_errno.def#L194-L196 https://github.com/oceanbase/oceanbase/blob/8e2580cf/deps/oblib/src/lib/ob_errno.h#L76-L77 https://github.com/oceanbase/oceanbase/blob/8e2580cf/src/share/ob_errno.cpp#L195-L225
更多详细内容请关注我的github: https://github.com/digoal/blog