## Module 2: Control The TiDB Server Run Queue By Using Request Unit Per Seconds Quota

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

### Step 1. Set RU quota for users

+ On [Terminal 1](ssh://monitor1-ssh#1), review and run the script to set up resource groups for `userA` and `userB` with a different quota on RU per seconds.

  ```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"}
  ```

### Step 2. Start the workloads

+ On [Terminal 1](ssh://monitor1-ssh#1), run the script to start an OLTP workload by `userA`. Keep the script running and proceed to the next step.

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

+ On [Terminal 2](ssh://monitor1-ssh#2), run the script to start an OLTP workload by `userB`. Keep the script running and proceed to the next step.

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

### Step 3. Observe the result

+ Wait for about 1 minute, observe that QPS for `userA` is higher than the QPS for `userB` because `userA` had been assigned with more `RU_PER_SEC` quota.

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

+ Log in to the [Grafana Prometheus Dashboard](http://${LAB:HOST_MONITOR1_PUBLIC_IP}:3000) with username and password as `admin` and `admin`, find `tidb-demo-TiDB-Resource-Control` -> `Resource Unit` -> `RU` panel and confirm `rg1` has higher `RU_PER_SEC` than rg2.

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

+ Press <kbd>Ctrl</kbd>+<kbd>C</kbd> to stop both workloads from [Terminal 1](ssh://monitor1-ssh#1) and [Terminal 2](ssh://monitor1-ssh#2) before proceeding to the next module.
