MySQL 服务器是一个多线程应用程序,它使用了许多内部锁和与锁相关的原语,例如互斥锁、读写锁(包括 prlocks 和 sxlocks)、条件变量和文件。在服务器中,锁相关对象的集合会随着新功能的实现和性能改进的代码重构而变化。与任何使用锁原语的多线程应用程序一样,当同时持有多个锁时,执行过程中总存在遇到死锁的风险。对于 MySQL 来说,死锁的影响是灾难性的,会导致服务的完全丧失。The MySQL server is a multithreaded application that uses numerous internal locking and lock-related primitives, such as mutexes, rwlocks (including prlocks and sxlocks), conditions, and files. Within the server, the set of lock-related objects changes with implementation of new features and code refactoring for performance improvements. As with any multithreaded application that uses locking primitives, there is always a risk of encountering a deadlock during execution when multiple locks are held at once. For MySQL, the effect of a deadlock is catastrophic, causing a complete loss of service.
为了启用锁获取死锁的检测并确保运行时执行没有死锁,MySQL 支持 LOCK_ORDER 工具。这允许在服务器设计中定义锁顺序依赖图,并在服务器运行时进行检查,以确保锁获取是无环的,并且执行路径符合该图。To enable detection of lock-acquisition deadlocks and enforcement
that runtime execution is free of them, MySQL supports
LOCK_ORDER tooling. This enables a lock-order
dependency graph to be defined as part of server design, and
server runtime checking to ensure that lock acquisition is acyclic
and that execution paths comply with the graph.
本节提供有关使用 LOCK_ORDER 工具的信息,但仅限于基础层面。有关完整详情,请参阅 MySQL Server Doxygen 文档中的锁顺序部分,可访问 https://dev.mysql.com/doc/index-other.html。This section provides information about using the
LOCK_ORDER tool, but only at a basic level. For
complete details, see the Lock Order section of the MySQL Server
Doxygen documentation, available at
https://dev.mysql.com/doc/index-other.html.
LOCK_ORDER 工具用于调试服务器,而不是用于生产环境。The LOCK_ORDER tool is intended for debugging
the server, not for production use.
要使用 LOCK_ORDER 工具,请按照以下步骤操作:To use the LOCK_ORDER tool, follow this
procedure:
从源代码构建 MySQL,并使用
-DWITH_LOCK_ORDER=ONCMake 选项进行配置,以便构建中包含LOCK_ORDER工具。Build MySQL from source, configuring it with the-DWITH_LOCK_ORDER=ONCMake option so that the build includesLOCK_ORDERtooling.Note启用
WITH_LOCK_ORDER选项后,MySQL 构建需要该flex程序。With theWITH_LOCK_ORDERoption enabled, MySQL builds require the flex program.要在启用
LOCK_ORDER工具的情况下运行服务器,请在服务器启动时启用lock_order系统变量。还有其他几个用于LOCK_ORDER配置的系统变量可用。To run the server with theLOCK_ORDERtool enabled, enable thelock_ordersystem variable at server startup. Several other system variables forLOCK_ORDERconfiguration are available as well.对于 MySQL 测试套件操作,mysql-test-run.pl 提供了
--lock-order选项,用于控制在测试用例执行期间是否启用LOCK_ORDER工具。For MySQL test suite operation, mysql-test-run.pl has a--lock-orderoption that controls whether to enable theLOCK_ORDERtool during test case execution.
下面描述的系统变量配置了LOCK_ORDER工具的操作,前提是 MySQL 已经构建包含LOCK_ORDER工具。主要变量是lock_order,它表示是否在运行时启用LOCK_ORDER工具:The system variables described following configure operation of
the LOCK_ORDER tool, assuming that MySQL has
been built to include LOCK_ORDER tooling. The
primary variable is lock_order,
which indicates whether to enable the
LOCK_ORDER tool at runtime:
如果
lock_order被禁用(默认值),其他任何LOCK_ORDER系统变量都不会生效。Iflock_orderis disabled (the default), no otherLOCK_ORDERsystem variables have any effect.如果
lock_order被启用,其他系统变量则用于配置启用哪些LOCK_ORDER功能。Iflock_orderis enabled, the other system variables configure whichLOCK_ORDERfeatures to enable.
通常情况下,LOCK_ORDER 工具的配置是通过执行带有 --lock-order 选项的 mysql-test-run.pl 来完成的,并且让 mysql-test-run.pl 将 LOCK_ORDER 系统变量设置为适当的值。In general, it is intended that the
LOCK_ORDER tool be configured by executing
mysql-test-run.pl with the
--lock-order option, and for
mysql-test-run.pl to set
LOCK_ORDER system variables to appropriate
values.
所有 LOCK_ORDER 系统变量必须在服务器启动时设置。在运行时,其值是可见的,但不能更改。All LOCK_ORDER system variables must be set at
server startup. At runtime, their values are visible but cannot be
changed.
一些系统变量成对存在,例如 lock_order_debug_loop 和 lock_order_trace_loop。对于这样的变量对,当与之关联的条件发生时,变量的区分如下:Some system variables exist in pairs, such as
lock_order_debug_loop and
lock_order_trace_loop. For such
pairs, the variables are distinguished as follows when the
condition occurs with which they are associated:
如果启用
_debug_变量,则会触发调试断言。If the_debug_variable is enabled, a debug assertion is raised.如果启用
_trace_变量,则会在日志中打印错误。If the_trace_variable is enabled, an error is printed to the logs.
表 7.8 LOCK_ORDER 系统变量摘要Table 7.8 LOCK_ORDER System Variable Summary
| 变量名Variable Name | 变量类型Variable Type | 变量作用域Variable Scope |
|---|---|---|
| 锁定顺序lock_order | 布尔Boolean | 全球Global |
| 锁定顺序调试循环lock_order_debug_loop | 布尔Boolean | 全球Global |
| 锁定顺序调试缺少弧lock_order_debug_missing_arc | 布尔Boolean | 全球Global |
| 锁定顺序调试缺少键lock_order_debug_missing_key | 布尔Boolean | 全球Global |
| 锁定顺序调试缺少解锁lock_order_debug_missing_unlock | 布尔Boolean | 全球Global |
| 锁定顺序依赖lock_order_dependencies | 文件名File name | 全球Global |
| 锁定顺序额外依赖lock_order_extra_dependencies | 文件名File name | 全球Global |
| 锁定顺序输出目录lock_order_output_directory | 目录名称Directory name | 全球Global |
| 锁顺序打印文本lock_order_print_txt | 布尔Boolean | 全球Global |
| 锁顺序跟踪循环lock_order_trace_loop | 布尔Boolean | 全球Global |
| 锁顺序跟踪缺失弧lock_order_trace_missing_arc | 布尔Boolean | 全球Global |
| 锁顺序跟踪缺失密钥lock_order_trace_missing_key | 布尔Boolean | 全球Global |
| 锁顺序跟踪缺失解锁lock_order_trace_missing_unlock | 布尔Boolean | 全球Global |
命令行格式Command-Line Format --lock-order[={OFF|ON}]系统变量System Variable lock_order范围Scope 全球Global 动态Dynamic 否No SET_VAR提示适用SET_VARHint Applies否No 类型Type 布尔Boolean 默认值Default Value OFFOFF是否在运行时启用
LOCK_ORDER工具。如果lock_order被禁用(默认值),则其他LOCK_ORDER系统变量不起作用。如果lock_order被启用,其他系统变量将配置哪些LOCK_ORDER功能需要启用。Whether to enable theLOCK_ORDERtool at runtime. Iflock_orderis disabled (the default), no otherLOCK_ORDERsystem variables have any effect. Iflock_orderis enabled, the other system variables configure whichLOCK_ORDERfeatures to enable.如果
lock_order被启用,当服务器遇到未在锁顺序图中声明的锁获取序列时,会引发错误。Iflock_orderis enabled, an error is raised if the server encounters a lock-acquisition sequence that is not declared in the lock-order graph.命令行格式Command-Line Format --lock-order-debug-loop[={OFF|ON}]系统变量System Variable lock_order_debug_loop范围Scope 全球Global 动态Dynamic 否No SET_VAR提示适用SET_VARHint Applies否No 类型Type 布尔Boolean 默认值Default Value OFFOFFLOCK_ORDER工具在遇到标记为锁顺序图中循环的依赖时是否会触发调试断言失败。Whether theLOCK_ORDERtool causes a debug assertion failure when it encounters a dependency that is flagged as a loop in the lock-order graph.命令行格式Command-Line Format --lock-order-debug-missing-arc[={OFF|ON}]系统变量System Variable lock_order_debug_missing_arc范围Scope 全球Global 动态Dynamic 否No SET_VAR提示适用SET_VARHint Applies否No 类型Type 布尔Boolean 默认值Default Value OFFOFF当 LOCK_ORDER 工具遇到锁顺序图中未声明的依赖关系时,是否会导致调试断言失败。Whether the LOCK_ORDER tool causes a debug assertion failure when it encounters a dependency that is not declared in the lock-order graph.
命令行格式Command-Line Format --lock-order-debug-missing-key[={OFF|ON}]--lock-order-debug-missing-key[={OFF|ON}]系统变量System Variable lock_order_debug_missing_key范围Scope 全球Global 动态Dynamic 否No SET_VAR提示适用SET_VARHint Applies否No 类型Type 布尔Boolean 默认值Default Value OFFOFF当
LOCK_ORDER工具遇到未通过性能模式(Performance Schema)正确标记的对象时,是否触发调试断言失败。Whether theLOCK_ORDERtool causes a debug assertion failure when it encounters an object that is not properly instrumented with the Performance Schema.lock_order_debug_missing_unlock命令行格式Command-Line Format --lock-order-debug-missing-unlock[={OFF|ON}]--lock-order-debug-missing-unlock[={OFF|ON}]系统变量System Variable lock_order_debug_missing_unlock范围Scope 全球Global 动态Dynamic 否No SET_VAR提示适用SET_VARHint Applies否No 类型Type 布尔Boolean 默认值Default Value OFFOFF当
LOCK_ORDER工具遇到仍被持有的锁被销毁时,是否会导致调试断言失败。Whether theLOCK_ORDERtool causes a debug assertion failure when it encounters a lock that is destroyed while still held.命令行格式Command-Line Format --lock-order-dependencies=file_name--lock-order-dependencies=file_name系统变量System Variable lock_order_dependencies范围Scope 全球Global 动态Dynamic 否No SET_VAR提示适用SET_VARHint Applies否No 类型Type 文件名File name 默认值Default Value empty stringempty string定义服务器锁顺序依赖图的
lock_order_dependencies.txt文件路径。The path to thelock_order_dependencies.txtfile that defines the server lock-order dependency graph.允许指定没有依赖。在这种情况下,将使用空的依赖图。It is permitted to specify no dependencies. An empty dependency graph is used in this case.
命令行格式Command-Line Format --lock-order-extra-dependencies=file_name--lock-order-extra-dependencies=file_name系统变量System Variable lock_order_extra_dependencies范围Scope 全球Global 动态Dynamic 否No SET_VAR提示适用SET_VARHint Applies否No 类型Type 文件名File name 默认值Default Value empty stringempty string包含锁顺序依赖图额外依赖项的文件路径。这对于在
lock_order_dependencies.txt文件中定义的主服务器依赖图上,添加描述第三方代码行为的额外依赖项非常有用。(另一种方法是直接修改lock_order_dependencies.txt文件,但这不推荐。)The path to a file containing additional dependencies for the lock-order dependency graph. This is useful to amend the primary server dependency graph, defined in thelock_order_dependencies.txtfile, with additional dependencies describing the behavior of third party code. (The alternative is to modifylock_order_dependencies.txtitself, which is not encouraged.)如果未设置此变量,则不使用辅助文件。If this variable is not set, no secondary file is used.
命令行格式Command-Line Format --lock-order-output-directory=dir_name--lock-order-output-directory=dir_name系统变量System Variable lock_order_output_directory范围Scope 全球Global 动态Dynamic 否No SET_VAR提示适用SET_VARHint Applies否No 类型Type 目录名称Directory name 默认值Default Value empty stringempty stringLOCK_ORDER工具写入日志的目录。如果未设置此变量,默认是当前目录。The directory where theLOCK_ORDERtool writes its logs. If this variable is not set, the default is the current directory.命令行格式Command-Line Format --lock-order-print-txt[={OFF|ON}]--lock-order-print-txt[={OFF|ON}]系统变量System Variable lock_order_print_txt范围Scope 全球Global 动态Dynamic 否No SET_VAR提示适用SET_VARHint Applies否No 类型Type 布尔Boolean 默认值Default Value OFFOFF是否
LOCK_ORDER工具执行锁顺序图分析并打印文本报告。报告包括检测到的任何锁获取循环。Whether theLOCK_ORDERtool performs a lock-order graph analysis and prints a textual report. The report includes any lock-acquisition cycles detected.命令行格式Command-Line Format --lock-order-trace-loop[={OFF|ON}]系统变量System Variable lock_order_trace_loop范围Scope 全球Global 动态Dynamic 否No SET_VAR提示适用SET_VARHint Applies否No 类型Type 布尔Boolean 默认值Default Value OFFOFF当
LOCK_ORDER工具在锁顺序图中遇到被标记为循环的依赖时,是否在日志文件中打印跟踪。Whether theLOCK_ORDERtool prints a trace in the log file when it encounters a dependency that is flagged as a loop in the lock-order graph.命令行格式Command-Line Format --lock-order-trace-missing-arc[={OFF|ON}]系统变量System Variable lock_order_trace_missing_arc范围Scope 全球Global 动态Dynamic 否No SET_VAR提示适用SET_VARHint Applies否No 类型Type 布尔Boolean 默认值Default Value ONON当
LOCK_ORDER工具遇到在锁顺序图中未声明的依赖时,是否在日志文件中打印跟踪。Whether theLOCK_ORDERtool prints a trace in the log file when it encounters a dependency that is not declared in the lock-order graph.命令行格式Command-Line Format --lock-order-trace-missing-key[={OFF|ON}]系统变量System Variable lock_order_trace_missing_key范围Scope 全球Global 动态Dynamic 否No SET_VAR提示适用SET_VARHint Applies否No 类型Type 布尔Boolean 默认值Default Value OFFOFF当
LOCK_ORDER工具遇到未正确使用性能模式(Performance Schema)插装的对象时,是否在日志文件中打印跟踪信息。Whether theLOCK_ORDERtool prints a trace in the log file when it encounters an object that is not properly instrumented with the Performance Schema.lock_order_trace_missing_unlock命令行格式Command-Line Format --lock-order-trace-missing-unlock[={OFF|ON}]系统变量System Variable lock_order_trace_missing_unlock范围Scope 全球Global 动态Dynamic 否No SET_VAR提示适用SET_VARHint Applies否No 类型Type 布尔Boolean 默认值Default Value ONON当
LOCK_ORDER工具遇到仍被持有的情况下销毁的锁时,是否在日志文件中打印跟踪信息。Whether theLOCK_ORDERtool prints a trace in the log file when it encounters a lock that is destroyed while still held.