Skip to content

MDEV-39548 Further cleanup of MDL_request boilerplate - #5160

Merged
svoj merged 1 commit into
MariaDB:mainfrom
longjinvan:MDEV-39548
Jun 5, 2026
Merged

svoj merged 1 commit into
MariaDB:mainfrom
longjinvan:MDEV-39548

Conversation

@longjinvan

@longjinvan longjinvan commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

Description

This is a follow-up work to PR 5075 ("Cleanup MDL_request boilerplate with ACQUIRE_LOCK macro").

Change thd->backup_commit_lock from MDL_request* to MDL_ticket*

Previously, thd->backup_commit_lock stored a pointer to a stack-allocated MDL_request, creating dangling pointer risks if the function returned early or the code was refactored without clearing the pointer. This patch changes the type to MDL_ticket*, which is managed by the MDL subsystem and does not depend on the caller's stack frame. Related call sites in handler.cc, log.cc, sql_class.cc, and xa.cc are converted to use MDL_ACQUIRE_LOCK directly.

Convert binlog management functions to local MDL_ticket*

In reload_acl_and_cache() (sql_reload.cc), purge_master_logs() and reset_master() (sql_repl.cc), the original code stored the lock into thd->backup_commit_lock, but this field is only meaningful for the "wait for prior commit" routine and was not actually needed here. These functions are converted to use MDL_ACQUIRE_LOCK with a local MDL_ticket* instead.

Convert partition_info.cc to MDL_ACQUIRE_LOCK

Investigation confirmed that tl->mdl_request is not accessed by downstream code after lock acquisition -- only table->mdl_ticket is used. The conversion is safe.

Add MDL_REQUEST_LIST_ADD() helper

For batch lock acquisition via acquire_locks(), callers previously needed 3 steps: allocate an MDL_request on MEM_ROOT, initialize it, and push it into the list. The new MDL_REQUEST_LIST_ADD() macro combines these into a single call with built-in OOM checking. Call sites in sql_base.cc and sp.cc are converted.

Release Notes

N/A

How can this PR be tested?

  • All MTR tests pass, confirming no regressions introduced by this refactoring.

Basing the PR against the correct MariaDB version

  • This is a refactoring change, and the PR is based against the latest MariaDB development branch.

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors metadata locking (MDL) by replacing local MDL_request stack allocations and manual lock acquisitions with helper macros like MDL_ACQUIRE_LOCK and MDL_REQUEST_LIST_ADD, and changing backup_commit_lock to store an MDL_ticket pointer. The review feedback highlights a code smell in sql_show.cc where a sentinel pointer (MDL_ticket *) 1 is returned to indicate errors; the reviewer suggests returning a boolean instead, populating the ticket directly, and encapsulating key initialization within the helper function.

Comment thread sql/sql_show.cc Outdated
Comment thread sql/sql_show.cc Outdated
Comment thread sql/sql_show.cc Outdated
Comment thread sql/sql_repl.cc
@knielsen

knielsen commented Jun 1, 2026 via email

Copy link
Copy Markdown
Member

@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Jun 2, 2026

@gkodinov gkodinov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a preliminary review. Thank you for your contribution!

LGTM. Please stand by for Svoj's final review.

@svoj svoj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice, thanks! A few minor comments inline.

Comment thread sql/log.cc Outdated
Comment thread sql/sp.cc
Comment thread sql/sql_reload.cc Outdated
Comment thread sql/handler.cc
Comment thread sql/sql_reload.cc Outdated
Comment thread sql/sql_show.cc Outdated
@svoj

svoj commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

@knielsen

The intention here is to block RESET MASTER from running concurrently with
the stage in mariabackup that backs up the binlog files. Since it's somewhat
tricky to handle copying the binlog files if the server is removing them
concurrently, and quite unnecessary. BACKUP_START is a minimal lock that
doesn't block most server operations.

Sounds like MDL_BACKUP_START may indeed be suitable for this purpose. Though I'd probably add a new lock type specifically for it. Still, I guess we can live with this.

I don't see that in the piece of code you quoted? But no,
thd->backup_commit_lock doesn't sound like something that would be involved
here in reset_master().

All 3 occurrences in FLUSH BINARY LOGS, PURGE BINARY LOGS and RESET MASTER are having this pattern:

  MDL_request mdl_request;
  MDL_REQUEST_INIT(&mdl_request, MDL_key::BACKUP, "", "", MDL_BACKUP_START,
                   MDL_EXPLICIT);
  if (thd->mdl_context.acquire_lock(&mdl_request,
                                    thd->variables.lock_wait_timeout))
    return TRUE;
  thd->backup_commit_lock= &mdl_request;

FWICS it is used exclusively by the "wait for the prior commit" routine, which is irrelevant here, right?

@knielsen

knielsen commented Jun 3, 2026 via email

Copy link
Copy Markdown
Member

@svoj svoj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments inline.

Comment thread sql/sql_show.cc
Comment thread sql/handler.cc Outdated
- Change thd->backup_commit_lock from MDL_request* to MDL_ticket*,
  and convert related call sites in handler.cc, log.cc, sql_class.cc,
  and xa.cc to use MDL_ACQUIRE_LOCK.

- Convert reload_acl_and_cache() in sql_reload.cc, purge_master_logs()
  and reset_master() in sql_repl.cc to MDL_ACQUIRE_LOCK, holding the
  ticket in a local MDL_ticket* (these functions used a local
  MDL_request originally and should not touch thd->backup_commit_lock).

- Convert acquire_lock() in partition_info.cc to MDL_ACQUIRE_LOCK.

- Add MDL_REQUEST_LIST_ADD() helper for enqueuing lock requests into
  MDL_request_list, and convert call sites in sql_base.cc and sp.cc.

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.
@svoj
svoj enabled auto-merge (rebase) June 5, 2026 08:08
@svoj

svoj commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

@longjinvan thanks for your work! If you're interested to continue improving MDL I can suggest the following steps:

  1. replace MDL_request with MDL_key + MDL_ticket combo in Sroutine_hash_entry
  2. lock_schema_name() and lock_object_name() - lock directly without lists, use MDL_savepoint
  3. I found inconsistencies in compatibility matrices for backup namespace bitmaps. That is comment is not conforming to code bitmaps. Both granted and waiting matrices should be reviewed and fixed. Target version is 10.11. Matrices are in mdl.cc, look for MDL_lock::MDL_backup_lock::m_granted_incompatible.

@svoj
svoj merged commit f40ea8f into MariaDB:main Jun 5, 2026
16 of 19 checks passed
@svoj

svoj commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

@longjinvan just checking if you're still interested to continue working on these improvements. If not, I will take it over.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

4 participants