## Module 5: Scale-in and Scale-out the TiDB Data Migration Cluster

### Step 1. Check the status of the deployed `TiDB Data Migration` cluster

  ```bash
  tiup dm display dm-test
  ```

  ```output
  tiup is checking updates for component dm ...
  Starting component `dm`: /home/ec2-user/.tiup/components/dm/v1.12.1/tiup-dm display dm-test
  Cluster type:       dm
  Cluster name:       dm-test
  Cluster version:    v7.5.1
  Deploy user:        tidb
  SSH type:           builtin
  Grafana URL:        http://10.90.4.231:3000
  ID                Role          Host         Ports      OS/Arch       Status     Data Dir                      Deploy Dir
  --                ----          ----         -----      -------       ------     --------                      ----------
  10.90.4.231:9093  alertmanager  10.90.4.231  9093/9094  linux/x86_64  Up         /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
  10.90.1.34:8261   dm-master     10.90.1.34   8261/8291  linux/x86_64  Healthy|L  /tidb-data/dm-master-8261     /tidb-deploy/dm-master-8261
  10.90.2.38:8261   dm-master     10.90.2.38   8261/8291  linux/x86_64  Healthy    /tidb-data/dm-master-8261     /tidb-deploy/dm-master-8261
  10.90.3.188:8261  dm-master     10.90.3.188  8261/8291  linux/x86_64  Healthy    /tidb-data/dm-master-8261     /tidb-deploy/dm-master-8261
  10.90.1.162:8262  dm-worker     10.90.1.162  8262       linux/x86_64  Bound      /tidb-data/dm-worker-8262     /tidb-deploy/dm-worker-8262
  10.90.2.108:8262  dm-worker     10.90.2.108  8262       linux/x86_64  Bound      /tidb-data/dm-worker-8262     /tidb-deploy/dm-worker-8262
  10.90.3.12:8262   dm-worker     10.90.3.12   8262       linux/x86_64  Free       /tidb-data/dm-worker-8262     /tidb-deploy/dm-worker-8262
  10.90.4.231:3000  grafana       10.90.4.231  3000       linux/x86_64  Up         -                             /tidb-deploy/grafana-3000
  10.90.4.231:9090  prometheus    10.90.4.231  9090       linux/x86_64  Up         /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
  Total nodes: 9
  ```

### Step 2. Scale in the DM cluster

+ Run the `tiup dm scale-in` command to scale in the `TiDB Data Migration` cluster, and take the `DM-worker` node with `KV server 1` IP address offline.

  ```bash
  tiup dm scale-in dm-test -N ${HOST_KV1_PRIVATE_IP}:8262
  ```

  ```output
  ...
  Do you want to continue? [y/N]:(default=N) y
  ...
  Scaled cluster `dm-test` in successfully
  ```

### Step 3. Check the status of the deployed `TiDB Data Migration` cluster again

  ```bash
  tiup dm display dm-test
  ```

  ```output
  tiup is checking updates for component dm ...
  Starting component `dm`: /home/ec2-user/.tiup/components/dm/v1.12.1/tiup-dm display dm-test
  Cluster type:       dm
  Cluster name:       dm-test
  Cluster version:    v7.5.1
  Deploy user:        tidb
  SSH type:           builtin
  Grafana URL:        http://10.90.4.231:3000
  ID                Role          Host         Ports      OS/Arch       Status     Data Dir                      Deploy Dir
  --                ----          ----         -----      -------       ------     --------                      ----------
  10.90.4.231:9093  alertmanager  10.90.4.231  9093/9094  linux/x86_64  Up         /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
  10.90.1.34:8261   dm-master     10.90.1.34   8261/8291  linux/x86_64  Healthy|L  /tidb-data/dm-master-8261     /tidb-deploy/dm-master-8261
  10.90.2.38:8261   dm-master     10.90.2.38   8261/8291  linux/x86_64  Healthy    /tidb-data/dm-master-8261     /tidb-deploy/dm-master-8261
  10.90.3.188:8261  dm-master     10.90.3.188  8261/8291  linux/x86_64  Healthy    /tidb-data/dm-master-8261     /tidb-deploy/dm-master-8261
  10.90.1.162:8262  dm-worker     10.90.1.162  8262       linux/x86_64  Bound      /tidb-data/dm-worker-8262     /tidb-deploy/dm-worker-8262
  10.90.3.12:8262   dm-worker     10.90.3.12   8262       linux/x86_64  Bound      /tidb-data/dm-worker-8262     /tidb-deploy/dm-worker-8262
  10.90.4.231:3000  grafana       10.90.4.231  3000       linux/x86_64  Up         -                             /tidb-deploy/grafana-3000
  10.90.4.231:9090  prometheus    10.90.4.231  9090       linux/x86_64  Up         /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
  Total nodes: 8
  ```

### Step 4. Prepare the `dm-scale.yaml` file

+ Check the contents in the `dm-scale.yaml` file.

  ```bash
  cat solution-dm-scale.yaml
  ```

  ```output
  worker_servers:
  - host: ${LAB:HOST_KV1_PRIVATE_IP}
  ```

### Step 5. Scale out the DM cluster

  ```bash
  tiup dm --user ec2-user -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem scale-out dm-test solution-dm-scale.yaml
  ```

  ```output
  ...
  Do you want to continue? [y/N]: (default=N) y
  ...
  Scaled cluster `dm-test` out successfully
  ```

### Step 6. Check the status of the deployed `TiDB Data Migration` cluster again

  ```bash
  tiup dm display dm-test
  ```

### Step 7. Execute the `stop-task` command to stop data migration tasks

  ```bash
  tiup dmctl:v7.5.1 --master-addr=${HOST_PD3_PRIVATE_IP}:8261 stop-task dm-task.yaml
  ```

  ```output
  tiup is checking updates for component dmctl ...
  Starting component `dmctl`: /home/ec2-user/.tiup/components/dmctl/v7.5.1/dmctl/dmctl --master-addr=10.90.2.38:8261 stop-task dm-task.yaml
  {
        "op": "Delete",
        "result": true,
        "msg": "",
        "sources": [
           {
              "result": true,
              "msg": "",
              "source": "mysql-replica-01",
              "worker": "dm-10.90.3.12-8262"
           },
           {
              "result": true,
              "msg": "",
              "source": "mysql-replica-02",
              "worker": "dm-10.90.1.162-8262"
           }
        ]
  }
  ```

### Step 8. Verify that the DM cluster data migration has stopped

  ```bash
  tiup dmctl:v7.5.1 --master-addr=${HOST_PD3_PRIVATE_IP}:8261 query-status dm-task.yaml
  ```

  ```output
  Starting component `dmctl`: /home/ec2-user/.tiup/components/dmctl/v7.5.1/dmctl/dmctl --master-addr=10.90.2.38:8261 query-status dm-task.yaml
  {
        "result": false,
        "msg": "task dm-task has no source or not exist",
        "sources": [
        ]
  }
  ```

### Step 9. Stop the DM cluster

  ```bash
  tiup dm stop dm-test
  ```

  ```output
  ...
  Do you want to continue? [y/N]:(default=N) y
  ...
  Stopped cluster `dm-test` successfully
  ```

### Step 10. Check the status of the `TiDB Data Migration` cluster

  ```bash
  tiup dm display dm-test
  ```

### Step 11. Destroy your DM cluster

  ```bash
  tiup dm destroy dm-test
  ```
