MySQL 8.4 Reference Manual Including MySQL NDB Cluster 8.4

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

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