## Module 3: Control The TiDB Cluster Scheduling Queue By Using Resource Group Priorities

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

### Step 1. Set resource group priorities for users

+ On [Terminal 1](ssh://monitor1-ssh#1), review and run the script to set up resource groups for `userA` as `(priority:HIGH)` and `userB` as `(priority:MEDIUM)`.

  ```bash
  cat lab2-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=50000 PRIORITY=HIGH;
  CREATE RESOURCE GROUP rg2 RU_PER_SEC=50000 PRIORITY=MEDIUM;
  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
  ./lab2-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	50000	HIGH	NO	NULL	NULL
  rg2	50000	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 a batch workload by `userA` (no output). Keep the script running and proceed to the next step.

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

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

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

### Step 3. Observe the result

+ Wait for 1 minute, log in to the [Grafana Prometheus Dashboard](http://${LAB:HOST_MONITOR1_PUBLIC_IP}:3000), find `tidb-demo-TiDB-Resource-Control` -> `Resource Details` -> `KV CPU Time`, confirm that `rg1` is capable of consuming more `CPU Time` than `rg2`.

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

+ Press <kbd>Ctrl</kbd>+<kbd>C</kbd> to stop both workloads before proceeding to the next module.
