## 模块 3: 开启 TiKV 集群的平衡调整其速率

+ 本模块中，参与者会解决上一模块中，TiKV 集群扩容后没有进行平衡的问题，并且调整其速率。

### 步骤 1. 调查 PD 是否开启了平衡调度功能，并开启

+ 添加 PD 组件的 `balance-region-scheduler`。

   ```bash
   ~/.tiup/bin/tiup ctl:v${LAB:TIDB_VERSION} pd -u http://${HOST_PD1_PRIVATE_IP}:2379 scheduler show
   ```

   ```output
   Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.178:2379 scheduler show
   [
   "evict-slow-store-scheduler",
   "balance-leader-scheduler",
   "balance-hot-region-scheduler"
   ]
   ```

   ```bash
   ~/.tiup/bin/tiup ctl:v${LAB:TIDB_VERSION} pd -u http://${HOST_PD1_PRIVATE_IP}:2379 scheduler add balance-region-scheduler
   ```

   ```output
   Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.178:2379 scheduler add balance-region-scheduler
   Success! The scheduler is created.
   ```

   ```bash
   ~/.tiup/bin/tiup ctl:v${LAB:TIDB_VERSION} pd -u http://${HOST_PD1_PRIVATE_IP}:2379 scheduler show
   ```

   ```output
   Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.178:2379 scheduler show
   [
   "balance-region-scheduler",
   "balance-leader-scheduler",
   "balance-hot-region-scheduler",
   "evict-slow-store-scheduler"
   ]
   ```

+ 登录 [Grafana 监控](http://${LAB:HOST_MONITOR1_PUBLIC_IP}:3000)，进入`Home`，然后选择`tidb-demo`，接着选择`tidb-demo-PD`。 在`tidb-demo-PD`下半部分，打开 `Scheduler` 标签，可以看到 PD 组件的 `balance-region-scheduler` 已经被加入。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/2ad5a2ececf1dc8cac303e828270f34539539187.png)

+ 等待 30 秒，在`tidb-demo-PD`上半部分，可以看到 `Online store progress` 为 `No Data`，`Left time` 为 `N/A`，`Current scaling speed` 为 `0 MB/s`。 这表示平衡依然没有开始，接下来继续进行调查。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/adb4348802beb9d2722289e55e99d477906e6bc8.png)

### 步骤 2. 调查 `region-schedule-limit` 是否为 `0`，并进行设置

+ 在`tidb-demo-PD`下半部分，打开 `Operator` 标签，可以看到 `Scheduler operator create` 面板和 `Scheduler operator finish` 面板均显示没有 `balance-region-scheduler` 的 operator 产生并被执行。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/30ff56c740955c7475e0c10380d510e33a371e11.png)

+ 调整 `region-schedule-limit` 为 `2048`。

   ```bash
   ~/.tiup/bin/tiup ctl:v${LAB:TIDB_VERSION} pd -u http://${HOST_PD1_PRIVATE_IP}:2379 config set region-schedule-limit 2048
   ```

   ```output
   Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.178:2379 config set region-schedule-limit 2048
   Success!
   ```

+ 回到 [Grafana 监控](http://${LAB:HOST_MONITOR1_PUBLIC_IP}:3000) 的 `tidb-demo-PD`，等待 30 秒，在`tidb-demo-PD`下半部分，打开 `Operator` 标签，可以看到 `Scheduler operator create` 面板和 `Scheduler operator finish` 面板均显示有 `balance-region-scheduler` 的 operator 产生并被执行。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/cd10d370eb1503fb94dbf7f852b70901556ab7e8.png)

+ 等待，在`tidb-demo-PD`上半部分，可以看到 `Online store progress`，`Left time` 和 `Current scaling speed` 均有数值。 这表示平衡已经开始，但此时平衡的速率非常缓慢，`Left time` 也显示需要很长时间才会完成。接下来需要调高平衡的速率。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/19b35aa528cf6ce53e18c83c652246dd38d60444.png)

+ 关于加入 `balance-region-scheduler` 和设置 `region-schedule-limit` 后为何会开启数据平衡的原因，请参考模块 1 的 步骤 8.

+ 在 `tidb-demo-TiKV-Details` 的 `Cluster` 标签，可以看到各个 TiKV 节点的 IO 使用率 `IO utillzation` 目前比较低。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/c42203151614b13996cf5d789006b2f35e48ba10.png)

### 步骤 3. 对 TiKV 集群扩容后的平衡进行加速

+ 在`tidb-demo-PD`上半部分，等待 `Online store progress` 超过 10% 后（注意：建议在 `Online store progress` 为 10% 到 20% 间开启此步骤， 因为如果超过 20%， 可能没有时间进行之后的步骤）, 将所有 TiKV 节点的 `store limit` 的项目从 1 设置为 5 来提高 TiKV 执行平衡的速率。

   ```bash
   ~/.tiup/bin/tiup ctl:v${LAB:TIDB_VERSION} pd -u http://${HOST_PD1_PRIVATE_IP}:2379 store limit all 5
   ```

   ```output
   Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.178:2379 store limit all 5
   Success! Set store limit successfully.
   ```

+ 确认所有 TiKV 节点的 `store limit` 的项目已经被设置为 `5`。

   ```bash
   ~/.tiup/bin/tiup ctl:v${LAB:TIDB_VERSION} pd -u http://${HOST_PD1_PRIVATE_IP}:2379 store limit
   ```

   ```output
   Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.178:2379 store limit
   {
      "1": {
         "add-peer": 5,
         "remove-peer": 5
      },
      "4": {
         "add-peer": 5,
         "remove-peer": 5
      },
      "576": {
         "add-peer": 5,
         "remove-peer": 5
      },
      "7": {
         "add-peer": 5,
         "remove-peer": 5
      }
   }
   ```

