8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 88%

7.9.3 锁定订单工具7.9.3 The LOCK_ORDER Tool

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:

  1. 从源代码构建 MySQL,并使用 -DWITH_LOCK_ORDER=ON CMake 选项进行配置,以便构建中包含 LOCK_ORDER 工具。Build MySQL from source, configuring it with the -DWITH_LOCK_ORDER=ON CMake option so that the build includes LOCK_ORDER tooling.

    Note

    启用WITH_LOCK_ORDER选项后,MySQL 构建需要该flex程序。With the WITH_LOCK_ORDER option enabled, MySQL builds require the flex program.

  2. 要在启用 LOCK_ORDER 工具的情况下运行服务器,请在服务器启动时启用 lock_order 系统变量。还有其他几个用于 LOCK_ORDER 配置的系统变量可用。To run the server with the LOCK_ORDER tool enabled, enable the lock_order system variable at server startup. Several other system variables for LOCK_ORDER configuration are available as well.

  3. 对于 MySQL 测试套件操作,mysql-test-run.pl 提供了 --lock-order 选项,用于控制在测试用例执行期间是否启用 LOCK_ORDER 工具。For MySQL test suite operation, mysql-test-run.pl has a --lock-order option that controls whether to enable the LOCK_ORDER tool 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系统变量都不会生效。If lock_order is disabled (the default), no other LOCK_ORDER system variables have any effect.

  • 如果lock_order被启用,其他系统变量则用于配置启用哪些LOCK_ORDER功能。If lock_order is enabled, the other system variables configure which LOCK_ORDER features to enable.

Note

