Dry | RDS MySQL 8.0 Analysis of statement-level concurrency control

RDS MySQL 8.0 statement-level concurrency control

background

In order to deal with unexpected database requests flow, excessive consumption of resources statement to access the upload, SQL access model changes, and maintain MySQL examples of sustained and stable operation, AliSQL version designed based concurrency control statement rules, Statement Concurrency Control, hereinafter referred to as CCL, effective concurrency control certain rules matching, and provides a set of toolkit (DBMS_CCL package) convenient use.

Design rules

CCL rule has defined three characteristic dimensions:
. 1) the SQL Command
The statement of the type, for example, 'the SELECT', 'the UPDATE', 'the INSERT', 'the DELETE';
2) Object
control operation according to the object of the statement, for example, TABLE , VIEW;
3) keywords
control statement keyword statement;

CCL definition rules, designed a system table, mysql.concurrency_control persistence save CCL rule:

Concurrency_control

CREATE TABLE `concurrency_control` (
  `Id` bigint(20) NOT NULL AUTO_INCREMENT,
  `Type` enum('SELECT','UPDATE','INSERT','DELETE') NOT NULL DEFAULT 'SELECT',
  `Schema_name` varchar(64) COLLATE utf8_bin DEFAULT NULL,
  `Table_name` varchar(64) COLLATE utf8_bin DEFAULT NULL,
  `Concurrency_count` bigint(20) DEFAULT NULL,
  `Keywords` text COLLATE utf8_bin,
  `State` enum('N','Y') NOT NULL DEFAULT 'Y',
  `Ordered` enum('N','Y') NOT NULL DEFAULT 'N',
  PRIMARY KEY (`Id`)
) /*!50100 TABLESPACE `mysql` */ ENGINE=InnoDB 
DEFAULT CHARSET=utf8 COLLATE=utf8_bin
STATS_PERSISTENT=0 COMMENT='Concurrency control'

COLUMNS

  • "Type"

    • Used to define the SQL command
  • "Schema_name" && "Table_name"

    • Used to define the Object
  • "Keywords"

    • It is used to define a keyword, use ';' separator plurality of keywords
  • "Concurrency_count"

    • Used to define the degree of concurrency
  • "State"

    • This rule represents whether active
  • "Ordered"

    • It indicates whether the keywords in order to match multiple keywords

The user can directly operate the table to define the rules, you may be used to operate DBMS_CCL kit CCL rule.

Management interface

For convenient management CCL rule, AliSQL defines four native procedure in DBMS_CCL package to manage;

1)Add CCL rule
dbms_ccl.add_ccl_rule(type=>, schema=>, table=>, Concurrency_count=>, keywords=>);

Add rules (including tables and memory), for example:

1. 增加 SELECT 语句的并发度为 10;
    mysql> call dbms_ccl.add_ccl_rule('SELECT', '', '', 10, '');
    Query OK, 0 rows affected (0.00 sec)

2. 增加 SELECT 语句,并在语句中出现关键字 key1 的并发度为 20
    mysql> call dbms_ccl.add_ccl_rule('SELECT', '', '', 20, 'key1');
    Query OK, 0 rows affected (0.00 sec)

3. 增加 test.t 表的 SELECT 语句的并发读为 20;
    mysql> call dbms_ccl.add_ccl_rule('SELECT', 'test', 't', 30, '');
    Query OK, 0 rows affected (0.00 sec)

A matching rule for matching the priority order 3> 2> 1.

2)Delete CCL rule
dbms_ccl.del_ccl_rule(rule_id=> );

Delete rules (including memory and table) For example:

1. 删除 rule id = 15 的 CCL rule
    mysql> call dbms_ccl.del_ccl_rule(15);
    Query OK, 0 rows affected (0.01 sec)

2. 如果删除的rule 不存在,语句报相应的 warning
    mysql> call dbms_ccl.del_ccl_rule(100);
    Query OK, 0 rows affected, 2 warnings (0.00 sec)

    mysql> show warnings;
+---------+------+----------------------------------------------------+
| Level   | Code | Message                                            |
+---------+------+----------------------------------------------------+
| Warning | 7514 | Concurrency control rule 100 is not found in table |
| Warning | 7514 | Concurrency control rule 100 is not found in cache |
+---------+------+----------------------------------------------------+

3) Show CCL rule
dbms_ccl.show_ccl_rule();

Visibility in memory active rule, for example:

mysql> call dbms_ccl.show_ccl_rule();
+------+--------+--------+-------+-------+-------+-------------------+---------+---------+----------+----------+
| ID   | TYPE   | SCHEMA | TABLE | STATE | ORDER | CONCURRENCY_COUNT | MATCHED | RUNNING | WAITTING | KEYWORDS |
+------+--------+--------+-------+-------+-------+-------------------+---------+---------+----------+----------+
|   17 | SELECT | test   | t     | Y     | N     |                30 |       0 |       0 |        0 |          |
|   16 | SELECT |        |       | Y     | N     |                20 |       0 |       0 |        0 | key1     |
|   18 | SELECT |        |       | Y     | N     |                10 |       0 |       0 |        0 |          |
+------+--------+--------+-------+-------+-------+-------------------+---------+---------+----------+----------+

