RDS MySQL 8.0 語句級併發控制

RDS MySQL 8.0 語句級併發控制

 

背景

爲了應對突發的數據庫請求流量,資源消耗過載的語句訪問,SQL 訪問模型的變化, 並保持 MySQL 實例持續穩定運行,AliSQL 版本設計了基於語句規則的併發控制,Statement Concurrency Control,如下簡稱 CCL,有效控制匹配某種規則的併發度,並提供了一組工具包(DBMS_CCL package) 方便快捷使用。mysql

 

規則設計

CCL 規則一共定義了三個維度的特徵:
1)SQL command
根據 statement 的類型,例如 ‘SELECT', 'UPDATE', 'INSERT', 'DELETE';
2)  Object
根據 statement 操做的對象進行控制, 例如 TABLE,VIEW;
3)keywords
根據 statement 語句的關鍵字進行控制;sql

CCL 根據規則的定義,設計了一個系統表,mysql.concurrency_control 持久化保存 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"併發

    • 用來定義 SQL command
  • "Schema_name" && "Table_name"工具

    • 用來定義 Object
  • "Keywords"測試

    • 用來定義關鍵字,可以使用 ';' 分隔符多個關鍵字
  • "Concurrency_count"lua

    • 用來定義併發度
  • "State"spa

    • 表示這條規則是否 active
  • "Ordered"線程

    • 表示keywords中多個關鍵字是否按順序匹配

用戶能夠直接操做這個表來定義規則,也能夠使用 DBMS_CCL 工具包來操做 CCL rule。設計

 

管理接口

爲了便捷的管理 CCL rule,AliSQL 在 DBMS_CCL package 中定義了四個 native procedure 來管理;

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

增長規則(包括表和內存)例如:

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)

規則的匹配按照 3 > 2 > 1 的優先級順序進行匹配。

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

刪除規則(包括內存和表中)例如:

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();

展現在內存中 active  rule 的狀況,例如:

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 |          |
+------+--------+--------+-------+-------+-------+-------------------+---------+---------+----------+----------+

除了 rule 自己的屬性以外,增長了三個數字統計:

1)MATCHED
規則匹配成功次數
2)RUNNING
在此規則下,正在 run 的線程數
3)WAITING
在此規則下,正在 wait的線程數

 4)Flush CCL rule
dbms_ccl.flush_ccl_rule();

  若是直接操做了concurrency_control table 修改規則, 不能當即生效,能夠調用 flush,從新生效。例如:

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)

 

壓力測試

測試場景

1)設計三條規則
 Rule 1:對 sbtest1 表 應用 Object rule 控制

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

Rule 2: 對sbtest2 表 應用 keyword rule 控制

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

Rule 3: 對sbtest3 表 應用 SQL command 控制

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

2)使用 sysbench 進行測試

  • 64 threads
  • 4 tables
  • select.lua

查看規則併發使用狀況,能夠到到 running 和 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)

查看線程運行狀況: 大部分處在 Concurrency control waitting 狀態。

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 |

......

 

使用規則和風險

  1. Concurrency_control 被設計成不產生 BINLOG,因此對於 CCL 的操做隻影響當前實例。
  2. 對於 DML 的併發控制,可能存在事務鎖死鎖的狀況, 除了 CCL 提供了超時機制,
    同時等待中的線程也會響應事務超時和線程 KILL 操做,以應對死鎖可能。

 

本文做者:Ali_冷香

原文連接

本文爲雲棲社區原創內容,未經容許不得轉載。

相關文章
相關標籤/搜索