通常情况下,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

  • lock_order

    命令行格式Command-Line Format --lock-order[={OFF|ON}]
    系统变量System Variable lock_order
    范围Scope 全球Global
    动态Dynamic 否No
    SET_VAR 提示适用SET_VAR Hint Applies 否No
    类型Type 布尔Boolean
    默认值Default Value OFFOFF

    是否在运行时启用 LOCK_ORDER 工具。如果 lock_order 被禁用(默认值),则其他 LOCK_ORDER 系统变量不起作用。如果 lock_order 被启用,其他系统变量将配置哪些 LOCK_ORDER 功能需要启用。Whether to enable the LOCK_ORDER tool at runtime. If lock_order is disabled (the default), no other LOCK_ORDER system variables have any effect. If lock_order is enabled, the other system variables configure which LOCK_ORDER features to enable.

    如果 lock_order 被启用,当服务器遇到未在锁顺序图中声明的锁获取序列时,会引发错误。If lock_order is enabled, an error is raised if the server encounters a lock-acquisition sequence that is not declared in the lock-order graph.

  • lock_order_debug_loop

    命令行格式Command-Line Format --lock-order-debug-loop[={OFF|ON}]
    系统变量System Variable lock_order_debug_loop
    范围Scope 全球Global
    动态Dynamic 否No
    SET_VAR 提示适用SET_VAR Hint 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 flagged as a loop in the lock-order graph.

  • lock_order_debug_missing_arc

    命令行格式Command-Line Format --lock-order-debug-missing-arc[={OFF|ON}]
    系统变量System Variable lock_order_debug_missing_arc
    范围Scope 全球Global
    动态Dynamic 否No
    SET_VAR 提示适用SET_VAR Hint 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.

  • lock_order_debug_missing_key

    命令行格式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_VAR Hint Applies 否No
    类型Type 布尔Boolean
    默认值Default Value OFFOFF

    当 LOCK_ORDER 工具遇到未通过性能模式(Performance Schema)正确标记的对象时,是否触发调试断言失败。Whether the LOCK_ORDER tool 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_VAR Hint Applies 否No
    类型Type 布尔Boolean
    默认值Default Value OFFOFF

    当 LOCK_ORDER 工具遇到仍被持有的锁被销毁时,是否会导致调试断言失败。Whether the LOCK_ORDER tool causes a debug assertion failure when it encounters a lock that is destroyed while still held.

  • lock_order_dependencies

    命令行格式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_VAR Hint Applies 否No
    类型Type 文件名File name
    默认值Default Value empty stringempty string

    定义服务器锁顺序依赖图的 lock_order_dependencies.txt 文件路径。The path to the lock_order_dependencies.txt file that defines the server lock-order dependency graph.

    允许指定没有依赖。在这种情况下,将使用空的依赖图。It is permitted to specify no dependencies. An empty dependency graph is used in this case.

  • lock_order_extra_dependencies

    命令行格式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_VAR Hint 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 the lock_order_dependencies.txt file, with additional dependencies describing the behavior of third party code. (The alternative is to modify lock_order_dependencies.txt itself, which is not encouraged.)

    如果未设置此变量,则不使用辅助文件。If this variable is not set, no secondary file is used.

  • lock_order_output_directory

    命令行格式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_VAR Hint Applies 否No
    类型Type 目录名称Directory name
    默认值Default Value empty stringempty string

    LOCK_ORDER 工具写入日志的目录。如果未设置此变量,默认是当前目录。The directory where the LOCK_ORDER tool writes its logs. If this variable is not set, the default is the current directory.

  • lock_order_print_txt

    命令行格式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_VAR Hint Applies 否No
    类型Type 布尔Boolean
    默认值Default Value OFFOFF

    是否 LOCK_ORDER 工具执行锁顺序图分析并打印文本报告。报告包括检测到的任何锁获取循环。Whether the LOCK_ORDER tool performs a lock-order graph analysis and prints a textual report. The report includes any lock-acquisition cycles detected.

  • lock_order_trace_loop

    命令行格式Command-Line Format --lock-order-trace-loop[={OFF|ON}]
    系统变量System Variable lock_order_trace_loop
    范围Scope 全球Global
    动态Dynamic 否No
    SET_VAR 提示适用SET_VAR Hint Applies 否No
    类型Type 布尔Boolean
    默认值Default Value OFFOFF

    当 LOCK_ORDER 工具在锁顺序图中遇到被标记为循环的依赖时,是否在日志文件中打印跟踪。Whether the LOCK_ORDER tool prints a trace in the log file when it encounters a dependency that is flagged as a loop in the lock-order graph.

  • lock_order_trace_missing_arc

    命令行格式Command-Line Format --lock-order-trace-missing-arc[={OFF|ON}]
    系统变量System Variable lock_order_trace_missing_arc
    范围Scope 全球Global
    动态Dynamic 否No
    SET_VAR 提示适用SET_VAR Hint Applies 否No
    类型Type 布尔Boolean
    默认值Default Value ONON

    当 LOCK_ORDER 工具遇到在锁顺序图中未声明的依赖时,是否在日志文件中打印跟踪。Whether the LOCK_ORDER tool prints a trace in the log file when it encounters a dependency that is not declared in the lock-order graph.

  • lock_order_trace_missing_key

    命令行格式Command-Line Format --lock-order-trace-missing-key[={OFF|ON}]
    系统变量System Variable lock_order_trace_missing_key
    范围Scope 全球Global
    动态Dynamic 否No
    SET_VAR 提示适用SET_VAR Hint Applies 否No
    类型Type 布尔Boolean
    默认值Default Value OFFOFF

    当 LOCK_ORDER 工具遇到未正确使用性能模式(Performance Schema)插装的对象时,是否在日志文件中打印跟踪信息。Whether the LOCK_ORDER tool 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_VAR Hint Applies 否No
    类型Type 布尔Boolean
    默认值Default Value ONON

    当 LOCK_ORDER 工具遇到仍被持有的情况下销毁的锁时,是否在日志文件中打印跟踪信息。Whether the LOCK_ORDER tool prints a trace in the log file when it encounters a lock that is destroyed while still held.