In addition to property rule itself, adding three numbers statistics:

1) MATCHED
rule matching the number of successful
2) RUNNING
Under this rule, being run thread number
3) WAITING
number under this rule are wait thread

 4)Flush CCL rule
dbms_ccl.flush_ccl_rule();

  If the direct operation of the concurrency_control table to change the rules can not take effect immediately, you can call flush, revalidated. E.g:

mysql> update mysql.concurrency_control set CONCURRENCY_COUNT = 15 where id = 18;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> call dbms_ccl.flush_ccl_rule();
Query OK, 0 rows affected (0.00 sec)

pressure test

testing scenarios

1) design three rules
 Rule  1: Application Object rule of sbtest1 table control

call dbms_ccl.add_ccl_rule('SELECT', 'test', 'sbtest1', 3, '');

Rule  2: Table of sbtest2 keyword rule application control

call dbms_ccl.add_ccl_rule('SELECT', '', '', 2, 'sbtest2');

Rule  3: the Command control sbtest3 apply SQL table

call dbms_ccl.add_ccl_rule('SELECT', '', '', 2, '');

2) Use test sysbench

  • 64 threads
  • 4 tables
  • select.lua

View concurrent usage rules, you can go to a number of running and waiting:

mysql> call dbms_ccl.show_ccl_rule();
+------+--------+--------+---------+-------+-------+-------------------+---------+---------+----------+----------+
| ID   | TYPE   | SCHEMA | TABLE   | STATE | ORDER | CONCURRENCY_COUNT | MATCHED | RUNNING | WAITTING | KEYWORDS |
+------+--------+--------+---------+-------+-------+-------------------+---------+---------+----------+----------+
|   20 | SELECT | test   | sbtest1 | Y     | N     |                 3 |     389 |       3 |        9 |          |
|   21 | SELECT |        |         | Y     | N     |                 2 |     375 |       2 |       14 | sbtest2  |
|   22 | SELECT |        |         | Y     | N     |                 2 |     519 |       2 |       34 |          |
+------+--------+--------+---------+-------+-------+-------------------+---------+---------+----------+----------+
3 rows in set (0.00 sec)

View thread operation: in most Concurrency control waitting state.

mysql> show processlist;
+-----+-----------------+-----------------+------+---------+------+------------------------------+--------------------------------------+
| Id  | User            | Host            | db   | Command | Time | State                        | Info                                 |
+-----+-----------------+-----------------+------+---------+------+------------------------------+--------------------------------------+
|  72 | root            | localhost:33601 | NULL | Query   |    0 | starting                     | show processlist                     |
| 171 | u1              | localhost:60120 | test | Query   |    2 | Concurrency control waitting | SELECT pad FROM sbtest3 WHERE id=51 |
| 172 | u1              | localhost:60128 | test | Query   |    5 | Concurrency control waitting | SELECT pad FROM sbtest4 WHERE id=35 |
| 174 | u1              | localhost:60385 | test | Query   |    4 | Concurrency control waitting | SELECT pad FROM sbtest3 WHERE id=54 |
| 178 | u1              | localhost:60136 | test | Query   |   12 | Concurrency control waitting | SELECT pad FROM sbtest4 WHERE id=51 |
| 179 | u1              | localhost:60149 | test | Query   |    5 | Concurrency control waitting | SELECT pad FROM sbtest2 WHERE id=51 |
| 182 | u1              | localhost:60124 | test | Query   |    1 | Concurrency control waitting | SELECT pad FROM sbtest4 WHERE id=51 |
| 183 | u1              | localhost:60371 | test | Query   |    5 | User sleep                   | SELECT pad FROM sbtest2 WHERE id=51 |
| 184 | u1              | localhost:60133 | test | Query   |    4 | Concurrency control waitting | SELECT pad FROM sbtest3 WHERE id=51 |
| 190 | u1              | localhost:60406 | test | Query   |    5 | Concurrency control waitting | SELECT pad FROM sbtest3 WHERE id=51 |
| 191 | u1              | localhost:60402 | test | Query   |    1 | Concurrency control waitting | SELECT pad FROM sbtest4 WHERE id=51 |
| 192 | u1              | localhost:60131 | test | Query   |    2 | User sleep                   | SELECT pad FROM sbtest1 WHERE id=51 |

......

Use rules and risk

  1. Concurrency_control is designed not to produce BINLOG, so for the operation of the CCL only to the current instance.
  2. For DML concurrency control, the presence of a transaction may lock the lock, in addition to CCL provides a time-out mechanism,
    while waiting thread will respond to KILL transaction timeout and thread operations to deal with the deadlock is possible.


Original link
This article Yunqi community original content may not be reproduced without permission.

Guess you like

Origin blog.csdn.net/weixin_43970890/article/details/93084780