With a simple Sysbench select_random_ranges workload on 1 table, 1 connection and 10000 rows, the following code is being exercised very frequently:
diff --git a/storage/innobase/buf/buf0buf.cc b/storage/innobase/buf/buf0buf.cc
|
index 44b093e93ed..ab720dc128f 100644
|
--- a/storage/innobase/buf/buf0buf.cc
|
+++ b/storage/innobase/buf/buf0buf.cc
|
@@ -2849,6 +2849,7 @@ buf_page_get_low(
|
&& fil_page_get_type(block->page.frame) == FIL_PAGE_INDEX
|
&& page_is_leaf(block->page.frame)) {
|
block->page.lock.x_lock();
|
+ sql_print_warning("InnoDB: x_locked %p", block);
|
state = block->page.state();
|
ut_ad(state < buf_page_t::READ_FIX);
|
|
Here is the corresponding patch for the 10.5 branch:
diff --git a/storage/innobase/buf/buf0buf.cc b/storage/innobase/buf/buf0buf.cc
|
index f37a96d5a7c..7b8aa0934d2 100644
|
--- a/storage/innobase/buf/buf0buf.cc
|
+++ b/storage/innobase/buf/buf0buf.cc
|
@@ -3198,6 +3198,7 @@ buf_page_get_low(
|
&& fil_page_get_type(fix_block->frame) == FIL_PAGE_INDEX
|
&& page_is_leaf(fix_block->frame)) {
|
rw_lock_x_lock_inline(&fix_block->lock, 0, file, line);
|
+ sql_print_warning("InnoDB: x_locked %p", block);
|
|
if (fix_block->page.ibuf_exist) {
|
fix_block->page.ibuf_exist = false;
|
When using the oltp_read_only workload, the above would only be covered only during sysbench prepare.
The purpose of the above code is to apply buffered changes to a secondary index leaf page if such changes exist. This logic was originally implemented in MDEV-19514. Note: the change buffer was removed in MariaDB Server 11.0 in MDEV-29694, and therefore 11.x is not affected by this.
The problem that is exposed by the select_random_ranges workload is that even though a shared latch was requested, we are first acquiring an exclusive latch. This could be improved in a number of ways:
- If a buffer-fix is being requested (rw_latch=RW_NO_LATCH), skip this logic. This mode should not be used together with allow_ibuf_merge=true.
- Acquire the latch that was requested, and if necessary, temporarily upgrade it to exclusive so that the buffered changes can be merged. Upgrading a latch used to be deadlock-prone before the locks were refactored in
MDEV-24142 (MariaDB Server 10.6). After the refinement in MDEV-34178, I think the needed upgrade step is trivial to implement.
- If ibuf.empty holds (the change buffer was found to be empty), it does not make any sense to try a change buffer merge.
{"report":{"fcp":801.2000000476837,"ttfb":191.09999990463257,"pageVisibility":"visible","entityId":130350,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"9ee51ecb-4f6e-4cc9-b8a8-e5c0c25f93b7","navigationType":0,"readyForUser":884.4000000953674,"redirectCount":0,"resourceLoadedEnd":521.9000000953674,"resourceLoadedStart":196.90000009536743,"resourceTiming":[{"duration":64,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":196.90000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":196.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":260.90000009536743,"responseStart":0,"secureConnectionStart":0},{"duration":64.59999990463257,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2bu7/820016/12ta74/8679b4946efa1a0bb029a3a22206fb5d/_/download/contextbatch/css/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true","startTime":197.20000004768372,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":197.20000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":261.7999999523163,"responseStart":0,"secureConnectionStart":0},{"duration":218.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/fbf975c0cce4b1abf04784eeae9ba1f4-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":197.29999995231628,"connectEnd":197.29999995231628,"connectStart":197.29999995231628,"domainLookupEnd":197.29999995231628,"domainLookupStart":197.29999995231628,"fetchStart":197.29999995231628,"redirectEnd":0,"redirectStart":0,"requestStart":265.2999999523163,"responseEnd":415.7999999523163,"responseStart":286,"secureConnectionStart":197.29999995231628},{"duration":324.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/099b33461394b8015fc36c0a4b96e19f-CDN/lu2bu7/820016/12ta74/8679b4946efa1a0bb029a3a22206fb5d/_/download/contextbatch/js/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true","startTime":197.40000009536743,"connectEnd":197.40000009536743,"connectStart":197.40000009536743,"domainLookupEnd":197.40000009536743,"domainLookupStart":197.40000009536743,"fetchStart":197.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":266.40000009536743,"responseEnd":521.9000000953674,"responseStart":319.09999990463257,"secureConnectionStart":197.40000009536743},{"duration":93.70000004768372,"initiatorType":"script","name":"https://jira.mariadb.org/s/94c15bff32baef80f4096a08aceae8bc-CDN/lu2bu7/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":197.5,"connectEnd":197.5,"connectStart":197.5,"domainLookupEnd":197.5,"domainLookupStart":197.5,"fetchStart":197.5,"redirectEnd":0,"redirectStart":0,"requestStart":268.09999990463257,"responseEnd":291.2000000476837,"responseStart":290.2999999523163,"secureConnectionStart":197.5},{"duration":96,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":197.59999990463257,"connectEnd":197.59999990463257,"connectStart":197.59999990463257,"domainLookupEnd":197.59999990463257,"domainLookupStart":197.59999990463257,"fetchStart":197.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":268.7000000476837,"responseEnd":293.59999990463257,"responseStart":291.5,"secureConnectionStart":197.59999990463257},{"duration":69,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2bu7/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":197.70000004768372,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":197.70000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":266.7000000476837,"responseStart":0,"secureConnectionStart":0},{"duration":96.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":197.70000004768372,"connectEnd":197.70000004768372,"connectStart":197.70000004768372,"domainLookupEnd":197.70000004768372,"domainLookupStart":197.70000004768372,"fetchStart":197.70000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":269.59999990463257,"responseEnd":293.7999999523163,"responseStart":291.90000009536743,"secureConnectionStart":197.70000004768372},{"duration":70.39999985694885,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2bu7/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/css/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.css?jira.create.linked.issue=true","startTime":197.90000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":197.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":268.2999999523163,"responseStart":0,"secureConnectionStart":0},{"duration":97.79999995231628,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":197.90000009536743,"connectEnd":197.90000009536743,"connectStart":197.90000009536743,"domainLookupEnd":197.90000009536743,"domainLookupStart":197.90000009536743,"fetchStart":197.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":270.7999999523163,"responseEnd":295.7000000476837,"responseStart":294.09999990463257,"secureConnectionStart":197.90000009536743},{"duration":101.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/3339d87fa2538a859872f2df449bf8d0-CDN/lu2bu7/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/js/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.js?jira.create.linked.issue=true&locale=en","startTime":198.09999990463257,"connectEnd":198.09999990463257,"connectStart":198.09999990463257,"domainLookupEnd":198.09999990463257,"domainLookupStart":198.09999990463257,"fetchStart":198.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":271.59999990463257,"responseEnd":300,"responseStart":294.7000000476837,"secureConnectionStart":198.09999990463257},{"duration":291.60000014305115,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":199.29999995231628,"connectEnd":199.29999995231628,"connectStart":199.29999995231628,"domainLookupEnd":199.29999995231628,"domainLookupStart":199.29999995231628,"fetchStart":199.29999995231628,"redirectEnd":0,"redirectStart":0,"requestStart":299.7000000476837,"responseEnd":490.90000009536743,"responseStart":487.59999990463257,"secureConnectionStart":199.29999995231628},{"duration":299.10000014305115,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":199.29999995231628,"connectEnd":199.29999995231628,"connectStart":199.29999995231628,"domainLookupEnd":199.29999995231628,"domainLookupStart":199.29999995231628,"fetchStart":199.29999995231628,"redirectEnd":0,"redirectStart":0,"requestStart":353.09999990463257,"responseEnd":498.40000009536743,"responseStart":495.90000009536743,"secureConnectionStart":199.29999995231628},{"duration":154,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":560,"connectEnd":560,"connectStart":560,"domainLookupEnd":560,"domainLookupStart":560,"fetchStart":560,"redirectEnd":0,"redirectStart":0,"requestStart":682.5999999046326,"responseEnd":714,"responseStart":713,"secureConnectionStart":560}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":15,"responseStart":191,"responseEnd":194,"domLoading":194,"domInteractive":996,"domContentLoadedEventStart":996,"domContentLoadedEventEnd":1042,"domComplete":1598,"loadEventStart":1598,"loadEventEnd":1598,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":960.5999999046326},{"name":"bigPipe.sidebar-id.end","time":961.4000000953674},{"name":"bigPipe.activity-panel-pipe-id.start","time":961.5999999046326},{"name":"bigPipe.activity-panel-pipe-id.end","time":964.0999999046326},{"name":"activityTabFullyLoaded","time":1060.4000000953674}],"measures":[],"correlationId":"1d5599c19412f9","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":103,"dbReadsTimeInMs":24,"dbConnsTimeInMs":34,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
I realized that a deadlock on an upgrade from shared to exclusive latch is possible if another thread is already waiting for an exclusive latch. After
MDEV-24142andMDEV-34178, an exclusive latch would be acquired as follows:When we upgrade a latch from shared, we’d be holding one share of readers. The next logical step would be to acquire the writer mutex and then invoke u_wr_upgrade() to update the nonzero readers word to WRITER. If we wait for the writer mutex while holding a readers reference, we will easily get into deadlock with other threads.
The solution is to employ a trylock operation: If the writer mutex cannot be acquired without waiting, we’d have to release the shared latch and acquire an exclusive one normally.