## 模块 2：通过每秒请求单位配额控制 TiDB 服务器运行队列

  ![40%](https://lab-static.pingcap.com/quick-demo/demo-006-1-en.png)

### 步骤 1. 设置用户的 RU 配额

+ 在 [终端 1](ssh://monitor1-ssh#1), 查看并运行脚本，为 `userA` 和 `userB` 设置不同的每秒 RU 配额资源组。

  ```bash
  cat lab1-rc-setup.sh
  ```

  ```output
  #!/bin/bash

  source ./hosts-env.sh

  # For cluster
  export MYSQL_PS1="tidb:db1> "
  mysql -h ${HOST_DB1_PRIVATE_IP} -P 4000 -u root --verbose << 'EOFX'
  ALTER USER userA RESOURCE GROUP default;
  ALTER USER userB RESOURCE GROUP default;
  DROP RESOURCE GROUP IF EXISTS rg1;
  DROP RESOURCE GROUP IF EXISTS rg2;
  CREATE RESOURCE GROUP rg1 RU_PER_SEC = 500;
  CREATE RESOURCE GROUP rg2 RU_PER_SEC = 250;
  ALTER USER userA RESOURCE GROUP rg1;
  ALTER USER userB RESOURCE GROUP rg2;
  SELECT * FROM information_schema.resource_groups;
  SELECT Host, user, User_attributes FROM mysql.user WHERE user IN ('userA','userB');
  EOFX
  ```

  ```bash
  ./lab1-rc-setup.sh
  ```

  ```output
  ...

  --------------
  SELECT * FROM information_schema.resource_groups
  --------------

  NAME	RU_PER_SEC	PRIORITY	BURSTABLE	QUERY_LIMIT	BACKGROUND
  default	UNLIMITED	MEDIUM	YES	NULL	NULL
  rg1	500	MEDIUM	NO	NULL	NULL
  rg2	250	MEDIUM	NO	NULL	NULL
  --------------
  SELECT Host, User, User_attributes FROM mysql.user WHERE user IN ('userA','userB')
  --------------

  Host	User	User_attributes
  %	userA	{"resource_group": "rg1"}
  %	userB	{"resource_group": "rg2"}
  ```

### 步骤 2. 启动工作负载

+ 在 [终端 1](ssh://monitor1-ssh#1) 上，运行脚本以启动由 `userA` 执行的 OLTP 工作负载。让脚本运行并继续下一步。

  ```bash
  ./lab1-workload-userA.sh
  ```

+ 在 [终端 2](ssh://monitor1-ssh#2) 上，运行脚本以启动由 `userB` 执行的 OLTP 工作负载。让脚本运行并继续下一步。

   ```bash
   ./lab1-workload-userB.sh
   ```

### 步骤 3. 观察结果

+ 等待大约 1 分钟，观察 `userA` 的 QPS 高于 `userB` 的 QPS，因为 `userA` 被分配了更多的每秒请求单位（RU_PER_SEC）配额。

    ![60%](https://lab-static.pingcap.com/quick-demo/demo-006-lab-2-1.png)

+ 使用用户名和密码 `admin/admin` 登录 [Grafana Prometheus Dashboard](http://${LAB:HOST_MONITOR1_PUBLIC_IP}:3000)，找到 `tidb-demo-TiDB-Resource-Control` -> `Resource Unit` -> `RU` 面板，并确认 `rg1` 的 `RU_PER_SEC` 高于 `rg2`。

    ![60%](https://lab-static.pingcap.com/quick-demo/demo-006-lab-2.png)

+ 在继续下一个模块之前，按 <kbd>Ctrl</kbd>+<kbd>C</kbd> 停止两个工作负载。
