Skip to content

Commit 4730314

Browse files
committed
MDEV-28369 ibuf_bitmap_mutex is an unnecessary contention point
The only purpose of ibuf_bitmap_mutex is to prevent a deadlock between two concurrent invocations of ibuf_update_free_bits_for_two_pages_low() on the same pair of bitmap pages, but in opposite order. The mutex is unnecessarily serializing the execution of the function even when it is being invoked on totally different tablespaces. To avoid deadlocks, it suffices to ensure that the two page latches are being acquired in a deterministic (sorted) order.
1 parent 372b0e6 commit 4730314

File tree

6 files changed

+14
-50
lines changed

6 files changed

+14
-50
lines changed

storage/innobase/handler/ha_innodb.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,6 @@ static PSI_mutex_info all_innodb_mutexes[] = {
629629
PSI_KEY(fts_doc_id_mutex),
630630
PSI_KEY(log_flush_order_mutex),
631631
PSI_KEY(hash_table_mutex),
632-
PSI_KEY(ibuf_bitmap_mutex),
633632
PSI_KEY(ibuf_mutex),
634633
PSI_KEY(ibuf_pessimistic_insert_mutex),
635634
PSI_KEY(index_online_log),

storage/innobase/ibuf/ibuf0ibuf.cc

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*****************************************************************************
22
33
Copyright (c) 1997, 2016, Oracle and/or its affiliates. All Rights Reserved.
4-
Copyright (c) 2016, 2021, MariaDB Corporation.
4+
Copyright (c) 2016, 2022, MariaDB Corporation.
55
66
This program is free software; you can redistribute it and/or modify it under
77
the terms of the GNU General Public License as published by the Free Software
@@ -253,9 +253,6 @@ static ib_mutex_t ibuf_pessimistic_insert_mutex;
253253
/** The mutex protecting the insert buffer structs */
254254
static ib_mutex_tibuf_mutex;
255255

256-
/** The mutex protecting the insert buffer bitmaps */
257-
static ib_mutex_tibuf_bitmap_mutex;
258-
259256
/** The area in pages from which contract looks for page numbers for merge */
260257
const ulint IBUF_MERGE_AREA = 8;
261258

@@ -402,8 +399,6 @@ ibuf_close(void)
402399

403400
mutex_free(&ibuf_mutex);
404401

405-
mutex_free(&ibuf_bitmap_mutex);
406-
407402
dict_table_t* ibuf_table = ibuf->index->table;
408403
rw_lock_free(&ibuf->index->lock);
409404
dict_mem_index_free(ibuf->index);
@@ -459,8 +454,6 @@ ibuf_init_at_db_start(void)
459454

460455
mutex_create(LATCH_ID_IBUF, &ibuf_mutex);
461456

462-
mutex_create(LATCH_ID_IBUF_BITMAP, &ibuf_bitmap_mutex);
463-
464457
mutex_create(LATCH_ID_IBUF_PESSIMISTIC_INSERT,
465458
&ibuf_pessimistic_insert_mutex);
466459

@@ -1035,26 +1028,16 @@ ibuf_update_free_bits_for_two_pages_low(
10351028
buf_block_t* block2,/*!< in: index page */
10361029
mtr_t* mtr)/*!< in: mtr */
10371030
{
1038-
ulint state;
1039-
1040-
ut_ad(mtr->is_named_space(block1->page.id.space()));
1041-
ut_ad(block1->page.id.space() == block2->page.id.space());
1042-
1043-
/* As we have to x-latch two random bitmap pages, we have to acquire
1044-
the bitmap mutex to prevent a deadlock with a similar operation
1045-
performed by another OS thread. */
1046-
1047-
mutex_enter(&ibuf_bitmap_mutex);
1048-
1049-
state = ibuf_index_page_calc_free(block1);
1050-
1051-
ibuf_set_free_bits_low(block1, state, mtr);
1052-
1053-
state = ibuf_index_page_calc_free(block2);
1031+
ut_ad(mtr->is_named_space(block1->page.id.space()));
1032+
ut_ad(block1->page.id.space() == block2->page.id.space());
10541033

1055-
ibuf_set_free_bits_low(block2, state, mtr);
1034+
/* Avoid deadlocks by acquiring multiple bitmap page latches in
1035+
a consistent order (smaller pointer first). */
1036+
if (block1 > block2)
1037+
std::swap(block1, block2);
10561038

1057-
mutex_exit(&ibuf_bitmap_mutex);
1039+
ibuf_set_free_bits_low(block1, ibuf_index_page_calc_free(block1), mtr);
1040+
ibuf_set_free_bits_low(block2, ibuf_index_page_calc_free(block2), mtr);
10581041
}
10591042

10601043
/** Returns TRUE if the page is one of the fixed address ibuf pages.

storage/innobase/include/sync0sync.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
44
Copyright (c) 2008, Google Inc.
55
Copyright (c) 2012, Facebook Inc.
6-
Copyright (c) 2020, MariaDB Corporation.
6+
Copyright (c) 2020, 2022, MariaDB Corporation.
77
88
Portions of this file contain modifications contributed and copyrighted by
99
Google, Inc. Those modifications are gratefully acknowledged and are described
@@ -63,7 +63,6 @@ extern mysql_pfs_key_t fts_delete_mutex_key;
6363
extern mysql_pfs_key_tfts_doc_id_mutex_key;
6464
extern mysql_pfs_key_tfts_pll_tokenize_mutex_key;
6565
extern mysql_pfs_key_thash_table_mutex_key;
66-
extern mysql_pfs_key_tibuf_bitmap_mutex_key;
6766
extern mysql_pfs_key_tibuf_mutex_key;
6867
extern mysql_pfs_key_tibuf_pessimistic_insert_mutex_key;
6968
extern mysql_pfs_key_tlog_sys_mutex_key;

storage/innobase/include/sync0types.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*****************************************************************************
22
33
Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
4-
Copyright (c) 2017, 2020, MariaDB Corporation.
4+
Copyright (c) 2017, 2022, MariaDB Corporation.
55
66
This program is free software; you can redistribute it and/or modify it under
77
the terms of the GNU General Public License as published by the Free Software
@@ -227,7 +227,6 @@ enum latch_level_t {
227227
SYNC_INDEX_ONLINE_LOG,
228228

229229
SYNC_IBUF_BITMAP,
230-
SYNC_IBUF_BITMAP_MUTEX,
231230
SYNC_IBUF_TREE_NODE,
232231
SYNC_IBUF_TREE_NODE_NEW,
233232
SYNC_IBUF_INDEX_TREE,
@@ -295,7 +294,6 @@ enum latch_id_t {
295294
LATCH_ID_FTS_DOC_ID,
296295
LATCH_ID_FTS_PLL_TOKENIZE,
297296
LATCH_ID_HASH_TABLE_MUTEX,
298-
LATCH_ID_IBUF_BITMAP,
299297
LATCH_ID_IBUF,
300298
LATCH_ID_IBUF_PESSIMISTIC_INSERT,
301299
LATCH_ID_LOG_SYS,

storage/innobase/sync/sync0debug.cc

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,6 @@ LatchDebug::LatchDebug()
473473
LEVEL_MAP_INSERT(SYNC_LOCK_WAIT_SYS);
474474
LEVEL_MAP_INSERT(SYNC_INDEX_ONLINE_LOG);
475475
LEVEL_MAP_INSERT(SYNC_IBUF_BITMAP);
476-
LEVEL_MAP_INSERT(SYNC_IBUF_BITMAP_MUTEX);
477476
LEVEL_MAP_INSERT(SYNC_IBUF_TREE_NODE);
478477
LEVEL_MAP_INSERT(SYNC_IBUF_TREE_NODE_NEW);
479478
LEVEL_MAP_INSERT(SYNC_IBUF_INDEX_TREE);
@@ -749,7 +748,6 @@ LatchDebug::check_order(
749748
case SYNC_LOCK_WAIT_SYS:
750749
case SYNC_RW_TRX_HASH_ELEMENT:
751750
case SYNC_TRX_SYS:
752-
case SYNC_IBUF_BITMAP_MUTEX:
753751
case SYNC_REDO_RSEG:
754752
case SYNC_NOREDO_RSEG:
755753
case SYNC_PURGE_LATCH:
@@ -834,22 +832,13 @@ LatchDebug::check_order(
834832
break;
835833

836834
case SYNC_IBUF_BITMAP:
837-
838-
/* Either the thread must own the master mutex to all
839-
the bitmap pages, or it is allowed to latch only ONE
840-
bitmap page. */
841-
842-
if (find(latches, SYNC_IBUF_BITMAP_MUTEX) != 0) {
843-
844-
basic_check(latches, level, SYNC_IBUF_BITMAP - 1);
845-
846-
} else if (!srv_is_being_started) {
835+
if (!srv_is_being_started) {
847836

848837
/* This is violated during trx_sys_create_rsegs()
849838
when creating additional rollback segments during
850839
upgrade. */
851840

852-
basic_check(latches, level, SYNC_IBUF_BITMAP);
841+
basic_check(latches, level, SYNC_IBUF_BITMAP - 1);
853842
}
854843
break;
855844

@@ -1291,9 +1280,6 @@ sync_latch_meta_init()
12911280
LATCH_ADD_MUTEX(HASH_TABLE_MUTEX, SYNC_BUF_PAGE_HASH,
12921281
hash_table_mutex_key);
12931282

1294-
LATCH_ADD_MUTEX(IBUF_BITMAP, SYNC_IBUF_BITMAP_MUTEX,
1295-
ibuf_bitmap_mutex_key);
1296-
12971283
LATCH_ADD_MUTEX(IBUF, SYNC_IBUF_MUTEX, ibuf_mutex_key);
12981284

12991285
LATCH_ADD_MUTEX(IBUF_PESSIMISTIC_INSERT, SYNC_IBUF_PESS_INSERT_MUTEX,

storage/innobase/sync/sync0sync.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
44
Copyright (c) 2008, Google Inc.
5-
Copyright (c) 2020, MariaDB Corporation.
5+
Copyright (c) 2020, 2022, MariaDB Corporation.
66
77
Portions of this file contain modifications contributed and copyrighted by
88
Google, Inc. Those modifications are gratefully acknowledged and are described
@@ -49,7 +49,6 @@ mysql_pfs_key_t fts_delete_mutex_key;
4949
mysql_pfs_key_tfts_doc_id_mutex_key;
5050
mysql_pfs_key_tfts_pll_tokenize_mutex_key;
5151
mysql_pfs_key_thash_table_mutex_key;
52-
mysql_pfs_key_tibuf_bitmap_mutex_key;
5352
mysql_pfs_key_tibuf_mutex_key;
5453
mysql_pfs_key_tibuf_pessimistic_insert_mutex_key;
5554
mysql_pfs_key_tlog_sys_mutex_key;

0 commit comments

Comments
 (0)