5.9.3 The LOCK_ORDER Tool

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.

As of MySQL 8.0.17, 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.

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.

The LOCK_ORDER tool is intended for debugging the server, not for production use.

To use the LOCK_ORDER tool, follow this procedure:

  1. Build MySQL from source, configuring it with the -DWITH_LOCK_ORDER=ON CMake option so that the build includes LOCK_ORDER tooling.

    Note注意

    With the WITH_LOCK_ORDER option enabled, MySQL builds require the flex program.

  2. 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. 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.

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:

Note注意

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.

All LOCK_ORDER system variables must be set at server startup. At runtime, their values are visible but cannot be changed.

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:

Table 5.7 LOCK_ORDER System Variable Summary

Variable NameVariable TypeVariable Scope
lock_orderBooleanGlobal
lock_order_debug_loopBooleanGlobal
lock_order_debug_missing_arcBooleanGlobal
lock_order_debug_missing_keyBooleanGlobal
lock_order_debug_missing_unlockBooleanGlobal
lock_order_dependenciesFile nameGlobal
lock_order_extra_dependenciesFile nameGlobal
lock_order_output_directoryDirectory nameGlobal
lock_order_print_txtBooleanGlobal
lock_order_trace_loopBooleanGlobal
lock_order_trace_missing_arcBooleanGlobal
lock_order_trace_missing_keyBooleanGlobal
lock_order_trace_missing_unlockBooleanGlobal