+ 回到 [Grafana 监控](http://${LAB:HOST_MONITOR1_PUBLIC_IP}:3000) 的 `tidb-demo-PD`，等待 10 秒，在`tidb-demo-PD` 的 `Operator` 标签，可以看到 `Scheduler operator create` 面板和 `Scheduler operator finish` 面板中 `balance-region-scheduler` 的速率开始上升，这是因为 TiKV 执行 Operator 的效率变高。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/cfac45c51fe095c95ee4be3df34f923994e4ec52.png)

+ 在 `tidb-demo-TiKV-Details` 的 `Cluster` 标签，可以看到各个 TiKV 节点的 IO 使用率 `IO utillzation` 开始上升， 这也表示新加入的 TiKV 节点和被选为源的 TiKV 节点会将 IO 资源用在平衡上， 这有可能会对现有的业务负载造成影响。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/c6d214e68fd635c0438e7915dc837424814fd5cd.png)

+ 等待，观察在`tidb-demo-PD`上半部分，可以看到 `Online store progress` 的百分比在加快上升，`Left time` 的数值在变小， `Current scaling speed` 上升。 这表示 Store limit 的增加可以加快平衡的速率。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/2e3208e8936fd688605f28771aa8346ee0b4ed23.png)

### 步骤 4. 对 TiKV 集群扩容后的平衡进行减速

+ 在`tidb-demo-PD`上半部分，等待 `Online store progress` 超过 50% 后（注意：建议在 `Online store progress` 为 50% 到 60% 间开启此步骤， 因为如果超过 60%， 可能没有时间进行之后的步骤），开始降低平衡的速率。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/1bf8559bc784ca742d1fec69d1e80f509b3a145f.png)

+ 将所有 TiKV 节点的 `store limit` 的项目设置为 `1` 来降低 TiKV 执行平衡的速率。

   ```bash
   ~/.tiup/bin/tiup ctl:v${LAB:TIDB_VERSION} pd -u http://${HOST_PD1_PRIVATE_IP}:2379 store limit all 1
   ```

   ```output
   Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.178:2379 store limit all 1
   Success! Set store limit successfully.
   ```

+ 确认所有 TiKV 节点的 `store limit` 的项目已经被设置为 `1`。

   ```bash
   ~/.tiup/bin/tiup ctl:v${LAB:TIDB_VERSION} pd -u http://${HOST_PD1_PRIVATE_IP}:2379 store limit
   ```

   ```output
   Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.178:2379 store limit
   {
      "1": {
         "add-peer": 1,
         "remove-peer": 1
      },
      "4": {
         "add-peer": 1,
         "remove-peer": 1
      },
      "576": {
         "add-peer": 1,
         "remove-peer": 1
      },
      "7": {
         "add-peer": 1,
         "remove-peer": 1
      }
   }
   ```

+ 回到 [Grafana 监控](http://${LAB:HOST_MONITOR1_PUBLIC_IP}:3000) 的 `tidb-demo-PD`，等待 10 秒，在`tidb-demo-PD` 的 `Operator` 标签，可以看到 `Scheduler operator create` 面板和 `Scheduler operator finish` 面板中 `balance-region-scheduler` 的速率开始下降，这是因为 TiKV 执行 Operator 的效率变低。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/247a958930cd5b9cb9c197cd48a45d6609cc2c6b.png)

+ 在 `tidb-demo-TiKV-Details` 的 `Cluster` 标签，可以看到各个 TiKV 节点的 IO 使用率 `IO utillzation` 开始下降， 这也表示新加入的 TiKV 节点和被选为源的 TiKV 节点会将少量 IO 资源用在平衡上， 这可能会避免对现有的业务负载造成影响，但扩容后的平衡速率会变得缓慢。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/27fd326a8002e4739f98bbdba4e5a5c538fb5666.png)

+ 等待，观察在`tidb-demo-PD`上半部分，可以看到 `Online store progress` 的百分比上升速度开始变慢，`Left time` 的数值在变长， `Current scaling speed` 开始下降。 这表示 Store limit 的降低可以减慢平衡的速率。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/2c3f49858b070480f48a2cab770bae758732cdc1.png)

### 步骤 5. 等待 TiKV 集群扩容后的平衡完成

+ 继续等待，并观察在`tidb-demo-PD`上半部分的 `Online store progress`，`Left time` 和 `Current scaling speed` 的变化，直到 `Online store progress` 为 `No data`, 表示扩容正式完成。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/d744c40ef5e03fe774d2abb27ec1145153fd65ed.png)

+ 也可以尝试将 `store limit` 调整到较大的值，比如 `20`，进行平衡的提速。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/800a2d65b07bf6bb389bc4361446833640c98b22.png)

+ 最终，`Online store progress` 为 `No data`, 表示扩容正式完成。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/60f7c026e5b42076f2d766b1f8a3656d55f476d5.png)

+ 在 `tidb-demo-TiKV-Details` 的 `Cluster` 标签找到  `IO utillzation` 面板，放大时间范围到整个扩容的过程， 会发现 TiKV 集群的负载情况根据 `store limit` 值进行变化。

  ![70%](https://lab-static.pingcap.com/images/2025/7/24/8420e7c08ed1d71db8bd7cd9475d4b1834da1474.png)
