forked from facebook/mysql-5.6
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix contention in MDL_key::ACL_CACHE
Summary: In 5.6 acl cache is protected by mutex which causes quite a bit of contention (especially if you have lots of acls), so we changed that to a mysql_rwlock_t in 5.6. In 8.0 this is replaced with a singleton MDL lock with namespace = MDL_key::ACL_CACHE with SHARED and EXCLUSIVE lock. However MDL lock has higher overhead than regular mysql_rwlock_t due to the way it maintains the tickets for granting/waiting and another internal mysql_prlock_rwlock (MDL_lock::m_rwlock) to update/traverse them, and therefore is more prone to contentions in high connection scenarios. In this particular case, the deadlock detection for ACL_CACHE leads to bad contention problems in the connection path, because taking rdlock on MDL_lock::m_rwlock while doing deadlock traversal takes longer with more granted and waiting tickets, but the releasing of these granted tickets are themselves blocked on taking wrlock on m_rwlock, meanwhile more connections are trying to come in and grant more tickets and taking rdlock as well in deadlock detection as well, so this becomes a serious lock contention that you can't get out of if you keep new connection come in at a fast pace. A easy way to trigger this is to run FLUSH PRIVILEGES on a server that has lots of incoming connections (with low connection timeout) with lots of connection from acl_check_host / has_global_grant. For ACL_CACHE, the deadlock detection in most cases doesn't provide a ton of value in production, because under ACL_cache lock we only operate on mysql.user/db/etc special system tables, so you'd need to take exclusive lock on them and then go through acl cache locking in order to trigger the deadlock detection, which seems quite unlikely in practice. We didn't see any issues in the past for 5.6 either, which was just a mutex and later we changed it to mysql_rwlock_t and had 0 deadlock detection. Therefore, I'm adding a new switch enable_acl_cache_deadlock_detection to turn it off for ACL_CACHE only. It defaults to on to maintain compat and we can turn it off in production. There are probably other options to fix this issue short term (such as replacing the ACL_cache lock with mysql_rwlock_t lock, moving allow_all_hosts check out of the lock, etc) with different risk trade offs, and longer term fixes that require bigger surgery to MDL locking system which probably should best left to upstream. This seems to be the lowest risk option at the moment. `#ifndef EXTRA_CODE_FOR_UNIT_TESTING` is needed to avoid compilation failure in locking unit tests (which doesn't seem to compile in the sys vars). Reviewed By: yoshinorim Differential Revision: D27740302
- Loading branch information
Showing
8 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
mysql-test/suite/sys_vars/r/enable_acl_cache_deadlock_detection_basic.result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
SET @start_global_value = @@GLOBAL.enable_acl_cache_deadlock_detection; | ||
SELECT @start_global_value; | ||
@start_global_value | ||
1 | ||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = on; | ||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = DEFAULT; | ||
select @@GLOBAL.enable_acl_cache_deadlock_detection; | ||
@@GLOBAL.enable_acl_cache_deadlock_detection | ||
1 | ||
FLUSH PRIVILEGES; | ||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = off; | ||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = DEFAULT; | ||
select @@GLOBAL.enable_acl_cache_deadlock_detection; | ||
@@GLOBAL.enable_acl_cache_deadlock_detection | ||
1 | ||
FLUSH PRIVILEGES; | ||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = on; | ||
select @@GLOBAL.enable_acl_cache_deadlock_detection; | ||
@@GLOBAL.enable_acl_cache_deadlock_detection | ||
1 | ||
FLUSH PRIVILEGES; | ||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = off; | ||
select @@GLOBAL.enable_acl_cache_deadlock_detection; | ||
@@GLOBAL.enable_acl_cache_deadlock_detection | ||
0 | ||
FLUSH PRIVILEGES; | ||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = something; | ||
ERROR 42000: Variable 'enable_acl_cache_deadlock_detection' can't be set to the value of 'something' | ||
select @@GLOBAL.enable_acl_cache_deadlock_detection; | ||
@@GLOBAL.enable_acl_cache_deadlock_detection | ||
0 | ||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = somethingelse; | ||
ERROR 42000: Variable 'enable_acl_cache_deadlock_detection' can't be set to the value of 'somethingelse' | ||
select @@GLOBAL.enable_acl_cache_deadlock_detection; | ||
@@GLOBAL.enable_acl_cache_deadlock_detection | ||
0 | ||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = @start_global_value; | ||
SELECT @@GLOBAL.enable_acl_cache_deadlock_detection; | ||
@@GLOBAL.enable_acl_cache_deadlock_detection | ||
1 |
75 changes: 75 additions & 0 deletions
75
mysql-test/suite/sys_vars/t/enable_acl_cache_deadlock_detection_basic.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
################ mysql-test\t\enable_acl_cache_deadlock_detection.test ######## | ||
# # | ||
# Variable Name: enable_acl_cache_deadlock_detection # | ||
# Scope: Global # | ||
# # | ||
# Creation Date: 2021-04-13 # | ||
# # | ||
# # | ||
# Description:Test Cases of Dynamic System Variable # | ||
# enable_acl_cache_deadlock_detection that checks the behavior of # | ||
# this variable in the following ways # | ||
# * Default Value # | ||
# * Valid Value # | ||
# * Invalid value # | ||
# # | ||
############################################################################### | ||
SET @start_global_value = @@GLOBAL.enable_acl_cache_deadlock_detection; | ||
SELECT @start_global_value; | ||
|
||
######################################################################## | ||
# Display the DEFAULT value of enable_acl_cache_deadlock_detection # | ||
######################################################################## | ||
|
||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = on; | ||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = DEFAULT; | ||
select @@GLOBAL.enable_acl_cache_deadlock_detection; | ||
|
||
# FLUSH PRIVILEGES not required but just a quick check to see acl cache | ||
# is working properly | ||
FLUSH PRIVILEGES; | ||
|
||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = off; | ||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = DEFAULT; | ||
select @@GLOBAL.enable_acl_cache_deadlock_detection; | ||
|
||
# FLUSH PRIVILEGES not required but just a quick check to see acl cache | ||
# is working properly | ||
FLUSH PRIVILEGES; | ||
|
||
############################################################################### | ||
# change the value of enable_acl_cache_deadlock_detection to a valid value # | ||
############################################################################### | ||
|
||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = on; | ||
select @@GLOBAL.enable_acl_cache_deadlock_detection; | ||
|
||
# FLUSH PRIVILEGES not required but just a quick check to see acl cache | ||
# is working properly | ||
FLUSH PRIVILEGES; | ||
|
||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = off; | ||
select @@GLOBAL.enable_acl_cache_deadlock_detection; | ||
|
||
# FLUSH PRIVILEGES not required but just a quick check to see acl cache | ||
# is working properly | ||
FLUSH PRIVILEGES; | ||
|
||
############################################################################### | ||
# change the value of enable_acl_cache_deadlock_detection to an invalid value # | ||
############################################################################### | ||
|
||
--Error ER_WRONG_VALUE_FOR_VAR | ||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = something; | ||
select @@GLOBAL.enable_acl_cache_deadlock_detection; | ||
|
||
--Error ER_WRONG_VALUE_FOR_VAR | ||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = somethingelse; | ||
select @@GLOBAL.enable_acl_cache_deadlock_detection; | ||
|
||
############################### | ||
# Restore initial value # | ||
############################### | ||
|
||
SET @@GLOBAL.enable_acl_cache_deadlock_detection = @start_global_value; | ||
SELECT @@GLOBAL.enable_acl_cache_deadlock_detection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters