Skip to content

Commit

Permalink
FB8-84: Add global variable max_nonsuper_connections (facebook#950) (
Browse files Browse the repository at this point in the history
…facebook#950)

Summary:
JIRA: https://jira.percona.com/browse/FB8-84

Reference Patch: facebook@478fa2a
Reference Patch: facebook@8a269ee4f97
Reference Patch: facebook@c53bf248a27
Reference Patch: facebook@ae0d38ae0c8

The new global variable `max_nonsuper_connections` will enforce the limit for
the sum of all non-admin connections. This will be useful to limit regular user
connections while still allow super users to connect to server.

Pull Request resolved: facebook#950

Reviewed By: lloyd

Differential Revision: D13987618

Pulled By: lth
  • Loading branch information
tianx authored and inikep committed Jul 31, 2024
1 parent ed33493 commit acffc2c
Show file tree
Hide file tree
Showing 16 changed files with 560 additions and 3 deletions.
113 changes: 113 additions & 0 deletions mysql-test/r/max_nonsuper_connections.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
create user test_user@localhost;
grant all on test to test_user@localhost;
create user super_user@localhost;
grant all on *.* to super_user@localhost with grant option;
SET @start_value = @@global.max_nonsuper_connections;
SET @@global.max_nonsuper_connections = 10;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
10
include/assert.inc [Non_super_connections should be 0]
connection default;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
ERROR 08004: Too many connections
connect con_root, localhost, root,,test;
# connection con_root
connection con_root;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
10
include/assert.inc [Non_super_connections should be 10]
connection con_root;
connection con_root;
disconnect con_root;
connection default;
connect con_super, localhost, super_user,,test;
connection con_super;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
10
include/assert.inc [Non_super_connections should be 10]
connection con_super;
connection con_super;
mysqltest: At line 1: Query 'change_user test_user' failed.
ERROR 1040 (08004): Too many connections
disconnect con_super;
connection con10;
connect con11, localhost, test_user,,test;
disconnect con11;
connection con10;
ERROR 08004: Too many connections
connection default;
disconnect con10;
disconnect con9;
disconnect con8;
disconnect con7;
disconnect con6;
disconnect con5;
disconnect con4;
disconnect con3;
disconnect con2;
disconnect con1;
Verifying nonsuper_connections is decremented when db is denied
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
ERROR 42000: Access denied for user 'test_user'@'localhost' to database 'bogus_db'
connection default;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
ERROR 08004: Too many connections
connection default;
SET @@global.max_nonsuper_connections = @start_value;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
0
include/assert.inc [Non_super_connections should be 10]
connection default;
connection default;
drop user test_user@localhost;
drop user super_user@localhost;
disconnect con10;
disconnect con9;
disconnect con8;
disconnect con7;
disconnect con6;
disconnect con5;
disconnect con4;
disconnect con3;
disconnect con2;
disconnect con1;
4 changes: 4 additions & 0 deletions mysql-test/r/mysqld--help-notwin.result
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,9 @@ The following options may be given as the first argument:
--max-length-for-sort-data=#
This variable is deprecated and will be removed in a
future release.
--max-nonsuper-connections=#
The maximum number of total active connections for
non-super user (0 = no limit)
--max-points-in-geometry[=#]
Maximum number of points in a geometry
--max-prepared-stmt-count=#
Expand Down Expand Up @@ -2528,6 +2531,7 @@ max-execution-time 0
max-heap-table-size 16777216
max-join-size 18446744073709551615
max-length-for-sort-data 4096
max-nonsuper-connections 0
max-points-in-geometry 65536
max-prepared-stmt-count 16382
max-relay-log-size 0
Expand Down
100 changes: 100 additions & 0 deletions mysql-test/suite/sys_vars/r/max_nonsuper_connections_basic.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
SET @start_value = @@global.max_nonsuper_connections;
SELECT @start_value;
@start_value
0
SET @@global.max_nonsuper_connections = DEFAULT;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
0
SET @@global.max_nonsuper_connections = 100000;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
100000
SET @@global.max_nonsuper_connections = 99999;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
99999
SET @@global.max_nonsuper_connections = 65536;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
65536
SET @@global.max_nonsuper_connections = 1;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
1
SET @@global.max_nonsuper_connections = 2;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
2
SET @@global.max_nonsuper_connections = TRUE;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
1
SET @@global.max_nonsuper_connections = FALSE;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
0
SET @@global.max_nonsuper_connections = -1;
Warnings:
Warning 1292 Truncated incorrect max_nonsuper_connections value: '-1'
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
0
SET @@global.max_nonsuper_connections = 100000000000;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
100000000000
SET @@global.max_nonsuper_connections = 10000.01;
ERROR 42000: Incorrect argument type to variable 'max_nonsuper_connections'
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
100000000000
SET @@global.max_nonsuper_connections = -1024;
Warnings:
Warning 1292 Truncated incorrect max_nonsuper_connections value: '-1024'
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
0
SET @@global.max_nonsuper_connections = ON;
ERROR 42000: Incorrect argument type to variable 'max_nonsuper_connections'
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
0
SET @@global.max_nonsuper_connections = 'test';
ERROR 42000: Incorrect argument type to variable 'max_nonsuper_connections'
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
0
SET @@session.max_nonsuper_connections = 4096;
ERROR HY000: Variable 'max_nonsuper_connections' is a GLOBAL variable and should be set with SET GLOBAL
SELECT @@session.max_nonsuper_connections;
ERROR HY000: Variable 'max_nonsuper_connections' is a GLOBAL variable
SET max_nonsuper_connections = 6000;
ERROR HY000: Variable 'max_nonsuper_connections' is a GLOBAL variable and should be set with SET GLOBAL
SELECT @@max_nonsuper_connections;
@@max_nonsuper_connections
0
SET local.max_nonsuper_connections = 7000;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'local.max_nonsuper_connections = 7000' at line 1
SELECT local.max_nonsuper_connections;
ERROR 42S02: Unknown table 'local' in field list
SET global.max_nonsuper_connections = 8000;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'global.max_nonsuper_connections = 8000' at line 1
SELECT global.max_nonsuper_connections;
ERROR 42S02: Unknown table 'global' in field list
SELECT max_nonsuper_connections = @@session.max_nonsuper_connections;
ERROR HY000: Variable 'max_nonsuper_connections' is a GLOBAL variable
SELECT @@global.max_nonsuper_connections = VARIABLE_VALUE
FROM performance_schema.global_variables
WHERE VARIABLE_NAME='max_nonsuper_connections';
@@global.max_nonsuper_connections = VARIABLE_VALUE
1
SELECT @@max_nonsuper_connections = VARIABLE_VALUE
FROM performance_schema.session_variables
WHERE VARIABLE_NAME='max_nonsuper_connections';
@@max_nonsuper_connections = VARIABLE_VALUE
1
SET @@global.max_nonsuper_connections = @start_value;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
0
95 changes: 95 additions & 0 deletions mysql-test/suite/sys_vars/t/max_nonsuper_connections_basic.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
--source include/load_sysvars.inc

#
# save original value
#
SET @start_value = @@global.max_nonsuper_connections;
SELECT @start_value;


#
# set default value
#
SET @@global.max_nonsuper_connections = DEFAULT;
SELECT @@global.max_nonsuper_connections;


#
# set various values
#
SET @@global.max_nonsuper_connections = 100000;
SELECT @@global.max_nonsuper_connections;
SET @@global.max_nonsuper_connections = 99999;
SELECT @@global.max_nonsuper_connections;
SET @@global.max_nonsuper_connections = 65536;
SELECT @@global.max_nonsuper_connections;
SET @@global.max_nonsuper_connections = 1;
SELECT @@global.max_nonsuper_connections;
SET @@global.max_nonsuper_connections = 2;
SELECT @@global.max_nonsuper_connections;
SET @@global.max_nonsuper_connections = TRUE;
SELECT @@global.max_nonsuper_connections;
SET @@global.max_nonsuper_connections = FALSE;
SELECT @@global.max_nonsuper_connections;


#
# set invalid values
#
# Value truncated
SET @@global.max_nonsuper_connections = -1;
SELECT @@global.max_nonsuper_connections;
# Value truncated
SET @@global.max_nonsuper_connections = 100000000000;
SELECT @@global.max_nonsuper_connections;
--Error ER_WRONG_TYPE_FOR_VAR
SET @@global.max_nonsuper_connections = 10000.01;
SELECT @@global.max_nonsuper_connections;
# Value truncated
SET @@global.max_nonsuper_connections = -1024;
SELECT @@global.max_nonsuper_connections;

--Error ER_WRONG_TYPE_FOR_VAR
SET @@global.max_nonsuper_connections = ON;
SELECT @@global.max_nonsuper_connections;
--Error ER_WRONG_TYPE_FOR_VAR
SET @@global.max_nonsuper_connections = 'test';
SELECT @@global.max_nonsuper_connections;

--Error ER_GLOBAL_VARIABLE
SET @@session.max_nonsuper_connections = 4096;
--Error ER_INCORRECT_GLOBAL_LOCAL_VAR
SELECT @@session.max_nonsuper_connections;

--Error ER_GLOBAL_VARIABLE
SET max_nonsuper_connections = 6000;
SELECT @@max_nonsuper_connections;
--Error ER_PARSE_ERROR
SET local.max_nonsuper_connections = 7000;
--Error ER_UNKNOWN_TABLE
SELECT local.max_nonsuper_connections;
--Error ER_PARSE_ERROR
SET global.max_nonsuper_connections = 8000;
--Error ER_UNKNOWN_TABLE
SELECT global.max_nonsuper_connections;
--Error ER_INCORRECT_GLOBAL_LOCAL_VAR
SELECT max_nonsuper_connections = @@session.max_nonsuper_connections;


#
# Check if the value in GLOBAL & SESSION Tables matches values in variable
#
SELECT @@global.max_nonsuper_connections = VARIABLE_VALUE
FROM performance_schema.global_variables
WHERE VARIABLE_NAME='max_nonsuper_connections';

SELECT @@max_nonsuper_connections = VARIABLE_VALUE
FROM performance_schema.session_variables
WHERE VARIABLE_NAME='max_nonsuper_connections';


#
# restore
#
SET @@global.max_nonsuper_connections = @start_value;
SELECT @@global.max_nonsuper_connections;
Loading

0 comments on commit acffc2c

Please sign in to comment.