MDEV-39548 Cleanup MDL_request boilerplate with ACQUIRE_LOCK macro#5075
MDEV-39548 Cleanup MDL_request boilerplate with ACQUIRE_LOCK macro#5075longjinvan wants to merge 1 commit into
Conversation
Add new acquire_lock() overloads on MDL_context that combine MDL_request initialization and lock acquisition into a single call, returning MDL_ticket* directly. Convert applicable call sites across the codebase to use the new ACQUIRE_LOCK macro. All new code of the whole pull request, including one or several files that are either new files or modified ones, are contributed under the BSD-new license. I am contributing on behalf of my employer Amazon Web Services, Inc.
There was a problem hiding this comment.
Code Review
This pull request streamlines the process of acquiring Metadata Locks (MDL) by adding new acquire_lock methods to the MDL_context class and introducing ACQUIRE_LOCK and ACQUIRE_LOCK_BY_KEY macros. These additions allow for more concise code by removing the need to manually declare and initialize MDL_request objects in many parts of the server, such as in backup operations, DDL logging, and table handling. The reviewer recommended adding an MDL_ prefix to the new macros to ensure consistency with existing naming patterns and to prevent potential namespace conflicts.
| #define ACQUIRE_LOCK(NAMESPACE, DB, NAME, TYPE, TIMEOUT, DURATION) \ | ||
| acquire_lock(NAMESPACE, DB, NAME, TYPE, TIMEOUT, DURATION, __FILE__, __LINE__) | ||
|
|
||
| #define ACQUIRE_LOCK_BY_KEY(KEY, TYPE, TIMEOUT, DURATION) \ | ||
| acquire_lock(KEY, TYPE, TIMEOUT, DURATION, __FILE__, __LINE__) |
There was a problem hiding this comment.
The new macros ACQUIRE_LOCK and ACQUIRE_LOCK_BY_KEY lack the MDL_ prefix, which is inconsistent with existing macros like MDL_REQUEST_INIT. Using a prefix also helps avoid potential name collisions in the global namespace. Consider renaming them to MDL_ACQUIRE_LOCK and MDL_ACQUIRE_LOCK_BY_KEY respectively.
References
- Macros should use consistent prefixes (e.g., MDL_) to maintain naming conventions and prevent namespace collisions. (link)
Description
MDL (Metadata Lock) is MariaDB's locking subsystem that protects database objects (tables, schemas, stored procedures, etc.) from concurrent DDL — for example, preventing a table from being dropped while another thread is reading from it. MDL_request is the "lock request form" that callers must create, initialize, submit, and then extract the resulting MDL_ticket from.
Currently, every MDL lock acquisition requires repetitive boilerplate:
This patch introduces MDL_context::ACQUIRE_LOCK() — a set of overloaded methods (wrapped in a macro for FILE/LINE tracking) that combine the above steps into a single call returning MDL_ticket* directly:
This is a step toward making MDL_request a private implementation detail of the MDL subsystem, reducing its exposure to callers and preventing the class of bugs where stale or misused MDL_request objects cause issues (MDEV-39241, MDEV-39184).
Release Notes
N/A
How can this PR be tested?
Basing the PR against the correct MariaDB version
Copyright
All new code of the whole pull request, including one or several files that are either new files or modified ones, are contributed under the BSD-new license. I am contributing on behalf of my employer Amazon Web Services, Inc.