规则设计

CCL 规则一共定义了三个维度的特征:
1)SQL command
根据 statement 的类型,例如 ‘SELECT’, ‘UPDATE’, ‘INSERT’, ‘DELETE’;
2) Object
根据 statement 操作的对象进行控制, 例如 TABLE,VIEW;
3)keywords
根据 statement 语句的关键字进行控制;

CCL 根据规则的定义,设计了一个系统表,mysql.concurrency_control 持久化保存 CCL rule:

  • “Type”
  • 用来定义 SQL command
  • “Schema_name” && “Table_name”
  • 用来定义 Object
  • “Keywords”
  • 用来定义关键字,可使用 ‘;’ 分隔符多个关键字
  • “Concurrency_count”
  • 用来定义并发度
  • “State”
  • 表示这条规则是否 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. mysql> call dbms_ccl.add_ccl_rule('SELECT', '', '', 10, '');
  2. Query OK, 0 rows affected (0.00 sec)
  3. 2. 增加 SELECT 语句,并在语句中出现关键字 key1 的并发度为 20
  4. mysql> call dbms_ccl.add_ccl_rule('SELECT', '', '', 20, 'key1');
  5. Query OK, 0 rows affected (0.00 sec)
  6. 3. 增加 test.t 表的 SELECT 语句的并发读为 20
  7. mysql> call dbms_ccl.add_ccl_rule('SELECT', 'test', 't', 30, '');
  8. Query OK, 0 rows affected (0.00 sec)

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

删除规则(包括内存和表中)例如:

  1. 1. 删除 rule id = 15 CCL rule
  2. mysql> call dbms_ccl.del_ccl_rule(15);
  3. Query OK, 0 rows affected (0.01 sec)
  4. 2. 如果删除的rule 不存在,语句报相应的 warning
  5. mysql> call dbms_ccl.del_ccl_rule(100);
  6. Query OK, 0 rows affected, 2 warnings (0.00 sec)
  7. mysql> show warnings;
  8. +---------+------+----------------------------------------------------+
  9. | Level | Code | Message |
  10. +---------+------+----------------------------------------------------+
  11. | Warning | 7514 | Concurrency control rule 100 is not found in table |
  12. | Warning | 7514 | Concurrency control rule 100 is not found in cache |

3) Show CCL rule
dbms_ccl.show_ccl_rule();

展示在内存中 active rule 的情况,例如:

除了 rule 本身的属性之外,增加了三个数字统计:

1)MATCHED
规则匹配成功次数
2)RUNNING
在此规则下,正在 run 的线程数
3)WAITING
在此规则下,正在 wait的线程数

如果直接操作了concurrency_control table 修改规则, 不能立即生效,可以调用 flush,重新生效。例如:

  1. mysql> update mysql.concurrency_control set CONCURRENCY_COUNT = 15 where id = 18;
  2. Query OK, 1 row affected (0.00 sec)
  3. mysql> call dbms_ccl.flush_ccl_rule();
  4. Query OK, 0 rows affected (0.00 sec)

测试场景

1)设计三条规则
Rule 1:对 sbtest1 表 应用 Object rule 控制

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

Rule 2: 对sbtest2 表 应用 keyword rule 控制

Rule 3: 对sbtest3 表 应用 SQL command 控制

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

2)使用 sysbench 进行测试

  • 64 threads
  • 4 tables
  • select.lua
  1. mysql> call dbms_ccl.show_ccl_rule();
  2. +------+--------+--------+---------+-------+-------+-------------------+---------+---------+----------+----------+
  3. | ID | TYPE | SCHEMA | TABLE | STATE | ORDER | CONCURRENCY_COUNT | MATCHED | RUNNING | WAITTING | KEYWORDS |
  4. +------+--------+--------+---------+-------+-------+-------------------+---------+---------+----------+----------+
  5. | 20 | SELECT | test | sbtest1 | Y | N | 3 | 389 | 3 | 9 | |
  6. | 21 | SELECT | | | Y | N | 2 | 375 | 2 | 14 | sbtest2 |
  7. | 22 | SELECT | | | Y | N | 2 | 519 | 2 | 34 | |
  8. +------+--------+--------+---------+-------+-------+-------------------+---------+---------+----------+----------+

查看线程运行情况: 大部分处在 Concurrency control waitting 状态。

  1. Concurrency_control 被设计成不产生 BINLOG,所以对于 CCL 的操作只影响当前实例。